Skip to content

Commit

Permalink
mpi: retain operation and datatype in non blocking collectives
Browse files Browse the repository at this point in the history
MPI standard states a user MPI_Op and/or user MPI_Datatype can be free'd
after a call to a non blocking collective and before the non-blocking
collective completes.
Retain user (only) MPI_Op and MPI_Datatype when the non blocking call is
invoked, and set a request callback so they are free'd when the MPI_Request
completes.

Thanks Thomas Ponweiser for reporting this

Fixes #2151
Fixes #1304

Signed-off-by: Gilles Gouaillardet <gilles@rist.or.jp>
  • Loading branch information
ggouaillardet committed Apr 9, 2019
1 parent fd686d7 commit 646391b
Show file tree
Hide file tree
Showing 23 changed files with 306 additions and 57 deletions.
164 changes: 162 additions & 2 deletions ompi/mca/coll/base/coll_base_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
* University of Stuttgart. All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* Copyright (c) 2014-2017 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* Copyright (c) 2014-2019 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
Expand All @@ -29,6 +29,27 @@
#include "ompi/mca/pml/pml.h"
#include "coll_base_util.h"

struct retain_op_data {
ompi_request_complete_fn_t req_complete_cb;
void *req_complete_cb_data;
ompi_op_t *op;
ompi_datatype_t *datatype;
};

struct retain_datatypes_data {
ompi_request_complete_fn_t req_complete_cb;
void *req_complete_cb_data;
ompi_datatype_t *stype;
ompi_datatype_t *rtype;
};

struct retain_datatypes_w_data {
ompi_request_complete_fn_t req_complete_cb;
void *req_complete_cb_data;
int count;
ompi_datatype_t *types[];
};

int ompi_coll_base_sendrecv_actual( const void* sendbuf, size_t scount,
ompi_datatype_t* sdatatype,
int dest, int stag,
Expand Down Expand Up @@ -103,3 +124,142 @@ int ompi_rounddown(int num, int factor)
num /= factor;
return num * factor; /* floor(num / factor) * factor */
}

static int release_op_callback(struct ompi_request_t *request) {
struct retain_op_data * p = (struct retain_op_data *)request->req_complete_cb_data;
int rc = OMPI_SUCCESS;
assert (NULL != p);
if (NULL != p->req_complete_cb) {
request->req_complete_cb = p->req_complete_cb;
request->req_complete_cb_data = p->req_complete_cb_data;
rc = request->req_complete_cb(request);
}
if (NULL != p->op) {
OBJ_RELEASE(p->op);
}
if (NULL != p->datatype) {
OBJ_RELEASE(p->datatype);
}
free(p);
return rc;
}

int ompi_coll_base_retain_op( ompi_request_t *request, ompi_op_t *op,
ompi_datatype_t *type) {
bool retain = !ompi_op_is_intrinsic(op);
retain |= !ompi_datatype_is_predefined(type);
if (OPAL_UNLIKELY(retain)) {
struct retain_op_data *p = (struct retain_op_data *)calloc(1, sizeof(struct retain_op_data));
if (OPAL_UNLIKELY(NULL == p)) {
return OMPI_ERR_OUT_OF_RESOURCE;
}
if (!ompi_op_is_intrinsic(op)) {
OBJ_RETAIN(op);
p->op = op;
}
if (!ompi_datatype_is_predefined(type)) {
OBJ_RETAIN(type);
p->datatype = type;
}
p->req_complete_cb = request->req_complete_cb;
p->req_complete_cb_data = request->req_complete_cb_data;
request->req_complete_cb = release_op_callback;
request->req_complete_cb_data = p;
}
return OMPI_SUCCESS;
}

static int release_datatypes_callback(struct ompi_request_t *request) {
struct retain_datatypes_data * p = (struct retain_datatypes_data *)request->req_complete_cb_data;
int rc = OMPI_SUCCESS;
assert (NULL != p);
if (NULL != p->req_complete_cb) {
request->req_complete_cb = p->req_complete_cb;
request->req_complete_cb_data = p->req_complete_cb_data;
rc = request->req_complete_cb(request);
}
if (NULL != p->stype) {
OBJ_RELEASE(p->stype);
}
if (NULL != p->rtype) {
OBJ_RELEASE(p->rtype);
}
free(p);
return rc;
}

int ompi_coll_base_retain_datatypes( ompi_request_t *request, ompi_datatype_t *stype,
ompi_datatype_t *rtype) {
bool retain = NULL != stype && !ompi_datatype_is_predefined(stype);
retain |= NULL != rtype && !ompi_datatype_is_predefined(rtype);
if (OPAL_UNLIKELY(retain)) {
struct retain_datatypes_data *p = (struct retain_datatypes_data *)calloc(1, sizeof(struct retain_datatypes_data));
if (OPAL_UNLIKELY(NULL == p)) {
return OMPI_ERR_OUT_OF_RESOURCE;
}
if (NULL != stype && !ompi_datatype_is_predefined(stype)) {
OBJ_RETAIN(stype);
p->stype = stype;
}
if (NULL != rtype && !ompi_datatype_is_predefined(rtype)) {
OBJ_RETAIN(rtype);
p->rtype = rtype;
}
p->req_complete_cb = request->req_complete_cb;
p->req_complete_cb_data = request->req_complete_cb_data;
request->req_complete_cb = release_datatypes_callback;
request->req_complete_cb_data = p;
}
return OMPI_SUCCESS;
}

static int release_datatypes_w_callback(struct ompi_request_t *request) {
struct retain_datatypes_w_data * p = (struct retain_datatypes_w_data *)request->req_complete_cb_data;
int rc = OMPI_SUCCESS;
assert (NULL != p);
if (NULL != p->req_complete_cb) {
request->req_complete_cb = p->req_complete_cb;
request->req_complete_cb_data = p->req_complete_cb_data;
rc = request->req_complete_cb(request);
}
for (int i=0; i<p->count; i++) {
OBJ_RELEASE(p->types[i]);
}
free(p);
return rc;
}

int ompi_coll_base_retain_datatypes_w( ompi_request_t *request, int count,
ompi_datatype_t *const stypes[], ompi_datatype_t *const rtypes[]) {
int datatypes = 0;
for (int i=0; i<count; i++) {
if (NULL != stypes[i] && !ompi_datatype_is_predefined(stypes[i])) {
datatypes++;
}
if (NULL != rtypes[i] && !ompi_datatype_is_predefined(rtypes[i])) {
datatypes++;
}
}
if (OPAL_UNLIKELY(0 < datatypes)) {
struct retain_datatypes_w_data *p = (struct retain_datatypes_w_data *)calloc(1, sizeof(struct retain_datatypes_data)+(datatypes-1)*sizeof(ompi_datatype_t *));
if (OPAL_UNLIKELY(NULL == p)) {
return OMPI_ERR_OUT_OF_RESOURCE;
}
datatypes = 0;
for (int i=0; i<count; i++) {
if (NULL != stypes[i] && !ompi_datatype_is_predefined(stypes[i])) {
p->types[datatypes++] = stypes[i];
OBJ_RETAIN(stypes[i]);
}
if (NULL != rtypes[i] && !ompi_datatype_is_predefined(rtypes[i])) {
p->types[datatypes++] = rtypes[i];
OBJ_RETAIN(rtypes[i]);
}
}
p->req_complete_cb = request->req_complete_cb;
p->req_complete_cb_data = request->req_complete_cb_data;
request->req_complete_cb = release_datatypes_w_callback;
request->req_complete_cb_data = p;
}
return OMPI_SUCCESS;
}
15 changes: 13 additions & 2 deletions ompi/mca/coll/base/coll_base_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
* University of Stuttgart. All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* Copyright (c) 2014-2017 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* Copyright (c) 2014-2019 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
Expand All @@ -27,6 +27,7 @@
#include "ompi/mca/mca.h"
#include "ompi/datatype/ompi_datatype.h"
#include "ompi/request/request.h"
#include "ompi/op/op.h"
#include "ompi/mca/pml/pml.h"

BEGIN_C_DECLS
Expand Down Expand Up @@ -84,5 +85,15 @@ unsigned int ompi_mirror_perm(unsigned int x, int nbits);
*/
int ompi_rounddown(int num, int factor);

int ompi_coll_base_retain_op( ompi_request_t *request, ompi_op_t *op,
ompi_datatype_t *type);

