How to get username in Parallel Computing

조회 수: 8 (최근 30일)
Stefano Rognoni
Stefano Rognoni 2015년 7월 8일
답변: Stefano Rognoni 2015년 7월 14일
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?

채택된 답변

Stefano Rognoni
Stefano Rognoni 2015년 7월 14일
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.

추가 답변 (1개)

Edric Ellis
Edric Ellis 2015년 7월 8일
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.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by