1

std::bind2ndを推力で使用しようとしています。ホストポインターでコンパイルするコードがありますが、デバイスポインターではコンパイルしません。それらは同一であり、どちらの場合でも機能するはずだと思います。

// this compiles fine
thrust::host_vector<unsigned int> h_vec(nwords);
thrust::host_vector<unsigned int> h_map(nwords);
thrust::host_vector<unsigned int> h_out1(nwords);
thrust::copy_if(h_vec.begin(), h_vec.end(), h_map.begin(), h_out1.begin(), 
                std::bind2nd(thrust::equal_to<int>(),1));

// this compilation fails with the error below
thrust::device_vector<unsigned int> d_map(nwords);
thrust::device_vector<unsigned int> d_vec(nwords);
thrust::device_vector<unsigned int> d_out1(nwords);
thrust::copy_if(d_vec.begin(), d_vec.end(), d_map.begin(), d_out1.begin(), 
                std::bind2nd(thrust::equal_to<int>(),1));

bind2ndを使用して2番目のcopy_ifを呼び出そうとすると、次のエラーが発生します。

 /opt/cuda/include/thrust/detail/internal_functional.h(99): warning: calling a
 __host__ function from a __host__ __device__ function is not allowed

推力でバイナリ関数にアダプタを使用する別の方法はありますか?Webの例で「thrust::bind2nd」を使用している人を見たことがありますが、どのヘッダーファイルでもそれを見つけることができません。

4

1 に答える 1

3

アダプターを推力で使用することが可能です。ただし、GPUでそれらを使用する場合、アダプターは__device__関数である必要があります。これが、最初のコンパイルと2番目のコンパイルが行われない理由ですcopy_if。述語はデバイス関数ではなくホスト関数であり、の使用はdevice_vectorデバイスのコンパイル軌道を意味します。

つまり、GPUで使用するアダプター関数が必要な場合は、自分でアダプター関数を作成する必要があります。標準ライブラリのもの(、、bind1stbind2ndbind使用できません。

于 2013-03-01T17:22:55.020 に答える