# examples/tutorial/hello_world.py from charmpy import Chare, Group, charm class Hello(Chare): def SayHi(self): print("Hello World from element", self.thisIndex) def main(args): # create Group of Hello objects (one object exists and runs on each core) hellos = Group(Hello) # call method 'SayHi' of all group members, wait for method to be invoked on all hellos.SayHi(ret=True).get() charm.exit() charm.start(main)
self.check() A = numpy.arange(1000, dtype='float64') work = 1000 * (charm.myPe() + 1) # elements in higher PEs do more work for i in range(work): A += 1.33 self.iteration += 1 if self.iteration == MAX_ITER: self.contribute(None, None, ro.controllers[0].done) elif self.iteration % 20 == 0: self.AtSync() else: self.thisProxy[self.thisIndex].start() def resumeFromSync(self): self.start() def check( self ): # check that my attributes haven't changed as a result of migrating assert (self.originalPe == arrayElemHomeMap[self.thisIndex[0]]) v = numpy.arange(100, dtype='int64') * (self.originalPe + 1) numpy.testing.assert_allclose(self.data, v) def main(args): ro.controllers = Group(Controller) ro.array = Array(Test, charm.numPes() * 4) charm.start(entry=main)
from charmpy import charm, Mainchare, Group from charmpy import readonlies as ro import hello, goodbye class Main(Mainchare): def __init__(self, args): ro.mainProxy = self.thisProxy hellos = Group(hello.Hello) ro.byes = Group(goodbye.Goodbye) hellos.SayHi() def done(self): charm.exit() charm.start(modules=['hello', 'goodbye'])
def doneReduction(self, result): assert result == 1 * self.nElements, "Reduction for dynamic array insertion failed." print("All done.") charm.exit() class Hello(Chare): def __init__(self): print("Hello " + str(self.thisIndex) + " created on PE " + str(CkMyPe())) def SayHi(self, hiNo): print("Hi[" + str(hiNo) + "] from element " + str(self.thisIndex) + " on PE " + str(CkMyPe())) if self.thisIndex == ro.lastIdx: ro.mainProxy.done() else: nextIndex = list(self.thisIndex) for i in range(ro.nDims - 1, -1, -1): nextIndex[i] = (nextIndex[i] + 1) % ro.ARRAY_SIZE[i] if nextIndex[i] != 0: break return self.thisProxy[nextIndex].SayHi(hiNo + 1) def TestReduction(self): self.contribute(1, Reducer.sum, ro.mainProxy.doneReduction) # ---- start charm ---- charm.start()
class Main(Chare): def __init__(self, args): ro.main = self.thisProxy num_chares = min(charm.numPes() * CHARES_PER_PE, MAX_CHARES) assert num_chares % 2 == 0 workers = Array(Worker, num_chares) self.num_responses1 = self.num_responses2 = 0 self.result = 0 for i in range(NUM_ITER): workers.sendVal() self.wait("self.num_responses1 == " + str(num_chares//2)) self.wait("self.num_responses2 == " + str(num_chares//2)) assert(self.result == num_chares * 237) assert(self.num_responses1 == num_chares//2) assert(self.num_responses2 == num_chares//2) self.num_responses1 = self.num_responses2 = 0 self.result = 0 charm.printStats() charm.exit() def collectResult(self, result, tag): if tag == 0: self.num_responses1 += 1 else: self.num_responses2 += 1 self.result += result charm.start(Main)