我試圖計算積分程序並行化使用OpenMP C++
#include <iostream>
#include <omp.h>
using namespace std;
double my_exp(double x) {
double res = 1., term = 1.;
for(int n=1; n<=1000; n++) {
term *= x/n;
res += term;
}
return res;
}
double f(double x) {
return x*my_exp(x);
}
int main() {
double a=0., b=1., result = 0.;
int nsteps = 1000000;
double h = (b - a)/nsteps;
for(int i=1; i<nsteps; i++) result += f(a + i*h);
result = (result + .5*(f(a) + f(b)))*h;
cout.precision(15);
cout << "Result: " << result << endl;
return 0;
}
這項計劃數積分和返回結果Result: 1.00000000000035
來算積分。但執行時間很多。 我應該平行我的計劃,我想我應該補充#pragma omp parallel for
,但它不工作
我建議你使用g ++。你用'-fopenmp'編譯了它嗎? – muXXmit2X
是的,我使用g ++。我嘗試'-fopenmp',但它並不會影響執行程序的時間。我應該改變我的代碼,但我從來沒有使用'openmp' –