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

Function main

cpp/0_Introduction/vectorAddDrv/vectorAddDrv.cpp:75–192  ·  view source on GitHub ↗

Host code

Source from the content-addressed store, hash-verified

73
74// Host code
75int main(int argc, char **argv)
76{
77 printf("Vector Addition (Driver API)\n");
78 int N = 50000, devID = 0;
79 size_t size = N * sizeof(float);
80 CUctxCreateParams ctxCreateParams = {};
81
82 // Initialize
83 checkCudaErrors(cuInit(0));
84
85 cuDevice = findCudaDeviceDRV(argc, (const char **)argv);
86 // Create context
87 checkCudaErrors(cuCtxCreate(&cuContext, &ctxCreateParams, 0, cuDevice));
88
89 // first search for the module path before we load the results
90 string module_path;
91
92 std::ostringstream fatbin;
93
94 if (!findFatbinPath(FATBIN_FILE, module_path, argv, fatbin)) {
95 exit(EXIT_FAILURE);
96 }
97 else {
98 printf("> initCUDA loading module: <%s>\n", module_path.c_str());
99 }
100
101 if (!fatbin.str().size()) {
102 printf("fatbin file empty. exiting..\n");
103 exit(EXIT_FAILURE);
104 }
105
106 // Create module from binary file (FATBIN)
107 checkCudaErrors(cuModuleLoadData(&cuModule, fatbin.str().c_str()));
108
109 // Get function handle from module
110 checkCudaErrors(cuModuleGetFunction(&vecAdd_kernel, cuModule, "VecAdd_kernel"));
111
112 // Allocate input vectors h_A and h_B in host memory
113 h_A = (float *)malloc(size);
114 h_B = (float *)malloc(size);
115 h_C = (float *)malloc(size);
116
117 // Initialize input vectors
118 RandomInit(h_A, N);
119 RandomInit(h_B, N);
120
121 // Allocate vectors in device memory
122 checkCudaErrors(cuMemAlloc(&d_A, size));
123
124 checkCudaErrors(cuMemAlloc(&d_B, size));
125
126 checkCudaErrors(cuMemAlloc(&d_C, size));
127
128 // Copy vectors from host memory to device memory
129 checkCudaErrors(cuMemcpyHtoD(d_A, h_A, size));
130
131 checkCudaErrors(cuMemcpyHtoD(d_B, h_B, size));
132

Callers

nothing calls this directly

Calls 7

cuInitFunction · 0.85
findFatbinPathFunction · 0.85
fabsFunction · 0.85
RandomInitFunction · 0.70
CleanupNoFailureFunction · 0.70
findCudaDeviceDRVFunction · 0.50
sizeMethod · 0.45

Tested by

no test coverage detected