User`s guide
9 GPU Computing
9-14
Example: Run Your MATLAB Code
In this example, a small function applies correction data to an array of measurement
data. The function defined in the file myCal.m is:
function c = myCal(rawdata, gain, offst)
c = (rawdata .* gain) + offst;
The function performs only element-wise operations when applying a gain factor and
offset to each element of the rawdata array.
Create some nominal measurement:
meas = ones(1000)*3; % 1000-by-1000 matrix
The function allows the gain and offset to be arrays of the same size as rawdata, so that
unique corrections can be applied to individual measurements. In a typical situation, you
might keep the correction data on the GPU so that you do not have to transfer it for each
application:
gn = rand(1000,'gpuArray')/100 + 0.995;
offs = rand(1000,'gpuArray')/50 - 0.01;
Run your calibration function on the GPU:
corrected = arrayfun(@myCal,meas,gn,offs);
This runs on the GPU because the input arguments gn and offs are already in GPU
memory.
Retrieve the corrected results from the GPU to the MATLAB workspace:
results = gather(corrected);