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

Refactor `fmpi_reduce`

parent 3feb2764
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.
* *
...@@ -24,11 +26,8 @@ ...@@ -24,11 +26,8 @@
/*============================================================================== /*==============================================================================
INCLUDE INCLUDE
==============================================================================*/ ==============================================================================*/
// C Standard Library
// External
// Internal // Internal
#include "fmpi_ctx.h" #include "internal/generic/fmpi_reduce_generic.h"
#include "internal/fmpi_reduce_internal.h"
/*============================================================================== /*==============================================================================
PUBLIC FUNCTION PUBLIC FUNCTION
==============================================================================*/ ==============================================================================*/
...@@ -50,7 +49,19 @@ ...@@ -50,7 +49,19 @@
*/ */
#define fmpi_local_reduce_prod(ctx, array, length) \ #define fmpi_local_reduce_prod(ctx, array, length) \
FMPI_GENERIC_FUNC(array, local_reduce_prod, FMPI_REDUCE_PROD_TYPES)(ctx, array, length) FMPI_GENERIC_FUNC(array, local_reduce_prod, FMPI_REDUCE_PROD_TYPES)(ctx, array, length)
/*------------------------------------------------------------------------------
fmpi_reduce_prod()
------------------------------------------------------------------------------*/
/**
* TODO
*
* @param ctx : TODO
* @param array : TODO
*
* @example{
* TODO
* }
*/
#define fmpi_reduce_prod(ctx, array) \ #define fmpi_reduce_prod(ctx, array) \
FMPI_GENERIC_FUNC(array, reduce_prod, FMPI_REDUCE_PROD_TYPES)(ctx, array) FMPI_GENERIC_FUNC(array, reduce_prod, FMPI_REDUCE_PROD_TYPES)(ctx, array)
/*============================================================================== /*==============================================================================
......
...@@ -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.
* *
...@@ -24,27 +26,25 @@ ...@@ -24,27 +26,25 @@
/*============================================================================== /*==============================================================================
INCLUDE INCLUDE
==============================================================================*/ ==============================================================================*/
// C Standard Library
#include <stddef.h> // size_t
// Internal // Internal
#include "fmpi_ctx.h"
#include "fmpi_generic.h" #include "fmpi_generic.h"
/*============================================================================== /*==============================================================================
MACRO MACRO
==============================================================================*/ ==============================================================================*/
struct fmpi_ctx;
#define FMPI_REDUCE_PROD_TYPES \ #define FMPI_REDUCE_PROD_TYPES \
FMPI_TYPE_REAL FMPI_TYPE_REAL
#define FMPI_PROTO_LOCAL_REDUCE_PROD(T) \ #define FMPI_REDUCE_DECLARATION(T) \
T fmpi_local_reduce_prod_##T(struct fmpi_ctx * const ctx, T * const array, int length) T fmpi_local_reduce_prod_##T( \
const struct fmpi_ctx * ctx, const T * array, size_t length \
#define FMPI_PROTO_REDUCE_PROD(T) \ ); \
T fmpi_reduce_prod_##T(struct fmpi_ctx * const ctx, T * const array) T fmpi_reduce_prod_##T(const struct fmpi_ctx * ctx, const T * array)
FMPI_DECLARE_FUNC( FMPI_DECLARE_FUNCS(FMPI_REDUCE_DECLARATION, FMPI_REDUCE_PROD_TYPES);
FMPI_PROTO_LOCAL_REDUCE_PROD, FMPI_REDUCE_PROD_TYPES
);
FMPI_DECLARE_FUNC(
FMPI_PROTO_REDUCE_PROD, FMPI_REDUCE_PROD_TYPES
);
/*============================================================================== /*==============================================================================
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.
* *
...@@ -26,40 +28,39 @@ ...@@ -26,40 +28,39 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
// Internal // Internal
#include "fmpi_futhark.h" #include "internal/fmpi_ctx.h"
#include "fmpi_mpi.h" #include "internal/fmpi_futhark.h"
#include "internal/fmpi_fut.h" #include "internal/fmpi_futhark_entry.h"
#include "internal/fmpi_generic.h" #include "internal/fmpi_mpi.h"
#include "internal/generic/fmpi_generic.h"
/*============================================================================== /*==============================================================================
STRUCT STRUCT
==============================================================================*/ ==============================================================================*/
/*============================================================================== /*==============================================================================
PUBLIC FUNCTION PUBLIC FUNCTION
==============================================================================*/ ==============================================================================*/
#define FMPI_DEFINE_LOCAL_REDUCE_PROD(T) \ #define FMPI_REDUCE_DEFINITION(T) \
FMPI_PROTO_LOCAL_REDUCE_PROD(T) \ T fmpi_local_reduce_prod_##T( \
{ \ const struct fmpi_ctx * const ctx, const T * const array, const size_t length \
){ \
assert(ctx != NULL); \ assert(ctx != NULL); \
assert(array != NULL); \ assert(array != NULL); \
\ struct futhark_##T##_1d * data = fmpi_futhark_new_1d_##T(ctx, array, length);\
struct futhark_##T##_1d * xs = fmpi_new_##T##_1d(ctx, array, length);\ T result = FMPI_TYPE_DEFAULT_##T; \
T result = FMPI_DEFAULT_VALUE_##T; \ const int err = futhark_entry_reduce_prod_##T(ctx->fut->ctx, &result, data); \
futhark_entry_reduce_prod_##T(ctx->futhark, &result, xs); \ if(err != 0) { \
fmpi_futhark_abort_on_error(ctx, CPL_STRINGIFY(futhark_entry_reduce_prod_##T())); \ fmpi_futhark_check_error(ctx->fut, CPL_STRINGIFY(futhark_entry_reduce_prod_##T)); \
fmpi_futhark_sync(ctx); \ } \
fmpi_free_##T##_1d(ctx, xs); \ fmpi_futhark_sync(ctx->fut); \
fmpi_futhark_free_1d_##T(ctx, data); \
return result; \ return result; \
} }\
T fmpi_reduce_prod_##T(const struct fmpi_ctx * const ctx, const T * const array) \
#define FMPI_DEFINE_REDUCE_PROD(T) \
FMPI_PROTO_REDUCE_PROD(T) \
{ \ { \
assert(ctx != NULL); \ assert(ctx != NULL); \
assert(array != NULL); \ assert(array != NULL); \
\ T result = FMPI_TYPE_DEFAULT_##T; \
T result = FMPI_DEFAULT_VALUE_##T; \
/*! @cast MPI uses `int` for `size`. */ \ /*! @cast MPI uses `int` for `size`. */ \
/* Casting `int` to `size_t` should be safe if the value isn't negative. */ \ /* Casting `int` to `size_t` should be safe if the value isn't negative. */ \
/* This is asserted in`fmpi_init()`.*/ \ /* This is asserted in`fmpi_init()`.*/ \
...@@ -67,17 +68,15 @@ FMPI_PROTO_REDUCE_PROD(T) \ ...@@ -67,17 +68,15 @@ FMPI_PROTO_REDUCE_PROD(T) \
if(fact_global == NULL) { \ if(fact_global == NULL) { \
fprintf(stderr, "malloc(fact_global) failed!\n"); \ fprintf(stderr, "malloc(fact_global) failed!\n"); \
} \ } \
int err_id = MPI_Gather(array, 1, FMPI_MPI_DATATYPE_##T, fact_global, 1, FMPI_MPI_DATATYPE_##T, ctx->mpi->root, MPI_COMM_WORLD); \ int err_id = MPI_Gather(array, 1, FMPI_TYPE_MPI_##T, fact_global, 1, FMPI_TYPE_MPI_##T, ctx->mpi->root, MPI_COMM_WORLD); \
if(fmpi_mpi_has_error(ctx->mpi, err_id, "MPI_Gather()")) { \ if(fmpi_mpi_check_error(ctx->mpi, err_id, "MPI_Gather()") == true) { \
free(fact_global); \ free(fact_global); \
fmpi_abort(ctx); \
} \ } \
if(fmpi_mpi_is_root(ctx->mpi)) { \ if(fmpi_mpi_is_root(ctx->mpi)) { \
result = fmpi_local_reduce_prod_##T(ctx, fact_global, ctx->mpi->size); \ result = fmpi_local_reduce_prod_##T(ctx, fact_global, (size_t)ctx->mpi->size); \
} \ } \
free(fact_global); \ free(fact_global); \
return result; \ return result; \
} }
FMPI_DEFINE_FUNC(FMPI_DEFINE_LOCAL_REDUCE_PROD, FMPI_REDUCE_PROD_TYPES) FMPI_DEFINE_FUNCS(FMPI_REDUCE_DEFINITION, FMPI_REDUCE_PROD_TYPES)
FMPI_DEFINE_FUNC(FMPI_DEFINE_REDUCE_PROD, FMPI_REDUCE_PROD_TYPES)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment