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

4 ビュー (過去 30 日間)
Camille Barker
Camille Barker 2023 年 9 月 18 日
編集済み: Dirk Engel 2024 年 4 月 11 日
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

回答 (1 件)

Dirk Engel
Dirk Engel 2024 年 4 月 11 日
編集済み: Dirk Engel 2024 年 4 月 11 日
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.

カテゴリ

Help Center および File ExchangeMATLAB Compiler SDK についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by