diff --git a/include/internal/fmpi_mpi.h b/include/internal/fmpi_mpi.h index 3f09b4391648888e344eb75eff4ba9787456f772..d3de3ee1c2dda93b5113559332e8f9ac14558bbe 100644 --- a/include/internal/fmpi_mpi.h +++ b/include/internal/fmpi_mpi.h @@ -126,7 +126,7 @@ _Bool fmpi_mpi_is_root(const struct fmpi_mpi_ctx * ctx); * TODO * * @param[in] ctx : TODO - * @param[in] err_id : TODO + * @param[in] err : TODO * @param[in] func : TODO * * @return @@ -139,8 +139,8 @@ _Bool fmpi_mpi_is_root(const struct fmpi_mpi_ctx * ctx); * TODO * } */ -_Bool fmpi_mpi_check_error( - const struct fmpi_mpi_ctx * ctx, int err_id, const char * func +int fmpi_mpi_check_error( + const struct fmpi_mpi_ctx * ctx, int err, const char * func ); /*------------------------------------------------------------------------------ fmpi_mpi_ctx_print() @@ -200,7 +200,7 @@ _Bool fmpi_mpi_finalized(const struct fmpi_mpi_ctx * ctx); * TODO * } */ -void fmpi_mpi_abort(const struct fmpi_mpi_ctx * ctx); +int fmpi_mpi_abort(const struct fmpi_mpi_ctx * ctx); /*------------------------------------------------------------------------------ fmpi_mpi_world_barrier() ------------------------------------------------------------------------------*/ diff --git a/src/fmpi_mpi.c b/src/fmpi_mpi.c index cb852f4af794dd1d91ff0f74ed37efcac60d0d88..912c1c03439c9d73ce061a8c931634684960429a 100644 --- a/src/fmpi_mpi.c +++ b/src/fmpi_mpi.c @@ -59,33 +59,41 @@ struct fmpi_mpi_ctx * fmpi_mpi_init( ){ struct fmpi_mpi_ctx * ctx = malloc(sizeof(*ctx)); if(ctx == NULL) { - FMPI_RAISE_ERROR(err_handler, "MPI", "malloc(fmpi_mpi_ctx) failed!"); + FMPI_RAISE_ERROR(err_handler, "FMPI", "malloc(fmpi_mpi_ctx) failed!"); return NULL; } ctx->err_handler = err_handler; ctx->root = FMPI_MPI_ROOT; - int err_id = MPI_Init(argc, argv); - if(fmpi_mpi_check_error(ctx, err_id, "MPI_Init") == true) { - return ctx; + int err = MPI_Init(argc, argv); + if(fmpi_mpi_check_error(ctx, err, "MPI_Init") != FMPI_SUCCESS) { + free(ctx); + return NULL; } - err_id = MPI_Comm_dup(MPI_COMM_WORLD, &ctx->world); - if(fmpi_mpi_check_error(ctx, err_id, "MPI_Comm_dup") == true) { - free(ctx); + err = MPI_Comm_dup(MPI_COMM_WORLD, &ctx->world); + if(fmpi_mpi_check_error(ctx, err, "MPI_Comm_dup") != FMPI_SUCCESS) { + fmpi_mpi_exit(&ctx); return NULL; } - err_id = MPI_Comm_rank(ctx->world, &ctx->rank); - fmpi_mpi_check_error(ctx, err_id, "MPI_Comm_rank"); + err = MPI_Comm_rank(ctx->world, &ctx->rank); + if(fmpi_mpi_check_error(ctx, err, "MPI_Comm_rank") != FMPI_SUCCESS) { + fmpi_mpi_exit(&ctx); + return NULL; + } - err_id = MPI_Comm_size(ctx->world, &ctx->size); - fmpi_mpi_check_error(ctx, err_id, "MPI_Comm_size"); - // `ctx->size` is used with `malloc()` and `malloc(0)` is implementation-defined. + err = MPI_Comm_size(ctx->world, &ctx->size); + if(fmpi_mpi_check_error(ctx, err, "MPI_Comm_size") != FMPI_SUCCESS) { + fmpi_mpi_exit(&ctx); + return NULL; + } + // `ctx->size` is used with `malloc()` and `malloc(0)` is implementation + // defined. assert(ctx->size > 0); - err_id = MPI_Get_processor_name(ctx->cpu_name, &ctx->cpu_name_length); - fmpi_mpi_check_error(ctx, err_id, "MPI_Get_processor_name"); + err = MPI_Get_processor_name(ctx->cpu_name, &ctx->cpu_name_length); + fmpi_mpi_check_error(ctx, err, "MPI_Get_processor_name"); return ctx; } @@ -97,12 +105,13 @@ int fmpi_mpi_exit(struct fmpi_mpi_ctx ** const ctx) assert(ctx != NULL); assert(*ctx != NULL); - int err_id = MPI_Finalize(); - if(fmpi_mpi_check_error(*ctx, err_id, "MPI_Finalize") == true) { - err_id = -1; + int err = FMPI_SUCCESS; + if(fmpi_mpi_finalized(*ctx) == false) { + err = MPI_Finalize(); + err = fmpi_mpi_check_error(*ctx, err, "MPI_Finalize"); } free(*ctx); *ctx = NULL; - return err_id; + return err; } /*------------------------------------------------------------------------------ fmpi_mpi_is_root() @@ -115,22 +124,22 @@ _Bool fmpi_mpi_is_root(const struct fmpi_mpi_ctx * const ctx) /*------------------------------------------------------------------------------ fmpi_mpi_check_error() ------------------------------------------------------------------------------*/ -_Bool fmpi_mpi_check_error( - const struct fmpi_mpi_ctx * ctx, int err_id, const char * const func +int fmpi_mpi_check_error( + const struct fmpi_mpi_ctx * ctx, int err, const char * const func ){ assert(ctx != NULL); assert(func != NULL); - if(err_id != MPI_SUCCESS) { + if(err != MPI_SUCCESS) { char err_str[MPI_MAX_ERROR_STRING] = {'\0'}; int err_str_len = 0; - err_id = MPI_Error_string(err_id, err_str, &err_str_len); - if(err_id != MPI_SUCCESS) { + err = MPI_Error_string(err, err_str, &err_str_len); + if(err != MPI_SUCCESS) { FMPI_RAISE_MPI_ERROR(ctx, "MPI_Error_string() failed!"); } FMPI_RAISE_MPI_ERROR(ctx, "%s() failed! %s", func, err_str); - return true; + return FMPI_ERR_MPI; } - return false; + return FMPI_SUCCESS; } /*------------------------------------------------------------------------------ fmpi_mpi_ctx_print() @@ -151,10 +160,11 @@ void fmpi_mpi_ctx_print(const struct fmpi_mpi_ctx * const ctx) int fmpi_mpi_world_rank(const struct fmpi_mpi_ctx * const ctx) { assert(ctx != NULL); - int rank = -1; - int err_id = MPI_Comm_rank(ctx->world, &rank); - if(fmpi_mpi_check_error(ctx, err_id, "MPI_Comm_rank") == true) { - return -1; + int rank = FMPI_ERROR; + int err = MPI_Comm_rank(ctx->world, &rank); + err = fmpi_mpi_check_error(ctx, err, "MPI_Comm_rank"); + if(err != FMPI_SUCCESS) { + return err; } return rank; } @@ -163,23 +173,25 @@ int fmpi_mpi_world_rank(const struct fmpi_mpi_ctx * const ctx) ------------------------------------------------------------------------------*/ _Bool fmpi_mpi_finalized(const struct fmpi_mpi_ctx * const ctx) { + assert(ctx != NULL); int finalized = 0; - int err_id = MPI_Finalized(&finalized); - if(ctx != NULL) { - fmpi_mpi_check_error(ctx, err_id, "MPI_Finalized"); + const int err = MPI_Finalized(&finalized); + if(err != MPI_SUCCESS) { + FMPI_RAISE_MPI_ERROR(ctx, "MPI_Finalized() failed with err=%d!", err); } return finalized; } /*------------------------------------------------------------------------------ fmpi_mpi_abort() ------------------------------------------------------------------------------*/ -void fmpi_mpi_abort(const struct fmpi_mpi_ctx * const ctx) +int fmpi_mpi_abort(const struct fmpi_mpi_ctx * const ctx) { assert(ctx != NULL); if(fmpi_mpi_finalized(ctx) == false) { - int err_id = MPI_Abort(ctx->world, MPI_ERR_UNKNOWN); - fmpi_mpi_check_error(ctx, err_id, "MPI_Abort"); + const int err = MPI_Abort(ctx->world, MPI_ERR_UNKNOWN); + return fmpi_mpi_check_error(ctx, err, "MPI_Abort"); } + return FMPI_ERROR; } /*------------------------------------------------------------------------------ fmpi_mpi_world_barrier() @@ -187,11 +199,8 @@ void fmpi_mpi_abort(const struct fmpi_mpi_ctx * const ctx) int fmpi_mpi_world_barrier(const struct fmpi_mpi_ctx * const ctx) { assert(ctx != NULL); - const int err_id = MPI_Barrier(ctx->world); - if(fmpi_mpi_check_error(ctx, err_id, "MPI_Barrier") == true) { - return -1; - } - return err_id; + const int err = MPI_Barrier(ctx->world); + return fmpi_mpi_check_error(ctx, err, "MPI_Barrier"); } /*------------------------------------------------------------------------------ fmpi_mpi_type() @@ -230,7 +239,7 @@ int fmpi_mpi_gather_in_place( if(send_type == recv_type) { assert(send_cnt <= recv_cnt); } - int err = 0; + int err = MPI_SUCCESS; if(ctx->rank == root) { err = MPI_Gather( MPI_IN_PLACE, (int)send_cnt, send_type, @@ -244,8 +253,5 @@ int fmpi_mpi_gather_in_place( root, comm ); } - if(fmpi_mpi_check_error(ctx, err, "MPI_Gather") == true) { - return -1; - } - return 0; + return fmpi_mpi_check_error(ctx, err, "MPI_Gather"); }