How to run a MEX function asynchronous in Matlab?
Mostra commenti meno recenti
Hi,
I have developed a MEX function (in CUDA) which uses a GPU for processing the data transfered from a device (let's call is D1) to my PC. D1 uses Matlab to communicate with my PC. In a sequential way, once the data is transferred from D1 to my PC, the MEX function starts to process. The transfer time from D1 to PC takes about 0.3 s. The MEX function takes about 0.4 s to finish the processing. So, it total I need 0.7 s to get the ouput of the MEX function.
Now, I've been thinking that maybe I could run the MEX function asynchronous to data transfer from D1 to PC; This means that in parallel of processing in the MEX function, I want to transfer the data from D1 to Matlab. In this way, I do not have to wait for 0.3 s (of data transfer) because it is done during processing of the MEX function. Could you please let me know if this is possible in Matlab or not. And if yes, how?
Thanks in advance.
Moein.
5 Commenti
Joss Knight
il 5 Lug 2021
Sure, you can do whatever you like inside your MEX function, including getting data from a device and processing it using CUDA libraries or kernels. If you allocate your GPU memory using MATLAB's mxGPU APIs (such as mxGPUCreateGPUArray) you will be able to take advantage of MATLAB's memory pooling to avoid the synchronization that happens when you call the CUDA memory allocation routines.
However, what I don't understand is how you can benefit from asynchronous execution here? Surely you need to wait for your data to be transferred from D1 to PC before you can process it?
Moein Mozaffarzadeh
il 5 Lug 2021
Joss Knight
il 8 Lug 2021
Cool. In which case, go for it. Start asynchronously copying data from the device into an output mxArray and then process the input mxArray with your CUDA kernel. I presume you have some sort of asynchronous API to do this, or else you can launch a thread to do it and rejoin it before returning.
Moein Mozaffarzadeh
il 8 Lug 2021
Modificato: Moein Mozaffarzadeh
il 8 Lug 2021
Joss Knight
il 12 Lug 2021
Right, so you can't do the data transfer in C++ code inside your MEX function?
In which case use some form of parallel pool and run your data transfer in the background. You might want to look at the documentation for parfeval. You can just set a whole bunch of data transfer going in the background and use fetchNext to pull data off the queue at the start of each iteration.
Risposte (0)
Categorie
Scopri di più su GPU Computing in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
