I'm trying to make use of openmp to parallelise the function below in creating a 3D grid. Classes Point, Triangle, and Box have been declared & structured but the code is not pasted here as it's quite long to include in the question (about 110 lines). If you need it and allow me to paste it, then I can edit this question.
I've tried using #pragma omp parallel for
on the outermost for() loop and the program crashed as I run it. Then, I tried implementing the following #pragma
codes below and the program took a longer time to run it (compared to the serial coding). I referred to this link here in implementing it. Actually first of all, is it possible to parallelise the code below using openmp? If yes, then I really need your help in this.
int main()
{
vector<Box> boxes;
Triangle dummy1;
float dx = 81.0, dy = 121.0, dz = 98.0, delta=1.0;
long int nx = 5*ceil(dx/(5.0*delta));
long int ny = 5*ceil(dy/(5.0*delta));
long int nz = 5*ceil(dz/(5.0*delta));
long int Box_ID=1, Pt_ID=1;
float x_val=0.0, y_val=0.0, z_val=0.0;
float x0=-42.0f, y0=-3.0f, z0=-52.0f;
long int i, j, k;
for(i=0; i<nz+2; i++)
{
z_val=i*delta + z0;
for(j=0; j<ny+2; j++)
{
y_val=j*delta + y0;
#pragma omp parallel
{
vector<Box> box_private;
#pragma omp for nowait schedule(static)
for(k=0; k<nx+2; k++)
{
x_val=k*delta + x0;
Point x1(x_val, y_val, z_val, Pt_ID);
Point x2(x_val+delta, y_val, z_val, Pt_ID+1);
Point x3(x_val, y_val+delta, z_val, Pt_ID+2);
Point x4(x_val+delta, y_val+delta, z_val, Pt_ID+3);
Point x5(x_val, y_val, z_val+delta, Pt_ID+4);
Point x6(x_val+delta, y_val, z_val+delta, Pt_ID+5);
Point x7(x_val, y_val+delta, z_val+delta, Pt_ID+6);
Point x8(x_val+delta, y_val+delta, z_val+delta, Pt_ID+7);
box_private.push_back(Box(x1,x2,x3,x4,x5,x6,x7,x8,dummy1,Box_ID));
Box_ID++;
Pt_ID++;
}
#pragma omp for schedule(static) ordered
for(int i=0; i<omp_get_num_threads(); i++)
{
#pragma omp ordered
boxes.insert(boxes.end(), box_private.begin(), box_private.end());
}
}
}
}
}
Program crashes when I implement the codes below instead of the above one.
#pragma omp parallel for
for(i=0; i<nz+2; i++)
{
z_val=i*delta + z0;
for(j=0; j<ny+2; j++)
{
y_val=j*delta + y0;
for(k=0; k<nx+2; k++)
{
x_val=k*delta + x0;
/* Point x1 to x8...*/
boxes.push_back(Box(x1,x2,x3,x4,x5,x6,x7,x8,dummy1,Box_ID));
Box_ID++;
Pt_ID++;
}
}
}