How can I generate CPU-optimized code for some functions when GPU Coder is used for code generation?

I have two MATLAB functions that I would like to generate code for, "cpu_fun" and "gpu_fun".
"cpu_fun" has maximum performance when it runs on the CPU, whereas "gpu_fun" has maximum performance when it runs on the GPU. 
I would like to configure GPU Coder or MATLAB Coder to generate code such that "cpu_fun" runs on the CPU and "gpu_fun" runs on the GPU for the best performance.

 Réponse acceptée

To extract maximum performance for a project where some functions run faster on the GPU and some functions run faster on the CPU, there are two possible workflows:
  1. Use GPU Coder to generate code for the entire project.
    1. If "gpu_fun" is the entry point function and internally calls "cpu_fun", add "coder.inline('never')" at the start of the "cpu_fun" function, and add "coder.gpu.kernelfun" at the start of the "gpu_fun" function.
    2. If cpu_fun is the entry point function and internally calls "gpu_fun", add "coder.line('never')" and "coder.gpu.kernelfun" at the start of the "gpu_fun" function, and add "coder.gpu.kernel" at the end of the "cpu_fun" function.
  2. Generate code for both functions separately. I.e., use MATLAB Coder for functions that have better performance on CPU and GPU Coder for functions that have better performance on GPU. After this, you will have to manually integrate the code generated from MATLAB Coder to the code generated by GPU Coder and re-compile the code. Note that this may not always be possible due to the functional dependencies of your project.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with GPU Coder dans Centre d'aide et File Exchange

Produits

Version

R2025b

Community Treasure Hunt

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

Start Hunting!

Translated by