CUDA矢量相加

#include "cuda_runtime.h"
#include "cublas_v2.h"
#include "time.h"
#include
using namespace std;

#define N 10

__global__ void add(int *a, int *b, int *c)
{


int tid = blockIdx.x;
if (tid < N)
c[tid] = a[tid] + b[tid];


}
int main(void)
{
int a[N],b[N],c[N];
int *dev_a, *dev_b, *dev_c;
cudaMalloc((void**)&dev_a, N*sizeof(int));
cudaMalloc((void**)&dev_b, N*sizeof(int));
cudaMalloc((void**)&dev_c, N*sizeof(int));


//在CPU上为a和b赋值
for (int i = 0; i < N; i++)
{
a[i] = -i;
b[i] = i*i;
}
//将数组复制到GPU
cudaMemcpy(dev_a, a, N*sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(dev_b, b, N*sizeof(int), cudaMemcpyHostToDevice);


add << > >(dev_a, dev_b, dev_c);
cudaMemcpy(c, dev_c, N*sizeof(int), cudaMemcpyDeviceToHost);
for (int i = 0; i < N; i++)
{
printf("%d+%d=%d\n",a[i],b[i],c[i]);
}
cudaFree(dev_a);
cudaFree(dev_b);
cudaFree(dev_c);
getchar();
return 0;
}

你可能感兴趣的:(C++)