Main Content

Work with Complex Numbers on a GPU

You can accelerate many functions in MATLAB® that operate on or return complex numbers by using a GPU. This page describes the conditions you should be aware of when you work with complex numbers and your data is a gpuArray. For information about handling complex data in your custom CUDA® code running in MATLAB instead, see Run MEX Functions Containing CUDA Code.

Conditions for Working with Complex Numbers on a GPU

If the output of a function running on a GPU could potentially be complex, you must explicitly specify its input arguments as complex using the complex function. This applies to functions operating on gpuArray objects directly and to functions operating on gpuArray data using arrayfun.

For example, to successfully compute sqrt(G) where G is a gpuArray containing negative elements, specify that G is complex.

x = [-1 0 1];
G = gpuArray(complex(x));
rootG = sqrt(G)
   0.0000 + 1.0000i   0.0000 + 0.0000i   1.0000 + 0.0000i

If the result is a gpuArray of complex data and all the imaginary parts are zero, these parts are retained and the data remains complex. This could result in unnecessary calculations being performed when operating on the data using, for example, sort and isreal.

When you use arrayfun to apply functions whose outputs could potentially be complex, specify the function input as complex within the arrayfun call.

x = [-1 0 1];
G = gpuArray(x);
A = arrayfun(@(p) sqrt(complex(p)),G)
   0.0000 + 1.0000i   0.0000 + 0.0000i   1.0000 + 0.0000i

Functions That Return Complex Data

This table lists the functions that might return complex data, along with the input range over which the output remains real.

FunctionInput Range for Real Output
acos, acosdabs(x) <= 1
acoshx >= 1
acothabs(x) >= 1
acsc, acscdabs(x) >= 1
asec, asecdabs(x) >= 1
asech0 <= x <= 1
asin, asindabs(x) <= 1
atanhabs(x) <= 1
logx >= 0
log1px >= -1
log10x >= 0
log2x >= 0
powerx >= 0
sqrtx >= 0

See Also

|

Topics