【发布时间】:2018-03-15 10:33:11
【问题描述】:
您好,我在使用这个程序时遇到了困难,我应该依次遍历整个数据向量,并使用 openmp 并行地对其中的每个向量求和(并将总和存储在 solution[i] )。但是程序由于某种原因卡住了。我得到的输入向量并不多,但非常大(每个 2.5m 个整数)。知道我在做什么错吗? 下面是代码,ps:忽略未使用的minVectorSize参数:
void sumsOfVectors_omp_per_vector(const vector<vector<int8_t>> &data, vector<long> &solution, unsigned long minVectorSize) {
unsigned long vectorNum = data.size();
for (int i = 0; i < vectorNum; i++) {
#pragma omp parallel
{
unsigned long sum = 0;
int thread = omp_get_thread_num();
int threadnum = omp_get_num_threads();
int begin = thread * data[i].size() / threadnum;
int end = ((thread + 1) * data[i].size() / threadnum) - 1;
for (int j = begin; j <= end; j++) {
sum += data[i][j];
}
#pragma omp critical
{
solution[i] += sum;
}
}
}
}
【问题讨论】:
标签: c++ multithreading openmp