From 88f070222be3e05012c8e8153690425807ae9e44 Mon Sep 17 00:00:00 2001 From: Nicolas Cornu Date: Thu, 28 Sep 2023 15:06:16 +0200 Subject: [PATCH 1/3] Unified all mpi assert mechanisms Use a new nrn_mpi_assert everywhere. --- src/nrnmpi/bbsmpipack.cpp | 57 ++++++++++++---------------- src/nrnmpi/mpispike.cpp | 58 +++++++++++++---------------- src/nrnmpi/nrnmpi.cpp | 78 +++++++++++++++++++-------------------- 3 files changed, 86 insertions(+), 107 deletions(-) diff --git a/src/nrnmpi/bbsmpipack.cpp b/src/nrnmpi/bbsmpipack.cpp index c939cb852d..ef7be8b113 100644 --- a/src/nrnmpi/bbsmpipack.cpp +++ b/src/nrnmpi/bbsmpipack.cpp @@ -22,18 +22,7 @@ #include #include -#if 0 -#define guard(f) nrn_assert(f == MPI_SUCCESS) -#else -#define guard(f) \ - { \ - int _i = f; \ - if (_i != MPI_SUCCESS) { \ - printf("%s %d\n", #f, _i); \ - assert(0); \ - } \ - } -#endif +#define nrn_mpi_assert(arg) nrn_assert(arg == MPI_SUCCESS) #define nrnmpidebugleak 0 #define debug 0 @@ -71,7 +60,7 @@ static void unpack(void* buf, int count, int my_datatype, bbsmpibuf* r, const ch r->size); #endif assert(r->upkpos >= 0 && r->size >= r->upkpos); - guard(MPI_Unpack(r->buf, r->size, &r->upkpos, type, 2, MPI_INT, nrn_bbs_comm)); + nrn_mpi_assert(MPI_Unpack(r->buf, r->size, &r->upkpos, type, 2, MPI_INT, nrn_bbs_comm)); #if debug printf("%d unpack r=%p size=%d upkpos=%d type[0]=%d datatype=%d type[1]=%d count=%d\n", nrnmpi_myid_bbs, @@ -95,7 +84,8 @@ static void unpack(void* buf, int count, int my_datatype, bbsmpibuf* r, const ch } assert(type[0] == my_datatype); assert(type[1] == count); - guard(MPI_Unpack(r->buf, r->size, &r->upkpos, buf, count, mytypes[my_datatype], nrn_bbs_comm)); + nrn_mpi_assert( + MPI_Unpack(r->buf, r->size, &r->upkpos, buf, count, mytypes[my_datatype], nrn_bbs_comm)); } void nrnmpi_upkbegin(bbsmpibuf* r) { @@ -113,12 +103,12 @@ void nrnmpi_upkbegin(bbsmpibuf* r) { hoc_execerror("subworld process with nhost > 0 cannot use", "the bulletin board"); } r->upkpos = 0; - guard(MPI_Unpack(r->buf, r->size, &r->upkpos, &p, 1, MPI_INT, nrn_bbs_comm)); + nrn_mpi_assert(MPI_Unpack(r->buf, r->size, &r->upkpos, &p, 1, MPI_INT, nrn_bbs_comm)); if (p > r->size) { printf("\n %d nrnmpi_upkbegin keypos=%d size=%d\n", nrnmpi_myid_bbs, p, r->size); } assert(p <= r->size); - guard(MPI_Unpack(r->buf, r->size, &p, &type, 1, MPI_INT, nrn_bbs_comm)); + nrn_mpi_assert(MPI_Unpack(r->buf, r->size, &p, &type, 1, MPI_INT, nrn_bbs_comm)); #if debug printf("%d nrnmpi_upkbegin type=%d keypos=%d\n", nrnmpi_myid_bbs, type, p); #endif @@ -232,7 +222,7 @@ void nrnmpi_pkbegin(bbsmpibuf* r) { printf( "%d nrnmpi_pkbegin %p size=%d pkposition=%d\n", nrnmpi_myid_bbs, r, r->size, r->pkposition); #endif - guard(MPI_Pack(&type, 1, MPI_INT, r->buf, r->size, &r->pkposition, nrn_bbs_comm)); + nrn_mpi_assert(MPI_Pack(&type, 1, MPI_INT, r->buf, r->size, &r->pkposition, nrn_bbs_comm)); } void nrnmpi_enddata(bbsmpibuf* r) { @@ -242,7 +232,7 @@ void nrnmpi_enddata(bbsmpibuf* r) { #if debug printf("%d nrnmpi_enddata %p size=%d pkposition=%d\n", nrnmpi_myid_bbs, r, r->size, p); #endif - guard(MPI_Pack_size(1, MPI_INT, nrn_bbs_comm, &isize)); + nrn_mpi_assert(MPI_Pack_size(1, MPI_INT, nrn_bbs_comm, &isize)); oldsize = r->size; resize(r, r->pkposition + isize); #if debug @@ -250,7 +240,7 @@ void nrnmpi_enddata(bbsmpibuf* r) { printf("%d %p need %d more. end up with total of %d\n", nrnmpi_myid_bbs, r, isize, r->size); } #endif - guard(MPI_Pack(&type, 1, MPI_INT, r->buf, r->size, &r->pkposition, nrn_bbs_comm)); + nrn_mpi_assert(MPI_Pack(&type, 1, MPI_INT, r->buf, r->size, &r->pkposition, nrn_bbs_comm)); #if debug printf("%d nrnmpi_enddata buf=%p size=%d pkposition=%d\n", nrnmpi_myid_bbs, @@ -258,7 +248,7 @@ void nrnmpi_enddata(bbsmpibuf* r) { r->size, r->pkposition); #endif - guard(MPI_Pack(&p, 1, MPI_INT, r->buf, r->size, &type, nrn_bbs_comm)); + nrn_mpi_assert(MPI_Pack(&p, 1, MPI_INT, r->buf, r->size, &type, nrn_bbs_comm)); #if debug printf("%d after nrnmpi_enddata, %d was packed at beginning and 0 was packed before %d\n", nrnmpi_myid_bbs, @@ -280,8 +270,8 @@ static void pack(void* inbuf, int incount, int my_datatype, bbsmpibuf* r, const r->pkposition, e); #endif - guard(MPI_Pack_size(incount, mytypes[my_datatype], nrn_bbs_comm, &dsize)); - guard(MPI_Pack_size(2, MPI_INT, nrn_bbs_comm, &isize)); + nrn_mpi_assert(MPI_Pack_size(incount, mytypes[my_datatype], nrn_bbs_comm, &dsize)); + nrn_mpi_assert(MPI_Pack_size(2, MPI_INT, nrn_bbs_comm, &isize)); oldsize = r->size; resize(r, r->pkposition + dsize + isize); #if debug @@ -295,8 +285,8 @@ static void pack(void* inbuf, int incount, int my_datatype, bbsmpibuf* r, const #endif type[0] = my_datatype; type[1] = incount; - guard(MPI_Pack(type, 2, MPI_INT, r->buf, r->size, &r->pkposition, nrn_bbs_comm)); - guard(MPI_Pack( + nrn_mpi_assert(MPI_Pack(type, 2, MPI_INT, r->buf, r->size, &r->pkposition, nrn_bbs_comm)); + nrn_mpi_assert(MPI_Pack( inbuf, incount, mytypes[my_datatype], r->buf, r->size, &r->pkposition, nrn_bbs_comm)); #if debug printf("%d pack done pkposition=%d\n", nrnmpi_myid_bbs, r->pkposition); @@ -359,9 +349,9 @@ void nrnmpi_bbssend(int dest, int tag, bbsmpibuf* r) { if (r) { assert(r->buf && r->keypos <= r->size); - guard(MPI_Send(r->buf, r->size, MPI_PACKED, dest, tag, nrn_bbs_comm)); + nrn_mpi_assert(MPI_Send(r->buf, r->size, MPI_PACKED, dest, tag, nrn_bbs_comm)); } else { - guard(MPI_Send(NULL, 0, MPI_PACKED, dest, tag, nrn_bbs_comm)); + nrn_mpi_assert(MPI_Send(NULL, 0, MPI_PACKED, dest, tag, nrn_bbs_comm)); } errno = 0; #if debug @@ -378,8 +368,8 @@ int nrnmpi_bbsrecv(int source, bbsmpibuf* r) { #if debug printf("%d nrnmpi_bbsrecv %p\n", nrnmpi_myid_bbs, r); #endif - guard(MPI_Probe(source, MPI_ANY_TAG, nrn_bbs_comm, &status)); - guard(MPI_Get_count(&status, MPI_PACKED, &size)); + nrn_mpi_assert(MPI_Probe(source, MPI_ANY_TAG, nrn_bbs_comm, &status)); + nrn_mpi_assert(MPI_Get_count(&status, MPI_PACKED, &size)); #if debug printf("%d nrnmpi_bbsrecv probe size=%d source=%d tag=%d\n", nrnmpi_myid_bbs, @@ -388,7 +378,8 @@ int nrnmpi_bbsrecv(int source, bbsmpibuf* r) { status.MPI_TAG); #endif resize(r, size); - guard(MPI_Recv(r->buf, r->size, MPI_PACKED, source, MPI_ANY_TAG, nrn_bbs_comm, &status)); + nrn_mpi_assert( + MPI_Recv(r->buf, r->size, MPI_PACKED, source, MPI_ANY_TAG, nrn_bbs_comm, &status)); errno = 0; /* Some MPI implementations limit tags to be less than full MPI_INT domain In the past we allowed TODO mesages to have tags > 20 (FIRSTID of src/parallel/bbssrv.h) @@ -427,14 +418,14 @@ int nrnmpi_bbssendrecv(int dest, int tag, bbsmpibuf* s, bbsmpibuf* r) { int nrnmpi_iprobe(int* size, int* tag, int* source) { int flag = 0; MPI_Status status; - guard(MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, nrn_bbs_comm, &flag, &status)); + nrn_mpi_assert(MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, nrn_bbs_comm, &flag, &status)); if (flag) { if (source) *source = status.MPI_SOURCE; if (tag) *tag = status.MPI_TAG; if (size) - guard(MPI_Get_count(&status, MPI_PACKED, size)); + nrn_mpi_assert(MPI_Get_count(&status, MPI_PACKED, size)); } return flag; } @@ -442,13 +433,13 @@ int nrnmpi_iprobe(int* size, int* tag, int* source) { void nrnmpi_probe(int* size, int* tag, int* source) { int flag = 0; MPI_Status status; - guard(MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, nrn_bbs_comm, &status)); + nrn_mpi_assert(MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, nrn_bbs_comm, &status)); if (source) *source = status.MPI_SOURCE; if (tag) *tag = status.MPI_TAG; if (size) - guard(MPI_Get_count(&status, MPI_PACKED, size)); + nrn_mpi_assert(MPI_Get_count(&status, MPI_PACKED, size)); } bbsmpibuf* nrnmpi_newbuf(int size) { diff --git a/src/nrnmpi/mpispike.cpp b/src/nrnmpi/mpispike.cpp index 317f27f7b9..53a86e6549 100644 --- a/src/nrnmpi/mpispike.cpp +++ b/src/nrnmpi/mpispike.cpp @@ -23,6 +23,8 @@ #include #include +#define nrn_mpi_assert(arg) nrn_assert(arg == MPI_SUCCESS) + extern void nrnbbs_context_wait(); static int np; @@ -286,13 +288,10 @@ static int MPI_Alltoallv_sparse(void* sendbuf, int* rdispls, MPI_Datatype recvtype, MPI_Comm comm) { - int status; int myrank; int nranks; - status = MPI_Comm_rank(comm, &myrank); - assert(status == MPI_SUCCESS); - status = MPI_Comm_size(comm, &nranks); - assert(status == MPI_SUCCESS); + nrn_mpi_assert(MPI_Comm_rank(comm, &myrank)); + nrn_mpi_assert(MPI_Comm_size(comm, &nranks)); int rankp; for (rankp = 0; nranks > (1 << rankp); rankp++) @@ -302,10 +301,8 @@ static int MPI_Alltoallv_sparse(void* sendbuf, ptrdiff_t send_elsize; ptrdiff_t recv_elsize; - status = MPI_Type_get_extent(sendtype, &lb, &send_elsize); - assert(status == MPI_SUCCESS); - status = MPI_Type_get_extent(recvtype, &lb, &recv_elsize); - assert(status == MPI_SUCCESS); + nrn_mpi_assert(MPI_Type_get_extent(sendtype, &lb, &send_elsize)); + nrn_mpi_assert(MPI_Type_get_extent(recvtype, &lb, &recv_elsize)); MPI_Request* requests = (MPI_Request*) hoc_Emalloc(nranks * 2 * sizeof(MPI_Request)); hoc_malchk(); @@ -321,18 +318,16 @@ static int MPI_Alltoallv_sparse(void* sendbuf, continue; if (recvcnts[target] == 0) continue; - status = MPI_Irecv((static_cast(recvbuf)) + recv_elsize * rdispls[target], - recvcnts[target], - recvtype, - target, - ALLTOALLV_SPARSE_TAG, - comm, - &requests[n_requests++]); - assert(status == MPI_SUCCESS); + nrn_mpi_assert(MPI_Irecv((static_cast(recvbuf)) + recv_elsize * rdispls[target], + recvcnts[target], + recvtype, + target, + ALLTOALLV_SPARSE_TAG, + comm, + &requests[n_requests++])); } - status = MPI_Barrier(comm); - assert(status == MPI_SUCCESS); + nrn_mpi_assert(MPI_Barrier(comm)); for (ngrp = 0; ngrp < (1 << rankp); ngrp++) { int target = myrank ^ ngrp; @@ -340,22 +335,19 @@ static int MPI_Alltoallv_sparse(void* sendbuf, continue; if (sendcnts[target] == 0) continue; - status = MPI_Isend((static_cast(sendbuf)) + send_elsize * sdispls[target], - sendcnts[target], - sendtype, - target, - ALLTOALLV_SPARSE_TAG, - comm, - &requests[n_requests++]); - assert(status == MPI_SUCCESS); - } - - status = MPI_Waitall(n_requests, requests, MPI_STATUSES_IGNORE); - assert(status == MPI_SUCCESS); + nrn_mpi_assert(MPI_Isend((static_cast(sendbuf)) + send_elsize * sdispls[target], + sendcnts[target], + sendtype, + target, + ALLTOALLV_SPARSE_TAG, + comm, + &requests[n_requests++])); + } + + nrn_mpi_assert(MPI_Waitall(n_requests, requests, MPI_STATUSES_IGNORE)); free(requests); - status = MPI_Barrier(comm); - assert(status == MPI_SUCCESS); + nrn_mpi_assert(MPI_Barrier(comm)); return MPI_SUCCESS; } diff --git a/src/nrnmpi/nrnmpi.cpp b/src/nrnmpi/nrnmpi.cpp index bea98bb84d..386e96ac89 100644 --- a/src/nrnmpi/nrnmpi.cpp +++ b/src/nrnmpi/nrnmpi.cpp @@ -24,11 +24,7 @@ extern double nrn_timeus(); #if NRNMPI #include -#define asrt(arg) nrn_assert(arg == MPI_SUCCESS) -#define USE_HPM 0 -#if USE_HPM -#include -#endif +#define nrn_mpi_assert(arg) nrn_assert(arg == MPI_SUCCESS) #if NRN_MUSIC #include "nrnmusicapi.h" @@ -118,12 +114,12 @@ for (i=0; i < *pargc; ++i) { #if (NRN_ENABLE_THREADS) int required = MPI_THREAD_SERIALIZED; int provided; - asrt(MPI_Init_thread(pargc, pargv, required, &provided)); + nrn_mpi_assert(MPI_Init_thread(pargc, pargv, required, &provided)); if (required > provided) { nrn_cannot_use_threads_and_mpi = 1; } #else - asrt(MPI_Init(pargc, pargv)); + nrn_mpi_assert(MPI_Init(pargc, pargv)); #endif nrnmpi_under_nrncontrol_ = 1; #if NRN_MUSIC @@ -134,20 +130,20 @@ for (i=0; i < *pargc; ++i) { #if NRN_MUSIC if (nrnmusic) { - asrt(MPI_Comm_dup(nrnmusic_comm, &nrnmpi_world_comm)); + nrn_mpi_assert(MPI_Comm_dup(nrnmusic_comm, &nrnmpi_world_comm)); } else { #else { #endif - asrt(MPI_Comm_dup(MPI_COMM_WORLD, &nrnmpi_world_comm)); + nrn_mpi_assert(MPI_Comm_dup(MPI_COMM_WORLD, &nrnmpi_world_comm)); } } grp_bbs = MPI_GROUP_NULL; grp_net = MPI_GROUP_NULL; - asrt(MPI_Comm_dup(nrnmpi_world_comm, &nrnmpi_comm)); - asrt(MPI_Comm_dup(nrnmpi_world_comm, &nrn_bbs_comm)); - asrt(MPI_Comm_rank(nrnmpi_world_comm, &nrnmpi_myid_world)); - asrt(MPI_Comm_size(nrnmpi_world_comm, &nrnmpi_numprocs_world)); + nrn_mpi_assert(MPI_Comm_dup(nrnmpi_world_comm, &nrnmpi_comm)); + nrn_mpi_assert(MPI_Comm_dup(nrnmpi_world_comm, &nrn_bbs_comm)); + nrn_mpi_assert(MPI_Comm_rank(nrnmpi_world_comm, &nrnmpi_myid_world)); + nrn_mpi_assert(MPI_Comm_size(nrnmpi_world_comm, &nrnmpi_numprocs_world)); nrnmpi_numprocs = nrnmpi_numprocs_bbs = nrnmpi_numprocs_world; nrnmpi_myid = nrnmpi_myid_bbs = nrnmpi_myid_world; nrnmpi_spike_initialize(); @@ -239,44 +235,44 @@ void nrnmpi_subworld_size(int n) { return; } if (nrnmpi_comm != MPI_COMM_NULL) { - asrt(MPI_Comm_free(&nrnmpi_comm)); + nrn_mpi_assert(MPI_Comm_free(&nrnmpi_comm)); nrnmpi_comm = MPI_COMM_NULL; } if (nrn_bbs_comm != MPI_COMM_NULL) { - asrt(MPI_Comm_free(&nrn_bbs_comm)); + nrn_mpi_assert(MPI_Comm_free(&nrn_bbs_comm)); nrn_bbs_comm = MPI_COMM_NULL; } if (grp_bbs != MPI_GROUP_NULL) { - asrt(MPI_Group_free(&grp_bbs)); + nrn_mpi_assert(MPI_Group_free(&grp_bbs)); grp_bbs = MPI_GROUP_NULL; } if (grp_net != MPI_GROUP_NULL) { - asrt(MPI_Group_free(&grp_net)); + nrn_mpi_assert(MPI_Group_free(&grp_net)); grp_net = MPI_GROUP_NULL; } MPI_Group wg; - asrt(MPI_Comm_group(nrnmpi_world_comm, &wg)); + nrn_mpi_assert(MPI_Comm_group(nrnmpi_world_comm, &wg)); int r = nrnmpi_myid_world; /* special cases */ if (n == 1) { - asrt(MPI_Group_incl(wg, 1, &r, &grp_net)); - asrt(MPI_Comm_dup(nrnmpi_world_comm, &nrn_bbs_comm)); - asrt(MPI_Comm_create(nrnmpi_world_comm, grp_net, &nrnmpi_comm)); - asrt(MPI_Comm_rank(nrnmpi_comm, &nrnmpi_myid)); - asrt(MPI_Comm_size(nrnmpi_comm, &nrnmpi_numprocs)); - asrt(MPI_Comm_rank(nrn_bbs_comm, &nrnmpi_myid_bbs)); - asrt(MPI_Comm_size(nrn_bbs_comm, &nrnmpi_numprocs_bbs)); + nrn_mpi_assert(MPI_Group_incl(wg, 1, &r, &grp_net)); + nrn_mpi_assert(MPI_Comm_dup(nrnmpi_world_comm, &nrn_bbs_comm)); + nrn_mpi_assert(MPI_Comm_create(nrnmpi_world_comm, grp_net, &nrnmpi_comm)); + nrn_mpi_assert(MPI_Comm_rank(nrnmpi_comm, &nrnmpi_myid)); + nrn_mpi_assert(MPI_Comm_size(nrnmpi_comm, &nrnmpi_numprocs)); + nrn_mpi_assert(MPI_Comm_rank(nrn_bbs_comm, &nrnmpi_myid_bbs)); + nrn_mpi_assert(MPI_Comm_size(nrn_bbs_comm, &nrnmpi_numprocs_bbs)); nrnmpi_subworld_id = nrnmpi_myid_bbs; nrnmpi_numprocs_subworld = nrnmpi_numprocs_bbs; } else if (n == nrnmpi_numprocs_world) { - asrt(MPI_Group_incl(wg, 1, &r, &grp_bbs)); - asrt(MPI_Comm_dup(nrnmpi_world_comm, &nrnmpi_comm)); - asrt(MPI_Comm_create(nrnmpi_world_comm, grp_bbs, &nrn_bbs_comm)); - asrt(MPI_Comm_rank(nrnmpi_comm, &nrnmpi_myid)); - asrt(MPI_Comm_size(nrnmpi_comm, &nrnmpi_numprocs)); + nrn_mpi_assert(MPI_Group_incl(wg, 1, &r, &grp_bbs)); + nrn_mpi_assert(MPI_Comm_dup(nrnmpi_world_comm, &nrnmpi_comm)); + nrn_mpi_assert(MPI_Comm_create(nrnmpi_world_comm, grp_bbs, &nrn_bbs_comm)); + nrn_mpi_assert(MPI_Comm_rank(nrnmpi_comm, &nrnmpi_myid)); + nrn_mpi_assert(MPI_Comm_size(nrnmpi_comm, &nrnmpi_numprocs)); if (r == 0) { - asrt(MPI_Comm_rank(nrn_bbs_comm, &nrnmpi_myid_bbs)); - asrt(MPI_Comm_size(nrn_bbs_comm, &nrnmpi_numprocs_bbs)); + nrn_mpi_assert(MPI_Comm_rank(nrn_bbs_comm, &nrnmpi_myid_bbs)); + nrn_mpi_assert(MPI_Comm_size(nrn_bbs_comm, &nrnmpi_numprocs_bbs)); } else { nrnmpi_myid_bbs = -1; nrnmpi_numprocs_bbs = -1; @@ -300,21 +296,21 @@ void nrnmpi_subworld_size(int n) { range[1] = nw - 1; } range[2] = 1; /* stride */ - asrt(MPI_Group_range_incl(wg, 1, &range, &grp_net)); - asrt(MPI_Comm_create(nrnmpi_world_comm, grp_net, &nrnmpi_comm)); - asrt(MPI_Comm_rank(nrnmpi_comm, &nrnmpi_myid)); - asrt(MPI_Comm_size(nrnmpi_comm, &nrnmpi_numprocs)); + nrn_mpi_assert(MPI_Group_range_incl(wg, 1, &range, &grp_net)); + nrn_mpi_assert(MPI_Comm_create(nrnmpi_world_comm, grp_net, &nrnmpi_comm)); + nrn_mpi_assert(MPI_Comm_rank(nrnmpi_comm, &nrnmpi_myid)); + nrn_mpi_assert(MPI_Comm_size(nrnmpi_comm, &nrnmpi_numprocs)); /* nrn_bbs_com ranks stride is nrnmpi_numprocs */ /* only rank 0 of each subworld participates in nrn_bbs_comm */ range[0] = 0; /* first world rank in nrn_bbs_comm */ range[1] = (nb - 1) * n; /* last world rank in nrn_bbs_comm */ range[2] = n; /* stride */ - asrt(MPI_Group_range_incl(wg, 1, &range, &grp_bbs)); - asrt(MPI_Comm_create(nrnmpi_world_comm, grp_bbs, &nrn_bbs_comm)); + nrn_mpi_assert(MPI_Group_range_incl(wg, 1, &range, &grp_bbs)); + nrn_mpi_assert(MPI_Comm_create(nrnmpi_world_comm, grp_bbs, &nrn_bbs_comm)); if (r % n == 0) { /* only rank 0 participates in nrn_bbs_comm */ - asrt(MPI_Comm_rank(nrn_bbs_comm, &nrnmpi_myid_bbs)); - asrt(MPI_Comm_size(nrn_bbs_comm, &nrnmpi_numprocs_bbs)); + nrn_mpi_assert(MPI_Comm_rank(nrn_bbs_comm, &nrnmpi_myid_bbs)); + nrn_mpi_assert(MPI_Comm_size(nrn_bbs_comm, &nrnmpi_numprocs_bbs)); } else { nrnmpi_myid_bbs = -1; nrnmpi_numprocs_bbs = -1; @@ -326,7 +322,7 @@ void nrnmpi_subworld_size(int n) { } } nrnmpi_subworld_change_cnt++; - asrt(MPI_Group_free(&wg)); + nrn_mpi_assert(MPI_Group_free(&wg)); } /* so src/nrnpython/inithoc.cpp does not have to include a c++ mpi.h */ From 7ad9347a46f7174968de6471f5c530ce6374276c Mon Sep 17 00:00:00 2001 From: Nicolas Cornu Date: Fri, 29 Sep 2023 13:27:14 +0200 Subject: [PATCH 2/3] Create an helper function to get the MPI_Op from type (#2560) Internally we have some constants that mean: 1 - MPI_SUM 2 - MPI_MAX 3 - MPI_MIN --- src/coreneuron/mpi/lib/mpispike.cpp | 37 ++++++------------ src/nrnmpi/mpispike.cpp | 58 +++++++++-------------------- 2 files changed, 28 insertions(+), 67 deletions(-) diff --git a/src/coreneuron/mpi/lib/mpispike.cpp b/src/coreneuron/mpi/lib/mpispike.cpp index bbe81ac6c2..2e62ab578a 100644 --- a/src/coreneuron/mpi/lib/mpispike.cpp +++ b/src/coreneuron/mpi/lib/mpispike.cpp @@ -289,46 +289,31 @@ void nrnmpi_barrier_impl() { MPI_Barrier(nrnmpi_comm); } -double nrnmpi_dbl_allreduce_impl(double x, int type) { - double result; +static MPI_Op type2OP(int type) { MPI_Op tt; if (type == 1) { - tt = MPI_SUM; + return MPI_SUM; } else if (type == 2) { - tt = MPI_MAX; + return MPI_MAX; } else { - tt = MPI_MIN; + return MPI_MIN; } - MPI_Allreduce(&x, &result, 1, MPI_DOUBLE, tt, nrnmpi_comm); +} + +double nrnmpi_dbl_allreduce_impl(double x, int type) { + double result; + MPI_Allreduce(&x, &result, 1, MPI_DOUBLE, type2OP(type), nrnmpi_comm); return result; } void nrnmpi_dbl_allreduce_vec_impl(double* src, double* dest, int cnt, int type) { - MPI_Op tt; assert(src != dest); - if (type == 1) { - tt = MPI_SUM; - } else if (type == 2) { - tt = MPI_MAX; - } else { - tt = MPI_MIN; - } - MPI_Allreduce(src, dest, cnt, MPI_DOUBLE, tt, nrnmpi_comm); - return; + MPI_Allreduce(src, dest, cnt, MPI_DOUBLE, type2OP(type), nrnmpi_comm); } void nrnmpi_long_allreduce_vec_impl(long* src, long* dest, int cnt, int type) { - MPI_Op tt; assert(src != dest); - if (type == 1) { - tt = MPI_SUM; - } else if (type == 2) { - tt = MPI_MAX; - } else { - tt = MPI_MIN; - } - MPI_Allreduce(src, dest, cnt, MPI_LONG, tt, nrnmpi_comm); - return; + MPI_Allreduce(src, dest, cnt, MPI_LONG, type2OP(type), nrnmpi_comm); } #if NRN_MULTISEND diff --git a/src/nrnmpi/mpispike.cpp b/src/nrnmpi/mpispike.cpp index 53a86e6549..c84e2d0ae1 100644 --- a/src/nrnmpi/mpispike.cpp +++ b/src/nrnmpi/mpispike.cpp @@ -617,25 +617,26 @@ void nrnmpi_barrier() { MPI_Barrier(nrnmpi_comm); } -double nrnmpi_dbl_allreduce(double x, int type) { - double result; - MPI_Op t; - if (nrnmpi_numprocs < 2) { - return x; - } +static MPI_Op type2OP(int type) { if (type == 1) { - t = MPI_SUM; + return MPI_SUM; } else if (type == 2) { - t = MPI_MAX; + return MPI_MAX; } else { - t = MPI_MIN; + return MPI_MIN; + } +} + +double nrnmpi_dbl_allreduce(double x, int type) { + if (nrnmpi_numprocs < 2) { + return x; } - MPI_Allreduce(&x, &result, 1, MPI_DOUBLE, t, nrnmpi_comm); + double result; + MPI_Allreduce(&x, &result, 1, MPI_DOUBLE, type2OP(type), nrnmpi_comm); return result; } extern "C" void nrnmpi_dbl_allreduce_vec(double* src, double* dest, int cnt, int type) { - MPI_Op t; assert(src != dest); if (nrnmpi_numprocs < 2) { for (int i = 0; i < cnt; ++i) { @@ -643,56 +644,31 @@ extern "C" void nrnmpi_dbl_allreduce_vec(double* src, double* dest, int cnt, int } return; } - if (type == 1) { - t = MPI_SUM; - } else if (type == 2) { - t = MPI_MAX; - } else { - t = MPI_MIN; - } - MPI_Allreduce(src, dest, cnt, MPI_DOUBLE, t, nrnmpi_comm); + MPI_Allreduce(src, dest, cnt, MPI_DOUBLE, type2OP(type), nrnmpi_comm); return; } void nrnmpi_longdbl_allreduce_vec(longdbl* src, longdbl* dest, int cnt, int type) { - int i; - MPI_Op t; assert(src != dest); if (nrnmpi_numprocs < 2) { - for (i = 0; i < cnt; ++i) { + for (int i = 0; i < cnt; ++i) { dest[i] = src[i]; } return; } - if (type == 1) { - t = MPI_SUM; - } else if (type == 2) { - t = MPI_MAX; - } else { - t = MPI_MIN; - } - MPI_Allreduce(src, dest, cnt, MPI_LONG_DOUBLE, t, nrnmpi_comm); + MPI_Allreduce(src, dest, cnt, MPI_LONG_DOUBLE, type2OP(type), nrnmpi_comm); return; } void nrnmpi_long_allreduce_vec(long* src, long* dest, int cnt, int type) { - int i; - MPI_Op t; assert(src != dest); if (nrnmpi_numprocs < 2) { - for (i = 0; i < cnt; ++i) { + for (int i = 0; i < cnt; ++i) { dest[i] = src[i]; } return; } - if (type == 1) { - t = MPI_SUM; - } else if (type == 2) { - t = MPI_MAX; - } else { - t = MPI_MIN; - } - MPI_Allreduce(src, dest, cnt, MPI_LONG, t, nrnmpi_comm); + MPI_Allreduce(src, dest, cnt, MPI_LONG, type2OP(type), nrnmpi_comm); return; } From ae02186e786c45da925e01100d5fdc08ff91f86b Mon Sep 17 00:00:00 2001 From: Nicolas Cornu Date: Wed, 4 Oct 2023 08:37:16 +0200 Subject: [PATCH 3/3] Reduce sonarcloud warnings --- src/coreneuron/mpi/lib/mpispike.cpp | 1 - src/nrnmpi/bbsmpipack.cpp | 2 +- src/oc/nrnassrt.h | 33 +++++------------------------ 3 files changed, 6 insertions(+), 30 deletions(-) diff --git a/src/coreneuron/mpi/lib/mpispike.cpp b/src/coreneuron/mpi/lib/mpispike.cpp index 2e62ab578a..b7c1bcf1bf 100644 --- a/src/coreneuron/mpi/lib/mpispike.cpp +++ b/src/coreneuron/mpi/lib/mpispike.cpp @@ -290,7 +290,6 @@ void nrnmpi_barrier_impl() { } static MPI_Op type2OP(int type) { - MPI_Op tt; if (type == 1) { return MPI_SUM; } else if (type == 2) { diff --git a/src/nrnmpi/bbsmpipack.cpp b/src/nrnmpi/bbsmpipack.cpp index 1ef709a904..0112a9530c 100644 --- a/src/nrnmpi/bbsmpipack.cpp +++ b/src/nrnmpi/bbsmpipack.cpp @@ -349,7 +349,7 @@ void nrnmpi_bbssend(int dest, int tag, bbsmpibuf* r) { assert(r->buf && r->keypos <= r->size); nrn_mpi_assert(MPI_Send(r->buf, r->size, MPI_PACKED, dest, tag, nrn_bbs_comm)); } else { - nrn_mpi_assert(MPI_Send(NULL, 0, MPI_PACKED, dest, tag, nrn_bbs_comm)); + nrn_mpi_assert(MPI_Send(nullptr, 0, MPI_PACKED, dest, tag, nrn_bbs_comm)); } errno = 0; #if debug diff --git a/src/oc/nrnassrt.h b/src/oc/nrnassrt.h index d7b5a9338d..abd026ae66 100644 --- a/src/oc/nrnassrt.h +++ b/src/oc/nrnassrt.h @@ -12,44 +12,21 @@ has side effects which need to be executed regardles of NDEBUG. #if defined(hocassrt_h) /* hoc_execerror form */ #include "oc_ansi.h" -#if defined(__STDC__) #define nrn_assert(ex) \ - { \ + do { \ if (!(ex)) { \ fprintf(stderr, "Assertion failed: file %s, line %d\n", __FILE__, __LINE__); \ - hoc_execerror(#ex, (char*) 0); \ + hoc_execerror(#ex, nullptr); \ } \ - } -#else -#define nrn_assert(ex) \ - { \ - if (!(ex)) { \ - fprintf(stderr, "Assertion failed: file %s, line %d\n", __FILE__, __LINE__); \ - hoc_execerror("ex", (char*) 0); \ - } \ - } -#endif - + } while (0) #else /* abort form */ - -#if defined(__STDC__) #define nrn_assert(ex) \ - { \ + do { \ if (!(ex)) { \ fprintf(stderr, "Assertion failed: file %s, line %d\n", __FILE__, __LINE__); \ abort(); \ } \ - } -#else -#define nrn_assert(ex) \ - { \ - if (!(ex)) { \ - fprintf(stderr, "Assertion failed: file %s, line %d\n", __FILE__, __LINE__); \ - abort(); \ - } \ - } -#endif - + } while (0) #endif