Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
ErrorHandlingSimulation.cpp
Go to the documentation of this file.
1 /**
2  * @file ErrorHandlingSimulation.cpp
3  * @brief Example demonstrating MOAB's enhanced error handling in parallel
4  *
5  * This example shows how to:
6  * - Initialize and finalize MOAB's error handler
7  * - Simulate different types of errors in parallel execution
8  * - Handle global fatal errors vs per-processor errors
9  * - Use error propagation through function call hierarchy
10  * - Demonstrate error reporting behavior across processors
11  *
12  * The example demonstrates four test cases:
13  * - Test case 1: Global fatal error (MB_NOT_IMPLEMENTED) on all processors
14  * - Test case 2: Per-processor error (MB_INDEX_OUT_OF_RANGE) on all processors
15  * - Test case 3: Per-processor error (MB_TYPE_OUT_OF_RANGE) on non-root processors
16  * - Test case 4: Different errors on specific processors (1 and 3)
17  *
18  * @author MOAB Development Team
19  * @date 2024
20  *
21 
22  * Description: This example simulates MOAB's enhanced error handling in parallel. \n
23  * All of the errors are contrived, used for simulation purpose only. \n
24  *
25  * Note: We do not need a moab instance for this example
26  *
27  * <b>To run</b>: mpiexec -np 4 ./ErrorHandlingSimulation <test_case_num(1 to 4)> \n
28  *
29  * @param argc Number of command line arguments
30  * @param argv Command line arguments array
31  * @return 0 on success, 1 on failure
32  */
33 
34 #include "moab/MOABConfig.h"
35 #include "moab/ErrorHandler.hpp"
36 #ifdef MOAB_HAVE_MPI
37 #include "moab_mpi.h"
38 #endif
39 
40 #include <iostream>
41 #include <cstdlib>
42 #include <memory>
43 
44 using namespace moab;
45 using namespace std;
46 
47 // Functions that create and handle contrived errors
48 // Call hierarchy: A calls B, and B calls C
49 ErrorCode FunctionC( int test_case_num, int rank )
50 {
51  switch( test_case_num )
52  {
53  case 1:
54  // Simulate a global fatal error MB_NOT_IMPLEMENTED on all processors
55  // Note, it is printed by root processor 0 only
56  MB_SET_GLB_ERR( MB_NOT_IMPLEMENTED, "A contrived global error MB_NOT_IMPLEMENTED" );
57  break;
58  case 2:
59  // Simulate a per-processor relevant error MB_INDEX_OUT_OF_RANGE on all processors
60  // Note, it is printed by all processors
61  MB_SET_ERR( MB_INDEX_OUT_OF_RANGE, "A contrived error MB_INDEX_OUT_OF_RANGE on processor " << rank );
62  break;
63  case 3:
64  // Simulate a per-processor relevant error MB_TYPE_OUT_OF_RANGE on all processors except
65  // root Note, it is printed by all non-root processors
66  if( 0 != rank )
67  MB_SET_ERR( MB_TYPE_OUT_OF_RANGE, "A contrived error MB_TYPE_OUT_OF_RANGE on processor " << rank );
68  break;
69  case 4:
70  // Simulate a per-processor relevant error MB_INDEX_OUT_OF_RANGE on processor 1
71  // Note, it is printed by processor 1 only
72  if( 1 == rank )
73  MB_SET_ERR( MB_INDEX_OUT_OF_RANGE, "A contrived error MB_INDEX_OUT_OF_RANGE on processor 1" );
74 
75  // Simulate a per-processor relevant error MB_TYPE_OUT_OF_RANGE on processor 3
76  // Note, it is printed by processor 3 only
77  if( 3 == rank ) MB_SET_ERR( MB_TYPE_OUT_OF_RANGE, "A contrived error MB_TYPE_OUT_OF_RANGE on processor 3" );
78  break;
79  default:
80  break;
81  }
82 
83  return MB_SUCCESS;
84 }
85 
86 ErrorCode FunctionB( int test_case_num, int rank )
87 {
88  MB_CHK_ERR( FunctionC( test_case_num, rank ) );
89 
90  return MB_SUCCESS;
91 }
92 
93 ErrorCode FunctionA( int test_case_num, int rank )
94 {
95  MB_CHK_ERR( FunctionB( test_case_num, rank ) );
96 
97  return MB_SUCCESS;
98 }
99 
100 int main( int argc, char** argv )
101 {
102  if( argc < 2 )
103  {
104  cout << "Usage: " << argv[0] << " <test_case_num(1 to 4)>" << endl;
105  return 0;
106  }
107 
108 #ifdef MOAB_HAVE_MPI
109  MPI_Init( &argc, &argv );
110 #endif
111 
112  // Initialize error handler, required for this example (not using a moab instance)
114 
115  int test_case_num = atoi( argv[1] );
116  int rank = 0;
117 #ifdef MOAB_HAVE_MPI
118  MPI_Comm_rank( MPI_COMM_WORLD, &rank );
119 #endif
120 
121  MB_CHK_ERR( FunctionA( test_case_num, rank ) );
122 
123  // Finalize error handler, required for this example (not using a moab instance)
125 
126 #ifdef MOAB_HAVE_MPI
127  MPI_Finalize();
128 #endif
129 
130  return 0;
131 }