> while using parfor loop, I get "The process cannot access the file > because it is being used by another process." error generated from an > executable function embedded in the parfor loop. The function opens a > file "tmp.key" and writes an image file "tmp.pgm" into "tmp.key" > file. However, it seems that when this parfor loop runs on parallel > processors, the processors are unable to access this "tmp.key" at the > same time, and hence, the error is generated. First of all, is my > assesment correct? secondly, how to resolve this issue and use parfor > loop successfully. > > Any help is greatly appreciated, please.
It sounds like your assessment is correct. By default, we keep matlabpool workers having the same current directory as your MATLAB client, but it sounds like you need to override this when calling your executable. I'd proceed by making a wrapper function around the executable which does something like this:
%---8<---8<---8<---8<---8<--- function result = wrapCallToExecutable(args) t = getCurrentTask(); % empty on the client if ~isempty(t) % Get a unique directory name and create that uniqueDirName = tempname(); ok = mkdir(uniqueDirName); assert(ok);
% remember the old working directory, and use onCleanup % to get back there oldPwd = pwd; restorePwd = onCleanup(@() cd(oldPwd));
% make an onCleanup to remove the temporary directory removeTempDir = onCleanup(@() rmdir(uniqueDirName, 's')); % move to the unique directory cd(uniqueDirName); end