2

私はdevice_vector構造を通過しようとしています

struct point 
{
    unsigned int x;
    unsigned int y;
}

次の方法で関数に:

void print(thrust::device_vector<point> &points, unsigned int index)
{
    std::cout << points[index].y << points[index].y << std::endl;
}

myvector は正しく初期化されました

print(myvector, 0);

次のエラーが表示されます。

error: class "thrust::device_reference<point>" has no member "x"
error: class "thrust::device_reference<point>" has no member "y"

どうしたの?

4

2 に答える 2

5

残念ながら、device_reference<T>のメンバーを公開することはできませんがT、 に変換することはできTます。

を実装printするには、一時的な に変換して、各要素の一時的なコピーを作成しますtemp

void print(thrust::device_vector<point> &points, unsigned int index)
{
    point temp = points[index];
    std::cout << temp.y << temp.y << std::endl;
}

を呼び出すたびにprint、GPU からシステム メモリへの転送が発生し、一時ファイルが作成されます。pointsのコレクション全体を一度に出力する必要がある場合、より効率的な方法では、ベクトル全体をまとめpointshost_vectororにstd::vector( を使用してthrust::copy) コピーし、通常どおりコレクションを反復処理します。

于 2011-07-09T06:15:24.960 に答える
1

http://thrust.googlecode.com/svn/tags/1.1.0/doc/html/structthrust_1_1device__reference.htmlから:

device_referenceは、デバイスメモリに格納されているオブジェクトへの参照として機能します。device_referenceは、直接使用するためのものではありません。むしろ、このタイプはdevice_ptrを延期した結果です。同様に、device_referenceのアドレスを取得すると、device_ptrが生成されます。

多分あなたは次のようなものが必要です

(&points[index]).get()->x

それ以外の

points[index].x

少し醜いですが、CUDAにはRAMとGPUの間でデータを転送するメカニズムが必要です。

于 2011-07-08T15:16:18.283 に答える