Assignment-04/Question-04/mpi_root.c
// The slave process receives the intervals to calculate and the width
The slave process receives the intervals to calculate and the width
The slave process receives the intervals to calculate and the width
void slave(int proc_id) {
    double bounds[2] = { 0.0 };
    double ans[4] = { 1.0 };
    double h = 0.0, r = 0.0, result = 0.0;
    MPI_Status status;
    MPI_Recv(bounds, 2, MPI_DOUBLE, MASTER, REQUEST,
             MPI_COMM_WORLD, &status);
    while (bounds[0] >= 0) {
        h = (bounds[1] - bounds[0]) / (double)(WIDTH -1);
        for (int i = 0; i < WIDTH; ++i) {
            r = bounds[0] + i*h;
            result = f(r);
            if (fabs(ans[0]) > fabs(result)) {
                ans[0] = result;
                ans[1] = r;
                ans[2] = bounds[0];
                ans[3] = bounds[1];
            }
        }
        MPI_Send(ans, 4, MPI_DOUBLE, MASTER, REPLY, 
                 MPI_COMM_WORLD);
        MPI_Recv(bounds, 2, MPI_DOUBLE, MASTER, REQUEST, 
                 MPI_COMM_WORLD, &status);
    }
}
void slave ( int proc_id ) { double bounds [ 2 ] = { 0.0 } ; double ans [ 4 ] = { 1.0 } ; double h = 0.0 , r = 0.0 , result = 0.0 ; MPI_Status status ; MPI_Recv ( bounds , 2 , MPI_DOUBLE , MASTER , REQUEST , MPI_COMM_WORLD , & status ) ; while ( bounds [ 0 ] >= 0 ) { h = ( bounds [ 1 ] - bounds [ 0 ] ) / ( double ) ( WIDTH - 1 ) ; for ( int i = 0 ; i < WIDTH ; ++ i ) { r = bounds [ 0 ] + i * h ; result = f ( r ) ; if ( fabs ( ans [ 0 ] ) > fabs ( result ) ) { ans [ 0 ] = result ; ans [ 1 ] = r ; ans [ 2 ] = bounds [ 0 ] ; ans [ 3 ] = bounds [ 1 ] ; } } MPI_Send ( ans , 4 , MPI_DOUBLE , MASTER , REPLY , MPI_COMM_WORLD ) ; MPI_Recv ( bounds , 2 , MPI_DOUBLE , MASTER , REQUEST , MPI_COMM_WORLD , & status ) ; } }
The slave process receives the intervals to calculate and the width
The slave process receives the intervals to calculate and the width
// Given the width iterate through each value of r // to find the result that is closest to 0 // Return the results to master process and wait for more work