Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Cuda atomicInc不工作_C++_Cuda_Parallel Processing - Fatal编程技术网

C++ Cuda atomicInc不工作

C++ Cuda atomicInc不工作,c++,cuda,parallel-processing,C++,Cuda,Parallel Processing,我正在GPU上实现k-means,目前我有以下代码: __device__ unsigned int cuda_delta = 0; __global__ void kmeans_kernel(const sequence_t *data, const sequence_t *centroids, int * membership, uns

我正在GPU上实现k-means,目前我有以下代码:

__device__ unsigned int cuda_delta = 0;

__global__ void kmeans_kernel(const sequence_t *data,
                          const sequence_t *centroids,
                          int * membership,
                          unsigned int n,
                          unsigned int numClusters )
{
int index = blockIdx.x * blockDim.x  + threadIdx.x;
if (index < n){

    int min_distance = INT_MAX;
    int nearest = -1;

    for (int i = 0; i < numClusters; i++){
        sequence_t centroid = centroids[i];
        int distance = distance(centroid, data[index]);
        if(distance < min_distance) {
            nearest = i;
            min_distance = distance;
        }
    }

    if(membership[index] != nearest) {
        membership[index]=nearest;
        atomicInc(&cuda_delta,n);
    }
}
预期值为18634。我是不是遗漏了什么

编辑完整的代码在上可用,要运行示例,只需使用make编译即可。并使用以下参数多次运行程序,您将看到第一次迭代的增量值并不总是预期值

./cuda-means mus_musmusculus.dat 859
提前感谢

cudaMemcpyToSymbol(cuda_delta, &delta,sizeof(unsigned int));

这是你的问题

从文件中:

    cudaError_t cudaMemcpyFromSymbol ( void* dst, const void* symbol, size_t count, size_t offset = 0, cudaMemcpyKind kind = cudaMemcpyDeviceToHost )
Copies data from the given symbol on the device.
    Parameters

dst
    - Destination memory address 
symbol
    - Device symbol address 
count
    - Size in bytes to copy 
offset
    - Offset from start of symbol in bytes 
kind
    - Type of transfer
cudaMemcpyFromSymbol
期望地址和符号作为第二个参数,而不是设备符号

您可以使用
cudaGetSymbolAddress(void**devPtr,const void*symbol)


void*
是纯粹的邪恶…

我真可耻!原子能运转正常


我不是“memseting”成员数组。我修好后,一切都正常。

您确定您想要的是cudaInc,而不是cudaAdd吗?仅供参考是的,我想要cudaInc。我已经查过这个问题了,但事实并非如此。n值正确,有时打印的值为预期值“18634”。不过还是要谢谢你。你看起来没有在内核上做什么。您也可以尝试使用
cuda memcheck
运行代码。SSCCE.org代码不是您的“完整代码”。它是编译和演示问题的一个小子集。您应该做一些工作来创建它,而不仅仅是将完整的代码转储到github存储库中。你可以查一下。只需控制“cudaMemcpyToSymbol(devData,&value,sizeof(float));”的+f
cudaMemcpyToSymbol(cuda_delta, &delta,sizeof(unsigned int));
cudaMemcpyFromSymbol(&delta,cuda_delta,sizeof(unsigned int));
    cudaError_t cudaMemcpyFromSymbol ( void* dst, const void* symbol, size_t count, size_t offset = 0, cudaMemcpyKind kind = cudaMemcpyDeviceToHost )
Copies data from the given symbol on the device.
    Parameters

dst
    - Destination memory address 
symbol
    - Device symbol address 
count
    - Size in bytes to copy 
offset
    - Offset from start of symbol in bytes 
kind
    - Type of transfer