Last active
October 4, 2021 07:21
-
-
Save sachith-1/ad203d8bc260f7a74b1f50e1480be2ef 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> | |
#define N 200 | |
void dataToArray(int* a, int n){ | |
int i; | |
for (i = 0; i < n; ++i){ | |
a[i] = i; | |
} | |
} | |
__global__ void getSum(int *n1, int *n2, int *sumData,int size) { | |
int index = threadIdx.x + blockIdx.x * blockDim.x; | |
if(index<size){ | |
sumData[index] = n1[index] + n2[index]; | |
} | |
} | |
int main() { | |
int *n1, *n2, *sum; | |
int *dN1, *dN2, *dSum; | |
int size = sizeof(int) * N; | |
// allocate device memory | |
cudaMalloc((void **)&dN1, size); | |
cudaMalloc((void **)&dN2, size); | |
cudaMalloc((void **)&dSum, size); | |
// allocate host memory | |
n1 = (int *)malloc(size); | |
n2 = (int *)malloc(size); | |
sum = (int *)malloc(size); | |
// add data to arrays | |
dataToArray(n1,N); | |
dataToArray(n2,N); | |
//copy from host memory to device memory | |
cudaMemcpy(dN1, n1, size, cudaMemcpyHostToDevice); | |
cudaMemcpy(dN2, n2, size, cudaMemcpyHostToDevice); | |
dim3 Dg(N/5); | |
dim3 Db(5); | |
getSum << <Dg, Db >> > (dN1, dN2, dSum,N); | |
cudaMemcpy(sum, dSum, size, cudaMemcpyDeviceToHost); | |
for(int i=0;i<N;i++){ | |
printf("n1[%d] + n2[%d] = %d\n", i,i,sum[i]); | |
} | |
free(n1); | |
free(n2); | |
free(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