When do I need to use MPI_Barrier()?

前端 未结 3 1784
难免孤独
难免孤独 2020-12-24 13:05

I wonder when do I need to use barrier? Do I need it before/after a scatter/gather for example? Or should OMPI ensure all processes have reached that point before scatter/ga

3条回答
  •  遥遥无期
    2020-12-24 14:01

    One use of MPI_Barrier is for example to control access to an external resource such as the filesystem, which is not accessed using MPI. For example, if you want each process to write stuff to a file in sequence, you could do it like this:

    int rank, size;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    for ( int ii = 0; ii < size; ++ii ) {
        if ( rank == ii ) {
            // my turn to write to the file
            writeStuffToTheFile();
        }
        MPI_Barrier(MPI_COMM_WORLD);
    }
    

    That way, you can be sure that no two processes are concurrently calling writeStuffToTheFile.

提交回复
热议问题