Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.01 KB | None | 0 0
  1. #define _BSD_SOURCE
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <math.h>
  6.  
  7. #include <sys/types.h>
  8.  
  9. #define DEBUG
  10. #include "debug.h"
  11. #include <getopt.h>
  12.  
  13. #include <mpi.h>
  14.  
  15. // Integration bounds
  16. const double left_integration_bound = 0;
  17. const double right_integration_bound = 1;
  18.  
  19. // Upper bound of integration integration step
  20. // may be reduced to step * n = length of interval
  21. const double integration_step = 0.00000002;
  22.  
  23. // This function will be integrated
  24. double f( double x ) {
  25.     return exp( x );
  26. }
  27.  
  28. // Integrate fucntion by trapezoid method
  29. double integrate(  double ( *func )( double x ),
  30.                                   double from, double to, double step ) {
  31.     // f - function to integrate
  32.     // from - start bound
  33.     // to - stop bound
  34.     // step - upper bound of interation step
  35.    
  36.     if (step <= 0) {
  37.         PERROR( "step must be greater then 0 but %lg was given and multiplied by -1", step );
  38.         step *= -1;        
  39.     }
  40.     register int n_steps = abs( ceil( ( to - from ) / step ) );
  41.    
  42.     //LOG_VAR( n_steps, "%d" );
  43.    
  44.     register double integration_sum = 0;
  45.     for ( register int i = 0; i <= n_steps; i++ ) {
  46.         integration_sum += f( from + (to - from) / (double)n_steps * i );            
  47.     }
  48.     integration_sum -= ( f(from) + f(to) ) / 2;
  49.     return integration_sum * (to - from) / n_steps;
  50. }
  51.  
  52.  
  53. int main (int argc, char** argv, char** env) {
  54.     TIMER_START(MAIN);
  55.     int rc;
  56.     // MPI_Wtime -  Returns an elapsed time on the calling processor
  57.     // double MPI_Wtime( void )
  58.     double start_time = MPI_Wtime( ), active_time = 0;
  59.    
  60.     // MPI_Init - Initialize the MPI execution environment
  61.     // int MPI_Init( int *argc, char ***argv )
  62.     if ( rc = MPI_Init( &argc, &argv ) ) {
  63.         PERROR( "MPI_Init failed with code rc = %d", rc );
  64.         goto exit_failure_section;
  65.     }
  66.    
  67.     int cluster_size = 0;
  68.     // MPI_Comm_size -  Determines the size of the group associated with a communicator
  69.     // int MPI_Comm_size( MPI_Comm comm, int *size )
  70.     if ( rc = MPI_Comm_size( MPI_COMM_WORLD, &cluster_size ) ) {
  71.         PERROR( "MPI_Comm_size failed with code rc = %d", rc );
  72.         goto exit_failure_section;
  73.     }
  74.    
  75.     int current_node_rank = 0;
  76.     // MPI_Comm_rank -  Determines the rank of the calling process in the communicator
  77.     // int MPI_Comm_rank( MPI_Comm comm, int *rank )
  78.     if ( rc = MPI_Comm_rank( MPI_COMM_WORLD, &current_node_rank ) ) {
  79.         PERROR( "MPI_Comm_rank failed with code rc = %d", rc );
  80.         goto exit_failure_section;
  81.     }
  82.    
  83.     LOG_VAR( cluster_size, "%d" );
  84.     LOG_VAR( current_node_rank, "%d" );
  85.        
  86.     double local_left = left_integration_bound +
  87.                     ( right_integration_bound - left_integration_bound )
  88.                     / cluster_size * current_node_rank;
  89.    
  90.     double local_right = left_integration_bound +
  91.                 ( right_integration_bound - left_integration_bound )
  92.                 / cluster_size * (current_node_rank + 1);
  93.    
  94.    
  95.     double local_result = integrate( f, local_left, local_right, integration_step );
  96.    
  97.     double global_result = 0;
  98.     // MPI_Reduce -  Reduces values on all processes to a single value
  99.     // int MPI_Reduce(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype,
  100.     //                      MPI_Op op, int root, MPI_Comm comm)
  101.     if ( rc = MPI_Reduce( &local_result, &global_result,
  102.                           1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD  
  103.                         ) ) {
  104.         PERROR( "MPI_Reduce failed with code rc = %d", rc );
  105.         goto exit_failure_section;
  106.     }
  107.    
  108.     // MPI_Bcast -  Broadcasts a message from the process with rank "root" to all other pro‐
  109.     // cesses of the communicator
  110.     // int MPI_Bcast( void *buffer, int count, MPI_Datatype datatype, int root,
  111.     //                                MPI_Comm comm )
  112.     // if ( rc = MPI_Bcast( &global_result, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD ) ) {
  113.     //    PERROR( "MPI_Bcast failed with code rc = %d", rc );
  114.     //    goto exit_failure_section;
  115.     // }
  116.    
  117.     LOG_VAR( global_result, "%lg" );
  118.    
  119.     // see declaration previously
  120.     active_time = (MPI_Wtime() - start_time);
  121.     printf("T:%.03lg\n", active_time);
  122.  
  123.     // MPI_Finalize -  Terminates MPI execution environment
  124.     // int MPI_Finalize( void )
  125.     MPI_Finalize();
  126.     // LOG( "TIME = %.04f", (stop_time - start_time) * 1000 );
  127.     TIMER_DUMP(MAIN);
  128.     return EXIT_SUCCESS;  
  129.        
  130.    
  131. // Exit with some nonzero errorcode
  132. exit_failure_section:
  133.  
  134.     // MPI_Abort -  Terminates MPI execution environment
  135.     // int MPI_Abort(MPI_Comm comm, int errorcode)
  136.     MPI_Abort( MPI_COMM_WORLD, rc );
  137.    
  138.     // see declaration previously
  139.     active_time = (MPI_Wtime() - start_time);  
  140.     printf("T:%.03lg\n", active_time);
  141.  
  142.     TIMER_DUMP(MAIN);
  143.  
  144.     // MPI_Finalize -  Terminates MPI execution environment
  145.     // int MPI_Finalize( void )
  146.     MPI_Finalize();
  147.    
  148.     return EXIT_FAILURE;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement