def create_subcomm(comm, ranks):
    """Create a communicator for a set of ranks."""
    group = comm.Get_group()
    subgroup = group.Incl(ranks)
    subcomm = comm.Create(subgroup)
    subgroup.Free()
    group.Free()
    return subcomm
