Skip to content
Snippets Groups Projects
Verified Commit 3feb2764 authored by raphael.bach's avatar raphael.bach
Browse files

Refactor `fmpi_ctx` module

parent 120d7b16
Branches
No related tags found
No related merge requests found
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
* @license{ * @license{
* BSD Zero Clause License * BSD Zero Clause License
* *
* Copyright (c) 2022 by Raphael Bach
*
* Permission to use, copy, modify, and/or distribute this software for any * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted. * purpose with or without fee is hereby granted.
* *
...@@ -21,29 +23,67 @@ ...@@ -21,29 +23,67 @@
==============================================================================*/ ==============================================================================*/
#ifndef FMPI_INIT_H_20211218002623 #ifndef FMPI_INIT_H_20211218002623
#define FMPI_INIT_H_20211218002623 #define FMPI_INIT_H_20211218002623
/*==============================================================================
INCLUDE
==============================================================================*/
// C Standard Library
// External
// Internal
/*============================================================================== /*==============================================================================
STRUCT STRUCT
==============================================================================*/ ==============================================================================*/
struct fmpi_ctx { /*------------------------------------------------------------------------------
struct fmpi_mpi_ctx * mpi; fmpi_ctx
struct futhark_context_config * futhark_cfg; ------------------------------------------------------------------------------*/
struct futhark_context * futhark; /**
}; * TODO
*
* @example{
* TODO
* }
*/
typedef struct fmpi_ctx {
struct fmpi_mpi_ctx * mpi; //!< TODO
struct fmpi_futhark_ctx * fut; //!< TODO
double timer_start; //!< TODO
double timer_stop; //!< TODO
} fmpi_ctx;
/*============================================================================== /*==============================================================================
PUBLIC FUNCTION PUBLIC FUNCTION
==============================================================================*/ ==============================================================================*/
struct fmpi_ctx * fmpi_init(int * argc, char ** argv[]); /*------------------------------------------------------------------------------
void fmpi_exit(struct fmpi_ctx * ctx); fmpi_ctx_create()
_Noreturn void fmpi_abort(struct fmpi_ctx * ctx); ------------------------------------------------------------------------------*/
struct fmpi_ctx * fmpi_ctx_create(void); /**
void fmpi_ctx_destroy(struct fmpi_ctx ** const ctx); * TODO
void fmpi_ctx_print(const struct fmpi_ctx * const ctx); *
* @param[in] argc : TODO
* @param[in] argv : TODO
*
* @return
* - @success: TODO
* - @failure: TODO
*
* @warning TODO
*
* @example{
* TODO
* }
*/
struct fmpi_ctx * fmpi_ctx_create(int * argc, char ** argv[]);
/*------------------------------------------------------------------------------
fmpi_ctx_destroy()
------------------------------------------------------------------------------*/
/**
* TODO
*
* @param[in] ctx : TODO
*
* @return
* - @success: TODO
* - @failure: TODO
*
* @warning TODO
*
* @example{
* TODO
* }
*/
int fmpi_ctx_destroy(struct fmpi_ctx ** ctx);
/*============================================================================== /*==============================================================================
GUARD GUARD
==============================================================================*/ ==============================================================================*/
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
* @license{ * @license{
* BSD Zero Clause License * BSD Zero Clause License
* *
* Copyright (c) 2022 by Raphael Bach
*
* Permission to use, copy, modify, and/or distribute this software for any * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted. * purpose with or without fee is hereby granted.
* *
...@@ -20,7 +22,7 @@ ...@@ -20,7 +22,7 @@
INCLUDE INCLUDE
==============================================================================*/ ==============================================================================*/
// Own header // Own header
#include "fmpi_ctx.h" #include "internal/fmpi_ctx.h"
// C Standard Library // C Standard Library
#include <assert.h> #include <assert.h>
#include <stdint.h> #include <stdint.h>
...@@ -28,108 +30,103 @@ ...@@ -28,108 +30,103 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
// Internal // Internal
#include "fmpi_futhark.h" #include "internal/fmpi_error.h"
#include "fmpi_mpi.h" #include "internal/fmpi_futhark.h"
#include "internal/fmpi_fut.h" #include "internal/fmpi_futhark_entry.h"
#include "internal/fmpi_mpi.h"
/*============================================================================== /*==============================================================================
PUBLIC FUNCTION PRIVATE FUNCTION DECLARATION
==============================================================================*/ ==============================================================================*/
/*------------------------------------------------------------------------------ /*------------------------------------------------------------------------------
fmpi_init() fmpi_mpi_error_callback()
------------------------------------------------------------------------------*/ ------------------------------------------------------------------------------*/
struct fmpi_ctx * fmpi_init(int * argc, char ** argv[]) static void fmpi_error_callback(
{ const struct fmpi_error * err, const void * user_data
struct fmpi_ctx * ctx = fmpi_ctx_create(); );
if(ctx == NULL) {
fprintf(stderr, "fmpi_ctx_create(fmpi_ctx) failed!\n");
return NULL;
}
ctx->mpi = fmpi_mpi_init(argc, argv);
if(ctx->mpi == NULL) {
fprintf(stderr, "fmpi_mpi_init() failed!\n");
fmpi_ctx_destroy(&ctx);
return NULL;
}
const int err_id = fmpi_futhark_init(ctx);
if(err_id != MPI_SUCCESS) {
fprintf(stderr, "fmpi_futhark_init() failed!\n");
fmpi_ctx_destroy(&ctx);
return NULL;
}
return ctx;
}
/*------------------------------------------------------------------------------
fmpi_exit()
------------------------------------------------------------------------------*/
void fmpi_exit(struct fmpi_ctx * ctx)
{
assert(ctx != NULL);
fmpi_futhark_exit(ctx);
fmpi_mpi_destroy(&ctx->mpi);
fmpi_mpi_exit(ctx->mpi);
fmpi_ctx_destroy(&ctx);
}
/*------------------------------------------------------------------------------ /*------------------------------------------------------------------------------
fmpi_abort() fmpi_ctx_print_error()
------------------------------------------------------------------------------*/ ------------------------------------------------------------------------------*/
_Noreturn void fmpi_abort(struct fmpi_ctx * ctx) static void fmpi_ctx_print_error(
{ const struct fmpi_error * err, const void * user_data
if(ctx != NULL) { );
fmpi_futhark_exit(ctx); /*==============================================================================
fmpi_mpi_destroy(&ctx->mpi); PUBLIC FUNCTION DEFINITION
fmpi_ctx_destroy(&ctx); ==============================================================================*/
}
fmpi_mpi_abort(NULL);
abort();
}
/*------------------------------------------------------------------------------ /*------------------------------------------------------------------------------
fmpi_ctx_create() fmpi_ctx_create()
------------------------------------------------------------------------------*/ ------------------------------------------------------------------------------*/
struct fmpi_ctx * fmpi_ctx_create(void) struct fmpi_ctx * fmpi_ctx_create(int * argc, char ** argv[])
{ {
struct fmpi_ctx * ctx = malloc(sizeof(*ctx)); struct fmpi_ctx * ctx = malloc(sizeof(*ctx));
if(ctx == NULL) { if(ctx == NULL) {
fprintf(stderr, "malloc(fmpi_ctx) failed!\n"); fprintf(stderr, "malloc(fmpi_ctx) failed!\n");
return NULL; return NULL;
} }
ctx->mpi = NULL; // MPI initialization
ctx->futhark_cfg = NULL; ctx->mpi = fmpi_mpi_init(argc, argv, &(struct fmpi_error_handler) {
ctx->futhark = NULL; .func = fmpi_error_callback,
.user_data = ctx
});
if(ctx->mpi == NULL) {
fprintf(stderr, "fmpi_mpi_init() failed!\n");
free(ctx);
return NULL;
}
// Futhark Initialization
ctx->fut = fmpi_futhark_init(&(struct fmpi_error_handler) {
.func = fmpi_error_callback,
.user_data = ctx
});
return ctx; return ctx;
} }
/*------------------------------------------------------------------------------ /*------------------------------------------------------------------------------
fmpi_ctx_create() fmpi_ctx_create()
------------------------------------------------------------------------------*/ ------------------------------------------------------------------------------*/
void fmpi_ctx_destroy(struct fmpi_ctx ** const ctx) int fmpi_ctx_destroy(struct fmpi_ctx ** const ctx)
{ {
assert(ctx != NULL); assert(ctx != NULL);
assert(*ctx != NULL); assert(*ctx != NULL);
if((*ctx)->futhark_cfg != NULL && (*ctx)->futhark != NULL) { int err_id = -1;
fmpi_futhark_exit(*ctx); if((*ctx)->fut != NULL) {
err_id = fmpi_futhark_exit(&(*ctx)->fut);
} }
/*if((*ctx)->mpi != NULL) { if((*ctx)->mpi != NULL) {
fmpi_mpi_exit(*ctx); err_id = fmpi_mpi_exit(&(*ctx)->mpi);
}*/
free((*ctx)->mpi);
(*ctx)->mpi = NULL;
free(*ctx);
*ctx = NULL;
} }
free(*ctx); *ctx = NULL;
return err_id;
}
/*==============================================================================
PRIVATE FUNCTION DEFINITION
==============================================================================*/
/*------------------------------------------------------------------------------ /*------------------------------------------------------------------------------
fmpi_ctx_print() fmpi_error_callback()
------------------------------------------------------------------------------*/ ------------------------------------------------------------------------------*/
void fmpi_ctx_print(const struct fmpi_ctx * const ctx) static void fmpi_error_callback(
{ const struct fmpi_error * const err, const void * const user_data
assert(ctx != NULL); ){
assert(err != NULL);
if(ctx->mpi != NULL) { fmpi_ctx_print_error(err, user_data);
fmpi_mpi_ctx_print(ctx->mpi); }
/*------------------------------------------------------------------------------
fmpi_ctx_print_error()
------------------------------------------------------------------------------*/
static void fmpi_ctx_print_error(
const struct fmpi_error * const err, const void * const user_data
){
assert(err != NULL);
int rank = -1;
if(user_data != NULL) {
rank = ((const fmpi_ctx *)user_data)->mpi->rank;
} }
char timestamp_buf[FMPI_ERROR_STR_BUF_SIZE] = {'\0'};
const char * timestamp_str = timestamp_buf;
const char * ret = fmpi_error_timestamp_str(err->time, timestamp_buf, sizeof(timestamp_buf));
if(ret == NULL) {
timestamp_str = "<Error getting timestamp>";
}
fprintf(stderr, "%s - %s:%zu:%s() - [%s ERROR rank=%d] %s\n",
timestamp_str, err->file, err->line, err->func, err->module, rank, err->msg
);
} }
/*==============================================================================
PRIVATE FUNCTION
==============================================================================*/
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment