Azzera filtri
Azzera filtri

Is it possible to call a MATLAB .dll from C# in parallel?

10 visualizzazioni (ultimi 30 giorni)
Using MATLAB compiler SDK, I have compiled a matlab .dll called IAP that has a class called Analysis. This class performs some analysis on a single sample which takes about 20~ minutes. Often I need to perform this analysis on several samples so to speed up the process I would like to run samples in parallel but it seems they are running sequentially when I use a Parallel.For loop in my C# application.
This is how I am calling in my C# application:
Analysis analysis = new Analysis();
/*
* SET UP INPUTS
* FOR ANALYSIS HERE
*/
Parallel.For(0, samples.NumberOfElements, i => {
Thread thread = Thread.CurrentThread;
System.Diagnostics.Debug.WriteLine($"working on sample = {i}, thread = {thread.ManagedThreadId}, bckgrnd = {thread.IsBackground}, state = {thread.ThreadState}");
results[i] = analysis.run_analysis(1, samples_arr[i], inputs);
});
OUTPUT:
working on sample = 1, thread = 5, bckgrnd = True, state = Background
working on sample = 0, thread = 8, bckgrnd = True, state = Background
working on sample = 2, thread = 3, bckgrnd = True, state = Background
working on sample = 3, thread = 6, bckgrnd = True, state = Background
working on sample = 4, thread = 15, bckgrnd = True, state = Background
working on sample = 5, thread = 16, bckgrnd = True, state = Background
working on sample = 6, thread = 17, bckgrnd = True, state = Background
working on sample = 7, thread = 18, bckgrnd = True, state = Background
I can see in the output window that the threads are set up but when I look at my analysis output files I can see only one sample is being ran.
Why is this happening and is there some way I can run the samples in parallel using the matlab .dll?
TY

Risposte (1)

Dirk Engel
Dirk Engel il 11 Apr 2024
Modificato: Dirk Engel il 11 Apr 2024
No, because the MATLAB runtime instance that is used under the hood to execute your run_analysis() method is single-threaded. If you call the method in parallel from a multi-threaded application, the calls will be queued and processed sequentially. You would need to parallelize your m-code instead by using the Parallel Computing Toolbox.

Categorie

Scopri di più su MATLAB Compiler SDK in Help Center e File Exchange

Prodotti


Release

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by