Anil KD
Anil KD

Reputation: 15

MPI writing file unequal size vectors

I am having small doubt regarding file writing in MPI. Lets say I have "N" no of process working on a program. At the end of the program, each process will have "m" number of particles (positions+velocities). But the number of particles, m , differs for each process. How would I write all the particle info (pos + vel) in a single file. What I understood from searching is that I can do so with MPI_File_open, MPI_File_set_view,MPI_File_write_all, But I need to have same no of particles in each process. Any ideas how I could do it in my case ?

Upvotes: 1

Views: 139

Answers (2)

Rob Latham
Rob Latham

Reputation: 5223

You don't need the same number of particles on each processor. What you do need is for every processor to participate. One or more could very well have zero particles, even.

Allgather is a fine way to do it, and the single integer exchanged among all processes is not such large overhead.

However, a better way is to use MPI_SCAN:

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);

Upvotes: 1

Ed Smith
Ed Smith

Reputation: 13216

You need to perform

 MPI_Allgather(np, 1, MPI_INTEGER, procnp, 1, &
               MPI_INTEGER, MPI_COMM_WORLD, ierr)

where np is the number of particles per process and procnp is an array of size number of processes nprocs. This gives you an array on every process of the number of molecules on all other processes. That way MPI_File_set_view can be chosen correctly for each processes by calculating the offset based on the process id. This psudocode to get the offset is something like,

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

This was taken from fortran code so irank is from 1 to nprocs

Upvotes: 0

Related Questions