Skip to content

Instantly share code, notes, and snippets.

@Bhavya031
Created September 11, 2023 17:25
Show Gist options
  • Save Bhavya031/c38f94208bd10c8a3a0c5a1298ccc477 to your computer and use it in GitHub Desktop.
Save Bhavya031/c38f94208bd10c8a3a0c5a1298ccc477 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<cuda.h>
__global__ void arradd(int *x,int *y, int *z)
{
int id=blockIdx.x * blockDim.x+threadIdx.x;
/* blockIdx.x gives the respective block id which starts from 0 */
/* threadIdx.x gives the respective thread id which starts from 0 */
/* blockDim.x gives the dimension of block i.e. number of threads in one block */
z[id]=x[id]+y[id];
}
int main()
{
int a[6];
int b[6];
int c[6];
int *d,*e,*f;
int i;
printf("\n Enter six elements of first array\n");
for(i=0;i<6;i++)
{
scanf("%d",&a[i]);
}
printf("\n Enter six elements of second array\n");
for(i=0;i<6;i++)
{
scanf("%d",&b[i]);
}
cudaMalloc((void **)&d,6*sizeof(int));
cudaMalloc((void **)&e,6*sizeof(int));
cudaMalloc((void **)&f,6*sizeof(int));
cudaMemcpy(d,a,6*sizeof(int),cudaMemcpyHostToDevice);
cudaMemcpy(e,b,6*sizeof(int),cudaMemcpyHostToDevice);
arradd<<<2,3>>>(d,e,f);
cudaMemcpy(c,f,6*sizeof(int),cudaMemcpyDeviceToHost);
printf("\nSum of two arrays:\n ");
for(i=0;i<6;i++)
{
printf("%d\t",c[i]);
}
cudaFree(d);
cudaFree(e);
cudaFree(f);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment