在不同的GPU上初始化struct

Hie*_*ham 0 c c++ cuda thrust multiple-gpu

我有一个看起来像这样的结构

struct LstmLayer {
  int deviceId;
  thrust::device_vector <real_t> W;
  thrust::device_vector <real_t> gradW;

  LstmLayer() : deviceId(0) {}

  LstmLayer(int __deviceId__) : deviceId(__deviceId__) {}

  void setDevice(int __deviceId__) { deviceId = __deviceId__; }

  void init(bool initParams) {
    W.resize(4*lstmSize * 2*lstmSize);
    gradW.resize(4*lstmSize * 2*lstmSize);

    if (initParams) GPU_Random_Vector(W);
  }
}
Run Code Online (Sandbox Code Playgroud)

现在我想初始化一个数组LstmLayer,每个元素都在不同的GPU设备上.我这样做如下

  struct LstmLayer lstmLayers[MAX_NUM_LSTM_LAYERS];

  for (int i = 0; i < numLstmLayers; ++i) {
    CUDA_SAFE_CALL(cudaSetDevice(i));
    lstmLayers[i].setDevice(i);
    lstmLayers[i].init(true);
  }
Run Code Online (Sandbox Code Playgroud)

运行此程序会出现以下错误

terminate called after throwing an instance of 'thrust::system::system_error'
  what():  driver shutting down
Run Code Online (Sandbox Code Playgroud)

请告诉我我的代码有什么问题以及如何正确执行?先谢谢你.

tal*_*ies 5

问题是您在同一个CUDA GPU上下文中实例化所有向量,然后尝试在不同的上下文中使用它们.发生这种情况是因为在device_vector定义结构数组时会调用每个的默认构造函数.要注释您的代码:

struct LstmLayer lstmLayers[MAX_NUM_LSTM_LAYERS]; // default constructor for each device vector called here in one GPU context.

for (int i = 0; i < numLstmLayers; ++i) {
    CUDA_SAFE_CALL(cudaSetDevice(i));
    lstmLayers[i].setDevice(i);
    lstmLayers[i].init(true); // Error here, you changed to a different device and called resize
}
Run Code Online (Sandbox Code Playgroud)

解决方案可能是将设备向量重新定义为指针,并在init方法中显式调用它们的构造函数.有很多不同的方法可以做到,例如:

 struct LstmLayer {
  int deviceId;
  thrust::device_vector <real_t>  * W;
  thrust::device_vector <real_t> * gradW;

  LstmLayer() : deviceId(0) {}

  LstmLayer(int __deviceId__) : deviceId(__deviceId__) {}

  void setDevice(int __deviceId__) { deviceId = __deviceId__; }

  void init(bool initParams) {
    W = new thrust::device_vector<real_t>(4*lstmSize * 2*lstmSize);
    gradW = new thrust::device_vector<real_t>(4*lstmSize * 2*lstmSize);

    if (initParams) GPU_Random_Vector(W);
  }
}
Run Code Online (Sandbox Code Playgroud)

[免责声明:用浏览器编写,永不编译,自担风险使用]

显然你需要定义一个析构函数来防止内存泄漏.还有其他可能性,我会将其作为练习留给读者.