如何将OpenMp添加到三重嵌套的for循环

编程入门 行业动态 更新时间:2024-10-17 05:00:11
本文介绍了如何将OpenMp添加到三重嵌套的for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

目标是向以下Cholesky因子函数添加尽可能多的OpenMP以增加并行化.到目前为止,我只有一个正确实现的 #pragma omp并行. vector< vector< double>> 表示二维矩阵.我已经尝试为添加 #pragma omp parallel for for(int i = 0; i< n; ++ i), for(int k = 0; k< i; ++ k)和 for(int j = 0; j< k; ++ j),但是并行化出错. makeMatrix(n,n)初始化大小为 nxn 的全零的 vector< vector< double>> .

The goal is to add as much OpenMP to the following Cholesky factor function to increase parallelization. So far, I only have one #pragma omp parallel for implemented correctly. vector<vector<double>> represents a 2-D matrix. I've already tried adding #pragma omp parallel for for for (int i = 0; i < n; ++i), for (int k = 0; k < i; ++k), and for (int j = 0; j < k; ++j) but the parallelization goes wrong. makeMatrix(n, n) initializes a vector<vector<double>> of all zeroes of size nxn.

vector<vector<double>> cholesky_factor(vector<vector<double>> input) { int n = input.size(); vector<vector<double>> result = makeMatrix(n, n); for (int i = 0; i < n; ++i) { for (int k = 0; k < i; ++k) { double value = input[i][k]; for (int j = 0; j < k; ++j) { value -= result[i][j] * result[k][j]; } result[i][k] = value / result[k][k]; } double value = input[i][i]; #pragma omp parallel for for (int j = 0; j < i; ++j) { value -= result[i][j] * result[i][j]; } result[i][i] = std::sqrt(value); } return result; }

推荐答案

我认为您无法使用此算法并行化更多的功能,因为外循环的 i 次迭代取决于内循环的第 1个迭代和第 k 个迭代的结果取决于第 k-1 个的结果迭代.

I don't think you can parallelize much more than this with this algorithm, as the ith iteration of the outer loop depends on the results of the i - 1th iteration and the kth iteration of the inner loop depends on the results of the k - 1th iteration.

vector<vector<double>> cholesky_factor(vector<vector<double>> input) { int n = input.size(); vector<vector<double>> result = makeMatrix(n, n); for (int i = 0; i < n; ++i) { for (int k = 0; k < i; ++k) { double value = input[i][k]; // reduction(-: value) does the same // (private instances of value are initialized to zero and // added to the initial instance of value when the threads are joining #pragma omp parallel for reduction(+: value) for (int j = 0; j < k; ++j) { value -= result[i][j] * result[k][j]; } result[i][k] = value / result[k][k]; } double value = input[i][i]; #pragma omp parallel for reduction(+: value) for (int j = 0; j < i; ++j) { value -= result[i][j] * result[i][j]; } result[i][i] = std::sqrt(value); } return result; }

更多推荐

如何将OpenMp添加到三重嵌套的for循环

本文发布于:2023-11-29 20:53:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647604.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   如何将   OpenMp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!