import math


def pack_ranks(nodesize,count):
    """!Spreads count MPI ranks over the fewest possible nodes whose
    maximum PETs per node (ppn) is nodesize.  Attempts to distribute
    the ranks per node as equally as possible.  Any node will never
    have more than one or less than one rank than any other node.

    @returns a list of two-element lists.  Each two element list
    contains a pair [number of nodes, PETs per node (ppn)] for nodes
    that have an identical number of PETs per node (ppn)
    @param nodesize the maximum number of PETS per node of any given compute node
    @param count the number of MPI ranks"""
    out=list()
    n=count
    if n<nodesize:  # special case: smaller than one node
        out.append([1,n])
    elif nodesize*(n//nodesize)==n: #exact number of nodes
        out.append([n//nodesize,nodesize])
    else:
        need=math.ceil(n/float(nodesize))
        averagef=n/math.ceil(need)
        af,ai = math.modf(averagef)
        n1=need-round(af*need)
        n2=round(af*need)
        if n1: out.append([n1, ai])
        if n2: out.append([n2, ai+1])
    return out