Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.
2464 Discussions

Simple program-using Parallel_for. What is wrong here?

jobin007007
Beginner
199 Views
[cpp][/cpp]
Hi,

I am new to TBB. I am using the parallel_for and am running into problems when using it in a simple example i made.

When I run the code in serial mode by commenting out the parallel section, the variable count is (10 to the power 10) as should be and variable count1 is (10 to the power5). When i set grainsize to 10,000 or 50,000 and run it in the parallel mode, my count is less than (10 to the power 10) and count1 is less than (10 to the power 5). The variable count should always be equal to (10 to the power 10) always because the function Foo should update it that many times. It shoulnt matter whether I am running in parallel or serial mode. If I set the grainsize for my parallel_for to 100,000 or above, my count and count1 are good.(I guess its just running in a single processor if i set grainsize equal to 100,000 as the no of iterations is equal to 100,000)

What am I doing wrong? Please help! I cant understand this.

#include "stdafx.h"
#include
#include "tbb/task_scheduler_init.h"
#include "tbb/blocked_range.h"
#include
#include "tbb/parallel_for.h"

using namespace tbb;
using namespace std;

static int count;
static int count1;
class ClassFoo{
float *const my_a;
public:

ClassFoo(float* a):my_a(a)
{
cout<<"here";

}
ClassFoo():my_a()
{
}

public:
void operator()(const blocked_range& r) const{
float *a=my_a;
for(size_t i=r.begin();i!=r.end();++i)
{Foo(a);
count1=count1+1;
}
}

void SerialApply(float s[],size_t n)
{

for(size_t i=0;i Foo(s);

}



static void Foo( float g)
{
for(int i=0;i<10000;i++)
for(int j=0;j<1;j++)
count=count+1;
}

};


int main(int argc, _TCHAR* argv[])
{
task_scheduler_init init;
count=0;
count1=0;

float a[100000];
int i;
for( i=0;i<100000;i++)
{
a=(float)i;
}
//ClassFoo s1(a);

time_t second;
second=time(NULL);

//s1.SerialApply(a,100000);


parallel_for(blocked_range(0,i,1),ClassFoo(a));

cout<<"\n"<<<"\n "<<<" \n";
cout<<"\nCount-"<<<" "<}
0 Kudos
2 Replies
robert-reed
Valued Contributor II
199 Views
Quoting - jobin007007
I am new to TBB. I am using the parallel_for and am running into problems when using it in a simple example i made.

When I run the code in serial mode by commenting out the parallel section, the variable count is (10 to the power 10) as should be and variable count1 is (10 to the power5). When i set grainsize to 10,000 or 50,000 and run it in the parallel mode, my count is less than (10 to the power 10) and count1 is less than (10 to the power 5). The variable count should always be equal to (10 to the power 10) always because the function Foo should update it that many times. It shoulnt matter whether I am running in parallel or serial mode. If I set the grainsize for my parallel_for to 100,000 or above, my count and count1 are good.(I guess its just running in a single processor if i set grainsize equal to 100,000 as the no of iterations is equal to 100,000)

One of the tricky bits of parallel programming is that with multiple agents simultaneously updating things in memory, programmers need to guard against the possibility that multiple threads may try to change the same things at the same time, a so-called race condition.

The variables count and count1 may have this problem in the code sample you shared. Both function Foo and the ClassFoo functor will have multiple copies executing simultaneously, so updates to them need to be guarded, either by using a critical section around each of the increments or in this case declaring each of them atomic. You might learn more about races here.

0 Kudos
Alexey-Kukanov
Employee
199 Views
To put it simple, make count and count1 tbb::atomic, and use the increment operator (++) to increment counts by 1. The expression like count=count+1 is not atomic on the C++ code level, so tbb::atomic can not make it thread-safe. If you need to increment to more than 1, use count += value.
0 Kudos
Reply