diff --git a/include/internal/fmpi_futhark.h b/include/internal/fmpi_futhark.h index 1947b5c4211cf8c682888ae5a258bcaf9e34b5d2..3fc63a43201ed44efc851164f3292f1272dc0f97 100644 --- a/include/internal/fmpi_futhark.h +++ b/include/internal/fmpi_futhark.h @@ -124,7 +124,7 @@ int fmpi_futhark_sync(const struct fmpi_futhark_ctx * ctx); * - \p{ctx} must be a valid pointer allocated with fmpi_init(). * - \p{func} must not be `NULL`. */ -_Bool fmpi_futhark_check_error(const struct fmpi_futhark_ctx * ctx, const char * func); +int fmpi_futhark_check_error(const struct fmpi_futhark_ctx * ctx, const char * func); /*------------------------------------------------------------------------------ fmpi_futhark_new_data_sync() ------------------------------------------------------------------------------*/ diff --git a/src/fmpi_futhark.c b/src/fmpi_futhark.c index 98e88e3e109109ecd03d9435f263b8982981158d..2782ba3bcaf15a11480e336eb3ff20d89d949d2f 100644 --- a/src/fmpi_futhark.c +++ b/src/fmpi_futhark.c @@ -82,14 +82,15 @@ struct fmpi_futhark_ctx * fmpi_futhark_init( ){ struct fmpi_futhark_ctx * ctx = malloc(sizeof(*ctx)); if(ctx == NULL) { - FMPI_RAISE_ERROR(err_handler, "FUTHARK", "malloc(fmpi_futhark_ctx) failed!"); + FMPI_RAISE_ERROR(err_handler, "FMPI", "malloc(fmpi_futhark_ctx) failed!"); return NULL; } ctx->err_handler = err_handler; ctx->cfg = futhark_context_config_new(); if(ctx->cfg == NULL) { FMPI_RAISE_FUTHARK_ERROR(ctx, "futhark_context_config_new() failed!"); - return ctx; + free(ctx); + return NULL; } ctx->ctx = futhark_context_new(ctx->cfg); // The doc says in case of error futhark_context_free() should be called @@ -100,12 +101,14 @@ struct fmpi_futhark_ctx * fmpi_futhark_init( if(ctx->ctx == NULL) { FMPI_RAISE_FUTHARK_ERROR(ctx, "futhark_context_new() failed!"); futhark_context_config_free(ctx->cfg); - return ctx; + free(ctx); + return NULL; } - if(fmpi_futhark_check_error(ctx, "futhark_context_new") == true) { - fmpi_futhark_sync(ctx); - futhark_context_free(ctx->ctx); - futhark_context_config_free(ctx->cfg); + if(fmpi_futhark_check_error(ctx, "futhark_context_new") != FMPI_SUCCESS) { + if(fmpi_futhark_exit(&ctx) != FMPI_SUCCESS) { + FMPI_RAISE_ERROR(err_handler, "FMPI", "fmpi_futhark_exit() failed!"); + } + return NULL; } return ctx; } @@ -117,14 +120,14 @@ int fmpi_futhark_exit(struct fmpi_futhark_ctx ** const ctx) assert(ctx != NULL); assert(*ctx != NULL); - int err_id = 0; - if(futhark_context_sync((*ctx)->ctx) != 0) { - err_id = -1; - } + const int err = futhark_context_sync((*ctx)->ctx); futhark_context_free((*ctx)->ctx); futhark_context_config_free((*ctx)->cfg); free(*ctx); *ctx = NULL; - return err_id; + if(err != FUTHARK_SUCCESS) { + return FMPI_ERR_FUTHARK; + } + return FMPI_SUCCESS; } /*------------------------------------------------------------------------------ fmpi_futhark_sync() @@ -132,17 +135,15 @@ int fmpi_futhark_exit(struct fmpi_futhark_ctx ** const ctx) int fmpi_futhark_sync(const struct fmpi_futhark_ctx * const ctx) { assert(ctx != NULL); - const int err = futhark_context_sync(ctx->ctx); - if(err != 0) { - fmpi_futhark_check_error(ctx, "futhark_context_sync"); - return -1; + if(futhark_context_sync(ctx->ctx) != FUTHARK_SUCCESS) { + return fmpi_futhark_check_error(ctx, "futhark_context_sync"); } - return 0; + return FMPI_SUCCESS; } /*------------------------------------------------------------------------------ fmpi_futhark_check_error() ------------------------------------------------------------------------------*/ -_Bool fmpi_futhark_check_error( +int fmpi_futhark_check_error( const struct fmpi_futhark_ctx * const ctx, const char * const func ){ assert(ctx != NULL); @@ -150,10 +151,10 @@ _Bool fmpi_futhark_check_error( char * err_str = futhark_context_get_error(ctx->ctx); if(err_str != NULL) { FMPI_RAISE_FUTHARK_ERROR(ctx, "%s() failed! %s", func, err_str); - free(err_str); err_str = NULL; - return true; + free(err_str); + return FMPI_ERR_FUTHARK; } - return false; + return FMPI_SUCCESS; } /*------------------------------------------------------------------------------ fmpi_futhark_new_data_sync() @@ -169,7 +170,11 @@ void * fmpi_futhark_new_data_sync( const size_t idx = FMPI_PRIV_FUTHARK_FUNC_IDX(dim_cnt, type); void * new_data = fmpi_futhark_new_data_func_list[idx](ctx, data, x, y, z); fmpi_futhark_sync(ctx); - fmpi_futhark_check_error(ctx, "futhark_new_##T##_##D##"); + //! @todo Construct a proper string with dimension and type + const int err = fmpi_futhark_check_error(ctx, "futhark_new_##T##_##D##"); + if(new_data == NULL || err != FMPI_SUCCESS) { + return NULL; + } return new_data; } /*------------------------------------------------------------------------------ @@ -196,10 +201,10 @@ int fmpi_futhark_free_data_sync( assert(ctx != NULL); assert(data != NULL); const size_t idx = FMPI_PRIV_FUTHARK_FUNC_IDX(dim_cnt, type); - const int err = fmpi_futhark_free_data_func_list[idx](ctx, data); + fmpi_futhark_free_data_func_list[idx](ctx, data); fmpi_futhark_sync(ctx); - fmpi_futhark_check_error(ctx, "futhark_free_##T##_##D##"); - return err; + //! @todo Construct a proper string with dimension and type + return fmpi_futhark_check_error(ctx, "futhark_free_##T##_##D##"); } /*------------------------------------------------------------------------------ fmpi_futhark_free_data_async() @@ -227,7 +232,7 @@ void * fmpi_futhark_get_data_sync( const size_t idx = FMPI_PRIV_FUTHARK_FUNC_IDX(dim_cnt, type); fmpi_futhark_get_data_func_list[idx](ctx, in, out); fmpi_futhark_sync(ctx); - if(fmpi_futhark_check_error(ctx, "futhark_values_##T##_##D##d") == true) { + if(fmpi_futhark_check_error(ctx, "futhark_values_##T##_##D##d") != FMPI_SUCCESS) { return NULL; } return out; @@ -244,19 +249,18 @@ void * fmpi_futhark_get_data_async( assert(out != NULL); assert(dim_cnt <= FMPI_DIM_MAX); const size_t idx = FMPI_PRIV_FUTHARK_FUNC_IDX(dim_cnt, type); - const int err = fmpi_futhark_get_data_func_list[idx](ctx, in, out); - if(err != FUTHARK_SUCCESS) { + if(fmpi_futhark_get_data_func_list[idx](ctx, in, out) != FMPI_SUCCESS) { return NULL; } return out; } #define FMPI_FUTHARK_DEFINITION(D, T) \ +_Static_assert((D) > 0 && (D) <= FMPI_DIM_MAX, ""); \ void * fmpi_futhark_new_##D##d_##T( \ const struct fmpi_futhark_ctx * const ctx, const void * const data, \ const size_t x, const size_t y, const size_t z \ ){ \ - _Static_assert((D) <= FMPI_DIM_MAX, ""); \ assert(ctx != NULL); \ assert(data != NULL); \ struct futhark_##T##_##D##d * new_data = FMPI_FUTHARK_NEW_##D(T, ctx->ctx, data, x, y, z); \ @@ -268,27 +272,25 @@ void * fmpi_futhark_new_##D##d_##T( \ int fmpi_futhark_free_##D##d_##T( \ const struct fmpi_futhark_ctx * const ctx, void * const data \ ){ \ - _Static_assert((D) <= FMPI_DIM_MAX, ""); \ assert(ctx != NULL); \ assert(data != NULL); \ - const int err = futhark_free_##T##_##D##d(ctx->ctx, data); \ - if(err != FUTHARK_SUCCESS) { \ + if(futhark_free_##T##_##D##d(ctx->ctx, data) != FUTHARK_SUCCESS) { \ FMPI_RAISE_FUTHARK_ERROR(ctx, CPL_STRINGIFY(futhark_free_##T##_##D##d)"() failed!"); \ + return FMPI_ERR_FUTHARK; \ } \ - return err; \ + return FMPI_SUCCESS; \ } \ int fmpi_futhark_values_##D##d_##T( \ const struct fmpi_futhark_ctx * const ctx, void * const in, void * const out \ ){ \ - _Static_assert((D) <= FMPI_DIM_MAX, ""); \ assert(ctx != NULL); \ assert(in != NULL); \ assert(out != NULL); \ - const int err = futhark_values_##T##_##D##d(ctx->ctx, in, out); \ - if(err != FUTHARK_SUCCESS) { \ - FMPI_RAISE_FUTHARK_ERROR(ctx, CPL_STRINGIFY(futhark_free_##T##_##D##d)"() failed!"); \ + if(futhark_values_##T##_##D##d(ctx->ctx, in, out) != FUTHARK_SUCCESS) { \ + FMPI_RAISE_FUTHARK_ERROR(ctx, CPL_STRINGIFY(fmpi_futhark_values_##T##_##D##d)"() failed!"); \ + return FMPI_ERR_FUTHARK; \ } \ - return err; \ + return FMPI_SUCCESS; \ } FMPI_DEFINE_FUNCS_DT(FMPI_FUTHARK_DEFINITION, 1, FMPI_FUTHARK_TYPES)