Skip to content

Commit

Permalink
test: convert rest of the attr tests to functions
Browse files Browse the repository at this point in the history
Only test in attr folder that is not converted is attrend2 since that
test tests MPI_Finalize behaviors.
  • Loading branch information
hzhou committed Nov 18, 2024
1 parent b87edbc commit f5b87fa
Show file tree
Hide file tree
Showing 20 changed files with 253 additions and 263 deletions.
46 changes: 22 additions & 24 deletions test/mpi/attr/attr2type.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,34 @@
* See COPYRIGHT in top-level directory
*/

#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include "mpitest.h"

#ifdef MULTI_TESTS
#define run attr_attr2type
int run(const char *arg);
#endif

static int foo_keyval = MPI_KEYVAL_INVALID;

int foo_initialize(void);
void foo_finalize(void);
static int foo_initialize(void);
static void foo_finalize(void);

int foo_copy_attr_function(MPI_Datatype type, int type_keyval,
void *extra_state, void *attribute_val_in,
void *attribute_val_out, int *flag);
int foo_delete_attr_function(MPI_Datatype type, int type_keyval,
void *attribute_val, void *extra_state);
static int foo_copy_attr_function(MPI_Datatype type, int type_keyval,
void *extra_state, void *attribute_val_in,
void *attribute_val_out, int *flag);
static int foo_delete_attr_function(MPI_Datatype type, int type_keyval,
void *attribute_val, void *extra_state);
static const char *my_func = 0;
static int verbose = 0;
static int delete_called = 0;
static int copy_called = 0;

int main(int argc, char *argv[])
int run(const char *arg)
{
MPI_Datatype type, duptype;
int rank;
int errs = 0;

MTest_Init(&argc, &argv);

MPI_Comm_rank(MPI_COMM_WORLD, &rank);

foo_initialize();
Expand Down Expand Up @@ -61,15 +61,13 @@ int main(int argc, char *argv[])
fflush(stdout);
}

MTest_Finalize(errs);

return MTestReturnValue(errs);
return errs;
}

