コード例 #1
0
    def __init__(self, args):
        if sys.version_info < (3, 0, 0):  # not supported in Python 2.7
            exit()
        assert charm.numPes() >= 4
        self.done = -1
        workers = Group(Worker)
        controllers = Array(Controller, charm.numPes())
        receivers = Array(CallbackReceiver,
                          charm.numPes(),
                          args=[self.thisProxy])
        workers.work(receivers[1].getResult)
        self.wait('self.done == 1')
        self.done = -1

        controllers[1].start(workers, receivers[2].getResult)
        self.wait('self.done == 2')
        self.done = -1

        controllers[2].start(workers, receivers.getResultBroadcast)
        self.wait('self.done == ' + str(charm.numPes()))
        self.done = -1

        f = Future()
        controllers[3].start(workers, f)
        assert f.get() == (charm.numPes() * (charm.numPes() - 1)) // 2

        exit()
コード例 #2
0
def test_op(done, op, vector_size, use_numpy=False):
    if vector_size > 1:
        if use_numpy:
            data = np.random.rand(vector_size)
        else:
            data = list(range(0, vector_size))
    else:
        data = random.uniform(0, 5)

    finished_future = Future(2)
    chares = Array(TestVec, vector_size, args=[op, data])
    chares.do_test(None, finished_future, awaitable=True).get()
    section = chares[0:vector_size]
    section.do_test(section, finished_future)
    val1, val2 = finished_future.get()

    try:
        if vector_size > 1:
            if use_numpy:
                assert np.isclose(val1, val2, atol=1e-5).all()
            else:
                assert list(val1) == list(val2)
        else:
            assert val1 == val2
        print('[Main] Reduction with Reducer.%s passes.' % get_op_name(op))
        done(True)
    except AssertionError:
        print('[Main] Reduction with Reducer.%s is not correct.' %
              get_op_name(op))
        done(False)
コード例 #3
0
def main(args):
    t0 = time.time()
    durations2 = []
    num_cpus = charm.numPes()
    # same seed for fair comparison
    np.random.seed(seed=1234)
    streaming_actors = Group(StreamingPrefixCount)
    for _ in range(num_trials):
        streaming_actors.reset(awaitable=True).get()
        start_time = time.time()

        for i in range(num_cpus * 10):
            document = [np.random.bytes(20) for _ in range(10000)]
            #document = [np.random.bytes(5) for _ in range(1000)]   # smaller task size
            streaming_actors[i % num_cpus].add_document(document)

        # wait for quiescence
        charm.waitQD()
        # get the aggregated results
        results = Future()
        streaming_actors.get_popular(results)
        popular_prefixes = results.get()

        duration2 = time.time() - start_time
        durations2.append(duration2)
        print(
            'Stateful computation workload took {} seconds.'.format(duration2))
    print('Total time=', time.time() - t0)
    exit()
コード例 #4
0
ファイル: wave2d.py プロジェクト: sdrave/charm4py
    def __init__(self, args):
        self.RENDER = True
        try:
            args.remove('--NO-RENDER')
            self.RENDER = False
        except ValueError:
            pass

        print('\nUsage: wave2d.py [num_iterations] [max_framerate])')
        global NUM_ITERATIONS, MAX_FRAMERATE
        if len(args) > 1:
            NUM_ITERATIONS = int(args[1])
        if len(args) > 2:
            MAX_FRAMERATE = int(args[2])

        print('Running wave2d on', charm.numPes(), 'processors for', NUM_ITERATIONS, 'iterations')
        print('Max framerate is', MAX_FRAMERATE, 'frames per second')

        self.count = 0  # tracks from how many workers I have received a subimage for this iteration
        programStartTime = frameStartTime = time.time()

        # Create new 2D array of worker chares
        array = Array(Wave, (CHARE_ARRAY_WIDTH, CHARE_ARRAY_HEIGHT))
        # tell all the worker chares to start the simulation
        array.work(self.thisProxy)

        if self.RENDER:
            tk = tkinter.Tk()
            self.frame = Image.new('RGB', (IMAGE_WIDTH, IMAGE_HEIGHT))
            img = ImageTk.PhotoImage(self.frame)
            label_image = tkinter.Label(tk, image=img)
            label_image.pack()

        self.frameReady = Future()
        for i in range(NUM_ITERATIONS):
            self.frameReady.get()  # wait for the next frame
            if MAX_FRAMERATE > 0:
                elapsed = time.time() - frameStartTime
                if elapsed < 1/MAX_FRAMERATE:
                    # enforce framerate
                    charm.sleep(1/MAX_FRAMERATE - elapsed)
            if self.RENDER:
                fps = round(1/(time.time() - frameStartTime))
                # draw frames per second value on image
                d = ImageDraw.Draw(self.frame)
                d.text((10,10), str(fps) + ' fps', fill=(0,0,0,255))
                img = ImageTk.PhotoImage(self.frame)
                label_image.configure(image=img)
                label_image.image = img
                tk.update_idletasks()
                tk.update()

            # loop simulation every 1000 iterations
            reset = (i % 1000 == 0)
            frameStartTime = time.time()
            array.resume(reset)  # tell workers to resume
            self.frameReady = Future()

        print('Program Done!, Total time=', time.time() - programStartTime)
        exit()
