C++ 分段故障计数排序C++;11 OpenMP
我需要这类并行计数的帮助。我有一个分割错误。Gdb表示分段错误的来源在这条线上:c[i]=0; 可能出现什么问题,以及如何修复?谢谢C++ 分段故障计数排序C++;11 OpenMP,c++,c++11,openmp,C++,C++11,Openmp,我需要这类并行计数的帮助。我有一个分割错误。Gdb表示分段错误的来源在这条线上:c[i]=0; 可能出现什么问题,以及如何修复?谢谢 void radix_sort::sort_array(int array[], int n) { std::hash<int> hash; std::size_t m = n / nthreads; std::vector <int> a(n); a.insert(a.end(), &
void radix_sort::sort_array(int array[], int n)
{
std::hash<int> hash;
std::size_t m = n / nthreads;
std::vector <int> a(n);
a.insert(a.end(), &array[0], &array[n]);
std::vector<int>::iterator begin = a.begin();
std::vector<int>::iterator end = a.end();
int max = *std::max_element(a.begin(), a.end());
//int min = *std::min_element(a.begin(), a.end());
//int x = max - min + 1;
int *split_positions = new int [nthreads+1];
for(std::size_t i=0; i<a.size(); i=i+m){
if(a.begin()+i+m <= a.end()){
split_positions[i] = *a.begin()+i;
split_positions[i+1] = *a.begin()+i+m;
}
else {
split_positions[i] = *a.begin()+i;
split_positions[i+1] = *a.end();
}
}
// create one counter array for each thread
int **thread_counters = new int* [nthreads];
for (int i = 0; i < nthreads; i++)
thread_counters[i] = new int[m];
// count occurences
#pragma omp parallel num_threads(_nthreads)
{
int thread_id = omp_get_thread_num();
int *&c = thread_counters[thread_id];
// reset counters
for (int i = 0; i <= max; i++)
c[i] = 0;
// count occurences
for (int i = split_positions[thread_id]; i < split_positions[thread_id + 1]; i++)
{
c[hash(begin[i])]++;
}
}
// Compute global prefix sums / ranks from local ones. We *could*
// make this parallel, too, but there are only num_threads * (max_key + 1)
// entries in total.
for (int i = 0, sum = 0; i <= max; i++)
{
for (int j = 0; j < nthreads; j++)
{
int t = thread_counters[j][i];
thread_counters[j][i] = sum;
sum += t;
}
}
int *buffer = new int[n]; // backbuffer, copied back to input later
// write sorted result to backbuffer
#pragma omp parallel num_threads(_nthreads)
{
int thread_id = omp_get_thread_num();
int *&c = thread_counters[thread_id];
for (int i = split_positions[thread_id]; i < split_positions[thread_id + 1]; i++)
{
buffer[c[hash(begin[i])]++] = begin[i];
}
}
// write result from buffer back into input
std::copy(buffer, buffer + n, array);
// cleanup
delete [] buffer;
for (int i = 0; i < nthreads; i++)
delete [] thread_counters[i];
delete [] thread_counters;
delete [] split_positions;
}
void基数排序::排序数组(int数组[],int n)
{
std::散列;
标准:尺寸m=n/n个读数;
std::载体a(n);
a、 插入(a.end(),&array[0],&array[n]);
std::vector::iterator begin=a.begin();
std::vector::iterator end=a.end();
int max=*std::max_元素(a.begin(),a.end());
//int min=*std::min_元素(a.begin(),a.end());
//int x=最大值-最小值+1;
int*split_positions=新的int[nthreads+1];
对于(std::size_t i=0;i早期max
太大,无法作为c[]
的合法索引。请找出可能发生的情况;要么max
不应同时用作值和键,要么出于某种原因c[]
未被设置为足够大的值,而您可能打算这样做i