yade-dev team mailing list archive
-
yade-dev team
-
Mailing list archive
-
Message #07351
Re: Re: Performance optimization of InsertionSortCollider
22.03.2011 14:58, Bruno Chareyre wrote:
but parallelizing a serial algo the non-intrusive way is
exactly what OpenMP is supposed to do
No. OpenMP is supposed to run a loops in a few threads. Not anymore.
Parallelizing of a _algo_ is a problem of a developers.
Proof in the attached file. Compile it and run (may be several times) by
g++ -fopenmp -lgomp insertion-sort-test.cpp -o insertion-sort-test
./insertion-sort-test
You should see few "insertion sort failed" and negative values on wrong
places in resulting sorted vector.
--
Best regards,
Sergei D.
// Compilation:
// g++ -fopenmp -lgomp insertion-sort-test.cpp -o insertion-sort-test
#include <iostream>
#include <vector>
#include <omp.h>
void print_vector(const std::vector<int>& v)
{
for (int i=0; i<v.size(); i++)
std::cout << v[i] <<',';
std::cout<<std::endl;
}
void check_sorting(std::vector<int>& v)
{
for (int i=1; i<v.size(); i++) {
if (v[i]<v[i-1]) {
std::cout << "sorting failed" << std::endl;
v[i]*=-1;
}
}
}
int main()
{
std::vector<int> arr;
for (int i=50; i>0; --i)
arr.push_back(i);
print_vector(arr);
std::cout << "max_threads="<<omp_get_max_threads()<< std::endl;
#pragma omp parallel for schedule(static)
for (int i = 1; i < arr.size(); i++)
{
std::cout << "hello from thread "<<omp_get_thread_num()<< std::endl;
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
check_sorting(arr);
print_vector(arr);
}
Follow ups
References