Doubts with parfor in Matlab? - matlab

Could you explain me why
the following code with parfor in Matlab does not work and how to fix it?
R=10;
Power=zeros(2,R);
parfor s=1:R
Power(1,s)=1
Power(2,s)=2;
end

It doesn't work because you have 1 variable that is sent to different workers (power) and you want to write on it using different cores.
How can you write in the same variable with different workers? who stores the memory? How do workers communicate where did they write and where not? The structure of the code is very important when doing parallel computing, as you need to be aware of what memory you send to what worker. Just choosing a wrong approach in passing variables can make your code slower than the non-parallel one.
The code you show can be changed to:
R=10;
Power1=zeros(1,R);
Power2=zeros(1,R);
parfor s=1:R
Power1(1,s)=1
Power2(1,s)=2;
end
Power=[Power1;Power2]
I suggest you go to http://uk.mathworks.com/help/distcomp/parallel-for-loops-parfor.html
and read the "concepts" section, especially the variable types in parfors, that the MATLAB error directs you to.

Related

parallel code by parfor is slower than serial version by for

I have written a code by matlab and i've used parallel computing toolbox
more description about my code:
i'm trying to implement parallel genetic algorithm by matlab and parallel computing toolbox.
i've implemented that but i've a problem. that is my parallel code with parfor is so much slower that serial one with for.
my code:
tic
for j=1:maxIteration
parfor i=1:numIslands
if migrationInterval
doMigration;
end
doCrossover;
doMutation;
newSpring;
end
end
toc
numIslands is always small number (5 to 12)
maxiteration is always big number (1500 to 5000)
please help me
thank you
I recommend you to run your function using "Run and Time" tool. The results will show, if the reason is in parfor-procedure or in your function.
It can be the parfor procedure is unnecesary and gives no advantage, but it depends always on the function you run.
You mention that your CPU has two cores. One problem might being the code itself, it looks as if you are calling scripts instead of functions, so you might be flooding your workspace unnecessarily by doing so. Furthermore, if any of those scripts is declaring variables on the fly, you might be clogging your RAM (Matlab is particularly good at that) thus making your code run slower.
Try to optimize each of the scripts first.
I would really recommend you to use functions instead of scripts though.

Notify parallel workers within matlab

I'm using the parallel toolbox of matlab. Does anyone knows a way to inform parallel workers to update a global variable?
Thanks for any advice!
The parallel workers are separate MATLAB processes, and do not share global state either with each other, or with the MATLAB client process. You ''can'' declare and use global data on workers (although you are not permitted to place a global declaration directly in the body of an spmd block or a parfor loop), but this is not ideal since you'll need to take extra steps to ensure that the workers have the correct values.

How to use parallel 'for' loop in Octave or Scilab?

I have two for loops running in my Matlab code. The inner loop is parallelized using Matlabpool in 12 processors (which is maximum Matlab allows in a single machine).
I dont have Distributed computing license. Please help me how to do it using Octave or Scilab. I just want to parallelize 'for' loop ONLY.
There are some broken links given while I searched for it in google.
parfor is not really implemented in octave yet. The keyword is accepted, but is a mere synonym of for (http://octave.1599824.n4.nabble.com/Parfor-td4630575.html).
The pararrayfun and parcellfun functions of the parallel package are handy on multicore machines.
They are often a good replacement to a parfor loop.
For examples, see
http://wiki.octave.org/Parallel_package.
To install, issue (just once)
pkg install -forge parallel
And then, once on each session
pkg load parallel
before using the functions
In Scilab you can use parallel_run:
function a=g(arg1)
a=arg1*arg1
endfunction
res=parallel_run(1:10, g);
Limitations
uses only one core on Windows platforms.
For now, parallel_run only handles arguments and results of scalar matrices of real values and the types argument is not used
one should not rely on side effects such as modifying variables from outer scope : only the data stored into the result variables will be copied back into the calling environment.
macros called by parallel_run are not allowed to use the JVM
no stack resizing (via gstacksize() or via stacksize()) should take place during a call to parallel_run
In GNU Octave you can use the parfor construct:
parfor i=1:10
# do stuff that may run in parallel
endparfor
For more info: help parfor
To see a list of Free and Open Source alternatives to MATLAB-SIMULINK please check its Alternativeto page or my answer here. Specifically for SIMULINK alternatives see this post.
something you should consider is the difference between vectorized, parallel, concurrent, asynchronous and multithreaded computing. Without going much into the details vectorized programing is a way to avoid ugly for-loops. For example map function and list comprehension on Python is vectorised computation. It is the way you write the code not necesarily how it is being handled by the computer. Parallel computation, mostly used for GPU computing (data paralleism), is when you run massive amount of arithmetic on big arrays, using GPU computational units. There is also task parallelism which mostly refers to ruing a task on multiple threads, each processed by a separate CPU core. Concurrent or asynchronous is when you have just one computational unit, but it does multiple jobs at the same time, without blocking the processor unconditionally. Basically like a mom cooking and cleaning and taking care of its kid at the same time but doing only one job at the time :)
Given the above description there are lot in the FOSS world for each one of these. For Scilab specifically check this page. There is MPI interface for distributed computation (multithreading/parallelism on multiple computers). OpenCL interfaces for GPU/data-parallel computation. OpenMP interface for multithreading/task-parallelism. The feval functions is not parallelism but a way to vectorize a conventional function.Scilab matrix arithmetic and parallel_run are vectorized or parallel depending to the platform, hardware and version of the Scilab.

Running identical Matlab scripts on multiple local threads

I have a quad-core desktop computer
I have the Parallel Computing toolbox in Matlab.
I have a script file that I need to run simultaneously on each core
I'm not sure what the most efficient way to do this is, I know I can create a 'matlabpool' with 4 local workers, but how do I then assign the same script to each one? Or can I use the 'batch' command to run the script on a specific thread, then do that for each one?
Thank you!
You can run a single script using multiple cores using the Parallel Computing toolbox, by using
matlabpool open local 4
and using parfor instead of for loops to execute whatever is in your loop across four threads. I'm not sure if Parallel Computing toolbox supports running the entirety of the script individually on each core, this will likely not be supported by your hardware.
Not sure if this works, but here is something to try:
When trying to paralelize calculations, they are usually wrapped with something like parfor
So I would recommend doing the same with your script, make sure that all required inputs and outputs have the neccesary dimensions and just call:
parfor ii = 1:4
myscript;
end
Sidenote: Before trying this kind of stuff you may want to check your cpu utilization. If it is already high that means that the inner part of the code uses parallel processing and you should not expect much speedup.

Share variables between two MATLAB m-files executing separately

I'd like to know if there is something like pointers in MATLAB.
I have two matlab routines which execute simulataneously (that is they are lunched together and run on the same machine, which is, therefore, synchronized in terms of time-stamps).
The first routine, A, has a parfor loop where a data vector is continuously updated. The second routine, B, needs to get access to a specific (but variable in time) row of the vector (of routine A) and do further calculation.
My first guess is to print the updating rows of routine A in a txt file and then get access to it in B when necessary. However, this will result in a large amount of waste time.
I know that this could be related to parallel jobs and scheduler but I dont know how to implement it.
Thank you for any help, guesses or solutions.
Probably this code will help you, if i got correctly your problem. I am waiting for further comments.
Best wishes!