morzh
Jan. 29, 2012 09:28:03
I'm testing THREADED_METHOD1 on a mesh (grid) with 100 points and getting some weird results. The code is:
THREADED_METHOD1( SOP_connectPoints, 1,sum, float *, result );
float sumPts;
float* pSumPts;
void SOP_connectPoints::sumPartial(float* result, const UT_JobInfo &info){
int i, n;
float total=0;
for (info.divideWork(gdp->points().entries(), i, n); i<n; i++){
total += 1;
}
{
UT_AutoJobInfoLock a(info);
*result = total;
}
}
in SOP_connectPoints::cookMySop
pSumPts = &sumPts;
sum(pSumPts);
So on 2 cored machine i get
sumPts = 50
BUT when DOMULTI=0 ( THREADED_METHOD1( SOP_connectPoints, 0,sum, float *, result ) ) all works fine and
sumPts = 100
Any suggestions ?
orr
Jan. 29, 2012 12:32:59
you have 2 cores so i divides the work into 50 points each.
in your for loop you are counting these points so total is set to 50 each.
Now you you trying to set result like this
{
UT_AutoJobInfoLock a(info);
*result = total;
}
so basically your first thread runs counts 50 points and sets result to 50. meanwhile your second thread runs aswell counts 50 points and sets results to 50 aswell.
I think what you wanted to do is more like this:
{
UT_AutoJobInfoLock a(info);
*result += total; // Maybe just a typo?
}
That way result will be 100 in the end.
orr
Jan. 29, 2012 17:01:58
hm, it's working for me. I just did a test with 301 points and 8 cores it gives me:
37
38
37
38
38
38
37
38
sum: 301
I also put pratheses around result like this (*result) += total; just to make sure
.
that's how i call it:
sumPts = 0;
sum(&sumPts);
cout << “sum: ” << sumPts << endl;
morzh
Jan. 30, 2012 09:03:28
!!!! All works fine. orr, thank you for your help !!! All the problems was cause I've made a silly mistake in my code. The next question is how to make the multithreaded loop over group of points. We have FOR_ALL_OPT_GROUP_POINTS macro, so how could we make multhithreaded version for this loop …