From 199c857d65a8218c008a80c65cc36be63fc7d04d Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@esi-group.com> Date: Mon, 10 Dec 2018 17:39:20 +0100 Subject: [PATCH] ENH: check if MPI buffer really was attached before deattaching - can occur if MPI has be initialized for communication but OpenFOAM itself is operating in serial mode and thus did not attach any MPI buffers. --- src/Pstream/mpi/UPstream.C | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/Pstream/mpi/UPstream.C b/src/Pstream/mpi/UPstream.C index 4e68dc8bf71..62d04495f5a 100644 --- a/src/Pstream/mpi/UPstream.C +++ b/src/Pstream/mpi/UPstream.C @@ -187,6 +187,9 @@ bool Foam::UPstream::init(int& argc, char**& argv, const bool needsThread) Pout<< "UPstream::init : mpi-buffer-size " << bufSize << endl; } + // TBD: could add error handling here. + // Delete allocated and leave if we fail to attach the buffer? + MPI_Buffer_attach(new char[bufSize], bufSize); } #endif @@ -225,10 +228,19 @@ void Foam::UPstream::exit(int errnum) #ifndef SGIMPI { - int size; - char* buff; - MPI_Buffer_detach(&buff, &size); - delete[] buff; + // Some MPI notes suggest that the return code is MPI_SUCCESS when + // no buffer is attached. + // Be extra careful and require a non-zero size as well. + + int bufSize = 0; + char* buf = nullptr; + + flag = MPI_Buffer_detach(&buf, &bufSize); + + if (MPI_SUCCESS == flag && bufSize) + { + delete[] buf; + } } #endif -- GitLab