Page 1 of 1

Parallelization in Easyspin functions

Posted: Thu Nov 03, 2016 8:09 am
by thanasis
My question is relevant to a previous (locked) post, "Parallelization and esfit".

Disclaimer: I am entirely new to parallelization and I just want to ascertain whether there is potential benefit for Easyspin. Implementing this will come after I do a lot more reading.

My current problem is running simulations (not fits) for systems with high Hilbert spaces (e.g. polynuclear clusters), and which take a long time to calculate.

E.g. can a simple pepper or curry simulation of a large system (i.e. without for-loops, so no parfor) benefit simply from being run on a multicore cluster? Does the code need to be enhanced, or can we simply benefit just from adding more cores and workers?

Should we expect that increasing e.g. from 4 to 16 cores, will decrease the calculation time to 1/4?

Apologies for asking a something potentially self-evident, but I have no previous experience and ES's documentation is not very detailed on this issue.

Thanks in advance!

Re: Parallelization in Easyspin functions

Posted: Fri Nov 04, 2016 2:48 pm
by Stefan Stoll
Some Matlab matrix functions have built-in support for multi-core systems, so you should see an effect on performance. However, is is unlikely to give you n-fold performance increase with n cores, since there is communication overhead.

Here is a link that explains Matlab's multithreading support.

Re: Parallelization in Easyspin functions

Posted: Mon Nov 07, 2016 11:34 am
by thanasis
OK, I started from what seems to be the simplest and most clear-cut case of parallelization:

I am calculating a series of systems with a for loop:

Code: Select all

for i=1:n
Sys(i).S = ...
Sys(i).g = ...;
etc
end

Exp...

pepper({Sys(1),Sys(2)...},Exp);

It has been executing very well and reliably. As Matlab's instructions say, if the loops are independent of one another, we can distribute them with a parfor loop.

So I fired up my parallel pool of... 2 workers on my laptop and replaced for with parfor (per Mathworks it should be as simple as that). But I get the error:
Analyzing and transferring files to the workers ...done.

Error using plot_triangle (line 68)
An UndefinedFunction error was thrown on the workers for 'Sys'. This might be because the file containing 'Sys' is not accessible on the workers.
Use addAttachedFiles(pool, files) to specify the required files to be attached. See the documentation for 'parallel.Pool/addAttachedFiles' for more
details.

Caused by:
Undefined function or variable "Sys".
Any thoughts?

Re: Parallelization in Easyspin functions

Posted: Tue Nov 08, 2016 8:53 am
by joscha_nehrkorn
parfor does not like structures. You have to define the structure inside the loop explicit with the struct() command. The following workaround work:

Code: Select all

clear all

Exp.mwFreq = 9.6; 
Exp.Range = [0 500];
parfor i=1:20
  Sys = struct('g', 2*rand);
  s(i,:) = pepper(Sys,Exp);
end

Re: Parallelization in Easyspin functions

Posted: Tue Nov 08, 2016 3:17 pm
by thanasis
Yes, that was it.

Thanks Joscha!