Clear Filters
Clear Filters

Parallel Computing in C when using Matlab Coder (parpool and gpuArray)

15 views (last 30 days)
In Matlab I'm able to run my code parallel either with utilizing "parpool" or "gpuArray".
Now, with the help of Matlab Coder I'm able to convert my code in C by generating a dynamic library, which I can access and run via Visual Studio.
Still, I have no clue how to run the generated code in Visual Studio parallel.
My thought was:
Using parpool and gpuArray in Matlab
-> Converting via Matlab Coder in dynamic library
-> Running in Visual Studio parallelized Code
I'm aware that Matlab is multi-processing and C is multi-threading, but I hope there is a possibility to tell Matlab during the convertation to get "multi-processing in multi-threading".
Thank you for any help in advance!

Accepted Answer

Hassaan on 8 Jan 2024
When you convert MATLAB code to C/C++ using MATLAB Coder, the parallelism from high-level constructs like parpool or gpuArray isn't automatically translated to parallel C/C++ code. To run code in parallel in C/C++, you need to use libraries or APIs designed for this purpose.
Here are steps to parallelize the generated C/C++ code in Visual Studio:
  1. Review the Generated Code: Check the generated code to understand its structure and where parallelism might be beneficial.
  2. Parallel Libraries in C/C++:
  • Use OpenMP for CPU multi-threading. It's supported by Visual Studio and allows you to add parallelism with simple #pragma directives.
  • For GPU acceleration, you can use CUDA (if the GPU is from NVIDIA) or OpenCL. These are more complex and require a good understanding of GPU programming.
  1. OpenMP Example:
  • Enable OpenMP in Visual Studio by going to Project Properties -> C/C++ -> Language -> OpenMP Support.
  • Add OpenMP directives to your C/C++ code:
#pragma omp parallel for
for (int i = 0; i < N; ++i) {
// Your parallel code here
  1. CUDA or OpenCL:
  • If you used gpuArray in MATLAB, this indicates that the code may benefit from GPU acceleration.
  • Learn the basics of CUDA or OpenCL to understand how to offload computations to the GPU.
  • Write CUDA kernels or OpenCL programs that reflect the operations you performed with gpuArray.
  1. Manual Conversion:
  • Identify the parts of the MATLAB code that were running under parpool or using gpuArray.
  • Manually convert these parts to use OpenMP, CUDA, or OpenCL.
  1. Testing and Profiling:
  • After adding parallelism, test the code thoroughly to ensure it behaves correctly.
  • Use profiling tools to understand performance bottlenecks and optimize the parallel sections.
  1. Integration:
  • Make sure that your parallel code is well-integrated with the rest of the C/C++ code that was generated by MATLAB Coder.
  • Ensure proper data transfer between CPU and GPU if using CUDA or OpenCL.
Keep in mind that MATLAB's parallel computing toolbox and GPU support handle a lot of complexity behind the scenes. When converting to C/C++, you'll need to handle this complexity manually. It's a non-trivial task that requires good knowledge of parallel programming paradigms and the associated C/C++ libraries or extensions.
Also, the C code generated by MATLAB Coder is not meant to be human-readable or human-editable, it is meant to be functionally equivalent to the original MATLAB code. If you need to add parallelism to the generated code, it might be better to write new code that calls the generated code where necessary but manages parallelism separately in a way that you can control and understand.
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

More Answers (1)

Joss Knight
Joss Knight on 13 Jan 2024
GPU Coder will generate CUDA code for you. It can even automatically vectorize for loops. For a multithreaded parallel for loop, you're probably better off generating code for an ordinary for loop and then annotating it for appropriate OpenMP or TBB extensions. parfor is not supported for code generation.
Joss Knight
Joss Knight on 15 Jan 2024
  • GPU Coder and Parallel Computing Toolbox have different licenses. Maybe you have one and not the other. However, to generate or use GPU mex functions, GPU Coder requires a Parallel Computing Toolbox license.
  • GPU Coder requires a compiler to be installed and MinGW does not work.
  • The workflow is different. If you want to use Coder for desktop computation you have to first compile and then integrate a mex function into your MATLAB code. You would need to separately maintain the mex function code and the code using it.
  • GPU Coder can vectorize for loops for you.
  • Coder generates CUDA kernels that you can subsequently edit.
  • There will be differences in performance and precise answers, since the implementations are not the same.
Wladimir Plotnikov
Wladimir Plotnikov on 16 Jan 2024
Thank you for the detailed answer.
I will deal with it as soon as possible.
Fortunately, my university apparently has all the necessary licenses, so that I can try out the GPU Coder.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!