Parallelization in Easyspin functions

General forum for EasySpin: questions, how to's, etc.
Post Reply
thanasis
Local Expert
Posts: 252
Joined: Thu Jan 21, 2016 6:28 am
Location: Strasbourg

Parallelization in Easyspin functions

Post 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!
Stefan Stoll
EasySpin Creator
Posts: 1100
Joined: Mon Jul 21, 2014 10:11 pm
Location: University of Washington

Re: Parallelization in Easyspin functions

Post 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.
thanasis
Local Expert
Posts: 252
Joined: Thu Jan 21, 2016 6:28 am
Location: Strasbourg

Re: Parallelization in Easyspin functions

Post 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?
joscha_nehrkorn
EasySpin Guru
Posts: 32
Joined: Wed Jan 20, 2016 11:35 am

Re: Parallelization in Easyspin functions

Post 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
thanasis
Local Expert
Posts: 252
Joined: Thu Jan 21, 2016 6:28 am
Location: Strasbourg

Re: Parallelization in Easyspin functions

Post by thanasis »

Yes, that was it.

Thanks Joscha!
Post Reply