PageRenderTime 27ms CodeModel.GetById 11ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/bench/kilim/bench/BigPingPong.java

http://github.com/kilim/kilim
Java | 113 lines | 80 code | 16 blank | 17 comment | 14 complexity | 52027f9dbf1dc16536accee905b9fa34 MD5 | raw file
  1/* Copyright (c) 2006, Sriram Srinivasan
  2 *
  3 * You may distribute this software under the terms of the license 
  4 * specified in the file "License"
  5 */
  6
  7package kilim.bench;
  8import kilim.*;
  9public class BigPingPong extends Task {
 10    static Mailbox<Msg>[] mboxes;
 11    static Mailbox<Msg> mainmb;
 12
 13    
 14    @SuppressWarnings("unchecked")
 15    public static void main(String args[]) throws Exception {
 16        boolean noargs = args.length == 0;
 17        int nTasks = noargs ? 10 : Integer.parseInt(args[0]);
 18        int nSchedulers = noargs ? 1 : Integer.parseInt(args[1]);
 19        int nThreadsPerScheduler = noargs ? 1 : Integer.parseInt(args[2]);
 20        Scheduler [] schedulers = new Scheduler[nSchedulers];
 21        
 22        System.out.println("nTasks : " + nTasks + ", nSchedulers: " + nSchedulers + 
 23                ", nThreadsPerScheduler: " + nThreadsPerScheduler);
 24        
 25        for (int c = 0; c < 13; c++) { // Timing loop
 26            long beginTime = System.currentTimeMillis();
 27            mboxes = new Mailbox[nTasks];
 28//            mainmb = new Mailbox<Msg>(/* initial size = */ nTasks);
 29            mainmb = new Mailbox<Msg>(/* initial size = */ nTasks, nTasks);
 30            for (int i = 0; i < nTasks; i++) {
 31                mboxes[i] = new Mailbox<Msg>(/* initial size = */ nTasks, nTasks);
 32            }
 33
 34            for (int i = 0 ; i < nSchedulers; i++) {
 35                schedulers[i] = Scheduler.make(nThreadsPerScheduler);
 36            }
 37            
 38            BigPingPong[] tasks = new BigPingPong[nTasks];
 39
 40            for (int i = 0; i < nTasks; i++) {
 41                BigPingPong t = new BigPingPong(i);
 42                tasks[i] = t;
 43                t.setScheduler(schedulers[i % nSchedulers]);
 44                t.start();
 45            }
 46            
 47            for (int i = 0; i < nTasks; i++) {
 48//                mainmb.getWait();
 49                Msg m = mainmb.getb(20000);
 50//              Msg m = mainmb.getb();
 51                if (m == null) {
 52                    System.err.println("TIME OUT (20s). No of tasks finished: " + i);
 53//                    for (BigPingPong t: tasks) {
 54//                        System.err.println(t);
 55//                    }
 56//                    for (Mailbox mb: mboxes) {
 57//                        System.err.println(mb);
 58//                    }
 59                    System.exit(1);
 60                }
 61            }
 62            // Total number of messages: from each task to every other task,
 63            // and finally one to the main mailbox to signal completion
 64            // nTasks * (nTasks - 1) + nTasks
 65            int nMessages = nTasks * nTasks; 
 66            System.out.println("Elapsed ms (" + nTasks + " tasks, " + 
 67                    nMessages + " messages) " + (System.currentTimeMillis() - beginTime));
 68            System.gc();
 69            Thread.sleep(1000);
 70            for (int i = 0; i < nSchedulers; i++) {
 71                schedulers[i].shutdown();
 72            }
 73        }
 74        System.exit(0);
 75    }
 76
 77    int n; // Task's position in the slot array
 78    BigPingPong(int num) {
 79        n = num;
 80    }
 81    
 82    boolean done = false;
 83    int  numRcvd = 0;
 84    
 85    public void execute() throws Pausable {
 86        done = false;
 87        int l = mboxes.length;
 88        Msg mymsg = new Msg(id);
 89        
 90        int me = n;
 91        Mailbox<Msg> mymb = mboxes[me];
 92        
 93        for (int i = 0; i < l; i++) {
 94            if (i == me)
 95                continue;
 96            mboxes[i].put(mymsg);
 97        }
 98        for (int i = 0; i < l - 1; i++) {
 99            Msg m = mymb.get();
100            assert m != null;
101            numRcvd++;
102        }
103        mainmb.put(mymsg);
104        done = true;
105    }
106    
107    private static class Msg {
108        static Msg gMsg = new Msg(0);
109        int from;
110        Msg(int f) {from = f;}
111    };
112}
113