gpt4 book ai didi

CUDAatomicAdd() 给出了错误的结果

转载 作者:行者123 更新时间:2023-11-30 20:02:16 28 4
gpt4 key购买 nike

我使用atomicAdd()使用两种不同的方案将1添加到数组c = {0,0,0,0,0}的每个元素

  1. c[i] = c[i] + 1;

result - c = {1,1,1,1,1}

  1. c[i] =atomicAdd(&(c[i]),1);

result c = {0,0,0,0,0}

我完全不知道为什么会得到这样的结果,这是我用来获取结果的小代码。

#include "cuda_runtime.h"
#include "device_launch_parameters.h"

#include <stdio.h>
#include<windows.h>

void addWithCuda(int *c, int size);

__global__ void addKernel(int *c, int size)
{
int i = threadIdx.x;
if (i < size)
c[i] = c[i] + 1;
//c[i] = atomicAdd(&(c[i]),(int)1);
}

int main()
{
const int arraySize = 5;

int c[arraySize] = {0,0,0,0,0};

// Add vectors in parallel.
addWithCuda(c, arraySize);

Sleep(3000);
printf("result = {%d,%d,%d,%d,%d}\n",
c[0], c[1], c[2], c[3], c[4]);
return 0;
}

// Helper function for using CUDA to add vectors in parallel.
void addWithCuda(int *c, int size)
{

int *dev_c = 0;
cudaError_t cudaStatus;

// Choose which GPU to run on, change this on a multi-GPU system.
cudaStatus = cudaSetDevice(0);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaSetDevice failed! Do you have a CUDA-capable GPU installed?");

}

// Allocate GPU buffers for three vectors (two input, one output) .
cudaStatus = cudaMalloc((void**)&dev_c, size * sizeof(int));
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMalloc failed!");

}

cudaStatus = cudaMemcpy(dev_c, c, size * sizeof(int), cudaMemcpyHostToDevice);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMemcpy failed!");

}

// Launch a kernel on the GPU with one thread for each element.
addKernel<<<1, size>>>(dev_c, size);


// cudaDeviceSynchronize waits for the kernel to finish, and returns
// any errors encountered during the launch.
cudaStatus = cudaDeviceSynchronize();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);

}

// Copy output vector from GPU buffer to host memory.
cudaStatus = cudaMemcpy(c, dev_c, size * sizeof(int), cudaMemcpyDeviceToHost);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMemcpy failed!");

}
}

最佳答案

c[i] = atomicAdd(&(c[i]),(int)1);

应该是

atomicAdd(&(c[i]),(int)1); 

基本上是&(c[i]),引用调用用于直接在数组中添加+1。 atomicAdd 返回 0 ;并且您将零放入数组中。

关于CUDAatomicAdd() 给出了错误的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20910569/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com