int ompi_coll_base_retain_datatypes( ompi_request_t *request, ompi_datatype_t *stype,
ompi_datatype_t *rtype);

int ompi_coll_base_retain_datatypes_w( ompi_request_t *request, int count,
ompi_datatype_t *const stypes[],
ompi_datatype_t *const rtypes[]);

END_C_DECLS
#endif /* MCA_COLL_BASE_UTIL_EXPORT_H */
8 changes: 6 additions & 2 deletions ompi/mpi/c/iallgather.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* Copyright (c) 2012 Oak Ridge National Laboratory. All rights reserved.
* Copyright (c) 2013 Los Alamos National Security, LLC. All rights
* reserved.
* Copyright (c) 2015 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* Copyright (c) 2015-2019 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
Expand All @@ -31,6 +31,7 @@
#include "ompi/communicator/communicator.h"
#include "ompi/errhandler/errhandler.h"
#include "ompi/datatype/ompi_datatype.h"
#include "ompi/mca/coll/base/coll_base_util.h"
#include "ompi/memchecker.h"
#include "ompi/runtime/ompi_spc.h"

Expand Down Expand Up @@ -102,6 +103,9 @@ int MPI_Iallgather(const void *sendbuf, int sendcount, MPI_Datatype sendtype,
err = comm->c_coll->coll_iallgather(sendbuf, sendcount, sendtype,
recvbuf, recvcount, recvtype, comm,
request, comm->c_coll->coll_iallgather_module);
if (OPAL_LIKELY(OMPI_SUCCESS == err)) {
ompi_coll_base_retain_datatypes(*request, sendtype, recvtype);
}

OMPI_ERRHANDLER_RETURN(err, comm, err, FUNC_NAME);
}
8 changes: 6 additions & 2 deletions ompi/mpi/c/iallgatherv.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* Copyright (c) 2012 Cisco Systems, Inc. All rights reserved.
* Copyright (c) 2012-2013 Los Alamos National Security, LLC. All rights
* reserved.
* Copyright (c) 2015 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* Copyright (c) 2015-2019 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
Expand All @@ -31,6 +31,7 @@
#include "ompi/communicator/communicator.h"
#include "ompi/errhandler/errhandler.h"
#include "ompi/datatype/ompi_datatype.h"
#include "ompi/mca/coll/base/coll_base_util.h"
#include "ompi/memchecker.h"
#include "ompi/runtime/ompi_spc.h"

Expand Down Expand Up @@ -126,6 +127,9 @@ int MPI_Iallgatherv(const void *sendbuf, int sendcount, MPI_Datatype sendtype,
recvbuf, recvcounts, displs,
recvtype, comm, request,
comm->c_coll->coll_iallgatherv_module);
if (OPAL_LIKELY(OMPI_SUCCESS == err)) {
ompi_coll_base_retain_datatypes(*request, sendtype, recvtype);
}
OMPI_ERRHANDLER_RETURN(err, comm, err, FUNC_NAME);
}

10 changes: 6 additions & 4 deletions ompi/mpi/c/iallreduce.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
* All rights reserved.
* Copyright (c) 2013 Los Alamos National Security, LLC. All rights
* reserved.
* Copyright (c) 2015 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* Copyright (c) 2015-2019 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* Copyright (c) 2016 IBM Corporation. All rights reserved.
* $COPYRIGHT$
*
Expand All @@ -31,6 +31,7 @@
#include "ompi/errhandler/errhandler.h"
#include "ompi/datatype/ompi_datatype.h"
#include "ompi/op/op.h"
#include "ompi/mca/coll/base/coll_base_util.h"
#include "ompi/memchecker.h"
#include "ompi/runtime/ompi_spc.h"

Expand Down Expand Up @@ -112,10 +113,11 @@ int MPI_Iallreduce(const void *sendbuf, void *recvbuf, int count,

/* Invoke the coll component to perform the back-end operation */

OBJ_RETAIN(op);
err = comm->c_coll->coll_iallreduce(sendbuf, recvbuf, count, datatype,
op, comm, request, comm->c_coll->coll_iallreduce_module);
OBJ_RELEASE(op);
if (OPAL_LIKELY(OMPI_SUCCESS == err)) {
ompi_coll_base_retain_op(*request, op, datatype);
}
OMPI_ERRHANDLER_RETURN(err, comm, err, FUNC_NAME);
}

