# Support for Complex-valued CuArray

**URL:** <https://discourse.julialang.org/t/support-for-complex-valued-cuarray/16177>\
**Category:** GPU\
**Tags:** question\
**Created:** [October 11, 2018, 1:31pm UTC](https://discourse.julialang.org/t/support-for-complex-valued-cuarray/16177 "2018-10-11T13:31:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fucai](https://avatars.discourse-cdn.com/v4/letter/f/c4cdca/32.png) [@fucai](https://discourse.julialang.org/u/fucai)\
**Post date:** [October 11, 2018, 1:31pm UTC](https://discourse.julialang.org/t/support-for-complex-valued-cuarray/16177/1 "2018-10-11T13:31:45Z")

</div>

It seems the support for complex -valued CuArray is not yet in place. Or, I may miss something here. Thanks.

‘’’  
using GPUArrays, CuArrays  
x = randn(Float32, 3,3);  
x = complex(x);  
cx = cu(x);  
y = map(sin, cx)  
‘’’

ERROR: MethodError: no method matching sin(::Complex{Float32})  
You may have intended to import Base.sin

Stacktrace:  
[1] \_broadcast\_getindex\_evalf at .\broadcast.jl:574 [inlined]  
[2] \_broadcast\_getindex at .\broadcast.jl:547 [inlined]  
[3] getindex at .\broadcast.jl:507 [inlined]  
[4] copy at .\broadcast.jl:758 [inlined]  
[5] materialize(::Base.Broadcast.Broadcasted{Base.Broadcast.ArrayStyle{CuArray},Nothing,typeof(CUDAnative.sin),Tuple{CuArray{Complex{Float32},2}}}) at .\broadcast.jl:724

Julia 1.0.0  
CuArrays V0.8.0

---

<div class="post-metadata">

**Author:** ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)\
**Post date:** [October 11, 2018, 1:41pm UTC](https://discourse.julialang.org/t/support-for-complex-valued-cuarray/16177/2 "2018-10-11T13:41:47Z")

</div>

`CUDAnative.sin` isn’t implemented for complex numbers. At the same time, the base implementation that presumably calls `sin(::Real)` somewhere down the road isn’t usable because that would end up dispatching to an incompatible version of `sin` for CPUs. Not sure there’s an easy fix here, ideally we use `Base.sin` everywhere and ultimately replace calls to `openlibm` with CUDA-specific ones using Cassette, but that’s not for tomorrow. Implementing `CUDAnative.sin(::Complex)` seems like a lot of code duplication, but maybe CuArrays.jl could provide these workaround definitions for now.

Feel free to open an issue on CuArrays.jl.

---

<div class="post-metadata">

**Author:** ![fucai](https://avatars.discourse-cdn.com/v4/letter/f/c4cdca/32.png) [@fucai](https://discourse.julialang.org/u/fucai)\
**Post date:** [October 12, 2018, 1:34am UTC](https://discourse.julialang.org/t/support-for-complex-valued-cuarray/16177/3 "2018-10-12T01:34:33Z")

</div>

@Tim, thanks a lot. A related issue is Julia will crash and quits if one wants to run command exp(cx), where cx is a cuarray containing complex numbers.
