#include #include __global__ void add(int *a, int *b, int *c) { *c = *a + *b; } int main(int argc, char** argv) { int a, b, c; // host copies of a, b, c int *d_a, *d_b, *d_c; // device copies of a, b, c int size = sizeof(int); // Allocate space for device copies of a, b, c fprintf(stdout, "... allocating memory ...\n"); cudaMalloc((void **)&d_a, size); cudaMalloc((void **)&d_b, size); cudaMalloc((void **)&d_c, size); // Setup input values a = 2; b = 7; // Copy inputs to device fprintf(stdout, "... copy from host to the GPU ...\n"); cudaMemcpy(d_a, &a, size, cudaMemcpyHostToDevice); cudaMemcpy(d_b, &b, size, cudaMemcpyHostToDevice); // Launch add() kernel on GPU fprintf(stdout, "... launch the kernel ...\n"); add<<<1,1>>>(d_a, d_b, d_c); cudaDeviceSynchronize(); // Copy result back to host fprintf(stdout, "... copy data from GPU to host ...\n"); cudaMemcpy(&c, d_c, size, cudaMemcpyDeviceToHost); cudaDeviceSynchronize(); // Cleanup fprintf(stdout, "... clean up ...\n"); cudaFree(d_a); cudaFree(d_b); cudaFree(d_c); // print the result // fprintf(stdout, "sum = %d\n", c); return 0; }