int foo_copy_attr_function(MPI_Datatype type,
int type_keyval,
void *extra_state,
void *attribute_val_in, void *attribute_val_out, int *flag)
static int foo_copy_attr_function(MPI_Datatype type,
int type_keyval,
void *extra_state,
void *attribute_val_in, void *attribute_val_out, int *flag)
{
if (verbose)
printf("copy fn. called\n");
Expand All @@ -80,8 +78,8 @@ int foo_copy_attr_function(MPI_Datatype type,
return MPI_SUCCESS;
}

int foo_delete_attr_function(MPI_Datatype type,
int type_keyval, void *attribute_val, void *extra_state)
static int foo_delete_attr_function(MPI_Datatype type,
int type_keyval, void *attribute_val, void *extra_state)
{
if (verbose)
printf("delete fn. called in %s\n", my_func);
Expand All @@ -90,7 +88,7 @@ int foo_delete_attr_function(MPI_Datatype type,
return MPI_SUCCESS;
}

int foo_initialize(void)
static int foo_initialize(void)
{
/* create keyval for use later */
MPI_Type_create_keyval(foo_copy_attr_function, foo_delete_attr_function, &foo_keyval, NULL);
Expand All @@ -100,7 +98,7 @@ int foo_initialize(void)
return 0;
}

void foo_finalize(void)
static void foo_finalize(void)
{
/* remove keyval */
MPI_Type_free_keyval(&foo_keyval);
Expand Down
24 changes: 13 additions & 11 deletions test/mpi/attr/attrdeleteget.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,33 @@
* See COPYRIGHT in top-level directory
*/

#include <stdio.h>
#include "mpi.h"
#include "mpitest.h"

int key = MPI_KEYVAL_INVALID;
char a[100];
#ifdef MULTI_TESTS
#define run attr_attrdeleteget
int run(const char *arg);
#endif

static int key = MPI_KEYVAL_INVALID;
static char a[100];

int delete_fn(MPI_Comm, int, void *, void *);
static int delete_fn(MPI_Comm, int, void *, void *);

int main(int argc, char **argv)
int run(const char *arg)
{
MPI_Comm scomm;
int errs = 0;

MTest_Init(&argc, &argv);
MPI_Comm scomm;
MPI_Comm_split(MPI_COMM_WORLD, 1, 0, &scomm);
MPI_Comm_create_keyval(MPI_NULL_COPY_FN, delete_fn, &key, &errs);
MPI_Comm_set_attr(scomm, key, a);
MPI_Comm_free(&scomm);
MPI_Comm_free_keyval(&key);
MTest_Finalize(errs);
return MTestReturnValue(errs);

return errs;
}

int delete_fn(MPI_Comm comm, int keyval, void *attr_val, void *extra_state)
static int delete_fn(MPI_Comm comm, int keyval, void *attr_val, void *extra_state)
{
/* The standard is not explicit that the 'comm' argument of
* delete_fn must be valid, so this test is only in effect when
Expand Down
56 changes: 25 additions & 31 deletions test/mpi/attr/attrend.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,64 +9,58 @@
MPI_Finalize. This is useful for tools that want to perform the MPI
equivalent of an "at_exit" action.
*/
#include <stdio.h>
#include "mpi.h"
/* NOTE: we modified the test to check the delete attributes behavior at
* MPI_Comm_free. The behavior at MPI_Finalize for builtin-comms are covered
* in attrend2.
*/
#include "mpitest.h"

int exit_key = MPI_KEYVAL_INVALID;
int wasCalled = 0;
int foundError = 0;
/* #define DEBUG */
int delete_fn(MPI_Comm, int, void *, void *);
#ifdef DEBUG
#define FFLUSH fflush(stdout);
#else
#define FFLUSH
#ifdef MULTI_TESTS
#define run attr_attrend
int run(const char *arg);
#endif

int main(int argc, char **argv)
{
int errs = 0, wrank;
static int wasCalled = 0;
static int foundError = 0;
static int delete_fn(MPI_Comm, int, void *, void *);

MPI_Init(&argc, &argv);
int run(const char *arg)
{
int errs = 0;
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);

MPI_Comm_rank(MPI_COMM_WORLD, &wrank);
MPI_Comm comm_self;
MPI_Comm_dup(MPI_COMM_SELF, &comm_self);

/* create the keyval for the exit handler */
int exit_key = MPI_KEYVAL_INVALID;
MPI_Keyval_create(MPI_NULL_COPY_FN, delete_fn, &exit_key, (void *) 0);

/* Attach to comm_self */
MPI_Attr_put(MPI_COMM_SELF, exit_key, (void *) 0);
MPI_Attr_put(comm_self, exit_key, (void *) 0);
/* We can free the key now */
MPI_Keyval_free(&exit_key);

/* Now, exit MPI */
MPI_Finalize();
MPI_Comm_free(&comm_self);

/* Check that the exit handler was called, and without error */
if (wrank == 0) {
if (rank == 0) {
/* In case more than one process exits MPI_Finalize */
if (wasCalled != 1) {
errs++;
printf("Attribute delete function on MPI_COMM_SELF was not called\n");
printf("Attribute delete function on comm_self was not called\n");
}
if (foundError != 0) {
errs++;
printf("Found %d errors while executing delete function in MPI_COMM_SELF\n",
foundError);
}
if (errs == 0) {
printf(" No Errors\n");
} else {
printf(" Found %d errors\n", errs);
printf("Found %d errors while executing delete function in comm_self\n", foundError);
}
fflush(stdout);
}

return MTestReturnValue(errs);
return errs;
}

int delete_fn(MPI_Comm comm, int keyval, void *attribute_val, void *extra_state)
static int delete_fn(MPI_Comm comm, int keyval, void *attribute_val, void *extra_state)
{
int flag;
wasCalled++;
Expand Down
33 changes: 16 additions & 17 deletions test/mpi/attr/attrerr.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,21 @@
failure.
*/
#include <stdio.h>
#include "mpi.h"
#include "mpitest.h"

int test_communicators(void);
void abort_msg(const char *, int);
int copybomb_fn(MPI_Comm, int, void *, void *, void *, int *);
int deletebomb_fn(MPI_Comm, int, void *, void *);
#ifdef MULTI_TESTS
#define run attr_attrerr
int run(const char *arg);
#endif

static int test_communicators(void);
static void abort_msg(const char *, int);
static int copybomb_fn(MPI_Comm, int, void *, void *, void *, int *);
static int deletebomb_fn(MPI_Comm, int, void *, void *);

int main(int argc, char **argv)
int run(const char *arg)
{
int errs;
MTest_Init(&argc, &argv);
errs = test_communicators();
MTest_Finalize(errs);
return MTestReturnValue(errs);
return test_communicators();
}

/*
Expand All @@ -37,8 +36,8 @@ int main(int argc, char **argv)
* Proposals to specify particular values (e.g., user's value) failed.
*/
/* Return an error as the value */
int copybomb_fn(MPI_Comm oldcomm, int keyval, void *extra_state,
void *attribute_val_in, void *attribute_val_out, int *flag)
static int copybomb_fn(MPI_Comm oldcomm, int keyval, void *extra_state,
void *attribute_val_in, void *attribute_val_out, int *flag)
{
/* Note that if (sizeof(int) < sizeof(void *), just setting the int
* part of attribute_val_out may leave some dirty bits
Expand All @@ -50,20 +49,20 @@ int copybomb_fn(MPI_Comm oldcomm, int keyval, void *extra_state,
/* Set delete flag to 1 to allow the attribute to be deleted */
static int delete_flag = 0;

int deletebomb_fn(MPI_Comm comm, int keyval, void *attribute_val, void *extra_state)
static int deletebomb_fn(MPI_Comm comm, int keyval, void *attribute_val, void *extra_state)
{
if (delete_flag)
return MPI_SUCCESS;
return MPI_ERR_OTHER;
}

void abort_msg(const char *str, int code)
static void abort_msg(const char *str, int code)
{
fprintf(stderr, "%s, err = %d\n", str, code);
MPI_Abort(MPI_COMM_WORLD, code);
}

int test_communicators(void)
static int test_communicators(void)
{
MPI_Comm dup_comm_world, d2;
int world_rank, world_size, key_1;
Expand Down
33 changes: 16 additions & 17 deletions test/mpi/attr/attrerrcomm.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,21 @@
failure.
*/
#include <stdio.h>
#include "mpi.h"
#include "mpitest.h"

int test_communicators(void);
void abort_msg(const char *, int);
int copybomb_fn(MPI_Comm, int, void *, void *, void *, int *);
int deletebomb_fn(MPI_Comm, int, void *, void *);
#ifdef MULTI_TESTS
#define run attr_attrerrcomm
int run(const char *arg);
#endif

static int test_communicators(void);
static void abort_msg(const char *, int);
static int copybomb_fn(MPI_Comm, int, void *, void *, void *, int *);
static int deletebomb_fn(MPI_Comm, int, void *, void *);

int main(int argc, char **argv)
int run(const char *arg)
{
int errs;
MTest_Init(&argc, &argv);
errs = test_communicators();
MTest_Finalize(errs);
return MTestReturnValue(errs);
return test_communicators();
}

/*
Expand All @@ -37,8 +36,8 @@ int main(int argc, char **argv)
* Proposals to specify particular values (e.g., user's value) failed.
*/
/* Return an error as the value */
int copybomb_fn(MPI_Comm oldcomm, int keyval, void *extra_state,
void *attribute_val_in, void *attribute_val_out, int *flag)
static int copybomb_fn(MPI_Comm oldcomm, int keyval, void *extra_state,
void *attribute_val_in, void *attribute_val_out, int *flag)
{
/* Note that if (sizeof(int) < sizeof(void *), just setting the int
* part of attribute_val_out may leave some dirty bits
Expand All @@ -50,20 +49,20 @@ int copybomb_fn(MPI_Comm oldcomm, int keyval, void *extra_state,
/* Set delete flag to 1 to allow the attribute to be deleted */
static int delete_flag = 0;

int deletebomb_fn(MPI_Comm comm, int keyval, void *attribute_val, void *extra_state)
static int deletebomb_fn(MPI_Comm comm, int keyval, void *attribute_val, void *extra_state)
{
if (delete_flag)
return MPI_SUCCESS;
return MPI_ERR_OTHER;
}

void abort_msg(const char *str, int code)
static void abort_msg(const char *str, int code)
{
fprintf(stderr, "%s, err = %d\n", str, code);
MPI_Abort(MPI_COMM_WORLD, code);
}

int test_communicators(void)
static int test_communicators(void)
{
MPI_Comm dup_comm_world, d2;
int world_rank, world_size, key_1;
Expand Down
Loading

0 comments on commit f5b87fa

Please sign in to comment.