3

我想在 MPI 应用程序中生成一个唯一的文件名。

阅读MPI_FILE_OPEN规范 2.2 版中的“对实现者的建议”表明uriPrefix:foo/bar/baz除了通常的文件名之外的文件名foo/bar/baz也是可能的。就像文件名一样baz/PASSWORD=SECRET。MPI 实现有望使用这些附加信息做正确的事情。

这些额外的文件名元素将对mkstemp(3). 在前一种情况下,uriPrefix可以指示文件应该存放在哪里。在后一种情况下,bazXXXXXX/PASSWORD=SECRET将搞砸mkstemp(3)的模板约定。

有没有人有关于如何安全结合mkstemp(3)的建议MPI_FILE_OPEN

4

1 回答 1

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.

于 2012-11-01T17:06:26.110 回答