Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _BSD_SOURCE
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <math.h>
- #include <sys/types.h>
- #define DEBUG
- #include "debug.h"
- #include <getopt.h>
- #include <mpi.h>
- // Integration bounds
- const double left_integration_bound = 0;
- const double right_integration_bound = 1;
- // Upper bound of integration integration step
- // may be reduced to step * n = length of interval
- const double integration_step = 0.00000002;
- // This function will be integrated
- double f( double x ) {
- return exp( x );
- }
- // Integrate fucntion by trapezoid method
- double integrate( double ( *func )( double x ),
- double from, double to, double step ) {
- // f - function to integrate
- // from - start bound
- // to - stop bound
- // step - upper bound of interation step
- if (step <= 0) {
- PERROR( "step must be greater then 0 but %lg was given and multiplied by -1", step );
- step *= -1;
- }
- register int n_steps = abs( ceil( ( to - from ) / step ) );
- //LOG_VAR( n_steps, "%d" );
- register double integration_sum = 0;
- for ( register int i = 0; i <= n_steps; i++ ) {
- integration_sum += f( from + (to - from) / (double)n_steps * i );
- }
- integration_sum -= ( f(from) + f(to) ) / 2;
- return integration_sum * (to - from) / n_steps;
- }
- int main (int argc, char** argv, char** env) {
- TIMER_START(MAIN);
- int rc;
- // MPI_Wtime - Returns an elapsed time on the calling processor
- // double MPI_Wtime( void )
- double start_time = MPI_Wtime( ), active_time = 0;
- // MPI_Init - Initialize the MPI execution environment
- // int MPI_Init( int *argc, char ***argv )
- if ( rc = MPI_Init( &argc, &argv ) ) {
- PERROR( "MPI_Init failed with code rc = %d", rc );
- goto exit_failure_section;
- }
- int cluster_size = 0;
- // MPI_Comm_size - Determines the size of the group associated with a communicator
- // int MPI_Comm_size( MPI_Comm comm, int *size )
- if ( rc = MPI_Comm_size( MPI_COMM_WORLD, &cluster_size ) ) {
- PERROR( "MPI_Comm_size failed with code rc = %d", rc );
- goto exit_failure_section;
- }
- int current_node_rank = 0;
- // MPI_Comm_rank - Determines the rank of the calling process in the communicator
- // int MPI_Comm_rank( MPI_Comm comm, int *rank )
- if ( rc = MPI_Comm_rank( MPI_COMM_WORLD, ¤t_node_rank ) ) {
- PERROR( "MPI_Comm_rank failed with code rc = %d", rc );
- goto exit_failure_section;
- }
- LOG_VAR( cluster_size, "%d" );
- LOG_VAR( current_node_rank, "%d" );
- double local_left = left_integration_bound +
- ( right_integration_bound - left_integration_bound )
- / cluster_size * current_node_rank;
- double local_right = left_integration_bound +
- ( right_integration_bound - left_integration_bound )
- / cluster_size * (current_node_rank + 1);
- double local_result = integrate( f, local_left, local_right, integration_step );
- double global_result = 0;
- // MPI_Reduce - Reduces values on all processes to a single value
- // int MPI_Reduce(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype,
- // MPI_Op op, int root, MPI_Comm comm)
- if ( rc = MPI_Reduce( &local_result, &global_result,
- 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD
- ) ) {
- PERROR( "MPI_Reduce failed with code rc = %d", rc );
- goto exit_failure_section;
- }
- // MPI_Bcast - Broadcasts a message from the process with rank "root" to all other proβ
- // cesses of the communicator
- // int MPI_Bcast( void *buffer, int count, MPI_Datatype datatype, int root,
- // MPI_Comm comm )
- // if ( rc = MPI_Bcast( &global_result, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD ) ) {
- // PERROR( "MPI_Bcast failed with code rc = %d", rc );
- // goto exit_failure_section;
- // }
- LOG_VAR( global_result, "%lg" );
- // see declaration previously
- active_time = (MPI_Wtime() - start_time);
- printf("T:%.03lg\n", active_time);
- // MPI_Finalize - Terminates MPI execution environment
- // int MPI_Finalize( void )
- MPI_Finalize();
- // LOG( "TIME = %.04f", (stop_time - start_time) * 1000 );
- TIMER_DUMP(MAIN);
- return EXIT_SUCCESS;
- // Exit with some nonzero errorcode
- exit_failure_section:
- // MPI_Abort - Terminates MPI execution environment
- // int MPI_Abort(MPI_Comm comm, int errorcode)
- MPI_Abort( MPI_COMM_WORLD, rc );
- // see declaration previously
- active_time = (MPI_Wtime() - start_time);
- printf("T:%.03lg\n", active_time);
- TIMER_DUMP(MAIN);
- // MPI_Finalize - Terminates MPI execution environment
- // int MPI_Finalize( void )
- MPI_Finalize();
- return EXIT_FAILURE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement