1

Hi it's the first time I want to use the parallel toolbox from matlab. I have this loop

for y=1:size(pxyvector,1)
    if (strcmp(pxyvector{y,1}, emotionword))&&(strcmp(pxyvector{y,2},tweet{x}))
          pxyvector{y,3} = pxyvector{y,3} +1;
          invector = true;
    end
end

How would I go and make this work in a parallel for. I read the sliced variables part of matlab, but I don't get how I can do this here.

pxyvector is a 100000x3 cell array

tweet{x} is a string

emotionword is a string too.

invector is a value that is used later outside the loop.

So basically I compare the first value from a row of the pxyvector with the emotion word, and the second value from a row of the pxyvector to tweet{x}. If they are the same. The third value in the row gets incremented.

During the loop the same value cannot be incremented twice.

The problems in this for loop are that I need to change a variable that is used outside the loop too and increment a value.

Some data to play with : http://ojtwist.be/pxyvector.mat (variable is pxyvector2 in this .mat file, so change that in the code, if you want to test it)

6
  • Well, which error you get when you try parfor? btw you can split the pxyvector and use a SIMD approach on different workers. Commented Nov 24, 2012 at 13:42
  • pxyvector is indexed in different ways, potentially causing dependencies between iterations. And invector is used after the parfor loop but its value is nondeterministic. How ouwld SIMD approach work here ? Commented Nov 24, 2012 at 13:49
  • pxyvector seems fine to me as only one row is touched at a time, i.e., it should be fine to calculate it in parallel. However, invector is a problem. How about changing that line to invector(y) = true;? Then it should run in the parfor loop too. After the loop, just use invector = any(invector);. Commented Nov 24, 2012 at 15:19
  • Your second suggestion might be a solution to that line. But it still won't execute because the " pxyvector is indiced in different ways ..." error Commented Nov 24, 2012 at 15:55
  • Can you please change a little bit your code in order to have the same situation but allowing other people here to easily work on it? Commented Nov 24, 2012 at 16:42

1 Answer 1

1

I guess the problem here is that matlab doesn't handle cell matrices as normal matrices (in fact a{1,:} doesn't behave as you expect).

AFAIK, to use parfor you need a different organization of data, specifically as a Nx1 cell of 3x1 cell elements.

The following works, for instance

tot = size(pxyvector,1)
%%%%// just to reshape data correctly
pxvector_a = pxyvector;
pxyvector = cell(1,tot);
for i = 1:tot
   pxyvector{i} = {pxvector_a{i,1} pxvector_a{i,2} pxvector_a{i,3}};    
end
%%%%

 parfor y=1:tot
    if (strcmp(pxyvector{y}{1}, 'almost'))&&(strcmp(pxyvector{y}{2},'that'))
          pxyvector{y}{3} = pxyvector{y}{3} +1;
          invector = true;
    end
end

you can still use smpd, but your current organization of data will make the creation of proper distributed arrays hard.

Sign up to request clarification or add additional context in comments.

1 Comment

@Ojtwist, likely a memory access problem: data distribution gets more random. Try to separate things so to have 3 (Nx1) cell arrays.

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.