Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <iostream>
- #include <mpi.h>
- #include <math.h>
- using namespace std;
- int main(int argc, char *argv[]) {
- int tid, nthreads;
- char *cpu_name;
- double time_initial, time_current, time;
- //////////////////////////////////////////////////////////////*/
- int n=10000, i;
- double PI25DT = 3.141592653589793238462643;
- double mypi, pi, h, sum, x;
- MPI_Init(&argc, &argv);
- time_initial = MPI_Wtime();
- MPI_Comm_rank(MPI_COMM_WORLD, &tid);
- MPI_Comm_size(MPI_COMM_WORLD, &nthreads);
- cpu_name = (char *) calloc(80, sizeof(char));
- gethostname(cpu_name, 80);
- while (1) {
- if (tid == 0) {
- cout << "Enter the number of intervals: (0 quits)"
- << endl;
- cin >> n;
- }
- MPI::COMM_WORLD.Bcast(&n, 1, MPI::INT, 0);
- if (n==0)
- break;
- else {
- h = 1.0 / (double) n;
- sum = 0.0;
- for ( i = tid + 1; i <= n; i++ ) {
- x = h * ((double)i - 0.5);
- sum += (4.0 / (1.0 + x*x));
- }
- mypi = h * sum;
- MPI::COMM_WORLD.Reduce(&mypi, &pi, 1, MPI::DOUBLE,
- MPI::SUM, 0);
- time_current = MPI_Wtime();
- time = time_current - time_initial;
- printf("tid %i wait time=%.3f on machine=%s [NCPU=%i]\n",
- tid , time, cpu_name, nthreads);
- if (tid == 0){
- printf("pi calculated: %.25f \nand real pi is %.25f \n", pi, PI25DT);
- cout<< "Error is "<<fabs(pi - PI25DT) <<endl;
- }
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- /*int loop_min, loop_max , i , n=2;
- double inf = 100000000, sum =0, p_sum=0;
- for(tid=0; tid < nthreads ; tid++)
- {
- loop_min = 1 + (int)((tid + 0) * (inf-1)/nthreads);
- loop_max = (int)((tid + 1) * (inf-1)/nthreads);
- for(i=loop_max-1;i>=loop_min;i--)
- {
- p_sum += 1.0/pow(i,(double)n);
- }
- }
- MPI_Reduce(&p_sum,&sum,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);
- */
- ///////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////
- // printf("zeta(2):%f\n",sum);
- MPI_Finalize();
- // printf("final zeta(2):%f\n",sum);
- return (0);
- }
Add Comment
Please, Sign In to add comment