Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. import x10.util.ArrayList;
  2. import x10.glb.ArrayListTaskBag;
  3. import x10.glb.TaskQueue;
  4. import x10.glb.TaskBag;
  5. import x10.glb.GLBParameters;
  6. import x10.glb.GLB;
  7. import x10.util.Team;
  8. import x10.glb.Context;
  9. import x10.glb.ContextI;
  10. import x10.glb.GLBResult;
  11.  
  12. public class GlbGlobalRef {
  13.  
  14. val ref = new GlobalRef[ Cell[Long] ]( new Cell[Long](0) );
  15.  
  16. class MyTaskQueue implements TaskQueue[MyTaskQueue, Long] {
  17. val tb = new ArrayListTaskBag[Long]();
  18. var results_of_current_worker:Long = 0;
  19.  
  20. public def init(n: Long) {
  21. Console.OUT.println("adding " + n + " at " + here);
  22. for( i in 1..n ) {
  23. tb.bag().add(i);
  24. }
  25. }
  26.  
  27. public def process(var n:Long, context: Context[MyTaskQueue,Long]):Boolean {
  28.  
  29. for( var i:Long = 0; tb.size() > 0 && i < n; i++) {
  30. val x = tb.bag().removeLast();
  31. Console.OUT.println("running at " + here + " processing " + x);
  32. results_of_current_worker += x;
  33. context.yield();
  34. }
  35. return tb.bag().size() > 0;
  36. }
  37.  
  38. public def count() {
  39. return 0;
  40. }
  41.  
  42. public def merge( var _tb: TaskBag): void {
  43. Console.OUT.println("MyTaskQueue#merge at " + here );
  44. tb.merge( _tb as ArrayListTaskBag[Long]);
  45. }
  46.  
  47. public def split(): TaskBag {
  48. Console.OUT.println("MyTaskQueue#split at " + here);
  49. return tb.split();
  50. }
  51.  
  52. public def printLog(): void {
  53. Console.OUT.println("MyTaskQueue#printLog at " + here);
  54. }
  55.  
  56. public def getResult(): MyResult {
  57. Console.OUT.println("MyTaskQueue#getResult at " + here);
  58. val r = results_of_current_worker;
  59. at(ref) atomic {
  60. Console.OUT.println(" at " + here);
  61. ref()() += r; }
  62. return new MyResult(r);
  63. }
  64.  
  65. class MyResult extends GLBResult[Long] {
  66. val result: Long;
  67.  
  68. public def this(local_result:Long) {
  69. Console.OUT.println("constructor of MyResult");
  70. result = local_result;
  71. }
  72.  
  73. public def getResult():x10.lang.Rail[Long] {
  74. val r = new Rail[Long](1);
  75. r(0) = result;
  76. Console.OUT.println("MyResult#getResult at " + here + " : " + r );
  77. return r;
  78. }
  79.  
  80. public def getReduceOperator():Int {
  81. return Team.ADD;
  82. }
  83.  
  84. public def display(r:Rail[Long]):void {
  85. Console.OUT.println("MyResult#display: " + r(0));
  86. }
  87. }
  88. }
  89.  
  90. def run(n: Long) {
  91. val init = () => { return new MyTaskQueue(); };
  92. val glb = new GLB[MyTaskQueue, Long](init, GLBParameters.Default, true);
  93.  
  94. Console.OUT.println("Starting...");
  95. val start = () => { glb.taskQueue().init(n); };
  96. val r = glb.run(start);
  97. Console.OUT.println(r);
  98.  
  99. at(ref) {
  100. Console.OUT.println("GlobalRef:");
  101. Console.OUT.println( ref()() );
  102. }
  103. }
  104.  
  105. public static def main(args:Rail[String]) {
  106. val n = args.size < 1 ? 10 : Long.parseLong(args(0));
  107. val o = new GlbGlobalRef();
  108. o.run(n);
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement