Can I run custom Matlab function or gpuArray on another GPU?

8 views (last 30 days)
Hello, everyone
I work on image processing and handle large scale images. I designed an algorithm that could get the result I wanted by multiple iterations, and it ran successfully.
%% code example
input = zeros(500,500,50);
a = zeros(500,500,50);
b = zeros(500,500,50);
c = zeros(500,500,50);
d = zeros(500,500,50);
input = gpuArray(input);
a = gpuArray(a);
b = gpuArray(b);
c = gpuArray(c);
d = gpuArray(d);
for i=1:100
a = input + d;
b = a + input;
c = b + a;
d = func1(c);
end
I use the gpuArray provided by matlab to speed up operations.But the size of the image I'm processing is constrained by the size of the GPU's memory.
Can I load c,d arrays on another gpu? Or can I run func1 on another GPU2? I want to reduce the consumption of memory of my current GPU1. Do you have any suggestions?

Accepted Answer

Joss Knight
Joss Knight on 20 Apr 2022
You can use parallel syntax to process other arrays on other GPUs at the same time, or to process some data on the CPU at the same time as processing others on the GPU. Try this documentation for some examples.
In your code example your initial arrays a, b and c are all immediately overwritten by other variables. Does that matter? Anyway, worth checking.
  5 Comments
Joss Knight
Joss Knight on 21 Apr 2022
Hi Zhenhong. It does look as if your algorithm may be inherently serial so there may be no way to parallelize. However, it is often true that a serial algorithm can be computed in parallel chunks, for instance, a sum a+b+c+d can be computed by calculating (a+b) and (c+d) in parallel before summing the result. So perhaps rethinking your algorithm might be a way to start. Alternatively, if you call your entire algorithm multiple times with different inputs you could do each call in parallel.
As for device selection, you shouldn't be doing that. The point is to select a different device on each worker, but this is done for you automatically. If you select the device manually you will reset the device and clear all GPU variables. If you have two devices and you open a pool with two workers, for instance by going parpool('local',gpuDeviceCount), then any operations on those workers will take place on different devices.
There are various other odd issues with your code but of course I understand it's just a simplified example. You shouldn't need to gather the inputs to func1, because it supports gpuArray inputs. It seems you did it to switch device, but as I pointed out you should never do that within a worker. When results are returned from the worker back to the client MATLAB, they are automatically transferred from whatever GPU the worker is using to the client's GPU.
Zhenhong Du
Zhenhong Du on 21 Apr 2022
I understand. Despite some frustration, thank you very much for your patient response.

Sign in to comment.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!