Complex vector inner product using cublas or thrust

Hello,

After searching a long time and trying different method, I still can’t solve this problem.

I have two vectors: x = [a1,…,aN], y = [b1,…bN].

And I want to compute their inner product: <x,y> = a1conj(b1) + … + aNconj(bN). (conj(.) means conjugate operation)

I have tried cublasCdotu, and it just computes a1b1 + … + aNbN.

And cublasCdotc returns conj(a1)*conj(b1) + … + conj(aN)*conj(bN).

Finally, I tried thrust::inner_product, and it computes a1b1 + … + aNbN too.

My thrust code is like the following:

typedef thrust::complex comThr;
thrust::host_vector< comThr > x( vec_size );
thrust::generate(x.begin(), x.end(), rand);

thrust::host_vector< comThr > y( vec_size );
thrust::generate(y.begin(), y.end(), rand);
comThr z = thrust::inner_product(x.begin(), x.end(), y.begin(), comThr(0.0f,0.0f) );

Could you give me some advice on this problem? Thank you!

thrus::transform_reduce

Write your transform functor to do the exact product operation you want
write your reduce functor to do the exact sum operation you want

Hello Robert,

Can thrust::transform_reduce works with two arrays ? I check its instruction on website:

host device OutputType thrust::transform_reduce ( const thrust::detail::execution_policy_base< DerivedPolicy > & exec,
InputIterator first,
InputIterator last,
UnaryFunction unary_op,
OutputType init,
BinaryFunction binary_op
)

It seems that it can only work on the input array.

I just learned thrust few days ago, apologize for my stupid question. Thanks.

you already have a suitable answer on your cross-posting on stackoverflow

https://stackoverflow.com/questions/55860197/how-to-compute-complex-vectors-inner-product-using-cublas-or-thrust

transform_reduce can work with two arrays if you use a zip_iterator

Thanks!