コード例 #5
0
ファイル: stencil.py プロジェクト: sdrave/charm4py
class Cell(Chare):
    def __init__(self, numChares):
        idx = self.thisIndex[0]
        self.nbs = []
        for i in range(1, 4):
            self.nbs.append(self.thisProxy[(idx + i) % numChares])
            self.nbs.append(self.thisProxy[(idx - i) % numChares])
        self.iteration = -1
        self.msgs_recvd = 0

    @coro
    def work(self, done_fut):
        for self.iteration in range(NUM_ITER):
            for nb in self.nbs:
                nb.recvData(self.iteration, None)
            self.iter_complete = Future()
            self.iter_complete.get()
        self.reduce(done_fut)

    @when('self.iteration == iteration')
    def recvData(self, iteration, data):
        self.msgs_recvd += 1
        if self.msgs_recvd == len(self.nbs):
            self.msgs_recvd = 0
            self.iter_complete()
コード例 #6
0
ファイル: stencil.py プロジェクト: sdrave/charm4py
 def work(self, done_fut):
     for self.iteration in range(NUM_ITER):
         for nb in self.nbs:
             nb.recvData(self.iteration, None)
         self.iter_complete = Future()
         self.iter_complete.get()
     self.reduce(done_fut)
コード例 #7
0
ファイル: wave2d.py プロジェクト: sdrave/charm4py
    def work(self, mainProxy):
        """ this is the main simulation loop for each chare """

        # size of my rectangular portion of the image
        self.mywidth = IMAGE_WIDTH // CHARE_ARRAY_WIDTH
        self.myheight = IMAGE_HEIGHT // CHARE_ARRAY_HEIGHT
        self.setInitialConditions()

        i = self.thisIndex
        X, Y = CHARE_ARRAY_WIDTH, CHARE_ARRAY_HEIGHT
        # establish a Channel with neighbor chares in the 2D grid
        left = Channel(self, remote=self.thisProxy[(i[0]-1)%X, i[1]])
        right = Channel(self, remote=self.thisProxy[(i[0]+1)%X, i[1]])
        top = Channel(self, remote=self.thisProxy[i[0], (i[1]-1)%Y])
        bottom = Channel(self, remote=self.thisProxy[i[0], (i[1]+1)%Y])

        width, height = self.mywidth, self.myheight
        # coordinate where my portion of the image is located
        sx = self.thisIndex[0] * width
        sy = self.thisIndex[1] * height
        # data will store my portion of the image
        data = np.zeros(width*height*3, dtype=np.uint8)
        buffers = [None] * 4

        # run simulation now
        while True:
            top_edge = self.pressure[[0],:].reshape(width)
            bottom_edge = self.pressure[[-1],:].reshape(width)
            left_edge = self.pressure[:,[0]].reshape(height)
            right_edge = self.pressure[:,[-1]].reshape(height)

            # send ghost values to neighbors
            left.send(RIGHT, left_edge)
            right.send(LEFT, right_edge)
            bottom.send(UP, bottom_edge)
            top.send(DOWN, top_edge)

            # receive ghost values from neighbors. iawait iteratively yields
            # channels as they become ready (have data to receive)
            for channel in charm.iwait((left, right, bottom, top)):
                side, ghost_values = channel.recv()
                buffers[side] = ghost_values

            check_and_compute(height, width,
                              buffers[LEFT], buffers[RIGHT], buffers[UP], buffers[DOWN],
                              self.pressure, self.pressure_old, self.pressure_new)

            # advance to next step by shifting the data back one step in time
            self.pressure_old, self.pressure, self.pressure_new = self.pressure, self.pressure_new, self.pressure_old

            # draw my part of the image, plus a nice 1 pixel border along my
            # right/bottom boundary
            fill_subimage(data, width, height, self.pressure)
            # provide my portion of the image to the mainchare
            mainProxy.depositSubImage(data, (sx, sy), (width, height))
            # wait for message from mainchare to resume simulation
            self.resumeFuture = Future()
            reset = self.resumeFuture.get()
            if reset:
                self.setInitialConditions()
