1

我正在努力学习 cuda。我正在尝试运行一个简单的代码

#include <stdlib.h>
#include <stdio.h>

__global__ void kernel(int *array)
{
 int index = blockIdx.x * blockDim.x + threadIdx.x;

  array[index] = 7;
}

int main(void)
{
  int num_elements = 256;

  int num_bytes = num_elements * sizeof(int);

  // pointers to host & device arrays
  int *device_array = 0;
  int *host_array = 0;

  // malloc a host array
  host_array = (int*)malloc(num_bytes);

  // cudaMalloc a device array
  cudaMalloc((void**)&device_array, num_bytes);

  int block_size = 128;
  int grid_size = num_elements / block_size;

  kernel<<<grid_size,block_size>>>(device_array);

  // download and inspect the result on the host:
  cudaMemcpy(host_array, device_array, num_bytes, cudaMemcpyDeviceToHost);

  // print out the result element by element
  for(int i=0; i < num_elements; ++i)
  {
    printf("%d ", host_array[i]);
  }

  // deallocate memory
  free(host_array);
  cudaFree(device_array);
}

它应该打印 7 但它打印 0 这个语句似乎没有被执行 "kernel<<>>(device_array);" 它也没有给出任何编译错误。有什么帮助吗??

4

1 回答 1

2

该代码在我的机器上运行良好,但请确保cudaDeviceSynchronize在内核调用之后添加和错误检查。

更改代码如下检查错误:

kernel<<<grid_size,block_size>>>(device_array);
// wait until tasks are completed
cudaDeviceSynchronize();

// check for errors
cudaError_t error = cudaGetLastError();
if (error != cudaSuccess) {
  fprintf(stderr, "ERROR: %s \n", cudaGetErrorString(error));
}
于 2013-01-19T00:12:49.053 回答