lion03 / thrust

Automatically exported from code.google.com/p/thrust
Apache License 2.0
0 stars 0 forks source link

Junk values return while doing inclusive_scan for pair #281

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Iam trying to do a basic operation between 2 pairs. Iam getting junk results 
sometimes.

INPUT PAIR             OUTPUT PAIR
data  d_vec            data     d_vec
0       3.4e+38         0       3.4e+38 
2       1.16667 2       1.16667
2       3.4e+38 2       3.4e+38
1       3.5             1       3.5
1       3.4e+38 1       3.4e+38
2       5.5             2       5.5
2       3.4e+38 2       3.4e+38
1       7.5             1       7.5
1       3.4e+38 1       3.4e+38
2       9.5                 2       9.5
0       3.4e+38       0       5.5 -------------------
2       3.4e+38       2       3.4e+38
2       3.4e+38       2       3.4e+38

struct parallel_scan
{
  template <typename Pair1, typename Pair2>
  __host__ __device__
    Pair1 operator()(const Pair1 &x, const Pair2 &y)
  {

     if(( y.second == 0) && (x.second == 2))
        return thrust::make_pair(x.first,y.second);
     else
        return thrust::make_pair(y.first,y.second);

   } // end operator()
}; // end add_pairs

From main I call the following:

typedef thrust::pair<float,int>P ;
thrust::device_vector<P> d_result(d_vec.size());
thrust::device_vector<P> final_pair(d_vec.size());
thrust::transform(d_vec.begin(), d_vec.end(), data.begin(), 
d_result.begin(),make_pair_functor());
thrust::inclusive_scan(d_result.begin(), d_result.end(), final_pair.begin(), 
parallel_scan());

My concerns:
1) For the underlined  I should never get 5.5 .
In any case i should get either 9.5 or 3.4e+38. But Iam getting  a value that 
is passed long back. And now iam getting juk value as my result.

Original issue reported on code.google.com by vinitha....@gmail.com on 18 Nov 2010 at 7:45

GoogleCodeExporter commented 8 years ago
Hi Vinitha, thanks for the report.

If you could post a complete, self-contained code sample which reproduces the 
error, we'll be happy to look into it.

Original comment by jaredhoberock on 18 Nov 2010 at 7:49

GoogleCodeExporter commented 8 years ago
Hi Attaching the complete file. You just need to compile.

Original comment by vinitha....@gmail.com on 18 Nov 2010 at 8:32

Attachments:

GoogleCodeExporter commented 8 years ago
Hi , Do you find a problem in there????

Original comment by vinitha....@gmail.com on 19 Nov 2010 at 1:47

GoogleCodeExporter commented 8 years ago
Hi Vinitha,

Actually, this result is correct, although not intuitive.  To parallelize scan 
we require that the binary operation be associative.  In this case the scan 
function is computing the partial sums
  P[0] = A[0];
  P[1] = P[0] op A[1];
  P[2] = P[1] op A[2];
  ...
  P[5] = P[4] op A[5];
and
  P[6] = A[6];
  P[7] = P[6] op A[7];
  P[8] = P[7] op A[8];
  ...
  P[11] = P[10] op A[9];
By associativity the final values are then computed as
  B[0] = P[0]
  B[1] = P[1]
  ...
  B[5] = P[5]
  B[6] = P[5] + P[6]
  B[7] = P[5] + P[7]
  ...
  B[10] = P[5] + P[10]
  B[11] = P[5] + P[11]
This is why the value of A[5] shows up in the result of B[10].  

The underlying problem is that your functor is not associative.  For example
  F( F({X,2}, {Y,1}), {Z,0}) = F({Y,1}, {Z,0}) = {Z,0}
but
  F( {X,2}, F({Y,1}, {Z,0})) = F({X,2}, {Z,0}) = {X,0}

Original comment by wnbell on 21 Nov 2010 at 5:42