46

問題は、Cudaカーネルでクラス「ベクトル」を使用する方法はありますか? 試してみると、次のエラーが表示されます。

error : calling a host function("std::vector<int, std::allocator<int> > ::push_back") from a __device__/__global__ function not allowed

グローバルセクションでベクトルを使用する方法はありますか? 私は最近、次のことを試しました:

  1. 新しい Cuda プロジェクトを作成する
  2. プロジェクトのプロパティに移動します
  3. Cuda C/C++ を開く
  4. デバイスに移動
  5. 「コード生成」の値を次の値に変更します: compute_20,sm_20

........その後、Cudaカーネルでprintf標準ライブラリ関数を使用できるようになりました。

vectorカーネル コードで printf がサポートされている方法で、標準ライブラリ クラスを使用する方法はありますか? これは、カーネル コードで printf を使用する例です。

// this code only to count the 3s in an array using Cuda
//private_count is an array to hold every thread's result separately 

__global__ void countKernel(int *a, int length, int* private_count) 
{
    printf("%d\n",threadIdx.x);  //it's print the thread id and it's working

    // vector<int> y;
    //y.push_back(0); is there a possibility to do this?

    unsigned int offset  = threadIdx.x * length;
    int i = offset;
    for( ; i < offset + length; i++)
    {
        if(a[i] == 3)
        {
            private_count[threadIdx.x]++;
            printf("%d ",a[i]);
        }
    }   
}
4

5 に答える 5

21

CUDA で STL を使用することはできませんが、Thrust ライブラリを使用して必要なことを実行できる場合があります。それ以外の場合は、ベクターの内容をデバイスにコピーして、通常どおり操作します。

于 2012-04-29T20:47:34.070 に答える
13

cuda ライブラリのスラストでは、thrust::device_vector<classT> を使用してデバイス上のベクトルを定義できます。ホスト STL ベクトルとデバイス ベクトルの間のデータ転送は非常に簡単です。この便利なリンクを参照できます: http://docs.nvidia.com/cuda/thrust/index.htmlで、いくつかの便利な例を見つけることができます。

于 2013-05-03T16:02:31.687 に答える
8

デバイス コードでは使用できませんstd::vector。代わりに配列を使用する必要があります。

于 2012-04-30T12:58:53.663 に答える