working version. needs small amount of placing tweaking to put
[charm.git] / examples / charm++ / typed_reduction / TypedReduction.cc
1 #include "TypedReduction.h"
2 #include <stdlib.h>
3
4 Driver::Driver(CkArgMsg* args) {
5     int array_size = 10;
6     if (args->argc > 1) array_size = strtol(args->argv[1], NULL, 10);
7     w = CProxy_Worker::ckNew(array_size);
8     CkCallback *cb = new CkCallback(CkIndex_Driver::untyped_done(NULL), thisProxy);
9     w.ckSetReductionClient(cb);
10     w.reduce();
11     delete args;
12 }
13
14 void Driver::untyped_done(CkReductionMsg* m) {
15     int* output = (int*)m->getData();
16     CkPrintf("Untyped Sum: %d\n", output[0]);
17     delete m;
18     CkCallback *cb = new CkCallback(
19             CkReductionTarget(Driver, typed_done), thisProxy);
20     w.ckSetReductionClient(cb);
21     w.reduce();
22 }
23
24 void Driver::typed_done(int result)
25 {
26     CkPrintf("Typed Sum: %d\n", result);
27     CkCallback *cb = new CkCallback(
28             CkReductionTarget(Driver, typed_array_done), thisProxy);
29     w.ckSetReductionClient(cb);
30     w.reduce_array();
31 }
32
33 void Driver::typed_array_done(int* results, int n)
34 {
35     CkPrintf("Typed Sum: [ ");
36     for (int i=0; i<n; ++i) CkPrintf("%d ", results[i]);
37     CkPrintf("]\n");
38     CkCallback *cb = new CkCallback(
39             CkReductionTarget(Driver, typed_array_done2), thisProxy);
40     w.ckSetReductionClient(cb);
41     w.reduce_array();
42 }
43
44 void Driver::typed_array_done2(int x, int y, int z)
45 {
46     CkPrintf("Typed Sum: (x, y, z) = (%d, %d, %d)\n", x, y, z);
47     CkCallback *cb = new CkCallback(
48             CkReductionTarget(Driver, typed_array_done3), thisProxy);
49     w.ckSetReductionClient(cb);
50     w.reduce_array_doubles();
51 }
52
53 void Driver::typed_array_done3(int n, double* results)
54 {
55     CkPrintf("Typed Sum: [ ");
56     for (int i=0; i<n; ++i) CkPrintf("%.5g ", results[i]);
57     CkPrintf("]\n");
58     CkExit();
59 }
60
61 Worker::Worker() { }
62
63 void Worker::reduce() {
64     int contribution=1;
65     contribute(1*sizeof(int), &contribution, CkReduction::sum_int); 
66 }
67
68 void Worker::reduce_array() {
69     int contribution[3]={1,2,3};
70     contribute(3*sizeof(int), contribution, CkReduction::sum_int); 
71 }
72
73 void Worker::reduce_array_doubles() {
74     double contribution[3] = { 0.16180, 0.27182, 0.31415 };
75     contribute(3*sizeof(double), contribution, CkReduction::sum_double);
76 }
77
78 #include "TypedReduction.def.h"