本文介绍了Openmp可以减少执行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 你好。 我试图优化这种编码我使用OpenMP来减少执行时间。但是,我尝试在for循环中添加#pragma omp parallel,但执行时间更长。如何使用OpenMP优化此代码? 我尝试过: #include < stdio.h > #include < omp.h > int main() { int array [ 10 ] [ 10 ],行,列,i,j,sum = 0 ; printf( \ n输入行数限制:\t); scanf( %d,& row); printf( \ n输入列的限制:\t); scanf( %d,& column); printf( \ nEnnter%d *%d Matrix \ n中的元素,row,column); for (j = 0 ; j< column; j ++) { for (i = 0 ; i< row; i ++) { scanf( %d,& array [i] [ J]); } } printf( \ nArray \ n); for (j = 0 ; j< column; j ++) { for (i = 0 ; i< row; i ++) { printf( %4d, array [i] [j]); } printf( \ n); } for (i = 0 ; i<行; i ++) { for (j = 0 ; j< column; j ++) { sum = sum + array [i] [j]; } printf( 列号[%d]的\ nSum:\ t%d,i,sum); sum = 0 ; } printf( \ n); return 0 ; } 解决方案 您的问题是MP需要成本,这意味着设置线程和收集结果需要花费时间。所以每个线程都需要做足够的工作才能使它值得花费。 一个好的编码风格也可能有助于加速线程。 例如: 在此代码中, sum 来自代码的另一部分,您必须转发事实,如果该变量未用于其他任务以获得正确的结果。 for (i = 0 ; i<行; i ++) { for (j = 0 ; j< column; j ++) { sum = sum + array [i] [j]; } printf( 列号[%d]的\ nSum:\ t%d,i,sum); sum = 0 ; } 这里总和从其他中间使用不会出错。 for (i = 0 ; i < row; i ++) { sum = 0 ; for (j = 0 ; j< column; j ++) { sum = sum + 阵列 [i] [j]; } printf( 列号[%d]的\ nSum:\ t%d,i,sum); } 这显然每个内部循环都不依赖于外部事物(假设乱序结果不是问题) 。 另一个问题: 你还没有测试过你的代码 with matrix 1 2 3 4 你得到 3 7 当你期待 4 6 ,因为您的代码对行而不是列进行求和。 Hello.Im trying to optimize this coding my using OpenMP to reduce the execution time. However, I tried adding #pragma omp parallel at for loop but the execution time is longer. How do I optimize this code using OpenMP?What I have tried:#include <stdio.h>#include <omp.h>int main(){ int array[10][10], row, column, i, j, sum = 0; printf("\nEnter The Limit of Rows:\t"); scanf("%d", &row); printf("\nEnter The Limit of Columns:\t"); scanf("%d", &column); printf("\nEnter Elements in the %d*%d Matrix\n", row, column); for(j = 0; j < column; j++) { for(i = 0; i < row; i++) { scanf("%d", &array[i][j]); } } printf("\nArray\n"); for(j = 0; j < column; j++) { for(i = 0; i < row; i++) { printf("%4d", array[i][j]); } printf("\n"); } for(i = 0; i < row; i++) { for(j = 0; j < column; j++) { sum = sum + array[i][j]; } printf("\nSum of Column No. [%d]:\t%d", i, sum); sum = 0; } printf("\n"); return 0;} 解决方案Your problem is that MP comes at cost, it means that setting threads and gathering results cost time. So each thread need to do enough work to make it worth the cost.A good coding style may also help to speedup threads.Exemple:In this code, sum comes from another part of code, you have to relay on fact that the variable if not used for another task to get correct result.for(i = 0; i < row; i++){ for(j = 0; j < column; j++) { sum = sum + array[i][j]; } printf("\nSum of Column No. [%d]:\t%d", i, sum); sum = 0;}Here sum can't get wrong from other intermediate use.for(i = 0; i < row; i++){ sum = 0; for(j = 0; j < column; j++) { sum = sum + array[i][j]; } printf("\nSum of Column No. [%d]:\t%d", i, sum);}And it make it obvious that each inner loop do not depend on something external (assuming out of order result is not a problem).Another problem:You haven't tested your codewith matrix1 23 4you get3 7when you expect4 6because your code sums rows instead of columns. 这篇关于Openmp可以减少执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-19 22:36