Thanks for the suggestions, both of you. OK, so I will simply insert values instead of appending. But I have come across a different problem (but still OpenMP-related), which is when I use an if-conditional. Here is my code, which merely generates (using boost) initial positions/velocities and propagates the particles (very simple...). If the particle is within a desired range, I want to keep track of it:
Code:
#include <iostream>
#include <boost/random.hpp>
#include <boost/random/normal_distribution.hpp>
using namespace std;
int propagate(double X, double Y, double Z, double vX, double vY, double vZ, double time)
{
X += vX*time;
Y += vY*time;
if(sqrt(X*X + Y*Y) < 0.01)
{
return 1;
}
return 0;
}
int main()
{
boost::mt19937 engine(static_cast<unsigned int>(time(0)));
boost::normal_distribution<double> nd(0.0, 1.0);
boost::variate_generator< boost::mt19937, boost::normal_distribution<double> > normal_std_one(engine, nd);
double coordX, coordY, coordZ, time;
double velX, velY, velZ;
int test;
int app=0;
int i;
#pragma omp parallel for
for(i=0; i<2000000; i++)
{
coordX = 0.01*normal_std_one();
coordY = 0.01*normal_std_one();
coordZ = 0.0;
velX = normal_std_one();
velY = normal_std_one();
velZ = 20.0*normal_std_one()+300;
time = 1.0/velZ;
test = propagate(coordX, coordY, coordZ,
velX, velY, velZ, time);
if(test==1)
{
app++;
}
}
cout << app << endl;
return 0;
}
If I comment out the parallelization, I get (on average) a different value than if I keep the parallelization. How come that is?