Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Trouble with indexing parfor loop

Subject: Trouble with indexing parfor loop

From: Braden Moore

Date: 18 Nov, 2010 13:59:04

Message: 1 of 6

I am trying to use a parfor loop on a program I am making, and Matlab does not believe my indicies are valid. Can anybody help?

As of right now I have a vector "k" that is comprised of all integer values. Here is the part of the code that is causing a problem.

parfor j = 1:N
...
Lel(k(j),:) = Lel(k(j),:)+w(j).*Qel.*Pel;
...
end

This code works fine not in parallel, and I understand how Matlab thinks that each row of Lel is not independent of each other. So does anybody have any ideas on how to get around this?

Subject: Trouble with indexing parfor loop

From: PLH

Date: 18 Nov, 2010 14:19:04

Message: 2 of 6

I'm sure a more experienced member will improve upon this, but for what it's worth:

Lel_temp = Lel(k(1:N),:); % declare a copy of the relevant part
parfor j=1:N
    Lel_temp(j,:) = Lel_temp(j,:)+w(j).*Qel.*Pel; % just work with the relevant parts
end
Lel(k(1:N),:) = Lel_temp; % replace the relevant parts

I've not set up a trial run of this, so it may be necessary to consider the 'spirit' of the suggestion rather the 'letter' of the code...

I hope that helps, or at least spurns a better suggestion from elsewhere...

PLH.

 Moore" <bradenm37@comcast.net> wrote in message <ic3bf8$l8e$1@fred.mathworks.com>...
> I am trying to use a parfor loop on a program I am making, and Matlab does not believe my indicies are valid. Can anybody help?
>
> As of right now I have a vector "k" that is comprised of all integer values. Here is the part of the code that is causing a problem.
>
> parfor j = 1:N
> ...
> Lel(k(j),:) = Lel(k(j),:)+w(j).*Qel.*Pel;
> ...
> end
>
> This code works fine not in parallel, and I understand how Matlab thinks that each row of Lel is not independent of each other. So does anybody have any ideas on how to get around this?

Subject: Trouble with indexing parfor loop

From: Steven_Lord

Date: 18 Nov, 2010 14:21:00

Message: 3 of 6



"Braden Moore" <bradenm37@comcast.net> wrote in message
news:ic3bf8$l8e$1@fred.mathworks.com...
> I am trying to use a parfor loop on a program I am making, and Matlab does
> not believe my indicies are valid. Can anybody help?
>
> As of right now I have a vector "k" that is comprised of all integer
> values. Here is the part of the code that is causing a problem.
>
> parfor j = 1:N
> ...
> Lel(k(j),:) = Lel(k(j),:)+w(j).*Qel.*Pel;
> ...
> end
>
> This code works fine not in parallel, and I understand how Matlab thinks
> that each row of Lel is not independent of each other. So does anybody
> have any ideas on how to get around this?

Your Lel variable looks like you're trying to make it a sliced variable, but
it doesn't satisfy the "Form of Indexing" requirement imposed upon sliced
variables.

http://www.mathworks.com/help/toolbox/distcomp/brdqtjj-1.html#bq_tcng-1

    "Within the list of indices for a sliced variable, one of these indices
is of the form i, i+k, i-k, k+i, or k-i, where i is the loop variable and k
is a constant or a simple (nonindexed) broadcast variable; and every other
index is a constant, a simple broadcast variable, colon, or end."

The expression k(j) is not of the form j, j+something, j-something,
something+j, or something-j.

What you could probably do instead is accumulate the rows in Lel(j, :) and
permute the rows according to k after the PARFOR is done.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Trouble with indexing parfor loop

From: Matt J

Date: 18 Nov, 2010 14:24:04

Message: 4 of 6

"Braden Moore" <bradenm37@comcast.net> wrote in message <ic3bf8$l8e$1@fred.mathworks.com>...
>
> As of right now I have a vector "k" that is comprised of all integer values. Here is the part of the code that is causing a problem.
>
> parfor j = 1:N
> ...
> Lel(k(j),:) = Lel(k(j),:)+w(j).*Qel.*Pel;
> ...
> end
==========

Check the dimensions of k and w. Also check to see that k is not just an integer, but that it is >0. Failing all that, copy/paste the error message you are getting.

Subject: Trouble with indexing parfor loop

From: Braden Moore

Date: 18 Nov, 2010 21:00:06

Message: 5 of 6

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <ic3cu4$s0o$1@fred.mathworks.com>...
> "Braden Moore" <bradenm37@comcast.net> wrote in message <ic3bf8$l8e$1@fred.mathworks.com>...
> >
> > As of right now I have a vector "k" that is comprised of all integer values. Here is the part of the code that is causing a problem.
> >
> > parfor j = 1:N
> > ...
> > Lel(k(j),:) = Lel(k(j),:)+w(j).*Qel.*Pel;
> > ...
> > end
> ==========
>
> Check the dimensions of k and w. Also check to see that k is not just an integer, but that it is >0. Failing all that, copy/paste the error message you are getting.

Thanks guys, the advice helped me out. The problem is solved!

Subject: Trouble with indexing parfor loop

From: Julien Emile-Geay

Date: 17 Jan, 2011 22:25:04

Message: 6 of 6

 
> Thanks guys, the advice helped me out. The problem is solved!

Hello Braden,
would you mind sharing your solution? I have a similar problem (in fact, it is slightly simpler), but cannot figure out how to do it.
The accumarray business is way over my head, so I'm trying to find a more intelligible solution.
Thanks!

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us