3

I'd like to generate a unique filename within an MPI application.

Reading "Advice to implementors" under MPI_FILE_OPEN in version 2.2 of the the specification indicates that filenames like uriPrefix:foo/bar/baz in addition to just the usual foo/bar/baz are possible. As are filenames like baz/PASSWORD=SECRET. The MPI implementation is expected to do the right thing with this additional information.

These additional filename elements will play havoc with mkstemp(3). In the former case, the uriPrefix may indicate where the file should be housed. In the later case, bazXXXXXX/PASSWORD=SECRET will screw up mkstemp(3)'s template conventions.

Does anyone have suggestions for how to safely combine mkstemp(3) with MPI_FILE_OPEN?

Rhys Ulerich
  • 1,242
  • 1
  • 12
  • 28

1 Answers1

2

MPI_FILE_OPEN is a collective operation. It is intended to be called by all MPI ranks with the same file name and the name should also point to a location on a shared filesystem. Otherwise it makes no sense. mkstemp(3) would generate different names if called in different ranks. Also it creates the file and returns its file descriptor. Probably not what you want if you'd like to use MPI parallel IO.

If you'd really want to create a unique file, then you could probably do something like this:

int rank;
char name[PATH_MAX] = "/path/to/shared/directory/prefixXXXXXX";
MPI_File fh;

MPI_Comm_rank(MPI_COMM_WORLD, &rank);

// Create the temporary file in rank 0 only
if (rank == 0)
{
   int fd = mkstemp(name);
   // Close the handle - we don't need it
   close(fd);
   // <----- Decorate name here
}
// Broadcast the file name to all other ranks
MPI_Bcast(name, PATH_MAX, MPI_CHAR, 0, MPI_COMM_WORLD);

// Now open the file for MPI parallel IO
MPI_File_open(MPI_COMM_WORLD, name, MPI_MODE_RDWR, MPI_INFO_NULL, &fh);

Once you've got the name from mkstemp(3), at the point marked as Decorate name here you can perform additional decorations of the name, e.g. append /PASSWORD=SECRET to it or prefix it with fstype:. You could also do it after the broadcast, if you need to put some process-specific decorations.

Hristo Iliev
  • 72,659
  • 12
  • 135
  • 186
  • The idea to "decorate" the name is a good one, but it's unclear if the decoration changes the filesystem on which the temporary file should be created. An MPI-aware `mkstemp` appears to be necessary, or some way to query the MPI implementation about the way in which the decoration will be interpreted. – Rhys Ulerich Nov 01 '12 at 21:16