import java.util.*; public class PAPITest { public static void main(String args[]) throws Throwable { int iters = 0; boolean verbose = (args.length > 0); long [] values = new long[allevents.length*4]; PAPICounter [] counters = new PAPICounter[allevents.length*4]; int cnt = 0; long seed = 0; boolean err = false; if (args.length >= 1) try { iters = Integer.parseInt(args[0]); } catch (Throwable exn) {} if (iters == 0) iters = 100; if (iters < 10) iters = 10; if (args.length >= 2) try { seed = Integer.parseInt(args[1]); } catch (Throwable exn) {} if (seed == 0) seed = System.currentTimeMillis() % 10000; Random rand = new Random(seed+Ti.thisProc()); if (verbose && Ti.thisProc() == 0) System.out.println("Starting papi test with "+iters+" iterations and seed=" + seed); Ti.barrier(); try { try { /* check if PAPI is enabled */ PAPICounter t1 = new PAPICounter(PAPICounter.PAPI_TOT_INS); } catch (PAPINotAvailException exn) { if (verbose) System.out.println("got exception: " + exn); System.out.println("Test Passed."); return; } Ti.barrier(); /* build a random set of events that can all run together */ if (verbose) System.out.println("Gathering counters..."); int maxlen = (1+(Math.abs(rand.nextInt())%4))*allevents.length; for (int i = (Math.abs(rand.nextInt())%allevents.length), trycnt = 0; trycnt < maxlen; trycnt++, i = (i+1)%allevents.length) { try { int evt = allevents[i]; if (evt != PAPICounter.PAPI_FP_OPS) { /* avoid PAPI_FP_OPS, causes internal PAPI errors */ PAPICounter pc = new PAPICounter(evt); pc.start(); counters[cnt] = pc; cnt++; pc.stop(); if (verbose) System.out.println("Added Counter:" + pc); pc.start(); } } catch (TooManyCounterException exn) { } catch (EventNotAvailException exn) { } } Ti.barrier(); if (verbose) System.out.println("Got "+cnt+" counters, stopping/clearing them..."); for (int i=0; i < cnt; i++) { PAPICounter pc = counters[i]; pc.stop(); assert pc.getCounterValue() >= 0; pc.clear(); assert pc.getCounterValue() == 0; } Ti.barrier(); if (verbose) System.out.println("Running "+iters+" iterations of tester..."); for (int iter = 0; iter < iters; iter++) { if (iter % 10 == 0) matmulDriver(); int id = (Math.abs(rand.nextInt())%cnt); PAPICounter pc = counters[id]; if (pc.running()) { pc.stop(); long curval = pc.getCounterValue(); if (curval < values[id]) System.out.println("ERROR: (seed="+seed+") got a counter decrease from "+values[id]+" to "+curval+" in "+pc); values[id] = curval; } else { pc.start(); } } Ti.barrier(); if (verbose) System.out.println("Getting final counter values..."); for (int i=0; i < cnt; i++) { PAPICounter pc = counters[i]; if (pc.running()) pc.stop(); assert pc.getCounterValue() >= 0; if (verbose) System.out.println("Final Counter:" + pc); } } catch (Throwable exn) { System.out.println("ERROR: (seed="+seed+") got exception that should never happen: " + exn); err = true; } Ti.barrier(); if (err) System.out.println("Test Failed."); else System.out.println("Test Passed."); } public static void matMul(double [2d] a, double [2d] b, double [2d] c) { RectDomain<2> aRow; RectDomain<2> bCol; Point<2> kj; int n = c.domain().max()[1]; // assumes square for now // why are points 1-based? double sum; foreach (ij in c.domain()) { int i = ij[1]; int j = ij[2]; aRow = [i:i,0:n]; foreach (ik in aRow) { kj = [ik[2],ij[2]]; c[ij] += a[ik] * b[kj]; } } } public static void matmulDriver () { for (int n = 32; n <= 32; n*=2) { RectDomain<2> d2 = [0:n-1,0:n-1]; double [2d] a = new double [d2]; double [2d] b = new double [d2]; double [2d] c = new double [d2]; foreach (ij in d2) { a[ij] = 2.0; b[ij] = 2.0; c[ij] = 0.0; } long ms = System.currentTimeMillis(); matMul(a,b,c); ms = System.currentTimeMillis() - ms; //System.out.println("n = " + n + ", Mflops = " + // ((double) 2*n*n*n)/((double) ms*1000)); foreach (ij in d2) { if (c[ij] != 4*n) System.out.println("Incorrect computation"); } } } public static int[] allevents = { /* all the PAPI Counters */ PAPICounter.PAPI_L1_DCM , PAPICounter.PAPI_L1_ICM , PAPICounter.PAPI_L2_DCM , PAPICounter.PAPI_L2_ICM , PAPICounter.PAPI_L3_DCM , PAPICounter.PAPI_L3_ICM , PAPICounter.PAPI_L1_TCM , PAPICounter.PAPI_L2_TCM , PAPICounter.PAPI_L3_TCM , PAPICounter.PAPI_CA_SNP , PAPICounter.PAPI_CA_SHR , PAPICounter.PAPI_CA_CLN , PAPICounter.PAPI_CA_INV , PAPICounter.PAPI_CA_ITV , PAPICounter.PAPI_L3_LDM , PAPICounter.PAPI_L3_STM , PAPICounter.PAPI_BRU_IDL , PAPICounter.PAPI_FXU_IDL , PAPICounter.PAPI_FPU_IDL , PAPICounter.PAPI_LSU_IDL , PAPICounter.PAPI_TLB_DM , PAPICounter.PAPI_TLB_IM , PAPICounter.PAPI_TLB_TL , PAPICounter.PAPI_L1_LDM , PAPICounter.PAPI_L1_STM , PAPICounter.PAPI_L2_LDM , PAPICounter.PAPI_L2_STM , PAPICounter.PAPI_BTAC_M , PAPICounter.PAPI_PRF_DM , PAPICounter.PAPI_L3_DCH , PAPICounter.PAPI_TLB_SD , PAPICounter.PAPI_CSR_FAL , PAPICounter.PAPI_CSR_SUC , PAPICounter.PAPI_CSR_TOT , PAPICounter.PAPI_MEM_SCY , PAPICounter.PAPI_MEM_RCY , PAPICounter.PAPI_MEM_WCY , PAPICounter.PAPI_STL_ICY , PAPICounter.PAPI_FUL_ICY , PAPICounter.PAPI_STL_CCY , PAPICounter.PAPI_FUL_CCY , PAPICounter.PAPI_HW_INT , PAPICounter.PAPI_BR_UCN , PAPICounter.PAPI_BR_CN , PAPICounter.PAPI_BR_TKN , PAPICounter.PAPI_BR_NTK , PAPICounter.PAPI_BR_MSP , PAPICounter.PAPI_BR_PRC , PAPICounter.PAPI_FMA_INS , PAPICounter.PAPI_TOT_IIS , PAPICounter.PAPI_TOT_INS , PAPICounter.PAPI_INT_INS , PAPICounter.PAPI_FP_INS , PAPICounter.PAPI_LD_INS , PAPICounter.PAPI_SR_INS , PAPICounter.PAPI_BR_INS , PAPICounter.PAPI_VEC_INS , PAPICounter.PAPI_RES_STL , PAPICounter.PAPI_FP_STAL , PAPICounter.PAPI_TOT_CYC , PAPICounter.PAPI_LST_INS , PAPICounter.PAPI_SYC_INS , PAPICounter.PAPI_L1_DCH , PAPICounter.PAPI_L2_DCH , PAPICounter.PAPI_L1_DCA , PAPICounter.PAPI_L2_DCA , PAPICounter.PAPI_L3_DCA , PAPICounter.PAPI_L1_DCR , PAPICounter.PAPI_L2_DCR , PAPICounter.PAPI_L3_DCR , PAPICounter.PAPI_L1_DCW , PAPICounter.PAPI_L2_DCW , PAPICounter.PAPI_L3_DCW , PAPICounter.PAPI_L1_ICH , PAPICounter.PAPI_L2_ICH , PAPICounter.PAPI_L3_ICH , PAPICounter.PAPI_L1_ICA , PAPICounter.PAPI_L2_ICA , PAPICounter.PAPI_L3_ICA , PAPICounter.PAPI_L1_ICR , PAPICounter.PAPI_L2_ICR , PAPICounter.PAPI_L3_ICR , PAPICounter.PAPI_L1_ICW , PAPICounter.PAPI_L2_ICW , PAPICounter.PAPI_L3_ICW , PAPICounter.PAPI_L1_TCH , PAPICounter.PAPI_L2_TCH , PAPICounter.PAPI_L3_TCH , PAPICounter.PAPI_L1_TCA , PAPICounter.PAPI_L2_TCA , PAPICounter.PAPI_L3_TCA , PAPICounter.PAPI_L1_TCR , PAPICounter.PAPI_L2_TCR , PAPICounter.PAPI_L3_TCR , PAPICounter.PAPI_L1_TCW , PAPICounter.PAPI_L2_TCW , PAPICounter.PAPI_L3_TCW , PAPICounter.PAPI_FML_INS , PAPICounter.PAPI_FAD_INS , PAPICounter.PAPI_FDV_INS , PAPICounter.PAPI_FSQ_INS , PAPICounter.PAPI_FNV_INS , PAPICounter.PAPI_FP_OPS }; }