8 changes: 6 additions & 2 deletions ompi/mpi/c/ialltoall.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* Copyright (c) 2012 Oak Ridge National Laboratory. All rights reserved.
* Copyright (c) 2013 Los Alamos National Security, LLC. All rights
* reserved.
* Copyright (c) 2014-2016 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* Copyright (c) 2014-2019 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
Expand All @@ -31,6 +31,7 @@
#include "ompi/communicator/communicator.h"
#include "ompi/errhandler/errhandler.h"
#include "ompi/datatype/ompi_datatype.h"
#include "ompi/mca/coll/base/coll_base_util.h"
#include "ompi/memchecker.h"
#include "ompi/runtime/ompi_spc.h"

Expand Down Expand Up @@ -101,5 +102,8 @@ int MPI_Ialltoall(const void *sendbuf, int sendcount, MPI_Datatype sendtype,
err = comm->c_coll->coll_ialltoall(sendbuf, sendcount, sendtype,
recvbuf, recvcount, recvtype, comm,
request, comm->c_coll->coll_ialltoall_module);
if (OPAL_LIKELY(OMPI_SUCCESS == err)) {
ompi_coll_base_retain_datatypes(*request, sendtype, recvtype);
}
OMPI_ERRHANDLER_RETURN(err, comm, err, FUNC_NAME);
}
8 changes: 6 additions & 2 deletions ompi/mpi/c/ialltoallv.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
* Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
* Copyright (c) 2012-2013 Los Alamos National Security, LLC. All rights
* reserved.
* Copyright (c) 2014-2016 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* Copyright (c) 2014-2019 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
Expand All @@ -30,6 +30,7 @@
#include "ompi/communicator/communicator.h"
#include "ompi/errhandler/errhandler.h"
#include "ompi/datatype/ompi_datatype.h"
#include "ompi/mca/coll/base/coll_base_util.h"
#include "ompi/memchecker.h"
#include "ompi/runtime/ompi_spc.h"

Expand Down Expand Up @@ -130,6 +131,9 @@ int MPI_Ialltoallv(const void *sendbuf, const int sendcounts[], const int sdispl
err = comm->c_coll->coll_ialltoallv(sendbuf, sendcounts, sdispls,
sendtype, recvbuf, recvcounts, rdispls,
recvtype, comm, request, comm->c_coll->coll_ialltoallv_module);
if (OPAL_LIKELY(OMPI_SUCCESS == err)) {
ompi_coll_base_retain_datatypes(*request, sendtype, recvtype);
}
OMPI_ERRHANDLER_RETURN(err, comm, err, FUNC_NAME);
}

11 changes: 9 additions & 2 deletions ompi/mpi/c/ialltoallw.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
* Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
* Copyright (c) 2012-2013 Los Alamos National Security, LLC. All rights
* reserved.
* Copyright (c) 2014-2016 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* Copyright (c) 2014-2019 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
Expand All @@ -30,6 +30,7 @@
#include "ompi/communicator/communicator.h"
#include "ompi/errhandler/errhandler.h"
#include "ompi/datatype/ompi_datatype.h"
#include "ompi/mca/coll/base/coll_base_util.h"
#include "ompi/memchecker.h"
#include "ompi/runtime/ompi_spc.h"

Expand Down Expand Up @@ -127,6 +128,12 @@ int MPI_Ialltoallw(const void *sendbuf, const int sendcounts[], const int sdispl
sendtypes, recvbuf, recvcounts,
rdispls, recvtypes, comm, request,
comm->c_coll->coll_ialltoallw_module);
if (OPAL_LIKELY(OMPI_SUCCESS == err)) {
ompi_coll_base_retain_datatypes_w(*request,
OMPI_COMM_IS_INTER(comm)?ompi_comm_remote_size(comm):ompi_comm_size(comm),
sendtypes,
recvtypes);
}
OMPI_ERRHANDLER_RETURN(err, comm, err, FUNC_NAME);
}

Loading

0 comments on commit 646391b

Please sign in to comment.