【发布时间】:2011-10-19 06:25:00
【问题描述】:
我有一个正在处理对象数组的 CUDA 应用程序;每个对象都有一个指向std::pair<int, double> 数组的指针。我正在尝试 cudaMemcpy 对象数组,然后 cudaMemcpy 对每个对象的数组,但是这给了我各种各样的悲伤。尝试复制到内部数组时崩溃;我不明白如何移动它...
#include <cuda.h>
#include <cuda_runtime.h>
#include <iostream>
using namespace std;
class Object
{
public:
int id;
float something;
std::pair<int, float> *somePairs;
};
Object *objects;
void initObjects()
{
objects = new Object[10];
for( int idx = 0; idx < 10; idx++ )
{
objects[idx].id = idx;
objects[idx].something = (float) idx;
objects[idx].somePairs = new std::pair<int, float>[10];
for ( int jdx = 10; jdx < 10; jdx++ )
{
objects[idx].somePairs[jdx] = std::pair<int, float>( jdx, (float) jdx );
}
}
}
void cudaMemcpyObjects()
{
Object *devObjects;
cudaMalloc( &devObjects, sizeof(Object) * 10 );
cudaMemcpy( devObjects, objects, sizeof(Object) * 10, cudaMemcpyHostToDevice );
for ( int idx = 0; idx < 10; idx++ )
{
size_t pairSetSize = sizeof(std::pair<int, float>) * 10;
// CRASH HERE ... v
cudaMalloc( &(devObjects[idx].somePairs), pairSetSize );
cudaMemcpy( devObjects[idx].somePairs, objects[idx].somePairs,
sizeof( std::pair<int, float> ) * 10, cudaMemcpyHostToDevice );
}
}
int main()
{
initObjects();
cudaMemcpyObjects();
return 0;
}
【问题讨论】:
-
这引出了一个问题:为什么? CUDA 代码不支持 C++ 标准库容器类。
-
嗯,首先,您可以在 CUDA 代码中读取 STD 容器。您可以轻松引用 .first 和 .second 。尽管您可以将其替换为任何数组内数组,但也会出现同样的问题。
-
CUDA 标准库不包含任何 c++ 容器的定义,如果不进行大量修改,主机版本将无法编译。如果您愿意发布一个,我非常希望看到一个自包含的 repro 内核来演示这一点。
-
@talonmies - this: pastebin.com/62L0a13J - 一个特别无用的示例,但可以编译并运行。同样,我对您的评论的反驳是,一个 可以 读取 CUDA 中的标准容器。您不能从 CUDA 调用主机代码,因此诸如 vector.at(i) 之类的东西将不起作用。