def mpi_run(custom_command=""):

    from mpi4py import MPI

    comm = MPI.COMM_WORLD
    nprocs = comm.Get_size()
    rank = comm.Get_rank()

    success = True
    folder = ''

    # If the process is not the zeroth one, then wait for a signal from your
    # n-1 before initializing the folder
    if rank != 0:
        status = comm.recv(source=rank-1, tag=1)
        folder = comm.recv(source=rank-1, tag=2)
        if status == 'failed':
            success = False
        else:
            number = status

    if success:
        if not custom_command:
            custom_command = " ".join(sys.argv[1:])
        if rank != 0:
            custom_command += " --chain-number %s" % str(int(number)+1)

        # First check if the folder is there
        already_sent = False
        if rank != 0 and rank < nprocs-1:
            status = int(number)+1
            if Data.folder_is_initialised(folder):
                comm.send(status, dest=rank+1, tag=1)
                comm.send(folder, dest=rank+1, tag=2)
                already_sent = True

        # Then, properly initialise
        cosmo, data, command_line, success = safe_initialisation(
            custom_command, comm, nprocs)

        # The first initialisation should check a few more things
        if rank == 0:
            # Check that the run asked is compatible with mpirun and prepare.
            if command_line.subparser_name == 'info':
                warnings.warn(
                    "Analyzing the chains is not supported in mpirun"
                    " so this will run on one core only.")
                status = 'failed'
            elif command_line.method == "MH":
                regexp = re.match(".*__(\w*).txt", data.out_name)
                suffix = regexp.groups()[0]
                status = suffix
            elif command_line.method == "NS":
                status = 1
            else:
                warnings.warn(
                    "The method '%s' is not supported"%(command_line.method) +
                    " in mpirun so this will run on one core only.")
                status = 'failed'
            folder = data.out_name
        elif rank < nprocs-1:
            status = int(number)+1
        # Send an "OK" signal to the next in line, giving the its own chain
        # number for the other to add 1
        if rank < nprocs-1:
            if not already_sent:
                comm.send(status, dest=rank+1, tag=1)
                comm.send(folder, dest=rank+1, tag=2)
    else:
        if rank < nprocs-1:
            comm.send('failed', dest=rank+1, tag=1)
            comm.send('', dest=rank+1, tag=2)

    if success:
        import sampler
        sampler.run(cosmo, data, command_line)

    return
Launch a simple MPI run, with no communication of covariance matrix
dataset/ETHPy150Open baudren/montepython_public/montepython/run.py/mpi_run