* Host main routine */
| 48 | * Host main routine |
| 49 | */ |
| 50 | int main(int argc, char **argv) |
| 51 | { |
| 52 | char *cubin, *kernel_file; |
| 53 | size_t cubinSize; |
| 54 | kernel_file = sdkFindFilePath("vectorAdd_kernel.cu", argv[0]); |
| 55 | compileFileToCUBIN(kernel_file, argc, argv, &cubin, &cubinSize, 0); |
| 56 | CUmodule module = loadCUBIN(cubin, argc, argv); |
| 57 | |
| 58 | CUfunction kernel_addr; |
| 59 | checkCudaErrors(cuModuleGetFunction(&kernel_addr, module, "vectorAdd")); |
| 60 | |
| 61 | // Print the vector length to be used, and compute its size |
| 62 | int numElements = 50000; |
| 63 | size_t size = numElements * sizeof(float); |
| 64 | printf("[Vector addition of %d elements]\n", numElements); |
| 65 | |
| 66 | // Allocate the host input vector A |
| 67 | float *h_A = reinterpret_cast<float *>(malloc(size)); |
| 68 | |
| 69 | // Allocate the host input vector B |
| 70 | float *h_B = reinterpret_cast<float *>(malloc(size)); |
| 71 | |
| 72 | // Allocate the host output vector C |
| 73 | float *h_C = reinterpret_cast<float *>(malloc(size)); |
| 74 | |
| 75 | // Verify that allocations succeeded |
| 76 | if (h_A == NULL || h_B == NULL || h_C == NULL) { |
| 77 | fprintf(stderr, "Failed to allocate host vectors!\n"); |
| 78 | exit(EXIT_FAILURE); |
| 79 | } |
| 80 | |
| 81 | // Initialize the host input vectors |
| 82 | for (int i = 0; i < numElements; ++i) { |
| 83 | h_A[i] = rand() / static_cast<float>(RAND_MAX); |
| 84 | h_B[i] = rand() / static_cast<float>(RAND_MAX); |
| 85 | } |
| 86 | |
| 87 | // Allocate the device input vector A |
| 88 | CUdeviceptr d_A; |
| 89 | checkCudaErrors(cuMemAlloc(&d_A, size)); |
| 90 | |
| 91 | // Allocate the device input vector B |
| 92 | CUdeviceptr d_B; |
| 93 | checkCudaErrors(cuMemAlloc(&d_B, size)); |
| 94 | |
| 95 | // Allocate the device output vector C |
| 96 | CUdeviceptr d_C; |
| 97 | checkCudaErrors(cuMemAlloc(&d_C, size)); |
| 98 | |
| 99 | // Copy the host input vectors A and B in host memory to the device input |
| 100 | // vectors in device memory |
| 101 | printf("Copy input data from the host memory to the CUDA device\n"); |
| 102 | checkCudaErrors(cuMemcpyHtoD(d_A, h_A, size)); |
| 103 | checkCudaErrors(cuMemcpyHtoD(d_B, h_B, size)); |
| 104 | |
| 105 | // Launch the Vector Add CUDA Kernel |
| 106 | int threadsPerBlock = 256; |
| 107 | int blocksPerGrid = (numElements + threadsPerBlock - 1) / threadsPerBlock; |
nothing calls this directly
no test coverage detected