Created
February 10, 2021 13:40
-
-
Save sachith-1/b636f3b062d7f3556e3dc1d846a8b3a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "cuda_runtime.h" | |
#include "device_launch_parameters.h" | |
#include <stdio.h> | |
#include <stdlib.h> | |
__global__ void getSum(int *n1, int *n2, int *sumData) { | |
*sumData = *n1 + *n2; | |
} | |
int main() { | |
int n1, n2, sum; | |
int *dN1, *dN2, *dSum; | |
int size = sizeof(int); | |
// allocate device memory | |
cudaMalloc((void **)&dN1, size); | |
cudaMalloc((void **)&dN2, size); | |
cudaMalloc((void **)&dSum, size); | |
n1 = 10; | |
n2 = 15; | |
//copy from host memory to device memory | |
cudaMemcpy(dN1, &n1, size, cudaMemcpyHostToDevice); | |
cudaMemcpy(dN2, &n2, size, cudaMemcpyHostToDevice); | |
dim3 Dg(2, 2, 1); | |
dim3 Db(2, 2, 4); | |
getSum << <Dg, Db >> > (dN1, dN2, dSum); | |
cudaMemcpy(&sum, dSum, size, cudaMemcpyDeviceToHost); | |
printf("result is %d\n", sum); | |
cudaFree(dN1); | |
cudaFree(dN2); | |
cudaFree(dSum); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment