-1

I'm using parfor, unfortunately two of my variables, x and y, which are both matrices, get broadcasted and don't know how to avoid it. I've read about it in the MATLAB help, but couldn't figure out a solution. How can I prevent broadcasting of x and y?

Here is my code :

parfor k=1:length(Lambda)

    lambda=Lambda(k);

    for p=1:length(Gamma)
        gamma=Gamma(p);

        for Fold=1:size(Fold_indices,2)

            x_Train=x(logical(Fold_indices(:,Fold)),1:end);

            Y_Train=y(logical(Fold_indices(:,Fold)),1:Num_Tasks);

            % Do sth with x_Train and Y_train
        end
    end
end

I've tried to slice broadcasted data(x) into a cell array and it didn't solve the problem a well.

   B=cell(1,J);

    % Fill each entry of B with a matrix
      % ...do it here
 ....
    parfor k=1:length(Lambda)

        lambda=Lambda(k);

        for p=1:length(Gamma)
            gamma=Gamma(p);

            for Fold=1:J)

                x_Train=B{1,J};

                % Do sth with x_Train and Y_train
            end
        end
    end

Interestingly, when I assign the broadacaste variable(B) to other varible(D), then it stops getting brodcated.

  B=cell(1,J);

    % Fill each entry of B with a matrix
      % ...do it here
 ....
    parfor k=1:length(Lambda)
        D=B;
        lambda=Lambda(k);

        for p=1:length(Gamma)
            gamma=Gamma(p);

            for Fold=1:J)

                x_Train=B{1,J};

                % Do sth with x_Train and Y_train
            end
        end
    end
user2806363
  • 2,513
  • 8
  • 29
  • 48
  • 1
    Your edit hardly helps, as you still don't define `Fold_indices`, and now suddenly there's `fill_it` which is not defined. Otherwise: please do clarify what your issue is here, as I told you there's no avoiding broadcasting in parallel computing, and I can't see how to slice `x` (or `B` for that matter) since you still require it in its entirety in each `parfor` iteration. – Adriaan Sep 18 '18 at 18:23
  • @Adriaan, I think should be a way to avoid it. Since B is sliced variable and each entry of it a matrix. , – user2806363 Sep 18 '18 at 18:28
  • @Adriaan, When I assigne the brocasted variable to other variable, it stop gettig broadcating (D=B;). – user2806363 Sep 18 '18 at 19:01
  • **Read my answer**. You don't stop broadcasting. You just do not get a warning that the entire variable is broadcast, *but it still is*. I'm leaving this question now, because you keep on refusing to read my answer and keep on commenting that you want to stop something I told you you cannot stop. – Adriaan Sep 18 '18 at 19:04

1 Answers1

1

First off: you need to broadcast. Each worker is a separate MATLAB instance, and it needs the data. Sending data to a worker's MATLAB instance is called broadcasting. So there's no preventing it when you use parallel computing, it's the core of it even.

Second: you can't avoid broadcasting x and y in its entirety here, since you use all of it in each separate parfor iteration. Avoiding broadcasting as a whole requires you to not need all of the matrix in each loop iteration, in which case you can slice your variables, as is presented in this answer; i.e. you'll have to rewrite your code in such a manner that you do not require all of x and y to be on each separate worker.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • Well, I've tried to fill up a cell variable, B, which each entry is matrix outside of parfor loop, and replaced it with x in the code. Still it get broadcasted. Do you have any idea what can I do with that? – user2806363 Sep 18 '18 at 18:03
  • 1
    @user2806363 no, as you haven't shown a `B` in the question. Second, please do read my answer first. I explicitly write there that broadcasting is unavoidable. You can't "do with that" in the sense that you can't do away with it. – Adriaan Sep 18 '18 at 18:05
  • @user2806363 A) there's no need to explicitly ping the owner of a post, B), what "doesn't work"? Does `B` still braadcast? Of course it's still broadcasted, as I told you quite loudly in the first paragraph: there's no getting out of broadcasting when doing parallel computing. Please, do rephrase your question to what you actually want, and don't keep asking the same one when you have the answer already. – Adriaan Sep 18 '18 at 18:18