jeudi 5 avril 2018

Adding to a vector in multithread

I want to do a selection of point on certain conditions and put the selected points in a new container.

void IntervalMapEstimator::extract_relevant_points_multithread(std::vector<Point3D>&  relevant_points ,std::vector<Point3D>& pointcloud, doubleIE cell_min_angle_sensor_rot, doubleIE cell_max_angle_sensor_rot)
{
    relevant_points.reserve (pointcloud.size ());

#pragma omp parallel for shared (relevant_points, pointcloud, cell_min_angle_sensor_rot, cell_max_angle_sensor_rot) num_threads(5)
        for(int i = 0; i < pointcloud.size(); i++) {
           if( -- condition -- ){
#pragma omp critical(push_in_relevant_points)
                     relevant_points.push_back(pointcloud[i]);                     
            }
        }
}

So i have a multi thread environment working with openMp, the thing is, every time a point is selected by the condition i have to use my critical clause( which basically work like a std::lock_guard) to push the value to relevant_point container. So there is like bottleneck to use this container.

I'm searching for a way to go around the problem and get more efficiency, do you have any ideas?

Aucun commentaire:

Enregistrer un commentaire