tony497
tony497

Reputation: 400

Error when I use collapse of OpenMp in C++

I have an error, i don't understand why collapse doesn't work in my code.

    #pragma omp parallel num_threads(IntervalMapEstimator::m_num_thread)
    {
        std::vector<Point3D> local_relevant_points;

        #pragma omp for collapse(2)
        for(int i = first_list_index; i < last_list_index ; i++)
        {
            for (int k = 0; k < pointcloud_ff_polar_angle_lists[i].size(); k++)
            {
                if ( pointcloud_ff_polar_angle_lists[i][k].pol_sensor_rot.phi >= cell_start_angle && pointcloud_ff_polar_angle_lists[i][k].pol_sensor_rot.phi <= cell_end_angle )
                {
                    #pragma omp critical
                    {
                        relevant_points.push_back(pointcloud_ff_polar_angle_lists[i][k]);
                    }
                }
            }
        }
    }

It doesn't work with the collapse, but it does when i'm remove the collapse. I can't figure out why, does somebody see why ? It also work in single thread.

Upvotes: 1

Views: 1152

Answers (1)

Qubit
Qubit

Reputation: 1255

Regarding the collapse statement, a nice explanation is provided here: Understanding the collapse clause in openmp. As stated in the answer, the collapse clause will only work when the inner loop does not depend on the outer, which in your case does not appear to hold as the size of the vector is not guaranteed to be the same for all values of i.

Unfortunately I do not have the reputation to post comments yet, so I will resort to posting this here - even though it is not exactly related to the original question.

You seem to be parallelizing a loop with fairly simple instructions, it is very likely that the threads will spend a considerable amount of time waiting around that critical region. Although I assume that is what local_relevant_points will be used for.

Upvotes: 5

Related Questions