Skip to content

Instantly share code, notes, and snippets.

@davetaflin
Created December 3, 2020 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davetaflin/8841e22ad9cd7584249e613cadd3ea2f to your computer and use it in GitHub Desktop.
Save davetaflin/8841e22ad9cd7584249e613cadd3ea2f to your computer and use it in GitHub Desktop.
OpenMPI apparent bug in MPI_File_seek() using MPI_SEEK_END
#include <mpi.h>
#include <string>
#include <vector>
#define DATA_SIZE (0x100000 + 20)
int main(int argc, char* argv[])
{
MPI_Init(&argc, &argv);
int commRank;
MPI_Comm_rank(MPI_COMM_WORLD, &commRank);
int commSize;
MPI_Comm_size(MPI_COMM_WORLD, &commSize);
MPI_File fileHandle;
MPI_File_open(MPI_COMM_WORLD, "fileseekbug.out", MPI_MODE_CREATE | MPI_MODE_WRONLY, MPI_INFO_NULL, &fileHandle);
MPI_Offset mpiOffset = (commRank - 1) * DATA_SIZE;
if (commRank == 0)
mpiOffset = (commSize - 1) * DATA_SIZE;
std::vector<char> data(DATA_SIZE, 0);
data[DATA_SIZE - 1] = 0x01;
MPI_File_seek(fileHandle, mpiOffset, MPI_SEEK_SET);
MPI_File_write(fileHandle, reinterpret_cast<void const*>(&data.front()), static_cast<int>(data.size()), MPI_CHAR, MPI_STATUS_IGNORE);
if (commRank == 0)
{
#if defined THIS_WORKS
MPI_File_get_size(fileHandle, &mpiOffset);
MPI_File_seek(fileHandle, mpiOffset, MPI_SEEK_SET);
#else // This doesn't work:
mpiOffset = 0;
MPI_File_seek(fileHandle, mpiOffset, MPI_SEEK_END);
#endif
std::string message("This should be at the end of the file");
MPI_File_write(fileHandle, reinterpret_cast<void const*>(message.c_str()), static_cast<int>(message.size() + 1), MPI_CHAR, MPI_STATUS_IGNORE);
}
MPI_File_close(&fileHandle);
MPI_Finalize();
return 0;
}
@davetaflin
Copy link
Author

davetaflin commented Dec 3, 2020

Run the sample code with 4 MPI ranks to demonstrate the bug. The output file should consist of all zeros except for a 1 at the end of each rank's data block, and the end of the file should have the message "This should be at the end of the file" The observed behavior is that the message is written to file position 0x400000, which is not the end of the file. This behavior was observed on Ubuntu 20.04 LTS with GCC 9.3.0, with both OpenMPI 3.1.5 and 4.0.5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment