Assignment-04/Question-04/mpi_root.c
// The master process divides the interval [0, 1] into subintervals
The master process divides the interval [0, 1] into subintervals
The master process divides the interval [ 0 1 ] into subintervals
void master(int n_proc) {
    double *intervals = (double *) malloc(n_proc*sizeof(double));
    set_intervals(intervals, 0.0, 1.0, n_proc);
    double ANS[4] = { 0.0 };  
    double SOL[4] = { 1.0 };  
    int n_sent = 0;
    MPI_Status status;
    for (int i = 1; i < n_proc; ++i) {
        double bounds[2] = { intervals[i-1], intervals[i] };
        MPI_Send(bounds, 2, MPI_DOUBLE, i, REQUEST, MPI_COMM_WORLD);
        n_sent++;
    }
    while (n_sent > 0) {
        MPI_Recv(ANS, 4, MPI_DOUBLE, MPI_ANY_SOURCE, REPLY,
                 MPI_COMM_WORLD, &status);
        if (fabs(ANS[0]) <= fabs(SOL[0])) {
            SOL[0] = ANS[0];
            SOL[1] = ANS[1];
            SOL[2] = ANS[2];
            SOL[3] = ANS[3];
            set_intervals(intervals, ANS[2], ANS[3], n_proc);
        }
        if (((ANS[3] - ANS[2]) / WIDTH) <= INTERVAL) {
            n_sent--;
        }
        double bounds[2] = { intervals[status.MPI_SOURCE-1],
                             intervals[status.MPI_SOURCE] };
        MPI_Send(bounds, 2, MPI_DOUBLE, status.MPI_SOURCE, REQUEST, MPI_COMM_WORLD);
    }
    for (int i = 1; i < n_proc; ++i) {
        double bounds[2] = { -1.0, -1.0 };
        MPI_Send(bounds, 2, MPI_DOUBLE, i, REQUEST, MPI_COMM_WORLD);
    }
    free(intervals);
    printf("Approx zero of function f(x): %1.15g\nr value: %1.15g\n", SOL[0], SOL[1]);
}
void master ( int n_proc ) { double * intervals = ( double * ) malloc ( n_proc * sizeof ( double ) ) ; set_intervals ( intervals , 0.0 , 1.0 , n_proc ) ; double ANS [ 4 ] = { 0.0 } ; double SOL [ 4 ] = { 1.0 } ; int n_sent = 0 ; MPI_Status status ; for ( int i = 1 ; i < n_proc ; ++ i ) { double bounds [ 2 ] = { intervals [ i - 1 ] , intervals [ i ] } ; MPI_Send ( bounds , 2 , MPI_DOUBLE , i , REQUEST , MPI_COMM_WORLD ) ; n_sent ++ ; } while ( n_sent > 0 ) { MPI_Recv ( ANS , 4 , MPI_DOUBLE , MPI_ANY_SOURCE , REPLY , MPI_COMM_WORLD , & status ) ; if ( fabs ( ANS [ 0 ] ) <= fabs ( SOL [ 0 ] ) ) { SOL [ 0 ] = ANS [ 0 ] ; SOL [ 1 ] = ANS [ 1 ] ; SOL [ 2 ] = ANS [ 2 ] ; SOL [ 3 ] = ANS [ 3 ] ; set_intervals ( intervals , ANS [ 2 ] , ANS [ 3 ] , n_proc ) ; } if ( ( ( ANS [ 3 ] - ANS [ 2 ] ) / WIDTH ) <= INTERVAL ) { n_sent -- ; } double bounds [ 2 ] = { intervals [ status . MPI_SOURCE - 1 ] , intervals [ status . MPI_SOURCE ] } ; MPI_Send ( bounds , 2 , MPI_DOUBLE , status . MPI_SOURCE , REQUEST , MPI_COMM_WORLD ) ; } for ( int i = 1 ; i < n_proc ; ++ i ) { double bounds [ 2 ] = { -1.0 , -1.0 } ; MPI_Send ( bounds , 2 , MPI_DOUBLE , i , REQUEST , MPI_COMM_WORLD ) ; } free ( intervals ) ; printf ( " \n \n " , SOL [ 0 ] , SOL [ 1 ] ) ; }
The master process divides the interval [0, 1] into subintervals
The master process divides the interval [ 0 1 ] into subintervals
// Divide the interval [0,1] into several subintervals for each process // The result, r value, bounds from processes // The final smallest solution, r value, bounds // Assign to each task an endpoint of one of the subintervals at which // the function f should be evaluated // Receive the r value and result that closest approximates function f(x) // Analyse the function values determined by each process to find a new // subinterval [a,b]c[0,1] within the solution r is guaranteed to lie // Update the subintervals for the search space // Reduce num sent when below 10E-11 // Divide subinterval into smaller pieces for execution again // Terminate all processes