1/** @example ErrorHandlingSimulation.cpp
2 * Description: This example simulates MOAB's enhanced error handling in parallel. \n
3 * All of the errors are contrived, used for simulation purpose only. \n
4 *
5 * Note: We do not need a moab instance for this example
6 *
7 * <b>To run</b>: mpiexec -np 4 ./ErrorHandlingSimulation <test_case_num(1 to 4)> \n
8 */910#include"moab/MOABConfig.h"11#include"moab/ErrorHandler.hpp"12#ifdef MOAB_HAVE_MPI13#include"moab_mpi.h"14#endif1516#include<iostream>17#include<cstdlib>1819usingnamespace moab;
20usingnamespace std;
2122// Functions that create and handle contrived errors23// Call hierarchy: A calls B, and B calls C24ErrorCode FunctionC( int test_case_num, int rank )
25 {
26switch( test_case_num )
27 {
28case1:
29// Simulate a global fatal error MB_NOT_IMPLEMENTED on all processors30// Note, it is printed by root processor 0 only31MB_SET_GLB_ERR( MB_NOT_IMPLEMENTED, "A contrived global error MB_NOT_IMPLEMENTED" );
32break;
33case2:
34// Simulate a per-processor relevant error MB_INDEX_OUT_OF_RANGE on all processors35// Note, it is printed by all processors36MB_SET_ERR( MB_INDEX_OUT_OF_RANGE, "A contrived error MB_INDEX_OUT_OF_RANGE on processor " << rank );
37break;
38case3:
39// Simulate a per-processor relevant error MB_TYPE_OUT_OF_RANGE on all processors except40// root Note, it is printed by all non-root processors41if( 0 != rank )
42MB_SET_ERR( MB_TYPE_OUT_OF_RANGE, "A contrived error MB_TYPE_OUT_OF_RANGE on processor " << rank );
43break;
44case4:
45// Simulate a per-processor relevant error MB_INDEX_OUT_OF_RANGE on processor 146// Note, it is printed by processor 1 only47if( 1 == rank )
48MB_SET_ERR( MB_INDEX_OUT_OF_RANGE, "A contrived error MB_INDEX_OUT_OF_RANGE on processor 1" );
4950// Simulate a per-processor relevant error MB_TYPE_OUT_OF_RANGE on processor 351// Note, it is printed by processor 3 only52if( 3 == rank ) MB_SET_ERR( MB_TYPE_OUT_OF_RANGE, "A contrived error MB_TYPE_OUT_OF_RANGE on processor 3" );
53break;
54default:
55break;
56 }
5758return MB_SUCCESS;
59 }
6061ErrorCode FunctionB( int test_case_num, int rank )
62 {
63 ErrorCode err_code = FunctionC( test_case_num, rank );MB_CHK_ERR( err_code );
6465return MB_SUCCESS;
66 }
6768ErrorCode FunctionA( int test_case_num, int rank )
69 {
70 ErrorCode err_code = FunctionB( test_case_num, rank );MB_CHK_ERR( err_code );
7172return MB_SUCCESS;
73 }
7475intmain( int argc, char** argv )
76 {
77if( argc < 2 )
78 {
79 cout << "Usage: " << argv[0] << " <test_case_num(1 to 4)>" << endl;
80return0;
81 }
8283#ifdef MOAB_HAVE_MPI84MPI_Init( &argc, &argv );
85#endif8687// Initialize error handler, required for this example (not using a moab instance)88MBErrorHandler_Init();
8990int test_case_num = atoi( argv[1] );
91int rank = 0;
92#ifdef MOAB_HAVE_MPI93MPI_Comm_rank( MPI_COMM_WORLD, &rank );
94#endif9596 ErrorCode rval = FunctionA( test_case_num, rank );MB_CHK_ERR( rval );
9798// Finalize error handler, required for this example (not using a moab instance)99MBErrorHandler_Finalize();
100101#ifdef MOAB_HAVE_MPI102MPI_Finalize();
103#endif104105return0;
106 }