Notify parallel workers within matlab - 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.

Related

How to implement an asynchronous parallel genetic algorithm using the MATLAB Parallel Computing Toolbox?

I am struggling to find a way to implement an asynchronous evolutionary process in MATLAB.
I've already implemented a "standard" GA that evaluates the fitness of individuals in parallel using MATLAB's parfor routine. However, this also means that the different workers in my parallel pool have to wait for the last worker to finish his assesment before I can create a new generation and continue with my parallel fitness evaluation. But when the fitness assessment time is very heterogeneous, such an approach can involve significant idle times.
The suggested solution in the literature is what is sometimes called asynchronous evolution. It means that there are no generations anymore, instead every time a worker becomes idle we immediately let him breed a new individual and evalute its fitness. As such there is hardly any idle time as no worker has to wait for others to complete their fitness assessment. The only problem is that those parallel threads/workers need to work on the same population, i.e. they need to communicate to each other whenever they want to breed a new individual (because they need to select parents out of a population that is constantly altered by the workers).
Now ideally you would control each worker separately, letting him access the joint population to select parents, evaluate the offspring's fitness and then replace a parent in the joint population. That involves an ongoing iterative process where parallel workers need to exchange and alter joint information and doing independent fitness evaluation.
My big question is: How can this be achieved in MATLAB?
Things I've already tried:
MATLAB's 'spmd'-functionality using the labSend and labReceive functions but this also seems to involve waiting for other workers.
creating jobs and assigning them tasks. Does not seem to work as multiple jobs are processed sequentially on the cluster. Only tasks within a job use multiple workers but that fails since you can't assign tasks dynamically (you always have to submit the whole job anew to the queue.
parfeval in a recursive while statement. I do not see how this would reduce waiting time.
So does anyone know a way how to implement something like this in MATLAB?

Doubts with parfor in 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.

Paralell computing opening and closing with calling parfor in MATLAB R2014a

I have a program that call a m-file that contains parfor for calculation. You know that in MATLAB R2014a we don't need open parallel computing using parpool or something likes that and parfor doing the same.
My question is about closing parallel computing. If i have this structure ( only parfor ) MATLAB closing parallel computing after ending process of parfor? I'm calling this parfor every 10 seconds. I don't want MATLAB close the pool in every iteration of my system.
Thanks.
From the documentation of parpool:
If you set your parallel preferences to automatically create a
parallel pool when necessary, you do not need to explicitly call the
parpool command. You might explicitly create a pool to control when
you incur the overhead time of setting it up, so the pool is ready for
subsequent parallel language constructs.
It is true that we don't have to use parpool, but it makes sense to use it if you want to control the overhead it causes.
As for your question - take a look at the Parallel Computing Toolbox Preferences:
I believe that the highlighted option is what was bothering you. If the default timeout is too short, you could either postpone it or disable it altogether.

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.