|
| 1 | +import argparse |
| 2 | +import array |
| 3 | +import sys |
| 4 | + |
| 5 | +from . import BOUNDS, MandelbrotSet, apply_to_gridarray |
| 6 | +from ._xypoints import GridSpec, BasicGridArray, BasicMultiprocessingGridArray |
| 7 | +from ._concurrency import CONCURRENCY |
| 8 | + |
| 9 | + |
| 10 | +def _get_grid(spec, concurrentname): |
| 11 | + if concurrentname == 'multiprocessing-not-shared': |
| 12 | + return BasicGridArray.from_spec(spec) |
| 13 | + if 'multiprocessing' in concurrentname: |
| 14 | + return BasicMultiprocessingGridArray.from_spec(spec) |
| 15 | + else: |
| 16 | + return BasicGridArray.from_spec(spec) |
| 17 | + |
| 18 | + |
| 19 | +def prep(maxiterations, density, concurrent=None): |
| 20 | + mbs = MandelbrotSet(maxiterations) |
| 21 | + gridspec = GridSpec(*BOUNDS, density) |
| 22 | + grid = _get_grid(gridspec, getattr(concurrent, '_concurrent', '')) |
| 23 | + return mbs, grid |
| 24 | + |
| 25 | + |
| 26 | +####################################### |
| 27 | +# output |
| 28 | + |
| 29 | +def output_text(values, width): |
| 30 | + hit = ' *' |
| 31 | + hit = '\u26aa' |
| 32 | + #hit = '\U0001f7e1' |
| 33 | + miss = ' ' |
| 34 | + |
| 35 | + def iter_lines(): |
| 36 | + for rowi in range(width): |
| 37 | + line = '' |
| 38 | + for i in range(rowi*width, rowi*width+width): |
| 39 | + if values[i] == 1: |
| 40 | + line += hit |
| 41 | + else: |
| 42 | + line += miss |
| 43 | + yield line |
| 44 | + |
| 45 | + print('*'*(width*2+4)) |
| 46 | + print('*', ' '*(width*2), '*') |
| 47 | + for line in iter_lines(): |
| 48 | + print('*', line, '*') |
| 49 | + print('*', ' '*(width*2), '*') |
| 50 | + print('*'*(width*2+4)) |
| 51 | + |
| 52 | + |
| 53 | +def output_image(values, width): |
| 54 | + from PIL import Image |
| 55 | + |
| 56 | + height = width |
| 57 | + BLACK_AND_WHITE = "1" |
| 58 | + |
| 59 | + image = Image.new(mode=BLACK_AND_WHITE, size=(width, height)) |
| 60 | + image.putdata(values) |
| 61 | + image.show() |
| 62 | + |
| 63 | + |
| 64 | +OUTPUT = { |
| 65 | + 'stdout': output_text, |
| 66 | + 'image': output_image, |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +####################################### |
| 71 | +# the script |
| 72 | + |
| 73 | +''' |
| 74 | + (with GIL) |
| 75 | + size: 100/100 |
| 76 | +
|
| 77 | + threads-not-shared 86s |
| 78 | + threads-shared-direct 105s |
| 79 | + threads-shared-executor 93s |
| 80 | +
|
| 81 | + interpreters-shared-pipe --- |
| 82 | + interpreters-not-shared-pipe 28s |
| 83 | + interpreters-shared-channel 19s |
| 84 | + interpreters-not-shared-channel 19s |
| 85 | +
|
| 86 | + multiprocessing-not-shared 19s |
| 87 | + multiprocessing-shared-direct 24s |
| 88 | + multiprocessing-shared-executor --- |
| 89 | +
|
| 90 | + async 76s |
| 91 | +''' |
| 92 | + |
| 93 | +def parse_args(argv=sys.argv[1:], prog=sys.argv[0]): |
| 94 | + parser = argparse.ArgumentParser(prog=prog) |
| 95 | + |
| 96 | + parser.add_argument('--maxiterations', type=int) |
| 97 | + parser.add_argument('--density', type=int) |
| 98 | + parser.add_argument('--output', choices=list(OUTPUT)) |
| 99 | + parser.add_argument('--concurrent', choices=list(CONCURRENCY)) |
| 100 | + |
| 101 | + args = parser.parse_args(argv) |
| 102 | + ns = vars(args) |
| 103 | + |
| 104 | + if args.concurrent is not None: |
| 105 | + args.concurrent = CONCURRENCY[args.concurrent] |
| 106 | + |
| 107 | + for key, value in list(ns.items()): |
| 108 | + if value is None: |
| 109 | + del ns[key] |
| 110 | + |
| 111 | + return ns |
| 112 | + |
| 113 | + |
| 114 | +def main(maxiterations=100, density=None, output='stdout', concurrent=None): |
| 115 | + if not density: |
| 116 | + if output == 'stdout': |
| 117 | + density = 80 |
| 118 | + else: |
| 119 | + density = 512 |
| 120 | + |
| 121 | + try: |
| 122 | + output = OUTPUT[output] |
| 123 | + except KeyError: |
| 124 | + raise ValueError(f'unsupported output {output!r}') |
| 125 | + |
| 126 | + mbs, grid = prep(maxiterations, density, concurrent) |
| 127 | + apply_to_gridarray(mbs, grid, concurrent) |
| 128 | + output(grid.values, grid.density) |
| 129 | + |
| 130 | + |
| 131 | +if __name__ == '__main__': |
| 132 | + kwargs = parse_args() |
| 133 | + main(**kwargs) |
0 commit comments