本文介绍了为什么不能共享成员变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在CUDA代码中实例化一个类,它与同一个块中的其他线程共享一些成员。

I would like to instantiate a class in CUDA code, that shares some of its members with other threads in the same block.

但是,当试图编译下面的代码,我得到的错误:»属性共享不适用这里«(nvcc版本4.2)。

However, when trying to compile the following code, I get the error: »attribute "shared" does not apply here« (nvcc version 4.2).

class SharedSomething {

public:
    __shared__ int i; // this is not allowed
};

__global__ void run() {

    SharedSomething something;
}

背后的理由是什么?

推荐答案

Rost解释了这个问题的解决方案。限制背后的理由。要回答问题的第二部分,一个简单的解决方法是让内核声明共享内存,并初始化一个指向它拥有的类的指针。在类构造函数中。示例。

Rost explained the rationale behind the limitation. To answer the second part of the question, a simple workaround is to have the kernel declare the shared memory, and initialize a pointer to it owned by the class, e.g. in the class constructor. Example.

class Foo
{
public:
  __device__
  Foo(int *sPtr) : sharedPointer(sPtr, gPtr) {
    sharedPointer[threadIdx.x] = gPtr[blockIdx.x * blockDim.x + threadIdx.x];
    __syncthreads();
  }

  __device__
  void useSharedData() { printf("my data: %f\n", sharedPointer[threadIdx.x]); }

private:
  int *sharedPointer;
};

__global__ void example(int *gData)
{
  __shared__ int sData[BLOCKDIM];

  Foo f(sData, gData);

  f.useSharedData();
}

Caveat:用浏览器编写的代码,未经验证,未经测试,但概念扩展到真正的代码,我已经使用这种技术自己)。

Caveat: code written in browser, unverified, untested (and it's a trivial example, but the concept extends to real code—I have used this technique myself).

这篇关于为什么不能共享成员变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 12:19