Function counts using parallel Fmincon

조회 수: 1 (최근 30일)
H R
H R 2017년 8월 31일
댓글: Walter Roberson 2017년 8월 31일
Hello all,
I would like to use parallel fmincon. The function receivea an input vector X, generates a file name say Filei.txt (i.e. File1.txt, File2.txt, File3.txt,... at each iteration), runs a external program for the file, and will produce an out put y.
function y= f(X)
% (1) 'count' is the function call number
% (2) genera a file using X and count
name=strcat( 'File.' ,num2str(count),'.txt');
% run an external program say C.exe which takes name as its input and return y
% return y
I would like to use the parallel Fmincon to minimize this function. However, I know that I can not use persisten with parallel fmincon to generate the variable 'count'. Could you please suggest what should I do in this case?

답변 (1개)

Walter Roberson
Walter Roberson 2017년 8월 31일
편집: Walter Roberson 2017년 8월 31일
Each worker has its own copy of the persistent variable. So use the local persistent variable together with the worker number to construct the file name. For example
t = getCurrentTask(); worker_number = t.ID;
name = sprintf('File.%d_%06d.txt', worker_number, count);
Question: is it important that the file names be sequential? Or do they just have to be different ? If they just have to be different, then use tempname()
  댓글 수: 2
H R
H R 2017년 8월 31일
편집: H R 2017년 8월 31일
Thank you Walter. The issue with worker_number ID is that it creates similar file names after each iteration as I think the t.ID would stay the same i.e. the files would be overwritten.
I would prefer the names to be sequential, as I just wanted to keep track of the files. In other words, I would like to know that which text file is generated at each iteration.
I have been thinking to use the current clock time as the name. Is there any way that the clock time includes a fraction of the second too? My fear is that some function calls would be at the same time.
Also, Could you please let me know how to use persistent as it cannot be used inside the function when I use the parallel fmincon (i.e. would persistent for a specific worker remains persistent throughout the parfor evaluations?)
Walter Roberson
Walter Roberson 2017년 8월 31일
You can use persistent in something called by parfor.
N = 123;
T = zeros(1,N);
parfor K = 1 : N; T(K) = tryper(); end
T
length(unique(T))
function c = tryper
persistent count
if isempty(count); count = 0; end
count = count + 1;
t = getCurrentTask(); worker_number = t.ID;
c = worker_number * 10000 + count;
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Direct Search에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by