1
votes

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)

1
Well, which error you get when you try parfor? btw you can split the pxyvector and use a SIMD approach on different workers. - Acorbe
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 ? - Olivier_s_j
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);. - H.Muster
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 - Olivier_s_j
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? - Acorbe

1 Answers

1
votes

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.