2017-05-15 6 views
0

私はそれがそのネストされた推力を示し、配列の各要素とアレイ「c」を埋めるために「」ネストされたスラスト::多様入力

を最小テストコードをテストしている::フィルである値の塗りが機能しません定数入力と呼ばれ、この入力値を入力配列に正しく埋め込みます。

入力値が値の配列の各要素、即ち変化値である場合は、それは多様な入力の一方のみ(最初または最後の)値

#include <thrust/inner_product.h> 
#include <thrust/functional.h> 
#include <thrust/device_vector.h> 
#include <thrust/random.h> 

#include <thrust/execution_policy.h> 

#include <iostream> 
#include <cmath> 
#include <boost/concept_check.hpp> 

struct bFuntor 
{ 
    bFuntor(int* av__, int* cv__, const int& N__) : av_(av__), cv_(cv__), N_(N__) {}; 

    __host__ __device__ 
    int operator()(const int& idx) 
    { 
     thrust::device_ptr<int> cv_dpt = thrust::device_pointer_cast(cv_); 
     thrust::device_ptr<int> cv_dpt1 = thrust::device_pointer_cast(cv_+N_); 

     thrust::detail::normal_iterator<thrust::device_ptr<int>> c0 = thrust::detail::make_normal_iterator<thrust::device_ptr<int>>(cv_dpt); 
     thrust::detail::normal_iterator<thrust::device_ptr<int>> c1 = thrust::detail::make_normal_iterator<thrust::device_ptr<int>>(cv_dpt1); 

     // ** this thrust::fill with varied values does not work 
     thrust::fill(thrust::device,c0,c1,av_[idx]); 

     // ** this thrust::fill with constant works 
//  thrust::fill(thrust::device,c0,c1,10); 

     printf("fill result:\n"); 
     for (int i=0; i<N_; i++) 
     printf("fill value: %d -> return value: %d \n",av_[idx],cv_[i]); 
     printf("\n"); 

     return cv_dpt[idx]; 
    } 

    int* av_; 
    int* cv_; 
    int N_; 
}; 

int main(void) 
{ 
     int N = 2; 
     std::vector<int> av = {0,1}; 
     std::vector<int> cv = {-1,-2}; 

     thrust::device_vector<int> av_d(N); 
     thrust::device_vector<int> cv_d(N); 
     av_d = av; cv_d = cv; 

     // call with nested manner 
     thrust::transform(thrust::counting_iterator<int>(0), 
      thrust::counting_iterator<int>(N), 
      cv_d.begin(), 
      bFuntor(thrust::raw_pointer_cast(av_d.data()), 
      thrust::raw_pointer_cast(cv_d.data()), 
        N));  

     return 0; 
} 

出力ケースと入力アレイを充填することができます値:一定の入力値の

fill result: 
fill value: 0 -> return value: 1 
fill value: 1 -> return value: 1 
fill value: 0 -> return value: 1 
fill value: 1 -> return value: 1 

出力ケース:

fill result: 
fill value: 10 -> return value: 10 
fill value: 10 -> return value: 10 
fill value: 10 -> return value: 10 
fill value: 10 -> return value: 10 

は、この推力のです問題?それともこのように使うべきではないのですか?

+0

これは最後の質問であなたに説明されたのとまったく同じ問題です – talonmies

+0

"thrust :: transform"は使用せず、この場合はそのファンクタを記述します。これは、ファンクタ内の変数の初期化が不正確になる危険性を排除します(この場合は存在しません)。 thrust :: fillインタフェースに関して、その4番目の入力は「const T&value」です。このインタフェースに準拠している限り動作するはずです。そうではありませんか? –

+2

同じ配列に異なる値を入力するために、2つの異なるパラレルデータ操作を要求しています。何が起こるはずですか? – talonmies

答えて

1

これは、データ競合の例である:ここ

int operator()(const int& idx) 
{ 
    thrust::device_ptr<int> cv_dpt = thrust::device_pointer_cast(cv_); 
    thrust::device_ptr<int> cv_dpt1 = thrust::device_pointer_cast(cv_+N_); 

    thrust::detail::normal_iterator<thrust::device_ptr<int>> c0 = thrust::detail::make_normal_iterator<thrust::device_ptr<int>>(cv_dpt); 
    thrust::detail::normal_iterator<thrust::device_ptr<int>> c1 = thrust::detail::make_normal_iterator<thrust::device_ptr<int>>(cv_dpt1); 


    thrust::fill(thrust::device,c0,c1,av_[idx]); 

    //..... 
} 

、ファンクタを呼び出すたびに異なる値と同じイテレータ範囲(C1とC0)を充填しようとします。明らかに、複数の関数呼び出しが並行して発生すると、問題が発生します。

関連する問題