How to get username in Parallel Computing

7 visualizzazioni (ultimi 30 giorni)
I'm using MatLab Parallel Computing Toolbox on several computers in my office: I've enstabilished a cluster managed by a custom scheduler (the MatLab's own kind: the MJS).
Everything seems to run smoothly as long as I run scripts and functions that have the same path on every PC (e.g. C:\Sample.m), but unfortunately I need to access to functions that are in the Dropbox folder of every computer (for example the file C:\Users\myusername\Dropbox\Sample.m).
To solve the problem, I tried something like this:
parfor j=1:100
myusername=getenv('username');
cd(['C:\Users\',myusername,'\Dropbox'])
Sample(1,2,3);
end
But with this code the "cd" function set the current folder in C:\Users\SYSTEM\Dropbox and obviously it can't find Sample.m in it.
Has anybody any way around this problem?

Risposta accettata

Stefano Rognoni
Stefano Rognoni il 14 Lug 2015
I solved this by creating a function called "nome":
function [username]=nome()
username='Stefano';
end
I put this function in the matlabroot in every PC on the cluster BUT instead of putting 'Stefano' in each one I put the name of the user in which the Dropbox function is placed. The I had to use cd in the right way:
cdorig=cd;
spmd
cd(matlabroot);
username=nome();
end
spmd
mypath=strsplit(cdorig,'\');
mypath{3}=username;
cd(fullfile(mypath{1:end}));
end
And it works.

Più risposte (1)

Edric Ellis
Edric Ellis il 8 Lug 2015
You have several options here. First, you could simply compute your username outside the parfor loop
myusername = getenv('username');
parfor ...
...
cd(['c:\Users\', myusername, '\Dropbox']);
...
end
If you're running a more recent version of MATLAB, then there is automatic dependency analysis that should transfer the functions that you need to the workers when required - which version are you using? You can use listAutoAttachedFiles to see what files have been attached to your pool.
Finally, if you get your MJS administrator to run at security level 3, then the workers will run with your credentials, and getenv('username') will behave as you expect on the workers.

Categorie

Scopri di più su Startup and Shutdown in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by