コード例 #8
0
def test_op_logical(done, op, vector_size, use_numpy=False):
    if vector_size > 1:
        if use_numpy:
            data = np.random.rand(vector_size)
            p = 0.1
            data = np.random.choice(a=[False, True],
                                    size=(vector_size),
                                    p=[p, 1 - p])
        else:
            data = list(map(bool, range(0, vector_size)))
    else:
        data = bool(random.randint(0, 1))

    finished_future = Future(2)
    chares = Array(TestVec, vector_size, args=[op, data])
    chares.do_test(None, finished_future)
    section = chares[0:vector_size]
    section.do_test(section, finished_future)
    val1, val2 = finished_future.get()

    try:
        if vector_size > 1:
            assert list(val1) == list(val2)
        else:
            assert val1 == val2
        print('[Main] Reduction with Reducer.%s passes.' % get_op_name(op))
        done(True)
    except AssertionError:
        print('[Main] Reduction with Reducer.%s is not correct.' %
              get_op_name(op))
        done(False)
コード例 #9
0
def main(args):
    print('\nUsage: particle.py [num_chares_x num_chares_y] [max_particles_per_cell_start]')
    if len(args) >= 3:
        array_dims = (int(args[1]), int(args[2]))
    else:
        array_dims = (8, 4)  # default: 2D chare array of 8 x 4 cells
    if len(args) == 4:
        max_particles_per_cell_start = int(args[3])
    else:
        max_particles_per_cell_start = 10000

    print('\nCell array size:', array_dims[0], 'x', array_dims[1], 'cells')

    # create 2D Cell chare array and start simulation
    sim_done = Future()
    cells = Array(Cell, array_dims,
                  args=[array_dims, max_particles_per_cell_start, sim_done],
                  useAtSync=True)
    num_particles_per_cell = cells.getNumParticles(ret=True).get()
    print('Total particles created:', sum(num_particles_per_cell))
    print('Initial conditions:\n\tmin particles per cell:', min(num_particles_per_cell),
          '\n\tmax particles per cell:', max(num_particles_per_cell))
    print('\nStarting simulation')
    t0 = time.time()
    cells.run()  # this is a broadcast
    # wait for the simulation to finish
    sim_done.get()
    print('Particle simulation done, elapsed time=', round(time.time() - t0, 3), 'secs')
    exit()
コード例 #10
0
ファイル: stencil.py プロジェクト: sdrave/charm4py
def main(args):
    numChares = charm.numPes() * CHARES_PER_PE
    cells = Array(Cell, numChares, args=[numChares])
    charm.awaitCreation(cells)
    f = Future()
    cells.work(f)
    f.get()
    exit()
