c++openmpboost-iterators

parallelize filter iterator using openMP v. 2.0


I have a big vector. I would like to increment only positive member in parallel.

class is_positive_number {
public:
    bool operator()(int x) { return 0 < x; }
};

int main()
{
    int numbers_[] = { 0, -1, 4, -3, 5, 8, -2, 3, 4, -1 };
    const int N = sizeof(numbers_) / sizeof(int);   
    std::vector<int> test_vec(numbers_, numbers_ + N);

    std::cout << "max: " << omp_get_max_threads() << std::endl;
    int number_of_threads = 4;
    int SubVecLen = N / number_of_threads,
        LeftOvers = N % number_of_threads;
    int k = 0, i = 0, n = 0;
    std::vector<int> start_vec, end_vec;
    while (n < number_of_threads)
    {
        k += i;
        i = (SubVecLen + (LeftOvers-- > 0 ? 1 : 0));
        start_vec.push_back(k);
        end_vec.push_back((k + i - 1));

        ++n;
    }
    for (size_t n = 0; n < number_of_threads; n++)
    {
        std::cout << start_vec[n] << "\t" << end_vec[n] << "\n";
    }

    is_positive_number predicate;
    typedef boost::filter_iterator<is_positive_number, std::vector<int>::iterator>
        FilterIter;

#pragma omp parallel
    {
#pragma omp for
        for (int s = 0; s < 4; s++)
        {
            FilterIter filter_iter_first(predicate, test_vec.begin(), (test_vec.begin() + start_vec[omp_get_thread_num()]) );
            FilterIter filter_iter_last(predicate, test_vec.begin(), (test_vec.begin() + end_vec[omp_get_thread_num()] + 1) );

            for (auto iter = filter_iter_first;
                iter != filter_iter_last; iter++)
            {
                std::cout << "num: " << *iter << std::endl;
                (*iter) = (*iter) + 1;
            }

        }
    }

    for (size_t n = 0; n < test_vec.size(); n++)
    {
        std::cout << test_vec[n] << "\n";
    }
    return 0;
}

The result is wrong!

However the real problem I am trying to solve is more complicated than this one. I tried to simplify my question so that it would be more understandable. I have to also mention that I am restricted to use openMP v.2.0. That is why I tried to split my vector and dedicate each part of the vector to a specific thread.

Any idea would be appreciated.


Solution

  • You use the wrong start and stop indices when creating your filter iterators. I've attached a working example. I used s to select the thread inside the parallel for statement. Not sure if your version works too, so I changed it to what I'm used to.

    #pragma omp parallel
    {
        #pragma omp for
        for (int s = 0; s < 4; s++)
        {
            /* The following lines are important */
            FilterIter filter_iter_first(predicate, test_vec.begin() + start_vec[s], (test_vec.begin() + end_vec[s] + 1));
            FilterIter filter_iter_last(predicate, test_vec.begin() + end_vec[s] + 1, (test_vec.begin() + end_vec[s] + 1));
    
            for (auto iter = filter_iter_first; iter != filter_iter_last; iter++)
            {
                std::cout << "num: " << *iter << std::endl;
                (*iter) = (*iter) + 1;
            }
        }
    }