Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File MPI写入文件大小不等的向量_File_Mpi - Fatal编程技术网

File MPI写入文件大小不等的向量

File MPI写入文件大小不等的向量,file,mpi,File,Mpi,我对用MPI编写文件有点怀疑。假设我没有处理程序的进程。在程序结束时,每个过程将有“m”个粒子数(位置+速度)。但是每个过程的粒子数m是不同的。如何在单个文件中写入所有粒子信息(pos+vel)。我从搜索中了解到,我可以使用MPI_文件打开、MPI_文件设置视图、MPI_文件写入所有视图来执行此操作,但我需要在每个进程中都有相同数量的粒子。你知道我该怎么做吗 您需要执行以下操作 MPI_Allgather(np, 1, MPI_INTEGER, procnp, 1, &

我对用MPI编写文件有点怀疑。假设我没有处理程序的进程。在程序结束时,每个过程将有“m”个粒子数(位置+速度)。但是每个过程的粒子数m是不同的。如何在单个文件中写入所有粒子信息(pos+vel)。我从搜索中了解到,我可以使用MPI_文件打开、MPI_文件设置视图、MPI_文件写入所有视图来执行此操作,但我需要在每个进程中都有相同数量的粒子。你知道我该怎么做吗

您需要执行以下操作

 MPI_Allgather(np, 1, MPI_INTEGER, procnp, 1, &
               MPI_INTEGER, MPI_COMM_WORLD, ierr)
其中np是每个过程的粒子数,procnp是一个过程数的大小数组
nprocs
。这就给了你一个数组,在每个过程中,所有其他过程中的分子数量。这样,通过基于进程id计算偏移量,就可以为每个进程正确选择
MPI\u文件\u集\u视图

    procdisp = 0
    !Obtain displacement of each processor using all other procs' np
    for i = 1, irank -1
        procdisp = procdisp + procnp(i)*datasize
    enddo

这是从fortran代码中提取的,因此irank的值是从1到NPROC,因此每个处理器上不需要相同数量的粒子。您需要的是让每个处理器都参与进来。甚至,一个或多个粒子很可能是零粒子

Allgather是一种很好的方法,所有进程之间交换的单个整数开销并不大

但是,更好的方法是使用MPI_扫描:

incr = numparts;
MPI_Scan(&incr, &new_offset, 1, MPI_LONG_LONG_INT, 
                      MPI_SUM, MPI_COMM_WORLD);
new_offset -= incr; /* or skip this with MPI_EXSCAN, but \
                       then rank 0 has an undefined result */
MPI_File_write_at_all(fh, new_offset, buf, count, datatype, status);