コード例 #11
0
ファイル: test2.py プロジェクト: sdrave/charm4py
def main(args):
    g1 = Group(Test)
    g2 = Group(Test)
    g3 = Group(Test)
    g4 = Group(Test)

    P = charm.numPes()
    a1 = Array(Test, P * 8)
    a2 = Array(Test, P * 10)
    a3 = Array(Test, P * 4)
    a4 = Array(Test, P * 1)

    charm.awaitCreation(g1, g2, g3, g4, a1, a2, a3, a4)

    chares = []  # proxies to all individual chares created
    for collection in (g1, g2, g3, g4):
        for idx in range(P):
            chares.append(collection[idx])

    for collection, numelems in ((a1, P * 8), (a2, P * 10), (a3, P * 4), (a4,
                                                                          P)):
        for idx in range(numelems):
            chares.append(collection[idx])

    print('There are', len(chares), 'chares')

    # establish random channels between chares
    global gchannels
    gchannels = {}
    num_self_channels = 0
    for level in range(NUM_LEVELS):
        gchannels[level] = defaultdict(list)
        for _ in range(NUM_CHANNELS):
            a = random.choice(chares)
            b = random.choice(chares)
            if a == b:
                num_self_channels += 1
            gchannels[level][a].append(b)
            gchannels[level][b].append(a)
    charm.thisProxy.updateGlobals({
        'gchannels': gchannels
    }, awaitable=True).get()

    done_fut = Future(8 *
                      NUM_LEVELS)  # wait for 8 collections to finish 3 levels
    for collection in (g1, g2, g3, g4, a1, a2, a3, a4):
        collection.setup(awaitable=True).get()
    print(NUM_CHANNELS * NUM_LEVELS, 'channels set up,', num_self_channels,
          'self channels')
    for collection in (g1, g2, g3, g4, a1, a2, a3, a4):
        for lvl in range(NUM_LEVELS):
            collection.work(lvl, done_fut)

    msgs = sum(done_fut.get())
    assert msgs == sum(LEVELS_NUM_ITER[:NUM_LEVELS]) * NUM_CHANNELS * 2
    print('total msgs received by chares=', msgs)
    exit()
コード例 #12
0
ファイル: inorder.py プロジェクト: sdrave/charm4py
 def __init__(self, args):
     assert charm.numPes() >= 2
     g = Group(Test)
     done_fut = Future()
     g[1].work(self.thisProxy, done_fut)
     ch = Channel(self, remote=g[1])
     for i in range(NUM_STEPS):
         ch.send(i)
     done_fut.get()
     exit()
コード例 #13
0
def main(args):
    home_pes = Future()
    array = Array(Test, charm.numPes() * 4, args=[home_pes], useAtSync=True)
    charm.thisProxy.updateGlobals(
        {
            'all_created': True,
            'arrayElemHomeMap': home_pes.get()
        },
        '__main__',
        awaitable=True).get()
    array.start()
