因此,我开始对CUDA感到沮丧,以至于我决定编写我能做的最简单的代码,只是为了了解我的方向。 但是似乎有些事情在我脑海中浮现。 在我的代码中,我只是添加两个数组,然后将它们存储在第三个数组中,如下所示:
#include <stdio.h>
#include <stdlib.h>
__global__ void add(int* these, int* those, int* answers)
{
int tid = blockIdx.x;
answers[tid] = these[tid] + those[tid];
}
int main()
{
int these[50];
int those[50];
int answers[50];
int *devthese;
int *devthose;
int *devanswers;
cudaMalloc((void**)&devthese, 50 * sizeof(int));
cudaMalloc((void**)&devthose, 50 * sizeof(int));
cudaMalloc((void**)&devanswers, 50 * sizeof(int));
int i;
for(i = 0; i < 50; i++)
{
these[i] = i;
those[i] = 2 * i;
}
cudaMemcpy(devthese, these, 50 * sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(devthose, those, 50 * sizeof(int), cudaMemcpyHostToDevice);
add<<<50,1>>>(devthese, devthose, devanswers);
cudaMemcpy(answers, devanswers, 50 * sizeof(int), cudaMemcpyDeviceToHost);
for(i = 0; i < 50; i++)
{
fprintf(stderr,"%i\n",answers[i]);
}
return 0;
}
但是,正在打印的int值并不遵循3的倍数的顺序,这正是我所期望的。 谁能解释出什么问题了?