/* A Parallel (aka Distributed) Vector "class." Class is in quotes, because this should be converted to a real class with internal state, but is currently a set of static operations, similar to the Vector class and the C code. */ public class PVector { /* Compute alpha*x + y and store in dest (daxpy-like operation) * * Arguments: * dest - destination vector. Can be the same as x or y. * alpha - scalar multiplier * x, y - vector inputs * n - vector size */ public static void axpy(double [1d] single [1d] allDest, double alpha, double [1d] single [1d] allX, double [1d] single [1d] allY) { int thisP = Ti.thisProc(); Vector.axpy(allDest[thisP], alpha, allX[thisP], allY[thisP]); } /* Compute the dot product of two vectors x'*y * * Arguments: * x, y - vector inputs * n - vector size */ public static double single dot(double [1d] single [1d] allX, double [1d] single [1d] allY) { int i; double sum = 0; int thisP = Ti.thisProc(); double [1d] myX = allX[thisP]; double [1d] myY = allY[thisP]; foreach (i in myX.domain()) { sum += myX[i]*myY[i]; } return Reduce.add(sum); } /* Should only be called by 1 processor . */ public static String toString (double [1d] single [1d] allV) { String result = ""; foreach (p in [0:Ti.numProcs()-1]) { result += Vector.toString(allV[p]); } return result; } /* toString requires concatentation, and is a bit ugly, so * we provide a print operation instead. This is for * debugging. */ public static void print (double [1d] single [1d] allV) { foreach (p in [0:Ti.numProcs()-1]) { if (p[1] == Ti.thisProc()) { System.out.print("P" + Ti.thisProc() + "'s part:"); System.out.println(Vector.toString(allV[Ti.thisProc()])); } Ti.barrier(); } } public static void print (int [1d] single [1d] allV) { foreach (p in [0:Ti.numProcs()-1]) { if (p[1] == Ti.thisProc()) { System.out.print("P" + Ti.thisProc() + "'s part:"); System.out.println(Vector.toString(allV[Ti.thisProc()])); } Ti.barrier(); } } public static int numPer (int n) { int myCount = n/Ti.numProcs(); if (Ti.thisProc() < n % Ti.numProcs()) { myCount += 1; } return myCount; } public static int lowPer (int n) { return lowPer(n, Ti.thisProc()); } public static int lowPer (int n, int p) { int myLow = n/Ti.numProcs() * p; if (p < n % Ti.numProcs()) { myLow += p; } else { myLow += n % Ti.numProcs(); } return myLow; } public static void tester (String [] args) { int n = 10; int single numP = Ti.numProcs(); int thisP = Ti.thisProc(); int myN = numPer(n); int myLow = lowPer(n); double [1d] myVec1 = new double [myLow:myLow+myN-1]; double [1d] myVec2 = new double [myLow:myLow+myN-1]; double [1d] myVec3 = new double [myLow:myLow+myN-1]; double [1d] single [1d] allVec1 = new double [0:numP-1][1d]; double [1d] single [1d] allVec2 = new double [0:numP-1][1d]; double [1d] single [1d] allVec3 = new double [0:numP-1][1d]; allVec1.exchange(myVec1); allVec2.exchange(myVec2); allVec3.exchange(myVec3); myVec1.set(1); foreach (p in myVec2.domain()) { myVec2[p] = p[1]; } System.out.print("allVec1 = "); print(allVec1); System.out.print("allVec2 = "); print(allVec2); System.out.println("dot(allVec1, allVec2) = " + dot(allVec1, allVec2)); axpy(allVec3,3,allVec1,allVec2); System.out.println("axpy(allVec3, 3, allVec1, allVec2) = "); print(allVec3); } }