def a2atest(): print("-----------------------") print("Testing Any2Any Channel") print("All readers and writers should report as done") # TODO: potential race if one of the writers/readers finish early and poison the channel! # the same problem might occur above as well! c = Channel() run_CSP(WN(1, c.write), WN(2, c.write), RN(3, c.read), RN(4, c.read))
def run_bm(): chans = [Channel(f'ch {i}') for i in range(N_CHANNELS)] procs = [] for cno, ch in enumerate(chans): for c_pid in range(N_PROCS_PER_CHAN): writer_id = (cno, c_pid) procs.append(stressed_writer(ch.write, writer_id)) procs.append(stressed_reader(chans, N_PROCS_PER_CHAN)) run_CSP(*procs)
def test(): a = Channel("a") b = Channel("b") c = Channel("c") d = BlackHoleChannel("d") run_CSP(PoisonTest(a.write), Identity(a.read, b.write), Identity(b.read, c.write), Identity(c.read, d.write)) for ch in [a, b, c, d]: print("State of channel", ch.name, "- poisoned is", ch.poisoned)
def run_timing(read_end, write_end): dts = [] res1 = Channel() res2 = Channel() for i in range(100): N = 1000 print(f" Run {i}:", end="") run_CSP(writer_timed(N, write_end, res1.write), reader_timed(N, read_end, res2.write), get_res(N, res1.read, res2.read, dts)) print(" -- min {:.3f} avg {:.3f} max {:.3f} ".format( min(dts), avg(dts), max(dts)))
def run_timing(read_end, write_end): dts = [] for i in range(5): N = 1000 print(f"Run {i}:", end="") #t1 = time.time() run_CSP(writer_timed(N, write_end), reader_timed(N, read_end)) #t2 = time.time() dt_ms = (t2 - t1) * 1000 dt_op_us = (dt_ms / N) * 1000 print(f" DT = {dt_ms:8.3f} ms per op: {dt_op_us:8.3f} us") dts.append(dt_op_us) print(" -- min {:.3f} avg {:.3f} max {:.3f} ".format( min(dts), avg(dts), max(dts)))
def CommsTimeBM(run_no, Delta2=Delta2): # Create channels a = Channel("a") b = Channel("b") c = Channel("c") d = Channel("d") rets = run_CSP( Prefix(c.read, a.write, prefixItem=0), # initiator Delta2(a.read, b.write, d.write), # forwarding to two Successor(b.read, c.write), # feeding back to prefix consumer(d.read, run_no)) # timing process return rets[-1]
def CommsTimeBM(): # Get access to remote channels. # TODO: we can only run this benchmark once before we need to restart the server side # as we will need to re_create the channels between each benchmark run (the first run will poison them). a = apycsp.net.get_channel_proxy_s("a") b = apycsp.net.get_channel_proxy_s("b") c = apycsp.net.get_channel_proxy_s("c") d = apycsp.net.get_channel_proxy_s("d") print("Running commstime") rets = run_CSP( Prefix(c.read, a.write, prefixItem=0), # initiator Delta2(a.read, b.write, d.write), # forwarding to two Successor(b.read, c.write), # feeding back to prefix consumer(d.read)) # timing process return rets[-1]
def CommsTimeBM(): # Get access to remote channels. # TODO: we can only run this benchmark once before we need to restart the server side # as we will need to re_create the channels between each benchmark run (the first run will poison them). a = apycsp.net.get_channel_proxy_s("a") b = apycsp.net.get_channel_proxy_s("b") c = apycsp.net.get_channel_proxy_s("c") d = apycsp.net.get_channel_proxy_s("d") print("Running commstime test") # Rather than pass the objects and get the channel ends wrong, or doing complex # addons like in csp.net, i simply pass the write and read functions as channel ends. # Note: c.read.im_self == c, also check im_func, im_class rets = run_CSP(Prefix(c.read, a.write, prefixItem = 0), # initiator Delta2(a.read, b.write, d.write), # forwarding to two Successor(b.read, c.write), # feeding back to prefix consumer(d.read)) # timing process return rets[-1]
def run_n_procs(n): print(f"Running with {n} simple_procs") ch = Any2OneChannel() pch = One2AnyChannel() t1 = time.time() tasks = [simple_proc(i, ch.write, pch.read) for i in range(N_PROCS)] tasks.append(killer(ch.read, pch, n)) t2 = time.time() res = run_CSP(*tasks) t3 = time.time() rss = res[-1] tcr = t2 - t1 trun = t3 - t2 print("Creating tasks: {:15.3f} us {:15.3f} ms {:15.9f} s".format( 1_000_000 * tcr, 1000 * tcr, tcr)) print("Running tasks: {:15.3f} us {:15.3f} ms {:15.9f} s".format( 1_000_000 * trun, 1000 * trun, trun)) print("{" + ( f'"nprocs" : {n}, "t1" : {t1}, "t2" : {t2}, "t3" : {t3}, "tcr" : {tcr}, "trun" : {trun}, "rss" : {rss}' ) + "}")
def o2otest(): print("-----------------------") print("Testing One2One Channel") print("Reader and writer should both report as done") c = Channel() run_CSP(WN(1, c.write), RN(2, c.read))
def setup_chan(): N = 10 ch = Channel('a') run_CSP(writer(N, ch.write), reader_verb(N, ch.read)) return ch
def AltTest5(): print("------------- AltTest5 ----------------") c = Channel('ch5') run_CSP(alt_timer(c.read), writer(c.write))
t2 = time.time() @process async def writer_timed(N, cout): global t1 for i in range(10): await cout(i) t1 = time.time() for i in range(N): await cout(i) N = 10 c = Channel('a') run_CSP(writer(N, c.write), reader_verb(N, c.read)) def run_timing(read_end, write_end): dts = [] for i in range(5): N = 1000 print(f"Run {i}:", end="") #t1 = time.time() run_CSP(writer_timed(N, write_end), reader_timed(N, read_end)) #t2 = time.time() dt_ms = (t2 - t1) * 1000 dt_op_us = (dt_ms / N) * 1000 print(f" DT = {dt_ms:8.3f} ms per op: {dt_op_us:8.3f} us") dts.append(dt_op_us) print(" -- min {:.3f} avg {:.3f} max {:.3f} ".format(
def bo2otest(): print("-----------------------") print("Testing BufferedOne2One Channel") print("Reader and writer should both report as done") c = BufferedChannel() run_CSP(FastWN(1, c.write), FastRN(2, c.read))
def AltTest(): print("------------- AltTest ----------------") run_CSP(AltTest_p())
def a2otest(): print("-----------------------") print("Testing Any2One Channel") print("Reader should report as done, none of the writers should") c = Channel() run_CSP(WN(1, c.write), WN(2, c.write), RN(3, c.read))
def o2atest(): print("-----------------------") print("Testing One2Any Channel") print("Writer should report as done, none of the readers should") c = Channel() run_CSP(WN(1, c.write), RN(2, c.read), RN(3, c.read))
def AltTest2(): print("------------- AltTest2 ----------------") c = Channel('ch2') run_CSP(p1(c.read), p2(c.write))
def AltTest3(): print("------------- AltTest3 ----------------") c = Channel('ch3') run_CSP(p1_b(c.read), p2(c.write))
def AltTest4(): print("------------- AltTest4 ----------------") c = Channel('ch4') run_CSP(alt_writer(c.write), p1(c.read))
def test2(): a = Channel() run_CSP(Count(a.write), Count(a.write), PoisonReader(a.read)) print("Processes done")
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright (c) 2007 John Markus Bjørndalen, [email protected]. # See LICENSE.txt for licensing details (MIT License). from common import handle_common_args from apycsp import process, Parallel, run_CSP, Sequence from apycsp.plugNplay import * handle_common_args() @process async def TestProc(n): print("This is test proc", n) return f'proc{n}' print("---- Testing Sequence") r = run_CSP(Sequence(TestProc(1), TestProc(2), TestProc(3))) print("Return values", r) print("\n---- Test of Parallel") r = run_CSP(Parallel(TestProc(1), TestProc(2), TestProc(3))) print("Return values", r)