为什么 omp 并行部分中的线程不会在其部分上划分?
Why won't the threads in the omp parallel section be divided on their sections?
我正在尝试使用OpenMP库并行实现三元搜索算法。我正在使用递归,这是我在代码实施中到目前为止所达到的。
这是搜索功能:
int ternarySearch(int arr[], int size, int left, int right, int num)
{
if (left < 0 || right > size - 1 || left > right){
return -1;
}
else if (num == arr[left]){
return left-1;
}
else if (num == arr[right]){
return right-1;
}
else if (num < arr[left]){
return ternarySearch(arr, size, left - 1, right, num);
}
else if (num > arr[left] && num < arr[right]){
return ternarySearch(arr, size, left + 1, right - 1, num);
}
else if (num > arr[right]){
return ternarySearch(arr, size, left, right + 1, num);
}
}
这是调用TernarySearch函数的主要功能的部分:
omp_set_num_threads(4);
int quarter = size / 4;
/*Using Recursion*/
cout << endl << "Parallel Using Recursion: " << endl << endl;
bool isFound = false;
double paraRecStartTime = omp_get_wtime();
#pragma omp parallel shared(isFound)
{
int id, start, end, left, right, result;
id = omp_get_thread_num();
start = id*quarter;
end = start + quarter;
left = (quarter / 3) + start;
right = end - (quarter / 3);
cout << id << endl;
result = ternarySearch(arr, end, left, right, num);
if(result != -1) {
cout << "Found by thread " << id << " in index " << result << endl;
isFound = true;
}
}
double paraRecRunTime = omp_get_wtime() - paraRecStartTime;
cout << "Ternary Search took " << paraRecRunTime << " sec using 4 threads." << endl << endl;
if (isFound == false) {
cout << "Number does not exist in the array." << endl << endl;
}
问题在于,在输出中,所有线程都会找到元素,而每个线程仅应给予数组的一部分,即使用三元搜索算法在内部进行搜索。有人可以帮我知道我出了什么问题吗?
在OpenMP标准中进一步阅读,并为此使用任务。它们比使用嵌套并行性更好地适合递归问题。
相关文章:
- 如何在 C 中正确使用 libiconv 使其不会报告"Arg list too long"?
- C++使用数组作为多维数组,尽管将其初始化为带有指针的 1D
- 获取字符串的长度并将其分配给数组
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号
- EvtExportLogneneneba API正在将远程计算机的事件日志保存到远程PC本身.如何将其保存到主机
- 类型总是使用其大小存储在内存中吗
- 如何在C++中将整数转换为其数字数组
- 从R调用C++函数并对其进行集成时出错
- C结构,其指针将被包装在unique_ptr中
- 缓存std::数组的选定元素,并在c++中自动保持其一致性
- 如何将输出数字划分为奇数和偶数
- 如何计算数组中元素的位数?(不是数组的长度),并计算其数字的总和
- muQueue.front() 给出了 const 实例,即使我没有将其标记为 const
- 为什么 omp 并行部分中的线程不会在其部分上划分?
- #包括在功能体内部或降低其可见性
- Valarray划分其元素
- 将图像划分为区域,并为其指定标签?使用指针
- 从文件中读取字符串并将其不同部分保存在C++中的不同变量中
- 根据向量的性质将其划分为更小的向量