0

I am using parfor for parallel computing in Matlab. I am not familiar with this command. If that is possible, please look at my code below and tell me if I can write it with parfor. These errors and warnings are appear in Matlab Editor:

  • The parfor loop cannot be run due to the way variable Dat is used. (when I comment line Dat.normXpj = normXpj(pj,:); This error is solved and other errors similar to the following error is appeared.

  • The entire array or structure Bound is broadcast variable. This
    might result in unnecessary communication overhead.

    parfor pj = 1:size(normXpj,1)
    Dat.normXpj = normXpj(pj,:);
    
    if size(Dat.InitialGuess)==0
        X = (Bound(:,1)+(Bound(:,2)-Bound(:,1)).*rand(Nvar,1))';
    else
        X = Dat.InitialGuess;
    end
    
    [Xsqp, ~, FLAG,Options] = mopOPT(X,Dat);
    FEVALS = Options.funcCount;
    
    FES = FES+FEVALS;
    
    PSet(pj,:) = Xsqp;
    PFront(pj,:) = mop(Xsqp,Dat,0);
            if FLAG==-2
        disp('.......... Algo paso...');
    else
      F = PFront(pj,:);
      if Nobj==2
          plot(F(1,1),F(1,2),'*r'); grid on; hold on;
      elseif Nobj==3
    
       end
    end 
    end
    
6
  • hmm, No one helps? :( Commented Mar 4, 2015 at 19:16
  • Can you post the exact error message with Dat ? Commented Mar 4, 2015 at 20:00
  • this is what I got in Matlab Editor window when I mouse over parfor: The parfor loop cannot be run due to the way variable Dat is used @CrazyRat Commented Mar 4, 2015 at 20:35
  • OK. This is coherent with my answer. Commented Mar 4, 2015 at 20:39
  • so, really Noway? :(( @CrazyRat Commented Mar 4, 2015 at 20:51

2 Answers 2

2

The problem here is that it we can see that you're not using Dat in a way that is order-dependent, but the static analysis machinery of parfor cannot deduce that because of the way you're assigning into it. I think you can work around this by instead creating a whole new Dat for each iteration of the loop, like so:

Dat = struct('normXpj', rand(10,1), 'InitialGuess', 3);
normXpj = rand(10);
parfor idx = 1:10
    tmpDat = struct('normXpj', normXpj(:,idx), 'InitialGuess', Dat.InitialGuess);
    % use 'tmpDat'
    disp(tmpDat);
end
Sign up to request clarification or add additional context in comments.

13 Comments

Why do you assume that Dat is a structure and not an object ? As you don't know what's inside mopOPT(X,Dat) or mop(Xsqp,Dat,0) for instance, it's hard to say that creating a new Dat at each iteration is the solution.
The OP's new question is still not order-dependent, and so it can run in parfor. You simply need to assign a whole row at a time into A1, like so: A1 = zeros(SG,Nvar); parfor i = 1:SG, row = zeros(1, Nvar); row([i, SG + i]) = [1, -1]; A1(i, :) = row; end
It's no surprise it's much slower because it's not doing sufficient work to make parfor worthwhile - it's spending all the time shuffling the data back and forth. I'm confused as to what still doesn't work in your original question with the change to overwrite the whole value of Dat.
Hm, numerical differences can occur because parfor workers run in single-computational-thread mode - but usually this results in only very small differences. Sounds like something else is going wrong.
To open a parallel pool of the "default" size - i.e. with the same number of workers as there are cores - just call parpool('local') (providing you haven't modified your local profile). I would remove the pause, I can't see any reason that would help.
|
1

The answer is no, unfortunately. At line:

Dat.normXpj = normXpj(pj,:);

you assign a value to Dat.normXpj, but you have to know that in a parfor loop there can be multiple iterations executing at the same time. So what value should be used for Dat.normXpj ? Matlab cannot decide, hence your error.

More generally, your code looks quite messy. I suppose you want to use parfor to increase execution speed. Probably a more efficient option would be to use the profiler (see profile) to detect the bottlenecks in your code, and apply a correction if that's possible.

Best,

5 Comments

Dat.normXpj is a 3*55 Matrix which is remained constant during the program. This look is run for 55 times and an optimization problem with fmincon is run there. So thats the most time consuming part of the whole code. @crazy-rat
Well, if Dat.normXpj is constant, why do you redefine it at every iteration in your loop ? Please provide some coherent information.
It was a mistake. It changes every iteration, only the size of of loop size(normXpj,1) is kept constant. @CrazyRat
:((( bad times, Thanks for your help anyway @crazyrat
Edric answer is working, Thanks for your help as well. @CrazyRat

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.