def __main(): import argparse import gc from pyske.examples.list.util import rand_list, print_experiment def __compute(): if test == _DIRECT: return dot_product(pl1, pl2) if test == _HAND: return pl2.map2(mul, pl1).reduce(add, 0) if test == _EVAL: return dot_product(PList.raw(pl1), PList.raw(pl2), uncurry=opt.uncurry).eval() return dot_product(PList.raw(pl1), PList.raw(pl2), uncurry=opt.uncurry).run() # Command-line arguments parsing parser = argparse.ArgumentParser() parser.add_argument("--size", help="size of the list to generate", type=int, default=1_000_000) parser.add_argument("--iter", help="number of iterations", type=int, default=30) parser.add_argument("--test", help="choice of the test", choices=[_DIRECT, _HAND, _EVAL, _OPT], default=_DIRECT) args = parser.parse_args() size = args.size test = args.test # Creation of input lists pl1 = rand_list(DPList, size) pl2 = rand_list(DPList, size) # Execution and timing time = Timing() par.at_root(lambda: print("Test:\t", test)) for iteration in range(0, args.iter): gc.collect() gc.disable() par.barrier() time.start() result = __compute() time.stop() print_experiment(result, time.get(), par.at_root, iteration)
def test_timing(test_f, data, name, preprocessing=lambda f, num: lambda: f(num), execute=lambda f: f(), print_test_f=False): # pylint: disable=too-many-arguments """ Iterate and output timing of the application of a function. :param test_f: the function to test. :param data: the input data to the function. :param name: of the test. :param preprocessing: a function to apply to the test function. :param execute: a function to executed the test function. :return: the result of the application of the test function. Output the timings on standard output. """ assert ITERATIONS > 0 par.at_root(lambda: print(f'Test: {name}')) skel = preprocessing(test_f, data) if print_test_f: par.at_root(lambda: print("Term: ", skel)) gc.collect() par.barrier() time: DPList = DPList.init(lambda _: par.wtime()) output = data for idx in range(0, ITERATIONS): def printer(val): return lambda: print(f' Iteration: {val}', end='\r') par.at_root(printer(idx)) output = execute(skel) elapsed = time.map(lambda num: par.wtime() - num).map(lambda num: num / ITERATIONS) par.at_root(lambda: print(30 * ' ', end='\r')) max_elapsed = elapsed.reduce(max) avg_elapsed = elapsed.reduce(add) / elapsed.length() all_elapsed = elapsed.mapi(lambda k, num: "[" + str(k) + "]:" + str(num)).to_seq() par.at_root(lambda: print(f'Time (max):\t{max_elapsed}\n' f'Time (avg):\t{avg_elapsed}\n' f'Time (all):\t{all_elapsed}')) return output
def __main(): parallel_list1 = PList.init(lambda i: __MSG[i], len(__MSG)) parallel_list2 = PList.init(lambda x: x, len(__MSG)) parallel_list4 = parallel_list1.map(lambda x: x.capitalize()).zip( parallel_list2) parallel_list6 = parallel_list4.map(lambda x: x[0]).mapi(lambda i, x: (i, x)) parallel_list7 = parallel_list6.map(lambda x: x[1]) parallel_list8 = parallel_list7.map(lambda x: 1) size = parallel_list8.reduce(lambda x, y: x + y, 0) parallel_list9 = parallel_list7.get_partition() parallel_list10 = parallel_list9.map( lambda l: SList(l).filter(lambda c: c != 'O')).flatten() parallel_list11 = PList.from_seq(["Hello World!"]) filtered = SList(parallel_list10.get_partition().reduce(concat, [])).reduce(add) str1 = SList(parallel_list9.reduce(add)).reduce(add) str2 = parallel_list11.to_seq()[0] par.at_root(lambda: print(f'Capitalized: \t{str1}\n' f'Identity:\t{str2}\n' f'Length:\t\t{size}\n' f'Filtered:\t{filtered}'))
def __main(): parser = argparse.ArgumentParser() parser.add_argument("-n", help="size of the list to generate", type=int, default=1000) parser.add_argument("-a", help="choice of the algorithm (1-3)", type=int, default=1) parser.add_argument("-s", help="choice of a seed", type=int, default=111) # Generating a parallel list of the size specified on the command line or 1000 args = parser.parse_args() size = args.n algorithm = args.a seed = args.s random.seed(seed) data = PList.init(lambda _: bool(random.getrandbits(1)), size) par.barrier() # Solution 1 timer = Timing() timer.start() if algorithm == 1: res = data.map(not_).reduce(and_) # Solution 2 if algorithm == 2: res = data.map_reduce(not_, and_, True) # Solution 3 if algorithm == 3: res = not data.reduce(or_, False) timer.stop() max_t, avg_t, all_t = timer.get() if algorithm in [1, 2, 3]: par.at_root(lambda: print(f'Result: \t{res}\n' f'Time (max):\t{max_t}\n' f'Time (avg):\t{avg_t}\n' f'Time (all):\t{all_t}'))
PARSER.add_argument("--iter", help="number of iterations", type=int, default=5) PARSER.add_argument("--size", help="size of the list to generate", type=int, default=1_000_000) PARSER.add_argument("--seq", help="choice of the data structure", action='store_true') PARSER.add_argument("--test", help="choice of the test", type=int, default=2) PARSER.add_argument("-v", help="verbose mode", action='store_true') ARGS = PARSER.parse_args() ITERATIONS = ARGS.iter SIZE = ARGS.size SEQ = ARGS.seq TST = ARGS.test VRB = ARGS.v if VRB: par.at_root(lambda: print("Iterations:\t", ITERATIONS, "\nSize:\t", SIZE, "\nSeq: \t", SEQ, "\nTest:\t", TST, "\nNprocs:\t", len(par.procs()))) def _test_mmr_direct(lst): lst1 = lst.map(_f_map) lst2 = lst1.map(_f_map) res = lst2.reduce(_f_reduce, 0) return res def _test_mr_direct(lst): def fct(num): return _f_map(_f_map(num)) lst1 = lst.map(fct)