Example #1
0
def get_distribution(plst: PList):
    """
    Returns the distribution (as a sequential list) of its argument.

    :param plst: PList
    :return: list
    """
    return plst.get_partition().map(len).to_seq()
Example #2
0
def pssr(input_list: PList) -> PList:
    """
    Sort the input list.

    Sorts using ``<`` only.

    Example::

        >>> from pyske.core import PList
        >>> pssr(PList.init(lambda i: 10-i, 10)).to_seq()
        [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    :param input_list: a parallel list.
        Pre-condition: the size of the local lists should be at least
        equal to the number of processors.
    :return: a sorted list that is a permutation of ``input_list``.
    """
    nprocs = len(par.procs())
    if nprocs == 1:
        return input_list.get_partition().map(sorted).flatten()
    for local_size in input_list.distribution:
        assert local_size >= nprocs

    def permutation(index: int):
        return int(index / nprocs) + nprocs * (index % nprocs)

    def _sample(list_to_sample):
        if list_to_sample:
            size = len(list_to_sample)
            step = int(size / nprocs)
            return list_to_sample[step:size:step]
        return []

    locally_sorted = input_list.get_partition().map(sorted)
    first_samples = locally_sorted.map(_sample).gather(0).get_partition()
    second_samples = bcast(first_samples.map(_merge).map(_sample), 0)
    slices = locally_sorted.map2(_slice, second_samples).flatten()
    result = slices.permute(permutation).get_partition().map(_merge).flatten()
    return result
Example #3
0
def is_sorted(input_list: PList) -> bool:
    """Check if the list is sorted."""
    return input_list.get_partition().map(_is_sorted_list).reduce(and_, True)