import java.util.*; public class scattergathertest { private Random rand = new Random(Ti.thisProc()); private int peer = (Ti.thisProc() + 1)%Ti.numProcs(); public Point<3> [1d] randomPointList(RectDomain<3> rd, int sz) { // generate a point list Point<3> [1d] ptlist = new Point<3>[0:sz-1]; Point<3> minpt = rd.min(); Point<3> maxpt = rd.max(); Hashtable ht = new Hashtable(); // use a hash to keep pts unique foreach (p in ptlist.domain()) { Point<3> pt; Integer i; do { pt = [ (int)(rand.nextFloat() * (maxpt[1]-minpt[1]-1) + minpt[1]), (int)(rand.nextFloat() * (maxpt[2]-minpt[2]-1) + minpt[2]), (int)(rand.nextFloat() * (maxpt[3]-minpt[3]-1) + minpt[3]) ]; i = new Integer(pt[1]*23123+pt[2]*213+pt[3]); // reasonable hash fn } while (ht.get(i) != null); ht.put(i,i); ptlist[p] = pt; } return ptlist; } public void testGather(int [3d] A, int sz) { Point<3> [1d] ptlist = randomPointList(A.domain(),sz); int [1d] packed = new int[0:sz-1]; A.gather(packed, ptlist); // verify foreach (p in packed.domain()) { int actual = packed[p]; int expected = ptlist[p][1] + ptlist[p][2] + ptlist[p][3]; if (actual != expected) { System.out.println("Verification failure in gather test, index " + p[1] + ", pt = " + ptlist[p] + " actual= " + actual + " expected= " + expected); } } } public void testScatter(int [3d] A, int sz) { Point<3> [1d] ptlist = randomPointList(A.domain(),sz); int [1d] packedsrc = new int[0:sz-1]; int [1d] packeddst = new int[0:sz-1]; foreach (p in packedsrc.domain()) { packedsrc[p] = rand.nextInt(); } A.scatter(packedsrc, ptlist); // scatter them out A.gather(packeddst, ptlist); // gather them back // verify foreach (p in packeddst.domain()) { int actual = packeddst[p]; int expected = packedsrc[p]; if (actual != expected) { System.out.println("Verification failure in scatter test, index " + p[1] + ", pt = " + ptlist[p] + " actual= " + actual + " expected= " + expected); } } } public void testCopy(int [3d] A, int sz) { int [3d] B = new int[A.domain().shrink(1)]; int [3d] C = new int[A.domain().shrink(1)]; Point<3> [1d] ptlist = randomPointList(B.domain(),sz); foreach (p in ptlist.domain()) { int val = 0; while (val == 0) val = rand.nextInt(); B[ptlist[p]] = val; } Domain<3> dom = Domain<3>.toDomain(ptlist); A.copy(B, dom); // copy out C.copy(A, ptlist); // copy back // verify foreach (pi in ptlist.domain()) { Point<3> p = ptlist[pi]; int actual = C[p]; int expected = B[p]; if (actual != expected) { System.out.println("Verification failure in scatter test, index " + pi[1] + ", pt = " + p + " actual= " + actual + " expected= " + expected); } } } public static void main(String[]args) { Stack s = new Stack(); // force heap dis-alignment for (int i=0; i < Ti.thisProc()*100; i++) s.push(new String("XXXX")); Domain<1> single szlist = [1:4]+[64:64]+[510:510]+[1023:1023]+[16526:16526]; int [3d] A = new int[[100,300,600]:[200,400,700]:[1,1,1]]; int [1d] single [3d] allA = new int[0:Ti.numProcs()-1][3d]; allA.exchange(A); // init arrays to some known values foreach (p in A.domain()) { A[p] = p[1]+p[2]+p[3]; } scattergathertest me = new scattergathertest(); if (Ti.thisProc() == 0) System.out.println("Testing gather..."); foreach (p in szlist) { int sz = p[1]; if (Ti.thisProc() == 0) System.out.println(" numpts = " + sz + " local gather"); Ti.barrier(); me.testGather(A, sz); // local gather Ti.barrier(); if (Ti.thisProc() == 0) System.out.println(" numpts = " + sz + " remote gather"); Ti.barrier(); me.testGather(allA[me.peer], sz); // remote gather Ti.barrier(); } Ti.barrier(); if (Ti.thisProc() == 0) System.out.println("Testing scatter..."); foreach (p in szlist) { int sz = p[1]; if (Ti.thisProc() == 0) System.out.println(" numpts = " + sz + " local scatter"); Ti.barrier(); me.testScatter(A, sz); // local Ti.barrier(); if (Ti.thisProc() == 0) System.out.println(" numpts = " + sz + " remote scatter"); Ti.barrier(); me.testScatter(allA[me.peer], sz); // remote Ti.barrier(); } if (Ti.thisProc() == 0) System.out.println("Testing sparse copy..."); foreach (p in szlist) { int sz = p[1]; if (Ti.thisProc() == 0) System.out.println(" numpts = " + sz + " local copy"); Ti.barrier(); me.testCopy(A, sz); // local Ti.barrier(); if (Ti.thisProc() == 0) System.out.println(" numpts = " + sz + " remote copy"); Ti.barrier(); me.testCopy(allA[me.peer], sz); // remote Ti.barrier(); } if (Ti.thisProc() == 0) System.out.println("done."); } }