MCPcopy Create free account
hub / github.com/NVIDIA/cuda-samples / main

Function main

cpp/0_Introduction/vectorAdd_nvrtc/vectorAdd.cpp:50–157  ·  view source on GitHub ↗

* Host main routine */

Source from the content-addressed store, hash-verified

48 * Host main routine
49 */
50int 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;

Callers

nothing calls this directly

Calls 4

sdkFindFilePathFunction · 0.85
compileFileToCUBINFunction · 0.85
loadCUBINFunction · 0.85
fabsFunction · 0.85

Tested by

no test coverage detected