Does the batch job run on a single worker if the code batched doesn't contain any parallel for loop?

10 views (last 30 days)
I got a code file, which solves a large sparse matrix, about 40000×40000, by calling function 'eigs'.
When I first run the code by simply typing its file name, like 'myfile.m', in the command window and pressing Enter, the code is running as it should be. I checked that matlab client is running with multiple threads. This indicates that the inner function EIGS itself can run parallelly. The computing time that the code cosumes is acceptable.
Now, I have to change the input varibles of myfile to run as many as tens of times manually. So I choose using function BATCH to submit jobs in sequency.
When I batch myfile, I found the client is running with only one worker. It does not run parallelly automatically as function EIGS indicate. I check in the tutorials and know BATCH will transfer myfile to a worker of the cluster. Since myfile has no any parfor loops, that's why the parallel pool does not start, even if function EIGS is in the code? And it costs a lot time to compute. So is there any way that can make the batched jobs myfile still run in a parallel mode, with no parfor but eigs? The time cost is appreciable with no parallel computing.
Thanks in advance!
  1 Comment
shuai li
shuai li on 3 Dec 2020
the code runs in a parallel way without BATCH. When I call batch(@myfile,1,{inputs}), it seems the code will only run on a single worker. Should that be so? Or any idea to make batch jobs still run with mutiple threads? Remind: no parfor loop but eigs in myfile.

Sign in to comment.

Accepted Answer

Raymond Norris
Raymond Norris on 4 Dec 2020
Your local machine is making use of maxNumCompThreads, which is set to the number of physical cores on your machine. The 'parallelism' is threads, not processes, that are spawned by eigs onto those cores.
When you call batch, you can pass it a pool argument, specifying how many workers (processes) should start up. batch will request one additional worker to act as your proxy MATLAB client. For example,
j = batch(...,'Pool',3);
will reqest 4 workers run on your cluster. As you've noticed, you don't need additional workers for your code. You just need the single worker that batch will request, which in turn needs to spawn threads (across several cores).
Your question then is, why does eigs run with multiple threads locally, but only a single thread on the cluster? By default, MATLAB will only request a single thread per worker. To increase this, set the number of threads per worker, as such (8 is only an example)
c = parcluster(name-of-cluster-profile);
c.NumThreads = 8;
j = c.batch('myfile');
If, for some reason, that doesn't work, you can call maxNumCompThreads in myfile. The caveat is that setting NumThreads should request additional cores (needed for the additional threads). If setting NumThreads didn't work, you'll only get a single core. Then, calling maxNumCompThreads may have one of the following consequences
  1. Starting threads on cores you don't own
  2. Starting all on the same core, if cgroups are implemented
Neither is desireable.
  2 Comments
shuai li
shuai li on 7 Dec 2020
Thanks, Raymond. Your advise helps a lot. I recently read some Technical Articles, where the authors tested the execution time of Multithreaded Math Libraries Within Multiple MATLAB Tasks. While the number of labs gets larger, the execution speed for tasks using more than 1 thread per lab can be even lower than that using only 1 thread, because the multiple cores cannot get data from memory fast enough.
For my calculation, multiple threads may not help accelerating the execution speed. I have tested that, as the Articles do.
Raymond Norris
Raymond Norris on 7 Dec 2020
I suspect the article you're referring to is hyper-threading, where the physical core is "split". This can lead to memory bottlenecks, since both cores are contending for resources on the memory bus.
Our multi-threaded support uses physical cores -- the computational threads run on their own core. On a single node, you either want to use implicit multi-threading (which you get for free) or explicit multi-core processing (by explicitly calling parfor). If calling parfor doesn't speed things up linearly (assuming there's enough work to do), it probably means your computations where already being speed up by the multi-threading. Starting a parpool, by default, will disble the multi-threading on the workers for this reason.

Sign in to comment.

More Answers (0)

Categories

Find more on Parallel Computing Fundamentals in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!