File: intsort.py

package info (click to toggle)
cctools 9.9-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 44,624 kB
  • sloc: ansic: 192,539; python: 20,827; cpp: 20,199; sh: 11,719; perl: 4,106; xml: 3,688; makefile: 1,224
file content (52 lines) | stat: -rw-r--r-- 1,812 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import os
import itertools

from weaver.stack import WeaverNests
from weaver.util import Stash

BYTES=8
assert(BYTES % 2 == 0)
SIZE=128*2**30
MINSPLIT=2**30

#WeaverNests.push(Nest(work_dir = 'sort'))

# XXX Use arguments to pass nest pwd, then cd into it. generate named files (add them to outputs/inputs array)
isort = Function('./intsort', cmd_format = '{EXE} isort output input > stdout 2> stderr')
merge = Function('./intsort', cmd_format = '{EXE} merge output input1 input2 > stdout 2> stderr')
split = Function('./intsort', cmd_format = '{EXE} split input output1 output2 > stdout 2> stderr')

def mergesort(result, data, size):
    assert(size % BYTES == 0)
    half = size//2
    assert(half % BYTES == 0)

    if half <= MINSPLIT:
        isort(inputs = data, outputs = result)
    else:
        lsplit = CurrentNest().stash.next()
        rsplit = CurrentNest().stash.next()
        split(arguments = [half], inputs = [data], outputs = [lsplit, rsplit])

        lsorted = CurrentNest().stash.next()
        mergesort(lsorted, lsplit, half)
        rsorted = CurrentNest().stash.next()
        mergesort(rsorted, rsplit, half)

        merge(outputs = result, inputs = [lsorted, rsorted])

data = os.path.join(CurrentNest().work_dir, 'data')

makerandom = ShellFunction('''
    set -e
    mkdir -p "$(dirname "$2")"
    touch "$2"
    openssl enc -aes-256-ctr -pass pass:"$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64)" -nosalt < /dev/zero 2>/dev/null | head --bytes="$1" > "$2"
''', cmd_format = '{EXE} {ARG} {OUT}')
makerandom(arguments = [SIZE], outputs = data, local = True)

result = os.path.join(CurrentNest().work_dir, 'result')
mergesort(result, data, SIZE)
Function('./intsort', cmd_format = '{EXE} assert {IN}')(inputs = [result])

# vim: set sts=4 sw=4 ts=8 expandtab ft=python: