How do I implement functions like SINC on GPUs without using either loops or conditions in MATLAB 7.11 (R2010b)?

5 次查看(过去 30 天)
I would like to implement functions like SINC or GPU without using loops or conditions.

回答(1 个)

Sanchali Purandare
Sanchali Purandare 2011-1-16
Conditonal checks are not allowed in GPU kernels. There is no in-built function to filter out the NaN values from vectors in GPU kernels. This leads to interesting implementation of functions like SINC(x) which require filtering out NaN values in cases where supported functions like ISNAN return NaNs as logicals.
In such a scenario, filtering out the NaN values from a vector containing non-zeros and NaN's can be done using masking.
No value generated by SINC function will exceed 1 by definition which can be demonstrated by the following code:
a = -10*pi:0.01:10*pi;
plot(sinc(a));
MAX or MIN comparison of any number with NaN gives the same number which can be demonstrated by the following code:
b = NaN;
c = -1e-10;
max(b,c)
min(b,c)
Since MAX and MIN are supported on GPUs and work well on vectors, taking this into account, for the case of SINC, without using either loops or condition variables, we can create masking to filter out the NaN values.
This can be further illustrated by the following example using ONES (because the upper limit of SINC is 1)to generate the 'masking' vector:
a = [ .3 .4 NaN .5]
b = ones(size(a))
c = min(a,b)
A = gpuarray(a)
B = ones(size(A))
C = min(A,B)% this will create a GPUArray C or you can overwrite A
cc = gather(C)
isequal(c,cc) % the result was true on my machine
This approach will work as long as the MAX or MIN values of the input function are known before hand.

类别

Help CenterFile Exchange 中查找有关 GPU Computing 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by