Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _USE_MATH_DEFINES
- #include <iostream>
- #include <iomanip>
- #include "mpi.h"
- using namespace std;
- double MPI_Simps(double a, double b, int N, double Func(double), int& ProcID)
- {
- double h = (b - a) / (2 * N);
- int k, NumProc;
- double S1, mpi_S1, S2, mpi_S2, Tmp;
- MPI_Init(NULL, NULL);
- MPI_Comm_size(MPI_COMM_WORLD, &NumProc);
- MPI_Comm_rank(MPI_COMM_WORLD, &ProcID);
- MPI_Bcast(&N, 1, MPI_INT, 0, MPI_COMM_WORLD);
- mpi_S1 = 0; mpi_S2 = 0;
- for (k = 1 + ProcID; k < N; k += NumProc)
- {
- Tmp = a + (2 * k - 1) * h;
- mpi_S1 += Func(Tmp);
- mpi_S2 += Func(Tmp + h);
- }
- MPI_Reduce(&mpi_S1, &S1, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
- MPI_Reduce(&mpi_S2, &S2, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
- MPI_Finalize();
- S1 += Func(b - h);
- return h * (Func(a) + Func(b) + 4.0 * S1 + 2.0 * S2) / 3.0;
- }
- double fun(double x)
- {
- return 1.0 / (1.0 + x * x);
- }
- int main(int argc, char** argv)
- {
- int ProcID;
- double Tms = clock();
- double Intgr = MPI_Simps(0, 1000000, 1000000000, fun, ProcID);
- Tms = (clock() - Tms) / CLOCKS_PER_SEC;
- if (!ProcID)
- {
- cout << "Time=" << Tms << " sec" << endl;
- cout.precision(8);
- cout << "Intgr=" << Intgr << " " << M_PI / 2.0 << endl;
- }
- // Последовательная реализация
- // Time=25.396 sec
- // Intgr = 1.5707953 1.5707963
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement