I am trying to get a parallel effect in C++ program using the following code:
#include<iostream>
using namespace std;
int main()
{
#pragma omp parallel sections
{
#pragma omp section
{
cout<<"Hello";
cout<<" ";
cout<<"World";
cout<<endl;
}
#pragma omp section
{
cout<<"H";
cout<<"ello";
cout<<" W";
cout<<"orld";
cout<<endl;
}
#pragma omp section
cout<<"Hello"<<" "<<"World"<<endl;
#pragma omp section
{ cout<<"Hello ";
cout<<"World"<<endl;
}
}
return 0;
}
I had run this program many times.. I was expecting interleaved output due to parallelism..
However, every time I run this program the output is:
Hello World
Hello World
Hello World
Hello World
Am I doing something wrong?
Thanks
The code is correct, but interleaved output can be hard to get from such small programs. Try putting some calls to
sleepor similar between output statements and do some flushing.(Did you compile and link with
-openmp,-fopenmp, or whatever your compiler want to hear?)