how to run a function on GPU cores
10 views (last 30 days)
Show older comments
Answered: Joss Knight on 26 Jan 2023
I want to run a very simple custom function on GPU. This function repeat reading an image and perform some predictions using an available machine learning model (mdl) by the "predict" command. Can anyone guide me in performing these calculations on each GPU core?
I am not sure if my understanding is correct but using CPU parallel processing, we can run such commands on each core at a time. For instance, if we have 12 images and 12 cores in our CPU, then using the "parfor loop", we can run the function on 12 images simultaneously. I want to do this using the GPU. I know that GPUs usually have a lot of cores (~1000s), so it would be great if we could have a function like "parfor" for GPU-based parallel processing.
I would appreciate any help you could provide.
Matt J on 24 Jan 2023
so it would be great if we could have a function like "parfor" for GPU-based parallel processing.
No, you cannot use GPU cores in a manner that is analogous to parpool workers. It is a very different ype of "core" than what CPUs have.
More Answers (2)
Walter Roberson on 24 Jan 2023
You would have to use https://www.mathworks.com/help/gpucoder/index.html
Nvidia GPUs are constructed in a hierarchy. GPU cores individually cannot decode instructions and cannot run programs independently of others in the same group. GPU cores are grouped together and each group has a controller. Controllers are somewhat independent of each other. Each controller decodes an instruction and makes the information available on a bus shared by the group of cores. Each core in the group then executes the same instruction, just with different addresses.
Conditional work is not handled by having different cores in a group execute different instructions. Instead, the controller has a way of informing each core whether it is to perform the action, or if instead it should just idle. So if/else is handled by having a subset of the cores sit idle for one branch, and then the controller changes the information about which cores are to take part and has the other cores sit idle when some cores do work.
In traditional CPUs, each physical core (not hyperthreaded core) has its own instruction decoder and register set and can execute independently. The number of simultaneous independent programs is based on the number of physical cores.
In Nvidia GPUs, the number of simultaneous independent programs is based not on the number of cores, but rather on the number of controllers. The old generations of Nvidia GPUs started with only two or three controllers. The number has increased a fair bit in more modern models, but you should still expect each controller to be controlling at least 128 cores.
Joss Knight on 26 Jan 2023
It's probably best to read the extensive documentation available online that should give you a complete picture of how to leverage your GPU and what applications it is useful for.
Find more on GPU Computing in MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!