2016-10-06 9 views
0

私はCUDA実行用とCUDAなし(将来のOMPを念頭に置く)の2つの分岐をコード内に定義しようとしています。しかし、私がマクロ__CUDA_ARCH__を使用すると、ホストコードが実行されているかのように見えます。しかし、推力はデフォルトでCUDA(およびデバイスコードの分岐)を使用すると仮定しました。私のコードに何が問題なのですか? ここでは:コンスタンツ* I 2を掛けたとしてベクトルを定義する「形質転換」と期待推理:なぜ常にホストコードが__CUDA_ARCH__にもかかわらず実行されるのですか

#include <thrust/transform.h>         
#include <thrust/functional.h>         
#include <thrust/iterator/counting_iterator.h>     
#include <stdio.h>            

struct my_op             
{                
    my_op(int init_const) : constanta(init_const) {}  
    __host__ __device__ int operator()(const int &x) const 
    {              
     #if defined(__CUDA_ARCH__)      
      return 2 * x * constanta; // never executed - why? 
     #else          
      return x * constanta;  // always executed     
     #endif      
    }              

private:              
    int constanta;           
};                

int main()              
{                
int data[7] = { 0, 0, 0, 0, 0, 0, 0 };       
thrust::counting_iterator<int> first(10);      
thrust::counting_iterator<int> last = first + 7;    

int init_value = 1;           
my_op op(init_value);           

thrust::transform(first, last, data, op);      
for each (int el in data)          
    std::cout << el << " ";        

std::cout << std::endl;          
}     

私はホスト・コードが使用されていることがわかり - 出力は「10 11 12 13 14 15 16」であり、しません"20 22 24 26 28 30 32"(期待どおり)。

なぜですか?あなたはスラストアルゴリズムは、デバイス上で動作する場合、一般的にすべてを話す

thrust::transform(first, last, data, op); 
           ^^^^ 

:あなたのデータ項目の一つは、操作がホストメモリ内にある変換推力に供給されるので、

答えて

2

推力は、ホストパスを選択されましたコンテナ渡したデータは、デバイスメモリにも存在する必要があります。ここで

は、我々は、デバイス常駐コンテナでdataを交換する場合、推力はデバイスパスをたどることを実証しているあなたのコードの変更です:

$ cat t13.cu 
#include <thrust/transform.h> 
#include <thrust/functional.h> 
#include <thrust/iterator/counting_iterator.h> 
#include <thrust/device_vector.h> 
#include <stdio.h> 

struct my_op 
{ 
    my_op(int init_const) : constanta(init_const) {} 
    __host__ __device__ int operator()(const int &x) const 
    { 
     #if defined(__CUDA_ARCH__) 
      return 2 * x * constanta; // never executed - why? 
     #else 
      return x * constanta;  // always executed 
     #endif 
    } 

private: 
    int constanta; 
}; 

int main() 
{ 
// int data[7] = { 0, 0, 0, 0, 0, 0, 0 }; 
thrust::counting_iterator<int> first(10); 
thrust::counting_iterator<int> last = first + 7; 
thrust::device_vector<int> d_data(7); 

int init_value = 1; 
my_op op(init_value); 

thrust::transform(first, last, d_data.begin(), op); 
for (int el = 0; el < 7; el++) { 
    int dat = d_data[el]; 
    std::cout << dat << " "; } 

std::cout << std::endl; 
} 
$ nvcc -arch=sm_61 -o t13 t13.cu 
$ ./t13 
20 22 24 26 28 30 32 
$ 

あなたはスラストアルゴリズムの派遣について学ぶためにthrust quick start guideを読むことをお勧めします。

関連する問題