这是下面的代码:
#include <stdio.h>
#include <cuda.h>
#include <device_launch_parameters.h>
#include <cuda_runtime.h>
#define _crt_nonstdc_no_deprecate
__global__ void Kernel(float *d_arr_i,float *d_arr_o)
{
int i = threadIdx.x;
int j = threadIdx.y;
int k = threadIdx.z;
float f = d_arr_i[i];
d_arr_o[i] = f * f;
printf("%d \n",f);
printf("x = %d & y = %d & z = %d \n",i,j,k);
}
int main ()
{
//printf("Hello C..!");
const unsigned int arr_s = 12;
float h_arr_i[arr_s];
for (int i = 0;i < arr_s;i++)
{
h_arr_i[i] = float(i);
}
for (int i = 0;i<arr_s;i++)
{
printf("input arr %d : %e \n",(int)i,h_arr_i[i]);
}
float h_arr_o[arr_s];
float* d_arr_i;
float* d_arr_o;
const unsigned int d_arr_s = arr_s*sizeof(float);
cudaMalloc((void**)&d_arr_i,d_arr_s);
cudaMalloc((void**)&d_arr_o,d_arr_s);
cudaMemcpy(d_arr_i,h_arr_i,d_arr_s,cudaMemcpyHostToDevice);
Kernel<<<1,arr_s>>>(d_arr_i,d_arr_o);
cudaMemcpy(h_arr_o,d_arr_o,d_arr_s,cudaMemcpyDeviceToHost);
printf("\n");
for (int i = 0;i < arr_s;i++)
{
printf("output arr : %d \n",h_arr_o[i]);
}
int d;
cudaDeviceProp c;
int e;
cudaGetDeviceProperties(&c,e);
printf("\n %e",e);
cudaGetDeviceCount(&d);
printf("\n %d \n",d);
cudaFree(d_arr_i);
cudaFree(d_arr_o);
system("Pause");
return 0;
}
在上面的代码中,“输出 arr”应该给出数组中数字的平方,但它没有。
有人可以解释为什么会发生这个错误吗?一些细节,我正在运行代码:CUDA 2.3 / Emulation mode/without a NVIDIA GPU OS:Windows 7 64bit Visual Studio 2005 SP1