2

I am investigating the possibility of employing a .NET compiled MATLAB assembly (obtained using the Matlab Compiler Runtime) to perform simultaneous (in a multithreaded fashion) execution of several pieces of code.

My code reads

Thread t = new Thread(new ThreadStart(
    () =>
    {
        dotnetclass AClass_1 = new dotnetclass();
        Stopwatch sw = new Stopwatch();
        sw.Start();
        AClass_1.math_on_numbers(2, a, b);
        sw.Stop();
        Console.WriteLine("Elapsed (1): " + sw.Elapsed);
    }));
Thread t2 = new Thread(new ThreadStart(
    () =>
    {
        dotnetclass AClass_2 = new dotnetclass();
        Stopwatch sw = new Stopwatch();
        sw.Start();
        AClass_2.math_on_numbers(2, a, b);
        sw.Stop();
        Console.WriteLine("Elapsed (2): " + sw.Elapsed);
    }));  
t.Start();
t2.Start();

where dotnetclass is defined in a MATLAB .NET assembly, and the function math_on_numbers performs some dummy mathematical operation and sleeps 3 seconds before returning.

Executing the above code, I observe the "Elapsed (1)" message after 3 seconds, and the "Elapsed (2)" message after 6 seconds. This would make me think that concurrent calls to the MATLAB runtime will always be queued in a single-threaded fashion.

My questions:

  1. Is there a way to allow multithreading in this scenario?

  2. In other frameworks (I'm especially interested in a Java environment), would this limitation still occur?

1 Answer 1

3

Do you have the Parallel Computing Toolbox for MATLAB? You can use that in combination with the MATLAB Compiler to produce .NET assemblies that perform parallel computations either with processor cores available on the local machine or by submitting the job to a cluster. Otherwise, your MATLAB code will run sequentially. By the way, the Parallel Computing Toolbox is based on MPI and is structured to operate on "jobs" and "tasks". Threads are a much lower-level abstraction and more difficult to use correctly.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Matt, thanks for your answer. If I understand correctly, even using the Parallel Computing Toolbox, the number of concurrent running jobs would be limited by the number of available processors. Is that correct?
You can run as many worker processes as you have sufficient licensing for. However, for performance reasons it only makes sense to run a number of workers equal to the number of processor cores available (or less, if there are other applications besides MATLAB that are also using CPU resources).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.