MATLAB: Assign a code to a specific core on Linux server

multi-core processing

I have several independent MATLAB codes, say A1.m, A2.m, …,A7.m and I want to use a 32 core Linux server to run them. Each code individually takes 1 day to be completed; however, when I run all of them simultaneously (using "nohup" command), it will take 7 days to be finished. I monitored the CPU information using "top" command, and it shows that 7 CPU cores are at ~100% capacity. My first question is, does CPU (by default) assign each code to a separate core? and if so, why they are not finished in one day (since they are independent from each other and are running on different cores)? My second question is, how can I specify on which core I want to run A1.m? Note that I have tried writing the codes as a single A.m file so that I can use MATLAB Parallel toolbox and "parfor" command (with 12 workers). However, it still takes 7 days to be finished. Also, the MATLAB version on the server is R2012b.

Best Answer

When "Each code individually takes 1 day to be completed" is the case, that probably includes the automatic parallelization that MATLAB does for "large enough" mathematical operations that fit into some of the patterns that can be handled by BLAS or LAPACK or similar highly optimized parallel libraries. You do not need the Parallel Processing Toolbox for that to happen.
A MATLAB program that detects that it can use those parallelized libraries will call into the libraries, using up to MaxNumCompThreads which in turn probably depends upon your environment variables
If you run a bunch of independent MATLAB sessions all of which are trying to access the full set of processors, then you get contention amongst the processors which slows things down even further than the difficulty that a limited resource is being split among multiple programs.
If you run code that uses automatic parallelization while you are in a parpool but not within an explicitly parallel section, then the maximum number of processors uses is determined by the size of the parpool.
If you run code that uses automatic parallelization while you are in a particular worker of a parpool (because of an explicit parallel programming call such as parfor or spmd) then the code is restricted to a single CPU.
The official route to assigning tasks to individual processors for MATLAB is to use the Distributed Computing Server and set up multiple profiles each with a different core reservation, and then to submit your tasks to the different server profiles.