MPI_File_open: 是否可以让其发现正在使用的文件而放弃?
MPI_File_open: Can it be made to give up if it finds a file in use?
我在 MPI 代码中遇到这样一种情况,其中许多进程将读取许多文件并通过从各种文件获取各种数据来构建自己的域。大多数文件将被多个进程读取。大多数进程将从多个文件中读取。我正在尝试找出一种使所有进程保持活动状态的方法。我想我可能会尝试编写代码,让每个进程循环遍历它的文件列表(在 运行 时间确定,之前无法确定),尝试用 MPI_File_open
打开,然后,如果它看到其当前文件已在使用中,请继续并尝试下一个文件。这个循环将一直持续到所有数据被读取。
但是有没有可能让 MPI_File_open
以这种方式表现?据我所知,如果 MPI_File_open
看到一个文件已经在使用中,它只会等到可以打开它。我还没有找到任何可以改变这种行为的东西。
您似乎可以将信息传递给 mpi_file_open
以指定在移动到新文件之前要等待多长时间。这似乎是依赖于实现的,但是从 openmpi docs 看来,提示 shared_file_timeout
指定了如果文件在返回 MPI_ERR_TIMEDOUT
之前被锁定要等待多长时间。像这样的东西可以工作(我只在文件未锁定时正确测试了这个 compiles/runs )。
#include "mpi.h"
#include <stdio.h>
#include <sys/file.h>
int main( int argc, char *argv[] )
{
MPI_Fint handleA, handleB;
int rc, ec, rank;
MPI_File fh;
MPI_Info info;
//int fd = open("temp", O_CREAT | O_RDWR, 0666);
//int result = flock(fd, LOCK_EX);
MPI_Init( &argc, &argv );
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
MPI_Errhandler_set(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
MPI_Info_create( &info );
MPI_Info_set(info, "shared_file_timeout", "10.0");
ec = MPI_File_open( MPI_COMM_WORLD, "temp", MPI_MODE_RDONLY, info, &fh );
if (ec != MPI_SUCCESS) {
char estring[MPI_MAX_ERROR_STRING];
int len;
MPI_Error_string(ec, error_string, &len);
fprintf(stderr, "%3d: %s\n", rank, error_string);
} else{
fprintf(stderr, "%3d: %s\n", rank, "Success");
}
MPI_File_close( &fh );
MPI_Errhandler_set(MPI_COMM_WORLD, MPI_ERRORS_ARE_FATAL);
MPI_Finalize();
return 0;
}
注意事项,您可能需要设置 MPI_Errhandler
以确保 MPI_ERR_TIMEDOUT
错误不会导致终止。不确定如何使它在不同版本的 mpi 上具有可移植性,但 standard does not seem to specify useful hints for this case, leaving it to implementers. For mpich
this does not work and just blocks endlessly (I can't see an option in mpich to timeout). Non-blocking file open is being considered in the advanced features of MPI-3 所以可能不会很快。
另一种选择是简单地检查文件是否以您使用的任何语言锁定,然后仅在未锁定时才使用 mpi 打开。
我在 MPI 代码中遇到这样一种情况,其中许多进程将读取许多文件并通过从各种文件获取各种数据来构建自己的域。大多数文件将被多个进程读取。大多数进程将从多个文件中读取。我正在尝试找出一种使所有进程保持活动状态的方法。我想我可能会尝试编写代码,让每个进程循环遍历它的文件列表(在 运行 时间确定,之前无法确定),尝试用 MPI_File_open
打开,然后,如果它看到其当前文件已在使用中,请继续并尝试下一个文件。这个循环将一直持续到所有数据被读取。
但是有没有可能让 MPI_File_open
以这种方式表现?据我所知,如果 MPI_File_open
看到一个文件已经在使用中,它只会等到可以打开它。我还没有找到任何可以改变这种行为的东西。
您似乎可以将信息传递给 mpi_file_open
以指定在移动到新文件之前要等待多长时间。这似乎是依赖于实现的,但是从 openmpi docs 看来,提示 shared_file_timeout
指定了如果文件在返回 MPI_ERR_TIMEDOUT
之前被锁定要等待多长时间。像这样的东西可以工作(我只在文件未锁定时正确测试了这个 compiles/runs )。
#include "mpi.h"
#include <stdio.h>
#include <sys/file.h>
int main( int argc, char *argv[] )
{
MPI_Fint handleA, handleB;
int rc, ec, rank;
MPI_File fh;
MPI_Info info;
//int fd = open("temp", O_CREAT | O_RDWR, 0666);
//int result = flock(fd, LOCK_EX);
MPI_Init( &argc, &argv );
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
MPI_Errhandler_set(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
MPI_Info_create( &info );
MPI_Info_set(info, "shared_file_timeout", "10.0");
ec = MPI_File_open( MPI_COMM_WORLD, "temp", MPI_MODE_RDONLY, info, &fh );
if (ec != MPI_SUCCESS) {
char estring[MPI_MAX_ERROR_STRING];
int len;
MPI_Error_string(ec, error_string, &len);
fprintf(stderr, "%3d: %s\n", rank, error_string);
} else{
fprintf(stderr, "%3d: %s\n", rank, "Success");
}
MPI_File_close( &fh );
MPI_Errhandler_set(MPI_COMM_WORLD, MPI_ERRORS_ARE_FATAL);
MPI_Finalize();
return 0;
}
注意事项,您可能需要设置 MPI_Errhandler
以确保 MPI_ERR_TIMEDOUT
错误不会导致终止。不确定如何使它在不同版本的 mpi 上具有可移植性,但 standard does not seem to specify useful hints for this case, leaving it to implementers. For mpich
this does not work and just blocks endlessly (I can't see an option in mpich to timeout). Non-blocking file open is being considered in the advanced features of MPI-3 所以可能不会很快。
另一种选择是简单地检查文件是否以您使用的任何语言锁定,然后仅在未锁定时才使用 mpi 打开。