# Complex vector inner product using cublas or thrust

**URL:** <https://forums.developer.nvidia.com/t/complex-vector-inner-product-using-cublas-or-thrust/73739>\
**Category:** CUDA Programming and Performance\
**Created:** [April 26, 2019, 2:48am UTC](https://forums.developer.nvidia.com/t/complex-vector-inner-product-using-cublas-or-thrust/73739 "2019-04-26T02:48:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bit\_drinkwater](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@bit\_drinkwater](https://forums.developer.nvidia.com/u/bit_drinkwater)\
**Post date:** [April 26, 2019, 2:48am UTC](https://forums.developer.nvidia.com/t/complex-vector-inner-product-using-cublas-or-thrust/73739/1 "2019-04-26T02:48:34Z")

</div>

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\> = a1_conj(b1) + … + aN_conj(bN). (conj(.) means conjugate operation)

I have tried cublasCdotu, and it just computes a1_b1 + … + aN_bN.

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

Finally, I tried thrust::inner\_product, and it computes a1_b1 + … + aN_bN 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!

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [April 26, 2019, 3:40am UTC](https://forums.developer.nvidia.com/t/complex-vector-inner-product-using-cublas-or-thrust/73739/2 "2019-04-26T03:40:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bit\_drinkwater](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@bit\_drinkwater](https://forums.developer.nvidia.com/u/bit_drinkwater)\
**Post date:** [April 26, 2019, 6:47am UTC](https://forums.developer.nvidia.com/t/complex-vector-inner-product-using-cublas-or-thrust/73739/3 "2019-04-26T06:47:35Z")

</div>

> [@Robert\_Crovella](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [April 26, 2019, 1:37pm UTC](https://forums.developer.nvidia.com/t/complex-vector-inner-product-using-cublas-or-thrust/73739/4 "2019-04-26T13:37:47Z")

</div>

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

[url][c++ - How to compute complex vectors' inner product using cublas or thrust? - Stack Overflow](https://stackoverflow.com/questions/55860197/how-to-compute-complex-vectors-inner-product-using-cublas-or-thrust%5B/url%5D)

transform\_reduce can work with two arrays if you use a zip\_iterator

---

<div class="post-metadata">

**Author:** ![bit\_drinkwater](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@bit\_drinkwater](https://forums.developer.nvidia.com/u/bit_drinkwater)\
**Post date:** [April 26, 2019, 3:30pm UTC](https://forums.developer.nvidia.com/t/complex-vector-inner-product-using-cublas-or-thrust/73739/5 "2019-04-26T15:30:53Z")

</div>

> [@Robert\_Crovella](#):
>
> 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](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!
