//// Define an Epoch task that does some work//task Worker :{ // // This specifies a "message signature" which can be // used by and instance of this task to receive data // and commands from other code. // Work : integer a, integer b { // Do some computation integer result = a + b // Reply to the sender of the message with another // message, carrying the fact that we're done, and // the result of our computation. sender ~> Completed(result) }}//// Define another task, which is responsible// for generating work. It also must contain// the message response signature for taking// the results back from the worker.//task Requester :{ // // This message tells the requester to make up some work // Request : Worker ref worker { // Generate some data integer a = random() integer b = random() // Work on it! // This syntax directs the Work message to reply to this // task when it is done. The responses will be delivered // via Completed messages. worker ~> Work(a, b) ~> self } // // This message is received when the worker // is done doing its thing. It prints stuff // so work has some visible effect. // Completed : integer result { print(cast(string, result) ; " came from task!") }}//// Now we write a standard entrypoint procedure.// We're going to create a worker pool, and then// fire work at each worker a whole bunch.//entrypoint :{ // First our pool of workers... // This syntax allocates an object (task) which is garbage collected as necessary new Worker w1 new Worker w2 new Worker w3 new Worker w4 // Now a wrapper to handle requests... new Requester r // And now we get busy! integer workcount = 0 while((workcount++) < 1000) { r ~> Request(w1) // Asynchronously pass Request message with no reply expected r ~> Request(w2) r ~> Request(w3) r ~> Request(w4) }}
Voila - 4 core random number summation engine!
Hmm interesting. Eagerly anticipating more. Will parallel capabilities be part of the core language?