コード例 #14
0
ファイル: allreduce.py プロジェクト: sdrave/charm4py
def main(args):
    assert charm.numPes() % 2 == 0
    g = Group(Test)
    gsec = g[::2]  # make a section with even numbered elements

    f = Future(2)
    g.work(f, charm.numPes())
    gsec.work(f, charm.numPes() // 2, gsec)
    f.get()
    g.verify(awaitable=True).get()
    exit()
コード例 #15
0
ファイル: multi_futures.py プロジェクト: sdrave/charm4py
def main(args):
    numChares = min(charm.numPes() * CHARES_PER_PE, 64)
    testProxy = Array(Test, numChares)

    f = Future(num_vals=numChares)
    testProxy.getData(f)

    data = f.get()
    print('[Main] Received data: ' + str(data))
    assert sorted(data) == list(range(numChares)), 'Multi-futures failed!'
    print('[Main] All done.')
    exit()
コード例 #16
0
def main(args):
    threaded = False
    if len(args) > 1 and args[1] == '-t':
        threaded = True
    pings = Array(Ping, 2)
    charm.awaitCreation(pings)
    for _ in range(2):
        done_future = Future()
        pings[0].start(done_future, threaded)
        totalTime = done_future.get()
        print("ping pong time per iter (us)=", totalTime / NITER * 1000000)
    exit()
コード例 #17
0
ファイル: test1.py プロジェクト: sdrave/charm4py
 def __init__(self, args):
     assert charm.numPes() >= 3
     done_fut = Future(2)
     chare0 = Chare(Test, onPE=1, args=[0])
     chare1 = Chare(Test, onPE=2, args=[1])
     chare0.work(self.thisProxy, chare1, done_fut)
     chare1.work(self.thisProxy, chare0, done_fut)
     ch0 = Channel(self, remote=chare0)
     ch1 = Channel(self, remote=chare1)
     assert ch0.recv() == 'hello from 0'
     assert ch1.recv() == 'hello from 1'
     done_fut.get()
     exit()
コード例 #18
0
ファイル: test_futures.py プロジェクト: sdrave/charm4py
def main(args):
    testProxy = Array(Test, charm.numPes() * CHARES_PER_PE)

    sum_f = Future()
    min_f = Future()
    max_f = Future()
    testProxy.getStats((sum_f, min_f, max_f))

    print('[Main] Sum: ' + str(sum_f.get()) + ', Min: ' + str(min_f.get()) +
          ', Max: ' + str(max_f.get()))
    print('[Main] All done.')
    exit()
コード例 #19
0
ファイル: qd.py プロジェクト: sdrave/charm4py
 def testQD(self, callback):
     self.qdReached = False
     check_fut = Future()
     t0 = time()
     self.workers.start()
     if callback is not None:
         charm.startQD(callback)
         if isinstance(callback, threads.Future):
             callback.get()
             print('QD reached')
         else:
             self.wait('self.qdReached')
     else:
         charm.waitQD()
     assert time() - t0 > WORK_TIME
     self.workers.check(check_fut)
     check_fut.get()
コード例 #20
0
ファイル: test2.py プロジェクト: sdrave/charm4py
def main(args):
    numChares = charm.numPes() * 10
    a = Array(Test, numChares)
    g = Group(Test)
    charm.awaitCreation(a, g)
    f1 = Future()
    f2 = Future()
    a.start(f1)
    g.start(f2)
    f1.get()
    f2.get()
    exit()
コード例 #21
0
def main(args):

    num_tests = 28
    test_futures = [Future() for _ in range(num_tests)]
    fut = iter(test_futures)

    # tests that when all chares participate in a section
    # that the answer is the same as when the entire array reduces.
    test_op(next(fut), Reducer.sum, 500)
    test_op(next(fut), Reducer.product, 10)
    test_op(next(fut), Reducer.min, 100)
    test_op(next(fut), Reducer.max, 100)
    test_op_logical(next(fut), Reducer.logical_and, 10)
    test_op_logical(next(fut), Reducer.logical_or, 10)
    test_op_logical(next(fut), Reducer.logical_xor, 10)

    # tests that when all chares participate in a section
    # that the answer is the same as when the entire array reduces,
    # the values contributed are numpy arrays.
    test_op(next(fut), Reducer.sum, 500, True)
    test_op(next(fut), Reducer.product, 10, True)
    test_op(next(fut), Reducer.min, 100, True)
    test_op(next(fut), Reducer.max, 100, True)
    test_op_logical(next(fut), Reducer.logical_and, 100, True)
    test_op_logical(next(fut), Reducer.logical_or, 100, True)
    test_op_logical(next(fut), Reducer.logical_xor, 100, True)

    # test that single-value reductions still work
    test_op(next(fut), Reducer.sum, 1, False)
    test_op(next(fut), Reducer.product, 1, False)
    test_op(next(fut), Reducer.min, 1, False)
    test_op(next(fut), Reducer.max, 1, False)
    test_op_logical(next(fut), Reducer.logical_and, 1, False)
    test_op_logical(next(fut), Reducer.logical_or, 1, False)
    test_op_logical(next(fut), Reducer.logical_xor, 1, False)

    # tests that when all chares participate in a section
    # that the answer is the same as when the entire array reduces,
    # # the values contributed are numpy arrays.
    test_op(next(fut), Reducer.sum, 1, True)
    test_op(next(fut), Reducer.product, 1, True)
    test_op(next(fut), Reducer.min, 1, True)
    test_op(next(fut), Reducer.max, 1, True)
    test_op_logical(next(fut), Reducer.logical_and, 1, True)
    test_op_logical(next(fut), Reducer.logical_or, 1, True)
    test_op_logical(next(fut), Reducer.logical_xor, 1, True)

    passes = sum(map(lambda x: x.get(), test_futures))

    if passes == num_tests:
        print('All tests passed!')
        exit()
    else:
        print('ERROR: Not all tests passed.')
        exit(1)
コード例 #22
0
def main(args):
    f1 = Future()
    f2 = Future()
    Group(Test, args=[f1, f2])
    assert f1.get() == charm.numPes()
    assert f2.get() == charm.numPes()
    exit()
コード例 #23
0
ファイル: perf_test.py プロジェクト: UIUC-PPL/charm4py
def main(args):

    g = Group(Worker)

    random.seed(45782)
    ids = []
    for i in range(MAX_VALS):
        #for _ in range(PHASE_NUM):
        #ids.append(i)
        ids.append(i)
    random.shuffle(ids)

    done = Future()
    g.start(done, awaitable=True).get()
    t0 = time.time()
    for id in ids:
        #g.recv_id(id)
        for _ in range(PHASE_NUM):
            g.recv_id(id)
    done.get()
    print("Elapsed=", time.time() - t0)
    exit()
コード例 #24
0
class TestChare(Chare):
    @coro
    def __init__(self, done_future):
        self.done_future = done_future
        self.test_future = Future()

    @coro
    def send_future(self):
        self.test_future(TEST_VALUE)

    @coro
    def wait_future(self):
        data = self.test_future.get()
        assert data == TEST_VALUE
        self.done_future(data)
コード例 #25
0
def main(args):
    f1 = Future()
    f2 = Future()
    Group(Test, args=[f1])
    Array(Test, charm.numPes() * 4, args=[f2])
    np.testing.assert_allclose(f1.get(),
                               np.arange(10, dtype='float64') * charm.numPes())
    np.testing.assert_allclose(
        f2.get(),
        np.arange(10, dtype='float64') * charm.numPes() * 4)
    exit()
コード例 #26
0
def main(args):
    N = min(4, charm.numPes())
    g = Group(Test)
    futures = [Future() for _ in range(N)]
    for i in range(N):
        g[i].work(futures[i])

    futures.reverse()

    t0 = time.time()
    idx = 0
    for f in charm.iwait(futures):
        assert f.get() == idx
        idx += 1
        print(time.time() - t0)
    assert idx == N
    exit()
コード例 #27
0
ファイル: qd.py プロジェクト: sdrave/charm4py
    def __init__(self, args):
        assert charm.numPes() > 1
        numChares = charm.numPes() * CHARES_PER_PE
        self.workers = Array(Worker, numChares, args=[numChares])
        print('WORK_TIME=', WORK_TIME)
        qdGroupReceivers = Group(QDReceiver, args=[self.thisProxy])
        qdArrayReceivers = Array(QDReceiver, charm.numPes(), args=[self.thisProxy])
        charm.awaitCreation(self.workers, qdGroupReceivers, qdArrayReceivers)

        self.testQD(callback=self.thisProxy.recvQD)
        self.testQD(callback=qdGroupReceivers.recvQD)
        self.testQD(callback=qdArrayReceivers.recvQD)
        self.testQD(callback=qdGroupReceivers[1].recvQD)
        self.testQD(callback=qdArrayReceivers[1].recvQD)
        self.testQD(callback=Future())
        self.testQD(callback=None)

        exit()
コード例 #28
0
ファイル: scheduler.py プロジェクト: sdrave/charm4py
def main(args):
    assert charm.numPes() >= 2
    # create the Scheduler on PE 0
    scheduler = Chare(Scheduler, onPE=0)
    # create Futures to receive the results of two jobs
    future1 = Future()
    future2 = Future()
    # send two map_async jobs at the same time to the scheduler
    scheduler.map_async(square, [1, 2, 3, 4, 5], callback=future1)
    scheduler.map_async(square, [1, 3, 5, 7, 9], callback=future2)
    # wait for the two jobs to complete and print the results
    print('Final results are:')
    print(future1.get())
    print(future2.get())
    exit()
コード例 #29
0
    def start(self):
        assert charm.myPe() == 1

        N = charm.numPes() * 3
        a1 = charm.thisProxy[0].createArray(Test, N, ret=True).get()
        f = Future()
        a1.work(f, 5)
        assert f.get() == N * 5

        N = 25
        a2 = charm.thisProxy[0].createArray(Test, (5, 5), args=[33],
                                            ret=True).get()
        f = Future()
        a2.work(f, 6)
        assert f.get() == N * (6 + 33)

        exit()
コード例 #30
0
def main(args):
    done_future = Future()
    test_chare = Chare(TestChare, args=[done_future], onPE=1)

    test_chare.send_future(awaitable=True).get()
    test_chare.wait_future()
    assert done_future.get() == TEST_VALUE

    done_future = Future()
    test_chare = Chare(TestChare, args=[done_future], onPE=1)

    # now make sure it works when we wait before sending
    test_chare.wait_future()
    test_chare.send_future()
    assert done_future.get() == TEST_VALUE

    charm.exit()