This program is to add two vectors
Add<<<n,1>>> (Dev_a,dev_b,dev_c);//<n,1> The first parameter N represents the number of blocks, and the second parameter 1 represents the number of thread in each block
Tid=blockidx.x;//blockidx is a built-in variable, blockidx.x represents this is a 2-D index
Code:
/*
============================================================================
Name:vectorsum-cuda.cu
Author:can
Version:
Copyright:your Copyright Notice
Description:cuda Compute reciprocals
============================================================================
*/
#include <iostream>
using namespace Std;
#define N 10
__global__ void Add (int *a,int *b,int *c);
static void Checkcudaerroraux (const char *,unsigned, const char *,cudaerror_t);
#define Cuda_check_return (value) Checkcudaerroraux (__file__,__line__, #value, value)
int main ()
{
int a[n],b[n],c[n];
int *dev_a,*dev_b,*dev_c;
for (int i=0;i<n;i++)
{
A[i]=i;
B[i]=i*i;
}
Cuda_check_return (Cudamalloc (void * *) &dev_a,n*sizeof (int));
Cuda_check_return (Cudamalloc (void * *) &dev_b,n*sizeof (int));
Cuda_check_return (Cudamalloc (void * *) &dev_c,n*sizeof (int));
Cuda_check_return (cudamemcpy (dev_a,a,n*sizeof (int), cudamemcpyhosttodevice));
Cuda_check_return (cudamemcpy (dev_b,b,n*sizeof (int), cudamemcpyhosttodevice));
Add<<<n,1>>> (Dev_a,dev_b,dev_c);//<n,1> The first parameter N represents the number of blocks, and the second parameter 1 represents the number of thread in each block
Cuda_check_return (cudamemcpy (c,dev_c,n*sizeof (int), cudamemcpydevicetohost));
for (int j=0;j<n;j++)
{
cout<<a[j]<< "+" <<b[j]<< "=" <<c[j]<<endl;
}
return 0;
}
__global__ void Add (int* a,int* b,int* c)
{
int Tid=blockidx.x;//blockidx is a built-in variable, blockidx.x represents this is a 2-D index
if (tid<n)//Avoid error causing illegal memory access
{
C[tid]=a[tid]+b[tid];
}
}
static void Checkcudaerroraux (const char *file,unsigned line, const char *statement,cudaerror_t error)
{
if (error==cudasuccess)
{
Return
}
cout<<statement<< "returned:" <<cudageterrorstring (Error) << "at file:" <<file<< "Line:" <<line<<endl;
Exit (1);
}
Cuda Programming Learning 3--vectorsum