| 1 | #include <stdlib.h> | 
| 2 | #include <stdio.h> | 
| 3 |  | 
| 4 | #ifdef  IS_MPI | 
| 5 | #include <mpi.h> | 
| 6 |  | 
| 7 | int nChecks; | 
| 8 | #endif // IS_MPI | 
| 9 |  | 
| 10 | #include "simError.h" | 
| 11 |  | 
| 12 | errorStruct painCave; | 
| 13 |  | 
| 14 | #ifdef IS_MPI | 
| 15 |  | 
| 16 | char checkPointMsg[MAX_SIM_ERROR_MSG_LENGTH]; | 
| 17 | int worldRank; | 
| 18 |  | 
| 19 | #endif | 
| 20 |  | 
| 21 |  | 
| 22 | void initSimError( void ){ | 
| 23 | painCave.errMsg[0] = '\0'; | 
| 24 | painCave.isFatal = 0; | 
| 25 | #ifdef IS_MPI | 
| 26 | painCave.isEventLoop = 0; | 
| 27 | nChecks = 0; | 
| 28 | MPI_Comm_rank( MPI_COMM_WORLD, &worldRank ); | 
| 29 | #endif | 
| 30 | } | 
| 31 |  | 
| 32 | int simError( void ) { | 
| 33 |  | 
| 34 | #ifdef IS_MPI | 
| 35 | int myError = 1; | 
| 36 | int isError; | 
| 37 |  | 
| 38 | if( painCave.isEventLoop ){ | 
| 39 | fprintf( stderr, | 
| 40 | "MPI Event Error on node %d: %s\n", | 
| 41 | worldRank, | 
| 42 | painCave.errMsg ); | 
| 43 | return 1; | 
| 44 | } | 
| 45 | else{ | 
| 46 | if( painCave.isFatal ){ | 
| 47 | fprintf( stderr, | 
| 48 | "MPI Fatal Error on node %d: %s\n", | 
| 49 | worldRank, | 
| 50 | painCave.errMsg ); | 
| 51 | MPI_Allreduce( &myError, &isError, 1, MPI_INT, MPI_LOR, MPI_COMM_WORLD ); | 
| 52 |  | 
| 53 | MPI_Finalize(); | 
| 54 | exit(0); | 
| 55 | } | 
| 56 | else{ | 
| 57 | fprintf( stderr, | 
| 58 | "MPI Warning on node %d: %s\n", | 
| 59 | worldRank, | 
| 60 | painCave.errMsg ); | 
| 61 | } | 
| 62 | return 1; | 
| 63 | } | 
| 64 |  | 
| 65 | #else | 
| 66 |  | 
| 67 | if( painCave.isFatal ){ | 
| 68 | fprintf( stderr, | 
| 69 | "Fatal Error: %s\n", | 
| 70 | painCave.errMsg ); | 
| 71 | exit(0); | 
| 72 | } | 
| 73 | else{ | 
| 74 | fprintf( stderr, | 
| 75 | "Warning: %s\n", | 
| 76 | painCave.errMsg ); | 
| 77 | } | 
| 78 | return 1; | 
| 79 |  | 
| 80 | #endif // IS_MPI | 
| 81 |  | 
| 82 | return 0; // in case nobody caught that. | 
| 83 |  | 
| 84 | } | 
| 85 |  | 
| 86 |  | 
| 87 | #ifdef IS_MPI | 
| 88 |  | 
| 89 | void MPIcheckPoint( void ){ | 
| 90 |  | 
| 91 | int myError = 0; | 
| 92 | int isError; | 
| 93 |  | 
| 94 | MPI_Allreduce( &myError, &isError, 1, MPI_INT, MPI_LOR, MPI_COMM_WORLD ); | 
| 95 | if( isError ){ | 
| 96 | MPI_Finalize(); | 
| 97 | exit(0); | 
| 98 | } | 
| 99 |  | 
| 100 | #ifdef CHECKPOINT_VERBOSE | 
| 101 | nChecks++; | 
| 102 | if( worldRank == 0 ){ | 
| 103 | fprintf(stderr, | 
| 104 | "Checkpoint #%d reached: %s\n", | 
| 105 | nChecks, | 
| 106 | checkPointMsg ); | 
| 107 | } | 
| 108 | #endif // CHECKPOINT_VERBOSE | 
| 109 |  | 
| 110 | } | 
| 111 |  | 
| 112 | #endif // IS_MPI |