Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // integral_sequential.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <cmath>
- #include <ctime>
- #include <omp.h>
- using namespace std;
- double f(double x);
- double rectangle_method(double a, double b, int n) {
- double sum = 0;
- double h = (b - a) / n;
- int i;
- double cur = 0 , x;
- omp_set_num_threads(4);
- #pragma omp parallel for reduction(+:sum)
- for (i = 0; i < n; ++i) {
- x = a + h * i;
- sum = sum + f(0.5 * (x + x + h));
- }
- return sum * h;
- }
- double rec_method_seq(double a, double b, int n) {
- double h = (b - a) / n;
- double sum = 0.0;
- for (int i = 0; i < n; ++i) {
- double x = a + h * (double)i;
- sum += f(0.5 * (x + x + h));
- }
- return h * sum;
- }
- double f(double x) {
- return sin(x);
- }
- inline void solve(double a, double b, int n) {
- long long l = clock();
- double ans = rectangle_method(a, b, n);
- cout << "Parallel I(x) = " << ans << endl;
- cout << "Parallel time: " << (double)(clock() - l) / CLOCKS_PER_SEC << endl << endl;
- }
- inline void solve_seq(double a, double b, int n) {
- long long l = clock();
- double ans = rec_method_seq(a, b, n);
- cout << "Sequential I(x) = " << ans << endl;
- cout << "Sequential time: " << (double)(clock() - l) / CLOCKS_PER_SEC << endl << endl;
- }
- int main(int argc, char** argv)
- {
- cout.precision(5);
- cout.setf(ios::fixed);
- double a, b, n;
- a = 0;
- b = 10000;
- n = 1e9;
- solve(a, b, n);
- solve_seq(a, b, n);//sequentival solve
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement