From 8e04137c082846dd689ad3e5902a32fec5a72758 Mon Sep 17 00:00:00 2001 From: John Canny Date: Thu, 18 Feb 2021 23:20:24 -0800 Subject: [PATCH 1/3] finalized nocuda compile --- bidmat | 2 +- jni/pom.xml | 1 + jni/src/BIDMat_UTILS.cpp | 2 +- jni/src/Makefile | 4 +- lib/bidmat_init.sc | 3 +- pom.xml | 9 +- src/main/java/edu/berkeley/bid/CUMAT.java | 378 --- src/main/java/edu/berkeley/bid/CUMATD.java | 183 -- src/main/java/edu/berkeley/bid/NCCL.java | 35 - src/main/java/edu/berkeley/bid/SLATEC.java | 6 +- src/main/scala/BIDMat/BMat.scala | 8 +- src/main/scala/BIDMat/Benchmarks.scala | 2 +- src/main/scala/BIDMat/DFunctions.scala | 122 +- src/main/scala/BIDMat/DMat.scala | 40 +- src/main/scala/BIDMat/Dict.scala | 2 +- src/main/scala/BIDMat/FFilter.scala | 12 +- src/main/scala/BIDMat/FFunctions.scala | 200 +- src/main/scala/BIDMat/FMat.scala | 74 +- src/main/scala/BIDMat/GDFunctions.scala | 624 ----- src/main/scala/BIDMat/GDMat.scala | 2069 ---------------- src/main/scala/BIDMat/GFilter.scala | 602 ----- src/main/scala/BIDMat/GFunctions.scala | 1128 --------- src/main/scala/BIDMat/GIFunctions.scala | 362 --- src/main/scala/BIDMat/GIMat.scala | 1373 ----------- src/main/scala/BIDMat/GLFunctions.scala | 190 -- src/main/scala/BIDMat/GLMat.scala | 1484 ----------- src/main/scala/BIDMat/GMat.scala | 2598 -------------------- src/main/scala/BIDMat/GND.scalax | 1128 --------- src/main/scala/BIDMat/GSDMat.scala | 725 ------ src/main/scala/BIDMat/GSMat.scala | 814 ------ src/main/scala/BIDMat/HMat.scala | 3 +- src/main/scala/BIDMat/IDict.scala | 6 +- src/main/scala/BIDMat/IFunctions.scala | 30 +- src/main/scala/BIDMat/IMat.scala | 29 +- src/main/scala/BIDMat/LDict.scala | 9 +- src/main/scala/BIDMat/LFunctions.scala | 30 +- src/main/scala/BIDMat/LMat.scala | 20 +- src/main/scala/BIDMat/Mat.scala | 85 +- src/main/scala/BIDMat/MatFunctions.scala | 432 ++-- src/main/scala/BIDMat/MatHDF5.scala | 590 ----- src/main/scala/BIDMat/Operators.scala | 20 +- src/main/scala/BIDMat/SDMat.scala | 2 +- src/main/scala/BIDMat/SFunctions.scala | 6 +- src/main/scala/BIDMat/SMat.scala | 26 +- src/main/scala/BIDMat/SciFunctions.scala | 240 +- src/main/scala/BIDMat/Solvers.scala | 61 +- src/main/scala/BIDMat/TMat.scala | 63 +- src/test/scala/BIDMat/GDMatTest.scala | 931 ------- src/test/scala/BIDMat/GIMatTest.scala | 1019 -------- src/test/scala/BIDMat/GMatTest.scala | 1344 ---------- src/test/scala/BIDMat/GSMatTest.scala | 318 --- src/test/scala/BIDMat/TestHDF5.scala | 39 - 52 files changed, 740 insertions(+), 18743 deletions(-) delete mode 100755 src/main/java/edu/berkeley/bid/CUMAT.java delete mode 100755 src/main/java/edu/berkeley/bid/CUMATD.java delete mode 100644 src/main/java/edu/berkeley/bid/NCCL.java delete mode 100644 src/main/scala/BIDMat/GDFunctions.scala delete mode 100755 src/main/scala/BIDMat/GDMat.scala delete mode 100644 src/main/scala/BIDMat/GFilter.scala delete mode 100644 src/main/scala/BIDMat/GFunctions.scala delete mode 100644 src/main/scala/BIDMat/GIFunctions.scala delete mode 100755 src/main/scala/BIDMat/GIMat.scala delete mode 100644 src/main/scala/BIDMat/GLFunctions.scala delete mode 100755 src/main/scala/BIDMat/GLMat.scala delete mode 100644 src/main/scala/BIDMat/GND.scalax delete mode 100755 src/main/scala/BIDMat/GSDMat.scala delete mode 100755 src/main/scala/BIDMat/GSMat.scala delete mode 100644 src/test/scala/BIDMat/GDMatTest.scala delete mode 100755 src/test/scala/BIDMat/GIMatTest.scala delete mode 100644 src/test/scala/BIDMat/GMatTest.scala delete mode 100755 src/test/scala/BIDMat/GSMatTest.scala delete mode 100755 src/test/scala/BIDMat/TestHDF5.scala diff --git a/bidmat b/bidmat index 50bef3f6..07e885f8 100755 --- a/bidmat +++ b/bidmat @@ -3,7 +3,7 @@ # set CUDA_HOME="" # set if desired, otherwise defaults to /usr/local/cuda # note CUDA_PATH="" # Should be set to the latest toolkit in Windows # Change these if needed -MEMSIZE="-Xmx14G" +#MEMSIZE="-Xmx14G" BIDMAT_ROOT="${BASH_SOURCE[0]}" if [ ! `uname` = "Darwin" ]; then BIDMAT_ROOT=`readlink -f "${BIDMAT_ROOT}"` diff --git a/jni/pom.xml b/jni/pom.xml index b0d0959b..be9c5491 100644 --- a/jni/pom.xml +++ b/jni/pom.xml @@ -76,6 +76,7 @@ unix + amd64 !mac os x diff --git a/jni/src/BIDMat_UTILS.cpp b/jni/src/BIDMat_UTILS.cpp index 683dc4a2..fb981ac2 100755 --- a/jni/src/BIDMat_UTILS.cpp +++ b/jni/src/BIDMat_UTILS.cpp @@ -7,7 +7,7 @@ #ifdef __INTEL_COMPILER #include #endif -#include +//#include extern "C" { diff --git a/jni/src/Makefile b/jni/src/Makefile index 102228f1..1f9a776b 100755 --- a/jni/src/Makefile +++ b/jni/src/Makefile @@ -12,7 +12,7 @@ SLATEC_OBJS=csevl.$(OBJ) inits.$(OBJ) r1mach.$(OBJ) i1mach.$(OBJ) utils.$(OBJ) y .SUFFIXES: .$(OBJ) .c .cpp .cu -all: $(LIBPREPEND)bidmatcpu$(LIBAPPEND) $(LIBPREPEND)bidmatcuda$(LIBAPPEND) +all: $(LIBPREPEND)bidmatcpu$(LIBAPPEND) cudalib: $(LIBPREPEND)bidmatcuda$(LIBAPPEND) @@ -42,7 +42,7 @@ slatec.$(OBJ) : slatec/slatec.cu $(SLATEC_DEPS) %.$(OBJ) : slatec/%.c $(CC) $(CFLAGS) slatec/$*.c -install: installcudalib installcpulib +install: installcpulib installcudalib: $(INSTALL_DIR)/$(LIBPREPEND)bidmatcuda$(LIBAPPEND) diff --git a/lib/bidmat_init.sc b/lib/bidmat_init.sc index e07c441b..e535b91d 100755 --- a/lib/bidmat_init.sc +++ b/lib/bidmat_init.sc @@ -1,4 +1,4 @@ -import BIDMat.{BMat,CMat,CSMat,DMat,Dict,FMat,GMat,GDMat,GIMat,GLMat,GSMat,GSDMat,HMat,IDict,Image,IMat,LMat,Mat} +import BIDMat.{BMat,CMat,CSMat,DMat,Dict,FMat,HMat,IDict,Image,IMat,LMat,Mat} import BIDMat.{DQuaternion,Quaternion,SMat,SBMat,SDMat,TMat} import BIDMat.MatFunctions._ import BIDMat.SciFunctions._ @@ -7,5 +7,4 @@ import BIDMat.Plotting._ import BIDMat.Mat.console_publish Mat.checkMKL(false) -Mat.checkCUDA(true) diff --git a/pom.xml b/pom.xml index c00f0e10..5fc33307 100755 --- a/pom.xml +++ b/pom.xml @@ -77,6 +77,7 @@ unix + amd64 !mac os x @@ -192,7 +193,7 @@ akka-kryo-serialization_2.11 0.5.0 - + + ptplot ptplot diff --git a/src/main/java/edu/berkeley/bid/CUMAT.java b/src/main/java/edu/berkeley/bid/CUMAT.java deleted file mode 100755 index bacbf5fe..00000000 --- a/src/main/java/edu/berkeley/bid/CUMAT.java +++ /dev/null @@ -1,378 +0,0 @@ -package edu.berkeley.bid; -import jcuda.Pointer; -import jcuda.jcublas.cublasHandle; - -public final class CUMAT { - - private CUMAT() {} - - static { - LibUtils.loadLibrary("bidmatcuda"); - } - - public static native int intToFloat(Pointer A, Pointer B, int N); - - public static native int doubleToFloat(Pointer A, Pointer B, int N); - - public static native int longToFloat(Pointer A, Pointer B, int N); - - public static native int floatToLong(Pointer A, Pointer B, int N); - - public static native int floatToInt(Pointer A, Pointer B, int N); - - public static native int floatToDouble(Pointer A, Pointer B, int N); - - public static native int longToInt(Pointer A, Pointer B, int N); - - public static native int intToLong(Pointer A, Pointer B, int N); - - public static native int initSeq(Pointer A, int nrows, int ncols, int dorows); - - public static native int applyop(Pointer A, int Anrows, int Ancols, Pointer B, int Bnrows, int Bncols, Pointer C, int opn); - - public static native int applyiop(Pointer A, int Anrows, int Ancols, Pointer B, int Bnrows, int Bncols, Pointer C, int opn); - - public static native int applylop(Pointer A, int Anrows, int Ancols, Pointer B, int Bnrows, int Bncols, Pointer C, int opn); - - public static native int applydop(Pointer A, int Anrows, int Ancols, Pointer B, int Bnrows, int Bncols, Pointer C, int opn); - - public static native int copyToInds(Pointer A, Pointer B, Pointer I, long len); - - public static native int copyToInds2D(Pointer A, int lda, Pointer B, int ldb, Pointer I, int nrows, Pointer J, int ncols); - - public static native int copyToInds3D(Pointer A, int lda, int rda, Pointer B, int ldb, int rdb, Pointer I, int nrows, Pointer J, int ncols, Pointer K, int nd); - - public static native int copyToInds4D(Pointer A, int lda, int rda, int tda, Pointer B, int ldb, int rdb, int tdb, Pointer I, int nrows, Pointer J, int ncols, Pointer K, int nk, Pointer L, int nl); - - public static native int copyToIndsLong(Pointer A, Pointer B, Pointer I, long len); - - public static native int copyToInds2DLong(Pointer A, int lda, Pointer B, int ldb, Pointer I, int nrows, Pointer J, int ncols); - - public static native int copyToInds3DLong(Pointer A, int lda, int rda, Pointer B, int ldb, int rdb, Pointer I, int nrows, Pointer J, int ncols, Pointer K, int nd); - - public static native int copyToInds4DLong(Pointer A, int lda, int rda, int tda, Pointer B, int ldb, int rdb, int tdb, Pointer I, int nrows, Pointer J, int ncols, Pointer K, int nk, Pointer L, int nl); - - public static native int fillToInds(float A, Pointer B, Pointer I, long len); - - public static native int fillToInds2D(float A, Pointer B, int ldb, Pointer I, int nrows, Pointer J, int ncols); - - public static native int fillToInds3D(float A, Pointer B, int ldb, int rdb, Pointer I, int nrows, Pointer J, int ncols, Pointer K, int nd); - - public static native int fillToInds4D(float A, Pointer B, int ldb, int rdb, int tdb, Pointer I, int nrows, Pointer J, int ncols, Pointer K, int nk, Pointer L, int nl); - - public static native int fillToIndsInt(int A, Pointer B, Pointer I, long len); - - public static native int fillToInds2DInt(int A, Pointer B, int ldb, Pointer I, int nrows, Pointer J, int ncols); - - public static native int fillToInds3DInt(int A, Pointer B, int ldb, int rdb, Pointer I, int nrows, Pointer J, int ncols, Pointer K, int nd); - - public static native int fillToInds4DInt(int A, Pointer B, int ldb, int rdb, int tdb, Pointer I, int nrows, Pointer J, int ncols, Pointer K, int nk, Pointer L, int nl); - - public static native int fillToIndsLong(long A, Pointer B, Pointer I, long len); - - public static native int fillToIndsDouble(double A, Pointer B, Pointer I, long len); - - public static native int fillToInds2DLong(long A, Pointer B, int ldb, Pointer I, int nrows, Pointer J, int ncols); - - public static native int fillToInds3DLong(long A, Pointer B, int ldb, int rdb, Pointer I, int nrows, Pointer J, int ncols, Pointer K, int nd); - - public static native int fillToInds4DLong(long A, Pointer B, int ldb, int rdb, int tdb, Pointer I, int nrows, Pointer J, int ncols, Pointer K, int nk, Pointer L, int nl); - - public static native int copyFromInds(Pointer A, Pointer B, Pointer I, long len); - - public static native int copyFromInds2D(Pointer A, int lda, Pointer B, int ldb, Pointer I, int nrows, Pointer J, int ncols); - - public static native int copyFromInds3D(Pointer A, int lda, int rda, Pointer B, int ldb, int rdb, Pointer I, int nrows, Pointer J, int ncols, Pointer K, int nd); - - public static native int copyFromInds4D(Pointer A, int lda, int rda, int tda, Pointer B, int ldb, int rdb, int tdb, Pointer I, int nrows, Pointer J, int ncols, Pointer K, int nk, Pointer L, int nl); - - public static native int copyFromInds2DLong(Pointer A, int lda, Pointer B, int ldb, Pointer I, int nrows, Pointer J, int ncols); - - public static native int applygfun(Pointer A, Pointer B, int N, int opn); - - public static native int applygdfun(Pointer A, Pointer B, int N, int opn); - - public static native int applygfun2(Pointer A, Pointer B, Pointer C, int N, int opn); - - public static native int applygdfun2(Pointer A, Pointer B, Pointer C, int N, int opn); - - public static native int full(Pointer ir, Pointer ic, Pointer vv, Pointer dd, int nrows, int ncols, int nnz); - - public static native int setval(Pointer A, float vv, int N); - - public static native int setival(Pointer A, int iv, int N); - - public static native int setlval(Pointer A, long iv, int N); - - public static native int kron(Pointer A, Pointer B, Pointer C, int nrA, int ncA, int nrB, int ncB); - - public static native int kroni(Pointer A, Pointer B, Pointer C, int nrA, int ncA, int nrB, int ncB); - - public static native int reduce1op(int nr, int nc, Pointer A, Pointer B, float initv, int opn); - - public static native int reduce1iop(int nr, int nc, Pointer A, Pointer B, int initv, int opn); - - public static native int reduce1lop(int nr, int nc, Pointer A, Pointer B, long initv, int opn); - - public static native int reduce1dop(int nr, int nc, Pointer A, Pointer B, double initv, int opn); - - public static native int reduce2op(int nr, int nc, Pointer A, Pointer B, float initv, int opn); - - public static native int reduce2iop(int nr, int nc, Pointer A, Pointer B, int initv, int opn); - - public static native int reduce2lop(int nr, int nc, Pointer A, Pointer B, long initv, int opn); - - public static native int reduce2dop(int nr, int nc, Pointer A, Pointer B, double initv, int opn); - - public static native int reducebin1op(int nr, int nc, Pointer A, Pointer B, Pointer C, int opb, int opr); - - public static native int reducebin1dop(int nr, int nc, Pointer A, Pointer B, Pointer C, int opb, int opr); - - public static native int reducebin2op(int nr, int nc, Pointer A, Pointer B, Pointer C, int opb, int opr); - - public static native int reducebin2dop(int nr, int nc, Pointer A, Pointer B, Pointer C, int opb, int opr); - - public static native int sdoprow(int nr, int nc, int nnz, Pointer A, Pointer Ac, Pointer B, int len, int op); - - public static native int sdopdrow(int nr, int nc, int nnz, Pointer A, Pointer Ac, Pointer B, int len, int op); - - public static native int sdopcol(int nr, int nc, int nnz, Pointer A, Pointer Ar, Pointer B, int len, int op); - - public static native int sdopdcol(int nr, int nc, int nnz, Pointer A, Pointer Ar, Pointer B, int len, int op); - - public static native int dsmult(int nr, int nc, int nnz, Pointer A, Pointer Bdata, Pointer Bir, Pointer Bic, Pointer C); - - public static native int dsmultTile(int nr, int nc, int kk, int nnz, Pointer A, int lda, Pointer Bdata, Pointer Bir, Pointer Bic, - int broff, int bcoff, Pointer C, int ldc, int transpose); - - public static native int dsmulttune(int nr, int nc, int nnz, Pointer A, Pointer Bdata, Pointer Bir, Pointer Bic, Pointer C, int nblocks, int nthreads); - - public static native int dsmultxtune(int nr, int nc, int nnz, Pointer A, Pointer Bdata, Pointer Bir, Pointer Bic, Pointer C, int nblocks, int ntx, int nty); - - public static native int dsmultT(int nr, int nc, int nnz, Pointer A, Pointer Bdata, Pointer Bir, Pointer Bic, Pointer C); - - public static native void blockSgemm( int transA, int transB, int nr, int nc, int kk, int reps, Pointer A, int lda, int astep, - Pointer B, int ldb, int bstep, Pointer C, int ldc, int cstep, float beta); - - public static native int accum(Pointer I, Pointer J, Pointer V, Pointer S, int m, int nrows); - - public static native int accumI(int I, Pointer J, Pointer V, Pointer S, int m, int nrows); - - public static native int accumJ(Pointer I, int J, Pointer V, Pointer S, int m, int nrows); - - public static native int accumV(Pointer I, Pointer J, float V, Pointer S, int m, int nrows); - - public static native int accumIV(int I, Pointer J, float V, Pointer S, int m, int nrows); - - public static native int accumJV(Pointer I, int J, float V, Pointer S, int m, int nrows); - - public static native int iaccum(Pointer I, Pointer J, Pointer V, Pointer S, int m, int nrows); - - public static native int iaccumI(int I, Pointer J, Pointer V, Pointer S, int m, int nrows); - - public static native int iaccumJ(Pointer I, int J, Pointer V, Pointer S, int m, int nrows); - - public static native int iaccumV(Pointer I, Pointer J, int V, Pointer S, int m, int nrows); - - public static native int iaccumIV(int I, Pointer J, int V, Pointer S, int m, int nrows); - - public static native int iaccumJV(Pointer I, int J, int V, Pointer S, int m, int nrows); - - public static native int laccum(Pointer I, Pointer J, Pointer V, Pointer S, int m, int nrows); - - public static native int laccumI(int I, Pointer J, Pointer V, Pointer S, int m, int nrows); - - public static native int laccumJ(Pointer I, int J, Pointer V, Pointer S, int m, int nrows); - - public static native int laccumV(Pointer I, Pointer J, long V, Pointer S, int m, int nrows); - - public static native int laccumIV(int I, Pointer J, long V, Pointer S, int m, int nrows); - - public static native int laccumJV(Pointer I, int J, long V, Pointer S, int m, int nrows); - - public static native int sumTensor(Pointer A, Pointer B, int m, int n, int k); - - public static native int minTensor(Pointer A, Pointer B, int m, int n, int k); - - public static native int maxTensor(Pointer A, Pointer B, int m, int n, int k); - - public static native int prodTensor(Pointer A, Pointer B, int m, int n, int k); - - public static native int sumTensorI(Pointer A, Pointer B, int m, int n, int k); - - public static native int minTensorI(Pointer A, Pointer B, int m, int n, int k); - - public static native int maxTensorI(Pointer A, Pointer B, int m, int n, int k); - - public static native int prodTensorI(Pointer A, Pointer B, int m, int n, int k); - - public static native int spsum(int nr, int nc, int nnz, Pointer Air, Pointer Aic, Pointer P, Pointer B, int n); - - public static native int dds(int nr, int nnz, Pointer A, Pointer B, Pointer Cir, Pointer Cic, Pointer P); - - public static native int dds0(int nr, int nc, Pointer A, Pointer B, Pointer Cir, Pointer Cjc, Pointer P); - - public static native int LDAgibbs(int nr, int nnz, Pointer A, Pointer B, Pointer AN, Pointer BN, Pointer Cir, Pointer Cic, Pointer P, float nsamps); - - public static native int LDAgibbsx(int nr, int nnz, Pointer A, Pointer B, Pointer Cir, Pointer Cic, Pointer P, Pointer Ms, Pointer Us, int k); - - public static native int treeprod(Pointer trees, Pointer feats, Pointer tpos, Pointer otvs, int nrows, int ncols, int ns, int tstride, int ntrees); - - public static native int treesteps(Pointer trees, Pointer feats, Pointer tpos, Pointer otpos, int nrows, int ncols, int ns, int tstride, int ntrees, int tdepth); - - public static native int icopyt(Pointer iptrs, Pointer in, Pointer out, int stride, int nrows, int ncols); - - public static native int ocopyt(Pointer iptrs, Pointer in, Pointer out, int stride, int nrows, int ncols); - - public static native int ocopytadd(Pointer iptrs, Pointer in, Pointer out, int stride, int nrows, int ncols); - - public static native int ocopytmin(Pointer iptrs, Pointer in, Pointer out, int stride, int nrows, int ncols); - - public static native int transpose(Pointer A, int lda, Pointer B, int ldb, int nr, int nc); - - public static native int cumsumgi(Pointer in, Pointer out, Pointer jc, int nrows, int ncols, int m); - - public static native int cumsumgf(Pointer in, Pointer out, Pointer jc, int nrows, int ncols, int m); - - public static native int maxgi(Pointer in, Pointer out, Pointer outi, Pointer jc, int nrows, int ncols, int m); - - public static native int maxgf(Pointer in, Pointer out, Pointer outi, Pointer jc, int nrows, int ncols, int m); - - public static native int mingi(Pointer in, Pointer out, Pointer outi, Pointer jc, int nrows, int ncols, int m); - - public static native int mingf(Pointer in, Pointer out, Pointer outi, Pointer jc, int nrows, int ncols, int m); - - public static native int maxii(Pointer in, Pointer out, Pointer outi, int nrows, int ncols, int dir); - - public static native int maxil(Pointer in, Pointer out, Pointer outi, int nrows, int ncols, int dir); - - public static native int maxif(Pointer in, Pointer out, Pointer outi, int nrows, int ncols, int dir); - - public static native int minii(Pointer in, Pointer out, Pointer outi, int nrows, int ncols, int dir); - - public static native int minil(Pointer in, Pointer out, Pointer outi, int nrows, int ncols, int dir); - - public static native int minif(Pointer in, Pointer out, Pointer outi, int nrows, int ncols, int dir); - - public static native int embedmat2d(Pointer A, Pointer B, int nrows, int ncols, int sortdown); - - public static native int extractmat2d(Pointer A, Pointer B, int nrows, int ncols); - - public static native int embedmat(Pointer A, Pointer B, Pointer C, int n); - - public static native int extractmat(Pointer A, Pointer B, Pointer C, int n); - - public static native int isort(Pointer A, int n, int asc); - - public static native int fsort(Pointer A, int n, int asc); - - public static native int lsort(Pointer A, int n, int asc); - - public static native int l2sort(Pointer A, int n, int asc); - - public static native int l2sortk(Pointer A, Pointer B, int n, int asc); - - public static native int l4sort(Pointer A, int n, int asc); - - public static native int isortk(Pointer A, Pointer B, int n, int asc); - - public static native int lsortk(Pointer A, Pointer B, int n, int asc); - - public static native int dsortk(Pointer A, Pointer B, int n, int asc); - - public static native int fsorts(Pointer A, Pointer B, int[] jc, int m, int asc); - - public static native int fsort2d(Pointer A, int nrows, int ncols, int asc); - - public static native int fsort2dk(Pointer A, Pointer B, int nrows, int ncols, int asc); - - public static native int i4sort(Pointer A, int ncols, int asc); - - public static native int i3sortk(Pointer A, Pointer B, int ncols, int asc); - - public static native int fsortsizex(int n); - - public static native long fisortcubsize(Pointer inkeys, Pointer outkeys, Pointer invals, Pointer outvals, int nelems, int asc); - - public static native int fisortcub(Pointer inkeys, Pointer outkeys, Pointer invals, Pointer outvals, Pointer temp, long size, int nelems, int asc); - - public static native int lsortsizex(int n); - - public static native int lsortx(Pointer A, Pointer B, Pointer C, Pointer D, Pointer E, Pointer F, int n, int asc); - - public static native int fsort2dx(Pointer A, Pointer B, Pointer C, Pointer D, int nrows, int ncols, int asc); - - public static native int stratify(Pointer strata, int n, Pointer a, Pointer b, Pointer bi, int stride); - - public static native int stratifycounts(Pointer strata, int n, Pointer a, Pointer bi); - - public static native int radixcounts(Pointer a, int n, int digit, Pointer bi); - - public static native int distances(Pointer A, int lda, Pointer B, int ldb, Pointer C, int ldc, int d, int nrows, int ncols, float p); - - public static native int maxsumx(Pointer A, int lda, Pointer B, int ldb, Pointer C, int ldc, int d, int nrows, int ncols); - - public static native int dmv(Pointer A, int nrows, int ncols, Pointer B, Pointer C, int trans); - - public static native int veccmp(Pointer A, Pointer B, Pointer C); - - public static native int hammingdists(Pointer A, Pointer B, Pointer W, Pointer OP, Pointer OW, int n); - - public static native int poissonrnd(int n, Pointer Lambda, Pointer Out, int nthreads, long seed, long offset); - - public static native int binornd(int nrows, int ncols, Pointer prob, int atype, Pointer N, int ctype, Pointer Out, long seed, long offset); - - public static native int gamrnd(int nrows, int ncols, Pointer prob, int atype, Pointer B, int btype, Pointer Out, long seed, long offset); - - public static native int collectLVec(Pointer pkeys, Pointer okeys, Pointer pvals, Pointer ovals, int n); - - public static native int mergeLVecs(Pointer akeys, Pointer avals, Pointer bkeys, Pointer bvals, Pointer okeys, Pointer ovals, int n1, int n2); - - public static native int cumsumc(int nrows, int ncols, Pointer A, Pointer B); - - public static native int cumsumByKeyFF(Pointer A, Pointer B, Pointer out, long len); - - public static native int cumsumByKeyFI(Pointer A, Pointer B, Pointer out, long len); - - public static native int cumsumByKeyII(Pointer A, Pointer B, Pointer out, long len); - - public static native int cumsumByKeyFL(Pointer A, Pointer B, Pointer out, long len); - - public static native int cumsumByKeyIL(Pointer A, Pointer B, Pointer out, long len); - - public static native int cummaxByKeyFF(Pointer A, Pointer B, Pointer out, long len); - - public static native int cummaxByKeyFI(Pointer A, Pointer B, Pointer out, long len); - - public static native int cummaxByKeyII(Pointer A, Pointer B, Pointer out, long len); - - public static native int cummaxByKeyFL(Pointer A, Pointer B, Pointer out, long len); - - public static native int cummaxByKeyIL(Pointer A, Pointer B, Pointer out, long len); - - public static native int cumminByKeyFF(Pointer A, Pointer B, Pointer out, long len); - - public static native int cumminByKeyFI(Pointer A, Pointer B, Pointer out, long len); - - public static native int cumminByKeyII(Pointer A, Pointer B, Pointer out, long len); - - public static native int cumminByKeyFL(Pointer A, Pointer B, Pointer out, long len); - - public static native int cumminByKeyIL(Pointer A, Pointer B, Pointer out, long len); - - public static native int cumsumByKeyFFx(Pointer A, Pointer B, Pointer out, int nrows, int ncols); - - public static native int cumsum2ByKeyFF(Pointer A, Pointer B, Pointer out, int nrows, int ncols); - - public static native int reverse(Pointer A, Pointer out, long len); - - public static native void spermute(int d1, int d2, int d3, Pointer in, Pointer out); - - public static native int myCublasSgemmStridedBatched(cublasHandle handle, int transa, int transb, int m, int n, int k, float alpha, - Pointer A, int lda, int astep, Pointer B, int ldb, int bstep, - float beta, Pointer C, int ldc, int cstep, int reps); - - public static native int myCublasSgemmStridedBatched4D(cublasHandle handle, int transa, int transb, int m, int n, int k, float alpha, - Pointer A, int lda, int astep1, int astep2, Pointer B, int ldb, int bstep1, int bstep2, - float beta, Pointer C, int ldc, int cstep1, int cstep2, int reps1, int reps2); -} diff --git a/src/main/java/edu/berkeley/bid/CUMATD.java b/src/main/java/edu/berkeley/bid/CUMATD.java deleted file mode 100755 index 6d8b1327..00000000 --- a/src/main/java/edu/berkeley/bid/CUMATD.java +++ /dev/null @@ -1,183 +0,0 @@ -package edu.berkeley.bid; -import jcuda.Pointer; -import jcuda.jcublas.cublasHandle; - -public final class CUMATD { - - private CUMATD() {} - - static { - LibUtils.loadLibrary("bidmatcuda"); - } - - public static native int IntToDouble(Pointer A, Pointer B, int N); - - public static native int FloatToDouble(Pointer A, Pointer B, int N); - - public static native int toInt(Pointer A, Pointer B, int N); - - public static native int applyiop(Pointer A, int Anrows, int Ancols, Pointer B, int Bnrows, int Bncols, Pointer C, int opn); - - public static native int copyToInds(Pointer A, Pointer B, Pointer I, long len); - - public static native int copyToInds2D(Pointer A, int lda, Pointer B, int ldb, Pointer I, int nrows, Pointer J, int ncols); - - public static native int copyToInds3D(Pointer A, int lda, int rda, Pointer B, int ldb, int rdb, Pointer I, int nrows, Pointer J, int ncols, Pointer K, int nd); - - public static native int copyToInds4D(Pointer A, int lda, int rda, int tda, Pointer B, int ldb, int rdb, int tdb, Pointer I, int nrows, Pointer J, int ncols, Pointer K, int nk, Pointer L, int nl); - - public static native int copyFromInds(Pointer A, Pointer B, Pointer I, long len); - - public static native int copyFromInds2D(Pointer A, int lda, Pointer B, int ldb, Pointer I, int nrows, Pointer J, int ncols); - - public static native int copyFromInds3D(Pointer A, int lda, int rda, Pointer B, int ldb, int rdb, Pointer I, int nrows, Pointer J, int ncols, Pointer K, int nd); - - public static native int copyFromInds4D(Pointer A, int lda, int rda, int tda, Pointer B, int ldb, int rdb, int tdb, Pointer I, int nrows, Pointer J, int ncols, Pointer K, int nk, Pointer L, int nl); - - public static native int fillToInds(double A, Pointer B, Pointer I, long len); - - public static native int fillToInds2D(double A, Pointer B, int ldb, Pointer I, int nrows, Pointer J, int ncols); - - public static native int fillToInds3D(double A, Pointer B, int ldb, int rdb, Pointer I, int nrows, Pointer J, int ncols, Pointer K, int nd); - - public static native int fillToInds4D(double A, Pointer B, int ldb, int rdb, int tdb, Pointer I, int nrows, Pointer J, int ncols, Pointer K, int nk, Pointer L, int nl); - - public static native int full(Pointer ir, Pointer ic, Pointer vv, Pointer dd, int nrows, int ncols, int nnz); - - public static native int setval(Pointer A, double vv, int N); - - public static native int setival(Pointer A, int iv, int N); - - public static native int dsmult(int nr, int nc, int nnz, Pointer A, Pointer Bdata, Pointer Bir, Pointer Bic, Pointer C); - - public static native int dsmulttune(int nr, int nc, int nnz, Pointer A, Pointer Bdata, Pointer Bir, Pointer Bic, Pointer C, int nblocks, int nthreads); - - public static native int dsmultxtune(int nr, int nc, int nnz, Pointer A, Pointer Bdata, Pointer Bir, Pointer Bic, Pointer C, int nblocks, int ntx, int nty); - - public static native int dsmultT(int nr, int nc, int nnz, Pointer A, Pointer Bdata, Pointer Bir, Pointer Bic, Pointer C); - - public static native int accum(Pointer I, Pointer J, Pointer V, Pointer S, int m, int nrows); - - public static native int accumI(int I, Pointer J, Pointer V, Pointer S, int m, int nrows); - - public static native int accumJ(Pointer I, int J, Pointer V, Pointer S, int m, int nrows); - - public static native int accumV(Pointer I, Pointer J, double V, Pointer S, int m, int nrows); - - public static native int accumIV(int I, Pointer J, double V, Pointer S, int m, int nrows); - - public static native int accumJV(Pointer I, int J, double V, Pointer S, int m, int nrows); - - public static native int spsum(int nr, int nc, int nnz, Pointer Air, Pointer Aic, Pointer P, Pointer B, int n); - - public static native int sumTensor(Pointer A, Pointer B, int m, int n, int k); - - public static native int minTensor(Pointer A, Pointer B, int m, int n, int k); - - public static native int maxTensor(Pointer A, Pointer B, int m, int n, int k); - - public static native int prodTensor(Pointer A, Pointer B, int m, int n, int k); - - public static native int sumTensorL(Pointer A, Pointer B, int m, int n, int k); - - public static native int minTensorL(Pointer A, Pointer B, int m, int n, int k); - - public static native int maxTensorL(Pointer A, Pointer B, int m, int n, int k); - - public static native int prodTensorL(Pointer A, Pointer B, int m, int n, int k); - - public static native int dds(int nr, int nnz, Pointer A, Pointer B, Pointer Cir, Pointer Cic, Pointer P); - - public static native int dds0(int nr, int nc, Pointer A, Pointer B, Pointer Cir, Pointer Cjc, Pointer P); - - public static native int LDAgibbs(int nr, int nnz, Pointer A, Pointer B, Pointer AN, Pointer BN, Pointer Cir, Pointer Cic, Pointer P, double nsamps); - - public static native int LDAgibbsx(int nr, int nnz, Pointer A, Pointer B, Pointer Cir, Pointer Cic, Pointer P, Pointer Ms, Pointer Us, int k); - - public static native int treeprod(Pointer trees, Pointer feats, Pointer tpos, Pointer otvs, int nrows, int ncols, int ns, int tstride, int ntrees); - - public static native int treesteps(Pointer trees, Pointer feats, Pointer tpos, Pointer otpos, int nrows, int ncols, int ns, int tstride, int ntrees, int tdepth); - - public static native int icopyt(Pointer iptrs, Pointer in, Pointer out, int stride, int nrows, int ncols); - - public static native int ocopyt(Pointer iptrs, Pointer in, Pointer out, int stride, int nrows, int ncols); - - public static native int ocopytadd(Pointer iptrs, Pointer in, Pointer out, int stride, int nrows, int ncols); - - public static native int ocopytmin(Pointer iptrs, Pointer in, Pointer out, int stride, int nrows, int ncols); - - public static native int transpose(Pointer A, int lda, Pointer B, int ldb, int nr, int nc); - - public static native int cumsumgf(Pointer in, Pointer out, Pointer jc, int nrows, int ncols, int m); - - public static native int maxgf(Pointer in, Pointer out, Pointer outi, Pointer jc, int nrows, int ncols, int m); - - public static native int mingf(Pointer in, Pointer out, Pointer outi, Pointer jc, int nrows, int ncols, int m); - - public static native int maxif(Pointer in, Pointer out, Pointer outi, int nrows, int ncols, int dir); - - public static native int minif(Pointer in, Pointer out, Pointer outi, int nrows, int ncols, int dir); - - public static native int embedmat2d(Pointer A, Pointer B, int nrows, int ncols, int sortdown); - - public static native int extractmat2d(Pointer A, Pointer B, int nrows, int ncols); - - public static native int embedmat(Pointer A, Pointer B, Pointer C, int n); - - public static native int extractmat(Pointer A, Pointer B, Pointer C, int n); - - public static native int fsort(Pointer A, int n, int asc); - - public static native int dsortk(Pointer A, Pointer B, int n, int asc); - - public static native int fsorts(Pointer A, Pointer B, int[] jc, int m, int asc); - - public static native int fsort2d(Pointer A, Pointer B, int nrows, int ncols, int asc); - - public static native int fsortsizex(int n); - - public static native int fsort2dx(Pointer A, Pointer B, Pointer C, Pointer D, Pointer E, Pointer F, int nrows, int ncols, int asc); - - public static native int stratify(Pointer strata, int n, Pointer a, Pointer b, Pointer bi, int stride); - - public static native int stratifycounts(Pointer strata, int n, Pointer a, Pointer bi); - - public static native int radixcounts(Pointer a, int n, int digit, Pointer bi); - - public static native int distances(Pointer A, int lda, Pointer B, int ldb, Pointer C, int ldc, int d, int nrows, int ncols, double p); - - public static native int maxsumx(Pointer A, int lda, Pointer B, int ldb, Pointer C, int ldc, int d, int nrows, int ncols); - - public static native int dmv(Pointer A, int nrows, int ncols, Pointer B, Pointer C, int trans); - - public static native int veccmp(Pointer A, Pointer B, Pointer C); - - public static native int hammingdists(Pointer A, Pointer B, Pointer W, Pointer OP, Pointer OW, int n); - - public static native int cumsumc(int nrows, int ncols, Pointer A, Pointer B); - - public static native int cumsumByKeyDD(Pointer A, Pointer B, Pointer out, long len); - - public static native int cumsumByKeyLL(Pointer A, Pointer B, Pointer out, long len); - - public static native int cummaxByKeyDD(Pointer A, Pointer B, Pointer out, long len); - - public static native int cummaxByKeyLL(Pointer A, Pointer B, Pointer out, long len); - - public static native int cumminByKeyDD(Pointer A, Pointer B, Pointer out, long len); - - public static native int cumminByKeyLL(Pointer A, Pointer B, Pointer out, long len); - - public static native int reverse(Pointer A, Pointer out, long len); - - public static native void dpermute(int d1, int d2, int d3, Pointer in, Pointer out); - - public static native int myCublasDgemmStridedBatched(cublasHandle handle, int transa, int transb, int m, int n, int k, double alpha, - Pointer A, int lda, int astep, Pointer B, int ldb, int bstep, - double beta, Pointer C, int ldc, int cstep, int reps); - - public static native int myCublasDgemmStridedBatched4D(cublasHandle handle, int transa, int transb, int m, int n, int k, double alpha, - Pointer A, int lda, int astep1, int astep2, Pointer B, int ldb, int bstep1, int bstep2, - double beta, Pointer C, int ldc, int cstep1, int cstep2, int reps1, int reps2); - -} diff --git a/src/main/java/edu/berkeley/bid/NCCL.java b/src/main/java/edu/berkeley/bid/NCCL.java deleted file mode 100644 index 329605d1..00000000 --- a/src/main/java/edu/berkeley/bid/NCCL.java +++ /dev/null @@ -1,35 +0,0 @@ -package edu.berkeley.bid; -import jcuda.Pointer; -import jcuda.runtime.cudaStream_t; - -public final class NCCL { - - private long handle = 0; - - public NCCL() {} - - protected void finalize() { - if (handle != 0) { - ncclCommDestroy(this); - handle = 0; - } - } - - static { -// LibUtils.loadLibrary("bidmatcpu", true); - } - - public static native int hasNCCL(); - - public static native int ncclCommInitAll(NCCL [] comms); - - public static native void ncclCommDestroy(NCCL comm); - - /** - * Should pass float pointers for send/recv buffers. - * Enumeration types are defined here: https://github.com/NVIDIA/nccl/blob/master/src/nccl.h - */ - - public static native int ncclAllReduce(Pointer send, Pointer recv, int count, int datatype, int op, NCCL comm, cudaStream_t stream); - -} diff --git a/src/main/java/edu/berkeley/bid/SLATEC.java b/src/main/java/edu/berkeley/bid/SLATEC.java index 257c4adc..9b220907 100644 --- a/src/main/java/edu/berkeley/bid/SLATEC.java +++ b/src/main/java/edu/berkeley/bid/SLATEC.java @@ -1,5 +1,5 @@ package edu.berkeley.bid; -import jcuda.Pointer; +//import jcuda.Pointer; public final class SLATEC { @@ -7,10 +7,10 @@ private SLATEC() {} public static native int applyfun(float[] X, float[] Y, int N, int opn); - public static native int applygfun(Pointer X, Pointer Y, int N, int opn); + // public static native int applygfun(Pointer X, Pointer Y, int N, int opn); public static native int applyfun2(int nrows, int ncols, float[] A, int ar, int ac, float[] B, int br, int bc, float[] C, int cc, int opn); - public static native int applygfun2(int nrows, int ncols, Pointer A, int ar, int ac, Pointer B, int br, int bc, Pointer C, int cc, int opn); + // public static native int applygfun2(int nrows, int ncols, Pointer A, int ar, int ac, Pointer B, int br, int bc, Pointer C, int cc, int opn); } diff --git a/src/main/scala/BIDMat/BMat.scala b/src/main/scala/BIDMat/BMat.scala index e3c085a6..1ad0b2c3 100644 --- a/src/main/scala/BIDMat/BMat.scala +++ b/src/main/scala/BIDMat/BMat.scala @@ -1100,7 +1100,7 @@ case class BMat(dims0:Array[Int], val data:Array[Byte]) extends DenseMat[Byte](d /* * Specialize to GMats to help the type system. */ - def * (b : GMat) = Mop_Times.op(this, b, null) +/* def * (b : GMat) = Mop_Times.op(this, b, null) def *^ (b : GMat) = Mop_TimesT.op(this, b, null) def xT (b : GMat) = Mop_TimesT.op(this, b, null) def Tx (b : GMat) = Mop_TTimes.op(this, b, null) @@ -1131,6 +1131,7 @@ case class BMat(dims0:Array[Int], val data:Array[Byte]) extends DenseMat[Byte](d def >= (b : GMat) = Mop_GE.op(this, b, null) def <= (b : GMat) = Mop_LE.op(this, b, null) def != (b : GMat) = Mop_NE.op(this, b, null) + */ /* * Operators whose second arg is generic. @@ -1336,6 +1337,7 @@ class BPair(val omat:Mat, val mat:BMat) extends BIDMat.Pair(omat, mat) { /* * Specialize to GMat */ + /* def * (b : GMat) = Mop_Times.op(mat, b, omat) def *^ (b : GMat) = Mop_TimesT.op(mat, b, omat) def xT (b : GMat) = Mop_TimesT.op(mat, b, omat) @@ -1363,7 +1365,7 @@ class BPair(val omat:Mat, val mat:BMat) extends BIDMat.Pair(omat, mat) { def >= (b : GMat) = Mop_GE.op(mat, b, omat) def <= (b : GMat) = Mop_LE.op(mat, b, omat) def != (b : GMat) = Mop_NE.op(mat, b, omat) - +*/ /* * Generics */ @@ -1461,7 +1463,7 @@ object BMat { def apply(x:Mat):BMat = { val out:BMat = x match { - case _:GIMat | _:DMat | _:FMat | _:IMat => BMat.newOrCheckBMat(x.dims, null, x.GUID, "BMat".##); + case _:DMat | _:FMat | _:IMat => BMat.newOrCheckBMat(x.dims, null, x.GUID, "BMat".##); case ff:BMat => ff; case dd:DenseMat[Byte] @ unchecked => {val out = new BMat(dd.dims.data, dd._data); out.setGUID(dd.GUID); out} case _ => throw new RuntimeException("IMat apply unknown argument"); diff --git a/src/main/scala/BIDMat/Benchmarks.scala b/src/main/scala/BIDMat/Benchmarks.scala index 183daa67..e2bceb81 100755 --- a/src/main/scala/BIDMat/Benchmarks.scala +++ b/src/main/scala/BIDMat/Benchmarks.scala @@ -133,7 +133,7 @@ object AltaVista { if (j % 3 == 0) printf(".") } Mat.ilexsort2(ii.data, jj.data) - saveAs("/big/Yahoo/G2/test.mat",ii,"ii",jj,"jj") +// saveAs("/big/Yahoo/G2/test.mat",ii,"ii",jj,"jj") val avals = ones(ii.length.toInt, 1).data sofar = SparseMat.remdups(ii.data, jj.data, avals) val ss = new SMat(maxv, maxv, sofar, SparseMat.incInds(jj.data), new Array[Int](maxv+1), avals) diff --git a/src/main/scala/BIDMat/DFunctions.scala b/src/main/scala/BIDMat/DFunctions.scala index b9aa7570..45cd09d1 100644 --- a/src/main/scala/BIDMat/DFunctions.scala +++ b/src/main/scala/BIDMat/DFunctions.scala @@ -24,7 +24,7 @@ object DFunctions { /** Sort a set of keys ascending along a given direction '''dir''': 1=columns, 2=rows, 0=smart. */ def sort(keys:DMat, dir:Int):DMat = { keys match { - case gkeys:GDMat => throw new RuntimeException("GPU sort on Doubles not supported"); +// case gkeys:GDMat => throw new RuntimeException("GPU sort on Doubles not supported"); case _ => DMat(DenseMat.sort(keys, dir, true)); } } @@ -32,7 +32,7 @@ object DFunctions { /** Sort a set of keys ascending. */ def sort(keys:DMat):DMat = { keys match { - case gkeys:GDMat => throw new RuntimeException("GPU sort on Doubles not supported"); +// case gkeys:GDMat => throw new RuntimeException("GPU sort on Doubles not supported"); case _ => DMat(DenseMat.sort(keys, 0, true)) } } @@ -40,7 +40,7 @@ object DFunctions { /** Sort a set of keys ascending, and return sorted keys and indices. */ def sort2(keys:DMat):(DMat, IMat) = { keys match { - case gkeys:GDMat => throw new RuntimeException("GPU sort on Doubles not supported"); +// case gkeys:GDMat => throw new RuntimeException("GPU sort on Doubles not supported"); case _ => {val (d,i) = DenseMat.sort2(keys, true); (DMat(d), i)} } } @@ -48,7 +48,7 @@ object DFunctions { /** Sort a set of keys and return sorted keys and indices along a given direction: 1=columns, 2=rows, 0=smart */ def sort2(keys:DMat, dir:Int):(DMat, IMat) = { keys match { - case gkeys:GDMat => throw new RuntimeException("GPU sort on Doubles not supported"); +// case gkeys:GDMat => throw new RuntimeException("GPU sort on Doubles not supported"); case _ => {val (d,i) = DenseMat.sort2(keys, dir, true); (DMat(d), i)} } } @@ -56,7 +56,7 @@ object DFunctions { /** Sort a set of keys descending along a given direction: 1=columns, 2=rows, 0=smart. */ def sortdown(keys:DMat, dir:Int):DMat = { keys match { - case gkeys:GDMat => throw new RuntimeException("GPU sort on Doubles not supported"); +// case gkeys:GDMat => throw new RuntimeException("GPU sort on Doubles not supported"); case _ => DMat(DenseMat.sort(keys, dir, false)); } } @@ -64,7 +64,7 @@ object DFunctions { /** Sort a set of keys descending. */ def sortdown(keys:DMat):DMat = { keys match { - case gkeys:GDMat => throw new RuntimeException("GPU sort on Doubles not supported"); +// case gkeys:GDMat => throw new RuntimeException("GPU sort on Doubles not supported"); case _ => DMat(DenseMat.sort(keys, 0, false)) } } @@ -72,7 +72,7 @@ object DFunctions { /** Sort a set of keys descending and return sorted keys and indices. */ def sortdown2(keys:DMat):(DMat, IMat) = { keys match { - case gkeys:GDMat => throw new RuntimeException("GPU sort on Doubles not supported"); +// case gkeys:GDMat => throw new RuntimeException("GPU sort on Doubles not supported"); case _ => {val (d,i) = DenseMat.sort2(keys, false); (DMat(d), i)} } } @@ -80,7 +80,7 @@ object DFunctions { /** Sort a set of keys and return sorted keys and indices along a given direction: 1=columns, 2=rows, 0=smart */ def sortdown2(keys:DMat, dir:Int):(DMat, IMat) = { keys match { - case gkeys:GDMat => throw new RuntimeException("GPU sort on Doubles not supported"); +// case gkeys:GDMat => throw new RuntimeException("GPU sort on Doubles not supported"); case _ => {val (d,i) = DenseMat.sort2(keys, dir, false); (DMat(d), i)} } } @@ -101,8 +101,8 @@ object DFunctions { /** Accumulate (row, col, value) tuples from inds \\ vals. nr and nc are row and column bounds */ def accum(inds:IMat, vals:DMat, nr:Int, nc:Int):DMat = { (inds, vals) match { - case (ginds:GIMat, fvals:DMat) => GDFunctions.accum(ginds, GDMat(fvals), null, nr, nc); - case (finds:IMat, gvals:GDMat) => GDFunctions.accum(GIMat(finds), gvals, null, nr, nc); +// case (ginds:GIMat, fvals:DMat) => GDFunctions.accum(ginds, GDMat(fvals), null, nr, nc); +// case (finds:IMat, gvals:GDMat) => GDFunctions.accum(GIMat(finds), gvals, null, nr, nc); case _ => DMat(DenseMat.accum(inds, vals, nr, nc)) } } @@ -117,7 +117,7 @@ object DFunctions { /** Accumulate (row, col, value) tuples from inds \\ vals. nr and nc are row and column bounds */ def accum(inds:IMat, v:Double, nr:Int, nc:Int) = { inds match { - case ginds:GIMat => GDFunctions.accum(ginds, v, null, nr, nc); +// case ginds:GIMat => GDFunctions.accum(ginds, v, null, nr, nc); case _ => DMat(DenseMat.accum(inds, DMat.delem(v), nr, nc)); } } @@ -134,72 +134,72 @@ object DFunctions { def min(a:DMat, b:DMat, out:Mat) = { (a, b) match { - case (aa:GDMat, bb:DMat) => GDFunctions.min(aa, GDMat(b), out); - case (aa:DMat, bb:GDMat) => GDFunctions.min(GDMat(a), bb, out); +// case (aa:GDMat, bb:DMat) => GDFunctions.min(aa, GDMat(b), out); +// case (aa:DMat, bb:GDMat) => GDFunctions.min(GDMat(a), bb, out); case _ => a.ddMatOpv(b, DMat.vecMinFun, op_min, out); } } def max(a:DMat, b:DMat, out:Mat) = { (a, b) match { - case (aa:GDMat, bb:DMat) => GDFunctions.max(aa, GDMat(b), out); - case (aa:DMat, bb:GDMat) => GDFunctions.max(GDMat(a), bb, out); +// case (aa:GDMat, bb:DMat) => GDFunctions.max(aa, GDMat(b), out); +// case (aa:DMat, bb:GDMat) => GDFunctions.max(GDMat(a), bb, out); case _ => a.ddMatOpv(b, DMat.vecMaxFun, op_max, out); } } def min(a:DMat, b:Double, out:Mat) = { a match { - case aa:GDMat=> GDFunctions.min(aa, GDMat.elem(b), out); +// case aa:GDMat=> GDFunctions.min(aa, GDMat.elem(b), out); case _ => a.ddMatOpScalarv(b, DMat.vecMinFun, out); } } def max(a:DMat, b:Double, out:Mat) = { a match { - case aa:GDMat=> GDFunctions.max(aa, GDMat.elem(b), out); +// case aa:GDMat=> GDFunctions.max(aa, GDMat.elem(b), out); case _ => a.ddMatOpScalarv(b, DMat.vecMaxFun, out); } } def maxi(a:DMat, n:Int, out:Mat) = { a match { - case aa:GDMat => GDFunctions.maxi(aa, n, out); +// case aa:GDMat => GDFunctions.maxi(aa, n, out); case _ => a.ddReduceOpv(n, DMat.idFun, DMat.vecMaxFun, out); } } def mini(a:DMat, n:Int, out:Mat) = { a match { - case aa:GDMat => GDFunctions.mini(aa, n, out); +// case aa:GDMat => GDFunctions.mini(aa, n, out); case _ => a.ddReduceOpv(n, DMat.idFun, DMat.vecMinFun, out); } } def sum(a:DMat, n:Int, out:Mat) = { a match { - case aa:GDMat => GDFunctions.sum(aa, n, out); +// case aa:GDMat => GDFunctions.sum(aa, n, out); case _ => a.ddReduceOpv(n, DMat.idFun, DMat.vecAddFun, out); } } def prod(a:DMat, n:Int, out:Mat) = { a match { - case aa:GDMat => GDFunctions.prod(aa, n, out); +// case aa:GDMat => GDFunctions.prod(aa, n, out); case _ => a.ddReduceOpv(n, DMat.idFun, DMat.vecMulFun, out); } } def cumsum(a:DMat, n:Int, out:Mat) = { a match { - case aa:GDMat => GDFunctions.cumsum(aa, n, out); +// case aa:GDMat => GDFunctions.cumsum(aa, n, out); case _ => a.ddReduceAll(n, DMat.idFun, DMat.sumFun, out); } } def maxi2(a:DMat,d:Int):(DMat,IMat) = { a match { - case aa:GDMat => GDFunctions.maxi2(aa, null, null, d); +// case aa:GDMat => GDFunctions.maxi2(aa, null, null, d); case _ => { val (m,ii)=a.ggOpt2(d,DMat.gtPred); (DMat(m), ii) @@ -209,7 +209,7 @@ object DFunctions { def mini2(a:DMat,d:Int):(DMat,IMat) = { a match { - case aa:GDMat => GDFunctions.mini2(aa, null, null, d); +// case aa:GDMat => GDFunctions.mini2(aa, null, null, d); case _ => { val (m,ii)=a.ggOpt2(d,DMat.ltPred); (DMat(m), ii) @@ -557,7 +557,7 @@ object DFunctions { def sign(a:DMat):DMat = sign(a, null); def sign(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.sign(aa, out); +// case aa:GDMat => GDFunctions.sign(aa, out); case _ => applyDFun(a, out, null, signumFun, 1L); } } @@ -567,7 +567,7 @@ object DFunctions { def abs(a:DMat):DMat = abs(a, null); def abs(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.abs(aa, out); +// case aa:GDMat => GDFunctions.abs(aa, out); case _ => applyDFun(a, out, vdAbsFun, absFun, 1L); } } @@ -577,7 +577,7 @@ object DFunctions { def exp(a:DMat):DMat = exp(a, null); def exp(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.exp(aa, out); +// case aa:GDMat => GDFunctions.exp(aa, out); case _ => applyDFunV(a, out, vdExpFunMKL, vdExpFun, 1L); } } @@ -587,7 +587,7 @@ object DFunctions { def expm1(a:DMat):DMat = expm1(a, null); def expm1(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.expm1(aa, out); +// case aa:GDMat => GDFunctions.expm1(aa, out); case _ => applyDFun(a, out, vdExpm1Fun, expm1Fun, 10L); } } @@ -597,7 +597,7 @@ object DFunctions { def sqrt(a:DMat):DMat = sqrt(a, null); def sqrt(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.sqrt(aa, out); +// case aa:GDMat => GDFunctions.sqrt(aa, out); case _ => applyDFun(a, out, vdSqrtFun, sqrtFun, 10L); } } @@ -607,7 +607,7 @@ object DFunctions { def ln(a:DMat):DMat = ln(a, null); def ln(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.ln(aa, out); +// case aa:GDMat => GDFunctions.ln(aa, out); case _ => applyDFun(a, out, vdLnFun, lnFun, 10L); } } @@ -617,7 +617,7 @@ object DFunctions { def log10(a:DMat):DMat = log10(a, null); def log10(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.log10(aa, out); +// case aa:GDMat => GDFunctions.log10(aa, out); case _ => applyDFun(a, out, vdLog10Fun, log10Fun, 10L); } } @@ -627,7 +627,7 @@ object DFunctions { def log1p(a:DMat):DMat = log1p(a, null); def log1p(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.log1p(aa, out); +// case aa:GDMat => GDFunctions.log1p(aa, out); case _ => applyDFun(a, out, vdLog1pFun, log1pFun, 10L); } } @@ -637,7 +637,7 @@ object DFunctions { def cos(a:DMat):DMat = cos(a, null); def cos(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.cos(aa, out); +// case aa:GDMat => GDFunctions.cos(aa, out); case _ => applyDFun(a, out, vdCosFun, cosFun, 10L); } } @@ -647,7 +647,7 @@ object DFunctions { def sin(a:DMat):DMat = sin(a, null); def sin(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.sin(aa, out); +// case aa:GDMat => GDFunctions.sin(aa, out); case _ => applyDFun(a, out, vdSinFun, sinFun, 10L); } } @@ -657,7 +657,7 @@ object DFunctions { def tan(a:DMat):DMat = tan(a, null); def tan(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.tan(aa, out); +// case aa:GDMat => GDFunctions.tan(aa, out); case _ => applyDFun(a, out, vdTanFun, tanFun, 10L); } } @@ -667,7 +667,7 @@ object DFunctions { def cosh(a:DMat):DMat = cosh(a, null); def cosh(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.cosh(aa, out); +// case aa:GDMat => GDFunctions.cosh(aa, out); case _ => applyDFun(a, out, vdCoshFun, coshFun, 10L); } } @@ -677,7 +677,7 @@ object DFunctions { def sinh(a:DMat):DMat = sinh(a, null); def sinh(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.sinh(aa, out); +// case aa:GDMat => GDFunctions.sinh(aa, out); case _ => applyDFun(a, out, vdSinhFun, sinhFun, 10L); } } @@ -687,7 +687,7 @@ object DFunctions { def tanh(a:DMat):DMat = tanh(a, null); def tanh(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.tanh(aa, out); +// case aa:GDMat => GDFunctions.tanh(aa, out); case _ => applyDFun(a, out, vdTanhFun, tanhFun, 10L); } } @@ -697,7 +697,7 @@ object DFunctions { def acos(a:DMat):DMat = acos(a, null); def acos(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.acos(aa, out); +// case aa:GDMat => GDFunctions.acos(aa, out); case _ => applyDFun(a, out, vdAcosFun, acosFun, 10L); } } @@ -707,7 +707,7 @@ object DFunctions { def asin(a:DMat):DMat = asin(a, null); def asin(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.asin(aa, out); +// case aa:GDMat => GDFunctions.asin(aa, out); case _ => applyDFun(a, out, vdAsinFun, asinFun, 10L); } } @@ -717,7 +717,7 @@ object DFunctions { def atan(a:DMat):DMat = atan(a, null); def atan(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.atan(aa, out); +// case aa:GDMat => GDFunctions.atan(aa, out); case _ => applyDFun(a, out, vdAtanFun, atanFun, 10L); } } @@ -727,7 +727,7 @@ object DFunctions { def acosh(a:DMat):DMat = acosh(a, null); def acosh(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.acosh(aa, out); +// case aa:GDMat => GDFunctions.acosh(aa, out); case _ => applyDFun(a, out, vdAcoshFun, acoshFun, 10L); } } @@ -737,7 +737,7 @@ object DFunctions { def asinh(a:DMat):DMat = asinh(a, null); def asinh(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.asinh(aa, out); +// case aa:GDMat => GDFunctions.asinh(aa, out); case _ => applyDFun(a, out, vdAsinhFun, asinhFun, 10L); } } @@ -747,7 +747,7 @@ object DFunctions { def atanh(a:DMat):DMat = atanh(a, null); def atanh(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.atanh(aa, out); +// case aa:GDMat => GDFunctions.atanh(aa, out); case _ => applyDFun(a, out, vdAtanhFun, atanhFun, 10L); } } @@ -757,7 +757,7 @@ object DFunctions { def erf(a:DMat):DMat = erf(a, null); def erf(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.erf(aa, out); +// case aa:GDMat => GDFunctions.erf(aa, out); case _ => applyDFun(a, out, vdErfFun, erfFun, 10L); } } @@ -767,7 +767,7 @@ object DFunctions { def erfinv(a:DMat):DMat = erfinv(a, null); def erfinv(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.erfinv(aa, out); +// case aa:GDMat => GDFunctions.erfinv(aa, out); case _ => applyDFun(a, out, vdErfInvFun, erfinvFun, 10L); } } @@ -777,7 +777,7 @@ object DFunctions { def erfc(a:DMat):DMat = erfc(a, null); def erfc(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.erfc(aa, out); +// case aa:GDMat => GDFunctions.erfc(aa, out); case _ => applyDFun(a, out, vdErfcFun, erfcFun, 10L); } } @@ -787,7 +787,7 @@ object DFunctions { def erfcinv(a:DMat):DMat = erfcinv(a, null); def erfcinv(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.erfcinv(aa, out); +// case aa:GDMat => GDFunctions.erfcinv(aa, out); case _ => applyDFun(a, out, vdErfcInvFun, erfcInvFun, 10L); } } @@ -798,7 +798,7 @@ object DFunctions { def normcdf(a:DMat):DMat = normcdf(a, null); def normcdf(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.normcdf(aa, out); +// case aa:GDMat => GDFunctions.normcdf(aa, out); case _ => applyDFun(a, out, vdCdfNormFun, normcdfFun, 10L); } } @@ -808,7 +808,7 @@ object DFunctions { def normcdfinv(a:DMat):DMat = normcdfinv(a, null); def normcdfinv(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.normcdfinv(aa, out); +// case aa:GDMat => GDFunctions.normcdfinv(aa, out); case _ => applyDFun(a, out, vdCdfNormInvFun, normcdfinvFun, 10L); } } @@ -818,7 +818,7 @@ object DFunctions { def gamma(a:DMat):DMat = gamma(a, null); def gamma(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.gamma(aa, out); +// case aa:GDMat => GDFunctions.gamma(aa, out); case _ => applyDFun(a, out, vdTGammaFun, gammaFun, 10L); } } @@ -832,7 +832,7 @@ object DFunctions { def gammaln(a:DMat):DMat = gammaln(a, null); def gammaln(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.gammaln(aa, out); +// case aa:GDMat => GDFunctions.gammaln(aa, out); case _ => applyDFun(a, out, vdLGammaFun, gammalnFun, 10L); } } @@ -842,7 +842,7 @@ object DFunctions { def ceil(a:DMat):DMat = ceil(a, null); def ceil(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.ceil(aa, out); +// case aa:GDMat => GDFunctions.ceil(aa, out); case _ => applyDFun(a, out, vdCeilFun, ceilFun, 1L); } } @@ -852,7 +852,7 @@ object DFunctions { def floor(a:DMat):DMat = floor(a, null); def floor(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.floor(aa, out); +// case aa:GDMat => GDFunctions.floor(aa, out); case _ => applyDFun(a, out, vdFloorFun, floorFun, 1L); } } @@ -862,7 +862,7 @@ object DFunctions { def round(a:DMat):DMat = round(a, null); def round(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.round(aa, out); +// case aa:GDMat => GDFunctions.round(aa, out); case _ => applyDFun(a, out, vdRoundFun, roundFun, 1L); } } @@ -872,7 +872,7 @@ object DFunctions { def trunc(a:DMat):DMat = trunc(a, null); def trunc(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.trunc(aa, out); +// case aa:GDMat => GDFunctions.trunc(aa, out); case _ => applyDFun(a, out, vdTruncFun, truncFun, 1L); } } @@ -886,7 +886,7 @@ object DFunctions { def logistic(a:DMat):DMat = logistic(a, null); def logistic(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.logistic(aa, out); +// case aa:GDMat => GDFunctions.logistic(aa, out); case _ => applyDFun(a, out, vdLogisticFun, logisticFun, 10L); } } @@ -896,7 +896,7 @@ object DFunctions { def atan2(a:DMat, b:DMat):DMat = atan2(a, b, null); def atan2(a:DMat, b:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.atan2(aa, GDMat(b), out); +// case aa:GDMat => GDFunctions.atan2(aa, GDMat(b), out); case _ => applyD2Fun(a, b, out, vdAtan2Fun, atan2Fun, 10L); } } @@ -906,7 +906,7 @@ object DFunctions { def pow(a:DMat, b:DMat):DMat = pow(a, b, null); def pow(a:DMat, b:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.pow(aa, GDMat(b), out); +// case aa:GDMat => GDFunctions.pow(aa, GDMat(b), out); case _ => applyD2Fun(a, b, out, vdPowFun, powFun, 10L); } } @@ -915,7 +915,7 @@ object DFunctions { def powx(a:DMat, b:Double):DMat = powx(a, b, null); def powx(a:DMat, b:Double, out:Mat) = { a match { - case aa:GDMat => throw new RuntimeException("powx not implemented on GPU") +// case aa:GDMat => throw new RuntimeException("powx not implemented on GPU") case _ => applyD2xFun(a, b, out, vdPowxFun, powFun, 10L); } } @@ -924,7 +924,7 @@ object DFunctions { def exppsi(a:DMat):DMat = exppsi(a, null); def exppsi(a:DMat, out:Mat) = { a match { - case aa:GDMat => GDFunctions.exppsi(aa, out); +// case aa:GDMat => GDFunctions.exppsi(aa, out); case _ => applyDFun(a, out, null, exppsiFun, 3L); } } diff --git a/src/main/scala/BIDMat/DMat.scala b/src/main/scala/BIDMat/DMat.scala index 55be2874..711f322f 100755 --- a/src/main/scala/BIDMat/DMat.scala +++ b/src/main/scala/BIDMat/DMat.scala @@ -562,8 +562,8 @@ case class DMat(dims0:Array[Int], val data:Array[Double]) extends DenseMat[Doubl def ddMatOpv(b: Mat, f:(Array[Double],Int,Int,Array[Double],Int,Int,Array[Double],Int,Int,Int) => Double, optype:Int, out:Mat) = (this, b) match { - case (aa:GDMat, bb:DMat) => aa.gOp(bb, out, optype); - case (aa:DMat, bb:GDMat) => GDMat(this).gOp(bb, out, optype); +// case (aa:GDMat, bb:DMat) => aa.gOp(bb, out, optype); +// case (aa:DMat, bb:GDMat) => GDMat(this).gOp(bb, out, optype); case (aa:DMat, bb:DMat) => DMat(ggMatOpv(bb, f, out)); case _ => throw new RuntimeException("unsupported operation "+f+" on "+this+" and "+b) } @@ -598,7 +598,7 @@ case class DMat(dims0:Array[Int], val data:Array[Double]) extends DenseMat[Doubl throw new RuntimeException("DMat copyTo dimensions mismatch") } a match { - case out:GDMat => out.copyFrom(this); +// case out:GDMat => out.copyFrom(this); case out:DMat => System.arraycopy(data, 0, out.data, 0, length); case out:FMat => {Mat.copyToFloatArray(data, 0, out.data, 0, length)}; case out:IMat => {Mat.copyToIntArray(data, 0, out.data, 0, length)}; @@ -658,8 +658,8 @@ case class DMat(dims0:Array[Int], val data:Array[Double]) extends DenseMat[Doubl def fDMult(b:DMat, outmat:Mat):DMat = { (this, b) match { - case (aa:GDMat, bb:DMat) => aa.GMult(b, outmat); - case (aa:DMat, bb:GDMat) => GDMat(aa).GMult(bb, outmat); +// case (aa:GDMat, bb:DMat) => aa.GMult(b, outmat); +// case (aa:DMat, bb:GDMat) => GDMat(aa).GMult(bb, outmat); case _ => fDMultDD(b, outmat); } } @@ -721,8 +721,8 @@ case class DMat(dims0:Array[Int], val data:Array[Double]) extends DenseMat[Doubl def fSMult(b:SDMat, outmat:Mat):DMat = { (this, b) match { - case (aa:GDMat, bb:SDMat) => aa.GSMult(b, outmat); - case (aa:DMat, bb:GSDMat) => GDMat(aa).GSMult(bb, outmat); +// case (aa:GDMat, bb:SDMat) => aa.GSMult(b, outmat); +// case (aa:DMat, bb:GSDMat) => GDMat(aa).GSMult(bb, outmat); case _ => fSMultF(b, outmat); } } @@ -778,8 +778,8 @@ case class DMat(dims0:Array[Int], val data:Array[Double]) extends DenseMat[Doubl def multT(b:SDMat, outmat:Mat):DMat = { (this, b) match { - case (aa:GDMat, bb:SDMat) => aa.GSMultT(b, outmat); - case (aa:DMat, bb:GSDMat) => GDMat(aa).GSMultT(bb, outmat); +// case (aa:GDMat, bb:SDMat) => aa.GSMultT(b, outmat); +// case (aa:DMat, bb:GSDMat) => GDMat(aa).GSMultT(bb, outmat); case _ => multTS(b, outmat); } } @@ -800,8 +800,8 @@ case class DMat(dims0:Array[Int], val data:Array[Double]) extends DenseMat[Doubl def multT(b:DMat, outmat:Mat):DMat = { (this, b) match { - case (aa:GDMat, bb:DMat) => aa.GMultT(b, outmat); - case (aa:DMat, bb:GDMat) => GDMat(aa).GMultT(bb, outmat); +// case (aa:GDMat, bb:DMat) => aa.GMultT(b, outmat); +// case (aa:DMat, bb:GDMat) => GDMat(aa).GMultT(bb, outmat); case _ => multTD(b, outmat); } } @@ -822,8 +822,8 @@ case class DMat(dims0:Array[Int], val data:Array[Double]) extends DenseMat[Doubl def Tmult(b:DMat, outmat:Mat):DMat = { (this, b) match { - case (aa:GDMat, bb:DMat) => aa.GTMult(b, outmat); - case (aa:DMat, bb:GDMat) => GDMat(aa).GTMult(bb, outmat); +// case (aa:GDMat, bb:DMat) => aa.GTMult(b, outmat); +// case (aa:DMat, bb:GDMat) => GDMat(aa).GTMult(bb, outmat); case _ => TmultD(b, outmat); } } @@ -1609,7 +1609,8 @@ case class DMat(dims0:Array[Int], val data:Array[Double]) extends DenseMat[Doubl /* * Specialize to GMats to help the type system. - */ + */ + /* def * (b : GMat) = Mop_Times.op(this, b, null) def *^ (b : GMat) = Mop_TimesT.op(this, b, null) def xT (b : GMat) = Mop_TimesT.op(this, b, null) @@ -1643,6 +1644,7 @@ case class DMat(dims0:Array[Int], val data:Array[Double]) extends DenseMat[Doubl def != (b : GMat) = Mop_NE.op(this, b, null) def max (b : GMat) = Mop_Max.op(this, b, null) def min (b : GMat) = Mop_Min.op(this, b, null) + */ /* * Operators whose second arg is generic. */ @@ -1839,6 +1841,7 @@ class DPair (val omat:Mat, val mat:DMat) extends Pair(omat, mat) { /* * Specialize to GMat */ + /* def * (b : GMat) = Mop_Times.op(mat, b, omat) def *^ (b : GMat) = Mop_TimesT.op(mat, b, omat) def xT (b : GMat) = Mop_TimesT.op(mat, b, omat) @@ -1867,7 +1870,8 @@ class DPair (val omat:Mat, val mat:DMat) extends Pair(omat, mat) { def <= (b : GMat) = Mop_LE.op(mat, b, omat) def != (b : GMat) = Mop_NE.op(mat, b, omat) def max (b : GMat) = Mop_Max.op(mat, b, omat) - def min (b : GMat) = Mop_Min.op(mat, b, omat) + def min (b : GMat) = Mop_Min.op(mat, b, omat) + */ /* * Generics */ @@ -1932,14 +1936,14 @@ object DMat { def apply(x:Mat):DMat = { val out:DMat = x match { - case _:GMat | _:GDMat | _:FMat | _:IMat | _:LMat | _:BMat | _:SDMat => DMat.newOrCheckDMat(x.dims, null, x.GUID, "DMat".##); + case _:FMat | _:IMat | _:LMat | _:BMat | _:SDMat => DMat.newOrCheckDMat(x.dims, null, x.GUID, "DMat".##); case ff:DMat => ff; case dd:DenseMat[Double] @ unchecked => {val out = new DMat(dd.dims.data, dd._data); out.setGUID(dd.GUID); out} case _ => throw new RuntimeException("DMat apply unknown argument"); } x match { - case gg:GMat => {val ff = gg.toFMat(null); Mat.copyToDoubleArray(ff.data, 0, out.data, 0, ff.length)} - case gg:GDMat => gg.copyTo(out); +// case gg:GMat => {val ff = gg.toFMat(null); Mat.copyToDoubleArray(ff.data, 0, out.data, 0, ff.length)} +// case gg:GDMat => gg.copyTo(out); case _:DMat => {} case ff:FMat => {Mat.copyToDoubleArray(ff.data, 0, out.data, 0, ff.length)} case ii:IMat => {Mat.copyToDoubleArray(ii.data, 0, out.data, 0, ii.length)} diff --git a/src/main/scala/BIDMat/Dict.scala b/src/main/scala/BIDMat/Dict.scala index 8e57d4d6..e8d71208 100755 --- a/src/main/scala/BIDMat/Dict.scala +++ b/src/main/scala/BIDMat/Dict.scala @@ -1,7 +1,7 @@ package BIDMat import scala.collection.mutable.{Map,SynchronizedMap,HashMap} import MatFunctions._ -import edu.berkeley.bid.CUMAT +//import edu.berkeley.bid.CUMAT @SerialVersionUID(100L) class Dict(val cstr:CSMat) extends Serializable { diff --git a/src/main/scala/BIDMat/FFilter.scala b/src/main/scala/BIDMat/FFilter.scala index f760c804..68441c95 100644 --- a/src/main/scala/BIDMat/FFilter.scala +++ b/src/main/scala/BIDMat/FFilter.scala @@ -4,8 +4,8 @@ import edu.berkeley.bid.CBLAS._; import edu.berkeley.bid.UTILS import SciFunctions._; import edu.berkeley.bid.MurmurHash3.MurmurHash3_x64_64 -import jcuda.jcudnn._ -import jcuda.jcudnn.JCudnn._ +//import jcuda.jcudnn._ +//import jcuda.jcudnn.JCudnn._ // // Basic CPU convolutional Filter class. @@ -37,8 +37,8 @@ FMat(dataDims0.data, data0) with Filter { val pad = if (pad0.asInstanceOf[AnyRef] != null) pad0 else izeros(1,inDims.length); val outPad = if (outPad0.asInstanceOf[AnyRef] != null) outPad0 else izeros(1,inDims.length); val dataDims = dataDims0; - var tensorFormat = cudnnTensorFormat.CUDNN_TENSOR_NHWC; - var convType = cudnnConvolutionMode.CUDNN_CROSS_CORRELATION; +// var tensorFormat = cudnnTensorFormat.CUDNN_TENSOR_NHWC; +// var convType = cudnnConvolutionMode.CUDNN_CROSS_CORRELATION; var timer = 0f; @@ -984,7 +984,7 @@ object FFilter { new FFilter(indims, outdims, stride, pad, outpad, indims(0,0->(indims.length-1)) \ outdims(0), new Array[Float]((indims dotr outdims).v))} } - def apply(g:GFilter):FFilter = { +/* def apply(g:GFilter):FFilter = { val outnd = FMat.newOrCheckFMat(g.dims, null, g.GUID, "FFilter".##); val out = new FFilter(g.inDims, g.outDims, g.stride, g.pad, g.outPad, g.dataDims, outnd.data); GMat.GPUtoCPUarraycopy(g.pdata, 0, out.data, 0, g.length, "FFilter apply"); @@ -993,7 +993,7 @@ object FFilter { out.setGUID(MurmurHash3_x64_64(Array(g.GUID), "FFilter apply".##)); out; } - +*/ def FFilter1D(w:Int, nstride:Int, npad:Int, noutpad:Int):FFilter = { val inDims = irow(w); val outDims = irow(1); diff --git a/src/main/scala/BIDMat/FFunctions.scala b/src/main/scala/BIDMat/FFunctions.scala index 82b33edc..b2a1983c 100644 --- a/src/main/scala/BIDMat/FFunctions.scala +++ b/src/main/scala/BIDMat/FFunctions.scala @@ -22,7 +22,7 @@ object FFunctions { def norm(a:FMat) = { a match { - case aa:GMat => GFunctions.norm(aa); +// case aa:GMat => GFunctions.norm(aa); case _ => math.sqrt(sdot(a.length, a.data, 1, a.data, 1)).toFloat; } } @@ -30,9 +30,9 @@ object FFunctions { /** Sort a set of keys ascending along a given direction '''dir''': 1=columns, 2=rows, 0=smart. */ def sort(keys:FMat, dir:Int):FMat = { keys match { - case gkeys:GMat => if (dir < 2) { - GFunctions.sort(gkeys); - } else throw new RuntimeException("GPU sort across columns not supported"); +// case gkeys:GMat => if (dir < 2) { +// GFunctions.sort(gkeys); +// } else throw new RuntimeException("GPU sort across columns not supported"); case _ => FMat(DenseMat.sort(keys, dir, true)); } } @@ -40,7 +40,7 @@ object FFunctions { /** Sort a set of keys ascending. */ def sort(keys:FMat):FMat = { keys match { - case gkeys:GMat => GFunctions.sort(gkeys); +// case gkeys:GMat => GFunctions.sort(gkeys); case _ => FMat(DenseMat.sort(keys, 0, true)) } } @@ -48,7 +48,7 @@ object FFunctions { /** Sort a set of keys ascending, and return sorted keys and indices. */ def sort2(keys:FMat):(FMat, IMat) = { keys match { - case gkeys:GMat => GFunctions.sort2(gkeys) +// case gkeys:GMat => GFunctions.sort2(gkeys) case _ => {val (d,i) = DenseMat.sort2(keys, true); (FMat(d), i)} } } @@ -56,9 +56,9 @@ object FFunctions { /** Sort a set of keys and return sorted keys and indices along a given direction: 1=columns, 2=rows, 0=smart */ def sort2(keys:FMat, dir:Int):(FMat, IMat) = { keys match { - case gkeys:GMat => if (dir < 2) { - GFunctions.sort2(gkeys); - } else throw new RuntimeException("GPU sort across columns not supported"); +// case gkeys:GMat => if (dir < 2) { +// GFunctions.sort2(gkeys); +// } else throw new RuntimeException("GPU sort across columns not supported"); case _ => {val (d,i) = DenseMat.sort2(keys, dir, true); (FMat(d), i)} } } @@ -66,9 +66,9 @@ object FFunctions { /** Sort a set of keys descending along a given direction: 1=columns, 2=rows, 0=smart. */ def sortdown(keys:FMat, dir:Int):FMat = { keys match { - case gkeys:GMat => if (dir < 2) { - GFunctions.sortdown(gkeys); - } else throw new RuntimeException("GPU sort across columns not supported"); +// case gkeys:GMat => if (dir < 2) { +// GFunctions.sortdown(gkeys); +// } else throw new RuntimeException("GPU sort across columns not supported"); case _ => FMat(DenseMat.sort(keys, dir, false)); } } @@ -76,7 +76,7 @@ object FFunctions { /** Sort a set of keys descending. */ def sortdown(keys:FMat):FMat = { keys match { - case gkeys:GMat => GFunctions.sortdown(gkeys); +// case gkeys:GMat => GFunctions.sortdown(gkeys); case _ => FMat(DenseMat.sort(keys, 0, false)) } } @@ -84,7 +84,7 @@ object FFunctions { /** Sort a set of keys descending and return sorted keys and indices. */ def sortdown2(keys:FMat):(FMat, IMat) = { keys match { - case gkeys:GMat => GFunctions.sortdown2(gkeys); +// case gkeys:GMat => GFunctions.sortdown2(gkeys); case _ => {val (d,i) = DenseMat.sort2(keys, false); (FMat(d), i)} } } @@ -92,9 +92,9 @@ object FFunctions { /** Sort a set of keys and return sorted keys and indices along a given direction: 1=columns, 2=rows, 0=smart */ def sortdown2(keys:FMat, dir:Int):(FMat, IMat) = { keys match { - case gkeys:GMat => if (dir < 2) { - GFunctions.sortdown2(gkeys); - } else throw new RuntimeException("GPU sort across columns not supported"); +// case gkeys:GMat => if (dir < 2) { +// GFunctions.sortdown2(gkeys); +// } else throw new RuntimeException("GPU sort across columns not supported"); case _ => {val (d,i) = DenseMat.sort2(keys, dir, false); (FMat(d), i)} } } @@ -114,8 +114,8 @@ object FFunctions { /** Accumulate (row, col, value) tuples from inds \\ vals. nr and nc are row and column bounds */ def accum(inds:IMat, vals:FMat, nr:Int, nc:Int) = { (inds, vals) match { - case (ginds:GIMat, fvals:FMat) => GFunctions.accum(ginds, GMat(fvals), null, nr, nc); - case (finds:IMat, gvals:GMat) => GFunctions.accum(GIMat(finds), gvals, null, nr, nc); +// case (ginds:GIMat, fvals:FMat) => GFunctions.accum(ginds, GMat(fvals), null, nr, nc); +// case (finds:IMat, gvals:GMat) => GFunctions.accum(GIMat(finds), gvals, null, nr, nc); case _ => FMat(DenseMat.accum(inds, vals, nr, nc)) } } @@ -130,7 +130,7 @@ object FFunctions { /** Accumulate (row, col, value) tuples from inds \\ vals. nr and nc are row and column bounds */ def accum(inds:IMat, v:Float, nr:Int, nc:Int) = { inds match { - case ginds:GIMat => GFunctions.accum(ginds, v, null, nr, nc); +// case ginds:GIMat => GFunctions.accum(ginds, v, null, nr, nc); case _ => FMat(DenseMat.accum(inds, FMat.elem(v), nr, nc)); } } @@ -143,72 +143,72 @@ object FFunctions { def min(a:FMat, b:FMat, out:Mat) = { (a, b) match { - case (aa:GMat, bb:FMat) => GFunctions.min(aa, GMat(b), out); - case (aa:FMat, bb:GMat) => GFunctions.min(GMat(a), bb, out); +// case (aa:GMat, bb:FMat) => GFunctions.min(aa, GMat(b), out); +// case (aa:FMat, bb:GMat) => GFunctions.min(GMat(a), bb, out); case _ => a.ffMatOpv(b, FMat.vecMinFun, op_min, out); } } def max(a:FMat, b:FMat, out:Mat) = { (a, b) match { - case (aa:GMat, bb:FMat) => GFunctions.max(aa, GMat(b), out); - case (aa:FMat, bb:GMat) => GFunctions.max(GMat(a), bb, out); +// case (aa:GMat, bb:FMat) => GFunctions.max(aa, GMat(b), out); +// case (aa:FMat, bb:GMat) => GFunctions.max(GMat(a), bb, out); case _ => a.ffMatOpv(b, FMat.vecMaxFun, op_max, out); } } def min(a:FMat, b:Float, out:Mat) = { a match { - case aa:GMat=> GFunctions.min(aa, GMat.elem(b), out); +// case aa:GMat=> GFunctions.min(aa, GMat.elem(b), out); case _ => a.ffMatOpScalarv(b, FMat.vecMinFun, op_min, out); } } def max(a:FMat, b:Float, out:Mat) = { a match { - case aa:GMat=> GFunctions.max(aa, GMat.elem(b), out); +// case aa:GMat=> GFunctions.max(aa, GMat.elem(b), out); case _ => a.ffMatOpScalarv(b, FMat.vecMaxFun, op_max, out); } } def maxi(a:FMat, n:Int, out:Mat) = { a match { - case aa:GMat => GFunctions.maxi(aa, n, out); +// case aa:GMat => GFunctions.maxi(aa, n, out); case _ => a.ffReduceOpv(n, FMat.idFun, FMat.vecMaxFun, out); } } def mini(a:FMat, n:Int, out:Mat) = { a match { - case aa:GMat => GFunctions.mini(aa, n, out); +// case aa:GMat => GFunctions.mini(aa, n, out); case _ => a.ffReduceOpv(n, FMat.idFun, FMat.vecMinFun, out); } } def sum(a:FMat, n:Int, out:Mat) = { a match { - case aa:GMat => GFunctions.sum(aa, n, out); +// case aa:GMat => GFunctions.sum(aa, n, out); case _ => a.ffReduceOpv(n, FMat.idFun, FMat.vecAddFun, out); } } def prod(a:FMat, n:Int, out:Mat) = { a match { - case aa:GMat => GFunctions.prod(aa, n, out); +// case aa:GMat => GFunctions.prod(aa, n, out); case _ => a.ffReduceOpv(n, FMat.idFun, FMat.vecMulFun, out); } } def cumsum(a:FMat, n:Int, out:Mat) = { a match { - case aa:GMat => GFunctions.cumsum(aa, n, out); +// case aa:GMat => GFunctions.cumsum(aa, n, out); case _ => a.ffReduceAll(n, FMat.idFun, FMat.sumFun, out); } } def maxi2(a:FMat,d:Int):(FMat,IMat) = { a match { - case aa:GMat => GFunctions.maxi2(aa, null, null, d); +// case aa:GMat => GFunctions.maxi2(aa, null, null, d); case _ => { val (m,ii)=a.ggOpt2(d,FMat.gtPred); (FMat(m), ii) @@ -218,7 +218,7 @@ object FFunctions { def mini2(a:FMat,d:Int):(FMat,IMat) = { a match { - case aa:GMat => GFunctions.mini2(aa, null, null, d); +// case aa:GMat => GFunctions.mini2(aa, null, null, d); case _ => { val (m,ii)=a.ggOpt2(d,FMat.ltPred); (FMat(m), ii) @@ -228,16 +228,16 @@ object FFunctions { def rand(minv:Float, maxv:Float, out:FMat):FMat = { out match { - case aa:GMat => { - GFunctions.rand(aa); - if (maxv - minv != 1.0f) { - aa ~ aa * (maxv - minv); - } - if (minv != 0) { - aa ~ aa + minv; - } - aa; - } +// case aa:GMat => { +// GFunctions.rand(aa); +// if (maxv - minv != 1.0f) { +// aa ~ aa * (maxv - minv); +// } +// if (minv != 0) { +// aa ~ aa + minv; +// } +// aa; +// } case _ => { if (Mat.useMKLRand) { vsRngUniform( METHOD, stream, out.length, out.data, minv, maxv ); @@ -255,7 +255,7 @@ object FFunctions { def normrnd(mu:Float, sig:Float, out:FMat):FMat = { out match { - case aa:GMat => GFunctions.normrnd(mu, sig, aa); +// case aa:GMat => GFunctions.normrnd(mu, sig, aa); case _ => { if (Mat.useMKLRand) { vsRngGaussian(METHOD, stream, out.length, out.data, mu, sig ); @@ -274,7 +274,7 @@ object FFunctions { def poissrnd(lambda:FMat, out:IMat):IMat = { checkSizes(lambda, out); (lambda, out) match { - case (glambda:GMat, gout:GIMat) => GFunctions.poissrnd(glambda, gout); +// case (glambda:GMat, gout:GIMat) => GFunctions.poissrnd(glambda, gout); case _ => { if (Mat.useMKLRand) { viRngPoissonV( METHOD, stream, out.length, out.data, DMat(lambda).data ); @@ -292,7 +292,7 @@ object FFunctions { def poissrnd(lambda:Double, out:IMat):IMat = { out match { - case gout:GIMat => GFunctions.poissrnd(GMat.elem(lambda), gout); +// case gout:GIMat => GFunctions.poissrnd(GMat.elem(lambda), gout); case _ => { if (Mat.useMKLRand) { viRngPoisson( METHOD, stream, out.length, out.data, lambda ); @@ -309,7 +309,7 @@ object FFunctions { def gamrnd(shape:Float, scale:Float, out:FMat):FMat = { out match { - case aa:GMat => GFunctions.gamrnd(GMat.elem(shape), GMat.elem(scale), aa); +// case aa:GMat => GFunctions.gamrnd(GMat.elem(shape), GMat.elem(scale), aa); case _ => { if (Mat.useMKLRand) { vsRngGamma( METHOD, stream, out.length, out.data, shape, 0, scale ); @@ -327,7 +327,7 @@ object FFunctions { def gamrnd(shape:FMat, scale:FMat, out:FMat):FMat = { (shape, scale, out) match { - case (gshape:GMat, gscale:GMat, gout:GMat) => GFunctions.gamrnd(gshape, gscale, gout); +// case (gshape:GMat, gscale:GMat, gout:GMat) => GFunctions.gamrnd(gshape, gscale, gout); case _ => Random.gamrnd(shape, scale, out, myrand); } out; @@ -335,7 +335,7 @@ object FFunctions { def laprnd(a:Float, b:Float, out:FMat):FMat = { out match { - case aa:GMat => throw new RuntimeException("laprnd not implemented for GMats"); +// case aa:GMat => throw new RuntimeException("laprnd not implemented for GMats"); case _ => { vsRngLaplace( METHOD, stream, out.length, out.data, a, b ); Mat.nflops += 20L*out.length; @@ -346,7 +346,7 @@ object FFunctions { def cauchyrnd(a:Float, b:Float, out:FMat):FMat = { out match { - case aa:GMat => throw new RuntimeException("cauchyrnd not implemented for GMats"); +// case aa:GMat => throw new RuntimeException("cauchyrnd not implemented for GMats"); case _ => { if (Mat.useMKLRand) { vsRngCauchy( METHOD, stream, out.length, out.data, a, b ); @@ -363,7 +363,7 @@ object FFunctions { def exprnd(a:Float, b:Float, out:FMat):FMat = { out match { - case aa:GMat => throw new RuntimeException("exprnd not implemented for GMats"); +// case aa:GMat => throw new RuntimeException("exprnd not implemented for GMats"); case _ => { if (Mat.useMKLRand) { vsRngExponential( METHOD, stream, out.length, out.data, a, b ); @@ -380,7 +380,7 @@ object FFunctions { def betarnd(p:Float, q:Float, out:FMat):FMat = { out match { - case aa:GMat => throw new RuntimeException("betarnd not implemented for GMats"); +// case aa:GMat => throw new RuntimeException("betarnd not implemented for GMats"); case _ => { vsRngBeta( METHOD, stream, out.length, out.data, p, q, 0, 1 ); Mat.nflops += 20L*out.length; @@ -391,7 +391,7 @@ object FFunctions { def binornd(k:Int, p:Double, out:IMat):IMat = { out match { - case aa:GIMat => GFunctions.binornd(GIMat.elem(k), GMat.elem(p), aa); +// case aa:GIMat => GFunctions.binornd(GIMat.elem(k), GMat.elem(p), aa); case _ => { if (Mat.useMKLRand) { viRngBinomial( METHOD, stream, out.length, out.data, k, p ); @@ -408,7 +408,7 @@ object FFunctions { def binornd(k:IMat, p:FMat, out:IMat):IMat = { (k, p, out) match { - case (gk:GIMat, gp:GMat, gout:GIMat) => GFunctions.binornd(gk, gp, gout); +// case (gk:GIMat, gp:GMat, gout:GIMat) => GFunctions.binornd(gk, gp, gout); case _ => { var i = 0; while (i < out.length) {out.data(i) = acmrand.nextBinomial(k.data(i), p.data(i)).toInt; i += 1;} Mat.nflops += 20L*out.length; @@ -419,7 +419,7 @@ object FFunctions { def bernrnd(p:Double, out:IMat):IMat = { out match { - case aa:GIMat => throw new RuntimeException("bernrnd not implemented for GMats"); +// case aa:GIMat => throw new RuntimeException("bernrnd not implemented for GMats"); case _ => { if (Mat.useMKLRand) { viRngBernoulli( METHOD, stream, out.length, out.data, p ); @@ -436,7 +436,7 @@ object FFunctions { def geornd(p:Double, out:IMat):IMat = { out match { - case aa:GIMat => throw new RuntimeException("geornd not implemented for GMats"); +// case aa:GIMat => throw new RuntimeException("geornd not implemented for GMats"); case _ => { if (Mat.useMKLRand) { viRngGeometric( METHOD, stream, out.length, out.data, p ); @@ -453,7 +453,7 @@ object FFunctions { def nbinrnd(a:Double, p:Double, out:IMat):IMat = { out match { - case aa:GIMat => throw new RuntimeException("nbinrnd not implemented for GMats"); +// case aa:GIMat => throw new RuntimeException("nbinrnd not implemented for GMats"); case _ => { if (Mat.useMKLRand) { viRngNegbinomial( METHOD, stream, out.length, out.data, a, p ); @@ -561,7 +561,7 @@ object FFunctions { def sign(a:FMat):FMat = sign(a, null); def sign(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.sign(aa, out); +// case aa:GMat => GFunctions.sign(aa, out); case _ => applySFun(a, out, null, signumFun, 1L); } } @@ -571,7 +571,7 @@ object FFunctions { def abs(a:FMat):FMat = abs(a, null); def abs(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.abs(aa, out); +// case aa:GMat => GFunctions.abs(aa, out); case _ => applySFun(a, out, vsAbsFun, absFun, 1L); } } @@ -581,7 +581,7 @@ object FFunctions { def exp(a:FMat):FMat = exp(a, null); def exp(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.exp(aa, out); +// case aa:GMat => GFunctions.exp(aa, out); case _ => applySFunV(a, out, vsExpFunMKL, vsExpFun, 1L); } } @@ -591,7 +591,7 @@ object FFunctions { def expm1(a:FMat):FMat = expm1(a, null); def expm1(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.expm1(aa, out); +// case aa:GMat => GFunctions.expm1(aa, out); case _ => applySFun(a, out, vsExpm1Fun, expm1Fun, 10L); } } @@ -601,7 +601,7 @@ object FFunctions { def sqrt(a:FMat):FMat = sqrt(a, null); def sqrt(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.sqrt(aa, out); +// case aa:GMat => GFunctions.sqrt(aa, out); case _ => applySFun(a, out, vsSqrtFun, sqrtFun, 10L); } } @@ -611,7 +611,7 @@ object FFunctions { def ln(a:FMat):FMat = ln(a, null); def ln(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.ln(aa, out); +// case aa:GMat => GFunctions.ln(aa, out); case _ => applySFun(a, out, vsLnFun, lnFun, 10L); } } @@ -621,7 +621,7 @@ object FFunctions { def log10(a:FMat):FMat = log10(a, null); def log10(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.log10(aa, out); +// case aa:GMat => GFunctions.log10(aa, out); case _ => applySFun(a, out, vsLog10Fun, log10Fun, 10L); } } @@ -631,7 +631,7 @@ object FFunctions { def log1p(a:FMat):FMat = log1p(a, null); def log1p(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.log1p(aa, out); +// case aa:GMat => GFunctions.log1p(aa, out); case _ => applySFun(a, out, vsLog1pFun, log1pFun, 10L); } } @@ -641,7 +641,7 @@ object FFunctions { def cos(a:FMat):FMat = cos(a, null); def cos(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.cos(aa, out); +// case aa:GMat => GFunctions.cos(aa, out); case _ => applySFun(a, out, vsCosFun, cosFun, 10L); } } @@ -651,7 +651,7 @@ object FFunctions { def sin(a:FMat):FMat = sin(a, null); def sin(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.sin(aa, out); +// case aa:GMat => GFunctions.sin(aa, out); case _ => applySFun(a, out, vsSinFun, sinFun, 10L); } } @@ -661,7 +661,7 @@ object FFunctions { def tan(a:FMat):FMat = tan(a, null); def tan(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.tan(aa, out); +// case aa:GMat => GFunctions.tan(aa, out); case _ => applySFun(a, out, vsTanFun, tanFun, 10L); } } @@ -671,7 +671,7 @@ object FFunctions { def cosh(a:FMat):FMat = cosh(a, null); def cosh(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.cosh(aa, out); +// case aa:GMat => GFunctions.cosh(aa, out); case _ => applySFun(a, out, vsCoshFun, coshFun, 10L); } } @@ -681,7 +681,7 @@ object FFunctions { def sinh(a:FMat):FMat = sinh(a, null); def sinh(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.sinh(aa, out); +// case aa:GMat => GFunctions.sinh(aa, out); case _ => applySFun(a, out, vsSinhFun, sinhFun, 10L); } } @@ -691,7 +691,7 @@ object FFunctions { def tanh(a:FMat):FMat = tanh(a, null); def tanh(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.tanh(aa, out); +// case aa:GMat => GFunctions.tanh(aa, out); case _ => applySFun(a, out, vsTanhFun, tanhFun, 10L); } } @@ -701,7 +701,7 @@ object FFunctions { def acos(a:FMat):FMat = acos(a, null); def acos(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.acos(aa, out); +// case aa:GMat => GFunctions.acos(aa, out); case _ => applySFun(a, out, vsAcosFun, acosFun, 10L); } } @@ -711,7 +711,7 @@ object FFunctions { def asin(a:FMat):FMat = asin(a, null); def asin(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.asin(aa, out); +// case aa:GMat => GFunctions.asin(aa, out); case _ => applySFun(a, out, vsAsinFun, asinFun, 10L); } } @@ -721,7 +721,7 @@ object FFunctions { def atan(a:FMat):FMat = atan(a, null); def atan(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.atan(aa, out); +// case aa:GMat => GFunctions.atan(aa, out); case _ => applySFun(a, out, vsAtanFun, atanFun, 10L); } } @@ -731,7 +731,7 @@ object FFunctions { def acosh(a:FMat):FMat = acosh(a, null); def acosh(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.acosh(aa, out); +// case aa:GMat => GFunctions.acosh(aa, out); case _ => applySFun(a, out, vsAcoshFun, acoshFun, 10L); } } @@ -741,7 +741,7 @@ object FFunctions { def asinh(a:FMat):FMat = asinh(a, null); def asinh(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.asinh(aa, out); +// case aa:GMat => GFunctions.asinh(aa, out); case _ => applySFun(a, out, vsAsinhFun, asinhFun, 10L); } } @@ -751,7 +751,7 @@ object FFunctions { def atanh(a:FMat):FMat = atanh(a, null); def atanh(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.atanh(aa, out); +// case aa:GMat => GFunctions.atanh(aa, out); case _ => applySFun(a, out, vsAtanhFun, atanhFun, 10L); } } @@ -761,7 +761,7 @@ object FFunctions { def erf(a:FMat):FMat = erf(a, null); def erf(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.erf(aa, out); +// case aa:GMat => GFunctions.erf(aa, out); case _ => applySFun(a, out, vsErfFun, erfFun, 10L); } } @@ -771,7 +771,7 @@ object FFunctions { def erfinv(a:FMat):FMat = erfinv(a, null); def erfinv(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.erfinv(aa, out); +// case aa:GMat => GFunctions.erfinv(aa, out); case _ => applySFun(a, out, vsErfInvFun, erfinvFun, 10L); } } @@ -781,7 +781,7 @@ object FFunctions { def erfc(a:FMat):FMat = erfc(a, null); def erfc(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.erfc(aa, out); +// case aa:GMat => GFunctions.erfc(aa, out); case _ => applySFun(a, out, vsErfcFun, erfcFun, 10L); } } @@ -791,7 +791,7 @@ object FFunctions { def erfcinv(a:FMat):FMat = erfcinv(a, null); def erfcinv(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.erfcinv(aa, out); +// case aa:GMat => GFunctions.erfcinv(aa, out); case _ => applySFun(a, out, vsErfcInvFun, erfcInvFun, 10L); } } @@ -802,7 +802,7 @@ object FFunctions { def normcdf(a:FMat):FMat = normcdf(a, null); def normcdf(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.normcdf(aa, out); +// case aa:GMat => GFunctions.normcdf(aa, out); case _ => applySFun(a, out, vsCdfNormFun, normcdfFun, 10L); } } @@ -812,7 +812,7 @@ object FFunctions { def normcdfinv(a:FMat):FMat = normcdfinv(a, null); def normcdfinv(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.normcdfinv(aa, out); +// case aa:GMat => GFunctions.normcdfinv(aa, out); case _ => applySFun(a, out, vsCdfNormInvFun, normcdfinvFun, 10L); } } @@ -822,7 +822,7 @@ object FFunctions { def gamma(a:FMat):FMat = gamma(a, null); def gamma(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.gamma(aa, out); +// case aa:GMat => GFunctions.gamma(aa, out); case _ => applySFun(a, out, vsTGammaFun, gammaFun, 10L); } } @@ -836,7 +836,7 @@ object FFunctions { def gammaln(a:FMat):FMat = gammaln(a, null); def gammaln(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.gammaln(aa, out); +// case aa:GMat => GFunctions.gammaln(aa, out); case _ => applySFun(a, out, vsLGammaFun, gammalnFun, 10L); } } @@ -846,7 +846,7 @@ object FFunctions { def ceil(a:FMat):FMat = ceil(a, null); def ceil(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.ceil(aa, out); +// case aa:GMat => GFunctions.ceil(aa, out); case _ => applySFun(a, out, vsCeilFun, ceilFun, 1L); } } @@ -856,7 +856,7 @@ object FFunctions { def floor(a:FMat):FMat = floor(a, null); def floor(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.floor(aa, out); +// case aa:GMat => GFunctions.floor(aa, out); case _ => applySFun(a, out, vsFloorFun, floorFun, 1L); } } @@ -866,7 +866,7 @@ object FFunctions { def round(a:FMat):FMat = round(a, null); def round(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.round(aa, out); +// case aa:GMat => GFunctions.round(aa, out); case _ => applySFun(a, out, vsRoundFun, roundFun, 1L); } } @@ -876,7 +876,7 @@ object FFunctions { def trunc(a:FMat):FMat = trunc(a, null); def trunc(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.trunc(aa, out); +// case aa:GMat => GFunctions.trunc(aa, out); case _ => applySFun(a, out, vsTruncFun, truncFun, 1L); } } @@ -890,7 +890,7 @@ object FFunctions { def logistic(a:FMat):FMat = logistic(a, null); def logistic(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.logistic(aa, out); +// case aa:GMat => GFunctions.logistic(aa, out); case _ => applySFun(a, out, vsLogisticFun, logisticFun, 10L); } } @@ -898,7 +898,7 @@ object FFunctions { def psi(a:FMat):FMat = psi(a, null); def psi(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.psi(aa, out); +// case aa:GMat => GFunctions.psi(aa, out); case _ => applySlatecFun(a, out, 0, 100); } } @@ -906,7 +906,7 @@ object FFunctions { def psiinv(a:FMat):FMat = psiinv(a, null); def psiinv(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.psiinv(aa, out); +// case aa:GMat => GFunctions.psiinv(aa, out); case _ => applySlatecFun(a, out, 1, 400); } } @@ -914,7 +914,7 @@ object FFunctions { def psifn(a:FMat, b:FMat):FMat = psifn(a, b, null); def psifn(a:FMat, b:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.psifn(aa, GMat(b), out); +// case aa:GMat => GFunctions.psifn(aa, GMat(b), out); case _ => applySlatecFun2(a, b, out, 0, 200); } } @@ -924,7 +924,7 @@ object FFunctions { def atan2(a:FMat, b:FMat):FMat = atan2(a, b, null); def atan2(a:FMat, b:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.atan2(aa, GMat(b), out); +// case aa:GMat => GFunctions.atan2(aa, GMat(b), out); case _ => applyS2Fun(a, b, out, vsAtan2Fun, atan2Fun, 10L); } } @@ -934,7 +934,7 @@ object FFunctions { def pow(a:FMat, b:FMat):FMat = pow(a, b, null); def pow(a:FMat, b:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.pow(aa, GMat(b), out); +// case aa:GMat => GFunctions.pow(aa, GMat(b), out); case _ => applyS2Fun(a, b, out, vsPowFun, powFun, 10L); } } @@ -943,7 +943,7 @@ object FFunctions { def powx(a:FMat, b:Float):FMat = powx(a, b, null); def powx(a:FMat, b:Float, out:Mat) = { a match { - case aa:GMat => throw new RuntimeException("powx not implemented on GPU") +// case aa:GMat => throw new RuntimeException("powx not implemented on GPU") case _ => applyS2xFun(a, b, out, vsPowxFun, powFun, 10L); } } @@ -952,7 +952,7 @@ object FFunctions { def exppsi(a:FMat):FMat = exppsi(a, null); def exppsi(a:FMat, out:Mat) = { a match { - case aa:GMat => GFunctions.exppsi(aa, out); +// case aa:GMat => GFunctions.exppsi(aa, out); case _ => applySFun(a, out, null, exppsiFun, 3L); } } @@ -974,8 +974,8 @@ object FFunctions { throw new RuntimeException("LXdistance: ncols must match") } val c = FMat.newOrCheckFMat(a.nrows, b.nrows, omat, a.GUID, b.GUID, "LXdistance".##) - if (Mat.hasCUDA > 0) GFunctions.LXdist(a, b, c, p) - else { +// if (Mat.hasCUDA > 0) GFunctions.LXdist(a, b, c, p) +// else { val tmp = DMat.newOrCheckDMat(a.nrows, 1, null, a.GUID, b.GUID, "LXdistance_1".##) val tmp2 = DMat.newOrCheckDMat(a.nrows, 1, null, a.GUID, b.GUID, "LXdistance_2".##) val pinv = 1.0f/p @@ -1028,7 +1028,7 @@ object FFunctions { } Mat.nflops += 3L*a.nrows*a.ncols*b.nrows c - } +// } }; def fft(a:FMat, omat:Mat):FMat = { diff --git a/src/main/scala/BIDMat/FMat.scala b/src/main/scala/BIDMat/FMat.scala index b67c9dd1..d48f0b69 100755 --- a/src/main/scala/BIDMat/FMat.scala +++ b/src/main/scala/BIDMat/FMat.scala @@ -558,30 +558,30 @@ case class FMat(dims0:Array[Int], val data:Array[Float]) extends DenseMat[Float] def ffMatOp(b: Mat, f:(Float, Float) => Float, optype:Int, out:Mat):FMat = (this, b) match { - case (aa:GMat, bb:FMat) => aa.gOp(bb, out, optype); - case (aa:FMat, bb:GMat) => GMat(this).gOp(bb, out, optype); +// case (aa:GMat, bb:FMat) => aa.gOp(bb, out, optype); +// case (aa:FMat, bb:GMat) => GMat(this).gOp(bb, out, optype); case (aa:FMat, bb:FMat) => FMat(ggMatOp(bb, f, out)); case _ => throw new RuntimeException("unsupported operation "+f+" on "+this+" and "+b) } def ffMatOpv(b: Mat, f:(Array[Float],Int,Int,Array[Float],Int,Int,Array[Float],Int,Int,Int) => Float, optype:Int, out:Mat):FMat = (this, b) match { - case (aa:GMat, bb:FMat) => aa.gOp(bb, out, optype); - case (aa:FMat, bb:GMat) => GMat(this).gOp(bb, out, optype); +// case (aa:GMat, bb:FMat) => aa.gOp(bb, out, optype); +// case (aa:FMat, bb:GMat) => GMat(this).gOp(bb, out, optype); case (aa:FMat, bb:FMat) => FMat(ggMatOpv(bb, f, out)); case _ => throw new RuntimeException("unsupported operation "+f+" on "+this+" and "+b) } def ffMatOpScalar(b: Float, f:(Float, Float) => Float, opn:Int, out:Mat):FMat = { this match { - case aa:GMat => aa.gOp(GMat(b), out, opn); +// case aa:GMat => aa.gOp(GMat(b), out, opn); case _ => FMat(ggMatOpScalar(b, f, out)); } } def ffMatOpScalarv(b: Float, f:(Array[Float],Int,Int,Array[Float],Int,Int,Array[Float],Int,Int,Int) => Float, opn:Int, out:Mat) = { this match { - case aa:GMat => aa.gOp(GMat(b), out, opn); +// case aa:GMat => aa.gOp(GMat(b), out, opn); case _ => FMat(ggMatOpScalarv(b, f, out)); } } @@ -633,8 +633,8 @@ case class FMat(dims0:Array[Int], val data:Array[Float]) extends DenseMat[Float] override def copyTo(a:Mat) = { a match { - case aa:GMat => aa.copyFrom(this); - case aa:GDMat => aa.copyFrom(DMat(this)); +// case aa:GMat => aa.copyFrom(this); +// case aa:GDMat => aa.copyFrom(DMat(this)); case out:FMat => copyTo(out):FMat; case out:DMat => {Mat.copyToDoubleArray(data, 0, out.data, 0, length)} case ii:IMat => {Mat.copyToIntArray(data, 0, ii.data, 0, length)} @@ -838,8 +838,8 @@ case class FMat(dims0:Array[Int], val data:Array[Float]) extends DenseMat[Float] def fDMult(b:FMat, outmat:Mat):FMat = { (this, b) match { - case (aa:GMat, bb:FMat) => aa.GMult(b, outmat); - case (aa:FMat, bb:GMat) => GMat(aa).GMult(bb, outmat); +// case (aa:GMat, bb:FMat) => aa.GMult(b, outmat); +// case (aa:FMat, bb:GMat) => GMat(aa).GMult(bb, outmat); case _ => fDMultFF(b, outmat); } } @@ -1151,8 +1151,8 @@ case class FMat(dims0:Array[Int], val data:Array[Float]) extends DenseMat[Float] def fSMult(b:SMat, outmat:Mat):FMat = { (this, b) match { - case (aa:GMat, bb:SMat) => aa.GSMult(b, outmat); - case (aa:FMat, bb:GSMat) => GMat(aa).GSMult(bb, outmat); +// case (aa:GMat, bb:SMat) => aa.GSMult(b, outmat); +// case (aa:FMat, bb:GSMat) => GMat(aa).GSMult(bb, outmat); case _ => { if (ncols != b.nrows) { throw new RuntimeException("fSMult dimensions mismatch") @@ -1390,8 +1390,8 @@ case class FMat(dims0:Array[Int], val data:Array[Float]) extends DenseMat[Float] def multT(b:SMat, outmat:Mat):FMat = { (this, b) match { - case (aa:GMat, bb:SMat) => aa.GSMultT(b, outmat); - case (aa:FMat, bb:GSMat) => GMat(aa).GSMultT(bb, outmat); +// case (aa:GMat, bb:SMat) => aa.GSMultT(b, outmat); +// case (aa:FMat, bb:GSMat) => GMat(aa).GSMultT(bb, outmat); case _ => multTS(b, outmat); } } @@ -1459,8 +1459,8 @@ case class FMat(dims0:Array[Int], val data:Array[Float]) extends DenseMat[Float] def multT(b:FMat, outmat:Mat):FMat = { (this, b) match { - case (aa:GMat, bb:FMat) => aa.GMultT(b, outmat); - case (aa:FMat, bb:GMat) => GMat(aa).GMultT(bb, outmat); +// case (aa:GMat, bb:FMat) => aa.GMultT(b, outmat); +// case (aa:FMat, bb:GMat) => GMat(aa).GMultT(bb, outmat); case _ => multTFF(b, outmat); } } @@ -1497,8 +1497,8 @@ case class FMat(dims0:Array[Int], val data:Array[Float]) extends DenseMat[Float] def Tmult(b:FMat, outmat:Mat):FMat = { (this, b) match { - case (aa:GMat, bb:FMat) => aa.GTMult(b, outmat); - case (aa:FMat, bb:GMat) => GMat(aa).GTMult(bb, outmat); +// case (aa:GMat, bb:FMat) => aa.GTMult(b, outmat); +// case (aa:FMat, bb:GMat) => GMat(aa).GTMult(bb, outmat); case _ => TmultFF(b, outmat); } } @@ -1556,7 +1556,7 @@ case class FMat(dims0:Array[Int], val data:Array[Float]) extends DenseMat[Float] out } else throw new RuntimeException("dimensions mismatch"); - def GPUmult(b:FMat, out:Mat, btrans:Boolean) = GFunctions.GPUmult(this, b, out, btrans) +// def GPUmult(b:FMat, out:Mat, btrans:Boolean) = GFunctions.GPUmult(this, b, out, btrans) def ddot(a : FMat):Double = if (nrows != a.nrows || ncols != a.ncols) { @@ -1748,7 +1748,7 @@ case class FMat(dims0:Array[Int], val data:Array[Float]) extends DenseMat[Float] def cumsumByKey(keys:FMat, omat:Mat):FMat = { (this, keys) match { - case (gme:GMat, gkeys:GMat) => gme.cumsumByKey(gkeys, omat); +// case (gme:GMat, gkeys:GMat) => gme.cumsumByKey(gkeys, omat); case _ => { if (nrows != keys.nrows || ncols != keys.ncols) throw new RuntimeException("cumsumKey dimensions mismatch"); @@ -1783,7 +1783,7 @@ case class FMat(dims0:Array[Int], val data:Array[Float]) extends DenseMat[Float] def cumsumByKey(keys:IMat, omat:Mat):FMat = { (this, keys) match { - case (gme:GMat, gkeys:GIMat) => gme.cumsumByKey(gkeys, omat); +// case (gme:GMat, gkeys:GIMat) => gme.cumsumByKey(gkeys, omat); case _ => { if (nrows != keys.nrows || ncols != keys.ncols) throw new RuntimeException("cumsumKey dimensions mismatch"); @@ -1818,7 +1818,7 @@ case class FMat(dims0:Array[Int], val data:Array[Float]) extends DenseMat[Float] def cummaxByKey(keys:FMat, omat:Mat):FMat = { (this, keys) match { - case (gme:GMat, gkeys:GMat) => gme.cummaxByKey(gkeys, omat); +// case (gme:GMat, gkeys:GMat) => gme.cummaxByKey(gkeys, omat); case _ => { if (nrows != keys.nrows || ncols != keys.ncols) throw new RuntimeException("cummaxKey dimensions mismatch"); @@ -1853,7 +1853,7 @@ case class FMat(dims0:Array[Int], val data:Array[Float]) extends DenseMat[Float] def cummaxByKey(keys:IMat, omat:Mat):FMat = { (this, keys) match { - case (gme:GMat, gkeys:GIMat) => gme.cummaxByKey(gkeys, omat); +// case (gme:GMat, gkeys:GIMat) => gme.cummaxByKey(gkeys, omat); case _ => { if (nrows != keys.nrows || ncols != keys.ncols) throw new RuntimeException("cummaxKey dimensions mismatch"); @@ -1888,7 +1888,7 @@ case class FMat(dims0:Array[Int], val data:Array[Float]) extends DenseMat[Float] def cumminByKey(keys:FMat, omat:Mat):FMat = { (this, keys) match { - case (gme:GMat, gkeys:GMat) => gme.cumminByKey(gkeys, omat); +// case (gme:GMat, gkeys:GMat) => gme.cumminByKey(gkeys, omat); case _ => { if (nrows != keys.nrows || ncols != keys.ncols) throw new RuntimeException("cumminKey dimensions mismatch"); @@ -1923,7 +1923,7 @@ case class FMat(dims0:Array[Int], val data:Array[Float]) extends DenseMat[Float] def cumminByKey(keys:IMat, omat:Mat):FMat = { (this, keys) match { - case (gme:GMat, gkeys:GIMat) => gme.cumminByKey(gkeys, omat); +// case (gme:GMat, gkeys:GIMat) => gme.cumminByKey(gkeys, omat); case _ => { if (nrows != keys.nrows || ncols != keys.ncols) throw new RuntimeException("cumminKey dimensions mismatch"); @@ -2123,10 +2123,10 @@ case class FMat(dims0:Array[Int], val data:Array[Float]) extends DenseMat[Float] def *^ (b : FMat) = multT(b, null) def Tx (b : FMat) = Tmult(b, null) def ^* (b : FMat) = Tmult(b, null) - def xG (b :FMat) = GPUmult(b, null, false) - def xTG (b :FMat) = GPUmult(b, null, true) - def *! (b :FMat) = GPUmult(b, null, false) - def *^! (b :FMat) = GPUmult(b, null, true) +// def xG (b :FMat) = GPUmult(b, null, false) +// def xTG (b :FMat) = GPUmult(b, null, true) +// def *! (b :FMat) = GPUmult(b, null, false) +// def *^! (b :FMat) = GPUmult(b, null, true) def /< (b : FMat) = solvel(b) def \\ (b : FMat) = solver(b) def ◁ (b : FMat) = solvel(b) @@ -2452,10 +2452,10 @@ class FPair(val omat:Mat, val mat:FMat) extends Pair(omat, mat) { def xT (b : FMat) = mat.multT(b, omat) def Tx (b : FMat) = mat.Tmult(b, omat) def ^* (b : FMat) = mat.Tmult(b, omat) - def *! (b :FMat) = mat.GPUmult(b, omat, false) - def *^! (b :FMat) = mat.GPUmult(b, omat, true) - def xG (b :FMat) = mat.GPUmult(b, omat, false) - def xTG (b :FMat) = mat.GPUmult(b, omat, true) +// def *! (b :FMat) = mat.GPUmult(b, omat, false) +// def *^! (b :FMat) = mat.GPUmult(b, omat, true) +// def xG (b :FMat) = mat.GPUmult(b, omat, false) +// def xTG (b :FMat) = mat.GPUmult(b, omat, true) def + (b : FMat) = mat.ffMatOpv(b, FMat.vecAddFun, op_add, omat) def - (b : FMat) = mat.ffMatOpv(b, FMat.vecSubFun, op_sub, omat) def *@ (b : FMat) = mat.ffMatOpv(b, FMat.vecMulFun, op_mul, omat) @@ -2700,15 +2700,15 @@ object FMat { def apply(x:Mat):FMat = { val out:FMat = x match { - case _:GMat | _:GDMat | _:GIMat | _:CLMat | _:DMat | _:IMat | _:LMat | _:BMat | _:SMat => FMat.newOrCheckFMat(x.dims, null, x.GUID, "FMat".##); + case _:CLMat | _:DMat | _:IMat | _:LMat | _:BMat | _:SMat => FMat.newOrCheckFMat(x.dims, null, x.GUID, "FMat".##); case ff:FMat => ff; case dd:DenseMat[Float] @ unchecked => {val out = new FMat(dd.dims.data, dd._data); out.setGUID(dd.GUID); out} case _ => throw new RuntimeException("FMat apply unknown argument"); } x match { - case gg:GMat => gg.toFMat(out); - case gg:GDMat => gg.copyTo(out); - case gg:GIMat => gg.toFMat(out); +// case gg:GMat => gg.toFMat(out); +// case gg:GDMat => gg.copyTo(out); +// case gg:GIMat => gg.toFMat(out); case gg:CLMat => gg.toFMat(out); case _:FMat => {}; case dd:DMat => {Mat.copyToFloatArray(dd.data, 0, out.data, 0, dd.length)} diff --git a/src/main/scala/BIDMat/GDFunctions.scala b/src/main/scala/BIDMat/GDFunctions.scala deleted file mode 100644 index e75b0e87..00000000 --- a/src/main/scala/BIDMat/GDFunctions.scala +++ /dev/null @@ -1,624 +0,0 @@ -package BIDMat - -import edu.berkeley.bid.VML._ -import edu.berkeley.bid.VSL -import edu.berkeley.bid.VSL._ -import edu.berkeley.bid.CBLAS._ -import edu.berkeley.bid.RAND; -import edu.berkeley.bid.RAND._; -import edu.berkeley.bid.SLATEC; -import edu.berkeley.bid.CUMAT; -import edu.berkeley.bid.CUMATD; -import GMat.BinOp -import GMat.TransF -import GMat.TransF2 -import java.util.Random._; -import scala.concurrent.Future -import scala.concurrent.ExecutionContext.Implicits.global -//import MatFunctions._ -import SciState._ -import org.apache.commons.math3.special._ -import org.apache.commons.math3.util.FastMath -import org.apache.commons.math3.random.RandomDataGenerator; -import jcuda._ -import jcuda.runtime._ -import jcuda.runtime.JCuda._ -import jcuda.runtime.cudaMemcpyKind._ -import jcuda.jcublas._ -import jcuda.jcublas.JCublas._ -import jcuda.jcusparse._ - -object GDFunctions { - - def max(a:GDMat, b:GDMat, out:Mat):GDMat = a.gOp(b, out, BinOp.op_max) - def min(a:GDMat, b:GDMat, out:Mat):GDMat = a.gOp(b, out, BinOp.op_min) - - def maxi(a:GDMat, dir:Int, out:Mat):GDMat = a.reduceOp(out, dir, Float.MinValue, BinOp.op_max); - def mini(a:GDMat, dir:Int, out:Mat):GDMat = a.reduceOp(out, dir, Float.MaxValue, BinOp.op_min); - def sum(a:GDMat, dir:Int, out:Mat):GDMat = a.reduceOp(out, dir, 0f, BinOp.op_add); - def prod(a:GDMat, dir:Int, out:Mat):GDMat = a.reduceOp(out, dir, 1f, BinOp.op_mul); - - def rand(out:GDMat):GDMat = { - import jcuda.jcurand._ - Mat.nflops += 10L*out.length - JCurand.curandGenerateUniformDouble(GFunctions.cudarng(GFunctions.getGPU).asInstanceOf[curandGenerator], out.pdata, out.length) - jcuda.runtime.JCuda.cudaStreamSynchronize(Mat.SyncMethod) - out - } - - def rand(nr:Int, nc:Int):GDMat = { - val out = GDMat(nr, nc); - rand(out); - } - - def rand(dims:Array[Int]):GDMat = { - val out = GDMat.make(dims); - rand(out); - } - - def rand(dims:IMat):GDMat = rand(dims.data); - - def normrnd(mu:Double, sig:Double, out:GDMat):GDMat = { - import jcuda.jcurand._ - Mat.nflops += 10L*out.length - JCurand.curandGenerateNormalDouble(GFunctions.cudarng(GFunctions.getGPU).asInstanceOf[curandGenerator], out.pdata, out.length, mu, sig) - jcuda.runtime.JCuda.cudaStreamSynchronize(Mat.SyncMethod) - out - } - - - def applyGDfun(in:GDMat, omat:Mat, opn:Int, kflops:Long):GDMat = { - val out = GDMat.newOrCheckGDMat(in.nrows, in.ncols, omat, in.GUID, opn); - CUMAT.applygdfun(in.pdata, out.pdata, in.nrows*in.ncols, opn); - jcuda.runtime.JCuda.cudaStreamSynchronize(Mat.SyncMethod); - Mat.nflops += kflops*in.length; - out - } - - def applyGDfun(in:GDMat, opn:Int, kflops:Long):GDMat = { - val out = GDMat.newOrCheckGDMat(in.nrows, in.ncols, null, in.GUID, opn); - CUMAT.applygdfun(in.pdata, out.pdata, in.nrows*in.ncols, opn); - jcuda.runtime.JCuda.cudaStreamSynchronize(Mat.SyncMethod); - Mat.nflops += kflops*in.length; - out - } - - def applyGDfun2(a:GDMat, b:GDMat, omat:Mat, opn:Int, kflops:Long):GDMat = { - if (a.nrows == b.nrows && a.ncols == b.ncols) { - val out = GDMat.newOrCheckGDMat(a.nrows, a.ncols, omat, a.GUID, b.GUID, opn); - CUMAT.applygdfun2(a.pdata, b.pdata, out.pdata, a.nrows*a.ncols, opn); - jcuda.runtime.JCuda.cudaStreamSynchronize(Mat.SyncMethod); - Mat.nflops += kflops*a.length; - out - } else { - throw new RuntimeException("Dimensions mismatch") - } - } - - def applyGDfun2(a:GDMat, b:GDMat, opn:Int, kflops:Long):GDMat = { - if (a.nrows == b.nrows && a.ncols == b.ncols) { - val out = GDMat.newOrCheckGDMat(a.nrows, a.ncols, null, a.GUID, b.GUID, opn); - CUMAT.applygdfun2(a.pdata, b.pdata, out.pdata, a.nrows*a.ncols, opn); - jcuda.runtime.JCuda.cudaStreamSynchronize(Mat.SyncMethod); - Mat.nflops += kflops*a.length; - out - } else { - throw new RuntimeException("Dimensions mismatch") - } - } - - def abs(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.abs, 1L) - def exp(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.exp, 10L) - def expm1(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.expm1, 10L) - def sqrt(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.sqrt, 10L) - def ln(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.ln, 10L) - def log10(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.log10, 10L) - def log1p(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.log1p, 10L) - def cos(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.cos, 10L) - def sin(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.sin, 10L) - def tan(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.tan, 10L) - def cosh(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.cosh, 10L) - def sinh(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.sinh, 10L) - def tanh(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.tanh, 10L) - def acos(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.acos, 10L) - def asin(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.asin, 10L) - def atan(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.atan, 10L) - def acosh(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.acosh, 10L) - def asinh(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.asinh, 10L) - def atanh(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.atanh, 10L) - def erf(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.erf, 10L) - def erfinv(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.erfinv, 10L) - def erfc(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.erfc, 10L) - def erfcinv(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.erfcinv, 10L) - def gammaln(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.gammaln, 10L) - def gamma(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.gamma, 10L) - def Γ(a:GDMat, out:Mat) = gamma(a, out); - def ceil(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.ceil, 10L) - def floor(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.floor, 10L) - def round(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.round, 10L) - def trunc(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.trunc, 10L) - def sign(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.sign, 1L) - def exppsi(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.exppsi, 10L) - def normcdf(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.normcdf, 10L) - def normcdfinv(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.normcdfinv, 10L) - def logistic(in:GDMat, out:Mat):GDMat = applyGDfun(in, out, TransF.logistic, 10L) - - def atan2(a:GDMat, b:GDMat, out:Mat):GDMat = applyGDfun2(a, b, out, TransF2.atan2, 10L) - def pow(a:GDMat, b:GDMat, out:Mat):GDMat = applyGDfun2(a, b, out, TransF2.pow, 10L) - - def abs(in:GDMat):GDMat = applyGDfun(in, TransF.abs, 10L) - def exp(in:GDMat):GDMat = applyGDfun(in, TransF.exp, 10L) - def expm1(in:GDMat):GDMat = applyGDfun(in, TransF.expm1, 10L) - def sqrt(in:GDMat):GDMat = applyGDfun(in, TransF.sqrt, 10L) - def ln(in:GDMat):GDMat = applyGDfun(in, TransF.ln, 10L) - def log10(in:GDMat):GDMat = applyGDfun(in, TransF.log10, 10L) - def log1p(in:GDMat):GDMat = applyGDfun(in, TransF.log1p, 10L) - def cos(in:GDMat):GDMat = applyGDfun(in, TransF.cos, 10L) - def sin(in:GDMat):GDMat = applyGDfun(in, TransF.sin, 10L) - def tan(in:GDMat):GDMat = applyGDfun(in, TransF.tan, 10L) - def cosh(in:GDMat):GDMat = applyGDfun(in, TransF.cosh, 10L) - def sinh(in:GDMat):GDMat = applyGDfun(in, TransF.sinh, 10L) - def tanh(in:GDMat):GDMat = applyGDfun(in, TransF.tanh, 10L) - def acos(in:GDMat):GDMat = applyGDfun(in, TransF.acos, 10L) - def asin(in:GDMat):GDMat = applyGDfun(in, TransF.asin, 10L) - def atan(in:GDMat):GDMat = applyGDfun(in, TransF.atan, 10L) - def acosh(in:GDMat):GDMat = applyGDfun(in, TransF.acosh, 10L) - def asinh(in:GDMat):GDMat = applyGDfun(in, TransF.asinh, 10L) - def atanh(in:GDMat):GDMat = applyGDfun(in, TransF.atanh, 10L) - def erf(in:GDMat):GDMat = applyGDfun(in, TransF.erf, 10L) - def erfinv(in:GDMat):GDMat = applyGDfun(in, TransF.erfinv, 10L) - def erfc(in:GDMat):GDMat = applyGDfun(in, TransF.erfc, 10L) - def erfcinv(in:GDMat):GDMat = applyGDfun(in, TransF.erfcinv, 10L) - def gammaln(in:GDMat):GDMat = applyGDfun(in, TransF.gammaln, 10L) - def gamma(in:GDMat):GDMat = applyGDfun(in, TransF.gamma, 10L) - def Γ(a:GDMat) = gamma(a); - def ceil(in:GDMat):GDMat = applyGDfun(in, TransF.ceil, 10L) - def floor(in:GDMat):GDMat = applyGDfun(in, TransF.floor, 10L) - def round(in:GDMat):GDMat = applyGDfun(in, TransF.round, 10L) - def trunc(in:GDMat):GDMat = applyGDfun(in, TransF.trunc, 10L) - def sign(in:GDMat):GDMat = applyGDfun(in, TransF.sign, 1L) - def exppsi(in:GDMat):GDMat = applyGDfun(in, TransF.exppsi, 1L) - def normcdf(in:GDMat):GDMat = applyGDfun(in, TransF.normcdf, 10L) - def normcdfinv(in:GDMat):GDMat = applyGDfun(in, TransF.normcdfinv, 10L) - def logistic(in:GDMat):GDMat = applyGDfun(in, TransF.logistic, 10L) - - def atan2(a:GDMat, b:GDMat):GDMat = applyGDfun2(a, b, TransF2.atan2, 10L) - def pow(a:GDMat, b:GDMat):GDMat = applyGDfun2(a, b, TransF2.pow, 10L) - - def accumIJ(I:GIMat, J:GIMat, V:GDMat, omat:Mat, nrows:Int, ncols:Int):GDMat = { - val out = GDMat.newOrCheckGDMat(nrows, ncols, omat, I.GUID, J.GUID, V.GUID, "GDMat_accum".##) - out.clear - if (I.length != J.length || I.length != V.length) { - throw new RuntimeException("GDMat accum: index lengths dont match") - } - CUMATD.accum(I.pdata, J.pdata, V.pdata, out.pdata, I.length, nrows) - Mat.nflops += I.length - out - } - - def accumIJ(I:Int, J:GIMat, V:GDMat, omat:Mat, nrows:Int, ncols:Int):GDMat = { - val out = GDMat.newOrCheckGDMat(nrows, ncols, omat, I, J.GUID, V.GUID, "GDMat_accumI".##) - out.clear - if (J.length != V.length) { - throw new RuntimeException("GDMat accum: index lengths dont match") - } - CUMATD.accumI(I, J.pdata, V.pdata, out.pdata, J.length, nrows) - Mat.nflops += J.length - out - } - - def accumIJ(I:GIMat, J:Int, V:GDMat, omat:Mat, nrows:Int, ncols:Int):GDMat = { - val out = GDMat.newOrCheckGDMat(nrows, ncols, omat, I.GUID, J, V.GUID, "GDMat_accumJ".##) - out.clear - if (I.length != V.length) { - throw new RuntimeException("GDMat accum: index lengths dont match") - } - CUMATD.accumJ(I.pdata, J, V.pdata, out.pdata, I.length, nrows) - Mat.nflops += I.length - out - } - - def accumIJ(I:GIMat, J:GIMat, V:Double, omat:Mat, nrows:Int, ncols:Int):GDMat = { - val out = GDMat.newOrCheckGDMat(nrows, ncols, omat, I.GUID, J.GUID, V.hashCode, "GDMat_accumV".##) - out.clear - if (I.length != J.length) { - throw new RuntimeException("GDMat accum: index lengths dont match") - } - CUMATD.accumV(I.pdata, J.pdata, V, out.pdata, I.length, nrows) - Mat.nflops += I.length - out - } - - def accumIJ(I:Int, J:GIMat, V:Double, omat:Mat, nrows:Int, ncols:Int):GDMat = { - val out = GDMat.newOrCheckGDMat(nrows, ncols, omat, I, J.GUID, V.hashCode, "GDMat_accumIV".##) - out.clear - CUMATD.accumIV(I, J.pdata, V, out.pdata, J.length, nrows) - Mat.nflops += J.length - out - } - - def accumIJ(I:GIMat, J:Int, V:Double, omat:Mat, nrows:Int, ncols:Int):GDMat = { - val out = GDMat.newOrCheckGDMat(nrows, ncols, omat, I.GUID, J, V.hashCode, "GDMat_accumJV".##) - out.clear - CUMATD.accumJV(I.pdata, J, V, out.pdata, I.length, nrows) - Mat.nflops += I.length - out - } - - def accum(IJ:GIMat, V:GDMat, omat:Mat, nrows:Int, ncols:Int):GDMat = { - if (IJ.nrows != V.length || IJ.ncols > 2) { - throw new RuntimeException("GDMat accum: index lengths dont match") - } - val out = GDMat.newOrCheckGDMat(nrows, ncols, omat, IJ.GUID, V.GUID, "GDMat_accumIJ".##) - out.clear - if (IJ.ncols == 2) { - CUMATD.accum(IJ.pdata, IJ.pdata.withByteOffset(1L*IJ.nrows*Sizeof.INT), V.pdata, out.pdata, V.length, nrows) - } else { - CUMATD.accumJ(IJ.pdata, 0, V.pdata, out.pdata, V.length, nrows) - } - Mat.nflops += V.length - out - } - - def accum(IJ:GIMat, V:Double, omat:Mat, nrows:Int, ncols:Int):GDMat = { - if (IJ.ncols > 2) { - throw new RuntimeException("GDMat accum: index lengths dont match") - } - val out = GDMat.newOrCheckGDMat(nrows, ncols, omat, IJ.GUID, V.hashCode, "GDMat_accumIJV".##) - out.clear - if (IJ.ncols == 2) { - CUMATD.accumV(IJ.pdata, IJ.pdata.withByteOffset(1L*IJ.nrows*Sizeof.INT), V, out.pdata, IJ.nrows, nrows) - } else { - CUMATD.accumJV(IJ.pdata, 0, V, out.pdata, IJ.nrows, nrows) - } - Mat.nflops += IJ.nrows - out - } - - def cumsum(a:GDMat, dim0:Int, omat:Mat):GDMat = { - Mat.nflops += 1L * a.length; - val dim = if (a.nrows == 1 && dim0 == 0) 2 else math.max(1, dim0); - if (dim == 1) { - val out = GDMat.newOrCheckGDMat(a.nrows, a.ncols, omat, a.GUID, "cumsum".##) - CUMATD.cumsumc(a.nrows, a.ncols, a.pdata, out.pdata) - out - } else { - throw new RuntimeException("Cumsum across rows not supported yet") - } - } - - def cumsumg(a:GDMat, jc:GIMat, omat:Mat):GDMat = { - Mat.nflops += 1L * a.length - val out = GDMat.newOrCheckGDMat(a.nrows, a.ncols, omat, a.GUID, jc.GUID, "cumsumi".##) - val err = CUMATD.cumsumgf(a.pdata, out.pdata, jc.pdata, a.nrows, a.ncols, jc.length-1) - if (err != 0) throw new RuntimeException("cumsumi error %d: " + cudaGetErrorString(err) format err); - out - } - - def maxg(a:GDMat, jc:GIMat, omat:Mat, omati:Mat):(GDMat, GIMat) = { - Mat.nflops += 1L * a.length - val out = GDMat.newOrCheckGDMat(jc.length-1, a.ncols, omat, a.GUID, jc.GUID, "maxg".##) - val outi = GIMat.newOrCheckGIMat(jc.length-1, a.ncols, omati, a.GUID, jc.GUID, "maxg_1".##) - val err = CUMATD.maxgf(a.pdata, out.pdata, outi.pdata, jc.pdata, a.nrows, a.ncols, jc.length-1) - if (err != 0) throw new RuntimeException("maxg error %d: " + cudaGetErrorString(err) format err); - (out, outi) - } - - def ming(a:GDMat, jc:GIMat, omat:Mat, omati:Mat):(GDMat, GIMat) = { - Mat.nflops += 1L * a.length - val out = GDMat.newOrCheckGDMat(jc.length-1, a.ncols, omat, a.GUID, jc.GUID, "ming".##) - val outi = GIMat.newOrCheckGIMat(jc.length-1, a.ncols, omati, a.GUID, jc.GUID, "ming_1".##) - val err = CUMATD.mingf(a.pdata, out.pdata, outi.pdata, jc.pdata, a.nrows, a.ncols, jc.length-1) - if (err != 0) throw new RuntimeException("ming error %d: " + cudaGetErrorString(err) format err); - (out, outi) - } - - def maxi2(a:GDMat, omat:Mat, omati:Mat, dim0:Int):(GDMat, GIMat) = { - Mat.nflops += 1L * a.length - val dim = if (a.nrows == 1 && dim0 == 0) 2 else math.max(1, dim0) - if (dim == 1) { - val out = GDMat.newOrCheckGDMat(1, a.ncols, omat, a.GUID, "maxi2".##) - val outi = GIMat.newOrCheckGIMat(1, a.ncols, omati, a.GUID, "maxi2_1".##) - val err = CUMATD.maxif(a.pdata, out.pdata, outi.pdata, a.nrows, a.ncols, 1) - if (err != 0) throw new RuntimeException("maxi2 error %d: " + cudaGetErrorString(err) format err); - (out, outi) - } else if (dim == 2) { - val out = GDMat.newOrCheckGDMat(a.nrows, 1, omat, a.GUID, "maxi2".##) - val outi = GIMat.newOrCheckGIMat(a.nrows, 1, omati, a.GUID, "maxi2_1".##) - val err = CUMATD.maxif(a.pdata, out.pdata, outi.pdata, a.nrows, a.ncols, 2) - if (err != 0) throw new RuntimeException("maxi2 error %d: " + cudaGetErrorString(err) format err); - (out, outi) - } else { - throw new RuntimeException("maxi2 directions not recognized %d" format dim0) - } - } - - def mini2(a:GDMat, omat:Mat, omati:Mat, dim0:Int):(GDMat, GIMat) = { - Mat.nflops += 1L * a.length - val dim = if (a.nrows == 1 && dim0 == 0) 2 else math.max(1, dim0) - if (dim == 1) { - val out = GDMat.newOrCheckGDMat(1, a.ncols, omat, a.GUID, "maxi2".##) - val outi = GIMat.newOrCheckGIMat(1, a.ncols, omati, a.GUID, "maxi2_1".##) - val err = CUMATD.minif(a.pdata, out.pdata, outi.pdata, a.nrows, a.ncols, dim) - if (err != 0) throw new RuntimeException("mini2 error %d: " + cudaGetErrorString(err) format err); - (out, outi) - } else if (dim == 2) { - val out = GDMat.newOrCheckGDMat(a.nrows, 1, omat, a.GUID, "maxi2".##) - val outi = GIMat.newOrCheckGIMat(a.nrows, 1, omati, a.GUID, "maxi2_1".##) - val err = CUMATD.minif(a.pdata, out.pdata, outi.pdata, a.nrows, a.ncols, dim) - if (err != 0) throw new RuntimeException("mini2 error %d: " + cudaGetErrorString(err) format err); - (out, outi) - } else { - throw new RuntimeException("mini2 directions not recognized %d" format dim) - } - } - - def norm(a:GDMat) = math.sqrt(jcuda.jcublas.JCublas.cublasDdot(a.length, a.pdata, 1, a.pdata, 1)) - - def embedmat(a:GIMat, b:GDMat, oMat: Mat):GIMat = { - if (a.nrows != b.nrows || a.ncols != b.ncols) { - throw new RuntimeException("embedmat error: mismatched dimensions"); - } - val out = GIMat.newOrCheckGIMat(a.nrows * 2, a.ncols, oMat, a.GUID, b.GUID, "embedmat".##) - val err = CUMATD.embedmat(b.pdata, a.pdata, out.pdata, a.length); - if (err != 0) throw new RuntimeException("embedmat error %d: " + cudaGetErrorString(err) format err); - out - } - - def embedmat(a:GIMat, b: GDMat):GIMat = embedmat(a, b, null); - - def extractmat(a:Mat, b: Mat, c: GIMat):(GIMat, GDMat) = { - val outA = GIMat.newOrCheckGIMat(c.nrows /2, c.ncols, a, c.GUID, "extractmat_A".##) - val outB = GDMat.newOrCheckGDMat(c.nrows /2, c.ncols, b, c.GUID, "extractmat_B".##) - val err = CUMATD.extractmat(outB.pdata, outA.pdata, c.pdata, outA.length); - if (err != 0) throw new RuntimeException("extractmat error %d: " + cudaGetErrorString(err) format err); - (outA, outB) - } - - def extractmat(c: GIMat):(GIMat, GDMat) = extractmat(null, null, c); - - def GPUmult(a:FMat, b:FMat, omat:Mat, btrans:Boolean):FMat = { - val bnrows = if (btrans) b.ncols else b.nrows - val bncols = if (btrans) b.nrows else b.ncols - if (a.ncols != bnrows) { - throw new RuntimeException("dimensions mismatch in xG") - } else { - val maxrows = 8192 - val maxcols = 8192 - val c = FMat.newOrCheckFMat(a.nrows, bncols, omat, a.GUID, b.GUID, "GPUmult".##) - val rblkk = if (Mat.hasCUDA > 1) 2 else 1 - val cblkk = if (Mat.hasCUDA > 3) 2 else 1 - val rblk = rblkk*(math.max(1, math.ceil(c.nrows/maxrows/rblkk).toInt)) - val cblk = cblkk*(math.max(1, math.ceil(c.ncols/maxcols/cblkk).toInt)) - val kblk = math.max(1, math.ceil(a.ncols/maxcols).toInt) - val gcrows = 32*(c.nrows/rblk/32) - val gccols = 32*(c.ncols/cblk/32) - val garows = gcrows - val gacols = 32*(a.ncols/kblk/32) - val gbrows = if (btrans) gccols else gacols - val gbcols = if (btrans) gacols else gccols - - val done = IMat(rblkk*cblkk,1) - for (ix <- 0 until rblkk) { - for (iy <- 0 until cblkk) { - Future { - SciFunctions.setGPU(ix+iy*2) - val aa = new Pointer - val bb = new Pointer - val cc = new Pointer - var err = cublasAlloc(garows*gacols, Sizeof.DOUBLE, aa) - if (err != 0) throw new RuntimeException("CUDA alloc failed " + cudaGetErrorString(err)) - err = cublasAlloc(gbrows*gbcols, Sizeof.DOUBLE, bb) - if (err != 0) throw new RuntimeException("CUDA alloc failed " + cudaGetErrorString(err)) - err = cublasAlloc(gcrows*gccols, Sizeof.DOUBLE, cc) - if (err != 0) throw new RuntimeException("CUDA alloc failed "+err) - - var i = ix*gcrows; while (i < c.nrows) { - val ni = math.min(gcrows, c.nrows - i) - var j = iy*gccols; while (j < c.ncols) { - val nj = math.min(gccols, c.ncols - j) - var k = 0; while (k < a.ncols) { - val nk = math.min(gacols, a.ncols - k) - err = cudaMemcpy2D(aa, 1L*garows*Sizeof.DOUBLE, Pointer.to(a.data).withByteOffset(1L*(i+k*a.nrows)*Sizeof.DOUBLE), - 1L*a.nrows*Sizeof.DOUBLE, 1L*ni*Sizeof.DOUBLE, nk, cudaMemcpyHostToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - if (err != 0) throw new RuntimeException("CUDA copy a failed "+err) - if (btrans) { - err = cudaMemcpy2D(bb, 1L*gbrows*Sizeof.DOUBLE, Pointer.to(b.data).withByteOffset(1L*(j+k*b.nrows)*Sizeof.DOUBLE), - 1L*b.nrows*Sizeof.DOUBLE, 1L*nj*Sizeof.DOUBLE, nk, cudaMemcpyHostToDevice) - } else { - err = cudaMemcpy2D(bb, 1L*gbrows*Sizeof.DOUBLE, Pointer.to(b.data).withByteOffset(1L*(k+j*b.nrows)*Sizeof.DOUBLE), - 1L*b.nrows*Sizeof.DOUBLE, 1L*nk*Sizeof.DOUBLE, nj, cudaMemcpyHostToDevice) - } - cudaStreamSynchronize(Mat.SyncMethod) - if (err != 0) throw new RuntimeException("CUDA copy b failed "+err) - - cublasSgemm('n', if (btrans) 't' else 'n', ni, nj, nk, 1.0f, aa, garows, bb, gbrows, if (k==0) 0f else 1f, cc, gcrows) - - cudaStreamSynchronize(Mat.SyncMethod) - err = cudaGetLastError - if (err != 0) throw new RuntimeException("Cublas error in xG, sgemm "+err) - k += gacols - } - err = cudaMemcpy2D(Pointer.to(c.data).withByteOffset(1L*(i+j*c.nrows)*Sizeof.DOUBLE), 1L*c.nrows*Sizeof.DOUBLE, cc, 1L*gcrows*Sizeof.DOUBLE, 1L*ni*Sizeof.DOUBLE, nj, cudaMemcpyDeviceToHost) - cudaStreamSynchronize(Mat.SyncMethod) - if (err != 0) throw new RuntimeException("CUDA copy c failed "+err) - j += cblkk*gccols - } - i += rblkk*gcrows - } - - cublasFree(cc) - cublasFree(bb) - cublasFree(aa) - done(ix+2*iy,0) = 1 - } - } - } - while (SciFunctions.mini(done).v == 0) {Thread.`yield`} - - Mat.nflops += 2L * a.nrows * a.ncols * bncols - c - } - } - - - def sortxGPU(keys:GDMat, vals:GIMat):Unit = _sortxGPU(keys, vals, true) - - def sortdownxGPU(keys:GDMat, vals:GIMat):Unit = _sortxGPU(keys, vals, false) - - def _sortxGPU(keys:GDMat, vals:GIMat, asc:Boolean):Unit = { - if (keys.nrows != vals.nrows || keys.ncols != vals.ncols) - throw new RuntimeException("Dimensions mismatch in sortxGPU") - val nspine = CUMATD.fsortsizex(keys.nrows) - val tkeys = GDMat(keys.nrows, 1) - val tvals = GIMat(keys.nrows, 1) - val tspine = GIMat(nspine, 1) - val bflags = GIMat(32, 1) - - CUMATD.fsort2dx(keys.pdata, vals.pdata, tkeys.pdata, tvals.pdata, tspine.pdata, bflags.pdata, keys.nrows, keys.ncols, if (asc) 1 else 0) - - tkeys.free - tvals.free - tspine.free - bflags.free - Mat.nflops += keys.length - } - - - - def LXdist(a:GDMat, b:GDMat, omat:GDMat, p:Float):GDMat = { - if (a.ncols != b.ncols) { - throw new RuntimeException("LXdist number of columns = number of features must match") - } - val c = GDMat.newOrCheckGDMat(a.nrows, b.nrows, omat, a.GUID, b.GUID, "LXdist".##) - c.clear - Mat.nflops += 3L * c.nrows * c.ncols * a.ncols - var err = CUMATD.distances(a.pdata, a.nrows, b.pdata, b.nrows, c.pdata, c.nrows, a.ncols, c.nrows, c.ncols, p) - if (err != 0) throw new RuntimeException("LXdist kernel error "+err) - val easyp = (p == 0f || p == 1f || p == 2f) - if (!easyp) { - val pinv = GDMat(1/p) - err = CUMAT.applydop(c.pdata, c.nrows, c.ncols, pinv.pdata, 1, 1, c.pdata, BinOp.op_pow) - } - if (err != 0) throw new RuntimeException("LXdist scaling error "+err) - c - } - - def LXdist(a:FMat, b:FMat, omat:FMat, p:Float):FMat = { - if (a.ncols != b.ncols) { - throw new RuntimeException("LXdist number of columns = number of features must match") - } - val c = FMat.newOrCheckFMat(a.nrows, b.nrows, omat, a.GUID, b.GUID, "LXdist".##) - val easyp = (p == 0f || p == 1f || p == 2f) - val takeroot = (p != 0f && p != 1f) - val maxrows = if (easyp) 8192 else 2048 - val maxcols = if (easyp) 8192 else 2048 - val rblkk = if (Mat.hasCUDA > 1) 2 else 1 - val cblkk = if (Mat.hasCUDA > 3) 2 else 1 - val rblk = rblkk*(math.max(1, math.ceil(c.nrows/maxrows/rblkk).toInt)) - val cblk = cblkk*(math.max(1, math.ceil(c.ncols/maxcols/cblkk).toInt)) - val kblk = math.max(1, math.ceil(a.ncols/maxcols).toInt) - val gcrows = 32*(c.nrows/rblk/32) - val gccols = 32*(c.ncols/cblk/32) - val garows = gcrows - val gacols = 32*(a.ncols/kblk/32) - val gbrows = gccols - val gbcols = gacols - - val done = IMat(rblkk*cblkk,1) - for (ix <- 0 until rblkk) { - for (iy <- 0 until cblkk) { - Future { - val ithread = ix+iy*2 - var err = 0 - SciFunctions.setGPU(ithread) - val pinv = if (takeroot) GDMat(1f/p) else null:GDMat - val ga = GDMat(garows, gacols) - val gb = GDMat(gbrows, gbcols) - val gc = GDMat(gcrows, gccols) - val aa = ga.pdata - val bb = gb.pdata - val cc = gc.pdata - var i = ix*gcrows; - while (i < c.nrows) { - val ni = math.min(gcrows, c.nrows - i) - var j = iy*gccols; - while (j < c.ncols) { - val nj = math.min(gccols, c.ncols - j) - var k = 0; - cudaMemset(cc, 0, 1L*gcrows*gccols*Sizeof.DOUBLE) - cudaStreamSynchronize(Mat.SyncMethod) - while (k < a.ncols) { - val nk = math.min(gacols, a.ncols - k) - err = cudaMemcpy2D(aa, garows*Sizeof.DOUBLE, Pointer.to(a.data).withByteOffset(1L*(i+k*a.nrows)*Sizeof.DOUBLE), - a.nrows*Sizeof.DOUBLE, ni*Sizeof.DOUBLE, nk, cudaMemcpyHostToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - if (err != 0) throw new RuntimeException("LXdist copy a failed "+err) - err = cudaMemcpy2D(bb, gbrows*Sizeof.DOUBLE, Pointer.to(b.data).withByteOffset(1L*(j+k*b.nrows)*Sizeof.DOUBLE), - b.nrows*Sizeof.DOUBLE, nj*Sizeof.DOUBLE, nk, cudaMemcpyHostToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - if (err != 0) throw new RuntimeException("LXdist copy b failed "+err) - - err=CUMATD.distances(aa, garows, bb, gbrows, cc, gcrows, nk, ni, nj, p) - - // if (err != 0) throw new RuntimeException("CUDA error in LXdist %d thread %d %d %d %d" format (err, ithread, nk, ni, nj)) - if (err != 0) println("CUDA error in LXdist %d thread %d %d %d %d" format (err, ithread, nk, ni, nj)) - k += gacols - } - if (takeroot) err = CUMAT.applydop(cc, ni, nj, pinv.pdata, 1, 1, cc, BinOp.op_pow) - if (err != 0) throw new RuntimeException("LXdist scale c failed "+err) - err = cudaMemcpy2D(Pointer.to(c.data).withByteOffset(1L*(i+j*c.nrows)*Sizeof.DOUBLE), 1L*c.nrows*Sizeof.DOUBLE, - cc, 1L*gcrows*Sizeof.DOUBLE, 1L*ni*Sizeof.DOUBLE, nj, cudaMemcpyDeviceToHost) - cudaStreamSynchronize(Mat.SyncMethod) - if (err != 0) throw new RuntimeException("LXdist copy c failed "+err) - j += cblkk*gccols - } - i += rblkk*gcrows - } - gc.free - gb.free - ga.free - if (takeroot) pinv.free - done(ithread,0) = 1 - } - } - } - while (SciFunctions.mini(done).v == 0) Thread.`yield` - SciFunctions.setGPU(0) - Mat.nflops += 3L * c.nrows * c.ncols * a.ncols - c - } - - def sortdown2(a:DMat) = _sort2(a, true) - - def _sort2(a:DMat, asc:Boolean):(DMat, IMat) = { - if (a.ncols != 1) throw new RuntimeException("_sort2 works only on column pdata") - val outv = DMat.newOrCheckDMat(a.nrows, a.ncols, null, a.GUID, "_sort2_1".hashCode) - val outi = IMat.newOrCheckIMat(a.nrows, a.ncols, null, a.GUID, "_sort2_2".hashCode) - if (Mat.hasCUDA > 0) { - val (dmy, freebytes, allbytes) = SciFunctions.GPUmem - if (a.length*26L < freebytes) { - var i = 0; while (i < a.nrows) {outi(i) = i; i += 1} - val gv = GDMat(a.nrows, 2*a.ncols); - val gi = GMat(outi.nrows, outi.ncols) - gi <-- outi; - var err = cudaMemcpy(gv.pdata, Pointer.to(a.data), 1L*a.nrows*Sizeof.DOUBLE, cudaMemcpyKind.cudaMemcpyHostToDevice) - if (err != 0) throw new RuntimeException("sortGPU copy v error %d" format err) - cudaStreamSynchronize(Mat.SyncMethod) - CUMATD.dsortk(gv.pdata, gi.pdata, a.nrows, if (asc) 1 else 0) - err = cudaMemcpy(Pointer.to(outv.data), gv.pdata, 1L*a.nrows*Sizeof.DOUBLE, cudaMemcpyKind.cudaMemcpyDeviceToHost) - if (err != 0) throw new RuntimeException("sortGPU copy v error %d" format err) - outi <-- gi - gi.free - gv.free - } else { - DenseMat.sort2(a, 1, false, outv, outi) - } - } else { - DenseMat.sort2(a, 1, false, outv, outi) - } - (outv, outi) - } - -} \ No newline at end of file diff --git a/src/main/scala/BIDMat/GDMat.scala b/src/main/scala/BIDMat/GDMat.scala deleted file mode 100755 index 9592b267..00000000 --- a/src/main/scala/BIDMat/GDMat.scala +++ /dev/null @@ -1,2069 +0,0 @@ -package BIDMat -import jcuda._ -import jcuda.runtime._ -import jcuda.runtime.JCuda._ -import jcuda.runtime.cudaError._ -import jcuda.runtime.cudaMemcpyKind._ -import jcuda.jcublas._ -import jcuda.jcublas.JCublas2._ -import jcuda.jcusparse._ -import scala.concurrent.Future -import scala.concurrent.ExecutionContext.Implicits.global -import edu.berkeley.bid.CUMAT -import edu.berkeley.bid.CUMATD -import scala.util.hashing.MurmurHash3 -import edu.berkeley.bid.MurmurHash3.MurmurHash3_x64_64 -import GSDMat._ -import GMat.BinOp -import java.io._ - -class GDMat(dims0:Array[Int], @transient var pdata:Pointer, val realsize:Long) extends DMat(dims0, null) { - import GMat.BinOp._ - - /** 2D Constructor */ - def this(nr:Int, nc:Int, pdata:Pointer, realsize:Long) = this(Array(nr, nc), pdata, realsize); - - override def mytype = "GDMat"; - - /** hold indices in GPU mem */ - val ginds = new Array[GIMat](ndims); - - override def t = { - val out = GDMat.newOrCheckGDMat(ncols, nrows, null, GUID, "t".##) - CUMATD.transpose(this.pdata, nrows, out.pdata, ncols, nrows, ncols) - out - } - - override def dv:Double = - if (nrows > 1 || ncols > 1) { - throw new RuntimeException("Matrix should be 1x1 to extract value") - } else { - toDMat(null).data(0) - } - - override def fv:Float = - if (nrows > 1 || ncols > 1) { - throw new RuntimeException("Matrix should be 1x1 to extract value") - } else { - toDMat(null).data(0).toFloat - } - - override def contents() = { - new GDMat(length, 1, pdata, realsize) - } - - override def view(nr:Int, nc:Int):GDMat = { - if (1L * nr * nc > realsize) { - throw new RuntimeException("view dimensions too large") - } - if (nr == nrows && nc == ncols) { - this - } else { - val out = new GDMat(nr, nc, pdata, realsize); - out.setGUID(MurmurHash3.mix(MurmurHash3.mix(nr, nc), (GUID*3145341).toInt)); - out - } - } - - var saveMe:DMat = null - - private def writeObject(out:ObjectOutputStream):Unit = { - saveMe = DMat(this); - out.defaultWriteObject(); - } - - private def readObject(in:ObjectInputStream):Unit = { - in.defaultReadObject(); - val gpu = SciFunctions.getGPU; - SciFunctions.setGPU(myGPU); - pdata = GDMat(saveMe).pdata; - SciFunctions.setGPU(gpu); - saveMe = null; - } - - def getIndxWrapper(i:Int):GIMat = { - if (ginds(i).asInstanceOf[AnyRef] == null) { - ginds(i) = GIMat(1,1) - } - ginds(i); - } - - def getIndexValue(i:Int, v:Int):GIMat = { - getIndxWrapper(i); - ginds(i).set(v); - ginds(i); - } - - def getIndexMat(i:Int, v:IMat):GIMat = { - if (v.length == 0 || v.length > 1) { - GIMat(v); - } else { - getIndxWrapper(i).set(v.v); - ginds(i); - } - } - - def safePointer(ind:GIMat):Pointer = { - if (ind.asInstanceOf[AnyRef] == null) { - GMat.nullPointer; - } else { - ind.pdata; - } - } - - /** 1D access */ - - override def apply(ind:Int):Double = { - val tmp = new Array[Double](1); - GDMat.GPUtoCPUarraycopy(pdata, ind, tmp, 0, 1, "GMat apply"); - tmp(0) - } - - /** 2D access */ - - override def apply(i:Int, j:Int):Double = { - val tmp = new Array[Double](1); - GDMat.GPUtoCPUarraycopy(pdata, i + nrows * j, tmp, 0, 1, "GMat apply"); - tmp(0) - } - - /** ND access */ - - override def applyv(inds:Array[Int]):Double = { - val indx = ND.linearize(inds, dims.data); - val tmp = new Array[Double](1); - GDMat.GPUtoCPUarraycopy(pdata, indx, tmp, 0, 1, "GMat apply"); - tmp(0); - } - - /** apply to an index IMat, and mirror its structure in the result */ - - override def apply(inds:IMat):GDMat = { - inds match { - case aa:MatrixWildcard => { - val out = GDMat.newOrCheckGDMat(length, 1, null, GUID, inds.GUID, "apply(?)".##); - GDMat.GPUtoGPUarraycopy(pdata, 0, out.pdata, 0, length, "GDMat IMat apply" ); - out - } - case _ => { - val newinds = getIndexMat(0, inds); - val out = GDMat.newOrCheckGDMat(inds.dims, null, GUID, inds.GUID, "apply IMat".##); - val err = CUMATD.copyFromInds(pdata, out.pdata, safePointer(newinds), inds.length); - if (err != 0) throw new RuntimeException("GDMat apply(I) error " + cudaGetErrorString(err)); - out; - } - } - } - - override def apply(i1:IMat, i2:IMat):GDMat = applyi(Array(i1, i2), null); - override def apply(i1:IMat, i2:Int):GDMat = applyi(Array(i1, IMat.ielem(i2)), null); - override def apply(i1:Int, i2:IMat):GDMat = applyi(Array(IMat.ielem(i1), i2), null); - - override def applyi(inds:Array[IMat]):GDMat = applyi(inds, null); - - def applyi(inds:Array[IMat], omat:Mat):GDMat = { - if (inds.length > 2 && inds.length != _dims.length) throw new RuntimeException("GDMat applyi dims must match"); - val mydims = if (inds.length == 1) Array(length) else if (inds.length == 2) Array(nrows, ncols) else _dims; - val newdims = new Array[Int](inds.length) - val newinds = new Array[GIMat](inds.length) - for (i <- 0 until inds.length) { - inds(i) match { - case aa:MatrixWildcard => { - newdims(i) = mydims(i); - } - case _ => { - newdims(i) = inds(i).length; - newinds(i) = getIndexMat(i, inds(i)); - } - } - } - val out = GDMat.newOrCheckGDMat(newdims, omat, GUID, ND.hashGUIDs(inds), "GDMat.apply".##); - inds.length match { - case 1 => { - val err = CUMATD.copyFromInds(pdata, out.pdata, safePointer(newinds(0)), newdims(0)); - if (err != 0) throw new RuntimeException("GDMat apply(I) error " + cudaGetErrorString(err)); - } - case 2 => { - val err = CUMATD.copyFromInds2D(pdata, dims(0), out.pdata, newdims(0), safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1)); - if (err != 0) throw new RuntimeException("GDMat apply(I, J) error " + cudaGetErrorString(err)); - } - case 3 => { - val err = CUMATD.copyFromInds3D(pdata, dims(0), dims(1), out.pdata, newdims(0), newdims(1), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1), safePointer(newinds(2)), newdims(2)); - if (err != 0) throw new RuntimeException("GDMat apply(I, J, K) error " + cudaGetErrorString(err)); - } - case 4 => { - val err = CUMATD.copyFromInds4D(pdata, dims(0), dims(1), dims(2), out.pdata, newdims(0), newdims(1), newdims(2), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1), safePointer(newinds(2)), newdims(2), safePointer(newinds(3)), newdims(3)); - if (err != 0) throw new RuntimeException("GDMat apply(I, J, K, L) error " + cudaGetErrorString(err)); - } - case _ => throw new RuntimeException("GDMat slice access with more than 4 indices not supported"); - } - out; - } - - /** 1D update */ - - override def update(i:Int, v:Double):GDMat = { - val tmp = new Array[Double](1); - tmp(0) = v; - GDMat.CPUtoGPUarraycopy(tmp, 0, pdata, i, 1, "GDMat update"); - this - } - - /** 2D update */ - - override def update(i:Int, j:Int, v:Double):GDMat = { - val tmp = new Array[Double](1); - tmp(0) = v; - GDMat.CPUtoGPUarraycopy(tmp, 0, pdata, i + j * nrows, 1, "GDMat update"); - this - } - - /** ND update */ - - override def updatev(inds:Array[Int], v:Double):GDMat = { - val indx = ND.linearize(inds, _dims); - val tmp = Array[Double](v); - GDMat.CPUtoGPUarraycopy(tmp, 0, pdata, indx, 1, "GDMat update"); - this - } - - /** ND sliced updates */ - override def update(iv:IMat, b:DMat):DMat = updatei(Array(iv), GDMat(b)); - override def update(iv:IMat, jv:IMat, b:DMat):DMat = updatei(Array(iv, jv), GDMat(b)); - override def update(iv:IMat, j:Int, b:DMat):DMat = updatei(Array(iv, IMat.ielem(j)), GDMat(b)); - override def update(i:Int, jv:IMat, b:DMat):DMat = updatei(Array(IMat.ielem(i), jv), GDMat(b)); - - /** ND sliced updates with generic RHS */ - override def update(iv:IMat, b:Mat):DMat = updatei(Array(iv), GDMat(b)); - override def update(iv:IMat, jv:IMat, b:Mat):DMat = updatei(Array(iv, jv), GDMat(b)); - override def update(iv:IMat, j:Int, b:Mat):DMat = updatei(Array(iv, IMat.ielem(j)), GDMat(b)); - override def update(i:Int, jv:IMat, b:Mat):DMat = updatei(Array(IMat.ielem(i), jv), GDMat(b)); - - override def update(i1:IMat, vv:Double):DMat = updatei(Array(i1), vv); - override def update(i1:IMat, i2:IMat, vv:Double):DMat = updatei(Array(i1, i2), vv); - - override def updatei(inds:Array[IMat], vv:DMat):GDMat = updatei(inds, GDMat(vv)); - - def updatei(inds:Array[IMat], vv:GDMat):GDMat = { - if (inds.length > 2 && inds.length != _dims.length) throw new RuntimeException("GDMat update dims must match"); - val mydims = if (inds.length == 1) Array(length) else if (inds.length == 2) Array(nrows, ncols) else _dims; - val newdims = new Array[Int](inds.length) - val newinds = new Array[GIMat](inds.length) - for (i <- 0 until inds.length) { - inds(i) match { - case aa:MatrixWildcard => { - newdims(i) = mydims(i); - } - case _ => { - newdims(i) = inds(i).length; - newinds(i) = getIndexMat(i, inds(i)); - } - } - } - ND.checkDims("GDMat update:", ND.trimDims(newdims), ND.trimDims(vv._dims)); - inds.length match { - case 1 => { - val err = CUMAT.copyToIndsLong(vv.pdata, pdata, safePointer(newinds(0)), newdims(0)); - if (err != 0) throw new RuntimeException("GDMat update (I) error " + cudaGetErrorString(err)); - } - case 2 => { - val err = CUMATD.copyToInds2D(vv.pdata, vv.dims(0), pdata, nrows, - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1)); - if (err != 0) throw new RuntimeException("GDMat update (I, J) error " + cudaGetErrorString(err)); - } - case 3 => { - val err = CUMATD.copyToInds3D(vv.pdata, vv.dims(0), vv.dims(1), pdata, dims(0), dims(1), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1), safePointer(newinds(2)), newdims(2)); - if (err != 0) throw new RuntimeException("GDMat update (I, J, K) error " + cudaGetErrorString(err)); - } - case 4 => { - val err = CUMATD.copyToInds4D(vv.pdata, vv.dims(0), vv.dims(1), vv.dims(2), pdata, dims(0), dims(1), dims(2), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1), safePointer(newinds(2)), newdims(2), safePointer(newinds(3)), newdims(3)); - if (err != 0) throw new RuntimeException("GDMat udpate (I, J, K, L) error " + cudaGetErrorString(err)); - } - case _ => throw new RuntimeException("GDMat slice access with more than 4 indices not supported"); - } - this - } - - override def updatei(inds:Array[IMat], vv:Double):GDMat = { - if (inds.length > 2 && inds.length != _dims.length) throw new RuntimeException("GDMat update dims must match"); - val mydims = if (inds.length == 1) Array(length) else if (inds.length == 2) Array(nrows, ncols) else _dims; - val newdims = new Array[Int](inds.length); - val newinds = new Array[GIMat](inds.length); - for (i <- 0 until inds.length) { - inds(i) match { - case aa:MatrixWildcard => { - newdims(i) = mydims(i); - } - case _ => { - newdims(i) = inds(i).length; - newinds(i) = getIndexMat(i, inds(i)); - } - } - } - inds.length match { - case 1 => { - val err = CUMAT.fillToIndsDouble(vv, pdata, safePointer(newinds(0)), newdims(0)); - if (err != 0) throw new RuntimeException("GDMat update (I) error" + cudaGetErrorString(err)); - } - case 2 => { - val err = CUMATD.fillToInds2D(vv, pdata, dims(0), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1)); - if (err != 0) throw new RuntimeException("GDMat update (I, J) error" + cudaGetErrorString(err)); - } - case 3 => { - val err = CUMATD.fillToInds3D(vv, pdata, dims(0), dims(1), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1), safePointer(newinds(2)), newdims(2)); - if (err != 0) throw new RuntimeException("GDMat update (I, J, K) error" + cudaGetErrorString(err)); - } - case 4 => { - val err = CUMATD.fillToInds4D(vv, pdata, dims(0), dims(1), dims(2), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1), safePointer(newinds(2)), newdims(2), safePointer(newinds(3)), newdims(3)); - if (err != 0) throw new RuntimeException("GDMat udpate (I, J, K, L) error" + cudaGetErrorString(err)); - } - case _ => throw new RuntimeException("GDMat slice access with more than 4 indices not supported"); - } - this - } - - - override def colslice(a:Int, b:Int):GDMat = colslice(a, b, null, 0); - - override def colslice(a:Int, b:Int, omat:Mat):GDMat = colslice(a, b, omat, 0); - - override def colslice(a:Int, b:Int, omat:Mat, c:Int):GDMat = { - val newdims = _dims.clone; - newdims(dims.length-1) = b-a+c; - val out = if (omat.asInstanceOf[AnyRef] != null && omat.isInstanceOf[GDMat] && omat.ncols >= b-a+c && omat.nrows == nrows) { - omat.asInstanceOf[GDMat] - } else { - GDMat.newOrCheckGDMat(newdims, omat, GUID, a, b, c, "colslice".##); - } - cudaMemcpy(out.pdata.withByteOffset(1L*c*nrows*Sizeof.DOUBLE), pdata.withByteOffset(1L*a*nrows*Sizeof.DOUBLE), 1L*(b-a)*nrows*Sizeof.DOUBLE, cudaMemcpyDeviceToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError; - if (err != 0) throw new RuntimeException("GDMat colslice() error " + cudaGetErrorString(err)); - out - } - - override def colslice(a:Int, b:Int, omat:Mat, c:Int, pb:Boolean):GDMat = colslice(a, b, omat, c); - - val myGPU = SciFunctions.getGPU; - - /** reshaping */ - - override def reshape(newdims:Int*):DMat = reshape(newdims.toArray) - - override def reshape(newdims:Array[Int]):DMat = { - if (newdims.reduce(_*_) == length) { - val out = GDMat.newOrCheckGDMat(newdims, null, GUID, ND.hashInts(newdims), "reshape".##); - cudaMemcpy(out.pdata, pdata, 1L*llength*Sizeof.DOUBLE, cudaMemcpyDeviceToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError; - if (err != 0) throw new RuntimeException("GDMat reshape() error " + cudaGetErrorString(err)); - out - } else { - throw new RuntimeException("GDMat reshape total length doesnt match") - } - } - - override def reshapeView(newdims:Int*):DMat = reshapeView(newdims.toArray) - - override def reshapeView(newdims:Array[Int]):DMat = { - if (newdims.reduce(_*_) == length) { - val out = new GDMat(newdims, pdata, llength); - out.setGUID(MurmurHash3_x64_64(Array(GUID), "reshapeView".##)); - out - } else { - throw new RuntimeException("FMat reshapeView total length doesnt match") - } - } - - override def reshapeView(adims:IMat):DMat = reshapeView(adims.data); - - override def reshapeTrim(newdims:Int*):DMat = reshapeTrim(newdims.toArray) - - override def reshapeTrim(newdims:Array[Int]):DMat = { - if (newdims.reduce(_*_) <= realsize) { - val out = new GDMat(newdims, pdata, realsize); - out.setGUID(MurmurHash3_x64_64(Array(GUID), "reshapeTrim".##)); - out - } else { - throw new RuntimeException("FMat reshapeTrim total length too large") - } - } - - override def reshapeTrim(adims:IMat):DMat = reshapeTrim(adims.data); - - /** transpose */ - override def transpose(dims:Array[Int]):GDMat = transpose(MatFunctions.irow(dims)) - - override def transpose(perm:IMat):GDMat = { - val nd = _dims.length - if (perm.length != nd) { - throw new RuntimeException("GDMat transpose bad permutation ") - } - if (ND.isIdentity(perm)) { - this - } else { - val xdims = MatFunctions.irow(_dims); - val iperm = MatFunctions.invperm(perm); - val pdims = xdims(perm).data; - var out = GDMat.newOrCheckGDMat(pdims, null, GUID, ND.hashInts(pdims), "transpose".##); - var out2 = GDMat.newOrCheckGDMat(pdims, null, GUID, ND.hashInts(pdims), "transpose1".##); - cudaMemcpy(out.pdata, pdata, 1L*Sizeof.DOUBLE*length, cudaMemcpyDeviceToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - for (i <- (nd - 1) until 0 by -1) { - if (iperm(i) != i) { - val (d1, d2, d3) = ND.getDims(i, iperm, xdims); - if (d1 > 1 && d2 > 1) { - // println("spermute %d %d %d" format (d1,d2,d3)) - CUMATD.dpermute(d1, d2, d3, out.pdata, out2.pdata); - val tmp = out2; - out2 = out; - out = tmp; - } - ND.rotate(i, iperm, xdims); - } - } - out; - } - } - - override def clear = { - cudaMemset(pdata, 0, Sizeof.DOUBLE*length) - cudaStreamSynchronize(Mat.SyncMethod) - this - } - - override def set(v:Double):GDMat = { - CUMATD.setval(pdata, v, length) - cudaStreamSynchronize(Mat.SyncMethod) - this - } - - override def set(v:Float):GDMat = set(v.toDouble) - - override def toString:String = { - val nr = scala.math.min(nrows,10) - val nc = scala.math.min(ncols,50) - val tmpMat = DMat(nr, nc) - cublasGetMatrix(nr, nc, Sizeof.DOUBLE, pdata, nrows, Pointer.to(tmpMat.data), nr) - cudaStreamSynchronize(Mat.SyncMethod) - tmpMat.toString - } - - override def zeros(nr:Int, nc:Int) = GDMat.zeros(nr, nc) - - override def ones(nr:Int, nc:Int) = GDMat.ones(nr, nc) - - override def zeros(dims:IMat) = GDMat.zeros(dims) - - override def ones(dims:IMat) = GDMat.ones(dims) - - override def zero = GDMat.zeros(1, 1); - - override def one = GDMat.ones(1, 1); - - override def izeros(m:Int, n:Int) = { - GIMat.izeros(m,n) - } - - override def iones(m:Int, n:Int) = { - GIMat.iones(m,n) - } - - def horzcat(a:GDMat, omat:Mat) = { - if (nrows != a.nrows) - throw new RuntimeException("GMat \\ row dims not equal") - val out = GDMat.newOrCheckGDMat(nrows, ncols+a.ncols, omat, GUID, a.GUID, "horzcat".##) - cudaMemcpy(out.pdata, pdata, 1L*length*Sizeof.DOUBLE, cudaMemcpyKind.cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - cudaMemcpy(out.pdata.withByteOffset(1L*length*Sizeof.DOUBLE), a.pdata, 1L*a.length*Sizeof.DOUBLE, cudaMemcpyKind.cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - out - } - - def vertcat(a:GDMat, omat:Mat) = { - if (ncols != a.ncols) - throw new RuntimeException("GDMat on row dims not equal") - val out = GDMat.newOrCheckGDMat(nrows+a.nrows, ncols, omat, GUID, a.GUID, "vertcat".##) - cudaMemcpy2D(out.pdata, 1L*out.nrows*Sizeof.DOUBLE, pdata, 1L*nrows*Sizeof.DOUBLE, 1L*nrows*Sizeof.DOUBLE, 1L*ncols, cudaMemcpyKind.cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - cudaMemcpy2D(out.pdata.withByteOffset(1L*nrows*Sizeof.DOUBLE), 1L*out.nrows*Sizeof.DOUBLE, a.pdata, 1L*a.nrows*Sizeof.DOUBLE, 1L*a.nrows*Sizeof.DOUBLE, 1L*a.ncols, cudaMemcpyKind.cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - out - } - - def getHandle = { - val igpu = Array(0); - jcuda.runtime.JCuda.cudaGetDevice(igpu) - GFunctions.cublasHandles(igpu(0)).asInstanceOf[cublasHandle]; - } - - def GMult(aa:DMat, oldmat:Mat):GDMat = { - val a = GDMat(aa); - if (ncols == 1 && nrows == 1) { - val out = GDMat.newOrCheckGDMat(a.nrows, a.ncols, oldmat, GUID, a.GUID, "GMult1".##) - Mat.nflops += 1L * a.length - val err = CUMAT.applydop(pdata, nrows, ncols, a.pdata, a.nrows, a.ncols, out.pdata, GMat.BinOp.op_mul) - if (err != 0) {throw new RuntimeException("GMult: CUDA kernel error in applyop " + cudaGetErrorString(err))} - out - } else if (a.ncols == 1 && a.nrows == 1) { - val out = GDMat.newOrCheckGDMat(nrows, ncols, oldmat, GUID, a.GUID, "GMult2".##) - Mat.nflops += 1L * length - val err = CUMAT.applydop(pdata, nrows, ncols, a.pdata, a.nrows, a.ncols, out.pdata, GMat.BinOp.op_mul) - if (err != 0) {throw new RuntimeException("GMult: CUDA kernel error in CUMAT.applyop " + cudaGetErrorString(err))} - out - } else if (ncols == a.nrows) { - val out = GDMat.newOrCheckGDMat(nrows, a.ncols, oldmat, GUID, a.GUID, "GMult".##) - Mat.nflops += 2L * length * a.ncols - if (nrows == 1) { - // cublasSgemv('t', a.nrows, a.ncols, 1.0f, a.pdata, nrows, pdata, 1, 0f, out.pdata, 1) - out.clear - val err = CUMATD.dmv(a.pdata, a.nrows, a.ncols, pdata, out.pdata, 1) - if (err != 0) {throw new RuntimeException("GMult: CUDA kernel error in CUMAT.dmv " + cudaGetErrorString(err))} - } else if (a.ncols == 1) { - // cublasSgemv('n', nrows, ncols, 1.0f, pdata, nrows, a.pdata, 1, 0f, out.pdata, 1) - out.clear - val err = CUMATD.dmv(pdata, nrows, ncols, a.pdata, out.pdata, 0) - if (err != 0) {throw new RuntimeException("GMult: CUDA kernel error in CUMAT.dmv " + cudaGetErrorString(err))} - } else { - cublasDgemm(getHandle, cublasOperation.CUBLAS_OP_N, cublasOperation.CUBLAS_OP_N, nrows, a.ncols, ncols, GDMat.pONE, pdata, nrows, a.pdata, a.nrows, GDMat.pZERO, out.pdata, nrows) - cudaStreamSynchronize(Mat.SyncMethod) - val err = cudaGetLastError - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cublas error in * "+err) - } - } - - out - } else throw new RuntimeException("dimensions mismatch") - } - - def GMultT(aa:DMat, oldmat:Mat):GDMat = { - val a = GDMat(aa); - if (ncols == a.ncols) { - val out = GDMat.newOrCheckGDMat(nrows, a.nrows, oldmat, GUID, a.GUID, "GMultT".##) - Mat.nflops += 2L * length * a.nrows - cublasDgemm(getHandle, cublasOperation.CUBLAS_OP_N, cublasOperation.CUBLAS_OP_T, nrows, a.nrows, ncols, GDMat.pONE, pdata, nrows, a.pdata, a.nrows, GDMat.pZERO, out.pdata, nrows) - cudaStreamSynchronize(Mat.SyncMethod) - val err = cudaGetLastError - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cublas error in xT " + cudaGetErrorString(err)) - } - out - } else throw new RuntimeException("dimensions mismatch") - } - - def GTMult(aa:DMat, oldmat:Mat):GDMat = { - val a = GDMat(aa); - if (nrows == a.nrows) { - val out = GDMat.newOrCheckGDMat(ncols, a.ncols, oldmat, GUID, a.GUID, "GMultT".##) - Mat.nflops += 2L * length * a.ncols - cublasDgemm(getHandle, cublasOperation.CUBLAS_OP_T, cublasOperation.CUBLAS_OP_N, ncols, a.ncols, nrows, GDMat.pONE, pdata, nrows, a.pdata, a.nrows, GDMat.pZERO, out.pdata, out.nrows) - cudaStreamSynchronize(Mat.SyncMethod) - val err = cudaGetLastError - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cublas error in Tx " + cudaGetErrorString(err)) - } - out - } else throw new RuntimeException("dimensions mismatch") - } - - override def madd(bb:DMat, cc:DMat, at:Boolean, bt:Boolean):GDMat = { - val b = GDMat(bb); - val c = GDMat(cc); - val (arows, acols, atrans) = if (at) (ncols, nrows, cublasOperation.CUBLAS_OP_T) else (nrows, ncols, cublasOperation.CUBLAS_OP_N); - val (brows, bcols, btrans) = if (bt) (b.ncols, b.nrows, cublasOperation.CUBLAS_OP_T) else (b.nrows, b.ncols, cublasOperation.CUBLAS_OP_N); - if (acols != brows || arows != c.nrows || bcols != c.ncols) { - throw new RuntimeException("madd bad dimensions (%d %d) (%d %d) (%d %d)" format (arows, acols, brows, bcols, c.nrows, c.ncols)); - } - Mat.nflops += 2L * arows * bcols * acols; - cublasDgemm(getHandle, atrans, btrans, arows, bcols, acols, GDMat.pONE, pdata, nrows, b.pdata, b.nrows, GDMat.pONE, c.pdata, c.nrows); - c - } - - override def madd(b:DMat, c:DMat):GDMat = madd(b, c, false, false); - - override def madd(b:Mat, c:Mat, at:Boolean, bt:Boolean):Mat = { - (b, c) match { - case (bb:DMat, cc:DMat) => madd(bb, cc, at, bt) - } - c - } - - override def madd(b:Mat, c:Mat):Mat = madd(b, c, false, false); - - override def blockmult(bb:DMat, cc:DMat, nblocks:Int, at:Boolean, bt:Boolean, cfact:Float):DMat = { - val b = GDMat(bb); - val c = GDMat(cc); - - val (anrows, ancols) = if (dims.length >= 3) { - (dims(0), dims(1)) - } else { - (nrows/nblocks, ncols) - } - val (bnrows, bncols) = if (b.dims.length >= 3) { - (b.dims(0), b.dims(1)) - } else { - (b.nrows/nblocks, b.ncols) - } - val (cnrows,cncols) = if (c.dims.length >= 3) { - (c.dims(0), c.dims(1)) - } else { - (c.nrows/nblocks, c.ncols) - } - blockGemm(if (at) 1 else 0, if (bt) 1 else 0, cnrows, cncols, if (at) anrows else ancols, 1f, 0, anrows, anrows*ancols, - b, 0, bnrows, bnrows*bncols, cfact, c, 0, cnrows, cnrows*cncols, nblocks); - c - } - - override def blockmult(b:DMat, c:DMat, nblocks:Int, at:Boolean, bt:Boolean):DMat = blockmult(b, c, nblocks, at, bt, 0f); - - override def blockmult(b:Mat, c:Mat, nblocks:Int):Mat = blockmult(b, c, nblocks, false, false); - - override def blockmult(b:Mat, c:Mat, nblocks:Int, at:Boolean, bt:Boolean):Mat = { - (b, c) match { - case (bb:DMat, cc:DMat) => blockmult(bb, cc, nblocks, at, bt); - case _ => throw new RuntimeException("blockmult unsupported types %s %s" format (b.mytype, c.mytype)); - } - c - } - - override def blockmult2(bb:DMat, cc:DMat, nblocks:Int, at:Boolean, bt:Boolean, cfact:Float):DMat = { - val b = GDMat(bb); - val c = GDMat(cc); - - val anrows = dims(0) - val astep = dims(1) - val ancols = dims(2) - val bnrows = b.dims(0) - val bstep = b.dims(1) - val bncols = b.dims(2) - val cnrows = c.dims(0) - val cstep = c.dims(1) - val cncols = c.dims(2) - if (dims.length == 3) { - blockGemm(if (at) 1 else 0, if (bt) 1 else 0, cnrows, cncols, if (at) anrows else ancols, 1f, 0, anrows*astep, anrows, - b, 0, bnrows*bstep, bnrows, cfact, c, 0, cnrows*cstep, cnrows, nblocks); - } else { - val reps2 = dims.data.slice(3, dims.length).reduce(_*_); - blockGemm4D(if (at) 1 else 0, if (bt) 1 else 0, cnrows, cncols, if (at) anrows else ancols, 1f, 0, anrows*astep, anrows, anrows*ancols*astep, - b, 0, bnrows*bstep, bnrows, bnrows*bncols*bstep, cfact, c, 0, cnrows*cstep, cnrows, cnrows*cncols*cstep, nblocks, reps2); - } - c - } - - override def blockmult2(b:DMat, c:DMat, nblocks:Int, at:Boolean, bt:Boolean):DMat = blockmult2(b, c, nblocks, at, bt, 0f); - - override def blockmult2(b:Mat, c:Mat, nblocks:Int):Mat = blockmult2(b, c, nblocks, false, false); - - override def blockmult2(b:Mat, c:Mat, nblocks:Int, at:Boolean, bt:Boolean):Mat = { - (b, c) match { - case (bb:DMat, cc:DMat) => blockmult2(bb, cc, nblocks, at, bt); - case _ => throw new RuntimeException("blockmult2 unsupported types %s %s" format (b.mytype, c.mytype)); - } - c - } - - override def blockmadd(b:DMat, c:DMat, nblocks:Int, at:Boolean, bt:Boolean):DMat = blockmult(b, c, nblocks, at, bt, 1f); - - override def blockmadd(b:Mat, c:Mat, nblocks:Int):Mat = blockmadd(b, c, nblocks, false, false); - - override def blockmadd(b:Mat, c:Mat, nblocks:Int, at:Boolean, bt:Boolean):Mat = { - (b, c) match { - case (bb:DMat, cc:DMat) => blockmadd(bb, cc, nblocks, at, bt); - case _ => throw new RuntimeException("blockmadd unsupported types %s %s" format (b.mytype, c.mytype)); - } - c - } - - override def blockmadd2(b:DMat, c:DMat, nblocks:Int, at:Boolean, bt:Boolean):DMat = blockmult2(b, c, nblocks, at, bt, 1f); - - override def blockmadd2(b:Mat, c:Mat, nblocks:Int):Mat = blockmadd2(b, c, nblocks, false, false); - - override def blockmadd2(b:Mat, c:Mat, nblocks:Int, at:Boolean, bt:Boolean):Mat = { - (b, c) match { - case (bb:DMat, cc:DMat) => blockmadd2(bb, cc, nblocks, at, bt); - case _ => throw new RuntimeException("blockmadd2 unsupported types %s %s" format (b.mytype, c.mytype)); - } - c - } - - - def blockGemm(transa:Int, transb:Int, nr:Int, nc:Int, k:Int, alpha:Float, aoff:Int, lda:Int, astep:Int, - b:GDMat, boff:Int, ldb:Int, bstep:Int, beta:Float, c:GDMat, coff:Int, ldc:Int, cstep:Int, nreps:Int):GDMat = { - - val ax = if (transa == 0) nc else nr; - val alphad = alpha.toDouble - val betad = beta.toDouble - -// if (beta == 0f) c.clear; - Mat.nflops += 2L * nr * nc * k * nreps; - if (lda > astep || ldb > bstep || ldc > cstep) { - CUMATD.myCublasDgemmStridedBatched( - getHandle, transa, transb, - nr, nc, k, - alphad, - pdata.withByteOffset(1L * Sizeof.DOUBLE * aoff), lda, astep, - b.pdata.withByteOffset(1L * Sizeof.DOUBLE * boff), ldb, bstep, - betad, - c.pdata.withByteOffset(1L * Sizeof.DOUBLE * coff), ldc, cstep, - nreps); - } else { - cublasDgemmStridedBatched( - getHandle, transa, transb, - nr, nc, k, - Pointer.to(Array(alphad)), - pdata.withByteOffset(1L * Sizeof.DOUBLE * aoff), lda, astep, - b.pdata.withByteOffset(1L * Sizeof.DOUBLE * boff), ldb, bstep, - Pointer.to(Array(betad)), - c.pdata.withByteOffset(1L * Sizeof.DOUBLE * coff), ldc, cstep, - nreps); - } - cudaStreamSynchronize(Mat.SyncMethod) - - val err = cudaGetLastError() - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in GDMat blockGemm " + cudaGetErrorString(err)) - } - c; - } - - override def blockGemm(transa:Int, transb:Int, nr:Int, nc:Int, k:Int, alpha:Float, aoff:Int, lda:Int, astep:Int, - b:Mat, boff:Int, ldb:Int, bstep:Int, beta:Float, c:Mat, coff:Int, ldc:Int, cstep:Int, nreps:Int):GDMat = { - blockGemm(transa, transb, nr, nc, k, alpha, aoff, lda, astep, b.asInstanceOf[GDMat], boff, ldb, bstep, - beta, c.asInstanceOf[GDMat], coff, ldc, cstep, nreps); - } - - def blockGemm4D(transa:Int, transb:Int, nr:Int, nc:Int, k:Int, alpha:Float, - aoff:Int, lda:Int, astep1:Int, astep2:Int, - b:GDMat, boff:Int, ldb:Int, bstep1:Int, bstep2:Int, beta:Float, - c:GDMat, coff:Int, ldc:Int, cstep1:Int, cstep2:Int, nreps1:Int, nreps2:Int):GDMat = { - - Mat.nflops += 2L * nr * nc * k * nreps1 * nreps2; - CUMATD.myCublasDgemmStridedBatched4D( - getHandle, transa, transb, nr, nc, k, alpha, - pdata.withByteOffset(1L * Sizeof.DOUBLE * aoff), lda, astep1, astep2, - b.pdata.withByteOffset(1L * Sizeof.DOUBLE * boff), ldb, bstep1, bstep2, beta, - c.pdata.withByteOffset(1L * Sizeof.DOUBLE * coff), ldc, cstep1, cstep2, - nreps1, nreps2); - cudaStreamSynchronize(Mat.SyncMethod) - - val err = cudaGetLastError() - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in GDMat blockGemm4D " + cudaGetErrorString(err)) - } - c; - } - - override def blockGemm4D(transa:Int, transb:Int, nr:Int, nc:Int, k:Int, alpha:Float, aoff:Int, lda:Int, astep1:Int, astep2:Int, - b:Mat, boff:Int, ldb:Int, bstep1:Int, bstep2:Int, beta:Float, c:Mat, coff:Int, ldc:Int, cstep1:Int, cstep2:Int, nreps1:Int, nreps2:Int):GDMat = { - blockGemm4D(transa, transb, nr, nc, k, alpha, aoff, lda, astep1, astep2, b.asInstanceOf[GDMat], boff, ldb, bstep1, bstep2, - beta, c.asInstanceOf[GDMat], coff, ldc, cstep1, cstep2, nreps1, nreps2); - } - - def GSMult(aa:SDMat, oldmat:Mat):GDMat = { - val a = GSDMat(aa); - if (ncols == a.nrows) { - val out = GDMat.newOrCheckGDMat(nrows, a.ncols, oldmat, GUID, a.GUID, "GSMult".##) - Mat.nflops += 2L * nrows * a.nnz -/* if (nrows == 1) { // Alas, throws "too many resources requested for launch" with large a.nrows - val handle = GSDMat.getHandle // Also gives erroneous values - val descra = GSDMat.getDescr - var err = JCusparse.cusparseScsrmv(handle, cusparseOperation.CUSPARSE_OPERATION_NON_TRANSPOSE, - ncols, a.ncols, 1.0f, descra, a.pdata, a.jc, a.ir, pdata, 0, out.pdata) - cudaStreamSynchronize(Mat.SyncMethod)() - if (err == 0) err = cudaGetLastError - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in GSMult " + cudaGetErrorString(err)) - } - } else { */ - out.clear - val err = CUMATD.dsmult(nrows, a.ncols, a.nnz, pdata, a.pdata, a.pir, a.pic, out.pdata) - if (err != 0) throw new RuntimeException("GMult: CUDA kernel error in CUMAT.dsmult " + cudaGetErrorString(err)) -// } - out - } else throw new RuntimeException("dimensions mismatch") - } - - def GSMultT(aa:SDMat, oldmat:Mat):GDMat = { - val a = GSDMat(aa) - if (ncols == a.ncols) { - val out = GDMat.newOrCheckGDMat(nrows, a.nrows, oldmat, GUID, a.GUID, "GSMultT".##) - Mat.nflops += 2L * nrows * a.nnz - out.clear - val err = CUMATD.dsmultT(nrows, a.ncols, a.nnz, pdata, a.pdata, a.pir, a.pic, out.pdata) - if (err != 0) throw new RuntimeException("GMult: CUDA kernel error in CUMAT.dsmultT " + cudaGetErrorString(err)) - out - } else throw new RuntimeException("dimensions mismatch") - } - - def GMST(aa:DMat, oldmat:Mat):GDMat = { - val a = GDMat(aa) - if (ncols == a.ncols) { - val out = GDMat.newOrCheckGDMat(nrows, a.nrows, oldmat, GUID, a.GUID, "GMST".##) - Mat.nflops += 2L * nrows * a.nrows * ncols - out.clear - val err = CUMATD.maxsumx(pdata, nrows, a.pdata, a.nrows, out.pdata, nrows, ncols, nrows, a.nrows) - if (err != 0) throw new RuntimeException("GMult: CUDA kernel error in CUMAT.maxsumx " + cudaGetErrorString(err)) - out - } else throw new RuntimeException("dimensions mismatch") - } - - def gOp(aa:DMat, oldmat:Mat, op:Int):GDMat = { - val a = GDMat(aa); - val (nr, nc, nra, nca) = ND.compatibleGDims(_dims, aa._dims, "DenseMat Op"); - val dims = ND.maxDims(_dims, aa._dims); - val out = GDMat.newOrCheckGDMat(dims, oldmat, GUID, aa.GUID, op.hashCode); - Mat.nflops += scala.math.max(length, a.length); - val err = CUMAT.applydop(pdata, nr, nc, a.pdata, nra, nca, out.pdata, op); - if (err != 0) {throw new RuntimeException("CUDA kernel error %d in CUMATD.applyop" format err)} - out - } - - override def dot (aa:DMat, oldmat:Mat):GDMat = { - val a = GDMat(aa); - ND.checkDims("dot", dims, a.dims); - val odims = IMat.iones(1, dims.length); - odims(dims.length-1) = a.ncols; - val out = GDMat.newOrCheckGDMat(odims, oldmat, GUID, a.GUID, "dot".##); - Mat.nflops += 2L * length; - val err = CUMAT.reducebin1dop(nrows, ncols, pdata, a.pdata, out.pdata, op_mul, op_add); - if (err != 0) {throw new RuntimeException("GDMat dot: CUDA kernel error in CUMAT.reducebin1op " + cudaGetErrorString(err))} - out; - } - - override def dot (a:DMat):DMat = dot(a, null) - - override def dotr (aa:DMat, oldmat:Mat):GDMat = { - val a = GDMat(aa); - ND.checkDims("dotr", dims, a.dims); - val odims = a.dims.copy; - odims(odims.length-1) = 1; - val out = GDMat.newOrCheckGDMat(odims, oldmat, GUID, a.GUID, "dotr".##); - Mat.nflops += 2L * length; - val err = CUMAT.reducebin2dop(nrows, ncols, pdata, a.pdata, out.pdata, op_mul, op_add); - if (err != 0) {throw new RuntimeException("GDMat dotr: CUDA kernel error in CUMAT.reducebin2op " + cudaGetErrorString(err))} - out; - } - - override def dotr (a:DMat):GDMat = dotr(a, null) - - override def ddot (a:Mat):Double = - if (nrows != a.nrows || ncols != a.ncols) { - throw new RuntimeException("ddot dims not compatible") - } else { - a match { - case aa:GDMat => { - val result = Array(0.0) - cublasDdot(getHandle, length, pdata, 1, aa.pdata, 1, Pointer.to(result)) - cudaStreamSynchronize(Mat.SyncMethod) - val err = cudaGetLastError - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cublas error in ddot " + cudaGetErrorString(err)) - } - result(0) - } - } - } - - - def reduceOp(oldmat:Mat, dir:Int, initval:Double, op:Int):GDMat = { - if (dir == 1 || (dir == 0 && nrows > 1)) { - val out = GDMat.newOrCheckGDMat(1, ncols, oldmat, GUID, 1, op) - out.clear - val err = CUMAT.reduce1dop(nrows, ncols, pdata, out.pdata, initval, op) - if (err != 0) {throw new RuntimeException("GMult: CUDA kernel error in CUMAT.reduce1op " + cudaGetErrorString(err))} - Mat.nflops += length - out - } else if (dir == 2 || dir == 0) { - val out = GDMat.newOrCheckGDMat(nrows, 1, oldmat, GUID, 2, op) - out.clear - val err = CUMAT.reduce2dop(nrows, ncols, pdata, out.pdata, initval, op) - if (err != 0) {throw new RuntimeException("GMult: CUDA kernel error in CUMAT.reduce2op " + cudaGetErrorString(err))} - Mat.nflops += length - out - } else { - throw new RuntimeException("dimension must be 1 or 2") - } - } - - def toDMat(a:Mat):DMat = { - val out = DMat.newOrCheckDMat(nrows, ncols, a, GUID, "toDMat".##) - cudaMemcpy(Pointer.to(out.data), pdata, 1L*length*Sizeof.DOUBLE, cudaMemcpyDeviceToHost) - cudaStreamSynchronize(Mat.SyncMethod) - val err = cudaGetLastError - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("CUDA error in toDMat " + cudaGetErrorString(err)) - } - out - } - - def copyTo(a:DMat):DMat = { - ND.checkDims("GDMat copyTo DMat", dims, a.dims); - cudaMemcpy(Pointer.to(a.data), pdata, 1L*length*Sizeof.DOUBLE, cudaMemcpyDeviceToHost); - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError; - if (err != 0) { - println("device is %d" format SciFunctions.getGPU); - throw new RuntimeException("GDMat error in copyTo " + cudaGetErrorString(err)); - } - a; - } - - def copyTo(a:FMat):FMat = { - ND.checkDims("GDMat copyTo FMat", dims, a.dims); - val tmp = DMat.newOrCheckDMat(dims, null, GUID, "copyTo".##); - cudaMemcpy(Pointer.to(tmp.data), pdata, 1L*length*Sizeof.DOUBLE, cudaMemcpyDeviceToHost); - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError; - if (err != 0) { - println("device is %d" format SciFunctions.getGPU); - throw new RuntimeException("GDMat error in copyTo " + cudaGetErrorString(err)); - } - tmp.copyTo(a); - a - } - - def copyTo(a:GIMat):GIMat = { - ND.checkDims("GDMat copyTo GIMat", dims, a.dims); - val err = CUMATD.toInt(pdata, a.pdata, length) - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("GDMat error in copyTo " + cudaGetErrorString(err)) - } - a - } - - def copyTo(a:GMat):GMat = { - ND.checkDims("GDMat copyTo GMat", dims, a.dims); -// val a = out.recycle(nrows, ncols, 0) - CUMAT.doubleToFloat(pdata, a.pdata, length) - val err = cudaGetLastError - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("GDMat error in copyTo " + cudaGetErrorString(err)) - } - a - } - - def copyFrom(in:DMat):GDMat = { - ND.checkDims("GDMat copyFrom DMat", dims, in.dims); - cudaMemcpy(pdata, Pointer.to(in.data), 1L*nrows*ncols*Sizeof.DOUBLE, cudaMemcpyKind.cudaMemcpyHostToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - val err = cudaGetLastError - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cublas error in copyFrom " + cudaGetErrorString(err)) - } - this - } - - def copyTo(a:GDMat):GDMat = { - ND.checkDims("GDMat copyFrom GDMat", dims, a.dims); -// val a = out.recycle(nrows, ncols, 0) - cudaMemcpy(a.pdata, pdata, 1L*length*Sizeof.DOUBLE, cudaMemcpyKind.cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - val err = cudaGetLastError - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("CUDA error in copyTo " + cudaGetErrorString(err)) - } - a - } - - override def copyTo(out:Mat):Mat = { - out match { - case a:GMat => copyTo(a) - case a:GDMat => copyTo(a) - case a:FMat => copyTo(a) - case a:DMat => copyTo(a) - case a:GIMat => copyTo(a) - } - } - - override def copy() = { - val out = GDMat.newOrCheckGDMat(dims, null, GUID, "GDMat.copy".##) - copyTo(out) - } - - override def cumsumByKey(dkeys:DMat, omat:Mat):GDMat = { - val keys = GDMat(dkeys); - if (nrows != keys.nrows || ncols != keys.ncols) - throw new RuntimeException("cumsumKey dimensions mismatch"); - val out = GDMat.newOrCheckGDMat(nrows, ncols, omat, GUID, keys.GUID, "cumsumKey".##); - Mat.nflops += 2L*length; - if (nrows == 1 || ncols == 1) { - CUMATD.cumsumByKeyDD(pdata, keys.pdata, out.pdata, llength); - } else { - throw new RuntimeException("cumsumByKey only implemented for GDMat vectors"); - } - out - } - - override def cumsumByKey(keys:DMat):GDMat = cumsumByKey(keys, null); - - override def cummaxByKey(dkeys:DMat, omat:Mat):GDMat = { - val keys = GDMat(dkeys); - if (nrows != keys.nrows || ncols != keys.ncols) - throw new RuntimeException("cummaxKey dimensions mismatch"); - val out = GDMat.newOrCheckGDMat(nrows, ncols, omat, GUID, keys.GUID, "cummaxKey".##); - Mat.nflops += 2L*length; - if (nrows == 1 || ncols == 1) { - CUMATD.cummaxByKeyDD(pdata, keys.pdata, out.pdata, llength); - } else { - throw new RuntimeException("cummaxByKey only implemented for GDMat vectors"); - } - out - } - - override def cummaxByKey(keys:DMat):GDMat = cummaxByKey(keys, null); - - override def cumminByKey(dkeys:DMat, omat:Mat):GDMat = { - val keys = GDMat(dkeys); - if (nrows != keys.nrows || ncols != keys.ncols) - throw new RuntimeException("cumminKey dimensions mismatch"); - val out = GDMat.newOrCheckGDMat(nrows, ncols, omat, GUID, keys.GUID, "cumminKey".##); - Mat.nflops += 2L*length; - if (nrows == 1 || ncols == 1) { - CUMATD.cumminByKeyDD(pdata, keys.pdata, out.pdata, llength); - } else { - throw new RuntimeException("cumminByKey only implemented for GDMat vectors"); - } - out - } - - override def cumminByKey(keys:DMat):GDMat = cumminByKey(keys, null); - - override def _reverse(omat:Mat):GDMat = { - val out = GDMat.newOrCheckGDMat(nrows, ncols, omat, GUID, "reverse".##); - CUMATD.reverse(pdata, out.pdata, llength); - out - } - - override def reverse:GDMat = _reverse(null); - - override def reverse(omat:Mat):GDMat = _reverse(omat); - - override def recycle(nr:Int, nc:Int, nnz:Int):GDMat = { - if (nrows == nr && nc == ncols) { - this - } else if (realsize >= nr*nc) { - new GDMat(nr, nc, pdata, realsize) - } else { - GDMat(nr, nc) - } - } - - override def free() = { - if (pdata == null) throw new RuntimeException("Attempt to free an alread free'd GDMat") - cudaFree(pdata) - pdata = null; - this - } - - override def finalize = { -// if (pdata != null) free - } - - override def getdiag():GDMat = { - if (nrows != ncols) throw new RuntimeException("getdiag requires a square matrix, but dims= %d %d" format (nrows, ncols)) - val out = GDMat.newOrCheckGDMat(nrows, 1, null, GUID, "getdiag".##) - cudaMemcpy2D(out.pdata, Sizeof.DOUBLE, pdata, 1L*(nrows+1)*Sizeof.DOUBLE, Sizeof.DOUBLE, nrows, cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - val err = cudaGetLastError() - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in getdiag " + cudaGetErrorString(err)) - } - out - } - - - override def mkdiag():GDMat = { - if (math.min(nrows, ncols) != 1) throw new RuntimeException("mkdiag requires a vector argument, but dims= %d %d" format (nrows, ncols)) - val size = math.max(nrows, ncols) - val out = GDMat.newOrCheckGDMat(size, size, null, GUID, "mkdiag".##) - out.clear - var err = cudaMemcpy2D(out.pdata, 1L*(nrows+1)*Sizeof.DOUBLE, pdata, Sizeof.DOUBLE, Sizeof.DOUBLE, nrows, cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - if (err == 0) err = cudaGetLastError() - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in mkdiag " + cudaGetErrorString(err)) - } - out - } - - // Reduce using a set of 0-based indices - def reduce(inds:Array[Int], fctn:(GDMat,Int)=>GDMat, opname:String):GDMat = { - val alldims = MatFunctions.izeros(_dims.length,1); - val xinds = new IMat(inds.length, 1, inds); - val xdims = new IMat(_dims.length, 1, _dims); - alldims(xinds) = 1; - if (alldims.data.reduce(_+_) != inds.length) { - throw new RuntimeException(opname+ " indices arent a legal subset of dims"); - } - val restinds = MatFunctions.find(alldims == 0); - if (restinds(0) == 0) { - val tmp = transpose((restinds on xinds).data); - val tmpF = new GDMat(xdims(restinds).data.reduce(_*_), xdims(xinds).data.reduce(_*_), tmp.pdata, length); - tmpF.setGUID(ND.hash3(ND.hashInts(inds), GUID, ("reduce"+opname).##)); - val tmpSum:GDMat = fctn(tmpF, 2); - val pdims = xdims(restinds) on MatFunctions.iones(inds.length,1); - val out1 = new GDMat(pdims.data, tmpSum.pdata, tmpSum.length); - out1.setGUID(ND.hash3(ND.hashInts(inds), GUID, ("reduce2"+opname).##)); - out1.transpose(MatFunctions.invperm(restinds on xinds).data) - } else { - val tmp = transpose((xinds on restinds).data); - val tmpF = new GDMat(xdims(xinds).data.reduce(_*_), xdims(restinds).data.reduce(_*_), tmp.pdata, length); - tmpF.setGUID(ND.hash3(ND.hashInts(inds), GUID, ("reduce"+opname).##)); - val reduced:GDMat = fctn(tmpF,1); - val newdims = MatFunctions.iones(inds.length,1) on xdims(restinds); - val out1 = new GDMat(newdims.data, reduced.pdata, reduced.length); - out1.transpose(MatFunctions.invperm(xinds on restinds).data); - } - } - - /* - * Basic compute routines on pairs of GDMats - */ - override def unary_-() = gOp(GDMat(-1.0), null, op_mul) - def * (a : GDMat) = GMult(a, null) - def * (a : GSDMat) = GSMult(a, null) - def *^ (a : GDMat) = GMultT(a, null) - def *^ (a : GSDMat) = GSMultT(a, null) - def xT (a : GDMat) = GMultT(a, null) - def xT (a : GSDMat) = GSMultT(a, null) - def ^* (a : GDMat) = GTMult(a, null) - def *+^ (a : GDMat) = GMST(a, null) - def Tx (a : GDMat) = GTMult(a, null) - def + (a : GDMat) = gOp(a, null, op_add) - def - (a : GDMat) = gOp(a, null, op_sub) - def *@ (a : GDMat) = gOp(a, null, op_mul) - def ∘ (a : GDMat) = gOp(a, null, op_mul) - def / (a : GDMat) = gOp(a, null, op_div) - def ^ (a : GDMat) = gOp(a, null, op_pow) - def ∙ (a : GDMat) = dot(a) - def ∙→ (a : GDMat) = dotr(a) - - def > (b : GDMat) = gOp(b, null, op_gt) - def < (b : GDMat) = gOp(b, null, op_lt) - def == (b : GDMat) = gOp(b, null, op_eq) - def === (b : GDMat) = gOp(b, null, op_eq) - def >= (b : GDMat) = gOp(b, null, op_ge) - def <= (b : GDMat) = gOp(b, null, op_le) - def != (b : GDMat) = gOp(b, null, op_ne) - - def max (b : GDMat) = gOp(b, null, op_max) - def min (b : GDMat) = gOp(b, null, op_min) - - override def sum(ind:Int):GDMat = reduceOp(null, ind, 0f, op_add); - override def prod(ind:Int):GDMat = reduceOp(null, ind, 1f, op_mul); - override def maxi(ind:Int):GDMat = reduceOp(null, ind, Float.MinValue, op_max); - override def mini(ind:Int):GDMat = reduceOp(null, ind, Float.MaxValue, op_min); - override def mean(ind:Int):GDMat = SciFunctions._mean(this, ind).asInstanceOf[GDMat]; - override def variance(ind:Int):GDMat = SciFunctions._variance(this, ind).asInstanceOf[GDMat]; - - override def sum(inds:Array[Int]):DMat = reduce(inds, (a:GDMat, dir:Int) => GDFunctions.sum(a,dir,null), "sum"); - override def prod(inds:Array[Int]):DMat = reduce(inds, (a:GDMat, dir:Int) => GDFunctions.prod(a,dir,null), "prod"); - override def mean(inds:Array[Int]):DMat = reduce(inds, (a:GDMat, dir:Int) => SciFunctions.mean(a,dir), "mean") - override def variance(inds:Array[Int]):DMat = reduce(inds, (a:GDMat, dir:Int) => SciFunctions.variance(a,dir), "variance") - override def maxi(inds:Array[Int]):DMat = reduce(inds, (a:GDMat, dir:Int) => GDFunctions.maxi(a,dir,null), "maxi") - override def mini(inds:Array[Int]):DMat = reduce(inds, (a:GDMat, dir:Int) => GDFunctions.mini(a,dir,null), "mini") - override def amax(inds:Array[Int]):DMat = reduce(inds, (a:GDMat, dir:Int) => GDFunctions.maxi(a,dir,null), "amax") - override def amin(inds:Array[Int]):DMat = reduce(inds, (a:GDMat, dir:Int) => GDFunctions.mini(a,dir,null), "amin") - - override def sum(inds:IMat):DMat = reduce(inds.data, (a:GDMat, dir:Int) => GDFunctions.sum(a,dir,null), "sum"); - override def prod(inds:IMat):DMat = reduce(inds.data, (a:GDMat, dir:Int) => GDFunctions.prod(a,dir,null), "prod"); - override def maxi(inds:IMat):DMat = reduce(inds.data, (a:GDMat, dir:Int) => GDFunctions.maxi(a,dir,null), "maxi") - override def mini(inds:IMat):DMat = reduce(inds.data, (a:GDMat, dir:Int) => GDFunctions.mini(a,dir,null), "mini") - override def amax(inds:IMat):DMat = reduce(inds.data, (a:GDMat, dir:Int) => GDFunctions.maxi(a,dir,null), "amax") - override def amin(inds:IMat):DMat = reduce(inds.data, (a:GDMat, dir:Int) => GDFunctions.mini(a,dir,null), "amin") - - override def mean(inds:IMat):DMat = {val m = this.sum(inds); - m ~ m *@ (1.0/SciFunctions.prod(this.dims(inds)).v); - m} - override def variance(inds:IMat):DMat = {val m = this.sum(inds); - val n = SciFunctions.prod(this.dims(inds)).v; - m ~ m *@ (1.0/n) - val a = this - m; - a ~ a *@ a - val v = a.sum(inds); - v ~ v *@ (1.0/n); - v} - - override def * (a : DMat) = GMult(GDMat(a), null) - override def * (a : SDMat) = GSMult(GSDMat(a), null) - override def *^ (a : DMat) = GMultT(GDMat(a), null) - override def *^ (a : SDMat) = GSMultT(GSDMat(a), null) - override def xT (a : DMat) = GMultT(GDMat(a), null) - override def xT (a : SDMat) = GSMultT(GSDMat(a), null) - override def ^* (a : DMat) = GTMult(GDMat(a), null) - def *+^ (a : DMat) = GMST(GDMat(a), null) - override def Tx (a : DMat) = GTMult(GDMat(a), null) - override def kron(a: DMat) = kron(GDMat(a), null) - override def ⊗ (a : DMat) = kron(GDMat(a), null) - override def + (a : DMat) = gOp(GDMat(a), null, op_add) - override def - (a : DMat) = gOp(GDMat(a), null, op_sub) - override def *@ (a : DMat) = gOp(GDMat(a), null, op_mul) - override def ∘ (a : DMat) = gOp(GDMat(a), null, op_mul) - override def / (a : DMat) = gOp(GDMat(a), null, op_div) - override def ^ (a : DMat) = gOp(GDMat(a), null, op_pow) - override def ∙ (a : DMat) = dot(a) - override def ∙→ (a : DMat) = dotr(a) - - override def > (a : DMat) = gOp(GDMat(a), null, op_gt) - override def < (a : DMat) = gOp(GDMat(a), null, op_lt) - override def == (a : DMat) = gOp(GDMat(a), null, op_eq) - override def === (a : DMat) = gOp(GDMat(a), null, op_eq) - override def >= (a : DMat) = gOp(GDMat(a), null, op_ge) - override def <= (a : DMat) = gOp(GDMat(a), null, op_le) - override def != (a : DMat) = gOp(GDMat(a), null, op_ne) - - override def max (a : DMat) = gOp(GDMat(a), null, op_max) - override def min (a : DMat) = gOp(GDMat(a), null, op_min) - - - override def + (a : Float) = gOp(GDMat(a), null, op_add) - override def - (a : Float) = gOp(GDMat(a), null, op_sub) - override def *@ (a : Float) = gOp(GDMat(a), null, op_mul) - override def * (a : Float) = gOp(GDMat(a), null, op_mul) - override def ∘ (a : Float) = gOp(GDMat(a), null, op_mul) - override def / (a : Float) = gOp(GDMat(a), null, op_div) - override def ^ (a : Float) = gOp(GDMat(a), null, op_pow) - - override def < (b : Float) = gOp(GDMat(b), null, op_lt) - override def > (b : Float) = gOp(GDMat(b), null, op_gt) - override def <= (b : Float) = gOp(GDMat(b), null, op_le) - override def >= (b : Float) = gOp(GDMat(b), null, op_ge) - override def == (b : Float) = gOp(GDMat(b), null, op_eq) - override def != (b : Float) = gOp(GDMat(b), null, op_ne) - - override def max (b : Float) = gOp(GDMat(b), null, op_max) - override def min (b : Float) = gOp(GDMat(b), null, op_min) - - override def + (a : Double) = gOp(GDMat(a), null, op_add) - override def - (a : Double) = gOp(GDMat(a), null, op_sub) - override def *@ (a : Double) = gOp(GDMat(a), null, op_mul) - override def * (a : Double) = gOp(GDMat(a), null, op_mul) - override def ∘ (a : Double) = gOp(GDMat(a), null, op_mul) - override def / (a : Double) = gOp(GDMat(a), null, op_div) - override def ^ (a : Double) = gOp(GDMat(a), null, op_pow) - - override def < (b : Double) = gOp(GDMat(b), null, op_lt) - override def > (b : Double) = gOp(GDMat(b), null, op_gt) - override def <= (b : Double) = gOp(GDMat(b), null, op_le) - override def >= (b : Double) = gOp(GDMat(b), null, op_ge) - override def == (b : Double) = gOp(GDMat(b), null, op_eq) - override def != (b : Double) = gOp(GDMat(b), null, op_ne) - - override def max (b : Double) = gOp(GDMat(b), null, op_max) - override def min (b : Double) = gOp(GDMat(b), null, op_min) - - - override def + (a : Int) = gOp(GDMat(a.toDouble), null, op_add) - override def - (a : Int) = gOp(GDMat(a.toDouble), null, op_sub) - override def *@ (a : Int) = gOp(GDMat(a.toDouble), null, op_mul) - override def * (a : Int) = gOp(GDMat(a.toDouble), null, op_mul) - override def ∘ (a : Int) = gOp(GDMat(a.toDouble), null, op_mul) - override def / (a : Int) = gOp(GDMat(a.toDouble), null, op_div) - override def ^ (a : Int) = gOp(GDMat(a.toDouble), null, op_pow) - - override def < (b : Int) = gOp(GDMat(b), null, op_lt) - override def > (b : Int) = gOp(GDMat(b), null, op_gt) - override def <= (b : Int) = gOp(GDMat(b), null, op_le) - override def >= (b : Int) = gOp(GDMat(b), null, op_ge) - override def == (b : Int) = gOp(GDMat(b), null, op_eq) - override def != (b : Int) = gOp(GDMat(b), null, op_ne) - - override def max (b : Int) = gOp(GDMat(b), null, op_max) - override def min (b : Int) = gOp(GDMat(b), null, op_min) - - - - def on(a : GDMat) = vertcat(a, null) - def \ (a : GDMat) = horzcat(a, null) - - /* - * Specialize to IMats to help the type system. - */ - override def * (b : IMat) = Mop_Times.op(this, b, null) - override def *^ (b : IMat) = Mop_TimesT.op(this, b, null) - override def xT (b : IMat) = Mop_TimesT.op(this, b, null) - override def Tx (b : IMat) = Mop_TTimes.op(this, b, null) - override def ^* (b : IMat) = Mop_TTimes.op(this, b, null) - override def + (b : IMat) = Mop_Plus.op(this, b, null) - override def - (b : IMat) = Mop_Minus.op(this, b, null) - override def *@ (b : IMat) = Mop_ETimes.op(this, b, null) - override def ∘ (b : IMat) = Mop_ETimes.op(this, b, null) - override def /< (b : IMat) = Mop_Div.op(this, b, null) - override def \\ (b : IMat) = Mop_RSolve.op(this, b, null) - override def ◁ (b : IMat) = Mop_Div.op(this, b, null) - override def ▷ (b : IMat) = Mop_RSolve.op(this, b, null) - override def / (b : IMat) = Mop_EDiv.op(this, b, null) - override def ^ (b : IMat) = Mop_Pow.op(this, b, null) - override def ∙ (b : IMat) = Mop_Dot.op(this, b, null) - override def ∙→ (b : IMat) = Mop_Dotr.op(this, b, null) - override def dot (b : IMat) = Mop_Dot.op(this, b, null) - override def dotr(b : IMat) = Mop_Dotr.op(this, b, null) - override def \ (b : IMat) = Mop_HCat.op(this, b, null) - override def on (b : IMat) = Mop_VCat.op(this, b, null) - - override def > (b : IMat) = Mop_GT.op(this, b, null) - override def < (b : IMat) = Mop_LT.op(this, b, null) - override def == (b : IMat) = Mop_EQ.op(this, b, null) - override def === (b : IMat) = Mop_EQ.op(this, b, null) - override def >= (b : IMat) = Mop_GE.op(this, b, null) - override def <= (b : IMat) = Mop_LE.op(this, b, null) - override def != (b : IMat) = Mop_NE.op(this, b, null) - - /* - * Specialize to DMats to help the type system. - */ - /* - override def * (b : DMat) = Mop_Times.op(this, b, null) - override def *^ (b : DMat) = Mop_TimesT.op(this, b, null) - override def xT (b : DMat) = Mop_TimesT.op(this, b, null) - override def Tx (b : DMat) = Mop_TTimes.op(this, b, null) - override def ^* (b : DMat) = Mop_TTimes.op(this, b, null) - override def + (b : DMat) = Mop_Plus.op(this, b, null) - override def - (b : DMat) = Mop_Minus.op(this, b, null) - override def *@ (b : DMat) = Mop_ETimes.op(this, b, null) - override def ∘ (b : DMat) = Mop_ETimes.op(this, b, null) - override def /< (b : DMat) = Mop_Div.op(this, b, null) - override def \\ (b : DMat) = Mop_RSolve.op(this, b, null) - override def ◁ (b : DMat) = Mop_Div.op(this, b, null) - override def ▷ (b : DMat) = Mop_RSolve.op(this, b, null) - override def / (b : DMat) = Mop_EDiv.op(this, b, null) - override def ^ (b : DMat) = Mop_Pow.op(this, b, null) - override def ∙ (b : DMat) = Mop_Dot.op(this, b, null) - override def ∙→ (b : DMat) = Mop_Dotr.op(this, b, null) - override def dot (b : DMat) = Mop_Dot.op(this, b, null) - override def dotr(b : DMat) = Mop_Dotr.op(this, b, null) - override def \ (b : DMat) = Mop_HCat.op(this, b, null) - override def on (b : DMat) = Mop_VCat.op(this, b, null) - - override def > (b : DMat) = Mop_GT.op(this, b, null) - override def < (b : DMat) = Mop_LT.op(this, b, null) - override def == (b : DMat) = Mop_EQ.op(this, b, null) - override def === (b : DMat) = Mop_EQ.op(this, b, null) - override def >= (b : DMat) = Mop_GE.op(this, b, null) - override def <= (b : DMat) = Mop_LE.op(this, b, null) - override def != (b : DMat) = Mop_NE.op(this, b, null) - - */ - - /* - * Specialize to FMats to help the type system. - */ - override def * (b : FMat) = Mop_Times.op(this, b, null) - override def *^ (b : FMat) = Mop_TimesT.op(this, b, null) - override def xT (b : FMat) = Mop_TimesT.op(this, b, null) - override def Tx (b : FMat) = Mop_TTimes.op(this, b, null) - override def ^* (b : FMat) = Mop_TTimes.op(this, b, null) - override def + (b : FMat) = Mop_Plus.op(this, b, null) - override def - (b : FMat) = Mop_Minus.op(this, b, null) - override def *@ (b : FMat) = Mop_ETimes.op(this, b, null) - override def ∘ (b : FMat) = Mop_ETimes.op(this, b, null) - override def /< (b : FMat) = Mop_Div.op(this, b, null) - override def \\ (b : FMat) = Mop_RSolve.op(this, b, null) - override def ◁ (b : FMat) = Mop_Div.op(this, b, null) - override def ▷ (b : FMat) = Mop_RSolve.op(this, b, null) - override def / (b : FMat) = Mop_EDiv.op(this, b, null) - override def ^ (b : FMat) = Mop_Pow.op(this, b, null) - override def ∙ (b : FMat) = Mop_Dot.op(this, b, null) - override def ∙→ (b : FMat) = Mop_Dotr.op(this, b, null) - override def dot (b : FMat) = Mop_Dot.op(this, b, null) - override def dotr(b : FMat) = Mop_Dotr.op(this, b, null) - override def \ (b : FMat) = Mop_HCat.op(this, b, null) - override def on (b : FMat) = Mop_VCat.op(this, b, null) - - override def > (b : FMat) = Mop_GT.op(this, b, null) - override def < (b : FMat) = Mop_LT.op(this, b, null) - override def == (b : FMat) = Mop_EQ.op(this, b, null) - override def === (b : FMat) = Mop_EQ.op(this, b, null) - override def >= (b : FMat) = Mop_GE.op(this, b, null) - override def <= (b : FMat) = Mop_LE.op(this, b, null) - override def != (b : FMat) = Mop_NE.op(this, b, null) - - /* - * Operators whose second arg is generic. - */ - override def * (b : Mat) = Mop_Times.op(this, b, null) - override def *^ (b : Mat) = Mop_TimesT.op(this, b, null) - override def xT (b : Mat) = Mop_TimesT.op(this, b, null) - override def Tx (b : Mat) = Mop_TTimes.op(this, b, null) - override def ^* (b : Mat) = Mop_TTimes.op(this, b, null) - override def + (b : Mat) = Mop_Plus.op(this, b, null) - override def - (b : Mat) = Mop_Minus.op(this, b, null) - override def *@ (b : Mat) = Mop_ETimes.op(this, b, null) - override def ∘ (b : Mat) = Mop_ETimes.op(this, b, null) - override def / (b : Mat) = Mop_EDiv.op(this, b, null) - override def /< (b : Mat) = Mop_Div.op(this, b, null) - override def \\ (b : Mat) = Mop_RSolve.op(this, b, null) - override def ◁ (b : Mat) = Mop_Div.op(this, b, null) - override def ▷ (b : Mat) = Mop_RSolve.op(this, b, null) - override def ^ (b : Mat) = Mop_Pow.op(this, b, null) - override def ∙ (b : Mat) = Mop_Dot.op(this, b, null) - override def ∙→ (b : Mat) = Mop_Dotr.op(this, b, null) - override def dot (b : Mat) = Mop_Dot.op(this, b, null) - override def dotr (b : Mat) = Mop_Dotr.op(this, b, null) - override def \ (b : Mat) = Mop_HCat.op(this, b, null) - override def on (b : Mat) = Mop_VCat.op(this, b, null) - - override def > (b : Mat) = Mop_GT.op(this, b, null) - override def < (b : Mat) = Mop_LT.op(this, b, null) - override def >= (b : Mat) = Mop_GE.op(this, b, null) - override def <= (b : Mat) = Mop_LE.op(this, b, null) - override def == (b : Mat) = Mop_EQ.op(this, b, null) - override def === (b : Mat) = Mop_EQ.op(this, b, null) - override def != (b : Mat) = Mop_NE.op(this, b, null) - - /* - * Tilde operator - */ - def ~ (b: GDMat) = new GDPair(this, b) - def ~ (b: GSDMat) = new GSDPair(this, b) - override def ~ (b: DMat):DPair = new GDPair(this, GDMat(b)) -// override def ~ (b: SDMat):DSPair = new GSDPair(this, GSDMat(b)); - override def ~ (b: Mat):Pair = b match { - case bb:GDMat => new GDPair(this, bb) - case bb:GSDMat => new GSDPair(this, bb) - } - - /* - * @@ operator for DDS - */ - def @@ (b : GSDMat) = new GDDSPair(this, b) - def ^* (b : GDSPair) = MatFunctions.DDS(this, b.left, b.right, null) - def Tx (b : GDSPair) = MatFunctions.DDS(this, b.left, b.right, null) - override def ^* (b0 : DSPair) = {val b = b0.asInstanceOf[GDSPair]; MatFunctions.DDS(this, b.left, b.right, null)} - override def Tx (b0 : DSPair) = {val b = b0.asInstanceOf[GDSPair]; MatFunctions.DDS(this, b.left, b.right, null)} - -} - -/* - * Result of a@@b for DDS - */ -class GDDSPair(val left:GDMat, val right:GSDMat) extends DSPair {} - -/* - * GPair is the result of a~b - */ -class GDPair(omat:Mat, override val mat:GDMat) extends DPair(omat, mat) { - import GMat.BinOp._ - - override def t = { - val out = GDMat.newOrCheckGDMat(mat.ncols, mat.nrows, omat, mat.GUID, "pt".##) - CUMATD.transpose(mat.pdata, mat.nrows, out.pdata, mat.ncols, mat.nrows, mat.ncols) - out - } - def * (a : GDMat) = mat.GMult(a, omat) - def * (a : GSDMat) = mat.GSMult(a, omat) - def *^ (a : GDMat) = mat.GMultT(a, omat) - def *^ (a : GSDMat) = mat.GSMultT(a, omat) - def xT (a : GDMat) = mat.GMultT(a, omat) - def xT (a : GSDMat) = mat.GSMultT(a, omat) - def ^* (a : GDMat) = mat.GTMult(a, omat) - def *+^ (a : GDMat) = mat.GMST(a, omat) - def Tx (a : GDMat) = mat.GTMult(a, omat) - def + (a : GDMat) = mat.gOp(a, omat, op_add) - def - (a : GDMat) = mat.gOp(a, omat, op_sub) - def *@ (a : GDMat) = mat.gOp(a, omat, op_mul) - def ∘ (a : GDMat) = mat.gOp(a, omat, op_mul) - def / (a : GDMat) = mat.gOp(a, omat, op_div) - def ^ (a : GDMat) = mat.gOp(a, omat, op_pow) - def > (b : GDMat) = mat.gOp(b, omat, op_gt) - def < (b : GDMat) = mat.gOp(b, omat, op_lt) - def == (b : GDMat) = mat.gOp(b, omat, op_eq) - def === (b : GDMat) = mat.gOp(b, omat, op_eq) - def >= (b : GDMat) = mat.gOp(b, omat, op_ge) - def <= (b : GDMat) = mat.gOp(b, omat, op_le) - def != (b : GDMat) = mat.gOp(b, omat, op_ne) - def max (b : GDMat) = mat.gOp(b, omat, op_max) - def min (b : GDMat) = mat.gOp(b, omat, op_min) - - def dot (b :GDMat) = mat.dot(b, omat) - def dotr (b :GDMat) = mat.dotr(b, omat) - def ∙ (b :GDMat) = mat.dot(b, omat) - def ∙→ (b :GDMat) = mat.dotr(b, omat) - def on(a : GDMat) = mat.vertcat(a, omat) - def \ (a : GDMat) = mat.horzcat(a, omat) - - override def * (a : DMat) = mat.GMult(GDMat(a), omat); - override def * (a : SDMat) = mat.GSMult(GSDMat(a), omat); - override def *^ (a : DMat) = mat.GMultT(GDMat(a), omat) - override def *^ (a : SDMat) = mat.GSMultT(GSDMat(a), omat) - override def xT (a : DMat) = mat.GMultT(GDMat(a), omat) - override def xT (a : SDMat) = mat.GSMultT(GSDMat(a), omat) - override def ^* (a : DMat) = mat.GTMult(GDMat(a), omat) - def *+^ (a : DMat) = mat.GMST(GDMat(a), omat) - override def Tx (a : DMat) = mat.GTMult(GDMat(a), omat) - def kron(a: DMat):DMat = mat.kron(GDMat(a), omat) - override def ⊗ (b : DMat) = mat.kron(b, omat) - override def + (a : DMat) = mat.gOp(GDMat(a), omat, op_add) - override def - (a : DMat) = mat.gOp(GDMat(a), omat, op_sub) - override def *@ (a : DMat) = mat.gOp(GDMat(a), omat, op_mul) - override def ∘ (a : DMat) = mat.gOp(GDMat(a), omat, op_mul) - override def / (a : DMat) = mat.gOp(GDMat(a), omat, op_div) - override def ^ (a : DMat) = mat.gOp(GDMat(a), omat, op_pow) - override def > (a : DMat) = mat.gOp(GDMat(a), omat, op_gt) - override def < (a : DMat) = mat.gOp(GDMat(a), omat, op_lt) - override def == (a : DMat) = mat.gOp(GDMat(a), omat, op_eq) - override def === (a : DMat) = mat.gOp(GDMat(a), omat, op_eq) - override def >= (a : DMat) = mat.gOp(GDMat(a), omat, op_ge) - override def <= (a : DMat) = mat.gOp(GDMat(a), omat, op_le) - override def != (a : DMat) = mat.gOp(GDMat(a), omat, op_ne) - - override def max (a : DMat) = mat.gOp(GDMat(a), omat, op_max) - override def min (a : DMat) = mat.gOp(GDMat(a), omat, op_min) - - override def dot (a :DMat) = mat.dot(GDMat(a), omat) - override def dotr (a :DMat) = mat.dotr(GDMat(a), omat) - override def ∙ (a :DMat) = mat.dot(GDMat(a), omat) - override def ∙→ (a :DMat) = mat.dotr(GDMat(a), omat) - def on(a : DMat) = mat.vertcat(GDMat(a), omat) - def \ (a : DMat) = mat.horzcat(GDMat(a), omat) - - - override def * (b : Float) = mat.gOp(GDMat(b), omat, op_mul) - override def ∘ (b : Float) = mat.gOp(GDMat(b), omat, op_mul) - override def + (b : Float) = mat.gOp(GDMat(b), omat, op_add) - override def - (b : Float) = mat.gOp(GDMat(b), omat, op_sub) - override def / (b : Float) = mat.gOp(GDMat(b), omat, op_div) - override def ^ (b : Float) = mat.gOp(GDMat(b), omat, op_pow) - override def > (b : Float) = mat.gOp(GDMat(b), omat, op_gt) - override def < (b : Float) = mat.gOp(GDMat(b), omat, op_lt) - override def == (b : Float) = mat.gOp(GDMat(b), omat, op_eq) - override def != (b : Float) = mat.gOp(GDMat(b), omat, op_ne) - override def >= (b : Float) = mat.gOp(GDMat(b), omat, op_ge) - override def <= (b : Float) = mat.gOp(GDMat(b), omat, op_le) - override def max (b : Float) = mat.gOp(GDMat(b), omat, op_max) - override def min (b : Float) = mat.gOp(GDMat(b), omat, op_min) - - override def * (b : Double) = mat.gOp(GDMat(b), omat, op_mul) - override def ∘ (b : Double) = mat.gOp(GDMat(b), omat, op_mul) - override def + (b : Double) = mat.gOp(GDMat(b), omat, op_add) - override def - (b : Double) = mat.gOp(GDMat(b), omat, op_sub) - override def / (b : Double) = mat.gOp(GDMat(b), omat, op_div) - override def ^ (b : Double) = mat.gOp(GDMat(b), omat, op_pow) - override def > (b : Double) = mat.gOp(GDMat(b), omat, op_gt) - override def < (b : Double) = mat.gOp(GDMat(b), omat, op_lt) - override def == (b : Double) = mat.gOp(GDMat(b), omat, op_eq) - override def != (b : Double) = mat.gOp(GDMat(b), omat, op_ne) - override def >= (b : Double) = mat.gOp(GDMat(b), omat, op_ge) - override def <= (b : Double) = mat.gOp(GDMat(b), omat, op_le) - override def max (b : Double) = mat.gOp(GDMat(b), omat, op_max) - override def min (b : Double) = mat.gOp(GDMat(b), omat, op_min) - - override def * (b : Int) = mat.gOp(GDMat(b), omat, op_mul) - override def ∘ (b : Int) = mat.gOp(GDMat(b), omat, op_mul) - override def + (b : Int) = mat.gOp(GDMat(b), omat, op_add) - override def - (b : Int) = mat.gOp(GDMat(b), omat, op_sub) - override def / (b : Int) = mat.gOp(GDMat(b), omat, op_div) - override def ^ (b : Int) = mat.gOp(GDMat(b), omat, op_pow) - override def > (b : Int) = mat.gOp(GDMat(b), omat, op_gt) - override def < (b : Int) = mat.gOp(GDMat(b), omat, op_lt) - override def == (b : Int) = mat.gOp(GDMat(b), omat, op_eq) - override def != (b : Int) = mat.gOp(GDMat(b), omat, op_ne) - override def >= (b : Int) = mat.gOp(GDMat(b), omat, op_ge) - override def <= (b : Int) = mat.gOp(GDMat(b), omat, op_le) - override def max (b : Int) = mat.gOp(GDMat(b), omat, op_max) - override def min (b : Int) = mat.gOp(GDMat(b), omat, op_min) - - def ^* (b : GDSPair) = MatFunctions.DDS(mat, b.left, b.right, omat) - def Tx (b : GDSPair) = MatFunctions.DDS(mat, b.left, b.right, omat) - /* - * Specialize to IMat - */ - override def * (b : IMat) = Mop_Times.op(mat, b, omat) - override def *^ (b : IMat) = Mop_TimesT.op(mat, b, omat) - override def xT (b : IMat) = Mop_TimesT.op(mat, b, omat) - override def Tx (b : IMat) = Mop_TTimes.op(mat, b, omat) - override def ^* (b : IMat) = Mop_TTimes.op(mat, b, omat) - override def + (b : IMat) = Mop_Plus.op(mat, b, omat) - override def - (b : IMat) = Mop_Minus.op(mat, b, omat) - override def *@ (b : IMat) = Mop_ETimes.op(mat, b, omat) - override def ∘ (b : IMat) = Mop_ETimes.op(mat, b, omat) - override def / (b : IMat) = Mop_EDiv.op(mat, b, omat) - override def ^ (b : IMat) = Mop_Pow.op(mat, b, omat) - override def ∙ (b : IMat) = Mop_Dot.op(mat, b, omat) - override def ∙→ (b : IMat) = Mop_Dotr.op(mat, b, omat) - override def dot (b : IMat) = Mop_Dot.op(mat, b, omat) - override def dotr(b : IMat) = Mop_Dotr.op(mat, b, omat) - override def \ (b : IMat) = Mop_HCat.op(mat, b, omat) - override def on (b : IMat) = Mop_VCat.op(mat, b, omat) - - override def > (b : IMat) = Mop_GT.op(mat, b, omat) - override def < (b : IMat) = Mop_LT.op(mat, b, omat) - override def == (b : IMat) = Mop_EQ.op(mat, b, omat) - override def === (b : IMat) = Mop_EQ.op(mat, b, omat) - override def >= (b : IMat) = Mop_GE.op(mat, b, omat) - override def <= (b : IMat) = Mop_LE.op(mat, b, omat) - override def != (b : IMat) = Mop_NE.op(mat, b, omat) - - - /* - * Specialize to FMat - */ - override def * (b : FMat) = Mop_Times.op(mat, b, omat) - override def *^ (b : FMat) = Mop_TimesT.op(mat, b, omat) - override def xT (b : FMat) = Mop_TimesT.op(mat, b, omat) - override def Tx (b : FMat) = Mop_TTimes.op(mat, b, omat) - override def ^* (b : FMat) = Mop_TTimes.op(mat, b, omat) - override def + (b : FMat) = Mop_Plus.op(mat, b, omat) - override def - (b : FMat) = Mop_Minus.op(mat, b, omat) - override def *@ (b : FMat) = Mop_ETimes.op(mat, b, omat) - override def ∘ (b : FMat) = Mop_ETimes.op(mat, b, omat) - override def / (b : FMat) = Mop_EDiv.op(mat, b, omat) - override def ^ (b : FMat) = Mop_Pow.op(mat, b, omat) - override def ∙ (b : FMat) = Mop_Dot.op(mat, b, omat) - override def ∙→ (b : FMat) = Mop_Dotr.op(mat, b, omat) - override def dot (b : FMat) = Mop_Dot.op(mat, b, omat) - override def dotr(b : FMat) = Mop_Dotr.op(mat, b, omat) - override def \ (b : FMat) = Mop_HCat.op(mat, b, omat) - override def on (b : FMat) = Mop_VCat.op(mat, b, omat) - - override def > (b : FMat) = Mop_GT.op(mat, b, omat) - override def < (b : FMat) = Mop_LT.op(mat, b, omat) - override def == (b : FMat) = Mop_EQ.op(mat, b, omat) - override def === (b : FMat) = Mop_EQ.op(mat, b, omat) - override def >= (b : FMat) = Mop_GE.op(mat, b, omat) - override def <= (b : FMat) = Mop_LE.op(mat, b, omat) - override def != (b : FMat) = Mop_NE.op(mat, b, omat) - - /* - * Generics - */ - override def * (b : Mat):Mat = Mop_Times.op(mat, b, omat) - override def xT (b : Mat):Mat = Mop_TimesT.op(mat, b, omat) - override def *^ (b : Mat):Mat = Mop_TimesT.op(mat, b, omat) - override def Tx (b : Mat):Mat = Mop_TTimes.op(mat, b, omat) - override def ^* (b : Mat):Mat = Mop_TTimes.op(mat, b, omat) - override def + (b : Mat):Mat = Mop_Plus.op(mat, b, omat) - override def - (b : Mat):Mat = Mop_Minus.op(mat, b, omat) - override def *@ (b : Mat):Mat = Mop_ETimes.op(mat, b, omat) - override def ∘ (b : Mat):Mat = Mop_ETimes.op(mat, b, omat) - override def / (b : Mat):Mat = Mop_EDiv.op(mat, b, omat) - override def ^ (b : Mat):Mat = Mop_Pow.op(mat, b, omat) - override def /< (b : Mat):Mat = Mop_Div.op(mat, b, omat) - override def \\ (b : Mat):Mat = Mop_RSolve.op(mat, b, omat) - override def ◁ (b : Mat):Mat = Mop_Div.op(mat, b, omat) - override def ▷ (b : Mat):Mat = Mop_RSolve.op(mat, b, omat) - override def ∙ (b : Mat) = Mop_Dot.op(mat, b, omat) - override def ∙→ (b : Mat) = Mop_Dotr.op(mat, b, omat) - override def dot (b : Mat) = Mop_Dot.op(mat, b, omat) - override def dotr(b : Mat) = Mop_Dotr.op(mat, b, omat) - override def \ (b : Mat):Mat = Mop_HCat.op(mat, b, omat) - override def on (b : Mat):Mat = Mop_VCat.op(mat, b, omat) - - override def > (b : Mat):Mat = Mop_GT.op(mat, b, omat) - override def < (b : Mat):Mat = Mop_LT.op(mat, b, omat) - override def >= (b : Mat):Mat = Mop_GE.op(mat, b, omat) - override def <= (b : Mat):Mat = Mop_LE.op(mat, b, omat) - override def == (b : Mat):Mat = Mop_EQ.op(mat, b, omat) - override def === (b : Mat):Mat = Mop_EQ.op(mat, b, omat) - override def != (b : Mat):Mat = Mop_NE.op(mat, b, omat) -} - - -object GDMat { - - val nullPointer = new Pointer - - def apply(nr:Int, nc:Int):GDMat = { - val retv = new GDMat(nr, nc, new Pointer(), nr*nc) - if (Mat.debugMem) { - println("GDMat %d %d, %d %f" format (nr, nc, SciFunctions.getGPU, SciFunctions.GPUmem._1)) - if (nr*nc > Mat.debugMemThreshold) throw new RuntimeException("GDMat alloc too large"); - } - val len = nr * nc; - var err = if (1L*len*Sizeof.DOUBLE > Mat.hostAllocSize) { - cudaMallocHost(retv.pdata, 1L*len*Sizeof.DOUBLE); - } else { - cudaMalloc(retv.pdata, 1L*len*Sizeof.DOUBLE); - } - cudaStreamSynchronize(Mat.SyncMethod) - if (err == 0) err = cudaGetLastError() - if (err != 0) throw new RuntimeException("CUDA alloc failed " + cudaGetErrorString(err)) - retv - } - - def make(dims:Array[Int]):GDMat = { - val len = dims.reduce(_*_); - val retv = new GDMat(dims, new Pointer, len); - if (Mat.debugMem) { - println("GMat %d, %d %f" format (len, SciFunctions.getGPU, SciFunctions.GPUmem._1)) - if (len > Mat.debugMemThreshold) throw new RuntimeException("GDMat alloc too large"); - } - var err = if (1L*len*Sizeof.DOUBLE > Mat.hostAllocSize) { - cudaMallocHost(retv.pdata, 1L*len*Sizeof.DOUBLE); - } else { - cudaMalloc(retv.pdata, 1L*len*Sizeof.DOUBLE); - } - cudaStreamSynchronize(Mat.SyncMethod); - if (err == 0) err = cudaGetLastError(); - if (err != 0) throw new RuntimeException("CUDA alloc failed " + cudaGetErrorString(err)); - retv - } - - def make(dims:IMat):GDMat = make(dims.data); - - def zeros(nr:Int, nc:Int) = { - val out = GDMat(nr, nc) - cudaMemset(out.pdata, 0, Sizeof.DOUBLE*out.length) - cudaStreamSynchronize(Mat.SyncMethod) - val err = cudaGetLastError() - if (err != 0) { - val gpu = SciFunctions.getGPU - throw new RuntimeException("GPU "+gpu+": Cuda error in gzeros " + cudaGetErrorString(err)) - } - out - } - - def zeros(dims:IMat) = { - val out = GDMat.make(dims) - cudaMemset(out.pdata, 0, Sizeof.DOUBLE*out.length) - cudaStreamSynchronize(Mat.SyncMethod) - val err = cudaGetLastError() - if (err != 0) { - val gpu = SciFunctions.getGPU - throw new RuntimeException("GPU "+gpu+": Cuda error in gzeros " + cudaGetErrorString(err)) - } - out - } - - def ones(nr:Int, nc:Int) = { - val out = GDMat(nr, nc); - CUMATD.setval(out.pdata, 1.0, out.length); - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError(); - if (err != 0) { - println("device is %d" format SciFunctions.getGPU); - throw new RuntimeException("CUDA error in gones " + cudaGetErrorString(err)); - } - out - } - - def ones(dims:IMat) = { - val out = GDMat.make(dims); - CUMATD.setval(out.pdata, 1.0, out.length); - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError(); - if (err != 0) { - println("device is %d" format SciFunctions.getGPU); - throw new RuntimeException("CUDA error in gones " + cudaGetErrorString(err)); - } - out - } - - val pONE = Pointer.to(Array(1.0)); - - val pZERO = Pointer.to(Array(0.0)); - - def apply(a:DMat):GDMat = { - a match { - case g:GDMat => g; - case _ => { - val rsize = a.nrows*a.ncols; - val retv = GDMat.newOrCheckGDMat(a.dims, null, a.GUID, "GDMat_DMat".##); - cudaMemcpy(retv.pdata, Pointer.to(a.data), 1L*rsize*Sizeof.DOUBLE, cudaMemcpyKind.cudaMemcpyHostToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError(); - if (err != 0) { - println("device is %d" format SciFunctions.getGPU); - throw new RuntimeException("CUDA error in GDMat() " + cudaGetErrorString(err)); - } - retv; - } - } - } - - def apply(a:GIMat):GDMat = { - val rsize = a.nrows*a.ncols - val retv = GDMat.newOrCheckGDMat(a.dims, null, a.GUID, "GDMat_GIMat".##) - var err = CUMATD.IntToDouble(a.pdata, retv.pdata, a.length) - cudaStreamSynchronize(Mat.SyncMethod) - if (err == 0) err = cudaGetLastError() - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("GDMat(GIMat) error " + cudaGetErrorString(err)) - } - retv - } - - def apply(a:GMat):GDMat = { - val rsize = a.nrows*a.ncols - val retv = GDMat.newOrCheckGDMat(a.dims, null, a.GUID, "GDMat_GIMat".##) - var err = CUMATD.FloatToDouble(a.pdata, retv.pdata, a.length) - cudaStreamSynchronize(Mat.SyncMethod) - if (err == 0) err = cudaGetLastError() - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("GDMat(GIMat) error " + cudaGetErrorString(err)) - } - retv - } - - def apply(a:Mat):GDMat = a match { - case aa:GDMat => aa - case aa:GMat => GDMat(aa) - case aa:GIMat => GDMat(aa) - case aa:FMat => GDMat(DMat(aa)) - case aa:DMat => GDMat(aa) - case aa:IMat => GDMat(DMat(aa)) - } - - def apply(a:Float):GDMat = { - val out = GDMat.newOrCheckGDMat(1, 1, null, a.##, "GDMat_Float".##) - out.set(a.toDouble) - out - } - - def apply(a:Double):GDMat = { - val out = GDMat.newOrCheckGDMat(1, 1, null, a.##, "GDMat_Double".##) - out.set(a) - out - } - - def elem(a:Double):GDMat = { - val out = GDMat.newOrCheckGDMat(1, 1, null, a.##, "GDelem".##); - out.set(a) - out - } - - def toDMat(a:GDMat):DMat = a.toDMat(null) - - def fromDMat(a:DMat, b:GDMat):GDMat = { - val bb = GDMat.newOrCheckGDMat(a.nrows, a.ncols, b, a.GUID, "GDMat_fromDMat".##) - cudaMemcpy(bb.pdata, Pointer.to(a.data), a.length*1L*Sizeof.DOUBLE, cudaMemcpyKind.cudaMemcpyHostToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - var err = cudaGetLastError() - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("CUDA error in fromDMat " + cudaGetErrorString(err)) - } - bb - } - - def GPUtoGPUarraycopy(a:Pointer, aoffset:Int, b:Pointer, boffset:Int, len:Int, msg:String ) = { - cudaMemcpy(b.withByteOffset(1L*boffset*Sizeof.DOUBLE), a.withByteOffset(1L*aoffset*Sizeof.DOUBLE), 1L*len*Sizeof.DOUBLE, cudaMemcpyKind.cudaMemcpyDeviceToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError; - if (err != 0) throw new RuntimeException(msg +" error in memcpy "+ cudaGetErrorString(err)); - } - - def GPUtoCPUarraycopy(a:Pointer, aoffset:Int, b:Array[Double], boffset:Int, len:Int, msg:String ) = { - cudaMemcpy(Pointer.to(b).withByteOffset(1L*boffset*Sizeof.DOUBLE), a.withByteOffset(1L*aoffset*Sizeof.DOUBLE), 1L*len*Sizeof.DOUBLE, cudaMemcpyKind.cudaMemcpyDeviceToHost); - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError; - if (err != 0) throw new RuntimeException(msg +" error in memcpy "+ cudaGetErrorString(err)); - } - - def CPUtoGPUarraycopy(a:Array[Double], aoffset:Int, b:Pointer, boffset:Int, len:Int, msg:String ) = { - cudaMemcpy(b.withByteOffset(1L*boffset*Sizeof.DOUBLE), Pointer.to(a).withByteOffset(1L*aoffset*Sizeof.DOUBLE), 1L*len*Sizeof.DOUBLE, cudaMemcpyKind.cudaMemcpyHostToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError; - if (err != 0) throw new RuntimeException(msg +" error in memcpy "+ cudaGetErrorString(err)); - } - - - def newOrCheckGDMat(nr:Int, nc:Int, outmat:Mat):GDMat = { - if (outmat.asInstanceOf[AnyRef] == null || (outmat.nrows == 0 && outmat.ncols == 0)) { - GDMat(nr, nc) - } else { - outmat match { - case omat:GDMat => if (omat.nrows != nr || omat.ncols != nc) { - omat.recycle(nr, nc, 0) - } else { - omat - } - } - } - } - - def newOrCheckGDMat(dims:Array[Int], out:Mat):GDMat = { - if (out.asInstanceOf[AnyRef] != null && ND.compareDims(out.dims.data, dims)) { - out.asInstanceOf[GDMat] - } else { - GDMat.make(dims) - } - } - - def newOrCheckGDMat(dims:IMat, out:Mat):GDMat = newOrCheckGDMat(dims.data, out); - - def newOrCheckGDMat(nr:Int, nc:Int, outmat:Mat, matGuid:Long, opHash:Int):GDMat = { - val m = if (outmat.asInstanceOf[AnyRef] != null || !Mat.useGPUcache ) { - newOrCheckGDMat(nr, nc, outmat) - } else { - val key = (matGuid, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache3(key) - if (res != null) { - newOrCheckGDMat(nr, nc, res) - } else { - val omat = newOrCheckGDMat(nr, nc, null) - Mat.cache3put(key, omat) - omat - } - } - if (m.myGPU != SciFunctions.getGPU) { - throw new RuntimeException("newOrCheckGDMat1 problem with mat %d" format m.GUID) - } - m - } - - def newOrCheckGDMat(dims:Array[Int], out:Mat, matGuid:Long, opHash:Int):GDMat = { - if (out.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGDMat(dims, out) - } else { - val key = (matGuid, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache3(key) - if (res != null) { - newOrCheckGDMat(dims, res) - } else { - val omat = newOrCheckGDMat(dims, null) - Mat.cache3put(key, omat) - omat - } - } - } - - def newOrCheckGDMat(dims0:IMat, out:Mat, g1:Long, opHash:Int):GDMat = newOrCheckGDMat(dims0.data, out, g1, opHash); - - def newOrCheckGDMat(nr:Int, nc:Int, outmat:Mat, guid1:Long, guid2:Long, opHash:Int):GDMat = { - val m = if (outmat.asInstanceOf[AnyRef] != null || !Mat.useGPUcache ) { - newOrCheckGDMat(nr, nc, outmat) - } else { - val key = (guid1, guid2, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache4(key) - if (res != null) { - newOrCheckGDMat(nr, nc, res) - } else { - val omat = newOrCheckGDMat(nr, nc, null) - Mat.cache4put(key, omat) - omat - } - } - if (m.myGPU != SciFunctions.getGPU) { - throw new RuntimeException("newOrCheckGDMat2 problem with mat %d" format m.GUID) - } - m - } - - def newOrCheckGDMat(dims:Array[Int], out:Mat, guid1:Long, guid2:Long, opHash:Int):GDMat = { - if (out.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGDMat(dims, out) - } else { - val key = (guid1, guid2, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache4(key) - if (res != null) { - newOrCheckGDMat(dims, res) - } else { - val omat = newOrCheckGDMat(dims, null) - Mat.cache4put(key, omat) - omat - } - } - } - - def newOrCheckGDMat(dims0:IMat, out:Mat, g1:Long, g2:Long, opHash:Int):GDMat = newOrCheckGDMat(dims0.data, out, g1, g2, opHash); - - - def newOrCheckGDMat(nr:Int, nc:Int, outmat:Mat, guid1:Long, guid2:Long, guid3:Long, opHash:Int):GDMat = { - val m = if (outmat.asInstanceOf[AnyRef] != null || !Mat.useGPUcache ) { - newOrCheckGDMat(nr, nc, outmat) - } else { - val key = (guid1, guid2, guid3, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache5(key) - if (res != null) { - newOrCheckGDMat(nr, nc, res) - } else { - val omat = newOrCheckGDMat(nr, nc, null) - Mat.cache5put(key, omat) - omat - } - } - if (m.myGPU != SciFunctions.getGPU) { - throw new RuntimeException("newOrCheckGDMat3 problem with mat %d" format m.GUID) - } - m - } - - def newOrCheckGDMat(dims:Array[Int], out:Mat, g1:Long, g2:Long, g3:Long, opHash:Int):GDMat = { - if (out.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGDMat(dims, out) - } else { - val key = (g1, g2, g3, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache5(key) - if (res != null) { - newOrCheckGDMat(dims, res) - } else { - val omat = newOrCheckGDMat(dims, null) - Mat.cache5put(key, omat) - omat - } - } - } - - def newOrCheckGDMat(dims0:IMat, out:Mat, g1:Long, g2:Long, g3:Long, opHash:Int):GDMat = newOrCheckGDMat(dims0.data, out, g1, g2, g3, opHash); - - def newOrCheckGDMat(nr:Int, nc:Int, outmat:Mat, guid1:Long, guid2:Long, guid3:Long, guid4:Long, opHash:Int):GDMat = { - val m = if (outmat.asInstanceOf[AnyRef] != null || !Mat.useGPUcache ) { - newOrCheckGDMat(nr, nc, outmat) - } else { - val key = (guid1, guid2, guid3, guid4, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache6(key) - if (res != null) { - newOrCheckGDMat(nr, nc, res) - } else { - val omat = newOrCheckGDMat(nr, nc, null) - Mat.cache6put(key, omat) - omat - } - } - if (m.myGPU != SciFunctions.getGPU) { - throw new RuntimeException("newOrCheckGDMat3 problem with mat %d" format m.GUID) - } - m - } - - def newOrCheckGDMat(dims:Array[Int], out:Mat, g1:Long, g2:Long, g3:Long, g4:Long, opHash:Int):GDMat = { - if (out.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGDMat(dims, out) - } else { - val key = (g1, g2, g3, g4, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache6(key) - if (res != null) { - newOrCheckGDMat(dims, res) - } else { - val omat = newOrCheckGDMat(dims, null) - Mat.cache6put(key, omat) - omat - } - } - } - - def newOrCheckGDMat(dims0:IMat, out:Mat, g1:Long, g2:Long, g3:Long, g4:Long, opHash:Int):GDMat = newOrCheckGDMat(dims0.data, out, g1, g2, g3, g4, opHash); - -} - - - - - - - diff --git a/src/main/scala/BIDMat/GFilter.scala b/src/main/scala/BIDMat/GFilter.scala deleted file mode 100644 index 5e759914..00000000 --- a/src/main/scala/BIDMat/GFilter.scala +++ /dev/null @@ -1,602 +0,0 @@ -package BIDMat -import MatFunctions._ -import jcuda._ -import jcuda.runtime._ -import jcuda.runtime.JCuda._ -import jcuda.jcudnn._ -import jcuda.jcudnn.JCudnn._ -import jcuda.runtime.cudaMemcpyKind._ -import edu.berkeley.bid.MurmurHash3.MurmurHash3_x64_64 - -// -// Basic GPU convolutional Filter class. -// -// Currently a basic wrapper for NVIDIA CUDNN 4d filters (2 spatial dimensions). -// Supported tensor orders are NHWC and NCHW. The NHWC order matches BIDMat's CPU implementation. -// -// The filter is stored with the output dimension major, and the other dimensions in the same order as a data tensor. -// so a 4D filter block would be OHWC. This matches the ordering in NVIDIA CUDNN, and in BIDMat's CPU filter class. -// - - -@SerialVersionUID(100L) -class GFilter(inDims0:IMat, outDims0:IMat, stride0:IMat, pad0:IMat, outPad0:IMat, dataDims0:IMat, data0:Pointer) extends - GMat(dataDims0.data, data0, dataDims0.data.reduce(_*_)) with Filter { - - override def mytype = "GFilter"; - - val inDims = inDims0; - val outDims = outDims0; - val stride = if (stride0.asInstanceOf[AnyRef] != null) stride0 else iones(1, inDims.length); - val pad = if (pad0.asInstanceOf[AnyRef] != null) pad0 else izeros(1,inDims.length); - val outPad = if (outPad0.asInstanceOf[AnyRef] != null) outPad0 else izeros(1,inDims.length); - val dataDims = dataDims0; - var dataType = cudnnDataType.CUDNN_DATA_FLOAT; - var tensorFormat = cudnnTensorFormat.CUDNN_TENSOR_NHWC; - var convType = cudnnConvolutionMode.CUDNN_CROSS_CORRELATION; -// var convType = cudnnConvolutionMode.CUDNN_CONVOLUTION; - - val fwdAlgo = Array(0); - val bwdDataAlgo = Array(0); - val bwdFilterAlgo = Array(0); - var fwdTrained = false; - var bwdDataTrained = false; - var bwdFilterTrained = false; - var adesc:cudnnTensorDescriptor = null; - var bdesc:cudnnTensorDescriptor = null; - var fdesc:cudnnFilterDescriptor = null; - var convdesc:cudnnConvolutionDescriptor = null; - var setBwdDataAlgo = -1; - - var a:GMat = null; - @volatile var workspaceFWD:GMat = null; - @volatile var workspaceBWDdata:GMat = null; - @volatile var workspaceBWDfilter:GMat = null; - - var cudnnMainHandle:cudnnHandle = null; - var cudnn2ndHandle:cudnnHandle = null; - var cudnnMainStream:cudaStream_t = null; - var cudnn2ndStream:cudaStream_t = null; - - def initHandles() = { - cudnnMainHandle = new cudnnHandle; - cudnn2ndHandle = new cudnnHandle; - cudnnMainStream = new cudaStream_t; - cudnn2ndStream = new cudaStream_t; - - var err = cudnnCreate(cudnnMainHandle); - if (err == 0) err = cudnnCreate(cudnn2ndHandle); - if (err == 0) err = cudaStreamCreate(cudnnMainStream); - if (err == 0) err = cudaStreamCreate(cudnn2ndStream); - if (err == 0) err = cudnnSetStream(cudnnMainHandle, cudnnMainStream); - if (err == 0) err = cudnnSetStream(cudnn2ndHandle, cudnn2ndStream); - - if (err != 0) throw new RuntimeException("Error in CUDNN filter creation %s" format cudaGetErrorString(err)); - } - - initHandles(); - - def setNHWC = { - tensorFormat = cudnnTensorFormat.CUDNN_TENSOR_NHWC; - } - - def setNCHW = { - tensorFormat = cudnnTensorFormat.CUDNN_TENSOR_NCHW; - } - - def setTensorFormat(tformat:Int) = { - tformat match { - case cudnnTensorFormat.CUDNN_TENSOR_NCHW => setNCHW; - case cudnnTensorFormat.CUDNN_TENSOR_NHWC => setNHWC; - } - } - - def convolve(a:GMat, omat:Mat, doclear:Boolean, workspace:Mat):GMat = { - val bdims = Filter.getOutputDims(a.dims, inDims, outDims, stride, pad, outPad); - val hmm = ND.hashIMat(stride, ND.hashIMat(pad)); - val b = GMat.newOrCheckGMat(bdims, omat, a.GUID, GUID, hmm, "convout".##); - if (dims.length == 4) { - val adesc = new cudnnTensorDescriptor; - cudnnCreateTensorDescriptor(adesc); - val astatus = cudnnSetTensor4dDescriptor(adesc, tensorFormat, dataType, a.dims(3), a.dims(0), a.dims(2), a.dims(1)); - if (astatus > 0) throw new RuntimeException("Error creating A tensor for forward convolution %d, bad stride?" format astatus) - - val bdesc = new cudnnTensorDescriptor; - cudnnCreateTensorDescriptor(bdesc); - val bstatus = cudnnSetTensor4dDescriptor(bdesc, tensorFormat, dataType, b.dims(3), b.dims(0), b.dims(2), b.dims(1)); - if (bstatus > 0) throw new RuntimeException("Error creating B tensor for forward convolution %d, bad stride?" format bstatus) - - val fdesc = new cudnnFilterDescriptor; - cudnnCreateFilterDescriptor(fdesc); - val fstatus = cudnnSetFilter4dDescriptor(fdesc, dataType, tensorFormat, outDims(0), inDims(0), inDims(2), inDims(1)); - if (fstatus > 0) throw new RuntimeException("Error creating filter tensor for forward convolution %d" format fstatus) - - val convdesc = new cudnnConvolutionDescriptor; - cudnnCreateConvolutionDescriptor(convdesc); - val cstatus = cudnnSetConvolution2dDescriptor(convdesc, pad(2), pad(1), stride(2), stride(1), 1, 1, convType, dataType); - if (cstatus > 0) throw new RuntimeException("Error setting convolution descriptor for forward convolution %d" format cstatus); - - if (!fwdTrained) { - val (preference, limit) = if (workspace.asInstanceOf[AnyRef] != null) { - (cudnnConvolutionFwdPreference.CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT, workspace.length*4L); - } else { - (cudnnConvolutionFwdPreference.CUDNN_CONVOLUTION_FWD_PREFER_FASTEST, 0L); - } - val gstatus = cudnnGetConvolutionForwardAlgorithm(cudnnMainHandle, adesc, fdesc, convdesc, bdesc, preference, limit, fwdAlgo); - if (gstatus > 0) throw new RuntimeException("Error getting best algorithm for forward convolution %d" format gstatus); - fwdTrained = true; - } - - val _workspaceSizeInBytes = new Array[Long](1); - var wserr = cudnnGetConvolutionForwardWorkspaceSize(cudnnMainHandle, adesc, fdesc, convdesc, bdesc, fwdAlgo(0), _workspaceSizeInBytes); - val workspaceSizeInBytes = _workspaceSizeInBytes(0); - workspaceFWD = if (workspace.asInstanceOf[AnyRef] != null) { - workspace.asInstanceOf[GMat]; - } else { - GMat.newOrCheckGMat((workspaceSizeInBytes/4).toInt, 1, null, GUID, a.GUID, hmm, "ConvFwdWS".##); - } - - var err = cudnnConvolutionForward(cudnnMainHandle, GFilter.ONE, adesc, a.pdata, fdesc, pdata, convdesc, - fwdAlgo(0), workspaceFWD.pdata, workspaceSizeInBytes, if (doclear) GFilter.ZERO else GFilter.ONE, bdesc, b.pdata); - - cudaStreamSynchronize(cudnnMainStream); - if (err == 0) err = cudaGetLastError(); - if (err > 0) throw new RuntimeException("Error in CUDNN forward convolution %s" format cudaGetErrorString(err)); - - cudnnDestroyConvolutionDescriptor(convdesc); - cudnnDestroyFilterDescriptor(fdesc); - cudnnDestroyTensorDescriptor(bdesc); - cudnnDestroyTensorDescriptor(adesc); - } - Mat.nflops += computeFlops(a, stride, pad); - b - } - - def convolve(a:GMat, omat:Mat, doclear:Boolean):GMat = convolve(a, omat, doclear, null); - - def convolve(a:GMat):GMat = convolve(a, null, true, null); - - def convolveT(deriv:GMat, inderiv:Mat, doclear:Boolean, workspace:Mat):GMat = { - val ddims = if (inderiv.asInstanceOf[AnyRef] != null) { // The in-out map with stride/pad is many to one, so not invertible always - Filter.getOutputDims(inderiv.dims, inDims, outDims, stride, pad, outPad); // Here check if the deriv/inputDeriv pair are compatible - } else { - null; - } // Otherwise compute an input size from the params - val adims = if (ddims.asInstanceOf[AnyRef] != null && ND.compareDims(ddims.data, deriv.dims.data)){ - inderiv.dims; - } else { - Filter.getInputDims(deriv.dims, inDims, outDims, stride, pad, outPad); - } - val hmm = ND.hashIMat(stride, ND.hashIMat(pad)); - a = GMat.newOrCheckGMat(adims, inderiv, deriv.GUID, GUID, hmm, "convoutT".##); - if (dims.length == 4) { - val adesc = new cudnnTensorDescriptor; - cudnnCreateTensorDescriptor(adesc); - val astatus = cudnnSetTensor4dDescriptor(adesc, tensorFormat, dataType, a.dims(3), a.dims(0), a.dims(2), a.dims(1)); - if (astatus > 0) throw new RuntimeException("Error creating A tensor for backward data convolution %d, bad stride?" format astatus); - - val bdesc = new cudnnTensorDescriptor; - cudnnCreateTensorDescriptor(bdesc); - val bstatus = cudnnSetTensor4dDescriptor(bdesc, tensorFormat, dataType, deriv.dims(3), deriv.dims(0), deriv.dims(2), deriv.dims(1)); - if (bstatus > 0) throw new RuntimeException("Error creating B tensor for backward data convolution %d, bad stride?" format bstatus); - - val fdesc = new cudnnFilterDescriptor; - cudnnCreateFilterDescriptor(fdesc); - val fstatus = cudnnSetFilter4dDescriptor(fdesc, dataType, tensorFormat, outDims(0), inDims(0), inDims(2), inDims(1)); - if (fstatus > 0) throw new RuntimeException("Error creating filter tensor for backward data convolution %d" format fstatus); - - val convdesc = new cudnnConvolutionDescriptor; - cudnnCreateConvolutionDescriptor(convdesc); - val cstatus = cudnnSetConvolution2dDescriptor(convdesc, pad(2), pad(1), stride(2), stride(1), 1, 1, convType, dataType); - if (cstatus > 0) throw new RuntimeException("Error setting convolution descriptor for backward data convolution %d" format cstatus); - - if (!bwdDataTrained) { - val (preference, limit) = if (workspace.asInstanceOf[AnyRef] != null) { - (cudnnConvolutionBwdDataPreference.CUDNN_CONVOLUTION_BWD_DATA_SPECIFY_WORKSPACE_LIMIT, workspace.length*4L); - } else { - (cudnnConvolutionBwdDataPreference.CUDNN_CONVOLUTION_BWD_DATA_PREFER_FASTEST, 0L); - } - val gstatus = cudnnGetConvolutionBackwardDataAlgorithm(cudnnMainHandle, fdesc, bdesc, convdesc, adesc, preference, limit, bwdDataAlgo); - if (gstatus > 0) throw new RuntimeException("Error getting best algorithm for backward data convolution %d" format gstatus); - bwdDataTrained = true; - } - if (setBwdDataAlgo >= 0) - bwdDataAlgo(0) = setBwdDataAlgo; //Patch - - val _workspaceSizeInBytes = new Array[Long](1); - var wserr = cudnnGetConvolutionBackwardDataWorkspaceSize(cudnnMainHandle, fdesc, bdesc, convdesc, adesc, bwdDataAlgo(0), _workspaceSizeInBytes); - val workspaceSizeInBytes = _workspaceSizeInBytes(0); - workspaceBWDdata = if (workspace.asInstanceOf[AnyRef] != null) { - workspace.asInstanceOf[GMat]; - } else { - GMat.newOrCheckGMat((workspaceSizeInBytes/4).toInt, 1, null, GUID, a.GUID, hmm, "ConvBwdWS".##); - } - - var err = cudnnConvolutionBackwardData(cudnnMainHandle, GFilter.ONE, fdesc, pdata, bdesc, deriv.pdata, convdesc, - bwdDataAlgo(0), workspaceBWDdata.pdata, workspaceSizeInBytes, if (doclear) GFilter.ZERO else GFilter.ONE, adesc, a.pdata); - - cudaStreamSynchronize(cudnnMainStream); - if (err == 0) err = cudaGetLastError(); - if (err > 0) throw new RuntimeException("Error in CUDNN backward data convolution %s" format cudaGetErrorString(err)); - - cudnnDestroyConvolutionDescriptor(convdesc); - cudnnDestroyFilterDescriptor(fdesc); - cudnnDestroyTensorDescriptor(bdesc); - cudnnDestroyTensorDescriptor(adesc); - } - Mat.nflops += computeFlops(a, stride, pad); - a - } - - def convolveT(a:GMat, b:Mat, doclear:Boolean):GMat = convolveT(a, b, doclear, null); - - def convolveT(a:GMat):GMat = convolveT(a, null, true, null); - - def convolveMfork(a:GMat, b:GMat, doclear:Boolean, workspace:Mat):GFilter= { - val bdims = b.dims; - val outdims = Filter.getOutputDims(a.dims, inDims, outDims, stride, pad, outPad); - if ((bdims - outdims).data.exists(_ != 0)) { - throw new RuntimeException("Output dimensions mismatch in convolveM") - } - val hmm = ND.hashIMat(stride, ND.hashIMat(pad)); - if (dims.length == 4) { - adesc = new cudnnTensorDescriptor; - cudnnCreateTensorDescriptor(adesc); - val astatus = cudnnSetTensor4dDescriptor(adesc, tensorFormat, dataType, a.dims(3), a.dims(0), a.dims(2), a.dims(1)); - if (astatus > 0) throw new RuntimeException("Error creating A tensor for backward filter convolution %d, bad stride?" format astatus) - - bdesc = new cudnnTensorDescriptor; - cudnnCreateTensorDescriptor(bdesc); - val bstatus = cudnnSetTensor4dDescriptor(bdesc, tensorFormat, dataType, b.dims(3), b.dims(0), b.dims(2), b.dims(1)); - if (bstatus > 0) throw new RuntimeException("Error creating B tensor for backward filter convolution %d, bad stride?" format bstatus) - - fdesc = new cudnnFilterDescriptor; - cudnnCreateFilterDescriptor(fdesc); - val fstatus = cudnnSetFilter4dDescriptor(fdesc, dataType, tensorFormat, outDims(0), inDims(0), inDims(2), inDims(1)); - if (fstatus > 0) throw new RuntimeException("Error creating filter tensor for backward filter convolution %d" format fstatus) - - convdesc = new cudnnConvolutionDescriptor; - cudnnCreateConvolutionDescriptor(convdesc); - val cstatus = cudnnSetConvolution2dDescriptor(convdesc, pad(2), pad(1), stride(2), stride(1), 1, 1, convType, dataType); - if (cstatus > 0) throw new RuntimeException("Error setting convolution descriptor for backward filter convolution %d" format cstatus); - - if (!bwdFilterTrained) { - val (preference, limit) = if (workspace.asInstanceOf[AnyRef] != null) { - (cudnnConvolutionBwdFilterPreference.CUDNN_CONVOLUTION_BWD_FILTER_SPECIFY_WORKSPACE_LIMIT, workspace.length*4L); - } else { - (cudnnConvolutionBwdFilterPreference.CUDNN_CONVOLUTION_BWD_FILTER_PREFER_FASTEST, 0L); - } - val gstatus = cudnnGetConvolutionBackwardFilterAlgorithm(cudnn2ndHandle, adesc, bdesc, convdesc, fdesc, preference, limit, bwdFilterAlgo); - if (gstatus > 0) throw new RuntimeException("Error getting best algorithm for backward filter convolution %d" format gstatus); - bwdFilterTrained = true; - } - - val _workspaceSizeInBytes = new Array[Long](1); - var wserr = cudnnGetConvolutionBackwardFilterWorkspaceSize(cudnn2ndHandle, adesc, bdesc, convdesc, fdesc, bwdFilterAlgo(0), _workspaceSizeInBytes); - val workspaceSizeInBytes = _workspaceSizeInBytes(0); - - workspaceBWDfilter = if (workspace.asInstanceOf[AnyRef] != null) { - workspace.asInstanceOf[GMat]; - } else { - GMat.newOrCheckGMat((workspaceSizeInBytes/4).toInt, 1, null, GUID, a.GUID, hmm, "ConvBwdFilterWS".##); - } - var err = cudnnConvolutionBackwardFilter(cudnn2ndHandle, GFilter.ONE, adesc, a.pdata, bdesc, b.pdata, convdesc, - bwdFilterAlgo(0), workspaceBWDfilter.pdata, workspaceSizeInBytes, if (doclear) GFilter.ZERO else GFilter.ONE, fdesc, pdata); - - if (err > 0) throw new RuntimeException("Error in CUDNN backward filter convolution %s" format cudaGetErrorString(err)); - - } - Mat.nflops += computeFlops(a, stride, pad); - this - } - - def convolveMfork(a:GMat, b:GMat, doclear:Boolean):GFilter = convolveMfork(a, b, doclear, null); - - def convolveMfork(a:GMat, b:GMat):GFilter = convolveMfork(a, b, true, null); - - override def convolveMjoin:GFilter = { - cudaStreamSynchronize(cudnn2ndStream); - val err = cudaGetLastError(); - if (err > 0) throw new RuntimeException("Error in CUDNN backward data convolution %s" format cudaGetErrorString(err)); - cudnnDestroyConvolutionDescriptor(convdesc); - cudnnDestroyFilterDescriptor(fdesc); - cudnnDestroyTensorDescriptor(bdesc); - cudnnDestroyTensorDescriptor(adesc); - this; - } - - def convolveM(a:GMat, b:GMat, doclear:Boolean, ws:Mat):GFilter = { - convolveMfork(a, b, doclear, ws); - convolveMjoin; - } - - def convolveM(a:GMat, b:GMat, doclear:Boolean):GFilter = { - convolveMfork(a, b, doclear, null); - convolveMjoin; - } - - def convolveM(a:GMat, b:GMat):GFilter = { - convolveMfork(a, b, true, null); - convolveMjoin; - } - - override def convolve(b:Mat, omat:Mat, doclear:Boolean):Mat = { - b match { - case (bb:GMat) => convolve(bb, omat, doclear); - } - } - - override def convolve(b:Mat, omat:Mat, doclear:Boolean, workspace:Mat):Mat = { - b match { - case (bb:GMat) => convolve(bb, omat, doclear, workspace); - } - } - - override def convolveT(b:Mat, omat:Mat, doclear:Boolean):Mat = { - b match { - case (bb:GMat) => convolveT(bb, omat, doclear); - } - } - - override def convolveT(b:Mat, omat:Mat, doclear:Boolean, workspace:Mat):Mat = { - b match { - case (bb:GMat) => convolveT(bb, omat, doclear, workspace); - } - } - - override def convolveM(a:Mat, b:Mat, doclear:Boolean):Filter = { - (a, b) match { - case (aa:GMat, bb:GMat) => convolveM(aa, bb, doclear, null); - } - } - - override def convolveM(a:Mat, b:Mat, doclear:Boolean, ws:Mat):Filter = { - (a, b) match { - case (aa:GMat, bb:GMat) => convolveM(aa, bb, doclear, ws); - } - } - - override def convolveMfork(a:Mat, b:Mat, doclear:Boolean):Filter = { - (a, b) match { - case (aa:GMat, bb:GMat) => convolveMfork(aa, bb, doclear, null); - } - } - - override def convolveMfork(a:Mat, b:Mat, doclear:Boolean, ws:Mat):Filter = { - (a, b) match { - case (aa:GMat, bb:GMat) => convolveMfork(aa, bb, doclear, ws); - } - } - - override def copy:GFilter = { - val out = new GFilter(inDims.copy, outDims.copy, stride.copy, pad.copy, outPad.copy, dataDims.copy, new Pointer); - val len = 1L*length*Sizeof.FLOAT; - if (Mat.debugMem) { - println("GFilter %d %d, %d %f" format (nrows, ncols, SciFunctions.getGPU, SciFunctions.GPUmem._1)) - if (length > Mat.debugMemThreshold) throw new RuntimeException("GFilter alloc too large"); - } - out.convType = convType; - out.tensorFormat = tensorFormat; - cudaMalloc(out.pdata, len); - cudaStreamSynchronize(Mat.SyncMethod); - cudaMemcpy(out.pdata, pdata, len, cudaMemcpyDeviceToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - out; - } - - def toNCHW:GFilter = { - if (dims.length != 4) throw new RuntimeException("fromNHWCtoNCHW ndims must be 4"); - if (tensorFormat == cudnnTensorFormat.CUDNN_TENSOR_NCHW) { - return this; - } else { - val tmp = transpose(MatFunctions.irow(1,2,0,3)); - val out = new GFilter(inDims.copy, outDims.copy, stride.copy, pad.copy, outPad.copy, dataDims.copy, tmp.pdata); - out.setNCHW; - out.setGUID(MurmurHash3_x64_64(Array(GUID), "fromNHWCtoNCHW".##)); - out - } - } - - def toNHWC:GFilter = { - if (dims.length != 4) throw new RuntimeException("fromNCHWtoNHWC ndims must be 4"); - if (tensorFormat == cudnnTensorFormat.CUDNN_TENSOR_NHWC) { - return this; - } else { - val tmp = reshapeView(MatFunctions.irow(dims(1), dims(2), dims(0), dims(3))).transpose(MatFunctions.irow(2,0,1,3)); - val out = new GFilter(inDims.copy, outDims.copy, stride.copy, pad.copy, outPad.copy, dataDims.copy, tmp.pdata); - out.setNHWC; - out.setGUID(MurmurHash3_x64_64(Array(GUID), "fromNHWCtoNCHW".##)); - out; - } - } - - override def * (a:GMat):GMat = { - convolve(a); - } - - override def ^* (a:GMat):GMat = { - convolveT(a); - } - - override def xavier(scale:Float):GFilter = GFilter.xavier(this, scale); - - override def xavier:GFilter = GFilter.xavier(this, 1f); - - override def transpose(p:IMat):GFilter = { - new GFilter(inDims, outDims, stride, pad, outPad, dataDims(dataDims.length-1) \ dataDims(0->(dataDims.length-1)), _transpose(p).pdata); - } -} - -object GFilter { - - val ONE = Pointer.to(Array(1f)); - val ZERO = Pointer.to(Array(0f)); - - def apply(a:FFilter):GFilter = { - val outnd = GMat.newOrCheckGMat(a.dims, null, a.GUID, "GFilter".##); - val out = new GFilter(a.inDims, a.outDims, a.stride, a.pad, a.outPad, a.dataDims, outnd.pdata); - GMat.CPUtoGPUarraycopy(a.data, 0, out.pdata, 0, a.length, "GFilter apply"); - out.tensorFormat = a.tensorFormat; - out.convType = a.convType; - out.setGUID(MurmurHash3_x64_64(Array(a.GUID), "GFilter apply".##)); - out; - } - - def apply(inDims:IMat,outDims:IMat, stride:IMat, pad:IMat, outPad:IMat, dataDims:IMat) = { - val len = dataDims.data.reduce(_*_); - if (Mat.debugMem) { - println("GFilter %d %d %f" format (len, SciFunctions.getGPU, SciFunctions.GPUmem._1)) - if (len > Mat.debugMemThreshold) throw new RuntimeException("GFilter alloc too large"); - } - val out = new GFilter(inDims, outDims, stride, pad, outPad, dataDims,new Pointer); - cudaMalloc(out.pdata, 1L*len*Sizeof.FLOAT); - cudaStreamSynchronize(Mat.SyncMethod); - out; - } - - def alias(inDims:IMat,outDims:IMat, stride:IMat, pad:IMat, outPad:IMat, dataDims:IMat, a:GMat) = { - if (a.asInstanceOf[AnyRef] != null) { - ND.checkDims("GFilter alias", dataDims, a.dims); - val out = new GFilter(inDims, outDims, stride, pad, outPad, dataDims, a.pdata); - out.setGUID(MurmurHash3_x64_64(Array(a.GUID), "GFilter apply".##)); - out; - } else { - val out = new GFilter(inDims, outDims, stride, pad, outPad, dataDims, new Pointer); - val len = dataDims.data.reduce(_*_); - cudaMalloc(out.pdata, 1L*len*Sizeof.FLOAT); - cudaStreamSynchronize(Mat.SyncMethod); - if (Mat.debugMem) { - println("GFilter %d %d %f" format (len, SciFunctions.getGPU, SciFunctions.GPUmem._1)) - if (len > Mat.debugMemThreshold) throw new RuntimeException("GFilter alloc too large"); - } - out; - } - } - - def GFilter1D(w:Int, nstride:Int, npad:Int, noutpad:Int):GFilter = { - val inDims = irow(w); - val outDims = irow(1); - val stride = irow(nstride); - val pad = irow(npad); - val outPad = irow(noutpad); - val out = GFilter(inDims, outDims, stride, pad, outPad, irow(w, 1)); - out - } - - def GFilter1D(w:Int, nstride:Int, npad:Int, noutpad:Int, a:GMat):GFilter = { - val inDims = irow(w); - val outDims = irow(1); - val stride = irow(nstride); - val pad = irow(npad); - val outPad = irow(noutpad); - val out = GFilter.alias(inDims, outDims, stride, pad, outPad, irow(w, 1), a); - out - } - - def GFilter1D(w:Int, nstride:Int, npad:Int):GFilter = GFilter1D(w, nstride, npad, 0); - - def GFilter1D(w:Int, nstride:Int, npad:Int, a:GMat):GFilter = GFilter1D(w, nstride, npad, 0, a); - - def GFilter1Dd(w:Int, din:Int, dout:Int, nstride:Int, npad:Int, noutpad:Int):GFilter = { - val inDims = irow(din, w); - val outDims = irow(dout, 1); - val stride = irow(1, nstride); - val pad = irow(0, npad); - val outPad = irow(0, noutpad); - val out = GFilter(inDims, outDims, stride, pad, outPad, irow(din, w, dout)); - out - } - - def GFilter1Dd(w:Int, din:Int, dout:Int, nstride:Int, npad:Int, noutpad:Int, a:GMat):GFilter = { - val inDims = irow(din, w); - val outDims = irow(dout, 1); - val stride = irow(1, nstride); - val pad = irow(0, npad); - val outPad = irow(0, noutpad); - val out = GFilter.alias(inDims, outDims, stride, pad, outPad, irow(din, w, dout), a); - out - } - - def GFilter1Dd(w:Int, din:Int, dout:Int, nstride:Int, npad:Int):GFilter = GFilter1Dd(w, din, dout, nstride, npad, 0); - - def GFilter1Dd(w:Int, din:Int, dout:Int, nstride:Int, npad:Int, a:GMat):GFilter = GFilter1Dd(w, din, dout, nstride, npad, 0, a); - - def GFilter2D(w:Int, h:Int, nstride:Int, npad:Int, noutpad:Int):GFilter = { - val inDims = irow(w, h); - val outDims = irow(1, 1); - val stride = irow(nstride, nstride); - val pad = irow(npad, npad); - val outPad = irow(noutpad, noutpad); - val out = GFilter(inDims, outDims, stride, pad, outPad, irow(w, h)); - out - } - - def GFilter2D(w:Int, h:Int, nstride:Int, npad:Int, noutpad:Int, a:GMat):GFilter = { - val inDims = irow(w, h); - val outDims = irow(1, 1); - val stride = irow(nstride, nstride); - val pad = irow(npad, npad); - val outPad = irow(noutpad, noutpad); - val out = GFilter.alias(inDims, outDims, stride, pad, outPad, irow(w, h), a); - out - } - - def GFilter2D(w:Int, h:Int, nstride:Int, npad:Int):GFilter = GFilter2D(w, h, nstride, npad, 0); - - def GFilter2D(w:Int, h:Int, nstride:Int, npad:Int, a:GMat):GFilter = GFilter2D(w, h, nstride, npad, 0, a); - - def GFilter2Dd(w:Int, h:Int, din:Int, dout:Int, nstride:Int, npad:Int, noutpad:Int):GFilter = { - val inDims = irow(din, w, h); - val outDims = irow(dout, 1, 1); - val stride = irow(1, nstride, nstride); - val pad = irow(0, npad, npad); - val outPad = irow(0, noutpad, noutpad); - val out = GFilter(inDims, outDims, stride, pad, outPad, irow(din, w, h, dout)); - out - } - - def GFilter2Dd(w:Int, h:Int, din:Int, dout:Int, nstride:Int, npad:Int, noutpad:Int, a:GMat):GFilter = { - val inDims = irow(din, w, h); - val outDims = irow(dout, 1, 1); - val stride = irow(1, nstride, nstride); - val pad = irow(0, npad, npad); - val outPad = irow(0, noutpad, noutpad); - val out = GFilter.alias(inDims, outDims, stride, pad, outPad, irow(din, w, h, dout), a); - out - } - - def GFilter2Dd(w:Int, h:Int, din:Int, dout:Int, nstride:Int, npad:Int):GFilter = GFilter2Dd(w, h, dout, nstride, npad, 0); - - def GFilter2Dd(w:Int, h:Int, din:Int, dout:Int, nstride:Int, npad:Int, a:GMat):GFilter = GFilter2Dd(w, h, dout, nstride, npad, 0, a); - - def GFilter2Ddn(w:Int, h:Int, din:Int, dout:Int, nstride:Int, npad:Int, noutpad:Int):GFilter = { - val inDims = irow(din, w, h, 1); - val outDims = irow(dout, 1, 1, 1); - val stride = irow(1, nstride, nstride, 1); - val pad = irow(0, npad, npad, 0); - val outPad = irow(0, noutpad, noutpad, 0); - val out = GFilter(inDims, outDims, stride, pad, outPad, irow(din, w, h, dout)); - out - } - - def GFilter2Ddn(w:Int, h:Int, din:Int, dout:Int, nstride:Int, npad:Int, noutpad:Int, a:GMat):GFilter = { - val inDims = irow(din, w, h, 1); - val outDims = irow(dout, 1, 1, 1); - val stride = irow(1, nstride, nstride, 1); - val pad = irow(0, npad, npad, 0); - val outPad = irow(0, noutpad, noutpad, 0); - val out = GFilter.alias(inDims, outDims, stride, pad, outPad, irow(din, w, h, dout), a); - out - } - - def GFilter2Ddn(w:Int, h:Int, din:Int, dout:Int, nstride:Int, npad:Int):GFilter = GFilter2Ddn(w, h, din, dout, nstride, npad, 0); - - def GFilter2Ddn(w:Int, h:Int, din:Int, dout:Int, nstride:Int, npad:Int, a:GMat):GFilter = GFilter2Ddn(w, h, din, dout, nstride, npad, 0, a); - - def xavier(f:GFilter, fscale:Float):GFilter = { - val scale = f.inDims.data.reduce(_*_); - GFunctions.normrnd(0, fscale/math.sqrt(scale).toFloat, f); - f; - } - -} diff --git a/src/main/scala/BIDMat/GFunctions.scala b/src/main/scala/BIDMat/GFunctions.scala deleted file mode 100644 index 4c3f208a..00000000 --- a/src/main/scala/BIDMat/GFunctions.scala +++ /dev/null @@ -1,1128 +0,0 @@ -package BIDMat - -import java.util.Random._; -import org.apache.commons.math3.special._ -import org.apache.commons.math3.util.FastMath -import org.apache.commons.math3.random.RandomDataGenerator; -import scala.concurrent.Future -import scala.concurrent.ExecutionContext.Implicits.global -import edu.berkeley.bid.MurmurHash3.MurmurHash3_x64_64 -import edu.berkeley.bid.CUMAT -import edu.berkeley.bid.SLATEC; -import GMat.BinOp -import GMat.TransF -import GMat.TransF2 -import SciState._; -import jcuda._ -import jcuda.runtime._ -import jcuda.runtime.JCuda._ -import jcuda.runtime.cudaMemcpyKind._ -import jcuda.jcublas._ -import jcuda.jcublas.JCublas2._ -import jcuda.jcusparse._ - - -object GFunctions { - - var cudarng:Array[AnyRef] = null; // Use AnyRef to avoid errors from loading CUDA classes on non-CUDA machines. - var cudarngStream:Array[AnyRef] = null; // An array of per-GPU streams for random number generation. - var cublasHandles:Array[AnyRef] = null - - def initCUDArngs = { - val thisGPU = getGPU - cudarng = new Array[AnyRef](Mat.hasCUDA) - cudarngStream = new Array[AnyRef](Mat.hasCUDA) - for (i <- 0 until Mat.hasCUDA) { - setGPU(i) - initCUDArng(i) - } - setGPU(thisGPU) - } - - def initCublas = { - val thisGPU = getGPU - cublasHandles = new Array[AnyRef](Mat.hasCUDA) - for (i <- 0 until Mat.hasCUDA) { - setGPU(i); - val handle = new cublasHandle; - cublasCreate(handle); - cublasSetStream(handle, Mat.SyncMethod); - cublasHandles(i) = handle; - } - setGPU(thisGPU) - } - - def initCUDArng(igpu:Int) = { - import jcuda.jcurand.curandGenerator; - import jcuda.jcurand.JCurand._; - import jcuda.jcurand.curandRngType._; - val thisGPU = getGPU; - setGPU(igpu); - val generator = new curandGenerator; - curandCreateGenerator(generator, CURAND_RNG_PSEUDO_DEFAULT); - curandSetPseudoRandomGeneratorSeed(generator, GPUSEED+igpu); - cudarng(igpu) = generator; - val cstream = new cudaStream_t; - cudaStreamCreate(cstream); - curandSetStream(generator, cstream); - cudarngStream(igpu) = cstream; - setGPU(thisGPU); - } - - def resetGPU = { - import jcuda.runtime._; - JCuda.cudaDeviceReset - JCuda.cudaStreamSynchronize(Mat.SyncMethod) - initCUDArng(getGPU) - GSMat.cusparseContextsInitialized = false - GSMat.cusparseDescrsInitialized = false - Mat.clearCaches - } - - def initCUDNN(verbose:Boolean = false) = { - try { - Mat.hasCUDNN = true; - GFilter.GFilter1D(1,1,0); - } catch { - case e:Throwable => { - Mat.hasCUDNN = false; - if (verbose) { - e.printStackTrace(); - } - } - } - } - - def moveGPUseed = { - var i = 0; - while (i < GPUseedSteps) { - GPUSEED = SciState.myrand.nextLong(); - i += 1; - } - } - - - def resetGPUs = { - import jcuda.runtime._; - val oldi = getGPU - for (i <- 0 until Mat.hasCUDA) { - JCuda.cudaSetDevice(i); - resetGPU; - } - initCublas; - JCuda.cudaSetDevice(oldi) - } - - def initJCUDA = jcuda.runtime.JCuda.initialize; - - def setseed(seed:Int, igpu:Int) = { - import jcuda.jcurand.JCurand._ - val thisGPU = getGPU - setGPU(igpu) - GPUSEED = seed; - val gen = cudarng(igpu).asInstanceOf[jcuda.jcurand.curandGenerator]; - curandSetPseudoRandomGeneratorSeed(gen, GPUSEED+igpu) - curandSetGeneratorOffset(gen, 100) - setGPU(thisGPU) - } - - def setGPU(i:Int) = jcuda.runtime.JCuda.cudaSetDevice(i) - - def getGPU:Int = { - val ar = Array[Int](1) - jcuda.runtime.JCuda.cudaGetDevice(ar) - ar(0) - } - - def connect(i:Int) = { - val v0 = jcuda.runtime.JCuda.cudaDeviceEnablePeerAccess(i,0) - val j = getGPU - setGPU(i) - val v1 = jcuda.runtime.JCuda.cudaDeviceEnablePeerAccess(j,0) - setGPU(j) - (v0, v1) - } - - def disconnect(i:Int) = { - val v0 = jcuda.runtime.JCuda.cudaDeviceDisablePeerAccess(i) - val j = getGPU - setGPU(i) - val v1 = jcuda.runtime.JCuda.cudaDeviceDisablePeerAccess(j) - setGPU(j) - (v0, v1) - } - - def canconnect(i:Int) = { - val ar = Array[Int](1) - val j = getGPU - jcuda.runtime.JCuda.cudaDeviceCanAccessPeer(ar, i, j) - val v0 = ar(0) - jcuda.runtime.JCuda.cudaDeviceCanAccessPeer(ar, j, i) - (v0, ar(0)) - } - - -/* - * NCCL Ops - * - * ncclSum = 0, - * ncclProd = 1, - * ncclMax = 2, - * ncclMin = 3, - * - */ - - def ncclAllReduce(from:Array[GMat], to:Array[GMat], op:Int):Int = { - import edu.berkeley.bid.NCCL; - val len = from.length; - if (len != to.length) throw new RuntimeException("allReduce from a to array lengths must match"); - if (len > Mat.hasCUDA) throw new RuntimeException("allReduce array lengths must be <= number of GPUs"); - - val comms = new Array[NCCL](len); - for (i <- 0 until len) comms(i) = new NCCL; - NCCL.ncclCommInitAll(comms); - - val streams = new Array[cudaStream_t](len); - val thisGPU = SciFunctions.getGPU; - for (i <- 0 until len) { - SciFunctions.setGPU(i); - streams(i) = new cudaStream_t; - cudaStreamCreate(streams(i)); - NCCL.ncclAllReduce(from(i).pdata, to(i).pdata, from(i).length, 3, op, comms(i), streams(i)); - } - for (i <- 0 until len) { - SciFunctions.setGPU(i); - cudaStreamSynchronize(streams(i)); - } - for (i <- 0 until len) { - SciFunctions.setGPU(i); - cudaStreamDestroy(streams(i)); - NCCL.ncclCommDestroy(comms(i)); - } - SciFunctions.setGPU(thisGPU); - } - - val freeMemArray = new Array[Long](1) - val totalMemArray = new Array[Long](1); - - def GPUmem = { - jcuda.runtime.JCuda.cudaMemGetInfo(freeMemArray, totalMemArray) - val fm = freeMemArray(0); - val tm = totalMemArray(0); - (fm.toFloat/ tm, fm, tm) - } - - def GPUmemory = { - jcuda.runtime.JCuda.cudaMemGetInfo(freeMemArray, totalMemArray) - val fm = freeMemArray(0); - val tm = totalMemArray(0); - println("GPU memory %3.2f%% free out of %2.1f GB" format (fm.toFloat/tm, tm*1e-9)); - } - - def max(a:GMat, b:GMat, out:Mat):GMat = a.gOp(b, out, BinOp.op_max) - def min(a:GMat, b:GMat, out:Mat):GMat = a.gOp(b, out, BinOp.op_min) - - def maxi(a:GMat, dir:Int, out:Mat):GMat = a.reduceOp(out, dir, Float.MinValue, BinOp.op_max); - def mini(a:GMat, dir:Int, out:Mat):GMat = a.reduceOp(out, dir, Float.MaxValue, BinOp.op_min); - def sum(a:GMat, dir:Int, out:Mat):GMat = a.reduceOp(out, dir, 0f, BinOp.op_add); - def prod(a:GMat, dir:Int, out:Mat):GMat = a.reduceOp(out, dir, 1f, BinOp.op_mul); - - def rand(out:GMat):GMat = { - import jcuda.jcurand._ - Mat.nflops += 10L*out.length - JCurand.curandGenerateUniform(cudarng(getGPU).asInstanceOf[curandGenerator], out.pdata, out.length) - jcuda.runtime.JCuda.cudaStreamSynchronize(cudarngStream(getGPU).asInstanceOf[cudaStream_t]); - out - } - - def rand(dims:Array[Int]):GMat = { - val out = GMat.make(dims); - rand(out); - } - - def rand(dims:IMat):GMat = rand(dims.data); - - def normrnd(mu:Float, sig:Float, out:GMat):GMat = { - import jcuda.jcurand._ - Mat.nflops += 10L*out.length - JCurand.curandGenerateNormal(cudarng(getGPU).asInstanceOf[curandGenerator], out.pdata, out.length, mu, sig) - jcuda.runtime.JCuda.cudaStreamSynchronize(cudarngStream(getGPU).asInstanceOf[cudaStream_t]); - out - } - - def poissrnd(mu:Float, out:GIMat):GIMat = { - import jcuda.jcurand._; - Mat.nflops += 10L*out.length; - JCurand.curandGeneratePoisson(cudarng(getGPU).asInstanceOf[curandGenerator], out.pdata, out.length, mu); - jcuda.runtime.JCuda.cudaStreamSynchronize(cudarngStream(getGPU).asInstanceOf[cudaStream_t]); - out - } - - def poissrnd(mu:GMat, out:GIMat):GIMat = { - Mat.nflops += 10L*out.length; - val nthreads = math.max(1, mu.length / 1024); - moveGPUseed; - CUMAT.poissonrnd(out.length, mu.pdata, out.pdata, nthreads, GPUSEED, OFFSET); - out - } - - def getMatVecType(m:Mat):Int = { - if (m.nrows == 1) { - if (m.ncols == 1) 0 else 2; - } else { - if (m.ncols == 1) 1 else 3; - } - } - - def gamrnd(a:GMat, b:GMat, out:GMat):GMat = { - Mat.nflops += 100L*out.length; - val atype = getMatVecType(a); - val btype = getMatVecType(b); - moveGPUseed; - CUMAT.gamrnd(out.nrows, out.ncols, a.pdata, atype, b.pdata, btype, out.pdata, GPUSEED, OFFSET); - out; - } - - - def binornd(n:GIMat, p:GMat, out:GIMat):GIMat = { - Mat.nflops += 300L*out.length - val atype = getMatVecType(p); - val ctype = getMatVecType(n); - moveGPUseed; - CUMAT.binornd(out.nrows, out.ncols, p.pdata, atype, n.pdata, ctype, out.pdata, GPUSEED, OFFSET); - out; - } - - def applyGfun(in:GMat, omat:Mat, opn:Int, kflops:Long):GMat = { - val out = GMat.newOrCheckGMat(in.dims, omat, in.GUID, opn) - CUMAT.applygfun(in.pdata, out.pdata, in.nrows*in.ncols, opn) - Mat.nflops += kflops*in.length - out - } - - def applyGfun(in:GMat, opn:Int, kflops:Long):GMat = { - val out = GMat.newOrCheckGMat(in.dims, null, in.GUID, opn) - CUMAT.applygfun(in.pdata, out.pdata, in.nrows*in.ncols, opn) - Mat.nflops += kflops*in.length - out - } - - def applyGfun2(a:GMat, b:GMat, omat:Mat, opn:Int, kflops:Long):GMat = { - if (samedims(a.dims, b.dims)) { - val out = GMat.newOrCheckGMat(a.dims, omat, a.GUID, b.GUID, opn) - CUMAT.applygfun2(a.pdata, b.pdata, out.pdata, a.nrows*a.ncols, opn) - Mat.nflops += kflops*a.length - out - } else { - throw new RuntimeException("Dimensions mismatch") - } - } - - def applyGfun2(a:GMat, b:GMat, opn:Int, kflops:Long):GMat = { - if (samedims(a.dims, b.dims)) { - val out = GMat.newOrCheckGMat(a.dims, null, a.GUID, b.GUID, opn) - CUMAT.applygfun2(a.pdata, b.pdata, out.pdata, a.nrows*a.ncols, opn) - Mat.nflops += kflops*a.length - out - } else { - throw new RuntimeException("Dimensions mismatch") - } - } - - - def applySlatecGFun(a:GMat, omat:Mat, nfn:Int, nflops:Long) = { - val out = GMat.newOrCheckGMat(a.dims, omat, a.GUID, nfn) - SLATEC.applygfun(a.pdata, out.pdata, a.length, nfn); - Mat.nflops += nflops*a.length - out - } - - def applySlatecGFun2(a:GMat, b:GMat, omat:Mat, nfn:Int, nflops:Long) = { - val nr = math.max(a.nrows, b.nrows); - val nc = math.max(a.ncols, b.ncols); - val out = GMat.newOrCheckGMat(nr, nc, omat, a.GUID, b.GUID, nfn); - val arowi = if (a.nrows == nr) 1 else 0; - val browi = if (b.nrows == nr) 1 else 0; - val acoli = if (a.ncols == nc) a.nrows else 0; - val bcoli = if (b.ncols == nc) b.nrows else 0; - SLATEC.applygfun2(nr, nc, a.pdata, arowi, acoli, b.pdata, browi, bcoli, out.pdata, nr, nfn); - Mat.nflops += nflops*out.length - out - } - - - def abs(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.abs, 1L) - def exp(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.exp, 10L) - def expm1(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.expm1, 10L) - def sqrt(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.sqrt, 10L) - def ln(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.ln, 10L) - def log10(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.log10, 10L) - def log1p(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.log1p, 10L) - def cos(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.cos, 10L) - def sin(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.sin, 10L) - def tan(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.tan, 10L) - def cosh(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.cosh, 10L) - def sinh(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.sinh, 10L) - def tanh(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.tanh, 10L) - def acos(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.acos, 10L) - def asin(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.asin, 10L) - def atan(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.atan, 10L) - def acosh(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.acosh, 10L) - def asinh(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.asinh, 10L) - def atanh(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.atanh, 10L) - def erf(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.erf, 10L) - def erfinv(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.erfinv, 10L) - def erfc(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.erfc, 10L) - def erfcinv(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.erfcinv, 10L) - def gammaln(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.gammaln, 10L) - def gamma(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.gamma, 10L) - def Γ(a:GMat, out:Mat) = gamma(a, out); - def Γ(a:GMat) = gamma(a); - def ceil(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.ceil, 10L) - def floor(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.floor, 10L) - def round(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.round, 10L) - def trunc(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.trunc, 10L) - def sign(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.sign, 1L) - def exppsi(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.exppsi, 10L) - def normcdf(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.normcdf, 10L) - def normcdfinv(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.normcdfinv, 10L) - def logistic(in:GMat, out:Mat):GMat = applyGfun(in, out, TransF.logistic, 10L) - - def psi(a:GMat, out:Mat):GMat = applySlatecGFun(a, out, 0, 100); - def psi(a:GMat):GMat = psi(a, null); - - def psiinv(a:GMat, out:Mat):GMat = applySlatecGFun(a, out, 1, 400); - def psiinv(a:GMat):GMat = psiinv(a, null); - - def psifn(a:GMat, b:GMat, out:Mat):GMat = applySlatecGFun2(a, b, out, 0, 200); - def psifn(a:GMat, b:GMat):GMat = psifn(a, b, null); - - - def atan2(a:GMat, b:GMat, out:Mat):GMat = applyGfun2(a, b, out, TransF2.atan2, 10L) - def pow(a:GMat, b:GMat, out:Mat):GMat = applyGfun2(a, b, out, TransF2.pow, 10L) - - def abs(in:GMat):GMat = applyGfun(in, TransF.abs, 10L) - def exp(in:GMat):GMat = applyGfun(in, TransF.exp, 10L) - def expm1(in:GMat):GMat = applyGfun(in, TransF.expm1, 10L) - def sqrt(in:GMat):GMat = applyGfun(in, TransF.sqrt, 10L) - def ln(in:GMat):GMat = applyGfun(in, TransF.ln, 10L) - def log10(in:GMat):GMat = applyGfun(in, TransF.log10, 10L) - def log1p(in:GMat):GMat = applyGfun(in, TransF.log1p, 10L) - def cos(in:GMat):GMat = applyGfun(in, TransF.cos, 10L) - def sin(in:GMat):GMat = applyGfun(in, TransF.sin, 10L) - def tan(in:GMat):GMat = applyGfun(in, TransF.tan, 10L) - def cosh(in:GMat):GMat = applyGfun(in, TransF.cosh, 10L) - def sinh(in:GMat):GMat = applyGfun(in, TransF.sinh, 10L) - def tanh(in:GMat):GMat = applyGfun(in, TransF.tanh, 10L) - def acos(in:GMat):GMat = applyGfun(in, TransF.acos, 10L) - def asin(in:GMat):GMat = applyGfun(in, TransF.asin, 10L) - def atan(in:GMat):GMat = applyGfun(in, TransF.atan, 10L) - def acosh(in:GMat):GMat = applyGfun(in, TransF.acosh, 10L) - def asinh(in:GMat):GMat = applyGfun(in, TransF.asinh, 10L) - def atanh(in:GMat):GMat = applyGfun(in, TransF.atanh, 10L) - def erf(in:GMat):GMat = applyGfun(in, TransF.erf, 10L) - def erfinv(in:GMat):GMat = applyGfun(in, TransF.erfinv, 10L) - def erfc(in:GMat):GMat = applyGfun(in, TransF.erfc, 10L) - def ercinv(in:GMat):GMat = applyGfun(in, TransF.erfcinv, 10L) - def gammaln(in:GMat):GMat = applyGfun(in, TransF.gammaln, 10L) - def gamma(in:GMat):GMat = applyGfun(in, TransF.gamma, 10L) - def ceil(in:GMat):GMat = applyGfun(in, TransF.ceil, 10L) - def floor(in:GMat):GMat = applyGfun(in, TransF.floor, 10L) - def round(in:GMat):GMat = applyGfun(in, TransF.round, 10L) - def trunc(in:GMat):GMat = applyGfun(in, TransF.trunc, 10L) - def sign(in:GMat):GMat = applyGfun(in, TransF.sign, 1L) - def exppsi(in:GMat):GMat = applyGfun(in, TransF.exppsi, 10L) - def normcdf(in:GMat):GMat = applyGfun(in, TransF.normcdf, 10L) - def normcdfinv(in:GMat):GMat = applyGfun(in, TransF.normcdfinv, 10L) - def logistic(in:GMat):GMat = applyGfun(in, TransF.logistic, 10L) - - def atan2(a:GMat, b:GMat):GMat = applyGfun2(a, b, TransF2.atan2, 10L) - def pow(a:GMat, b:GMat):GMat = applyGfun2(a, b, TransF2.pow, 10L) - - - def norm(a:GMat) = math.sqrt(jcuda.jcublas.JCublas.cublasSdot(a.length, a.pdata, 1, a.pdata, 1)) - - def accumIJ(I:GIMat, J:GIMat, V:GMat, omat:Mat, nrows:Int, ncols:Int):GMat = { - val out = GMat.newOrCheckGMat(nrows, ncols, omat, I.GUID, J.GUID, V.GUID, "GMat_accum".##) - out.clear - if (I.length != J.length || I.length != V.length) { - throw new RuntimeException("GMat accum: index lengths dont match") - } - CUMAT.accum(I.pdata, J.pdata, V.pdata, out.pdata, I.length, nrows) - Mat.nflops += I.length - out - } - - def accumIJ(I:Int, J:GIMat, V:GMat, omat:Mat, nrows:Int, ncols:Int):GMat = { - val out = GMat.newOrCheckGMat(nrows, ncols, omat, I, J.GUID, V.GUID, "GMat_accumI".##) - out.clear - if (J.length != V.length) { - throw new RuntimeException("GMat accum: index lengths dont match") - } - CUMAT.accumI(I, J.pdata, V.pdata, out.pdata, J.length, nrows) - Mat.nflops += J.length - out - } - - def accumIJ(I:GIMat, J:Int, V:GMat, omat:Mat, nrows:Int, ncols:Int):GMat = { - val out = GMat.newOrCheckGMat(nrows, ncols, omat, I.GUID, J, V.GUID, "GMat_accumJ".##) - out.clear - if (I.length != V.length) { - throw new RuntimeException("GMat accum: index lengths dont match") - } - CUMAT.accumJ(I.pdata, J, V.pdata, out.pdata, I.length, nrows) - Mat.nflops += I.length - out - } - - def accumIJ(I:GIMat, J:GIMat, V:Float, omat:Mat, nrows:Int, ncols:Int):GMat = { - val out = GMat.newOrCheckGMat(nrows, ncols, omat, I.GUID, J.GUID, V.hashCode, "GMat_accumV".##) - out.clear - if (I.length != J.length) { - throw new RuntimeException("GMat accum: index lengths dont match") - } - CUMAT.accumV(I.pdata, J.pdata, V, out.pdata, I.length, nrows) - Mat.nflops += I.length - out - } - - def accumIJ(I:Int, J:GIMat, V:Float, omat:Mat, nrows:Int, ncols:Int):GMat = { - val out = GMat.newOrCheckGMat(nrows, ncols, omat, I, J.GUID, V.hashCode, "GMat_accumIV".##) - out.clear - CUMAT.accumIV(I, J.pdata, V, out.pdata, J.length, nrows) - Mat.nflops += J.length - out - } - - def accumIJ(I:GIMat, J:Int, V:Float, omat:Mat, nrows:Int, ncols:Int):GMat = { - val out = GMat.newOrCheckGMat(nrows, ncols, omat, I.GUID, J, V.hashCode, "GMat_accumJV".##) - out.clear - CUMAT.accumJV(I.pdata, J, V, out.pdata, I.length, nrows) - Mat.nflops += I.length - out - } - - def accum(IJ:GIMat, V:GMat, omat:Mat, nrows:Int, ncols:Int):GMat = { - if (IJ.nrows != V.length || IJ.ncols > 2) { - throw new RuntimeException("GMat accum: index lengths dont match") - } - val out = GMat.newOrCheckGMat(nrows, ncols, omat, IJ.GUID, V.GUID, "GMat_accumIJ".##) - out.clear - if (IJ.ncols == 2) { - CUMAT.accum(IJ.pdata, IJ.pdata.withByteOffset(1L*IJ.nrows*Sizeof.INT), V.pdata, out.pdata, V.length, nrows) - } else { - CUMAT.accumJ(IJ.pdata, 0, V.pdata, out.pdata, V.length, nrows) - } - Mat.nflops += V.length - out - } - - def accum(IJ:GIMat, V:Float, omat:Mat, nrows:Int, ncols:Int):GMat = { - if (IJ.ncols > 2) { - throw new RuntimeException("GMat accum: index lengths dont match") - } - val out = GMat.newOrCheckGMat(nrows, ncols, omat, IJ.GUID, V.hashCode, "GMat_accumIJV".##) - out.clear - if (IJ.ncols == 2) { - CUMAT.accumV(IJ.pdata, IJ.pdata.withByteOffset(1L*IJ.nrows*Sizeof.INT), V, out.pdata, IJ.nrows, nrows) - } else { - CUMAT.accumJV(IJ.pdata, 0, V, out.pdata, IJ.nrows, nrows) - } - Mat.nflops += IJ.nrows - out - } - - def cumsumg(a:GMat, jc:GIMat, omat:Mat):GMat = { - Mat.nflops += 1L * a.length - val out = GMat.newOrCheckGMat(a.nrows, a.ncols, omat, a.GUID, jc.GUID, "cumsumi".##) - val err = CUMAT.cumsumgf(a.pdata, out.pdata, jc.pdata, a.nrows, a.ncols, jc.length-1) - if (err != 0) throw new RuntimeException("cumsumi error %d: " + cudaGetErrorString(err) format err); - out - } - - def maxg(a:GMat, jc:GIMat, omat:Mat, omati:Mat):(GMat, GIMat) = { - Mat.nflops += 1L * a.length - val out = GMat.newOrCheckGMat(jc.length-1, a.ncols, omat, a.GUID, jc.GUID, "maxg".##) - val outi = GIMat.newOrCheckGIMat(jc.length-1, a.ncols, omati, a.GUID, jc.GUID, "maxg_1".##) - val err = CUMAT.maxgf(a.pdata, out.pdata, outi.pdata, jc.pdata, a.nrows, a.ncols, jc.length-1) - if (err != 0) throw new RuntimeException("maxg error %d: " + cudaGetErrorString(err) format err); - (out, outi) - } - - def ming(a:GMat, jc:GIMat, omat:Mat, omati:Mat):(GMat, GIMat) = { - Mat.nflops += 1L * a.length - val out = GMat.newOrCheckGMat(jc.length-1, a.ncols, omat, a.GUID, jc.GUID, "ming".##) - val outi = GIMat.newOrCheckGIMat(jc.length-1, a.ncols, omati, a.GUID, jc.GUID, "ming_1".##) - val err = CUMAT.mingf(a.pdata, out.pdata, outi.pdata, jc.pdata, a.nrows, a.ncols, jc.length-1) - if (err != 0) throw new RuntimeException("ming error %d: " + cudaGetErrorString(err) format err); - (out, outi) - } - - def maxi2(a:GMat, omat:Mat, omati:Mat, dim0:Int):(GMat, GIMat) = { - Mat.nflops += 1L * a.length - val dim = if (a.nrows == 1 && dim0 == 0) 2 else math.max(1, dim0) - if (dim == 1) { - val out = GMat.newOrCheckGMat(1, a.ncols, omat, a.GUID, "maxi2".##) - val outi = GIMat.newOrCheckGIMat(1, a.ncols, omati, a.GUID, "maxi2_1".##) - val err = CUMAT.maxif(a.pdata, out.pdata, outi.pdata, a.nrows, a.ncols, 1) - if (err != 0) throw new RuntimeException("maxi2 error %d: " + cudaGetErrorString(err) format err); - (out, outi) - } else if (dim == 2) { - val out = GMat.newOrCheckGMat(a.nrows, 1, omat, a.GUID, "maxi2".##) - val outi = GIMat.newOrCheckGIMat(a.nrows, 1, omati, a.GUID, "maxi2_1".##) - val err = CUMAT.maxif(a.pdata, out.pdata, outi.pdata, a.nrows, a.ncols, 2) - if (err != 0) throw new RuntimeException("maxi2 error %d: " + cudaGetErrorString(err) format err); - (out, outi) - } else { - throw new RuntimeException("maxi2 directions not recognized %d" format dim0) - } - } - - def mini2(a:GMat, omat:Mat, omati:Mat, dim0:Int):(GMat, GIMat) = { - Mat.nflops += 1L * a.length - val dim = if (a.nrows == 1 && dim0 == 0) 2 else math.max(1, dim0) - if (dim == 1) { - val out = GMat.newOrCheckGMat(1, a.ncols, omat, a.GUID, "mini2".##) - val outi = GIMat.newOrCheckGIMat(1, a.ncols, omati, a.GUID, "mini2_1".##) - val err = CUMAT.minif(a.pdata, out.pdata, outi.pdata, a.nrows, a.ncols, dim) - if (err != 0) throw new RuntimeException("mini2 error %d: " + cudaGetErrorString(err) format err); - (out, outi) - } else if (dim == 2) { - val out = GMat.newOrCheckGMat(a.nrows, 1, omat, a.GUID, "mini2".##) - val outi = GIMat.newOrCheckGIMat(a.nrows, 1, omati, a.GUID, "mini2_1".##) - val err = CUMAT.minif(a.pdata, out.pdata, outi.pdata, a.nrows, a.ncols, dim) - if (err != 0) throw new RuntimeException("mini2 error %d: " + cudaGetErrorString(err) format err); - (out, outi) - } else { - throw new RuntimeException("mini2 directions not recognized %d" format dim) - } - } - - - def cumsum(a:GMat, dim0:Int, omat:Mat):GMat = { - Mat.nflops += 1L * a.length; - val dim = if (a.nrows == 1 && dim0 == 0) 2 else math.max(1, dim0); - if (dim == 1) { - val out = GMat.newOrCheckGMat(a.nrows, a.ncols, omat, a.GUID, "cumsum".##) - CUMAT.cumsumc(a.nrows, a.ncols, a.pdata, out.pdata) - out - } else { - throw new RuntimeException("Cumsum across rows not supported yet") - } - } - - - def lexsort2i(a:GIMat, b:GMat, i:GIMat) { - val ab = embedmat(a,b) - val err = CUMAT.lsortk(ab.pdata, i.pdata, i.length, 1); - if (err != 0) throw new RuntimeException("lexsort2i error %d: " + cudaGetErrorString(err) format err); - extractmat(a, b, ab); - } - - def embedmat(a:GIMat, b:GMat, oMat: Mat):GIMat = { - if (a.nrows != b.nrows || a.ncols != b.ncols) { - throw new RuntimeException("embedmat error: mismatched dimensions"); - } - val out = GIMat.newOrCheckGIMat(a.nrows * 2, a.ncols, oMat, a.GUID, b.GUID, "embedmat".##) - val err = CUMAT.embedmat(b.pdata, a.pdata, out.pdata, a.length); - if (err != 0) throw new RuntimeException("embedmat error %d: " + cudaGetErrorString(err) format err); - out - } - - def embedmat(a:GIMat, b: GMat):GIMat = embedmat(a, b, null); - - def extractmat(a:Mat, b: Mat, c: GIMat):(GIMat, GMat) = { - val outA = GIMat.newOrCheckGIMat(c.nrows /2, c.ncols, a, c.GUID, "extractmat_A".##) - val outB = GMat.newOrCheckGMat(c.nrows /2, c.ncols, b, c.GUID, "extractmat_B".##) - val err = CUMAT.extractmat(outB.pdata, outA.pdata, c.pdata, outA.length); - if (err != 0) throw new RuntimeException("extractmat error %d: " + cudaGetErrorString(err) format err); - (outA, outB) - } - - def extractmat(c: GIMat):(GIMat, GMat) = extractmat(null, null, c); - - - // sort some indices on the GPU. Output to the input arrays. Also moves the contents of a secondary array. - // This can be used to build SMats from row, column, value arrays. - def sortInds(ii:IMat, jj:IMat, vals:Mat, asc:Int):Unit = { - val inds = ii \ jj; - val ginds = GIMat(inds.nrows, inds.ncols); - ginds <-- inds; - val gindst = ginds.t; - val (gvals, gpdata) = vals match { - case ivals:IMat => {val gd = GIMat(ivals); (gd, gd.pdata) } - case fvals:FMat => {val gd = GMat(fvals); (gd, gd.pdata) } - } - CUMAT.lsortk(gindst.pdata, gpdata, ginds.length/2, asc); - (ginds ~ gindst).t; - inds <-- ginds - vals <-- gvals - ii <-- inds(MatFunctions.?,0) - jj <-- inds(MatFunctions.?,1) - gvals.free - gindst.free - ginds.free - } - - def sortInds(ii:IMat, jj:IMat, vals:Mat):Unit = sortInds(ii, jj, vals, 1); - - def GPUsort_old(keys:FMat, vals:IMat):Unit = { - if (keys.nrows != vals.nrows || keys.ncols != vals.ncols) - throw new RuntimeException("Dimensions mismatch in GPUsort ("+keys.nrows+","+keys.ncols+") ("+vals.nrows+","+vals.ncols+")") - - val nthreads = math.min(8,math.max(0, Mat.hasCUDA)) - val maxsize = keys.nrows * math.min(32*1024*1024/keys.nrows, math.max(1, keys.ncols/nthreads)) - val nsize = keys.nrows * keys.ncols - val tall = (keys.nrows > 32*1024) - val done = IMat(nthreads,1) - - for (ithread <- 0 until nthreads) { - Future { - setGPU(ithread) - val aa = GMat(maxsize, 1).pdata - val vv = GIMat(maxsize, 1).pdata - val kk = if (!tall) GMat(maxsize, 2).pdata else null - - var ioff = ithread * maxsize - while (ioff < nsize) { - val todo = math.min(maxsize, nsize - ioff) - val colstodo = todo / keys.nrows - cudaMemcpy(aa, Pointer.to(keys.data).withByteOffset(1L*ioff*Sizeof.FLOAT), 1L*todo*Sizeof.FLOAT, cudaMemcpyKind.cudaMemcpyHostToDevice) - cudaMemcpy(vv, Pointer.to(vals.data).withByteOffset(1L*ioff*Sizeof.INT), 1L*todo*Sizeof.INT, cudaMemcpyKind.cudaMemcpyHostToDevice) - cudaStreamSynchronize(Mat.SyncMethod); - var err = cudaGetLastError; - if (err != 0) throw new RuntimeException("GMat GPUsort_old() error " + cudaGetErrorString(err)); - if (tall) { - CUMAT.fsort2dk(aa, vv, keys.nrows, colstodo, 0) - } else { - CUMAT.embedmat2d(aa, kk, keys.nrows, colstodo, 0) - CUMAT.lsortk(kk, vv, todo, 0) - CUMAT.extractmat2d(aa, kk, keys.nrows, colstodo) - } - cudaMemcpy(Pointer.to(keys.data).withByteOffset(1L*ioff*Sizeof.FLOAT), aa, 1L*todo*Sizeof.FLOAT, cudaMemcpyKind.cudaMemcpyDeviceToHost); - cudaMemcpy(Pointer.to(vals.data).withByteOffset(1L*ioff*Sizeof.INT), vv, 1L*todo*Sizeof.INT, cudaMemcpyKind.cudaMemcpyDeviceToHost); - err = cudaGetLastError; - if (err != 0) throw new RuntimeException("GMat GPUsort_old() error " + cudaGetErrorString(err)); - ioff += nthreads * maxsize - } - if (!tall) cudaFree(kk) - cudaFree(vv) - cudaFree(aa) - done(ithread,0) = 1 -// println("done %d" format ithread) - } - } - while (SciFunctions.mini(done).v == 0) Thread.`yield` - Mat.nflops += keys.length - } - - def sort2(keys:GMat):(GMat,GIMat) = { - val nkeys = GMat.newOrCheckGMat(keys.nrows, keys.ncols, null, keys.GUID, "GMat.sort2".##) - val nvals = GIMat.newOrCheckGIMat(keys.nrows, keys.ncols, null, keys.GUID, "GMat.sort2i".##) - CUMAT.initSeq(nvals.pdata, keys.nrows, keys.ncols, 1) - nkeys <-- keys - sortGPU(nkeys, nvals) - (nkeys, nvals) - } - - def sortdown2(keys:GMat):(GMat,GIMat) = { - val nkeys = GMat.newOrCheckGMat(keys.nrows, keys.ncols, null, keys.GUID, "GMat.sortdown2".##) - val nvals = GIMat.newOrCheckGIMat(keys.nrows, keys.ncols, null, keys.GUID, "GMat.sortdown2i".##) - CUMAT.initSeq(nvals.pdata, keys.nrows, keys.ncols, 1) - nkeys <-- keys - sortdownGPU(nkeys, nvals) - (nkeys, nvals) - } - - def sort(keys:GMat):(GMat) = { - val nkeys = GMat.newOrCheckGMat(keys.nrows, keys.ncols, null, keys.GUID, "GMat.sort".##) - nkeys <-- keys - sortGPU(nkeys) - (nkeys) - } - - def sortdown(keys:GMat):(GMat) = { - val nkeys = GMat.newOrCheckGMat(keys.nrows, keys.ncols, null, keys.GUID, "GMat.sortdown".##) - nkeys <-- keys - sortdownGPU(nkeys) - nkeys - } - - def sortGPU(keys:GMat, vals:GIMat):Unit = _sortGPU(keys, vals, true) - - def sortdownGPU(keys:GMat, vals:GIMat):Unit = _sortGPU(keys, vals, false) - - def sortGPU(keys:GMat):Unit = _sortGPU(keys, true) - - def sortdownGPU(keys:GMat):Unit = _sortGPU(keys, false) - - def _sortGPU(keys:GMat, vals:GIMat, asc:Boolean):Unit = { - if (keys.nrows != vals.nrows || keys.ncols != vals.ncols) - throw new RuntimeException("Dimensions mismatch in GPUsort") - if (keys.ncols == 1) { - val tkeys = GMat.newOrCheckGMat(keys.nrows, 1, null, keys.GUID, vals.GUID, "_sortGPU1".##); - val tvals = GIMat.newOrCheckGIMat(vals.nrows, 1, null, keys.GUID, vals.GUID, "_sortGPU2".##); - val ntemp = CUMAT.fisortcubsize(keys.pdata, tkeys.pdata, vals.pdata, tvals.pdata, keys.nrows, if (asc) 1 else 0); - val temp = GIMat.newOrCheckGIMat((1+(ntemp - 1)/4).toInt, 1, null, keys.GUID, vals.GUID, "_sortGPU3".##); - val err = CUMAT.fisortcub(keys.pdata, tkeys.pdata, vals.pdata, tvals.pdata, temp.pdata, ntemp, keys.nrows, if (asc) 1 else 0); - if (err != 0) - throw new RuntimeException("CUDA error in _sortGPU " + cudaGetErrorString(err)); - keys <-- tkeys; - vals <-- tvals; - } else if (keys.nrows > 128*1024) { - // val t1 = MatFunctions.toc; - CUMAT.fsort2dk(keys.pdata, vals.pdata, keys.nrows, keys.ncols, if (asc) 1 else 0); -// val t2 = MatFunctions.toc; -// println("GPU %d sort took %f s" format (SciFunctions.getGPU, t2 -t1)); - } else { - val maxsize = keys.nrows * math.min(16*1024*1024/keys.nrows, keys.ncols) - val nsize = keys.nrows*keys.ncols - val kk = GMat(maxsize, 2).pdata - var ioff = 0 - while (ioff < nsize) { - val todo = math.min(maxsize, nsize - ioff) - val colstodo = todo / keys.nrows - CUMAT.embedmat2d(keys.pdata.withByteOffset(1L*ioff*Sizeof.FLOAT), kk, keys.nrows, colstodo, if (asc) 0 else 1) - CUMAT.lsortk(kk, vals.pdata.withByteOffset(1L*ioff*Sizeof.INT), todo, if (asc) 1 else 0) - CUMAT.extractmat2d(keys.pdata.withByteOffset(1L*ioff*Sizeof.FLOAT), kk, keys.nrows, colstodo) - ioff += maxsize - } - cudaFree(kk) - } - Mat.nflops += keys.length - } - - def _sortGPU(keys:GMat, asc:Boolean):Unit = { - if (keys.nrows > 128*1024) { - CUMAT.fsort2d(keys.pdata, keys.nrows, keys.ncols, if (asc) 1 else 0) - } else { - val maxsize = keys.nrows * math.min(16*1024*1024/keys.nrows, keys.ncols) - val nsize = keys.nrows*keys.ncols - val kk = GMat(maxsize, 2).pdata - var ioff = 0 - while (ioff < nsize) { - val todo = math.min(maxsize, nsize - ioff) - val colstodo = todo / keys.nrows - CUMAT.embedmat2d(keys.pdata.withByteOffset(1L*ioff*Sizeof.FLOAT), kk, keys.nrows, colstodo, if (asc) 0 else 1) - CUMAT.lsort(kk, todo, if (asc) 1 else 0) - CUMAT.extractmat2d(keys.pdata.withByteOffset(1L*ioff*Sizeof.FLOAT), kk, keys.nrows, colstodo) - ioff += maxsize - } - cudaFree(kk) - } - Mat.nflops += keys.length - } - - def sortxGPU(keys:GMat, vals:GIMat):Unit = _sortxGPU(keys, vals, true) - - def sortdownxGPU(keys:GMat, vals:GIMat):Unit = _sortxGPU(keys, vals, false) - - def _sortxGPU(keys:GMat, vals:GIMat, asc:Boolean):Unit = { - if (keys.nrows != vals.nrows || keys.ncols != vals.ncols) - throw new RuntimeException("Dimensions mismatch in sortxGPU") - val tkeys = GMat(keys.nrows, 1) - val tvals = GIMat(keys.nrows, 1) - - CUMAT.fsort2dx(keys.pdata, vals.pdata, tkeys.pdata, tvals.pdata, keys.nrows, keys.ncols, if (asc) 1 else 0) - - tvals.free - tkeys.free - Mat.nflops += keys.length - } - - def sortGPU(keys:FMat, vals:IMat):Unit = _sortGPU(keys, vals, false) - - def sortdownGPU(keys:FMat, vals:IMat):Unit = _sortGPU(keys, vals, true) - - def _sortGPU(keys:FMat, vals:IMat, asc:Boolean):Unit = { - if (keys.nrows != vals.nrows || keys.ncols != vals.ncols) - throw new RuntimeException("Dimensions mismatch in sortGPU ("+keys.nrows+","+keys.ncols+") ("+vals.nrows+","+vals.ncols+")") - val iasc = if (asc) 1 else 0 - val nthreads = math.min(8,math.max(0, Mat.hasCUDA)) - val maxsize = keys.nrows * math.min(32*1024*1024/keys.nrows, math.max(1, keys.ncols/nthreads)) - val nsize = keys.nrows * keys.ncols - val tall = (keys.nrows > 32*1024) - val done = IMat(nthreads,1) - var err = 0 - var myturn = 0 - for (ithread <- 0 until nthreads) { - Future { - setGPU(ithread) - val aa = GMat(maxsize, 1) - val vv = GIMat(maxsize, 1) - val kk = if (!tall) GMat(maxsize, 2) else null - val tkeys = GMat(maxsize, 2) - val tvals = GIMat(maxsize, 1) - - var ioff = ithread * maxsize - while (ioff < nsize) { - val todo = math.min(maxsize, nsize - ioff) - val colstodo = todo / keys.nrows - err = cudaMemcpy(aa.pdata, Pointer.to(keys.data).withByteOffset(1L*ioff*Sizeof.FLOAT), 1L*todo*Sizeof.FLOAT, cudaMemcpyKind.cudaMemcpyHostToDevice) - if (err != 0) throw new RuntimeException("sortGPU copy a in failed thread %d error %d" format (ithread,err)) - cudaMemcpy(vv.pdata, Pointer.to(vals.data).withByteOffset(1L*ioff*Sizeof.INT), 1L*todo*Sizeof.INT, cudaMemcpyKind.cudaMemcpyHostToDevice) - if (err != 0) throw new RuntimeException("sortGPU copy v in failed thread %d error %d" format (ithread,err)) - cudaStreamSynchronize(Mat.SyncMethod) - if (tall) { - err = CUMAT.fsort2dx(aa.pdata, vv.pdata, tkeys.pdata, tvals.pdata, keys.nrows, colstodo, iasc) - if (err != 0) throw new RuntimeException("sortGPU tall sort failed thread %d error %d" format (ithread,err)) - } else { - err = CUMAT.embedmat2d(aa.pdata, kk.pdata, keys.nrows, colstodo, if (asc) 0 else 1) - if (err != 0) throw new RuntimeException("sortGPU embed failed thread %d error %d" format (ithread,err)) - err = CUMAT.lsortk(kk.pdata, vv.pdata, todo, iasc) - if (err != 0) throw new RuntimeException("sortGPU sort kernel failed thread %d error %d" format (ithread,err)) - err = CUMAT.extractmat2d(aa.pdata, kk.pdata, keys.nrows, colstodo) - if (err != 0) throw new RuntimeException("sortGPU extract failed thread %d error %d" format (ithread,err)) - } - cudaMemcpy(Pointer.to(keys.data).withByteOffset(1L*ioff*Sizeof.FLOAT), aa.pdata, 1L*todo*Sizeof.FLOAT, cudaMemcpyKind.cudaMemcpyDeviceToHost) - if (err != 0) throw new RuntimeException("sortGPU copy a out failed thread %d error %d" format (ithread,err)) - cudaMemcpy(Pointer.to(vals.data).withByteOffset(1L*ioff*Sizeof.INT), vv.pdata, 1L*todo*Sizeof.INT, cudaMemcpyKind.cudaMemcpyDeviceToHost) - if (err != 0) throw new RuntimeException("sortGPU copy v out failed thread %d error %d" format (ithread,err)) - ioff += nthreads * maxsize - } - tvals.free - tkeys.free - if (!tall) kk.free - vv.free - aa.free - done(ithread,0) = 1 - } - } - while (SciFunctions.mini(done).v == 0) Thread.`yield` - Mat.nflops += keys.length - } - - - def getHandle = { - val igpu = Array(0); - jcuda.runtime.JCuda.cudaGetDevice(igpu) - GFunctions.cublasHandles(igpu(0)).asInstanceOf[cublasHandle]; - } - - - def GPUmult(a:FMat, b:FMat, omat:Mat, btrans:Boolean):FMat = { - val bnrows = if (btrans) b.ncols else b.nrows - val bncols = if (btrans) b.nrows else b.ncols - if (a.ncols != bnrows) { - throw new RuntimeException("dimensions mismatch in xG") - } else { - val maxrows = 8192 - val maxcols = 8192 - val c = FMat.newOrCheckFMat(a.nrows, bncols, omat, a.GUID, b.GUID, "GPUmult".##) - val rblkk = if (Mat.hasCUDA > 1) 2 else 1 - val cblkk = if (Mat.hasCUDA > 3) 2 else 1 - val rblk = rblkk*(math.max(1, math.ceil(c.nrows/maxrows/rblkk).toInt)) - val cblk = cblkk*(math.max(1, math.ceil(c.ncols/maxcols/cblkk).toInt)) - val kblk = math.max(1, math.ceil(a.ncols/maxcols).toInt) - val gcrows = 32*(c.nrows/rblk/32) - val gccols = 32*(c.ncols/cblk/32) - val garows = gcrows - val gacols = 32*(a.ncols/kblk/32) - val gbrows = if (btrans) gccols else gacols - val gbcols = if (btrans) gacols else gccols - - val done = IMat(rblkk*cblkk,1) - for (ix <- 0 until rblkk) { - for (iy <- 0 until cblkk) { - Future { - GFunctions.setGPU(ix+iy*2) - val aa = new Pointer - val bb = new Pointer - val cc = new Pointer - var err = cudaMalloc(aa, 1L*garows*gacols*Sizeof.FLOAT); - if (err != 0) throw new RuntimeException("CUDA alloc failed " + cudaGetErrorString(err)) - err = cudaMalloc(bb, 1L*gbrows*gbcols*Sizeof.FLOAT); - if (err != 0) throw new RuntimeException("CUDA alloc failed " + cudaGetErrorString(err)) - err = cudaMalloc(cc, 1L*gcrows*gccols*Sizeof.FLOAT); - if (err != 0) throw new RuntimeException("CUDA alloc failed "+err) - - var i = ix*gcrows; while (i < c.nrows) { - val ni = math.min(gcrows, c.nrows - i) - var j = iy*gccols; while (j < c.ncols) { - val nj = math.min(gccols, c.ncols - j) - var k = 0; while (k < a.ncols) { - val nk = math.min(gacols, a.ncols - k) - err = cudaMemcpy2D(aa, garows*Sizeof.FLOAT, Pointer.to(a.data).withByteOffset(1L*(i+k*a.nrows)*Sizeof.FLOAT), - a.nrows*Sizeof.FLOAT, ni*Sizeof.FLOAT, nk, cudaMemcpyHostToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - if (err != 0) throw new RuntimeException("CUDA copy a failed "+err) - if (btrans) { - err = cudaMemcpy2D(bb, gbrows*Sizeof.FLOAT, Pointer.to(b.data).withByteOffset(1L*(j+k*b.nrows)*Sizeof.FLOAT), - b.nrows*Sizeof.FLOAT, nj*Sizeof.FLOAT, nk, cudaMemcpyHostToDevice) - } else { - err = cudaMemcpy2D(bb, gbrows*Sizeof.FLOAT, Pointer.to(b.data).withByteOffset(1L*(k+j*b.nrows)*Sizeof.FLOAT), - b.nrows*Sizeof.FLOAT, nk*Sizeof.FLOAT, nj, cudaMemcpyHostToDevice) - } - cudaStreamSynchronize(Mat.SyncMethod) - if (err != 0) throw new RuntimeException("CUDA copy b failed "+err) - - cublasSgemm(getHandle, cublasOperation.CUBLAS_OP_N, if (btrans) cublasOperation.CUBLAS_OP_T else cublasOperation.CUBLAS_OP_N, ni, nj, nk, - GMat.pONE, aa, garows, bb, gbrows, if (k==0) GMat.pZERO else GMat.pONE, cc, gcrows) - - cudaStreamSynchronize(Mat.SyncMethod) - err = cudaGetLastError - if (err != 0) throw new RuntimeException("Cublas error in xG, sgemm "+err) - k += gacols - } - err = cudaMemcpy2D(Pointer.to(c.data).withByteOffset(1L*(i+j*c.nrows)*Sizeof.FLOAT), c.nrows*Sizeof.FLOAT, cc, gcrows*Sizeof.FLOAT, ni*Sizeof.FLOAT, nj, cudaMemcpyDeviceToHost) - cudaStreamSynchronize(Mat.SyncMethod) - if (err != 0) throw new RuntimeException("CUDA copy c failed "+err) - j += cblkk*gccols - } - i += rblkk*gcrows - } - - cudaFree(cc) - cudaFree(bb) - cudaFree(aa) - done(ix+2*iy,0) = 1 - } - } - } - while (SciFunctions.mini(done).v == 0) {Thread.`yield`} - - Mat.nflops += 2L * a.nrows * a.ncols * bncols - c - } - } - - def LXdist(a:GMat, b:GMat, omat:GMat, p:Float):GMat = { - if (a.ncols != b.ncols) { - throw new RuntimeException("LXdist number of columns = number of features must match") - } - val c = GMat.newOrCheckGMat(a.nrows, b.nrows, omat, a.GUID, b.GUID, "LXdist".##) - c.clear - Mat.nflops += 3L * c.nrows * c.ncols * a.ncols - var err = CUMAT.distances(a.pdata, a.nrows, b.pdata, b.nrows, c.pdata, c.nrows, a.ncols, c.nrows, c.ncols, p) - if (err != 0) throw new RuntimeException("LXdist kernel error "+err) - val easyp = (p == 0f || p == 1f || p == 2f) - if (!easyp) { - val pinv = GMat(1/p) - err = CUMAT.applyop(c.pdata, c.nrows, c.ncols, pinv.pdata, 1, 1, c.pdata, GMat.BinOp.op_pow) - } - if (err != 0) throw new RuntimeException("LXdist scaling error "+err) - c - } - - - def LXdist(a:FMat, b:FMat, omat:FMat, p:Float):FMat = { - (a, b, omat) match { - case (aa:GMat, bb:GMat, oo:GMat) => LXdist(aa, bb, oo, p); - case _ => { - if (a.ncols != b.ncols) { - throw new RuntimeException("LXdist number of columns = number of features must match") - } - val c = FMat.newOrCheckFMat(a.nrows, b.nrows, omat, a.GUID, b.GUID, "LXdist".##); - val easyp = (p == 0f || p == 1f || p == 2f); - val takeroot = (p != 0f && p != 1f); - val maxrows = if (easyp) 8192 else 2048; - val maxcols = if (easyp) 8192 else 2048; - val rblkk = if (Mat.hasCUDA > 1) 2 else 1; - val cblkk = if (Mat.hasCUDA > 3) 2 else 1; - val rblk = rblkk*(math.max(1, math.ceil(c.nrows/maxrows/rblkk).toInt)); - val cblk = cblkk*(math.max(1, math.ceil(c.ncols/maxcols/cblkk).toInt)); - val kblk = math.max(1, math.ceil(a.ncols/maxcols).toInt); - val gcrows = 32*(c.nrows/rblk/32); - val gccols = 32*(c.ncols/cblk/32); - val garows = gcrows; - val gacols = 32*(a.ncols/kblk/32); - val gbrows = gccols; - val gbcols = gacols; - - val done = IMat(rblkk*cblkk,1); - for (ix <- 0 until rblkk) { - for (iy <- 0 until cblkk) { - Future { - val ithread = ix+iy*2; - var err = 0; - GFunctions.setGPU(ithread); - val pinv = if (takeroot) GMat(1f/p) else null:GMat; - val ga = GMat(garows, gacols); - val gb = GMat(gbrows, gbcols); - val gc = GMat(gcrows, gccols); - val aa = ga.pdata; - val bb = gb.pdata; - val cc = gc.pdata; - var i = ix*gcrows; - while (i < c.nrows) { - val ni = math.min(gcrows, c.nrows - i); - var j = iy*gccols; - while (j < c.ncols) { - val nj = math.min(gccols, c.ncols - j); - var k = 0; - cudaMemset(cc, 0, 1L*gcrows*gccols*Sizeof.FLOAT); - cudaStreamSynchronize(Mat.SyncMethod); - while (k < a.ncols) { - val nk = math.min(gacols, a.ncols - k); - err = cudaMemcpy2D(aa, garows*Sizeof.FLOAT, Pointer.to(a.data).withByteOffset(1L*(i+k*a.nrows)*Sizeof.FLOAT), - a.nrows*Sizeof.FLOAT, ni*Sizeof.FLOAT, nk, cudaMemcpyHostToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - if (err != 0) throw new RuntimeException("LXdist copy a failed "+err); - err = cudaMemcpy2D(bb, gbrows*Sizeof.FLOAT, Pointer.to(b.data).withByteOffset(1L*(j+k*b.nrows)*Sizeof.FLOAT), - b.nrows*Sizeof.FLOAT, nj*Sizeof.FLOAT, nk, cudaMemcpyHostToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - if (err != 0) throw new RuntimeException("LXdist copy b failed "+err); - - err=CUMAT.distances(aa, garows, bb, gbrows, cc, gcrows, nk, ni, nj, p); - - // if (err != 0) throw new RuntimeException("CUDA error in LXdist %d thread %d %d %d %d" format (err, ithread, nk, ni, nj)) - if (err != 0) println("CUDA error in LXdist %d thread %d %d %d %d" format (err, ithread, nk, ni, nj)); - k += gacols; - } - if (takeroot) err = CUMAT.applyop(cc, ni, nj, pinv.pdata, 1, 1, cc, GMat.BinOp.op_pow); - if (err != 0) throw new RuntimeException("LXdist scale c failed "+err); - err = cudaMemcpy2D(Pointer.to(c.data).withByteOffset(1L*(i+j*c.nrows)*Sizeof.FLOAT), c.nrows*Sizeof.FLOAT, - cc, gcrows*Sizeof.FLOAT, ni*Sizeof.FLOAT, nj, cudaMemcpyDeviceToHost); - cudaStreamSynchronize(Mat.SyncMethod); - if (err != 0) throw new RuntimeException("LXdist copy c failed "+err); - j += cblkk*gccols; - } - i += rblkk*gcrows; - } - gc.free; - gb.free; - ga.free; - if (takeroot) pinv.free - done(ithread,0) = 1 - } - } - } - while (SciFunctions.mini(done).v == 0) Thread.`yield`; - GFunctions.setGPU(0); - Mat.nflops += 3L * c.nrows * c.ncols * a.ncols; - c; - } - } - } - - def sortdown2(a:DMat) = _sort2(a, true) - - def _sort2(a:DMat, asc:Boolean):(DMat, IMat) = { - if (a.ncols != 1) throw new RuntimeException("_sort2 works only on column pdata") - val outv = DMat.newOrCheckDMat(a.nrows, a.ncols, null, a.GUID, "_sort2_1".hashCode) - val outi = IMat.newOrCheckIMat(a.nrows, a.ncols, null, a.GUID, "_sort2_2".hashCode) - if (Mat.hasCUDA > 0) { - val (dmy, freebytes, allbytes) = SciFunctions.GPUmem - if (a.length * 26L < freebytes) { - var i = 0; while (i < a.nrows) {outi(i) = i; i += 1} - val gv = GMat(a.nrows, 2*a.ncols) - val gi = GIMat(outi) - var err = cudaMemcpy(gv.pdata, Pointer.to(a.data), 1L*a.nrows*Sizeof.DOUBLE, cudaMemcpyKind.cudaMemcpyHostToDevice) - if (err != 0) throw new RuntimeException("sortGPU copy v error %d" format err) - cudaStreamSynchronize(Mat.SyncMethod) - CUMAT.dsortk(gv.pdata, gi.pdata, a.nrows, if (asc) 1 else 0) - err = cudaMemcpy(Pointer.to(outv.data), gv.pdata, 1L*a.nrows*Sizeof.DOUBLE, cudaMemcpyKind.cudaMemcpyDeviceToHost) - if (err != 0) throw new RuntimeException("sortGPU copy v error %d" format err) - outi <-- gi - gi.free - gv.free - } else { - DenseMat.sort2(a, 1, false, outv, outi) - } - } else { - DenseMat.sort2(a, 1, false, outv, outi) - } - (outv, outi) - } - -} diff --git a/src/main/scala/BIDMat/GIFunctions.scala b/src/main/scala/BIDMat/GIFunctions.scala deleted file mode 100644 index 87d7fec4..00000000 --- a/src/main/scala/BIDMat/GIFunctions.scala +++ /dev/null @@ -1,362 +0,0 @@ -package BIDMat - -import java.util.Random._; -import org.apache.commons.math3.special._ -import org.apache.commons.math3.util.FastMath -import org.apache.commons.math3.random.RandomDataGenerator; -import scala.concurrent.Future -import scala.concurrent.ExecutionContext.Implicits.global -import edu.berkeley.bid.MurmurHash3.MurmurHash3_x64_64 -import edu.berkeley.bid.CUMAT -import edu.berkeley.bid.SLATEC; -import GMat.BinOp -import GMat.TransF -import GMat.TransF2 -import SciState._; -import jcuda._ -import jcuda.runtime._ -import jcuda.runtime.JCuda._ -import jcuda.runtime.cudaMemcpyKind._ -import jcuda.jcublas._ -import jcuda.jcublas.JCublas._ -import jcuda.jcusparse._ - - -object GIFunctions { - import GMat.BinOp._ - - def max(a:GIMat, b:GIMat, out:Mat):GIMat = a.GIop(b, out, op_max) - def min(a:GIMat, b:GIMat, out:Mat):GIMat = a.GIop(b, out, op_min) - - def maxi(a:GIMat, dir:Int, out:Mat):GIMat = a.reduceOp(out, dir, Int.MinValue, BinOp.op_max); - def mini(a:GIMat, dir:Int, out:Mat):GIMat = a.reduceOp(out, dir, Int.MaxValue, BinOp.op_min); - def sum(a:GIMat, dir:Int, out:Mat):GIMat = a.reduceOp(out, dir, 0, BinOp.op_add); - def prod(a:GIMat, dir:Int, out:Mat):GIMat = a.reduceOp(out, dir, 1, BinOp.op_mul); - - def accumIJ(I:GIMat, J:GIMat, V:GIMat, omat:Mat, nrows:Int, ncols:Int):GIMat = { - val out = GIMat.newOrCheckGIMat(nrows, ncols, omat, I.GUID, J.GUID, V.GUID, "GIMat_accum".##) - out.clear - if (I.length != J.length || I.length != V.length) { - throw new RuntimeException("GIMat accum: index lengths dont match") - } - val err = CUMAT.iaccum(I.pdata, J.pdata, V.pdata, out.pdata, I.length, nrows); - if (err != 0) throw new RuntimeException("CUMAT.iaccum error " + cudaGetErrorString(err)); - Mat.nflops += I.length - out - } - - def accumIJ(I:Int, J:GIMat, V:GIMat, omat:Mat, nrows:Int, ncols:Int):GIMat = { - val out = GIMat.newOrCheckGIMat(nrows, ncols, omat, I, J.GUID, V.GUID, "GIMat_accumI".##) - out.clear - if (J.length != V.length) { - throw new RuntimeException("GIMat accum: index lengths dont match") - } - val err = CUMAT.iaccumI(I, J.pdata, V.pdata, out.pdata, J.length, nrows); - if (err != 0) throw new RuntimeException("CUMAT.iaccumI error " + cudaGetErrorString(err)); - Mat.nflops += J.length - out - } - - def accumIJ(I:GIMat, J:Int, V:GIMat, omat:Mat, nrows:Int, ncols:Int):GIMat = { - val out = GIMat.newOrCheckGIMat(nrows, ncols, omat, I.GUID, J, V.GUID, "GIMat_accumJ".##) - out.clear - if (I.length != V.length) { - throw new RuntimeException("GIMat accum: index lengths dont match") - } - val err = CUMAT.iaccumJ(I.pdata, J, V.pdata, out.pdata, I.length, nrows); - if (err != 0) throw new RuntimeException("CUMAT.iaccumJ error " + cudaGetErrorString(err)); - Mat.nflops += I.length - out - } - - def accumIJ(I:GIMat, J:GIMat, V:Int, omat:Mat, nrows:Int, ncols:Int):GIMat = { - val out = GIMat.newOrCheckGIMat(nrows, ncols, omat, I.GUID, J.GUID, V.hashCode, "GIMat_accumV".##) - out.clear - if (I.length != J.length) { - throw new RuntimeException("GIMat accum: index lengths dont match") - } - val err = CUMAT.iaccumV(I.pdata, J.pdata, V, out.pdata, I.length, nrows); - if (err != 0) throw new RuntimeException("CUMAT.iaccumV error " + cudaGetErrorString(err)); - Mat.nflops += I.length - out - } - - def accumIJ(I:Int, J:GIMat, V:Int, omat:Mat, nrows:Int, ncols:Int):GIMat = { - val out = GIMat.newOrCheckGIMat(nrows, ncols, omat, I, J.GUID, V.hashCode, "GIMat_accumIV".##) - out.clear - val err = CUMAT.iaccumIV(I, J.pdata, V, out.pdata, J.length, nrows); - if (err != 0) throw new RuntimeException("CUMAT.iaccumIV error " + cudaGetErrorString(err)); - Mat.nflops += J.length - out - } - - def accumIJ(I:GIMat, J:Int, V:Int, omat:Mat, nrows:Int, ncols:Int):GIMat = { - val out = GIMat.newOrCheckGIMat(nrows, ncols, omat, I.GUID, J, V.hashCode, "GIMat_accumJV".##) - out.clear - val err = CUMAT.iaccumJV(I.pdata, J, V, out.pdata, I.length, nrows); - if (err != 0) throw new RuntimeException("CUMAT.iaccumJV error " + cudaGetErrorString(err)); - Mat.nflops += I.length - out - } - - def accum(IJ:GIMat, V:GIMat, omat:Mat, nrows:Int, ncols:Int):GIMat = { - if (IJ.nrows != V.length || IJ.ncols > 2) { - throw new RuntimeException("GIMat accum: index lengths dont match") - } - val out = GIMat.newOrCheckGIMat(nrows, ncols, omat, IJ.GUID, V.GUID, "GIMat_accumIJ".##) - out.clear - if (IJ.ncols == 2) { - val err = CUMAT.iaccum(IJ.pdata, IJ.pdata.withByteOffset(1L*IJ.nrows*Sizeof.INT), V.pdata, out.pdata, V.length, nrows); - if (err != 0) throw new RuntimeException("CUMAT.iaccum error " + cudaGetErrorString(err)); - } else { - val err= CUMAT.iaccumJ(IJ.pdata, 0, V.pdata, out.pdata, V.length, nrows); - if (err != 0) throw new RuntimeException("CUMAT.iaccumJ error " + cudaGetErrorString(err)); - } - Mat.nflops += V.length - out - } - - def accum(IJ:GIMat, V:Int, omat:Mat, nrows:Int, ncols:Int):GIMat = { - if (IJ.ncols > 2) { - throw new RuntimeException("GIMat accum: index lengths dont match") - } - val out = GIMat.newOrCheckGIMat(nrows, ncols, omat, IJ.GUID, V.hashCode, "GIMat_accumIJV".##) - out.clear - if (IJ.ncols == 2) { - val err = CUMAT.iaccumV(IJ.pdata, IJ.pdata.withByteOffset(1L*IJ.nrows*Sizeof.INT), V, out.pdata, IJ.nrows, nrows); - if (err != 0) throw new RuntimeException("CUMAT.iaccumV error " + cudaGetErrorString(err)); - } else { - val err = CUMAT.iaccumJV(IJ.pdata, 0, V, out.pdata, IJ.nrows, nrows); - if (err != 0) throw new RuntimeException("CUMAT.iaccumJV error " + cudaGetErrorString(err)); - } - Mat.nflops += IJ.nrows - out - } - - def cumsumg(a:GIMat, jc:GIMat, omat:Mat):GIMat = { - Mat.nflops += 1L * a.length - val out = GIMat.newOrCheckGIMat(a.nrows, a.ncols, omat, a.GUID, jc.GUID, "cumsumg".##) - val err = CUMAT.cumsumgi(a.pdata, out.pdata, jc.pdata, a.nrows, a.ncols, jc.length-1) - if (err != 0) throw new RuntimeException("cumsumg error %d: " + cudaGetErrorString(err) format err); - out - } - - def maxg(a:GIMat, jc:GIMat, omat:Mat, omati:Mat):(GIMat, GIMat) = { - Mat.nflops += 1L * a.length - val out = GIMat.newOrCheckGIMat(jc.length-1, a.ncols, omat, a.GUID, jc.GUID, "maxg".##) - val outi = GIMat.newOrCheckGIMat(jc.length-1, a.ncols, omati, a.GUID, jc.GUID, "maxs_i".##) - val err = CUMAT.maxgi(a.pdata, out.pdata, outi.pdata, jc.pdata, a.nrows, a.ncols, jc.length-1) - if (err != 0) throw new RuntimeException("maxg error %d: " + cudaGetErrorString(err) format err); - (out, outi) - } - - def ming(a:GIMat, jc:GIMat, omat:Mat, omati:Mat):(GIMat, GIMat) = { - Mat.nflops += 1L * a.length - val out = GIMat.newOrCheckGIMat(jc.length-1, a.ncols, omat, a.GUID, jc.GUID, "ming".##) - val outi = GIMat.newOrCheckGIMat(jc.length-1, a.ncols, omati, a.GUID, jc.GUID, "ming_1".##) - val err = CUMAT.mingi(a.pdata, out.pdata, outi.pdata, jc.pdata, a.nrows, a.ncols, jc.length-1) - if (err != 0) throw new RuntimeException("ming error %d: " + cudaGetErrorString(err) format err); - (out, outi) - } - - def maxi2(a:GIMat, omat:Mat, omati:Mat, dim0:Int):(GIMat, GIMat) = { - Mat.nflops += 1L * a.length - val dim = if (a.nrows == 1 && dim0 == 0) 2 else math.max(1, dim0) - if (dim == 1) { - val out = GIMat.newOrCheckGIMat(1, a.ncols, omat, a.GUID, "maxi2".##) - val outi = GIMat.newOrCheckGIMat(1, a.ncols, omati, a.GUID, "maxi2_1".##) - val err = CUMAT.maxii(a.pdata, out.pdata, outi.pdata, a.nrows, a.ncols, 1) - if (err != 0) throw new RuntimeException("maxi2 error %d: " + cudaGetErrorString(err) format err); - (out, outi) - } else if (dim == 2) { - val out = GIMat.newOrCheckGIMat(a.nrows, 1, omat, a.GUID, "maxi2".##) - val outi = GIMat.newOrCheckGIMat(a.nrows, 1, omati, a.GUID, "maxi2_1".##) - val err = CUMAT.maxii(a.pdata, out.pdata, outi.pdata, a.nrows, a.ncols, 2) - if (err != 0) throw new RuntimeException("maxi2 error %d: " + cudaGetErrorString(err) format err); - (out, outi) - } else { - throw new RuntimeException("maxi2 dimension not recognized %d" format dim0) - } - } - - def mini2(a:GIMat, omat:Mat, omati:Mat, dim0:Int):(GIMat, GIMat) = { - Mat.nflops += 1L * a.length - val dim = if (a.nrows == 1 && dim0 == 0) 2 else math.max(1, dim0) - if (dim == 1) { - val out = GIMat.newOrCheckGIMat(1, a.ncols, omat, a.GUID, "mini2".##) - val outi = GIMat.newOrCheckGIMat(1, a.ncols, omati, a.GUID, "mini2_1".##) - val err = CUMAT.minii(a.pdata, out.pdata, outi.pdata, a.nrows, a.ncols, 1) - if (err != 0) throw new RuntimeException("mini2 error %d: " + cudaGetErrorString(err) format err); - (out, outi) - } else if (dim == 2) { - val out = GIMat.newOrCheckGIMat(a.nrows, 1, omat, a.GUID, "maxi2".##) - val outi = GIMat.newOrCheckGIMat(a.nrows, 1, omati, a.GUID, "maxi2_1".##) - val err = CUMAT.minii(a.pdata, out.pdata, outi.pdata, a.nrows, a.ncols, 2) - if (err != 0) throw new RuntimeException("mini2 error %d: " + cudaGetErrorString(err) format err); - (out, outi) - } else { - throw new RuntimeException("mini2 direction not recognized %d" format dim) - } - } - - - def i3sortlexIndsGPU(grams:IMat, inds:IMat, asc:Boolean) = { - if (grams.nrows != inds.nrows) throw new RuntimeException("i3sortlexIndsGPU mismatched dims") - val p1 = Pointer.to(grams.data) - val p2 = p1.withByteOffset(1L*inds.nrows*Sizeof.INT) - val p3 = p1.withByteOffset(1L*inds.nrows*2*Sizeof.INT) - val p4 = Pointer.to(inds.data) - p4sortlexGPU(p1, p2, p3, p4, grams.nrows, asc) - } - - def i4sortlexColsGPU(col1:IMat, col2:IMat, col3:IMat, inds:IMat, asc:Boolean) = { - if (col1.nrows != inds.nrows || col2.nrows != inds.nrows || col3.nrows != inds.nrows) { - throw new RuntimeException("i3sortlexColsGPU mismatched dims") - } - val p1 = Pointer.to(col1.data) - val p2 = Pointer.to(col2.data) - val p3 = Pointer.to(col3.data) - val p4 = Pointer.to(inds.data) - p4sortlexGPU(p1, p2, p3, p4, inds.nrows, asc) - } - - def p4sortlexGPU(p1:Pointer, p2:Pointer, p3:Pointer, p4:Pointer, nrows:Int, asc:Boolean) = { - val ggrams = GIMat(nrows, 4) - var status = cudaMemcpy(ggrams.pdata, p1, 1L*nrows*Sizeof.INT, cudaMemcpyHostToDevice) - if (status != 0) throw new RuntimeException("p4sortlexGPU error1 %d" format (status)) - status = cudaMemcpy(ggrams.pdata.withByteOffset(1L*nrows*Sizeof.INT), p2, 1L*nrows*Sizeof.INT, cudaMemcpyHostToDevice) - if (status != 0) throw new RuntimeException("p4sortlexGPU error2 %d" format (status)) - status = cudaMemcpy(ggrams.pdata.withByteOffset(1L*nrows*2*Sizeof.INT), p3, 1L*nrows*Sizeof.INT, cudaMemcpyHostToDevice) - if (status != 0) throw new RuntimeException("p4sortlexGPU error3 %d" format (status)) - status = cudaMemcpy(ggrams.pdata.withByteOffset(1L*nrows*3*Sizeof.INT), p4, 1L*nrows*Sizeof.INT, cudaMemcpyHostToDevice) - if (status != 0) throw new RuntimeException("p4sortlexGPU error4 %d" format (status)) - cudaStreamSynchronize(Mat.SyncMethod) - val ggramst = ggrams.t - ggrams.free - CUMAT.i4sort(ggramst.pdata, nrows, if (asc) 1 else 0) - val ograms = ggramst.t - ggramst.free - status = cudaMemcpy(p1, ograms.pdata, 1L*nrows*Sizeof.INT, cudaMemcpyDeviceToHost) - if (status != 0) throw new RuntimeException("p4sortlexGPU error5 %d" format (status)) - status = cudaMemcpy(p2, ograms.pdata.withByteOffset(1L*nrows*Sizeof.INT), 1L*nrows*Sizeof.INT, cudaMemcpyDeviceToHost) - if (status != 0) throw new RuntimeException("p4sortlexGPU error6 %d" format (status)) - status = cudaMemcpy(p3, ograms.pdata.withByteOffset(1L*nrows*2*Sizeof.INT), 1L*nrows*Sizeof.INT, cudaMemcpyDeviceToHost) - if (status != 0) throw new RuntimeException("p4sortlexGPU error7 %d" format (status)) - status = cudaMemcpy(p4, ograms.pdata.withByteOffset(1L*nrows*3*Sizeof.INT), 1L*nrows*Sizeof.INT, cudaMemcpyDeviceToHost) - if (status != 0) throw new RuntimeException("p4sortlexGPU error8 %d" format (status)) - ograms.free - } - - def i2sortlexIndsGPU(grams:IMat, inds:IMat, asc:Boolean) = { - if (grams.nrows != inds.nrows) throw new RuntimeException("i2sortlexIndsGPU mismatched dims") - val p1 = Pointer.to(grams.data) - val p2 = p1.withByteOffset(1L*inds.nrows*Sizeof.INT) - val p3 = Pointer.to(inds.data) - p3sortlexGPU(p1, p2, p3, inds.nrows, asc) - } - - def i2sortlexColsIndsGPU(col1:IMat, col2:IMat, inds:IMat, asc:Boolean) = { - if (col1.nrows != inds.nrows || col2.nrows != inds.nrows) throw new RuntimeException("i2sortlexColsIndsGPU mismatched dims") - val p1 = Pointer.to(col1.data) - val p2 = Pointer.to(col2.data) - val p3 = Pointer.to(inds.data) - p3sortlexGPU(p1, p2, p3, inds.nrows, asc) - } - /* - * Useful for creating sparse matrices - */ - - def i2sortlexColsIndsGPU(col1:IMat, col2:IMat, fvals:FMat, asc:Boolean) = { - if (col1.nrows != fvals.nrows || col2.nrows != fvals.nrows) throw new RuntimeException("i2sortlexGPU mismatched dims") - val p1 = Pointer.to(col1.data) - val p2 = Pointer.to(col2.data) - val p3 = Pointer.to(fvals.data) - p3sortlexGPU(p1, p2, p3, fvals.nrows, asc) - } - - /* - * This is not strictly a 3-column lex sort, only the first two columns are used, and the third is just permuted - */ - def p3sortlexGPU(p1:Pointer, p2:Pointer, p3:Pointer, nrows:Int, asc:Boolean) = { - val ggrams = GIMat(nrows, 2) - val gvals = GIMat(nrows, 1) - var status = cudaMemcpy(ggrams.pdata, p2, 1L*nrows*Sizeof.INT, cudaMemcpyHostToDevice) - if (status != 0) throw new RuntimeException("p3sortlexGPU error1 %d" format (status)) - status = cudaMemcpy(ggrams.pdata.withByteOffset(1L*nrows*Sizeof.INT), p1, 1L*nrows*Sizeof.INT, cudaMemcpyHostToDevice) - if (status != 0) throw new RuntimeException("p3sortlexGPU error2 %d" format (status)) - status = cudaMemcpy(gvals.pdata, p3, 1L*nrows*Sizeof.INT, cudaMemcpyHostToDevice) - if (status != 0) throw new RuntimeException("p3sortlexGPU error3 %d" format (status)) - cudaStreamSynchronize(Mat.SyncMethod) - val ggramst = ggrams.t - ggrams.free - CUMAT.lsortk(ggramst.pdata, gvals.pdata, nrows, if (asc) 1 else 0) - val ograms = ggramst.t - ggramst.free - status = cudaMemcpy(p1, ograms.pdata.withByteOffset(1L*nrows*Sizeof.INT), 1L*nrows*Sizeof.INT, cudaMemcpyDeviceToHost) - if (status != 0) throw new RuntimeException("p3sortlexGPU error4 %d" format (status)) - status = cudaMemcpy(p2, ograms.pdata, 1L*nrows*Sizeof.INT, cudaMemcpyDeviceToHost) - if (status != 0) throw new RuntimeException("p3sortlexGPU error5 %d" format (status)) - status = cudaMemcpy(p3, gvals.pdata, 1L*nrows*Sizeof.INT, cudaMemcpyDeviceToHost) - if (status != 0) throw new RuntimeException("p3sortlexGPU error6 %d" format (status)) - cudaStreamSynchronize(Mat.SyncMethod) - ograms.free - gvals.free - } - - def isortlexIndsGPU(grams:IMat, inds:IMat, asc:Boolean) = { - if (grams.nrows != inds.nrows) throw new RuntimeException("isortlexIndsGPU mismatched dims") - val p1 = Pointer.to(grams.data) - val p2 = Pointer.to(inds.data) - p2sortlexGPU(p1, p2, inds.nrows, asc) - } - - def i2sortlexGPU(mat:IMat, asc:Boolean) = { - if (mat.ncols != 2) throw new RuntimeException("i2sortlexGPU mismatched dims") - val p1 = Pointer.to(mat.data) - val p2 = Pointer.to(mat.data).withByteOffset(1L*mat.nrows*Sizeof.INT) - p2sortlexGPU(p1, p2, mat.nrows, asc) - } - - def i2sortlexColsGPU(col1:IMat, col2:IMat, asc:Boolean) = { - if (col1.nrows != col2.nrows) throw new RuntimeException("i2sortlexGPU mismatched dims") - val p1 = Pointer.to(col1.data) - val p2 = Pointer.to(col2.data) - p2sortlexGPU(p1, p2, col1.nrows, asc) - } - - - def p2sortlexGPU(p1:Pointer, p2:Pointer, nrows:Int, asc:Boolean) = { - val ggrams = GIMat(nrows, 2) - var status = cudaMemcpy(ggrams.pdata, p2, 1L*nrows*Sizeof.INT, cudaMemcpyHostToDevice) - if (status != 0) throw new RuntimeException("p3sortlexGPU error1 %d" format (status)) - status = cudaMemcpy(ggrams.pdata.withByteOffset(1L*nrows*Sizeof.INT), p1, 1L*nrows*Sizeof.INT, cudaMemcpyHostToDevice) - if (status != 0) throw new RuntimeException("p3sortlexGPU error2 %d" format (status)) - cudaStreamSynchronize(Mat.SyncMethod) - val ggramst = ggrams.t - ggrams.free - CUMAT.lsort(ggramst.pdata, nrows, if (asc) 1 else 0) - val ograms = ggramst.t - ggramst.free - status = cudaMemcpy(p1, ograms.pdata.withByteOffset(1L*nrows*Sizeof.INT), 1L*nrows*Sizeof.INT, cudaMemcpyDeviceToHost) - if (status != 0) throw new RuntimeException("p3sortlexGPU error4 %d" format (status)) - status = cudaMemcpy(p2, ograms.pdata, 1L*nrows*Sizeof.INT, cudaMemcpyDeviceToHost) - if (status != 0) throw new RuntimeException("p3sortlexGPU error5 %d" format (status)) - cudaStreamSynchronize(Mat.SyncMethod) - ograms.free - } - - /* - def cumsum(a:GIMat, dim0:Int, omat:Mat):GIMat = { - Mat.nflops += 1L * a.length; - val dim = if (a.nrows == 1 && dim0 == 0) 2 else math.max(1, dim0); - if (dim == 1) { - val out = GIMat.newOrCheckGIMat(a.nrows, a.ncols, omat, a.GUID, "cumsum".##) - CUMAT.cumsumc(a.nrows, a.ncols, a.pdata, out.pdata) - out - } else { - throw new RuntimeException("Cumsum across rows not supported yet") - } - } - * */ - -} \ No newline at end of file diff --git a/src/main/scala/BIDMat/GIMat.scala b/src/main/scala/BIDMat/GIMat.scala deleted file mode 100755 index 680d8349..00000000 --- a/src/main/scala/BIDMat/GIMat.scala +++ /dev/null @@ -1,1373 +0,0 @@ -package BIDMat -import jcuda._; -import jcuda.jcublas.JCublas; -import jcuda.runtime._ -import jcuda.runtime.JCuda._ -import jcuda.runtime.cudaMemcpyKind._ -import jcuda.runtime.cudaError._ -import jcuda.runtime.cudaMemcpyKind._ -import jcuda.jcublas._ -import jcuda.jcublas.JCublas._ -import jcuda.jcusparse._ -import edu.berkeley.bid.CUMAT; -import scala.util.hashing.MurmurHash3 -import edu.berkeley.bid.MurmurHash3.MurmurHash3_x64_64 -import java.io._ - -class GIMat(dims0:Array[Int], @transient var pdata:Pointer, val realsize:Long) extends IMat(dims0, null) { - import GIMat.BinOp._ - - def this(nr:Int, nc:Int, pdata:Pointer, realsize:Long) = this(Array(nr, nc), pdata, realsize); - - override def toString:String = { - val nr = scala.math.min(nrows,10) - val nc = scala.math.min(ncols,50) - if (nr*nc > 0) { - val tmpMat = IMat(nr, nc) - JCublas.cublasGetMatrix(nr, nc, Sizeof.INT, pdata, nrows, Pointer.to(tmpMat.data), nr) - tmpMat.toString - } else { - "" - } - } - - override def contents() = { - val out = new GIMat(length, 1, pdata, realsize); - out.setGUID(MurmurHash3.mix(MurmurHash3.mix(length, 1), (GUID*7897889).toInt)); - out - } - - override def dv:Double = - if (nrows > 1 || ncols > 1) { - throw new RuntimeException("Matrix should be 1x1 to extract value") - } else { - toIMat().data(0) - } - - override def v:Int = - if (nrows > 1 || ncols > 1) { - throw new RuntimeException("Matrix should be 1x1 to extract value") - } else { - toIMat().data(0) - } - - override def view(nr:Int, nc:Int):GIMat = { - if (1L * nr * nc > realsize) { - throw new RuntimeException("view dimensions too large") - } - if (nr == nrows && nc == ncols) { - this - } else { - val out = new GIMat(nr, nc, pdata, realsize); - out.setGUID(MurmurHash3.mix(MurmurHash3.mix(nr, nc), (GUID*3145341).toInt)); - out - } - } - - override def reshapeView(newdims:Int*):GIMat = reshapeView(newdims.toArray) - - override def reshapeView(newdims:Array[Int]):GIMat = { - if (newdims.reduce(_*_) == length) { - val out = new GIMat(newdims, pdata, llength); - out.setGUID(MurmurHash3_x64_64(newdims.map(_.toLong) :+ GUID, "reshapeView".##)); - out - } else { - throw new RuntimeException("GIMat reshapeView total length doesnt match") - } - } - - override def reshapeView(adims:IMat):GIMat = reshapeView(adims.data); - - override def reshapeTrim(newdims:Int*):GIMat = reshapeTrim(newdims.toArray) - - override def reshapeTrim(newdims:Array[Int]):GIMat = { - if (newdims.reduce(_*_) <= realsize) { - val out = new GIMat(newdims, pdata, realsize); - out.setGUID(MurmurHash3_x64_64(newdims.map(_.toLong) :+ GUID, "reshapeTrim".##)); - out - } else { - throw new RuntimeException("GIMat reshapeTrim total length too large") - } - } - - override def reshapeTrim(adims:IMat):GIMat = reshapeTrim(adims.data); - - override def mytype = "GIMat" - - override def nnz = length - - /** hold indices in GPU mem */ - val ginds = new Array[GIMat](ndims); - - val myGPU = SciFunctions.getGPU - - var saveMe:IMat = null - - private def writeObject(out:ObjectOutputStream):Unit = { - saveMe = IMat(this); - out.defaultWriteObject(); - } - - private def readObject(in:ObjectInputStream):Unit = { - in.defaultReadObject(); - val gpu = SciFunctions.getGPU; - SciFunctions.setGPU(myGPU); - pdata = GIMat(saveMe).pdata; - SciFunctions.setGPU(gpu); - saveMe = null; - } - - - def getIndxWrapper(i:Int):GIMat = { - if (ginds(i).asInstanceOf[AnyRef] == null) { - ginds(i) = GIMat(1,1) - } - ginds(i); - } - - def getIndexValue(i:Int, v:Int):GIMat = { - getIndxWrapper(i); - ginds(i).set(v); - ginds(i); - } - - def getIndexMat(i:Int, v:IMat):GIMat = { - if (v.length == 0 || v.length > 1) { - GIMat(v); - } else { - getIndxWrapper(i).set(v.v); - ginds(i); - } - } - - def safePointer(ind:GIMat):Pointer = { - if (ind.asInstanceOf[AnyRef] == null) { - GMat.nullPointer; - } else { - ind.pdata; - } - } - - /** 1D access */ - - override def apply(ind:Int):Int = { - val tmp = new Array[Int](1); - GIMat.GPUtoCPUarraycopy(pdata, ind, tmp, 0, 1, "GIMat apply"); - tmp(0) - } - - /** 2D access */ - - override def apply(i:Int, j:Int):Int = { - val tmp = new Array[Int](1); - GIMat.GPUtoCPUarraycopy(pdata, i + nrows * j, tmp, 0, 1, "GIMat apply"); - tmp(0) - } - - /** ND access */ - - override def applyv(inds:Array[Int]):Int = { - val indx = ND.linearize(inds, dims.data); - val tmp = new Array[Int](1); - GIMat.GPUtoCPUarraycopy(pdata, indx, tmp, 0, 1, "GIMat apply"); - tmp(0); - } - - override def apply(i1:IMat, i2:IMat):GIMat = applyi(Array(i1, i2), null); - override def apply(i1:IMat, i2:Int):GIMat = applyi(Array(i1, IMat.ielem(i2)), null); - override def apply(i1:Int, i2:IMat):GIMat = applyi(Array(IMat.ielem(i1), i2), null); - - override def applyi(inds:Array[IMat]):GIMat = applyi(inds, null); - - override def apply(inds:IMat):GIMat = { - inds match { - case aa:MatrixWildcard => { - val out = GIMat.newOrCheckGIMat(length, 1, null, GUID, inds.GUID, "apply(?)".##); - GMat.GPUtoGPUarraycopy(pdata, 0, out.pdata, 0, length, "GIMat IMat apply" ); - out - } - case _ => { - val newinds = getIndexMat(0, inds); - val out = GIMat.newOrCheckGIMat(inds.dims, null, GUID, inds.GUID, "apply IMat".##); - val err = CUMAT.copyFromInds(pdata, out.pdata, safePointer(newinds), inds.length); - if (err != 0) throw new RuntimeException("GIMat apply(I) error" + cudaGetErrorString(err)); - out; - } - } - } - - def applyi(inds:Array[IMat], omat:Mat):GIMat = { - val newdims = new Array[Int](_dims.length) - val newinds = new Array[GIMat](_dims.length) - for (i <- 0 until _dims.length) { - inds(i) match { - case aa:MatrixWildcard => { - newdims(i) = _dims(i); - } - case _ => { - newdims(i) = inds(i).length; - newinds(i) = getIndexMat(i, inds(i)); - } - } - } - val out = GIMat.newOrCheckGIMat(newdims, omat, GUID, ND.hashGUIDs(inds), "apply".##); - inds.length match { - case 1 => { - val err = CUMAT.copyFromInds(pdata, out.pdata, safePointer(newinds(0)), newdims(0)); - if (err != 0) throw new RuntimeException("GIMat apply(I) error" + cudaGetErrorString(err)); - } - case 2 => { - val err = CUMAT.copyFromInds2D(pdata, dims(0), out.pdata, newdims(0), safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1)); - if (err != 0) throw new RuntimeException("GIMat apply(I, J) error" + cudaGetErrorString(err)); - } - case 3 => { - val err = CUMAT.copyFromInds3D(pdata, dims(0), dims(1), out.pdata, newdims(0), newdims(1), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1), safePointer(newinds(2)), newdims(2)); - if (err != 0) throw new RuntimeException("GIMat apply(I, J, K) error" + cudaGetErrorString(err)); - } - case 4 => { - val err = CUMAT.copyFromInds4D(pdata, dims(0), dims(1), dims(2), out.pdata, newdims(0), newdims(1), newdims(2), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1), safePointer(newinds(2)), newdims(2), safePointer(newinds(3)), newdims(3)); - if (err != 0) throw new RuntimeException("GIMat apply(I, J, K, L) error" + cudaGetErrorString(err)); - } - case _ => throw new RuntimeException("GIMat slice access with more than 4 indices not supported"); - } - out; - } - - /** 1D update */ - - override def update(i:Int, v:Int):GIMat = { - val tmp = new Array[Int](1); - tmp(0) = v; - GIMat.CPUtoGPUarraycopy(tmp, 0, pdata, i, 1, "GIMat update"); - this - } - - /** 2D update */ - - override def update(i:Int, j:Int, v:Int):GIMat = { - val tmp = new Array[Int](1); - tmp(0) = v; - GIMat.CPUtoGPUarraycopy(tmp, 0, pdata, i + j * nrows, 1, "GIMat update"); - this - } - - /** ND update */ - - override def updatev(inds:Array[Int], v:Int):GIMat = { - val indx = ND.linearize(inds, _dims); - val tmp = Array[Int](v); - GIMat.CPUtoGPUarraycopy(tmp, 0, pdata, indx, 1, "GIMat update"); - this - } - - /** ND sliced updates */ - - override def update(inds:IMat, vv:IMat):GIMat = updatei(inds, GIMat(vv)); - - override def update(iv:IMat, jv:IMat, b:IMat):IMat = updatei(Array(iv, jv), GIMat(b)); - override def update(iv:IMat, j:Int, b:IMat):IMat = updatei(Array(iv, IMat.ielem(j)), GIMat(b)); - override def update(i:Int, jv:IMat, b:IMat):IMat = updatei(Array(IMat.ielem(i), jv), GIMat(b)); - - /* generic RHS */ - override def update(inds:IMat, vv:Mat):GIMat = updatei(inds, GIMat(vv)); - override def update(iv:IMat, jv:IMat, b:Mat):IMat = updatei(Array(iv, jv), GIMat(b)); - override def update(iv:IMat, j:Int, b:Mat):IMat = updatei(Array(iv, IMat.ielem(j)), GIMat(b)); - override def update(i:Int, jv:IMat, b:Mat):IMat = updatei(Array(IMat.ielem(i), jv), GIMat(b)); - - override def update(i1:IMat, i2:IMat, vv:Int):IMat = updatei(Array(i1, i2), vv); - - override def updatei(inds:Array[IMat], vv:IMat):GIMat = updatei(inds, GIMat(vv)); - - def updatei(inds:IMat, vv:GIMat):GIMat = { - val newinds = getIndexMat(0, inds); - val err = inds match { - case aa:MatrixWildcard => { - if (vv.length != length) throw new RuntimeException("GIMat column update length mismatch") - CUMAT.copyToInds(vv.pdata, pdata, safePointer(newinds), length); - } - case _ => { - if (inds.length != vv.length) throw new RuntimeException("GIMat column update length mismatch") - CUMAT.copyToInds(vv.pdata, pdata, safePointer(newinds), inds.length); - } - } - if (err != 0) throw new RuntimeException("GIMat update (I)=v error " + cudaGetErrorString(err)); - this; - } - - def updatei(inds:Array[IMat], vv:GIMat):GIMat = { - if (inds.length > 2 && inds.length != _dims.length) throw new RuntimeException("GIMat update dims must match") - val mydims = if (inds.length == 2) Array(nrows, ncols) else _dims; - val newdims = new Array[Int](inds.length) - val newinds = new Array[GIMat](inds.length) - var j = 0 - for (i <- 0 until inds.length) { - inds(i) match { - case aa:MatrixWildcard => { - newdims(i) = mydims(i); - } - case _ => { - newdims(i) = inds(i).length; - newinds(i) = getIndexMat(i, inds(i)); - } - } - } - ND.checkDims("GIMat update:", ND.trimDims(newdims), ND.trimDims(vv._dims)); - inds.length match { - case 2 => { - val err = CUMAT.copyToInds2D(vv.pdata, vv.dims(0), pdata, nrows, - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1)); - if (err != 0) throw new RuntimeException("GIMat update (I, J)=V error " + cudaGetErrorString(err)); - } - case 3 => { - val err = CUMAT.copyToInds3D(vv.pdata, vv.dims(0), vv.dims(1), pdata, dims(0), dims(1), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1), safePointer(newinds(2)), newdims(2)); - if (err != 0) throw new RuntimeException("GIMat update (I, J, K)=V error " + cudaGetErrorString(err)); - } - case 4 => { - val err = CUMAT.copyToInds4D(vv.pdata, vv.dims(0), vv.dims(1), vv.dims(2), pdata, dims(0), dims(1), dims(2), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1), safePointer(newinds(2)), newdims(2), safePointer(newinds(3)), newdims(3)); - if (err != 0) throw new RuntimeException("GIMat udpate (I, J, K, L)=V error " + cudaGetErrorString(err)); - } - case _ => throw new RuntimeException("GIMat slice access with more than 4 indices not supported"); - } - this - } - - override def update(inds:IMat, vv:Int):GIMat = { - val newinds = getIndexMat(0, inds); - val err = inds match { - case aa:MatrixWildcard => { - CUMAT.fillToIndsInt(vv, pdata, safePointer(newinds), length); - } - case _ => { - CUMAT.fillToIndsInt(vv, pdata, safePointer(newinds), inds.length); - } - } - if (err != 0) throw new RuntimeException("GMat update (I)=v error " + cudaGetErrorString(err)); - this; - } - - override def updatei(inds:Array[IMat], vv:Int):GIMat = { - if (inds.length > 2 && inds.length != _dims.length) throw new RuntimeException("GIMat update dims must match"); - val mydims = if (inds.length == 2) Array(nrows, ncols) else _dims; - val newdims = new Array[Int](inds.length); - val newinds = new Array[GIMat](inds.length); - for (i <- 0 until inds.length) { - inds(i) match { - case aa:MatrixWildcard => { - newdims(i) = mydims(i); - } - case _ => { - newdims(i) = inds(i).length; - newinds(i) = getIndexMat(i, inds(i)); - } - } - } - inds.length match { - case 2 => { - val err = CUMAT.fillToInds2DInt(vv, pdata, nrows, - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1)); - if (err != 0) throw new RuntimeException("GIMat update (I, J)=v error " + cudaGetErrorString(err)); - } - case 3 => { - val err = CUMAT.fillToInds3DInt(vv, pdata, dims(0), dims(1), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1), safePointer(newinds(2)), newdims(2)); - if (err != 0) throw new RuntimeException("GIMat update (I, J, K)=v error " + cudaGetErrorString(err)); - } - case 4 => { - val err = CUMAT.fillToInds4DInt(vv, pdata, dims(0), dims(1), dims(2), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1), safePointer(newinds(2)), newdims(2), safePointer(newinds(3)), newdims(3)); - if (err != 0) throw new RuntimeException("GIMat udpate (I, J, K, L)=v error " + cudaGetErrorString(err)); - } - case _ => throw new RuntimeException("GIMat slice update with more than 4 indices not supported"); - } - this - } - - override def colslice(a:Int, b:Int):GIMat = colslice(a, b, null, 0); - - override def colslice(a:Int, b:Int, omat:Mat):GIMat = colslice(a, b, omat, 0); - - override def colslice(a:Int, b:Int, omat:Mat, c:Int):GIMat = { - val newdims = _dims.clone; - newdims(dims.length-1) = b-a+c; - val out = if (omat.asInstanceOf[AnyRef] != null && omat.isInstanceOf[GIMat] && omat.ncols >= b-a+c && omat.nrows == nrows) { - omat.asInstanceOf[GIMat] - } else { - GIMat.newOrCheckGIMat(newdims, omat, GUID, a, b, "colslice".##); - } - cudaMemcpy(out.pdata.withByteOffset(1L*c*nrows*Sizeof.INT), pdata.withByteOffset(1L*a*nrows*Sizeof.INT), 1L*(b-a)*nrows*Sizeof.INT, cudaMemcpyDeviceToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError; - if (err != 0) throw new RuntimeException("GIMat colslice() error " + cudaGetErrorString(err)); - out - } - - override def colslice(a:Int, b:Int, omat:Mat, c:Int, pb:Boolean):GIMat = colslice(a, b, omat, c); - - override def clear = { - cudaMemset(pdata, 0, Sizeof.INT*length) - cudaStreamSynchronize(Mat.SyncMethod) - this - } - - override def izeros(m:Int, n:Int) = { - GIMat.izeros(m,n) - } - - override def iones(m:Int, n:Int) = { - GIMat.iones(m,n) - } - - override def t = { - val out = GIMat.newOrCheckGIMat(ncols, nrows, null, GUID, "t".##) - CUMAT.transpose(this.pdata, nrows, out.pdata, ncols, nrows, ncols) - cudaStreamSynchronize(Mat.SyncMethod) - out - } - - override def set(v:Int):GIMat = { - CUMAT.setival(pdata, v, length) - cudaStreamSynchronize(Mat.SyncMethod) - this - } - - def kron(a:GIMat, oldmat:Mat):GIMat = { - val out = GIMat.newOrCheckGIMat(nrows * a.nrows, ncols * a.ncols, oldmat, GUID, a.GUID, "kron".##); - Mat.nflops += 1L * out.nrows * out.ncols; - val err = CUMAT.kroni(pdata, a.pdata, out.pdata, nrows, ncols, a.nrows, a.ncols); - if (err != 0) throw new RuntimeException("kron: CUDA kernel error in CUMAT.kron " + cudaGetErrorString(err)); - out; - } - - def reduceOp(oldmat:Mat, dir:Int, initval:Int, op:Int):GIMat = { - if (dir == 1 || (dir == 0 && nrows > 1)) { - val out = GIMat.newOrCheckGIMat(1, ncols, oldmat, GUID, 1, op) - out.clear - val err = CUMAT.reduce1iop(nrows, ncols, pdata, out.pdata, initval, op) - if (err != 0) {throw new RuntimeException("CUDA kernel error in CUMAT.reduce1op " + cudaGetErrorString(err))} - Mat.nflops += length - out - } else if (dir == 2 || dir == 0) { - val out = GIMat.newOrCheckGIMat(nrows, 1, oldmat, GUID, 2, op) - out.clear - val err = CUMAT.reduce2iop(nrows, ncols, pdata, out.pdata, initval, op) - if (err != 0) {throw new RuntimeException("CUDA kernel error in CUMAT.reduce2op " + cudaGetErrorString(err))} - Mat.nflops += length - out - } else { - throw new RuntimeException("dimension must be 1 or 2"); - } - } - - def horzcat(a:GIMat, omat:Mat) = { - if (nrows != a.nrows) - throw new RuntimeException("GMat \\ row dims not equal") - val out = GIMat.newOrCheckGIMat(nrows, ncols+a.ncols, omat, GUID, a.GUID, "horzcat".##) - cudaMemcpy(out.pdata, pdata, 1L*length*Sizeof.INT, cudaMemcpyKind.cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - cudaMemcpy(out.pdata.withByteOffset(1L*length*Sizeof.INT), a.pdata, 1L*a.length*Sizeof.INT, cudaMemcpyKind.cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - out - } - - def vertcat(a:GIMat, omat:Mat) = { - if (ncols != a.ncols) - throw new RuntimeException("GMat on row dims not equal") - val out = GIMat.newOrCheckGIMat(nrows+a.nrows, ncols, omat, GUID, a.GUID, "vertcat".##) - cudaMemcpy2D(out.pdata, 1L*out.nrows*Sizeof.INT, pdata, 1L*nrows*Sizeof.INT, 1L*nrows*Sizeof.INT, 1L*ncols, cudaMemcpyKind.cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - cudaMemcpy2D(out.pdata.withByteOffset(1L*nrows*Sizeof.INT), 1L*out.nrows*Sizeof.INT, a.pdata, 1L*a.nrows*Sizeof.INT, 1L*a.nrows*Sizeof.INT, 1L*a.ncols, cudaMemcpyKind.cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - out - } - - def GIop(aa:IMat, oldmat:Mat, op:Int):GIMat = { - val a = GIMat(aa); - val (nr, nc, nra, nca) = ND.compatibleGDims(_dims, aa._dims, "DenseMat Op"); - val dims = ND.maxDims(_dims, aa._dims); - val out = GIMat.newOrCheckGIMat(dims, oldmat, GUID, aa.GUID, op.hashCode); - Mat.nflops += scala.math.max(length, a.length); - val err = CUMAT.applyiop(pdata, nr, nc, a.pdata, nra, nca, out.pdata, op); - if (err != 0) {throw new RuntimeException("CUDA kernel error %d in CUMAT.applyiop" format err)} - out - } - - override def recycle(nr:Int, nc:Int, nnz:Int):GIMat = { - if (nrows == nr && nc == ncols) { - this - } else if (realsize >= nr*nc) { - new GIMat(nr, nc, pdata, realsize) - } else { - free - GIMat(nr, nc) - } - } - - def toIMat():IMat = { - val out = IMat.newOrCheckIMat(nrows, ncols, null, GUID, "toIMat".##) - cudaMemcpy(Pointer.to(out.data), pdata, 1L*nrows*ncols * Sizeof.INT, cudaMemcpyKind.cudaMemcpyDeviceToHost); - cudaStreamSynchronize(Mat.SyncMethod) - out - } - - def toFMatRaw(omat:Mat):FMat = { - val out = FMat.newOrCheckFMat(nrows, ncols, omat, GUID, "toFMat".##) - cudaMemcpy(Pointer.to(out.data), pdata, 1L*nrows*ncols * Sizeof.INT, cudaMemcpyKind.cudaMemcpyDeviceToHost); - cudaStreamSynchronize(Mat.SyncMethod) - var i = 0; - val len = out.length - while (i < len) { - val ival = java.lang.Float.floatToRawIntBits(out.data(i)); - out.data(i) = ival.toFloat; - i += 1; - } - out - } - - def toFMat(omat:Mat):FMat = { - val out = FMat.newOrCheckFMat(dims, omat, GUID, "toFMat".##); - val a = IMat.newOrCheckIMat(dims, null, GUID, "toFMat2".##); - cudaMemcpy(Pointer.to(a.data), pdata, 1L*nrows*ncols * Sizeof.INT, cudaMemcpyKind.cudaMemcpyDeviceToHost); - cudaStreamSynchronize(Mat.SyncMethod) - var i = 0; - val len = out.length - while (i < len) { - out.data(i) = a.data(i).toFloat; - i += 1; - } - out - } - - def toLMatRaw():LMat = { - val out = LMat.newOrCheckLMat(nrows/2, ncols, null, GUID, "toLMatRaw".##); - cudaMemcpy(Pointer.to(out.data), pdata, 1L*length * Sizeof.INT, cudaMemcpyKind.cudaMemcpyDeviceToHost); - cudaStreamSynchronize(Mat.SyncMethod); - out - } - - def toLMat(omat:Mat):LMat = { - val out = LMat.newOrCheckLMat(dims, omat, GUID, "toLMat".##); - val a = IMat.newOrCheckIMat(dims, null, GUID, "toLMat2".##); - cudaMemcpy(Pointer.to(a.data), pdata, 1L*nrows*ncols * Sizeof.INT, cudaMemcpyKind.cudaMemcpyDeviceToHost); - cudaStreamSynchronize(Mat.SyncMethod) - var i = 0; - val len = out.length - while (i < len) { - out.data(i) = a.data(i); - i += 1; - } - out - } - - def copyTo(out:IMat):IMat = { - val a = out.recycle(nrows, ncols, 0) - cudaMemcpy(Pointer.to(a.data), pdata, 1L*nrows*ncols * Sizeof.INT, cudaMemcpyKind.cudaMemcpyDeviceToHost); - cudaStreamSynchronize(Mat.SyncMethod) - a - } - - def copyFrom(in:IMat):GIMat = { - cudaMemcpy(pdata, Pointer.to(in.data), nrows*ncols*Sizeof.INT, cudaMemcpyKind.cudaMemcpyHostToDevice); - cudaStreamSynchronize(Mat.SyncMethod) - this - } - - def copyTo(a:GMat):GMat = { - ND.checkDims("GIMat copyTo GMat", dims, a.dims); - val err = CUMAT.intToFloat(pdata, a.pdata, length); - cudaStreamSynchronize(Mat.SyncMethod); - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("error in copyTo " + cudaGetErrorString(err)) - } - a - } - - def copyTo(out:GIMat):GIMat = { - val a = out.recycle(nrows, ncols, 0) - cudaMemcpy(a.pdata, pdata, length*Sizeof.INT, cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - a - } - - override def copyTo(out:Mat):Mat = { - out match { - case a:GIMat => copyTo(a) - case a:GMat => copyTo(a) - case a:IMat => copyTo(a) - } - } - - override def free() = { - if (pdata == null) throw new RuntimeException("attempt to free an already free'd GIMat") - cudaFree(pdata); - this - } - - override def getdiag():GIMat = { - if (nrows != ncols) throw new RuntimeException("getdiag requires a square matrix, but dims= %d %d" format (nrows, ncols)) - val out = GIMat.newOrCheckGIMat(nrows, 1, null, GUID, "getdiag".##) - cudaMemcpy2D(out.pdata, Sizeof.INT, pdata, (nrows+1)*Sizeof.INT, Sizeof.INT, nrows, cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - val err = cudaGetLastError() - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in getdiag " + cudaGetErrorString(err)) - } - out - } - - override def mkdiag():GIMat = { - if (math.min(nrows, ncols) != 1) throw new RuntimeException("mkdiag requires a vector argument, but dims= %d %d" format (nrows, ncols)) - val size = math.max(nrows, ncols) - val out = GIMat.newOrCheckGIMat(size, size, null, GUID, "mkdiag".##) - out.clear - var err = cudaMemcpy2D(out.pdata, (nrows+1)*Sizeof.INT, pdata, Sizeof.INT, Sizeof.INT, nrows, cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - if (err == 0) err = cudaGetLastError() - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in mkdiag " + cudaGetErrorString(err)) - } - out - } - - def cumsumByKey(keys:GIMat, omat:Mat):GIMat = { - if (nrows != keys.nrows || ncols != keys.ncols) - throw new RuntimeException("cumsumKey dimensions mismatch"); - val out = GIMat.newOrCheckGIMat(nrows, ncols, omat, GUID, keys.GUID, "cumsumKey".##); - Mat.nflops += 2L*length; - if (nrows == 1 || ncols == 1) { - val err = CUMAT.cumsumByKeyII(pdata, keys.pdata, out.pdata, llength); - if (err != 0) throw new RuntimeException("CUMAT.cumsumByKeyII error " + cudaGetErrorString(err)); - } else { - val tmp = GLMat(nrows, ncols); - var err = CUMAT.embedmat2d(keys.pdata, tmp.pdata, nrows, ncols, 0); - if (err != 0) throw new RuntimeException("CUMAT.embedmat2d error " + cudaGetErrorString(err)); - if (err == 0) err = CUMAT.cumsumByKeyFL(pdata, tmp.pdata, out.pdata, llength); - if (err != 0) throw new RuntimeException("CUMAT.cumsumByKeyFL error " + cudaGetErrorString(err)); - tmp.free; - } - out - } - - def cumsumByKey(keys:GMat, omat:Mat):GIMat = { - if (nrows != keys.nrows || ncols != keys.ncols) - throw new RuntimeException("cumsumKey dimensions mismatch"); - val out = GIMat.newOrCheckGIMat(nrows, ncols, omat, GUID, keys.GUID, "cumsumKey".##); - Mat.nflops += 2L*length; - if (nrows == 1 || ncols == 1) { - val err = CUMAT.cumsumByKeyII(pdata, keys.pdata, out.pdata, llength); - if (err != 0) throw new RuntimeException("CUMAT.cumsumByKeyII error " + cudaGetErrorString(err)); - } else { - val tmp = GLMat(nrows, ncols); - var err = CUMAT.embedmat2d(keys.pdata, tmp.pdata, nrows, ncols, 0); - if (err != 0) throw new RuntimeException("CUMAT.embedmat error " + cudaGetErrorString(err)); - if (err == 0) err = CUMAT.cumsumByKeyIL(pdata, tmp.pdata, out.pdata, llength); - if (err != 0) throw new RuntimeException("CUMAT.cumsumByKeyIL error " + cudaGetErrorString(err)); - tmp.free; - } - out - } - - def cumsumByKey(keys:GIMat):GIMat = cumsumByKey(keys, null); - - def cumsumByKey(keys:GMat):GIMat = cumsumByKey(keys, null); - - def cummaxByKey(keys:GIMat, omat:Mat):GIMat = { - if (nrows != keys.nrows || ncols != keys.ncols) - throw new RuntimeException("cummaxKey dimensions mismatch"); - val out = GIMat.newOrCheckGIMat(nrows, ncols, omat, GUID, keys.GUID, "cummaxKey".##); - Mat.nflops += 2L*length; - if (nrows == 1 || ncols == 1) { - val err = CUMAT.cummaxByKeyII(pdata, keys.pdata, out.pdata, llength); - if (err != 0) { - throw new RuntimeException("CUMAT.cummaxByKey error " + cudaGetErrorString(err)) - } - } else { - val tmp = GLMat(nrows, ncols); - var err = CUMAT.embedmat2d(keys.pdata, tmp.pdata, nrows, ncols, 0); - if (err == 0) err = CUMAT.cummaxByKeyIL(pdata, tmp.pdata, out.pdata, llength); - if (err != 0) { - throw new RuntimeException("CUMAT.cummaxByKey error " + cudaGetErrorString(err)) - } - tmp.free; - } - out - } - - def cummaxByKey(keys:GMat, omat:Mat):GIMat = { - if (nrows != keys.nrows || ncols != keys.ncols) - throw new RuntimeException("cummaxKey dimensions mismatch"); - val out = GIMat.newOrCheckGIMat(nrows, ncols, omat, GUID, keys.GUID, "cummaxKey".##); - Mat.nflops += 2L*length; - if (nrows == 1 || ncols == 1) { - val err = CUMAT.cummaxByKeyII(pdata, keys.pdata, out.pdata, llength); - if (err != 0) { - throw new RuntimeException("CUMAT.cummaxByKey error " + cudaGetErrorString(err)) - } - } else { - val tmp = GLMat(nrows, ncols); - var err = CUMAT.embedmat2d(keys.pdata, tmp.pdata, nrows, ncols, 0); - if (err == 0) err = CUMAT.cummaxByKeyFL(pdata, tmp.pdata, out.pdata, llength); - if (err != 0) { - throw new RuntimeException("CUMAT.cummaxByKey error " + cudaGetErrorString(err)) - } - tmp.free; - } - out - } - - def cummaxByKey(keys:GMat):GIMat = cummaxByKey(keys, null); - - def cummaxByKey(keys:GIMat):GIMat = cummaxByKey(keys, null); - - def cumminByKey(keys:GMat, omat:Mat):GIMat = { - if (nrows != keys.nrows || ncols != keys.ncols) - throw new RuntimeException("cumminKey dimensions mismatch"); - val out = GIMat.newOrCheckGIMat(nrows, ncols, omat, GUID, keys.GUID, "cumminKey".##); - Mat.nflops += 2L*length; - if (nrows == 1 || ncols == 1) { - val err = CUMAT.cumminByKeyII(pdata, keys.pdata, out.pdata, llength); - if (err != 0) { - throw new RuntimeException("CUMAT.cumminByKey error " + cudaGetErrorString(err)) - } - } else { - val tmp = GLMat(nrows, ncols); - var err = CUMAT.embedmat2d(keys.pdata, tmp.pdata, nrows, ncols, 0); - if (err == 0) err = CUMAT.cumminByKeyIL(pdata, tmp.pdata, out.pdata, llength); - if (err != 0) { - throw new RuntimeException("CUMAT.cumminByKey error " + cudaGetErrorString(err)) - } - tmp.free; - } - out - } - - def cumminByKey(keys:GIMat, omat:Mat):GIMat = { - if (nrows != keys.nrows || ncols != keys.ncols) - throw new RuntimeException("cumminKey dimensions mismatch"); - val out = GIMat.newOrCheckGIMat(nrows, ncols, omat, GUID, keys.GUID, "cumminKey".##); - Mat.nflops += 2L*length; - if (nrows == 1 || ncols == 1) { - val err = CUMAT.cumminByKeyII(pdata, keys.pdata, out.pdata, llength); - if (err != 0) { - throw new RuntimeException("CUMAT.cumminByKey error " + cudaGetErrorString(err)) - } - } else { - val tmp = GLMat(nrows, ncols); - var err = CUMAT.embedmat2d(keys.pdata, tmp.pdata, nrows, ncols, 0); - if (err == 0) err = CUMAT.cumminByKeyIL(pdata, tmp.pdata, out.pdata, llength); - if (err != 0) { - throw new RuntimeException("CUMAT.cumminByKey error " + cudaGetErrorString(err)) - } - tmp.free; - } - out - } - - def cumminByKey(keys:GMat):GIMat = cumminByKey(keys, null); - - def cumminByKey(keys:GIMat):GIMat = cumminByKey(keys, null); - - - override def _reverse(omat:Mat):GIMat = { - val out = GIMat.newOrCheckGIMat(nrows, ncols, omat, GUID, "reverse".##); - val err = CUMAT.reverse(pdata, out.pdata, llength); - if (err != 0) throw new RuntimeException("CUMAT.reverse error " + cudaGetErrorString(err)); - out - } - - override def reverse:GIMat = _reverse(null); - - override def reverse(omat:Mat):GIMat = _reverse(omat); - - def reduce(inds0:Array[Int], fctn:(GIMat,Int)=>GIMat, fred:(Pointer, Pointer, Int, Int, Int)=>Int, opname:String):GIMat = { - var i = 1; - while (i < inds0.length) { - if (inds0(i-1) >= inds0(i)) { - throw new RuntimeException("GIMat reduce bad index vector"); - } - i += 1; - } - var inmat = this; - var outmat = this; - var inds = MatFunctions.irow(inds0); - var nextinds = getNextInds(inds); - var restinds = inds; - while (nextinds.asInstanceOf[AnyRef] != null) { - restinds = if (restinds.length > nextinds.length) restinds.colslice(0, restinds.length-nextinds.length) else null; - val outdims = inmat.dims.copy; - outdims(nextinds) = 1; - var n = 1; - for (i <- nextinds(0) to nextinds(nextinds.length-1)) n *= inmat.dims(i); - var k = 1; - for (i <- (nextinds(nextinds.length-1)+1) until inmat.dims.length) k *= inmat.dims(i); - if (nextinds(0) == 0) { - val tmpin = inmat.reshapeView(n, k); - val tmpout = fctn(tmpin,1); - outmat = tmpout.reshapeView(outdims); - } else { - outmat = GIMat.newOrCheckGIMat(outdims, null, inmat.GUID, ND.hashInts(outdims.data), "GMat_reduce".##); - var m = 1; - for (i <- 0 until nextinds(0)) m *= inmat.dims(i); - fred(inmat.pdata, outmat.pdata, m, n, k); - Mat.nflops += inmat.length; - } - nextinds = getNextInds(restinds); - inmat = outmat; - } - outmat; - } - - override def sum(inds:Array[Int]):IMat = reduce(inds, (a:GIMat, dir:Int) => GIFunctions.sum(a,dir,null), CUMAT.sumTensorI, "sum"); - override def prod(inds:Array[Int]):IMat = reduce(inds, (a:GIMat, dir:Int) => GIFunctions.prod(a,dir,null), CUMAT.prodTensorI, "prod"); - override def maxi(inds:Array[Int]):IMat = reduce(inds, (a:GIMat, dir:Int) => GIFunctions.maxi(a,dir,null), CUMAT.maxTensorI, "maxi") - override def mini(inds:Array[Int]):IMat = reduce(inds, (a:GIMat, dir:Int) => GIFunctions.mini(a,dir,null), CUMAT.minTensorI, "mini") - override def amax(inds:Array[Int]):IMat = reduce(inds, (a:GIMat, dir:Int) => GIFunctions.maxi(a,dir,null), CUMAT.maxTensorI, "amax") - override def amin(inds:Array[Int]):IMat = reduce(inds, (a:GIMat, dir:Int) => GIFunctions.mini(a,dir,null), CUMAT.minTensorI,"amin") - - override def sum(inds:IMat):IMat = reduce(inds.data, (a:GIMat, dir:Int) => GIFunctions.sum(a,dir,null), CUMAT.sumTensorI, "sum"); - override def prod(inds:IMat):IMat = reduce(inds.data, (a:GIMat, dir:Int) => GIFunctions.prod(a,dir,null), CUMAT.prodTensorI, "prod"); - override def maxi(inds:IMat):IMat = reduce(inds.data, (a:GIMat, dir:Int) => GIFunctions.maxi(a,dir,null), CUMAT.maxTensorI, "maxi") - override def mini(inds:IMat):IMat = reduce(inds.data, (a:GIMat, dir:Int) => GIFunctions.mini(a,dir,null), CUMAT.minTensorI, "mini") - override def amax(inds:IMat):IMat = reduce(inds.data, (a:GIMat, dir:Int) => GIFunctions.maxi(a,dir,null), CUMAT.maxTensorI, "amax") - override def amin(inds:IMat):IMat = reduce(inds.data, (a:GIMat, dir:Int) => GIFunctions.mini(a,dir,null), CUMAT.minTensorI,"amin") - - - override def unary_- () = GIop(GIMat(-1), null, 2) - def + (a : GIMat) = GIop(a, null, op_add) - def - (a : GIMat) = GIop(a, null, op_sub) - def *@ (a : GIMat) = GIop(a, null, op_mul) - def ∘ (a : GIMat) = GIop(a, null, op_mul) - def / (a : GIMat) = GIop(a, null, op_div) - def kron (a : GIMat):GIMat = kron(a, null) - def ⊗ (b : GIMat) = kron(b, null) - def > (b : GIMat) = GIop(b, null, op_gt) - def < (b : GIMat) = GIop(b, null, op_lt) - def == (b : GIMat) = GIop(b, null, op_eq) - def === (b : GIMat) = GIop(b, null,op_eq) - def >= (b : GIMat) = GIop(b, null, op_ge) - def <= (b : GIMat) = GIop(b, null, op_le) - def != (b : GIMat) = GIop(b, null, op_ne) - def max (b : GIMat) = GIop(b, null, op_max) - def min (b : GIMat) = GIop(b, null, op_min) - - def on(a : GIMat) = vertcat(a, null) - def \ (a : GIMat) = horzcat(a, null) - - override def + (a : Float) = GIop(GIMat(a.toInt), null, op_add) - override def - (a : Float) = GIop(GIMat(a.toInt), null, op_sub) - override def *@ (a : Float) = GIop(GIMat(a.toInt), null, op_mul) - override def ∘ (a : Float) = GIop(GIMat(a.toInt), null, op_mul) - override def / (a : Float) = GIop(GIMat(a.toInt), null, op_div) - override def ^ (a : Float) = GIop(GIMat(a.toInt), null, op_pow) - - override def < (b : Float) = GIop(GIMat(b.toInt), null, op_lt); - override def > (b : Float) = GIop(GIMat(b.toInt), null, op_gt); - override def <= (b : Float) = GIop(GIMat(b.toInt), null, op_le); - override def >= (b : Float) = GIop(GIMat(b.toInt), null, op_ge); - override def == (b : Float) = GIop(GIMat(b.toInt), null, op_eq); - override def != (b : Float) = GIop(GIMat(b.toInt), null, op_ne); - override def max (b : Float) = GIop(GIMat(b.toInt), null, op_max); - override def min (b : Float) = GIop(GIMat(b.toInt), null, op_min); - - override def + (a : Double) = GIop(GIMat(a.toInt), null, op_add) - override def - (a : Double) = GIop(GIMat(a.toInt), null, op_sub) - override def *@ (a : Double) = GIop(GIMat(a.toInt), null, op_mul) - override def ∘ (a : Double) = GIop(GIMat(a.toInt), null, op_mul) - override def / (a : Double) = GIop(GIMat(a.toInt), null, op_div) - override def ^ (a : Double) = GIop(GIMat(a.toInt), null, op_pow) - - override def < (b : Double) = GIop(GIMat(b.toInt), null, op_lt) - override def > (b : Double) = GIop(GIMat(b.toInt), null, op_gt) - override def <= (b : Double) = GIop(GIMat(b.toInt), null, op_le) - override def >= (b : Double) = GIop(GIMat(b.toInt), null, op_ge) - override def == (b : Double) = GIop(GIMat(b.toInt), null, op_eq) - override def != (b : Double) = GIop(GIMat(b.toInt), null, op_ne) - override def max (b : Double) = GIop(GIMat(b.toInt), null, op_max); - override def min (b : Double) = GIop(GIMat(b.toInt), null, op_min); - - override def + (a : Int) = GIop(GIMat(a), null, op_add) - override def - (a : Int) = GIop(GIMat(a), null, op_sub) - override def *@ (a : Int) = GIop(GIMat(a), null, op_mul) - override def ∘ (a : Int) = GIop(GIMat(a), null, op_mul) - override def / (a : Int) = GIop(GIMat(a), null, op_div) - override def ^ (a : Int) = GIop(GIMat(a), null, op_pow) - - override def < (b : Int) = GIop(GIMat(b), null, op_lt) - override def > (b : Int) = GIop(GIMat(b), null, op_gt) - override def <= (b : Int) = GIop(GIMat(b), null, op_le) - override def >= (b : Int) = GIop(GIMat(b), null, op_ge) - override def == (b : Int) = GIop(GIMat(b), null, op_eq) - override def != (b : Int) = GIop(GIMat(b), null, op_ne) - override def max (b : Int) = GIop(GIMat(b), null, op_max); - override def min (b : Int) = GIop(GIMat(b), null, op_min); - - /* - * Operators whose second arg is generic. - */ - override def * (b : Mat) = Mop_Times.op(this, b, null) - override def *^ (b : Mat) = Mop_TimesT.op(this, b, null) - override def xT (b : Mat) = Mop_TimesT.op(this, b, null) - override def Tx (b : Mat) = Mop_TTimes.op(this, b, null) - override def ^* (b : Mat) = Mop_TTimes.op(this, b, null) - override def + (b : Mat) = Mop_Plus.op(this, b, null) - override def - (b : Mat) = Mop_Minus.op(this, b, null) - override def *@ (b : Mat) = Mop_ETimes.op(this, b, null) - override def ∘ (b : Mat) = Mop_ETimes.op(this, b, null) - override def / (b : Mat) = Mop_EDiv.op(this, b, null) - override def /< (b : Mat) = Mop_Div.op(this, b, null) - override def \\ (b : Mat) = Mop_RSolve.op(this, b, null) - override def ◁ (b : Mat) = Mop_Div.op(this, b, null) - override def ▷ (b : Mat) = Mop_RSolve.op(this, b, null) - override def ^ (b : Mat) = Mop_Pow.op(this, b, null) - override def ∙ (b : Mat) = Mop_Dot.op(this, b, null) - override def ∙→ (b : Mat) = Mop_Dotr.op(this, b, null) - override def dot (b : Mat) = Mop_Dot.op(this, b, null) - override def dotr (b : Mat) = Mop_Dotr.op(this, b, null) - override def \ (b : Mat) = Mop_HCat.op(this, b, null) - override def on (b : Mat) = Mop_VCat.op(this, b, null) - - override def > (b : Mat) = Mop_GT.op(this, b, null) - override def < (b : Mat) = Mop_LT.op(this, b, null) - override def >= (b : Mat) = Mop_GE.op(this, b, null) - override def <= (b : Mat) = Mop_LE.op(this, b, null) - override def == (b : Mat) = Mop_EQ.op(this, b, null) - override def === (b : Mat) = Mop_EQ.op(this, b, null) - override def != (b : Mat) = Mop_NE.op(this, b, null) - - - def ~ (b: GIMat) = new GIPair(this, b); - override def ~ (b: IMat):IPair = new GIPair(this, GIMat(b)); - override def ~ (b: Mat):Pair = new GIPair(this, GIMat(b)); - -} - -class GIPair (omat:Mat, override val mat:GIMat) extends IPair (omat, mat){ - import GIMat.BinOp._ - - override def t = { - val out = GIMat.newOrCheckGIMat(mat.ncols, mat.nrows, omat, mat.GUID, "pt".##) - CUMAT.transpose(mat.pdata, mat.nrows, out.pdata, mat.ncols, mat.nrows, mat.ncols) - out - } - def + (a : GIMat) = mat.GIop(a, omat, op_add) - def - (a : GIMat) = mat.GIop(a, omat, op_sub) - def *@ (a : GIMat) = mat.GIop(a, omat, op_mul) - def ∘ (a : GIMat) = mat.GIop(a, omat, op_mul) - def / (a : GIMat) = mat.GIop(a, omat, op_div) - def kron (a : GIMat) = mat.kron(a, omat) - def ⊗ (b : GIMat) = mat.kron(b, omat) - def > (b : GIMat) = mat.GIop(b, omat, op_gt) - def < (b : GIMat) = mat.GIop(b, omat, op_lt) - def == (b : GIMat) = mat.GIop(b, omat, op_eq) - def === (b : GIMat) = mat.GIop(b, omat, op_eq) - def >= (b : GIMat) = mat.GIop(b, omat, op_ge) - def <= (b : GIMat) = mat.GIop(b, omat, op_le) - def != (b : GIMat) = mat.GIop(b, omat, op_ne) - def max (b : GIMat) = mat.GIop(b, omat, op_max) - def min (b : GIMat) = mat.GIop(b, omat, op_min) - - def on(a : GIMat) = mat.vertcat(a, omat) - def \ (a : GIMat) = mat.horzcat(a, omat) - - override def + (a : Float) = mat.GIop(GIMat(a.toInt), omat, op_add) - override def - (a : Float) = mat.GIop(GIMat(a.toInt), omat, op_sub) - override def *@ (a : Float) = mat.GIop(GIMat(a.toInt), omat, op_mul) - override def ∘ (a : Float) = mat.GIop(GIMat(a.toInt), omat, op_mul) - override def / (a : Float) = mat.GIop(GIMat(a.toInt), omat, op_div) - override def ^ (a : Float) = mat.GIop(GIMat(a.toInt), omat, op_pow) - - override def < (b : Float) = mat.GIop(GIMat(b.toInt), omat, op_lt) - override def > (b : Float) = mat.GIop(GIMat(b.toInt), omat, op_gt) - override def <= (b : Float) = mat.GIop(GIMat(b.toInt), omat, op_le) - override def >= (b : Float) = mat.GIop(GIMat(b.toInt), omat, op_ge) - override def == (b : Float) = mat.GIop(GIMat(b.toInt), omat, op_eq) - override def != (b : Float) = mat.GIop(GIMat(b.toInt), omat, op_ne) - override def max (b : Float) = mat.GIop(GIMat(b.toInt), omat, op_max) - override def min (b : Float) = mat.GIop(GIMat(b.toInt), omat, op_min) - - - override def + (a : Double) = mat.GIop(GIMat(a.toInt), omat, op_add) - override def - (a : Double) = mat.GIop(GIMat(a.toInt), omat, op_sub) - override def *@ (a : Double) = mat.GIop(GIMat(a.toInt), omat, op_mul) - override def ∘ (a : Double) = mat.GIop(GIMat(a.toInt), omat, op_mul) - override def / (a : Double) = mat.GIop(GIMat(a.toInt), omat, op_div) - override def ^ (a : Double) = mat.GIop(GIMat(a.toInt), omat, op_pow) - - override def < (b : Double) = mat.GIop(GIMat(b.toInt), omat, op_lt) - override def > (b : Double) = mat.GIop(GIMat(b.toInt), omat, op_gt) - override def <= (b : Double) = mat.GIop(GIMat(b.toInt), omat, op_le) - override def >= (b : Double) = mat.GIop(GIMat(b.toInt), omat, op_ge) - override def == (b : Double) = mat.GIop(GIMat(b.toInt), omat, op_eq) - override def != (b : Double) = mat.GIop(GIMat(b.toInt), null, op_ne) - override def max (b : Double) = mat.GIop(GIMat(b.toInt), omat, op_max) - override def min (b : Double) = mat.GIop(GIMat(b.toInt), omat, op_min) - - override def + (a : Int) = mat.GIop(GIMat(a), omat, op_add) - override def - (a : Int) = mat.GIop(GIMat(a), omat, op_sub) - override def *@ (a : Int) = mat.GIop(GIMat(a), omat, op_mul) - override def ∘ (a : Int) = mat.GIop(GIMat(a), omat, op_mul) - override def / (a : Int) = mat.GIop(GIMat(a), omat, op_div) - override def ^ (a : Int) = mat.GIop(GIMat(a), omat, op_pow) - - override def != (b : Int) = mat.GIop(GIMat(b), omat, op_ne) - override def == (b : Int) = mat.GIop(GIMat(b), omat, op_eq) - override def >= (b : Int) = mat.GIop(GIMat(b), omat, op_ge) - override def <= (b : Int) = mat.GIop(GIMat(b), omat, op_le) - override def < (b : Int) = mat.GIop(GIMat(b), omat, op_lt) - override def > (b : Int) = mat.GIop(GIMat(b), omat, op_gt) - override def max (b : Int) = mat.GIop(GIMat(b), omat, op_max) - override def min (b : Int) = mat.GIop(GIMat(b), omat, op_min) - - - /* - * Generics - */ - override def * (b : Mat):Mat = Mop_Times.op(mat, b, omat) - override def xT (b : Mat):Mat = Mop_TimesT.op(mat, b, omat) - override def *^ (b : Mat):Mat = Mop_TimesT.op(mat, b, omat) - override def Tx (b : Mat):Mat = Mop_TTimes.op(mat, b, omat) - override def ^* (b : Mat):Mat = Mop_TTimes.op(mat, b, omat) - override def + (b : Mat):Mat = Mop_Plus.op(mat, b, omat) - override def - (b : Mat):Mat = Mop_Minus.op(mat, b, omat) - override def *@ (b : Mat):Mat = Mop_ETimes.op(mat, b, omat) - override def ∘ (b : Mat):Mat = Mop_ETimes.op(mat, b, omat) - override def / (b : Mat):Mat = Mop_EDiv.op(mat, b, omat) - override def ^ (b : Mat):Mat = Mop_Pow.op(mat, b, omat) - override def /< (b : Mat):Mat = Mop_Div.op(mat, b, omat) - override def \\ (b : Mat):Mat = Mop_RSolve.op(mat, b, omat) - override def ◁ (b : Mat):Mat = Mop_Div.op(mat, b, omat) - override def ▷ (b : Mat):Mat = Mop_RSolve.op(mat, b, omat) - override def ∙ (b : Mat) = Mop_Dot.op(mat, b, omat) - override def ∙→ (b : Mat) = Mop_Dotr.op(mat, b, omat) - override def dot (b : Mat) = Mop_Dot.op(mat, b, omat) - override def dotr(b : Mat) = Mop_Dotr.op(mat, b, omat) - override def \ (b : Mat):Mat = Mop_HCat.op(mat, b, omat) - override def on (b : Mat):Mat = Mop_VCat.op(mat, b, omat) - - override def > (b : Mat):Mat = Mop_GT.op(mat, b, omat) - override def < (b : Mat):Mat = Mop_LT.op(mat, b, omat) - override def >= (b : Mat):Mat = Mop_GE.op(mat, b, omat) - override def <= (b : Mat):Mat = Mop_LE.op(mat, b, omat) - override def == (b : Mat):Mat = Mop_EQ.op(mat, b, omat) - override def === (b : Mat):Mat = Mop_EQ.op(mat, b, omat) - override def != (b : Mat):Mat = Mop_NE.op(mat, b, omat) -} - -class GIMatWildcard extends GIMat(0,0,null,0) with MatrixWildcard - -object GIMat { - - object BinOp { - val op_add=0 - val op_sub=1 - val op_mul=2 - val op_div=3 - val op_gt=4 - val op_lt=5 - val op_eq=6 - val op_ge=7 - val op_le=8 - val op_ne=9 - val op_max=10 - val op_min=11 - val op_atan2=12 - val op_pow=13 - } - - def apply(nr:Int, nc:Int):GIMat = { - val retv = new GIMat(nr, nc, new Pointer(), nr*nc) - if (Mat.debugMem && (nr*nc>1)) { - println("GIMat %d %d, %d %f" format (nr, nc, SciFunctions.getGPU, SciFunctions.GPUmem._1)) - if (nr*nc > Mat.debugMemThreshold) throw new RuntimeException("GIMat alloc too large"); - } - JCublas.cublasAlloc(nr*nc, Sizeof.INT, retv.pdata) - retv - } - - val wildcard = new GIMatWildcard - - def apply(a:IMat):GIMat = { - a match { - case g:GIMat => g; - case aa:MatrixWildcard => GIMat.wildcard - case _ => { - val retv = GIMat.newOrCheckGIMat(a.dims, null, a.GUID, "GIMat".##) - val rsize = a.nrows*a.ncols - cudaMemcpy(retv.pdata, Pointer.to(a.data), 1L*rsize*Sizeof.INT, cudaMemcpyKind.cudaMemcpyHostToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - retv - } - } - } - - def make(dims:Array[Int]):GIMat = { - val len = dims.reduce(_*_); - val out = new GIMat(dims, new Pointer, len); - if (Mat.debugMem) { - println("GIMat %d, %d %f" format (len, SciFunctions.getGPU, SciFunctions.GPUmem._1)); - if (len > Mat.debugMemThreshold) throw new RuntimeException("GIMat alloc too large"); - } - cudaMalloc(out.pdata, 1L*len*Sizeof.INT); - cudaStreamSynchronize(Mat.SyncMethod); - out - } - - def make(dims:IMat):GIMat = make(dims.data) - - def apply(a:GMat):GIMat = { - val rsize = a.nrows*a.ncols - val retv = GIMat.newOrCheckGIMat(a.dims, null, a.GUID, "GIMat_GMat".##) - var err = CUMAT.floatToInt(a.pdata, retv.pdata, a.length) - cudaStreamSynchronize(Mat.SyncMethod) - if (err == 0) err = cudaGetLastError() - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("GIMat(GMat) error " + cudaGetErrorString(err)) - } - retv - } - - def apply(a:Mat):GIMat = a match { - case aa:GIMat => aa - case aa:IMat => GIMat(aa) - case aa:GMat => GIMat(aa) - case aa:FMat => GIMat(GMat(aa)) - } - - def apply(a:Int):GIMat = { - val out = GIMat.newOrCheckGIMat(1, 1, null, a.##, "GIMat_Int".##) - out.set(a) - out - } - - def elem(a:Int):GIMat = { - val out = GIMat.newOrCheckGIMat(1, 1, null, a.##, "GIMat_elem".##) - out.set(a) - out - } - - def ielem(a:Int) = elem(a); - - def izeros(m:Int, n:Int):GIMat = { - val out = GIMat(m,n) - out.clear - out - } - - def izeros(dims:IMat):GIMat = { - val out = make(dims); - out.clear; - out; - } - - def iones(m:Int, n:Int):GIMat = { - val out = GIMat(m,n) - out.set(1) - out - } - - def iones(dims:IMat):GIMat = { - val out = make(dims); - out.set(1); - out; - } - - def GPUtoGPUarraycopy(a:Pointer, aoffset:Int, b:Pointer, boffset:Int, len:Int, msg:String ) = { - cudaMemcpy(b.withByteOffset(1L*boffset*Sizeof.INT), a.withByteOffset(1L*aoffset*Sizeof.INT), 1L*len*Sizeof.INT, cudaMemcpyKind.cudaMemcpyDeviceToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError; - if (err != 0) throw new RuntimeException(msg +" error in memcpy "+ cudaGetErrorString(err)); - } - - def GPUtoCPUarraycopy(a:Pointer, aoffset:Int, b:Array[Int], boffset:Int, len:Int, msg:String ) = { - cudaMemcpy(Pointer.to(b).withByteOffset(1L*boffset*Sizeof.INT), a.withByteOffset(1L*aoffset*Sizeof.INT), 1L*len*Sizeof.INT, cudaMemcpyKind.cudaMemcpyDeviceToHost); - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError; - if (err != 0) throw new RuntimeException(msg +" error in memcpy "+ cudaGetErrorString(err)); - } - - def CPUtoGPUarraycopy(a:Array[Int], aoffset:Int, b:Pointer, boffset:Int, len:Int, msg:String ) = { - cudaMemcpy(b.withByteOffset(1L*boffset*Sizeof.INT), Pointer.to(a).withByteOffset(1L*aoffset*Sizeof.INT), 1L*len*Sizeof.INT, cudaMemcpyKind.cudaMemcpyHostToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError; - if (err != 0) throw new RuntimeException(msg +" error in memcpy "+ cudaGetErrorString(err)); - } - - - - def newOrCheckGIMat(nr:Int, nc:Int, oldmat:Mat):GIMat = { - if (oldmat.asInstanceOf[AnyRef] == null || (oldmat.nrows == 0 && oldmat.ncols == 0)) { - GIMat(nr, nc) - } else { - if (oldmat.nrows != nr || oldmat.ncols != nc) { - oldmat.recycle(nr, nc, 0).asInstanceOf[GIMat] - } else { - oldmat.asInstanceOf[GIMat] - } - } - } - - def newOrCheckGIMat(dims:Array[Int], out:Mat):GIMat = { - if (out.asInstanceOf[AnyRef] != null && ND.compareDims( out.dims.data, dims)) { - out.asInstanceOf[GIMat] - } else { - GIMat.make(dims) - } - } - - def newOrCheckGIMat(dims:IMat, out:Mat):GIMat = newOrCheckGIMat(dims.data, out); - - def newOrCheckGIMat(nr:Int, nc:Int, outmat:Mat, matGuid:Long, opHash:Int):GIMat = { - if (outmat.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGIMat(nr, nc, outmat) - } else { - val key = (matGuid, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache3(key) - if (res != null) { - newOrCheckGIMat(nr, nc, res) - } else { - val omat = newOrCheckGIMat(nr, nc, null) - Mat.cache3put(key, omat) - omat - } - } - } - - def newOrCheckGIMat(dims:Array[Int], out:Mat, matGuid:Long, opHash:Int):GIMat = { - if (out.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGIMat(dims, out) - } else { - val key = (matGuid, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache3(key) - if (res != null) { - newOrCheckGIMat(dims, res) - } else { - val omat = newOrCheckGIMat(dims, null) - Mat.cache3put(key, omat) - omat - } - } - } - - def newOrCheckGIMat(dims:IMat, out:Mat, g1:Long, opHash:Int):GIMat = newOrCheckGIMat(dims.data, out, g1, opHash); - - - def newOrCheckGIMat(nr:Int, nc:Int, outmat:Mat, guid1:Long, guid2:Long, opHash:Int):GIMat = { - if (outmat.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGIMat(nr, nc, outmat) - } else { - val key = (guid1, guid2, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache4(key) - if (res != null) { - newOrCheckGIMat(nr, nc, res) - } else { - val omat = newOrCheckGIMat(nr, nc, null) - Mat.cache4put(key, omat) - omat - } - } - } - - def newOrCheckGIMat(dims:Array[Int], out:Mat, guid1:Long, guid2:Long, opHash:Int):GIMat = { - if (out.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGIMat(dims, out) - } else { - val key = (guid1, guid2, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache4(key) - if (res != null) { - newOrCheckGIMat(dims, res) - } else { - val omat = newOrCheckGIMat(dims, null) - Mat.cache4put(key, omat) - omat - } - } - } - - def newOrCheckGIMat(dims:IMat, out:Mat, g1:Long, g2:Long, opHash:Int):GIMat = newOrCheckGIMat(dims.data, out, g1, g2, opHash); - - - def newOrCheckGIMat(nr:Int, nc:Int, outmat:Mat, guid1:Long, guid2:Long, guid3:Long, opHash:Int):GIMat = { - if (outmat.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGIMat(nr, nc, outmat) - } else { - val key = (guid1, guid2, guid3, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache5(key) - if (res != null) { - newOrCheckGIMat(nr, nc, res) - } else { - val omat = newOrCheckGIMat(nr, nc, null) - Mat.cache5put(key, omat) - omat - } - } - } - - def newOrCheckGIMat(dims:Array[Int], out:Mat, g1:Long, g2:Long, g3:Long, opHash:Int):GIMat = { - if (out.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGIMat(dims, out) - } else { - val key = (g1, g2, g3, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache5(key) - if (res != null) { - newOrCheckGIMat(dims, res) - } else { - val omat = newOrCheckGIMat(dims, null) - Mat.cache5put(key, omat) - omat - } - } - } - - def newOrCheckGIMat(dims:IMat, out:Mat, g1:Long, g2:Long, g3:Long, opHash:Int):GIMat = newOrCheckGIMat(dims.data, out, g1, g2, g3, opHash); - - - def newOrCheckGIMat(nr:Int, nc:Int, outmat:Mat, guid1:Long, guid2:Long, guid3:Long, guid4:Long, opHash:Int):GIMat = { - if (outmat.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGIMat(nr, nc, outmat) - } else { - val key = (guid1, guid2, guid3, guid4, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache6(key) - if (res != null) { - newOrCheckGIMat(nr, nc, res) - } else { - val omat = newOrCheckGIMat(nr, nc, null) - Mat.cache6put(key, omat) - omat - } - } - } - - def newOrCheckGIMat(dims:Array[Int], out:Mat, g1:Long, g2:Long, g3:Long, g4:Long, opHash:Int):GIMat = { - if (out.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGIMat(dims, out) - } else { - val key = (g1, g2, g3, g4, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache6(key) - if (res != null) { - newOrCheckGIMat(dims, res) - } else { - val omat = newOrCheckGIMat(dims, null) - Mat.cache6put(key, omat) - omat - } - } - } - - def newOrCheckGIMat(dims:IMat, out:Mat, g1:Long, g2:Long, g3:Long, g4:Long, opHash:Int):GIMat = newOrCheckGIMat(dims.data, out, g1, g2, g3, g4, opHash); - -} - - - - - - - - diff --git a/src/main/scala/BIDMat/GLFunctions.scala b/src/main/scala/BIDMat/GLFunctions.scala deleted file mode 100644 index 8d0af69e..00000000 --- a/src/main/scala/BIDMat/GLFunctions.scala +++ /dev/null @@ -1,190 +0,0 @@ -package BIDMat - -import java.util.Random._; -import org.apache.commons.math3.special._ -import org.apache.commons.math3.util.FastMath -import org.apache.commons.math3.random.RandomDataGenerator; -import scala.concurrent.Future -import scala.concurrent.ExecutionContext.Implicits.global -import edu.berkeley.bid.MurmurHash3.MurmurHash3_x64_64 -import edu.berkeley.bid.CUMAT -import edu.berkeley.bid.SLATEC; -import GMat.BinOp -import GMat.TransF -import GMat.TransF2 -import SciState._; -import jcuda._ -import jcuda.runtime._ -import jcuda.runtime.JCuda._ -import jcuda.runtime.cudaMemcpyKind._ -import jcuda.jcublas._ -import jcuda.jcublas.JCublas._ -import jcuda.jcusparse._ - - -object GLFunctions { - import GMat.BinOp._ - - def max(a:GLMat, b:GLMat, out:Mat):GLMat = a.GIop(b, out, op_max) - def min(a:GLMat, b:GLMat, out:Mat):GLMat = a.GIop(b, out, op_min) - - def maxi(a:GLMat, dir:Int, out:Mat):GLMat = a.reduceOp(out, dir, Int.MinValue, BinOp.op_max); - def mini(a:GLMat, dir:Int, out:Mat):GLMat = a.reduceOp(out, dir, Int.MaxValue, BinOp.op_min); - def sum(a:GLMat, dir:Int, out:Mat):GLMat = a.reduceOp(out, dir, 0, BinOp.op_add); - def prod(a:GLMat, dir:Int, out:Mat):GLMat = a.reduceOp(out, dir, 1, BinOp.op_mul); - - def accumIJ(I:GIMat, J:GIMat, V:GLMat, omat:Mat, nrows:Int, ncols:Int):GLMat = { - val out = GLMat.newOrCheckGLMat(nrows, ncols, omat, I.GUID, J.GUID, V.GUID, "GLMat_accum".##) - out.clear - if (I.length != J.length || I.length != V.length) { - throw new RuntimeException("GLMat accum: index lengths dont match") - } - val err = CUMAT.laccum(I.pdata, J.pdata, V.pdata, out.pdata, I.length, nrows); - if (err != 0) throw new RuntimeException("CUMAT.laccum error " + cudaGetErrorString(err)); - Mat.nflops += I.length - out - } - - def accumIJ(I:Int, J:GIMat, V:GLMat, omat:Mat, nrows:Int, ncols:Int):GLMat = { - val out = GLMat.newOrCheckGLMat(nrows, ncols, omat, I, J.GUID, V.GUID, "GLMat_accumI".##) - out.clear - if (J.length != V.length) { - throw new RuntimeException("GLMat accum: index lengths dont match") - } - val err = CUMAT.laccumI(I, J.pdata, V.pdata, out.pdata, J.length, nrows); - if (err != 0) throw new RuntimeException("CUMAT.laccumI error " + cudaGetErrorString(err)); - Mat.nflops += J.length - out - } - - def accumIJ(I:GIMat, J:Int, V:GLMat, omat:Mat, nrows:Int, ncols:Int):GLMat = { - val out = GLMat.newOrCheckGLMat(nrows, ncols, omat, I.GUID, J, V.GUID, "GLMat_accumJ".##) - out.clear - if (I.length != V.length) { - throw new RuntimeException("GLMat accum: index lengths dont match") - } - val err = CUMAT.laccumJ(I.pdata, J, V.pdata, out.pdata, I.length, nrows); - if (err != 0) throw new RuntimeException("CUMAT.laccumJ error " + cudaGetErrorString(err)); - Mat.nflops += I.length - out - } - - def accumIJ(I:GIMat, J:GIMat, V:Long, omat:Mat, nrows:Int, ncols:Int):GLMat = { - val out = GLMat.newOrCheckGLMat(nrows, ncols, omat, I.GUID, J.GUID, V.hashCode, "GLMat_accumV".##) - out.clear - if (I.length != J.length) { - throw new RuntimeException("GLMat accum: index lengths dont match") - } - val err = CUMAT.laccumV(I.pdata, J.pdata, V, out.pdata, I.length, nrows); - if (err != 0) throw new RuntimeException("CUMAT.laccumV error " + cudaGetErrorString(err)); - Mat.nflops += I.length - out - } - - def accumIJ(I:Int, J:GIMat, V:Long, omat:Mat, nrows:Int, ncols:Int):GLMat = { - val out = GLMat.newOrCheckGLMat(nrows, ncols, omat, I, J.GUID, V.hashCode, "GLMat_accumIV".##) - out.clear - val err = CUMAT.laccumIV(I, J.pdata, V, out.pdata, J.length, nrows); - if (err != 0) throw new RuntimeException("CUMAT.laccumIV error " + cudaGetErrorString(err)); - Mat.nflops += J.length - out - } - - def accumIJ(I:GIMat, J:Int, V:Long, omat:Mat, nrows:Int, ncols:Int):GLMat = { - val out = GLMat.newOrCheckGLMat(nrows, ncols, omat, I.GUID, J, V.hashCode, "GLMat_accumJV".##) - out.clear - val err = CUMAT.laccumJV(I.pdata, J, V, out.pdata, I.length, nrows); - if (err != 0) throw new RuntimeException("CUMAT.laccumJV error " + cudaGetErrorString(err)); - Mat.nflops += I.length - out - } - - def accum(IJ:GIMat, V:GLMat, omat:Mat, nrows:Int, ncols:Int):GLMat = { - if (IJ.nrows != V.length || IJ.ncols > 2) { - throw new RuntimeException("GLMat accum: index lengths dont match") - } - val out = GLMat.newOrCheckGLMat(nrows, ncols, omat, IJ.GUID, V.GUID, "GLMat_accumIJ".##) - out.clear - if (IJ.ncols == 2) { - val err = CUMAT.laccum(IJ.pdata, IJ.pdata.withByteOffset(1L*IJ.nrows*Sizeof.INT), V.pdata, out.pdata, V.length, nrows); - if (err != 0) throw new RuntimeException("CUMAT.laccum error " + cudaGetErrorString(err)); - } else { - val err= CUMAT.laccumJ(IJ.pdata, 0, V.pdata, out.pdata, V.length, nrows); - if (err != 0) throw new RuntimeException("CUMAT.laccumJ error " + cudaGetErrorString(err)); - } - Mat.nflops += V.length - out - } - - def accum(IJ:GIMat, V:Long, omat:Mat, nrows:Int, ncols:Int):GLMat = { - if (IJ.ncols > 2) { - throw new RuntimeException("GLMat accum: index lengths dont match") - } - val out = GLMat.newOrCheckGLMat(nrows, ncols, omat, IJ.GUID, V.hashCode, "GLMat_accumIJV".##) - out.clear - if (IJ.ncols == 2) { - val err = CUMAT.laccumV(IJ.pdata, IJ.pdata.withByteOffset(1L*IJ.nrows*Sizeof.INT), V, out.pdata, IJ.nrows, nrows); - if (err != 0) throw new RuntimeException("CUMAT.laccumV error " + cudaGetErrorString(err)); - } else { - val err = CUMAT.laccumJV(IJ.pdata, 0, V, out.pdata, IJ.nrows, nrows); - if (err != 0) throw new RuntimeException("CUMAT.laccumJV error " + cudaGetErrorString(err)); - } - Mat.nflops += IJ.nrows - out - } - - def maxi2(a:GLMat, omat:Mat, omati:Mat, dim0:Int):(GLMat, GIMat) = { - Mat.nflops += 1L * a.length - val dim = if (a.nrows == 1 && dim0 == 0) 2 else math.max(1, dim0) - if (dim == 1) { - val out = GLMat.newOrCheckGLMat(1, a.ncols, omat, a.GUID, "maxi2".##) - val outi = GIMat.newOrCheckGIMat(1, a.ncols, omati, a.GUID, "maxi2_1".##) - val err = CUMAT.maxil(a.pdata, out.pdata, outi.pdata, a.nrows, a.ncols, 1) - if (err != 0) throw new RuntimeException("maxi2 error %d: " + cudaGetErrorString(err) format err); - (out, outi) - } else if (dim == 2) { - val out = GLMat.newOrCheckGLMat(a.nrows, 1, omat, a.GUID, "maxi2".##) - val outi = GIMat.newOrCheckGIMat(a.nrows, 1, omati, a.GUID, "maxi2_1".##) - val err = CUMAT.maxil(a.pdata, out.pdata, outi.pdata, a.nrows, a.ncols, 2) - if (err != 0) throw new RuntimeException("maxi2 error %d: " + cudaGetErrorString(err) format err); - (out, outi) - } else { - throw new RuntimeException("maxi2 dimension not recognized %d" format dim0) - } - } - - def mini2(a:GLMat, omat:Mat, omati:Mat, dim0:Int):(GLMat, GIMat) = { - Mat.nflops += 1L * a.length - val dim = if (a.nrows == 1 && dim0 == 0) 2 else math.max(1, dim0) - if (dim == 1) { - val out = GLMat.newOrCheckGLMat(1, a.ncols, omat, a.GUID, "mini2".##) - val outi = GIMat.newOrCheckGIMat(1, a.ncols, omati, a.GUID, "mini2_1".##) - val err = CUMAT.minil(a.pdata, out.pdata, outi.pdata, a.nrows, a.ncols, 1) - if (err != 0) throw new RuntimeException("mini2 error %d: " + cudaGetErrorString(err) format err); - (out, outi) - } else if (dim == 2) { - val out = GLMat.newOrCheckGLMat(a.nrows, 1, omat, a.GUID, "maxi2".##) - val outi = GIMat.newOrCheckGIMat(a.nrows, 1, omati, a.GUID, "maxi2_1".##) - val err = CUMAT.minil(a.pdata, out.pdata, outi.pdata, a.nrows, a.ncols, 2) - if (err != 0) throw new RuntimeException("mini2 error %d: " + cudaGetErrorString(err) format err); - (out, outi) - } else { - throw new RuntimeException("mini2 direction not recognized %d" format dim) - } - } - - /* - def cumsum(a:GIMat, dim0:Int, omat:Mat):GIMat = { - Mat.nflops += 1L * a.length; - val dim = if (a.nrows == 1 && dim0 == 0) 2 else math.max(1, dim0); - if (dim == 1) { - val out = GIMat.newOrCheckGIMat(a.nrows, a.ncols, omat, a.GUID, "cumsum".##) - CUMAT.cumsumc(a.nrows, a.ncols, a.pdata, out.pdata) - out - } else { - throw new RuntimeException("Cumsum across rows not supported yet") - } - } - * */ - -} \ No newline at end of file diff --git a/src/main/scala/BIDMat/GLMat.scala b/src/main/scala/BIDMat/GLMat.scala deleted file mode 100755 index b5147ef8..00000000 --- a/src/main/scala/BIDMat/GLMat.scala +++ /dev/null @@ -1,1484 +0,0 @@ -package BIDMat -import jcuda._; -import jcuda.jcublas.JCublas; -import jcuda.runtime._ -import jcuda.runtime.JCuda._ -import jcuda.runtime.cudaMemcpyKind._ -import jcuda.runtime.cudaError._ -import edu.berkeley.bid.CUMAT -import edu.berkeley.bid.CUMATD -import scala.util.hashing.MurmurHash3 -import edu.berkeley.bid.MurmurHash3.MurmurHash3_x64_64 -import java.io._ - -class GLMat(dims0:Array[Int], @transient var pdata:Pointer, val realsize:Long) extends LMat(dims0, null) { - import GIMat.BinOp._ - - def this(nr:Int, nc:Int, pdata:Pointer, realsize:Long) = this(Array(nr, nc), pdata, realsize); - - override def toString:String = { - val (nr, nc) = if (nrows == 1) { - (1, math.min(ncols,20000)); - } else { - (math.min(nrows,10), math.min(ncols,50)); - } - if (nr * nc > 0) { - val tmpMat = LMat(nr, nc); - cudaMemcpy2D(Pointer.to(tmpMat.data), 1L*nr*Sizeof.LONG, pdata, 1L*nrows*Sizeof.LONG, 1L*nr*Sizeof.LONG, nc, cudaMemcpyDeviceToHost); - cudaStreamSynchronize(Mat.SyncMethod) - tmpMat.toString; - } else { - "" - } - } - - override def contents() = { - val out = new GLMat(length, 1, pdata, realsize); - out.setGUID(MurmurHash3.mix(MurmurHash3.mix(length, 1), (GUID*7897889).toInt)); - out - } - - override def dv:Double = - if (nrows > 1 || ncols > 1) { - throw new RuntimeException("Matrix should be 1x1 to extract value") - } else { - toLMat().data(0) - } - - override def reshapeView(newdims:Int*):GLMat = reshapeView(newdims.toArray) - - override def reshapeView(newdims:Array[Int]):GLMat = { - if (newdims.reduce(_*_) == length) { - val out = new GLMat(newdims, pdata, llength); - out.setGUID(MurmurHash3_x64_64(newdims.map(_.toLong) :+ GUID, "reshapeView".##)); - out - } else { - throw new RuntimeException("GLMat reshapeView total length doesnt match") - } - } - - override def reshapeView(adims:IMat):GLMat = reshapeView(adims.data); - - override def reshapeTrim(newdims:Int*):GLMat = reshapeTrim(newdims.toArray) - - override def reshapeTrim(newdims:Array[Int]):GLMat = { - if (newdims.reduce(_*_) <= realsize) { - val out = new GLMat(newdims, pdata, realsize); - out.setGUID(MurmurHash3_x64_64(newdims.map(_.toLong) :+ GUID, "reshapeTrim".##)); - out - } else { - throw new RuntimeException("GLMat reshapeTrim total length too large") - } - } - - override def reshapeTrim(adims:IMat):GLMat = reshapeTrim(adims.data); - - - override def mytype = "GLMat" - - override def nnz = length; - - /** hold indices in GPU mem */ - val ginds = new Array[GIMat](ndims); - - override def view(nr:Int, nc:Int):GLMat = { - if (1L * nr * nc > realsize) { - throw new RuntimeException("view dimensions too large") - } - if (nr == nrows && nc == ncols) { - this - } else { - val out = new GLMat(nr, nc, pdata, realsize); - out.setGUID(MurmurHash3.mix(MurmurHash3.mix(nr, nc), (GUID*3145341).toInt)); - out - } - } - - val myGPU = SciFunctions.getGPU - - var saveMe:LMat = null - - private def writeObject(out:ObjectOutputStream):Unit = { - saveMe = LMat(this); - out.defaultWriteObject(); - } - - private def readObject(in:ObjectInputStream):Unit = { - in.defaultReadObject(); - val gpu = SciFunctions.getGPU; - SciFunctions.setGPU(myGPU); - pdata = GLMat(saveMe).pdata; - SciFunctions.setGPU(gpu); - saveMe = null; - } - - def getIndxWrapper(i:Int):GIMat = { - if (ginds(i).asInstanceOf[AnyRef] == null) { - ginds(i) = GIMat(1,1) - } - ginds(i); - } - - def getIndexValue(i:Int, v:Int):GIMat = { - getIndxWrapper(i); - ginds(i).set(v); - ginds(i); - } - - def getIndexMat(i:Int, v:IMat):GIMat = { - if (v.length == 0 || v.length > 1) { - GIMat(v); - } else { - getIndxWrapper(i).set(v.v); - ginds(i); - } - } - - def safePointer(ind:GIMat):Pointer = { - if (ind.asInstanceOf[AnyRef] == null) { - GMat.nullPointer; - } else { - ind.pdata; - } - } - - /** 1D access */ - - override def apply(ind:Int):Long = { - val tmp = new Array[Long](1); - GLMat.GPUtoCPUarraycopy(pdata, ind, tmp, 0, 1, "GMat apply"); - tmp(0) - } - - /** 2D access */ - - override def apply(i:Int, j:Int):Long = { - val tmp = new Array[Long](1); - GLMat.GPUtoCPUarraycopy(pdata, i + nrows * j, tmp, 0, 1, "GMat apply"); - tmp(0) - } - - /** ND access */ - - override def applyv(inds:Array[Int]):Long = { - val indx = ND.linearize(inds, dims.data); - val tmp = new Array[Long](1); - GLMat.GPUtoCPUarraycopy(pdata, indx, tmp, 0, 1, "GMat apply"); - tmp(0); - } - - override def apply(i1:IMat, i2:IMat):GLMat = applyi(Array(i1, i2), null); - override def apply(i1:IMat, i2:Int):GLMat = applyi(Array(i1, IMat.ielem(i2)), null); - override def apply(i1:Int, i2:IMat):GLMat = applyi(Array(IMat.ielem(i1), i2), null); - - /** apply to an index IMat, and mirror its structure in the result */ - - override def apply(inds:IMat):GLMat = { - inds match { - case aa:MatrixWildcard => { - val out = GLMat.newOrCheckGLMat(length, 1, null, GUID, inds.GUID, "apply(?)".##); - GDMat.GPUtoGPUarraycopy(pdata, 0, out.pdata, 0, length, "GLMat IMat apply" ); - out - } - case _ => { - val newinds = getIndexMat(0, inds); - val out = GLMat.newOrCheckGLMat(inds.dims, null, GUID, inds.GUID, "apply IMat".##); - val err = CUMATD.copyFromInds(pdata, out.pdata, safePointer(newinds), inds.length); - if (err != 0) throw new RuntimeException("GLMat apply(I) error" + cudaGetErrorString(err)); - out; - } - } - } - - override def applyi(inds:Array[IMat]):GLMat = applyi(inds, null); - - def applyi(inds:Array[IMat], omat:Mat):GLMat = { - val newdims = new Array[Int](_dims.length) - val newinds = new Array[GIMat](_dims.length) - for (i <- 0 until _dims.length) { - inds(i) match { - case aa:MatrixWildcard => { - newdims(i) = _dims(i); - } - case _ => { - newdims(i) = inds(i).length; - newinds(i) = getIndexMat(i, inds(i)); - } - } - } - val out = GLMat.newOrCheckGLMat(newdims, omat, GUID, ND.hashGUIDs(inds), "apply".##); - inds.length match { - case 1 => { - val err = CUMATD.copyFromInds(pdata, out.pdata, safePointer(newinds(0)), newdims(0)); - if (err != 0) throw new RuntimeException("GLMat apply(I) error" + cudaGetErrorString(err)); - } - case 2 => { - val err = CUMATD.copyFromInds2D(pdata, dims(0), out.pdata, newdims(0), safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1)); - if (err != 0) throw new RuntimeException("GLMat apply(I, J) error" + cudaGetErrorString(err)); - } - case 3 => { - val err = CUMATD.copyFromInds3D(pdata, dims(0), dims(1), out.pdata, newdims(0), newdims(1), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1), safePointer(newinds(2)), newdims(2)); - if (err != 0) throw new RuntimeException("GLMat apply(I, J, K) error" + cudaGetErrorString(err)); - } - case 4 => { - val err = CUMATD.copyFromInds4D(pdata, dims(0), dims(1), dims(2), out.pdata, newdims(0), newdims(1), newdims(2), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1), safePointer(newinds(2)), newdims(2), safePointer(newinds(3)), newdims(3)); - if (err != 0) throw new RuntimeException("GLMat apply(I, J, K, L) error" + cudaGetErrorString(err)); - } - case _ => throw new RuntimeException("GLMat slice access with more than 4 indices not supported"); - } - out; - } - - /** 1D update */ - - override def update(i:Int, v:Long):GLMat = { - val tmp = new Array[Long](1); - tmp(0) = v; - GLMat.CPUtoGPUarraycopy(tmp, 0, pdata, i, 1, "GLMat update"); - this - } - - /** 2D update */ - - override def update(i:Int, j:Int, v:Long):GLMat = { - val tmp = new Array[Long](1); - tmp(0) = v; - GLMat.CPUtoGPUarraycopy(tmp, 0, pdata, i + j * nrows, 1, "GLMat update"); - this - } - - /** ND update */ - - override def updatev(inds:Array[Int], v:Long):GLMat = { - val indx = ND.linearize(inds, _dims); - val tmp = Array[Long](v); - GLMat.CPUtoGPUarraycopy(tmp, 0, pdata, indx, 1, "GLMat update"); - this - } - - /** ND sliced updates */ - - override def update(iv:IMat, jv:IMat, b:LMat):LMat = updatei(Array(iv, jv), GLMat(b)); - override def update(iv:IMat, j:Int, b:LMat):LMat = updatei(Array(iv, IMat.ielem(j)), GLMat(b)); - override def update(i:Int, jv:IMat, b:LMat):LMat = updatei(Array(IMat.ielem(i), jv), GLMat(b)); - - /* Generic RHS */ - override def update(iv:IMat, jv:IMat, b:Mat):LMat = updatei(Array(iv, jv), GLMat(b)); - override def update(iv:IMat, j:Int, b:Mat):LMat = updatei(Array(iv, IMat.ielem(j)), GLMat(b)); - override def update(i:Int, jv:IMat, b:Mat):LMat = updatei(Array(IMat.ielem(i), jv), GLMat(b)); - - override def update(i1:IMat, vv:Long):LMat = updatei(Array(i1), vv); - override def update(i1:IMat, i2:IMat, vv:Long):LMat = updatei(Array(i1, i2), vv); - - override def updatei(inds:Array[IMat], vv:LMat):GLMat = updatei(inds, GLMat(vv)); - - def updatei(inds:Array[IMat], vv:GLMat):GLMat = { - if (inds.length != _dims.length) { - throw new RuntimeException("GLMat update wrong number of dims") - } - val newdims = new Array[Int](_dims.length) - val newinds = new Array[GIMat](_dims.length) - var j = 0 - for (i <- 0 until _dims.length) { - inds(i) match { - case aa:MatrixWildcard => { - newdims(i) = _dims(i); - } - case _ => { - newdims(i) = inds(i).length; - newinds(i) = getIndexMat(i, inds(i)); - } - } - } - ND.checkDims("GND update:", ND.trimDims(newdims), ND.trimDims(vv._dims)); - inds.length match { - case 1 => { - val err = CUMATD.copyToInds(vv.pdata, pdata, safePointer(newinds(0)), newdims(0)); - if (err != 0) throw new RuntimeException("GMat update (I, J) error" + cudaGetErrorString(err)); - } - case 2 => { - val err = CUMATD.copyToInds2D(vv.pdata, vv.dims(0), pdata, dims(0), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1)); - if (err != 0) throw new RuntimeException("GMat update (I, J) error" + cudaGetErrorString(err)); - } - case 3 => { - val err = CUMATD.copyToInds3D(vv.pdata, vv.dims(0), vv.dims(1), pdata, dims(0), dims(1), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1), safePointer(newinds(2)), newdims(2)); - if (err != 0) throw new RuntimeException("GMat update (I, J, K) error" + cudaGetErrorString(err)); - } - case 4 => { - val err = CUMATD.copyToInds4D(vv.pdata, vv.dims(0), vv.dims(1), vv.dims(2), pdata, dims(0), dims(1), dims(2), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1), safePointer(newinds(2)), newdims(2), safePointer(newinds(3)), newdims(3)); - if (err != 0) throw new RuntimeException("GMat udpate (I, J, K, L) error" + cudaGetErrorString(err)); - } - case _ => throw new RuntimeException("GMat slice access with more than 4 indices not supported"); - } - this - } - - override def updatei(inds:Array[IMat], vv:Long):GLMat = { - val newdims = new Array[Int](_dims.length); - val newinds = new Array[GIMat](_dims.length); - for (i <- 0 until _dims.length) { - inds(i) match { - case aa:MatrixWildcard => { - newdims(i) = _dims(i); - } - case _ => { - newdims(i) = inds(i).length; - newinds(i) = getIndexMat(i, inds(i)); - } - } - } - inds.length match { - case 1 => { - val err = CUMATD.fillToInds(vv, pdata, safePointer(newinds(0)), newdims(0)); - if (err != 0) throw new RuntimeException("GMat update (I, J) error" + cudaGetErrorString(err)); - } - case 2 => { - val err = CUMATD.fillToInds2D(vv, pdata, dims(0), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1)); - if (err != 0) throw new RuntimeException("GMat update (I, J) error" + cudaGetErrorString(err)); - } - case 3 => { - val err = CUMATD.fillToInds3D(vv, pdata, dims(0), dims(1), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1), safePointer(newinds(2)), newdims(2)); - if (err != 0) throw new RuntimeException("GMat update (I, J, K) error" + cudaGetErrorString(err)); - } - case 4 => { - val err = CUMATD.fillToInds4D(vv, pdata, dims(0), dims(1), dims(2), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1), safePointer(newinds(2)), newdims(2), safePointer(newinds(3)), newdims(3)); - if (err != 0) throw new RuntimeException("GMat udpate (I, J, K, L) error" + cudaGetErrorString(err)); - } - case _ => throw new RuntimeException("GMat slice access with more than 4 indices not supported"); - } - this - } - - - /** explicit ND access */ - /* see if superclass methods work - override def apply(i1:Int, i2:Int, i3:Int):Float = apply(Array(i1, i2, i3)); - override def apply(i1:Int, i2:Int, i3:Int, i4:Int):Float = apply(Array(i1, i2, i3, i4)); - override def apply(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int):Float = apply(Array(i1, i2, i3, i4, i5)); - override def apply(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, i6:Int):Float = apply(Array(i1, i2, i3, i4, i5 ,i6)); -*/ - - /** ND slicing */ - /* see if superclass methods work - - override def apply(i1:IMat, i2:IMat, i3:IMat):GMat = apply(Array(i1, i2, i3), null); - override def apply(i1:IMat, i2:IMat, i3:IMat, i4:IMat):GMat = apply(Array(i1, i2, i3, i4), null); - override def apply(i1:IMat, i2:IMat, i3:IMat, i4:IMat, i5:IMat):GMat = apply(Array(i1, i2, i3, i4, i5), null); - override def apply(i1:IMat, i2:IMat, i3:IMat, i4:IMat, i5:IMat, i6:IMat):GMat = apply(Array(i1, i2, i3, i4, i5), null); - - */ - - - /** Type helpers for 2D updating with Ints */ - /* Should come from superclass - override def update(i:Int, b:Double):FMat = update(i, b.toFloat); - override def update(i:Int, b:Int):FMat = update(i, b.toFloat); - - override def update(i:Int, j:Int, b:Double):FMat = update(i, j, b.toFloat); - override def update(i:Int, j:Int, b:Int):FMat = update(i, j, b.toFloat); - */ - - /** ND single element updates */ - /* should come from superclass - override def update(i1:Int, i2:Int, i3:Int, vv:Float):FMat = update(Array(i1, i2, i3), vv) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, vv:Float):FMat = update(Array(i1, i2, i3, i4), vv) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, vv:Float):FMat = update(Array(i1, i2, i3, i4, i5), vv) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, i6:Int, vv:Float):FMat = update(Array(i1, i2, i3, i4, i5, i6), vv) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, i6:Int, i7:Int, vv:Float):FMat = update(Array(i1, i2, i3, i4, i5, i6, i7), vv) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, i6:Int, i7:Int, i8:Int, vv:Float):FMat = update(Array(i1, i2, i3, i4, i5, i6, i7, i8), vv) - - override def update(i1:Int, i2:Int, i3:Int, vv:Double):FMat = update(Array(i1, i2, i3), vv.toFloat) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, vv:Double):FMat = update(Array(i1, i2, i3, i4), vv.toFloat) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, vv:Double):FMat = update(Array(i1, i2, i3, i4, i5), vv.toFloat) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, i6:Int, vv:Double):FMat = update(Array(i1, i2, i3, i4, i5, i6), vv.toFloat) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, i6:Int, i7:Int, vv:Double):FMat = update(Array(i1, i2, i3, i4, i5, i6, i7), vv.toFloat) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, i6:Int, i7:Int, i8:Int, vv:Double):FMat = update(Array(i1, i2, i3, i4, i5, i6, i7, i8), vv.toFloat) - - override def update(i1:Int, i2:Int, i3:Int, vv:Int):FMat = update(Array(i1, i2, i3), vv.toFloat) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, vv:Int):FMat = update(Array(i1, i2, i3, i4), vv.toFloat) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, vv:Int):FMat = update(Array(i1, i2, i3, i4, i5), vv.toFloat) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, i6:Int, vv:Int):FMat = update(Array(i1, i2, i3, i4, i5, i6), vv.toFloat) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, i6:Int, i7:Int, vv:Int):FMat = update(Array(i1, i2, i3, i4, i5, i6, i7), vv.toFloat) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, i6:Int, i7:Int, i8:Int, vv:Int):FMat = update(Array(i1, i2, i3, i4, i5, i6, i7, i8), vv.toFloat) - */ - - /* should be inherited from superclass - - override def update(i1:IMat, i2:IMat, i3:IMat, vv:FMat):GMat = update(Array(i1, i2, i3), GMat(vv)); - override def update(i1:IMat, i2:IMat, i3:IMat, i4:IMat, vv:FMat):GMat = update(Array(i1, i2, i3, i4), GMat(vv)); - override def update(i1:IMat, i2:IMat, i3:IMat, i4:IMat, i5:IMat, vv:FMat):GMat = update(Array(i1, i2, i3, i4, i5), GMat(vv)); - override def update(i1:IMat, i2:IMat, i3:IMat, i4:IMat, i5:IMat, i6:IMat, vv:FMat):GMat = update(Array(i1, i2, i3, i4, i5, i6), GMat(vv)); - - override def update(iv:IMat, jv:IMat, b:Mat):FMat = update(Array(iv, jv), GMat(b)); - override def update(iv:IMat, j:Int, b:Mat):FMat = update(Array(iv, IMat.ielem(j)), GMat(b)); - override def update(i:Int, jv:IMat, b:Mat):FMat = update(Array(IMat.ielem(i), jv), GMat(b)); - - override def update(i1:IMat, i2:IMat, i3:IMat, vv:Mat):GMat = update(Array(i1, i2, i3), GMat(vv)); - override def update(i1:IMat, i2:IMat, i3:IMat, i4:IMat, vv:Mat):GMat = update(Array(i1, i2, i3, i4), GMat(vv)); - override def update(i1:IMat, i2:IMat, i3:IMat, i4:IMat, i5:IMat, vv:Mat):GMat = update(Array(i1, i2, i3, i4, i5), GMat(vv)); - override def update(i1:IMat, i2:IMat, i3:IMat, i4:IMat, i5:IMat, i6:IMat, vv:Mat):GMat = update(Array(i1, i2, i3, i4, i5, i6), GMat(vv)); - */ - - override def colslice(a:Int, b:Int):GLMat = colslice(a, b, null, 0); - - override def colslice(a:Int, b:Int, omat:Mat):GLMat = colslice(a, b, omat, 0); - - override def colslice(a:Int, b:Int, omat:Mat, c:Int):GLMat = { - val newdims = _dims.clone; - newdims(dims.length-1) = b-a+c; - val out = if (omat.asInstanceOf[AnyRef] != null && omat.isInstanceOf[GLMat] && omat.ncols >= b-a+c && omat.nrows == nrows) { - omat.asInstanceOf[GLMat] - } else { - GLMat.newOrCheckGLMat(newdims, omat, GUID, a, b, c, "colslice".##); - } - cudaMemcpy(out.pdata.withByteOffset(1L*c*nrows*Sizeof.LONG), pdata.withByteOffset(1L*a*nrows*Sizeof.LONG), 1L*(b-a)*nrows*Sizeof.LONG, cudaMemcpyDeviceToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError; - if (err != 0) throw new RuntimeException("GLMat colslice() error " + cudaGetErrorString(err)); - out - } - - override def colslice(a:Int, b:Int, omat:Mat, c:Int, pb:Boolean):GLMat = colslice(a, b, omat, c); - - override def clear = { - cudaMemset(pdata, 0, Sizeof.LONG*length) - cudaStreamSynchronize(Mat.SyncMethod) - this - } - - def lzeros(m:Int, n:Int) = { - GLMat.lzeros(m,n) - } - - def lones(m:Int, n:Int) = { - GLMat.lones(m,n) - } - - override def t = { - val out = GLMat.newOrCheckGLMat(ncols, nrows, null, GUID, "t".##) - CUMATD.transpose(this.pdata, nrows, out.pdata, ncols, nrows, ncols) - cudaStreamSynchronize(Mat.SyncMethod) - out - } - - override def set(v:Int):GLMat = { - CUMAT.setlval(pdata, v.toLong, length) - cudaStreamSynchronize(Mat.SyncMethod) - this - } - - override def set(v:Long):GLMat = { - CUMAT.setlval(pdata, v, length) - cudaStreamSynchronize(Mat.SyncMethod) - this - } - - def reduceOp(oldmat:Mat, dir:Int, initval:Long, op:Int):GLMat = { - if (dir == 1 || (dir == 0 && nrows > 1)) { - val out = GLMat.newOrCheckGLMat(1, ncols, oldmat, GUID, 1, op) - out.clear - val err = CUMAT.reduce1lop(nrows, ncols, pdata, out.pdata, initval, op) - if (err != 0) {throw new RuntimeException("CUDA kernel error in CUMAT.reduce1op " + cudaGetErrorString(err))} - Mat.nflops += length - out - } else if (dir == 2 || dir == 0) { - val out = GLMat.newOrCheckGLMat(nrows, 1, oldmat, GUID, 2, op) - out.clear - val err = CUMAT.reduce2lop(nrows, ncols, pdata, out.pdata, initval, op) - if (err != 0) {throw new RuntimeException("CUDA kernel error in CUMAT.reduce2op " + cudaGetErrorString(err))} - Mat.nflops += length - out - } else { - throw new RuntimeException("dimension must be 1 or 2") - } - } - - def horzcat(a:GLMat, omat:Mat) = { - if (nrows != a.nrows) - throw new RuntimeException("GLMat \\ row dims not equal") - val out = GLMat.newOrCheckGLMat(nrows, ncols+a.ncols, omat, GUID, a.GUID, "horzcat".##) - cudaMemcpy(out.pdata, pdata, 1L*length*Sizeof.LONG, cudaMemcpyKind.cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - cudaMemcpy(out.pdata.withByteOffset(1L*length*Sizeof.LONG), a.pdata, 1L*a.length*Sizeof.LONG, cudaMemcpyKind.cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - out - } - - def vertcat(a:GLMat, omat:Mat) = { - if (ncols != a.ncols) - throw new RuntimeException("GLMat on row dims not equal") - val out = GLMat.newOrCheckGLMat(nrows+a.nrows, ncols, omat, GUID, a.GUID, "vertcat".##) - cudaMemcpy2D(out.pdata, 1L*out.nrows*Sizeof.LONG, pdata, 1L*nrows*Sizeof.LONG, 1L*nrows*Sizeof.LONG, 1L*ncols, cudaMemcpyKind.cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - cudaMemcpy2D(out.pdata.withByteOffset(1L*nrows*Sizeof.LONG), 1L*out.nrows*Sizeof.LONG, a.pdata, 1L*a.nrows*Sizeof.LONG, 1L*a.nrows*Sizeof.LONG, 1L*a.ncols, cudaMemcpyKind.cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - out - } - - def GIop(aa:LMat, oldmat:Mat, op:Int):GLMat = { - val a = GLMat(aa); - val (nr, nc, nra, nca) = ND.compatibleGDims(_dims, aa._dims, "DenseMat Op"); - val dims = ND.maxDims(_dims, aa._dims); - val out = GLMat.newOrCheckGLMat(dims, oldmat, GUID, aa.GUID, op.hashCode); - Mat.nflops += scala.math.max(length, a.length); - val err = CUMAT.applylop(pdata, nr, nc, a.pdata, nra, nca, out.pdata, op); - if (err != 0) {throw new RuntimeException("CUDA kernel error %d in CUMAT.applylop" format err)} - out - } - - - override def recycle(nr:Int, nc:Int, nnz:Int):GLMat = { - if (nrows == nr && nc == ncols) { - this - } else if (realsize >= nr*nc) { - new GLMat(nr, nc, pdata, realsize) - } else { - free - GLMat(nr, nc) - } - } - - def toLMat(omat:Mat):LMat = { - val out = LMat.newOrCheckLMat(nrows, ncols, omat, GUID, "toLMat".##) - cudaMemcpy(Pointer.to(out.data), pdata, 1L*nrows*ncols * Sizeof.LONG, cudaMemcpyKind.cudaMemcpyDeviceToHost); - cudaStreamSynchronize(Mat.SyncMethod) - out - } - - def toLMat():LMat = toLMat(null); - - def copyTo(out:LMat):LMat = { - val a = out.recycle(nrows, ncols, 0) - cudaMemcpy(Pointer.to(a.data), pdata, 1L*nrows*ncols * Sizeof.LONG, cudaMemcpyKind.cudaMemcpyDeviceToHost); - cudaStreamSynchronize(Mat.SyncMethod) - a - } - - def copyFrom(in:LMat):GLMat = { - cudaMemcpy(pdata, Pointer.to(in.data), 1L*nrows*ncols*Sizeof.LONG, cudaMemcpyKind.cudaMemcpyHostToDevice); - cudaStreamSynchronize(Mat.SyncMethod) - this - } - - def copyTo(a:GMat):GMat = { - if (nrows != a.nrows || ncols != a.ncols) - throw new RuntimeException("dimensions mismatch in GMat <-- GIMat") - val err = CUMAT.longToFloat(this.pdata, a.pdata, length) - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("error in copyTo " + cudaGetErrorString(err)) - } - a - } - - def copyTo(out:GLMat):GLMat = { - val a = out.recycle(nrows, ncols, 0) - cudaMemcpy(a.pdata, pdata, 1L*length*Sizeof.LONG, cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - a - } - - override def copyTo(out:Mat):Mat = { - out match { - case a:GLMat => copyTo(a) - case a:GMat => copyTo(a) - case a:LMat => copyTo(a) - } - } - - def cumsumByKey(keys:GLMat, omat:Mat):GLMat = { - if (nrows != keys.nrows || ncols != keys.ncols) - throw new RuntimeException("cumsumKey dimensions mismatch"); - val out = GLMat.newOrCheckGLMat(nrows, ncols, omat, GUID, keys.GUID, "cumsumKey".##); - Mat.nflops += 2L*length; - if (nrows == 1 || ncols == 1) { - CUMATD.cumsumByKeyLL(pdata, keys.pdata, out.pdata, llength); - } else { - throw new RuntimeException("cumsumByKey only implemented for GLMat vectors"); - } - out - } - - def cumsumByKey(keys:GLMat):GLMat = cumsumByKey(keys, null); - - def cummaxByKey(keys:GLMat, omat:Mat):GLMat = { - if (nrows != keys.nrows || ncols != keys.ncols) - throw new RuntimeException("cummaxKey dimensions mismatch"); - val out = GLMat.newOrCheckGLMat(nrows, ncols, omat, GUID, keys.GUID, "cummaxKey".##); - Mat.nflops += 2L*length; - if (nrows == 1 || ncols == 1) { - CUMATD.cummaxByKeyLL(pdata, keys.pdata, out.pdata, llength); - } else { - throw new RuntimeException("cummaxByKey only implemented for GLMat vectors"); - } - out - } - - def cummaxByKey(keys:GLMat):GLMat = cummaxByKey(keys, null); - - def cumminByKey(keys:GLMat, omat:Mat):GLMat = { - if (nrows != keys.nrows || ncols != keys.ncols) - throw new RuntimeException("cumminKey dimensions mismatch"); - val out = GLMat.newOrCheckGLMat(nrows, ncols, omat, GUID, keys.GUID, "cumminKey".##); - Mat.nflops += 2L*length; - if (nrows == 1 || ncols == 1) { - CUMATD.cumminByKeyLL(pdata, keys.pdata, out.pdata, llength); - } else { - throw new RuntimeException("cumminByKey only implemented for GLMat vectors"); - } - out - } - - def cumminByKey(keys:GLMat):GLMat = cumminByKey(keys, null); - - override def _reverse(omat:Mat):GLMat = { - val out = GLMat.newOrCheckGLMat(nrows, ncols, omat, GUID, "reverse".##); - CUMATD.reverse(pdata, out.pdata, llength); - out - } - - override def reverse:GLMat = _reverse(null); - - override def reverse(omat:Mat):GLMat = _reverse(omat); - - override def free() = { - if (pdata == null) throw new RuntimeException("Attempt to free an already free'd GLMat") - cudaFree(pdata); - this - } - - override def getdiag():GLMat = { - if (nrows != ncols) throw new RuntimeException("getdiag requires a square matrix, but dims= %d %d" format (nrows, ncols)) - val out = GLMat.newOrCheckGLMat(nrows, 1, null, GUID, "getdiag".##) - cudaMemcpy2D(out.pdata, Sizeof.LONG, pdata, (nrows+1)*Sizeof.LONG, Sizeof.LONG, nrows, cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - val err = cudaGetLastError() - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in getdiag " + cudaGetErrorString(err)) - } - out - } - - override def mkdiag():GLMat = { - if (math.min(nrows, ncols) != 1) throw new RuntimeException("mkdiag requires a vector argument, but dims= %d %d" format (nrows, ncols)) - val size = math.max(nrows, ncols) - val out = GLMat.newOrCheckGLMat(size, size, null, GUID, "mkdiag".##) - out.clear - var err = cudaMemcpy2D(out.pdata, (nrows+1)*Sizeof.LONG, pdata, Sizeof.LONG, Sizeof.LONG, nrows, cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - if (err == 0) err = cudaGetLastError() - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in mkdiag " + cudaGetErrorString(err)) - } - out - } - - override def unary_- () = GIop(GLMat(-1), null, 2) - def + (a : GLMat) = GIop(a, null, op_add) - def - (a : GLMat) = GIop(a, null, op_sub) - def *@ (a : GLMat) = GIop(a, null, op_mul) - def / (a : GLMat) = GIop(a, null, op_div) - def > (b : GLMat) = GIop(b, null, op_gt) - def < (b : GLMat) = GIop(b, null, op_lt) - def == (b : GLMat) = GIop(b, null, op_eq) - def === (b : GLMat) = GIop(b, null,op_eq) - def >= (b : GLMat) = GIop(b, null, op_ge) - def <= (b : GLMat) = GIop(b, null, op_le) - def != (b : GLMat) = GIop(b, null, op_ne) - def max (b : GLMat) = GIop(b, null, op_max) - def min (b : GLMat) = GIop(b, null, op_min) - - def on(a : GLMat) = vertcat(a, null) - def \ (a : GLMat) = horzcat(a, null) - - - override def + (a : Float) = GIop(GLMat(a.toLong), null, op_add) - override def - (a : Float) = GIop(GLMat(a.toLong), null, op_sub) - override def *@ (a : Float) = GIop(GLMat(a.toLong), null, op_mul) - override def ∘ (a : Float) = GIop(GLMat(a.toLong), null, op_mul) - override def / (a : Float) = GIop(GLMat(a.toLong), null, op_div) - override def ^ (a : Float) = GIop(GLMat(a.toLong), null, op_pow) - - override def < (b : Float) = GIop(GLMat(b.toLong), null, op_lt); - override def > (b : Float) = GIop(GLMat(b.toLong), null, op_gt); - override def <= (b : Float) = GIop(GLMat(b.toLong), null, op_le); - override def >= (b : Float) = GIop(GLMat(b.toLong), null, op_ge); - override def == (b : Float) = GIop(GLMat(b.toLong), null, op_eq); - override def != (b : Float) = GIop(GLMat(b.toLong), null, op_ne); - override def max (b : Float) = GIop(GLMat(b.toLong), null, op_max); - override def min (b : Float) = GIop(GLMat(b.toLong), null, op_min); - - override def + (a : Int) = GIop(GLMat(a), null, op_add) - override def - (a : Int) = GIop(GLMat(a), null, op_sub) - override def *@ (a : Int) = GIop(GLMat(a), null, op_mul) - override def ∘ (a : Int) = GIop(GLMat(a), null, op_mul) - override def / (a : Int) = GIop(GLMat(a), null, op_div) - override def ^ (a : Int) = GIop(GLMat(a), null, op_pow) - - override def < (b : Int) = GIop(GLMat(b), null, op_lt) - override def > (b : Int) = GIop(GLMat(b), null, op_gt) - override def <= (b : Int) = GIop(GLMat(b), null, op_le) - override def >= (b : Int) = GIop(GLMat(b), null, op_ge) - override def == (b : Int) = GIop(GLMat(b), null, op_eq) - override def != (b : Int) = GIop(GLMat(b), null, op_ne) - override def max (b : Int) = GIop(GLMat(b), null, op_max); - override def min (b : Int) = GIop(GLMat(b), null, op_min); - - override def + (a : Double) = GIop(GLMat(a.toLong), null, op_add) - override def - (a : Double) = GIop(GLMat(a.toLong), null, op_sub) - override def *@ (a : Double) = GIop(GLMat(a.toLong), null, op_mul) - override def ∘ (a : Double) = GIop(GLMat(a.toLong), null, op_mul) - override def / (a : Double) = GIop(GLMat(a.toLong), null, op_div) - override def ^ (a : Double) = GIop(GLMat(a.toLong), null, op_pow) - - override def < (b : Double) = GIop(GLMat(b.toLong), null, op_lt) - override def > (b : Double) = GIop(GLMat(b.toLong), null, op_gt) - override def <= (b : Double) = GIop(GLMat(b.toLong), null, op_le) - override def >= (b : Double) = GIop(GLMat(b.toLong), null, op_ge) - override def == (b : Double) = GIop(GLMat(b.toLong), null, op_eq) - override def != (b : Double) = GIop(GLMat(b.toLong), null, op_ne) - override def max (b : Double) = GIop(GLMat(b.toLong), null, op_max); - override def min (b : Double) = GIop(GLMat(b.toLong), null, op_min); - - - def ~ (b: GLMat) = new GLPair(this, b) - -} - -class GLPair (omat:Mat, override val mat:GLMat) extends LPair (omat, mat){ - import GIMat.BinOp._ - - override def t = { - val out = GLMat.newOrCheckGLMat(mat.ncols, mat.nrows, omat, mat.GUID, "pt".##) - CUMATD.transpose(mat.pdata, mat.nrows, out.pdata, mat.ncols, mat.nrows, mat.ncols) - out - } - def + (a : GLMat) = mat.GIop(a, omat, op_add) - def - (a : GLMat) = mat.GIop(a, omat, op_sub) - def *@ (a : GLMat) = mat.GIop(a, omat, op_mul) - def / (a : GLMat) = mat.GIop(a, omat, op_div) - def > (b : GLMat) = mat.GIop(b, omat, op_gt) - def < (b : GLMat) = mat.GIop(b, omat, op_lt) - def == (b : GLMat) = mat.GIop(b, omat, op_eq) - def === (b : GLMat) = mat.GIop(b, omat, op_eq) - def >= (b : GLMat) = mat.GIop(b, omat, op_ge) - def <= (b : GLMat) = mat.GIop(b, omat, op_le) - def != (b : GLMat) = mat.GIop(b, omat, op_ne) - def max (b : GLMat) = mat.GIop(b, omat, op_max) - def min (b : GLMat) = mat.GIop(b, omat, op_min) - - def on(a : GLMat) = mat.vertcat(a, omat) - def \ (a : GLMat) = mat.horzcat(a, omat) - - - override def + (a : Long) = mat.GIop(GLMat(a), omat, op_add) - override def - (a : Long) = mat.GIop(GLMat(a), omat, op_sub) - override def *@ (a : Long) = mat.GIop(GLMat(a), omat, op_mul) - override def ∘ (a : Long) = mat.GIop(GLMat(a), omat, op_mul) - override def / (a : Long) = mat.GIop(GLMat(a), omat, op_div) - override def ^ (a : Long) = mat.GIop(GLMat(a), omat, op_pow) - - override def < (b : Long) = mat.GIop(GLMat(b), omat, op_lt) - override def > (b : Long) = mat.GIop(GLMat(b), omat, op_gt) - override def <= (b : Long) = mat.GIop(GLMat(b), omat, op_le) - override def >= (b : Long) = mat.GIop(GLMat(b), omat, op_ge) - override def == (b : Long) = mat.GIop(GLMat(b), omat, op_eq) - override def != (b : Long) = mat.GIop(GLMat(b), omat, op_ne) - override def max (b : Long) = mat.GIop(GLMat(b), omat, op_max) - override def min (b : Long) = mat.GIop(GLMat(b), omat, op_min) - - override def + (a : Float) = mat.GIop(GLMat(a.toLong), omat, op_add) - override def - (a : Float) = mat.GIop(GLMat(a.toLong), omat, op_sub) - override def *@ (a : Float) = mat.GIop(GLMat(a.toLong), omat, op_mul) - override def ∘ (a : Float) = mat.GIop(GLMat(a.toLong), omat, op_mul) - override def / (a : Float) = mat.GIop(GLMat(a.toLong), omat, op_div) - override def ^ (a : Float) = mat.GIop(GLMat(a.toLong), omat, op_pow) - - override def < (b : Float) = mat.GIop(GLMat(b.toLong), omat, op_lt) - override def > (b : Float) = mat.GIop(GLMat(b.toLong), omat, op_gt) - override def <= (b : Float) = mat.GIop(GLMat(b.toLong), omat, op_le) - override def >= (b : Float) = mat.GIop(GLMat(b.toLong), omat, op_ge) - override def == (b : Float) = mat.GIop(GLMat(b.toLong), omat, op_eq) - override def != (b : Float) = mat.GIop(GLMat(b.toLong), omat, op_ne) - override def max (b : Float) = mat.GIop(GLMat(b.toLong), omat, op_max) - override def min (b : Float) = mat.GIop(GLMat(b.toLong), omat, op_min) - - override def + (a : Int) = mat.GIop(GLMat(a), omat, op_add) - override def - (a : Int) = mat.GIop(GLMat(a), omat, op_sub) - override def *@ (a : Int) = mat.GIop(GLMat(a), omat, op_mul) - override def ∘ (a : Int) = mat.GIop(GLMat(a), omat, op_mul) - override def / (a : Int) = mat.GIop(GLMat(a), omat, op_div) - override def ^ (a : Int) = mat.GIop(GLMat(a), omat, op_pow) - - override def < (b : Int) = mat.GIop(GLMat(b), omat, op_lt) - override def > (b : Int) = mat.GIop(GLMat(b), omat, op_gt) - override def <= (b : Int) = mat.GIop(GLMat(b), omat, op_le) - override def >= (b : Int) = mat.GIop(GLMat(b), omat, op_ge) - override def == (b : Int) = mat.GIop(GLMat(b), omat, op_eq) - override def != (b : Int) = mat.GIop(GLMat(b), omat, op_ne) - override def max (b : Int) = mat.GIop(GLMat(b), omat, op_max) - override def min (b : Int) = mat.GIop(GLMat(b), omat, op_min) - - override def + (a : Double) = mat.GIop(GLMat(a.toLong), omat, op_add) - override def - (a : Double) = mat.GIop(GLMat(a.toLong), omat, op_sub) - override def *@ (a : Double) = mat.GIop(GLMat(a.toLong), omat, op_mul) - override def ∘ (a : Double) = mat.GIop(GLMat(a.toLong), omat, op_mul) - override def / (a : Double) = mat.GIop(GLMat(a.toLong), omat, op_div) - override def ^ (a : Double) = mat.GIop(GLMat(a.toLong), omat, op_pow) - - override def < (b : Double) = mat.GIop(GLMat(b.toLong), omat, op_lt) - override def > (b : Double) = mat.GIop(GLMat(b.toLong), omat, op_gt) - override def <= (b : Double) = mat.GIop(GLMat(b.toLong), omat, op_le) - override def >= (b : Double) = mat.GIop(GLMat(b.toLong), omat, op_ge) - override def == (b : Double) = mat.GIop(GLMat(b.toLong), omat, op_eq) - override def != (b : Double) = mat.GIop(GLMat(b.toLong), omat, op_ne) - override def max (b : Double) = mat.GIop(GLMat(b.toLong), omat, op_max) - override def min (b : Double) = mat.GIop(GLMat(b.toLong), omat, op_min) -} - -object GLMat { - - object BinOp { - val op_add=0 - val op_sub=1 - val op_mul=2 - val op_div=3 - val op_gt=4 - val op_lt=5 - val op_eq=6 - val op_ge=7 - val op_le=8 - val op_ne=9 - val op_max=10 - val op_min=11 - val op_atan2=12 - val op_pow=13 - } - - def apply(nr:Int, nc:Int):GLMat = { - val retv = new GLMat(nr, nc, new Pointer(), nr*nc) - if (Mat.debugMem) { - println("GLMat %d %d, %d %f" format (nr, nc, SciFunctions.getGPU, SciFunctions.GPUmem._1)) - if (nr*nc > Mat.debugMemThreshold) throw new RuntimeException("GLMat alloc too large"); - } - cudaMalloc(retv.pdata, 1L*nr*nc*Sizeof.LONG) - retv - } - - def apply(a:LMat):GLMat = { - a match { - case g:GLMat => g; - case _ => { - val retv = GLMat.newOrCheckGLMat(a.nrows, a.ncols, null, a.GUID, "GLMat".##); - val rsize = a.nrows*a.ncols; - cudaMemcpy(retv.pdata, Pointer.to(a.data), 1L*rsize*Sizeof.LONG, cudaMemcpyKind.cudaMemcpyHostToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - retv; - } - } - } - - def make(dims:Array[Int]):GLMat = { - val len = dims.reduce(_*_); - val retv = new GLMat(dims, new Pointer, len); - if (Mat.debugMem) { - println("GLMat %d, %d %f" format (len, SciFunctions.getGPU, SciFunctions.GPUmem._1)) - if (len > Mat.debugMemThreshold) throw new RuntimeException("GLMat alloc too large"); - } - var err = if (1L*len*Sizeof.LONG > Mat.hostAllocSize) { - cudaMallocHost(retv.pdata, 1L*len*Sizeof.LONG); - } else { - cudaMalloc(retv.pdata, 1L*len*Sizeof.LONG); - } - cudaStreamSynchronize(Mat.SyncMethod); - if (err == 0) err = cudaGetLastError(); - if (err != 0) throw new RuntimeException("CUDA alloc failed " + cudaGetErrorString(err)); - retv - } - - def make(dims:IMat):GLMat = make(dims.data); - - def apply(a:GMat):GLMat = { - val rsize = a.nrows*a.ncols; - val retv = GLMat.newOrCheckGLMat(a.nrows, a.ncols, null, a.GUID, "GIMat_GMat".##); - var err = CUMAT.floatToLong(a.pdata, retv.pdata, a.length); - cudaStreamSynchronize(Mat.SyncMethod); - if (err == 0) err = cudaGetLastError(); - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("GIMat(GMat) error " + cudaGetErrorString(err)) - } - retv - } - - def apply(a:Mat):GLMat = a match { - case aa:GLMat => aa - case aa:LMat => GLMat(aa) - case aa:GMat => GLMat(aa) - case aa:FMat => GLMat(GMat(aa)) - } - - def apply(a:Long):GLMat = { - val out = GLMat.newOrCheckGLMat(1, 1, null, a.##, "GLMat_Int".##) - out.set(a) - out - } - - def elem(a:Long):GLMat = { - val out = GLMat.newOrCheckGLMat(1, 1, null, a.##, "GLelem".##); - out.set(a) - out - } - - def lelem(a:Long):GLMat = elem(a); - - def lzeros(m:Int, n:Int):GLMat = { - val out = GLMat(m,n) - out.clear - out - } - - def lones(m:Int, n:Int):GLMat = { - val out = GLMat(m,n) - out.set(1) - out - } - - def lzeros(dims:IMat):GLMat = { - val out = make(dims); - out.clear - out - } - - def lones(dims:IMat):GLMat = { - val out = make(dims); - out.set(1) - out - } - - - - def GPUtoGPUarraycopy(a:Pointer, aoffset:Int, b:Pointer, boffset:Int, len:Int, msg:String ) = { - cudaMemcpy(b.withByteOffset(1L*boffset*Sizeof.LONG), a.withByteOffset(1L*aoffset*Sizeof.LONG), 1L*len*Sizeof.LONG, cudaMemcpyKind.cudaMemcpyDeviceToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError; - if (err != 0) throw new RuntimeException(msg +" error in memcpy "+ cudaGetErrorString(err)); - } - - def GPUtoCPUarraycopy(a:Pointer, aoffset:Int, b:Array[Long], boffset:Int, len:Int, msg:String ) = { - cudaMemcpy(Pointer.to(b).withByteOffset(1L*boffset*Sizeof.LONG), a.withByteOffset(1L*aoffset*Sizeof.LONG), 1L*len*Sizeof.LONG, cudaMemcpyKind.cudaMemcpyDeviceToHost); - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError; - if (err != 0) throw new RuntimeException(msg +" error in memcpy "+ cudaGetErrorString(err)); - } - - def CPUtoGPUarraycopy(a:Array[Long], aoffset:Int, b:Pointer, boffset:Int, len:Int, msg:String ) = { - cudaMemcpy(b.withByteOffset(1L*boffset*Sizeof.LONG), Pointer.to(a).withByteOffset(1L*aoffset*Sizeof.LONG), 1L*len*Sizeof.LONG, cudaMemcpyKind.cudaMemcpyHostToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError; - if (err != 0) throw new RuntimeException(msg +" error in memcpy "+ cudaGetErrorString(err)); - } - - - def accumIJ(I:GIMat, J:GIMat, V:GLMat, omat:Mat, nrows:Int, ncols:Int):GLMat = { - val out = GLMat.newOrCheckGLMat(nrows, ncols, omat, I.GUID, J.GUID, V.GUID, "GLMat_accum".##) - out.clear - if (I.length != J.length || I.length != V.length) { - throw new RuntimeException("GIMat accum: index lengths dont match") - } - CUMAT.laccum(I.pdata, J.pdata, V.pdata, out.pdata, I.length, nrows) - Mat.nflops += I.length - out - } - - def accumIJ(I:Int, J:GIMat, V:GLMat, omat:Mat, nrows:Int, ncols:Int):GLMat = { - val out = GLMat.newOrCheckGLMat(nrows, ncols, omat, I, J.GUID, V.GUID, "GLMat_accumI".##) - out.clear - if (J.length != V.length) { - throw new RuntimeException("GIMat accum: index lengths dont match") - } - CUMAT.laccumI(I, J.pdata, V.pdata, out.pdata, J.length, nrows) - Mat.nflops += J.length - out - } - - def accumIJ(I:GIMat, J:Int, V:GLMat, omat:Mat, nrows:Int, ncols:Int):GLMat = { - val out = GLMat.newOrCheckGLMat(nrows, ncols, omat, I.GUID, J, V.GUID, "GIMat_accumJ".##) - out.clear - if (I.length != V.length) { - throw new RuntimeException("GIMat accum: index lengths dont match") - } - CUMAT.laccumJ(I.pdata, J, V.pdata, out.pdata, I.length, nrows) - Mat.nflops += I.length - out - } - - def accumIJ(I:GIMat, J:GIMat, V:Long, omat:Mat, nrows:Int, ncols:Int):GLMat = { - val out = GLMat.newOrCheckGLMat(nrows, ncols, omat, I.GUID, J.GUID, V.hashCode, "GLMat_accumV".##) - out.clear - if (I.length != J.length) { - throw new RuntimeException("GIMat accum: index lengths dont match") - } - CUMAT.laccumV(I.pdata, J.pdata, V, out.pdata, I.length, nrows) - Mat.nflops += I.length - out - } - - def accumIJ(I:Int, J:GIMat, V:Long, omat:Mat, nrows:Int, ncols:Int):GLMat = { - val out = GLMat.newOrCheckGLMat(nrows, ncols, omat, I, J.GUID, V.hashCode, "GLMat_accumIV".##) - out.clear - CUMAT.laccumIV(I, J.pdata, V, out.pdata, J.length, nrows) - Mat.nflops += J.length - out - } - - def accumIJ(I:GIMat, J:Int, V:Long, omat:Mat, nrows:Int, ncols:Int):GLMat = { - val out = GLMat.newOrCheckGLMat(nrows, ncols, omat, I.GUID, J, V.hashCode, "GLMat_accumJV".##) - out.clear - CUMAT.laccumJV(I.pdata, J, V, out.pdata, I.length, nrows) - Mat.nflops += I.length - out - } - - def accum(IJ:GIMat, V:GIMat, omat:Mat, nrows:Int, ncols:Int):GLMat = { - if (IJ.nrows != V.length || IJ.ncols > 2) { - throw new RuntimeException("GIMat accum: index lengths dont match") - } - val out = GLMat.newOrCheckGLMat(nrows, ncols, omat, IJ.GUID, V.GUID, "GLMat_accumIJ".##) - out.clear - if (IJ.ncols == 2) { - CUMAT.laccum(IJ.pdata, IJ.pdata.withByteOffset(1L*IJ.nrows*Sizeof.INT), V.pdata, out.pdata, V.length, nrows) - } else { - CUMAT.laccumJ(IJ.pdata, 0, V.pdata, out.pdata, V.length, nrows) - } - Mat.nflops += V.length - out - } - - def accum(IJ:GIMat, V:Int, omat:Mat, nrows:Int, ncols:Int):GLMat = { - if (IJ.ncols > 2) { - throw new RuntimeException("GIMat accum: index lengths dont match") - } - val out = GLMat.newOrCheckGLMat(nrows, ncols, omat, IJ.GUID, V.hashCode, "GLMat_accumIJV".##) - out.clear - if (IJ.ncols == 2) { - CUMAT.iaccumV(IJ.pdata, IJ.pdata.withByteOffset(1L*IJ.nrows*Sizeof.INT), V, out.pdata, IJ.nrows, nrows) - } else { - CUMAT.iaccumJV(IJ.pdata, 0, V, out.pdata, IJ.nrows, nrows) - } - Mat.nflops += IJ.nrows - out - } - - def maxi2(a:GLMat, omat:Mat, omati:Mat, dim0:Int):(GLMat, GIMat) = { - Mat.nflops += 1L * a.length - val dim = if (a.nrows == 1 && dim0 == 0) 2 else math.max(1, dim0) - if (dim == 1) { - val out = GLMat.newOrCheckGLMat(1, a.ncols, omat, a.GUID, "maxi2".##) - val outi = GIMat.newOrCheckGIMat(1, a.ncols, omati, a.GUID, "maxi2_1".##) - val err = CUMAT.maxil(a.pdata, out.pdata, outi.pdata, a.nrows, a.ncols, 1) - if (err != 0) throw new RuntimeException("maxi2 error %d: " + cudaGetErrorString(err) format err); - (out, outi) - } else if (dim == 2) { - val out = GLMat.newOrCheckGLMat(a.nrows, 1, omat, a.GUID, "maxi2".##) - val outi = GIMat.newOrCheckGIMat(a.nrows, 1, omati, a.GUID, "maxi2_1".##) - val err = CUMAT.maxil(a.pdata, out.pdata, outi.pdata, a.nrows, a.ncols, 2) - if (err != 0) throw new RuntimeException("maxi2 error %d: " + cudaGetErrorString(err) format err); - (out, outi) - } else { - throw new RuntimeException("maxi2 dimension not recognized %d" format dim0) - } - } - - def mini2(a:GLMat, omat:Mat, omati:Mat, dim0:Int):(GLMat, GIMat) = { - Mat.nflops += 1L * a.length - val dim = if (a.nrows == 1 && dim0 == 0) 2 else math.max(1, dim0) - if (dim == 1) { - val out = GLMat.newOrCheckGLMat(1, a.ncols, omat, a.GUID, "mini2".##) - val outi = GIMat.newOrCheckGIMat(1, a.ncols, omati, a.GUID, "mini2_1".##) - val err = CUMAT.minil(a.pdata, out.pdata, outi.pdata, a.nrows, a.ncols, 1) - if (err != 0) throw new RuntimeException("mini2 error %d: " + cudaGetErrorString(err) format err); - (out, outi) - } else if (dim == 2) { - val out = GLMat.newOrCheckGLMat(a.nrows, 1, omat, a.GUID, "maxi2".##) - val outi = GIMat.newOrCheckGIMat(a.nrows, 1, omati, a.GUID, "maxi2_1".##) - val err = CUMAT.minil(a.pdata, out.pdata, outi.pdata, a.nrows, a.ncols, 2) - if (err != 0) throw new RuntimeException("mini2 error %d: " + cudaGetErrorString(err) format err); - (out, outi) - } else { - throw new RuntimeException("mini2 direction not recognized %d" format dim) - } - } - - def isortlexIndsGPU(grams:LMat, inds:IMat, asc:Boolean) = { - if (grams.nrows != inds.nrows) throw new RuntimeException("isortlexIndsGPU mismatched dims") - val linds = LMat(inds); - val p1 = Pointer.to(grams.data) - val p2 = Pointer.to(linds.data) - p2sortlexGPU(p1, p2, inds.nrows, asc); - inds <-- linds; - } - - def i2sortlexGPU(mat:LMat, asc:Boolean) = { - if (mat.ncols != 2) throw new RuntimeException("i2sortlexGPU mismatched dims") - val p1 = Pointer.to(mat.data) - val p2 = Pointer.to(mat.data).withByteOffset(1L*mat.nrows*Sizeof.LONG) - p2sortlexGPU(p1, p2, mat.nrows, asc) - } - - def i2sortlexColsGPU(col1:LMat, col2:LMat, asc:Boolean) = { - if (col1.nrows != col2.nrows) throw new RuntimeException("i2sortlexGPU mismatched dims") - val p1 = Pointer.to(col1.data) - val p2 = Pointer.to(col2.data) - p2sortlexGPU(p1, p2, col1.nrows, asc) - } - - - def p2sortlexGPU(p1:Pointer, p2:Pointer, nrows:Int, asc:Boolean) = { - val ggrams = GLMat(nrows, 2) - var status = cudaMemcpy(ggrams.pdata, p2, 1L*nrows*Sizeof.LONG, cudaMemcpyHostToDevice) - if (status != 0) throw new RuntimeException("p3sortlexGPU error1 %d" format (status)) - status = cudaMemcpy(ggrams.pdata.withByteOffset(1L*nrows*Sizeof.LONG), p1, 1L*nrows*Sizeof.LONG, cudaMemcpyHostToDevice) - if (status != 0) throw new RuntimeException("p3sortlexGPU error2 %d" format (status)) - cudaStreamSynchronize(Mat.SyncMethod) - val ggramst = ggrams.t - ggrams.free - CUMAT.l2sort(ggramst.pdata, nrows, if (asc) 1 else 0) - val ograms = ggramst.t - ggramst.free - status = cudaMemcpy(p1, ograms.pdata.withByteOffset(1L*nrows*Sizeof.LONG), 1L*nrows*Sizeof.LONG, cudaMemcpyDeviceToHost) - if (status != 0) throw new RuntimeException("p3sortlexGPU error4 %d" format (status)) - status = cudaMemcpy(p2, ograms.pdata, 1L*nrows*Sizeof.LONG, cudaMemcpyDeviceToHost) - if (status != 0) throw new RuntimeException("p3sortlexGPU error5 %d" format (status)) - ograms.free - } - - - def i2sortlexIndsGPU(grams:LMat, inds:IMat, asc:Boolean) = { - if (grams.nrows != inds.nrows) throw new RuntimeException("i2sortlexIndsGPU mismatched dims") - val p1 = Pointer.to(grams.data) - val p2 = p1.withByteOffset(1L*inds.nrows*Sizeof.LONG); - val linds = LMat(inds); - val p3 = Pointer.to(linds.data) - p3sortlexGPU(p1, p2, p3, inds.nrows, asc); - inds <-- linds; - } - - def i2sortlexColsIndsGPU(col1:LMat, col2:LMat, inds:IMat, asc:Boolean) = { - if (col1.nrows != inds.nrows || col2.nrows != inds.nrows) throw new RuntimeException("i2sortlexColsIndsGPU mismatched dims") - val p1 = Pointer.to(col1.data) - val p2 = Pointer.to(col2.data) - val linds = LMat(inds); - val p3 = Pointer.to(linds.data) - p3sortlexGPU(p1, p2, p3, inds.nrows, asc) - inds <-- linds; - } - /* - * Useful for creating sparse matrices - */ - - def i2sortlexColsIndsGPU(col1:LMat, col2:LMat, fvals:FMat, asc:Boolean) = { - if (col1.nrows != fvals.nrows || col2.nrows != fvals.nrows) throw new RuntimeException("i2sortlexGPU mismatched dims") - val p1 = Pointer.to(col1.data) - val p2 = Pointer.to(col2.data) - val f2vals = fvals.t on FMat.zeros(1, fvals.length); - val p3 = Pointer.to(f2vals.data) - p3sortlexGPU(p1, p2, p3, fvals.nrows, asc); - fvals <-- f2vals(0, MatFunctions.?).t - } - - /* - * This is not strictly a 3-column lex sort, only the first two columns are used, and the third is just permuted - */ - def p3sortlexGPU(p1:Pointer, p2:Pointer, p3:Pointer, nrows:Int, asc:Boolean) = { - val ggrams = GLMat(nrows, 2) - val gvals = GLMat(nrows, 1) - var status = cudaMemcpy(ggrams.pdata, p2, 1L*nrows*Sizeof.LONG, cudaMemcpyHostToDevice) - if (status != 0) throw new RuntimeException("p3sortlexGPU error1 %d" format (status)) - status = cudaMemcpy(ggrams.pdata.withByteOffset(1L*nrows*Sizeof.LONG), p1, 1L*nrows*Sizeof.LONG, cudaMemcpyHostToDevice) - if (status != 0) throw new RuntimeException("p3sortlexGPU error2 %d" format (status)) - status = cudaMemcpy(gvals.pdata, p3, 1L*nrows*Sizeof.LONG, cudaMemcpyHostToDevice) - if (status != 0) throw new RuntimeException("p3sortlexGPU error3 %d" format (status)) - cudaStreamSynchronize(Mat.SyncMethod) - val ggramst = ggrams.t - ggrams.free - CUMAT.l2sortk(ggramst.pdata, gvals.pdata, nrows, if (asc) 1 else 0) - val ograms = ggramst.t - ggramst.free - status = cudaMemcpy(p1, ograms.pdata.withByteOffset(1L*nrows*Sizeof.LONG), 1L*nrows*Sizeof.LONG, cudaMemcpyDeviceToHost) - if (status != 0) throw new RuntimeException("p3sortlexGPU error4 %d" format (status)) - status = cudaMemcpy(p2, ograms.pdata, 1L*nrows*Sizeof.LONG, cudaMemcpyDeviceToHost) - if (status != 0) throw new RuntimeException("p3sortlexGPU error5 %d" format (status)) - status = cudaMemcpy(p3, gvals.pdata, 1L*nrows*Sizeof.LONG, cudaMemcpyDeviceToHost) - if (status != 0) throw new RuntimeException("p3sortlexGPU error6 %d" format (status)) - ograms.free - gvals.free - } - - - def i3sortlexIndsGPU(grams:LMat, inds:IMat, asc:Boolean) = { - if (grams.nrows != inds.nrows) throw new RuntimeException("i3sortlexIndsGPU mismatched dims") - val p1 = Pointer.to(grams.data) - val p2 = p1.withByteOffset(1L*inds.nrows*Sizeof.LONG) - val p3 = p1.withByteOffset(1L*inds.nrows*2*Sizeof.LONG) - val linds = LMat(inds); - val p4 = Pointer.to(linds.data) - p4sortlexGPU(p1, p2, p3, p4, grams.nrows, asc); - inds <-- linds - } - - def i4sortlexColsGPU(col1:LMat, col2:LMat, col3:LMat, inds:LMat, asc:Boolean) = { - if (col1.nrows != inds.nrows || col2.nrows != inds.nrows || col3.nrows != inds.nrows) { - throw new RuntimeException("i3sortlexColsGPU mismatched dims") - } - val p1 = Pointer.to(col1.data) - val p2 = Pointer.to(col2.data) - val p3 = Pointer.to(col3.data) - val p4 = Pointer.to(inds.data) - p4sortlexGPU(p1, p2, p3, p4, inds.nrows, asc) - } - - def p4sortlexGPU(p1:Pointer, p2:Pointer, p3:Pointer, p4:Pointer, nrows:Int, asc:Boolean) = { - val ggrams = GLMat(nrows, 4) - var status = cudaMemcpy(ggrams.pdata, p1, 1L*nrows*Sizeof.LONG, cudaMemcpyHostToDevice) - if (status != 0) throw new RuntimeException("p4sortlexGPU error1 %d" format (status)) - status = cudaMemcpy(ggrams.pdata.withByteOffset(1L*nrows*Sizeof.LONG), p2, 1L*nrows*Sizeof.LONG, cudaMemcpyHostToDevice) - if (status != 0) throw new RuntimeException("p4sortlexGPU error2 %d" format (status)) - status = cudaMemcpy(ggrams.pdata.withByteOffset(1L*nrows*2*Sizeof.LONG), p3, 1L*nrows*Sizeof.LONG, cudaMemcpyHostToDevice) - if (status != 0) throw new RuntimeException("p4sortlexGPU error3 %d" format (status)) - status = cudaMemcpy(ggrams.pdata.withByteOffset(1L*nrows*3*Sizeof.LONG), p4, 1L*nrows*Sizeof.LONG, cudaMemcpyHostToDevice) - if (status != 0) throw new RuntimeException("p4sortlexGPU error4 %d" format (status)) - cudaStreamSynchronize(Mat.SyncMethod) - val ggramst = ggrams.t - ggrams.free - CUMAT.l4sort(ggramst.pdata, nrows, if (asc) 1 else 0) - val ograms = ggramst.t - ggramst.free - status = cudaMemcpy(p1, ograms.pdata, 1L*nrows*Sizeof.LONG, cudaMemcpyDeviceToHost) - if (status != 0) throw new RuntimeException("p4sortlexGPU error5 %d" format (status)) - status = cudaMemcpy(p2, ograms.pdata.withByteOffset(1L*nrows*Sizeof.LONG), 1L*nrows*Sizeof.LONG, cudaMemcpyDeviceToHost) - if (status != 0) throw new RuntimeException("p4sortlexGPU error6 %d" format (status)) - status = cudaMemcpy(p3, ograms.pdata.withByteOffset(1L*nrows*2*Sizeof.LONG), 1L*nrows*Sizeof.LONG, cudaMemcpyDeviceToHost) - if (status != 0) throw new RuntimeException("p4sortlexGPU error7 %d" format (status)) - status = cudaMemcpy(p4, ograms.pdata.withByteOffset(1L*nrows*3*Sizeof.LONG), 1L*nrows*Sizeof.LONG, cudaMemcpyDeviceToHost) - if (status != 0) throw new RuntimeException("p4sortlexGPU error8 %d" format (status)) - ograms.free - } - - - def sortLVec(keys:GLMat, asc:Int) = { - CUMAT.lsort(keys.pdata, keys.length, asc) - } - - def sortLVec(keys:GLMat) = { - CUMAT.lsort(keys.pdata, keys.length, 1) - } - - def collectLVec(keys:GLMat, vals:GIMat, okeys:GLMat, ovals:GIMat):(GLMat, GIMat) = { - val len = CUMAT.collectLVec(keys.pdata, vals.pdata, okeys.pdata, ovals.pdata, keys.length); -// println("collect %d %d" format (keys.length, len)) - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError; - if (err != 0) throw new RuntimeException("GLMat.collect error %d: " + cudaGetErrorString(err) format err); - (new GLMat(1, len, okeys.pdata, okeys.realsize), new GIMat(1, len, ovals.pdata, ovals.realsize)); - } - - def mergeLVecs(akeys:GLMat, avals:GIMat, bkeys:GLMat, bvals:GIMat, okeys:GLMat, ovals:GIMat):(GLMat, GIMat) = { - val len = akeys.length + bkeys.length - val outkeys = new GLMat(1, len, okeys.pdata, okeys.realsize); - val outvals = new GIMat(1, len, ovals.pdata, ovals.realsize); -/* if (akeys.length == 0) { - outkeys <-- bkeys; - outvals <-- bvals; - } else if (bkeys.length == 0) { - outkeys <-- akeys; - outvals <-- avals; - } else { */ - val err = CUMAT.mergeLVecs(akeys.pdata, avals.pdata, bkeys.pdata, bvals.pdata, okeys.pdata, ovals.pdata, akeys.length, bkeys.length); - if (err != 0) throw new RuntimeException("GLMat.merge error %d: " + cudaGetErrorString(err) format err); -// } - (outkeys, outvals); - } - - def newOrCheckGLMat(nr:Int, nc:Int, oldmat:Mat):GLMat = { - if (oldmat.asInstanceOf[AnyRef] == null || (oldmat.nrows == 0 && oldmat.ncols == 0)) { - GLMat(nr, nc) - } else { - if (oldmat.nrows != nr || oldmat.ncols != nc) { - oldmat.recycle(nr, nc, 0).asInstanceOf[GLMat] - } else { - oldmat.asInstanceOf[GLMat] - } - } - } - - def newOrCheckGLMat(dims:Array[Int], out:Mat):GLMat = { - if (out.asInstanceOf[AnyRef] != null && ND.compareDims(out.dims.data, dims)) { - out.asInstanceOf[GLMat] - } else { - GLMat.make(dims) - } - } - - def newOrCheckGLMat(dims:IMat, out:Mat):GLMat = newOrCheckGLMat(dims.data, out); - - def newOrCheckGLMat(nr:Int, nc:Int, outmat:Mat, matGuid:Long, opHash:Int):GLMat = { - if (outmat.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGLMat(nr, nc, outmat) - } else { - val key = (matGuid, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache3(key) - if (res != null) { - newOrCheckGLMat(nr, nc, res) - } else { - val omat = newOrCheckGLMat(nr, nc, null) - Mat.cache3put(key, omat) - omat - } - } - } - - def newOrCheckGLMat(dims:Array[Int], out:Mat, matGuid:Long, opHash:Int):GLMat = { - if (out.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGLMat(dims, out) - } else { - val key = (matGuid, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache3(key) - if (res != null) { - newOrCheckGLMat(dims, res) - } else { - val omat = newOrCheckGLMat(dims, null) - Mat.cache3put(key, omat) - omat - } - } - } - - def newOrCheckGLMat(dims:IMat, out:Mat, g1:Long, opHash:Int):GLMat = newOrCheckGLMat(dims.data, out, g1, opHash); - - def newOrCheckGLMat(nr:Int, nc:Int, outmat:Mat, guid1:Long, guid2:Long, opHash:Int):GLMat = { - if (outmat.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGLMat(nr, nc, outmat) - } else { - val key = (guid1, guid2, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache4(key) - if (res != null) { - newOrCheckGLMat(nr, nc, res) - } else { - val omat = newOrCheckGLMat(nr, nc, null) - Mat.cache4put(key, omat) - omat - } - } - } - - def newOrCheckGLMat(dims:Array[Int], out:Mat, guid1:Long, guid2:Long, opHash:Int):GLMat = { - if (out.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGLMat(dims, out) - } else { - val key = (guid1, guid2, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache4(key) - if (res != null) { - newOrCheckGLMat(dims, res) - } else { - val omat = newOrCheckGLMat(dims, null) - Mat.cache4put(key, omat) - omat - } - } - } - - def newOrCheckGLMat(dims:IMat, out:Mat, g1:Long, g2:Long, opHash:Int):GLMat = newOrCheckGLMat(dims.data, out, g1, g2, opHash); - - def newOrCheckGLMat(nr:Int, nc:Int, outmat:Mat, guid1:Long, guid2:Long, guid3:Long, opHash:Int):GLMat = { - if (outmat.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGLMat(nr, nc, outmat) - } else { - val key = (guid1, guid2, guid3, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache5(key) - if (res != null) { - newOrCheckGLMat(nr, nc, res) - } else { - val omat = newOrCheckGLMat(nr, nc, null) - Mat.cache5put(key, omat) - omat - } - } - } - - def newOrCheckGLMat(dims:Array[Int], out:Mat, g1:Long, g2:Long, g3:Long, opHash:Int):GLMat = { - if (out.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGLMat(dims, out) - } else { - val key = (g1, g2, g3, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache5(key) - if (res != null) { - newOrCheckGLMat(dims, res) - } else { - val omat = newOrCheckGLMat(dims, null) - Mat.cache5put(key, omat) - omat - } - } - } - - def newOrCheckGLMat(dims:IMat, out:Mat, g1:Long, g2:Long, g3:Long, opHash:Int):GLMat = newOrCheckGLMat(dims.data, out, g1, g2, g3, opHash); - - - def newOrCheckGLMat(nr:Int, nc:Int, outmat:Mat, guid1:Long, guid2:Long, guid3:Long, guid4:Long, opHash:Int):GLMat = { - val m = if (outmat.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGLMat(nr, nc, outmat) - } else { - val key = (guid1, guid2, guid3, guid4, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache6(key) - if (res != null) { - newOrCheckGLMat(nr, nc, res) - } else { - val omat = newOrCheckGLMat(nr, nc, null) - Mat.cache6put(key, omat) - omat - } - } - if (m.myGPU != SciFunctions.getGPU) { - throw new RuntimeException("newOrCheckGLMat3 problem with mat %d" format m.GUID) - } - m - } - - def newOrCheckGLMat(dims:Array[Int], out:Mat, g1:Long, g2:Long, g3:Long, g4:Long, opHash:Int):GLMat = { - if (out.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGLMat(dims, out) - } else { - val key = (g1, g2, g3, g4, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache6(key) - if (res != null) { - newOrCheckGLMat(dims, res) - } else { - val omat = newOrCheckGLMat(dims, null) - Mat.cache6put(key, omat) - omat - } - } - } - - def newOrCheckGLMat(dims:IMat, out:Mat, g1:Long, g2:Long, g3:Long, g4:Long, opHash:Int):GLMat = newOrCheckGLMat(dims.data, out, g1, g2, g3, g4, opHash); - -} - - - - - - - - diff --git a/src/main/scala/BIDMat/GMat.scala b/src/main/scala/BIDMat/GMat.scala index 225601ae..8ebba92c 100755 --- a/src/main/scala/BIDMat/GMat.scala +++ b/src/main/scala/BIDMat/GMat.scala @@ -1,2245 +1,13 @@ package BIDMat -import jcuda._ -import jcuda.runtime._ -import jcuda.runtime.JCuda._ -import jcuda.runtime.cudaMemcpyKind._ -import jcuda.jcublas._ -import jcuda.jcublas.JCublas2._ -import jcuda.jcusparse._ import scala.concurrent.Future import scala.concurrent.ExecutionContext.Implicits.global import scala.util.hashing.MurmurHash3 import edu.berkeley.bid.MurmurHash3.MurmurHash3_x64_64 -import edu.berkeley.bid.CUMAT import java.io.ObjectOutputStream import java.io.ObjectInputStream -@SerialVersionUID(100L) -class GMat(dims0:Array[Int], @transient var pdata:Pointer, val realsize:Long) extends FMat(dims0, null) { - import GMat.BinOp._ - - /** 2D Constructor */ - def this(nr:Int, nc:Int, pdata:Pointer, realsize:Long) = this(Array(nr, nc), pdata, realsize); - - override def mytype = "GMat"; - - override def nnz = length; - - override def t = { - val out = GMat.newOrCheckGMat(ncols, nrows, null, GUID, "t".##) - CUMAT.transpose(this.pdata, nrows, out.pdata, ncols, nrows, ncols) - out - } - - /** hold indices in GPU mem */ - val ginds = new Array[GIMat](ndims); - - /** Access values and contents */ - override def dv:Double = - if (nrows > 1 || ncols > 1) { - throw new RuntimeException("Matrix should be 1x1 to extract value") - } else { - toFMat(null).data(0) - } - - override def fv:Float = - if (nrows > 1 || ncols > 1) { - throw new RuntimeException("Matrix should be 1x1 to extract value") - } else { - toFMat(null).data(0) - } - - override def contents() = { - val out = new GMat(length, 1, pdata, realsize); - out.setGUID(MurmurHash3.mix(MurmurHash3.mix(length, 1), (GUID*7897889).toInt)); - out - } - - override def view(nr:Int, nc:Int):GMat = { - if (1L * nr * nc > realsize) { - throw new RuntimeException("view dimensions too large") - } - if (nr == nrows && nc == ncols) { - this - } else { - val out = new GMat(nr, nc, pdata, realsize); - out.setGUID(MurmurHash3.mix(MurmurHash3.mix(nr, nc), (GUID*3145341).toInt)); - out - } - } - - var saveMe:FMat = null; - - var printVal:Array[Float] = null; - - override def printOne(i:Int):String = { - if (printVal.asInstanceOf[AnyRef] == null) printVal = new Array[Float](1); - GMat.GPUtoCPUarraycopy(pdata, i, printVal, 0, 1, "printOne"); - val v = printVal(0); - if (v % 1 == 0 && math.abs(v) < 1e10) { - "%d" format v.intValue - } else { - "%.5g" format v - } - } - - private def writeObject(out:ObjectOutputStream):Unit = { - saveMe = FMat(this); - out.defaultWriteObject(); - } - - private def readObject(in:ObjectInputStream):Unit = { - in.defaultReadObject(); - val gpu = SciFunctions.getGPU; - GFunctions.setGPU(myGPU); - pdata = GMat(saveMe).pdata; - GFunctions.setGPU(gpu); - saveMe = null; - } - - def getIndxWrapper(i:Int):GIMat = { - if (ginds(i).asInstanceOf[AnyRef] == null) { - ginds(i) = GIMat(1,1) - } - ginds(i); - } - - def getIndexValue(i:Int, v:Int):GIMat = { - getIndxWrapper(i); - ginds(i).set(v); - ginds(i); - } - - def getIndexMat(i:Int, v:IMat):GIMat = { - if (v.length == 0 || v.length > 1) { - GIMat(v); - } else { - getIndxWrapper(i).set(v.v); - ginds(i); - } - } - - def safePointer(ind:GIMat):Pointer = { - if (ind.asInstanceOf[AnyRef] == null) { - GMat.nullPointer; - } else { - ind.pdata; - } - } - - /** 1D access */ - - override def apply(ind:Int):Float = { - val tmp = new Array[Float](1); - GMat.GPUtoCPUarraycopy(pdata, ind, tmp, 0, 1, "GMat apply"); - tmp(0) - } - - /** 2D access */ - - override def apply(i:Int, j:Int):Float = { - val tmp = new Array[Float](1); - GMat.GPUtoCPUarraycopy(pdata, i + nrows * j, tmp, 0, 1, "GMat apply"); - tmp(0) - } - - /** ND access */ - - override def applyv(inds:Array[Int]):Float = { - val indx = ND.linearize(inds, dims.data); - val tmp = new Array[Float](1); - GMat.GPUtoCPUarraycopy(pdata, indx, tmp, 0, 1, "GMat apply"); - tmp(0); - } - - /** apply to an index IMat, and mirror its structure in the result */ - /* should be implemented in subclasses */ - - override def apply(inds:IMat):GMat = { - inds match { - case aa:MatrixWildcard => { - val out = GMat.newOrCheckGMat(length, 1, null, GUID, inds.GUID, "apply(?)".##); - GMat.GPUtoGPUarraycopy(pdata, 0, out.pdata, 0, length, "GMat IMat apply" ); - out - } - case _ => { - val newinds = getIndexMat(0, inds); - val out = GMat.newOrCheckGMat(inds.dims, null, GUID, inds.GUID, "apply IMat".##); - val err = CUMAT.copyFromInds(pdata, out.pdata, safePointer(newinds), inds.length); - if (err != 0) throw new RuntimeException("GMat apply(I) error" + cudaGetErrorString(err)); - out; - } - } - } - -// override def apply(i1:IMat):GMat = applyi(Array(i1), null); - override def apply(i1:IMat, i2:IMat):GMat = applyi(Array(i1, i2), null); - override def apply(i1:IMat, i2:Int):GMat = applyi(Array(i1, IMat.ielem(i2)), null); - override def apply(i1:Int, i2:IMat):GMat = applyi(Array(IMat.ielem(i1), i2), null); - - override def applyi(inds:Array[IMat]):GMat = applyi(inds, null); - - def applyi(inds:Array[IMat], omat:Mat):GMat = { - if (inds.length > 2 && inds.length != _dims.length) throw new RuntimeException("GMat applyi dims must match"); - val mydims = if (inds.length == 1) Array(length) else if (inds.length == 2) Array(nrows, ncols) else _dims; - val newdims = new Array[Int](inds.length) - val newinds = new Array[GIMat](inds.length) - for (i <- 0 until inds.length) { - inds(i) match { - case aa:MatrixWildcard => { - newdims(i) = mydims(i); - } - case _ => { - newdims(i) = inds(i).length; - newinds(i) = getIndexMat(i, inds(i)); - } - } - } - val out = GMat.newOrCheckGMat(newdims, omat, GUID, ND.hashGUIDs(inds), "apply".##); - inds.length match { - case 1 => { - val err = CUMAT.copyFromInds(pdata, out.pdata, safePointer(newinds(0)), newdims(0)); - if (err != 0) throw new RuntimeException("GMat apply(I) error: " + cudaGetErrorString(err)); - } - case 2 => { - val err = CUMAT.copyFromInds2D(pdata, dims(0), out.pdata, newdims(0), safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1)); - if (err != 0) throw new RuntimeException("GMat apply(I, J) error: " + cudaGetErrorString(err)); - } - case 3 => { - val err = CUMAT.copyFromInds3D(pdata, dims(0), dims(1), out.pdata, newdims(0), newdims(1), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1), safePointer(newinds(2)), newdims(2)); - if (err != 0) throw new RuntimeException("GMat apply(I, J, K) error: " + cudaGetErrorString(err)); - } - case 4 => { - val err = CUMAT.copyFromInds4D(pdata, dims(0), dims(1), dims(2), out.pdata, newdims(0), newdims(1), newdims(2), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1), safePointer(newinds(2)), newdims(2), safePointer(newinds(3)), newdims(3)); - if (err != 0) throw new RuntimeException("GMat apply(I, J, K, L) error: " + cudaGetErrorString(err)); - } - case _ => throw new RuntimeException("GMat slice access with more than 4 indices not supported"); - } - out; - } - - /** 1D update */ - - override def update(i:Int, v:Float):GMat = { - val tmp = new Array[Float](1); - tmp(0) = v; - GMat.CPUtoGPUarraycopy(tmp, 0, pdata, i, 1, "GMat update"); - this - } - - /** 2D update */ - - override def update(i:Int, j:Int, v:Float):GMat = { - val tmp = new Array[Float](1); - tmp(0) = v; - GMat.CPUtoGPUarraycopy(tmp, 0, pdata, i + j * nrows, 1, "GMat update"); - this - } - - /** ND update */ - - override def updatev(inds:Array[Int], v:Float):GMat = { - val indx = ND.linearize(inds, _dims); - val tmp = Array[Float](v); - GMat.CPUtoGPUarraycopy(tmp, 0, pdata, indx, 1, "GMat update"); - this - } - - /** ND sliced updates */ - - override def update(inds:IMat, vv:FMat):GMat = updatei(inds, GMat(vv)); - - override def update(iv:IMat, jv:IMat, b:FMat):FMat = updatei(Array(iv, jv), GMat(b)); - override def update(iv:IMat, j:Int, b:FMat):FMat = updatei(Array(iv, IMat.ielem(j)), GMat(b)); - override def update(i:Int, jv:IMat, b:FMat):FMat = updatei(Array(IMat.ielem(i), jv), GMat(b)); - - override def update(inds:IMat, vv:Mat):FMat = updatei(inds, GMat(vv)); - - override def update(iv:IMat, jv:IMat, b:Mat):FMat = updatei(Array(iv, jv), GMat(b)); - override def update(iv:IMat, j:Int, b:Mat):FMat = updatei(Array(iv, IMat.ielem(j)), GMat(b)); - override def update(i:Int, jv:IMat, b:Mat):FMat = updatei(Array(IMat.ielem(i), jv), GMat(b)); - -// override def update(i1:IMat, vv:Float):FMat = updatei(Array(i1), vv); - override def update(i1:IMat, i2:IMat, vv:Float):FMat = updatei(Array(i1, i2), vv); - - override def updatei(inds:Array[IMat], vv:FMat):GMat = updatei(inds, GMat(vv)); - - def updatei(inds:IMat, vv:GMat):GMat = { - val newinds = getIndexMat(0, inds); - val err = inds match { - case aa:MatrixWildcard => { - if (vv.length != length) throw new RuntimeException("GMat column update length mismatch") - CUMAT.copyToInds(vv.pdata, pdata, safePointer(newinds), length); - } - case _ => { - if (inds.length != vv.length) throw new RuntimeException("GMat column update length mismatch") - CUMAT.copyToInds(vv.pdata, pdata, safePointer(newinds), inds.length); - } - } - if (err != 0) throw new RuntimeException("GMat update (I)=v error " + cudaGetErrorString(err)); - this; - } - - def updatei(inds:Array[IMat], vv:GMat):GMat = { - if (inds.length > 2 && inds.length != _dims.length) throw new RuntimeException("GMat update dims must match") - val mydims = if (inds.length == 2) Array(nrows, ncols) else _dims; - val newdims = new Array[Int](inds.length) - val newinds = new Array[GIMat](inds.length) - var j = 0 - for (i <- 0 until inds.length) { - inds(i) match { - case aa:MatrixWildcard => { - newdims(i) = mydims(i); - } - case _ => { - newdims(i) = inds(i).length; - newinds(i) = getIndexMat(i, inds(i)); - } - } - } - ND.checkDims("GMat update:", ND.trimDims(newdims), ND.trimDims(vv._dims)); - inds.length match { - case 2 => { - val err = CUMAT.copyToInds2D(vv.pdata, vv.dims(0), pdata, nrows, - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1)); - if (err != 0) throw new RuntimeException("GMat update (I, J)=V error " + cudaGetErrorString(err)); - } - case 3 => { - val err = CUMAT.copyToInds3D(vv.pdata, vv.dims(0), vv.dims(1), pdata, dims(0), dims(1), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1), safePointer(newinds(2)), newdims(2)); - if (err != 0) throw new RuntimeException("GMat update (I, J, K)=V error " + cudaGetErrorString(err)); - } - case 4 => { - val err = CUMAT.copyToInds4D(vv.pdata, vv.dims(0), vv.dims(1), vv.dims(2), pdata, dims(0), dims(1), dims(2), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1), safePointer(newinds(2)), newdims(2), safePointer(newinds(3)), newdims(3)); - if (err != 0) throw new RuntimeException("GMat udpate (I, J, K, L)=V error " + cudaGetErrorString(err)); - } - case _ => throw new RuntimeException("GMat slice access with more than 4 indices not supported"); - } - this - } - - override def update(inds:IMat, vv:Float):GMat = { - val newinds = getIndexMat(0, inds); - val err = inds match { - case aa:MatrixWildcard => { - CUMAT.fillToInds(vv, pdata, safePointer(newinds), length); - } - case _ => { - CUMAT.fillToInds(vv, pdata, safePointer(newinds), inds.length); - } - } - if (err != 0) throw new RuntimeException("GMat update (I)=v error " + cudaGetErrorString(err)); - this; - } - - override def updatei(inds:Array[IMat], vv:Float):GMat = { - if (inds.length > 2 && inds.length != _dims.length) throw new RuntimeException("GMat update dims must match"); - val mydims = if (inds.length == 2) Array(nrows, ncols) else _dims; - val newdims = new Array[Int](inds.length); - val newinds = new Array[GIMat](inds.length); - for (i <- 0 until inds.length) { - inds(i) match { - case aa:MatrixWildcard => { - newdims(i) = mydims(i); - } - case _ => { - newdims(i) = inds(i).length; - newinds(i) = getIndexMat(i, inds(i)); - } - } - } - inds.length match { - case 2 => { - val err = CUMAT.fillToInds2D(vv, pdata, nrows, - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1)); - if (err != 0) throw new RuntimeException("GMat update (I, J)=v error " + cudaGetErrorString(err)); - } - case 3 => { - val err = CUMAT.fillToInds3D(vv, pdata, dims(0), dims(1), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1), safePointer(newinds(2)), newdims(2)); - if (err != 0) throw new RuntimeException("GMat update (I, J, K)=v error " + cudaGetErrorString(err)); - } - case 4 => { - val err = CUMAT.fillToInds4D(vv, pdata, dims(0), dims(1), dims(2), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1), safePointer(newinds(2)), newdims(2), safePointer(newinds(3)), newdims(3)); - if (err != 0) throw new RuntimeException("GMat udpate (I, J, K, L)=v error " + cudaGetErrorString(err)); - } - case _ => throw new RuntimeException("GMat slice update with more than 4 indices not supported"); - } - this - } - - - /** explicit ND access */ - /* see if superclass methods work - override def apply(i1:Int, i2:Int, i3:Int):Float = apply(Array(i1, i2, i3)); - override def apply(i1:Int, i2:Int, i3:Int, i4:Int):Float = apply(Array(i1, i2, i3, i4)); - override def apply(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int):Float = apply(Array(i1, i2, i3, i4, i5)); - override def apply(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, i6:Int):Float = apply(Array(i1, i2, i3, i4, i5 ,i6)); -*/ - - /** ND slicing */ - /* see if superclass methods work - - override def apply(i1:IMat, i2:IMat, i3:IMat):GMat = apply(Array(i1, i2, i3), null); - override def apply(i1:IMat, i2:IMat, i3:IMat, i4:IMat):GMat = apply(Array(i1, i2, i3, i4), null); - override def apply(i1:IMat, i2:IMat, i3:IMat, i4:IMat, i5:IMat):GMat = apply(Array(i1, i2, i3, i4, i5), null); - override def apply(i1:IMat, i2:IMat, i3:IMat, i4:IMat, i5:IMat, i6:IMat):GMat = apply(Array(i1, i2, i3, i4, i5), null); - - */ - - - /** Type helpers for 2D updating with Ints */ - /* Should come from superclass - override def update(i:Int, b:Double):FMat = update(i, b.toFloat); - override def update(i:Int, b:Int):FMat = update(i, b.toFloat); - - override def update(i:Int, j:Int, b:Double):FMat = update(i, j, b.toFloat); - override def update(i:Int, j:Int, b:Int):FMat = update(i, j, b.toFloat); - */ - - /** ND single element updates */ - /* should come from superclass - override def update(i1:Int, i2:Int, i3:Int, vv:Float):FMat = update(Array(i1, i2, i3), vv) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, vv:Float):FMat = update(Array(i1, i2, i3, i4), vv) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, vv:Float):FMat = update(Array(i1, i2, i3, i4, i5), vv) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, i6:Int, vv:Float):FMat = update(Array(i1, i2, i3, i4, i5, i6), vv) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, i6:Int, i7:Int, vv:Float):FMat = update(Array(i1, i2, i3, i4, i5, i6, i7), vv) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, i6:Int, i7:Int, i8:Int, vv:Float):FMat = update(Array(i1, i2, i3, i4, i5, i6, i7, i8), vv) - - override def update(i1:Int, i2:Int, i3:Int, vv:Double):FMat = update(Array(i1, i2, i3), vv.toFloat) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, vv:Double):FMat = update(Array(i1, i2, i3, i4), vv.toFloat) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, vv:Double):FMat = update(Array(i1, i2, i3, i4, i5), vv.toFloat) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, i6:Int, vv:Double):FMat = update(Array(i1, i2, i3, i4, i5, i6), vv.toFloat) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, i6:Int, i7:Int, vv:Double):FMat = update(Array(i1, i2, i3, i4, i5, i6, i7), vv.toFloat) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, i6:Int, i7:Int, i8:Int, vv:Double):FMat = update(Array(i1, i2, i3, i4, i5, i6, i7, i8), vv.toFloat) - - override def update(i1:Int, i2:Int, i3:Int, vv:Int):FMat = update(Array(i1, i2, i3), vv.toFloat) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, vv:Int):FMat = update(Array(i1, i2, i3, i4), vv.toFloat) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, vv:Int):FMat = update(Array(i1, i2, i3, i4, i5), vv.toFloat) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, i6:Int, vv:Int):FMat = update(Array(i1, i2, i3, i4, i5, i6), vv.toFloat) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, i6:Int, i7:Int, vv:Int):FMat = update(Array(i1, i2, i3, i4, i5, i6, i7), vv.toFloat) - override def update(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, i6:Int, i7:Int, i8:Int, vv:Int):FMat = update(Array(i1, i2, i3, i4, i5, i6, i7, i8), vv.toFloat) - */ - - /* should be inherited from superclass - - override def update(i1:IMat, i2:IMat, i3:IMat, vv:FMat):GMat = update(Array(i1, i2, i3), GMat(vv)); - override def update(i1:IMat, i2:IMat, i3:IMat, i4:IMat, vv:FMat):GMat = update(Array(i1, i2, i3, i4), GMat(vv)); - override def update(i1:IMat, i2:IMat, i3:IMat, i4:IMat, i5:IMat, vv:FMat):GMat = update(Array(i1, i2, i3, i4, i5), GMat(vv)); - override def update(i1:IMat, i2:IMat, i3:IMat, i4:IMat, i5:IMat, i6:IMat, vv:FMat):GMat = update(Array(i1, i2, i3, i4, i5, i6), GMat(vv)); - - override def update(iv:IMat, jv:IMat, b:Mat):FMat = update(Array(iv, jv), GMat(b)); - override def update(iv:IMat, j:Int, b:Mat):FMat = update(Array(iv, IMat.ielem(j)), GMat(b)); - override def update(i:Int, jv:IMat, b:Mat):FMat = update(Array(IMat.ielem(i), jv), GMat(b)); - - override def update(i1:IMat, i2:IMat, i3:IMat, vv:Mat):GMat = update(Array(i1, i2, i3), GMat(vv)); - override def update(i1:IMat, i2:IMat, i3:IMat, i4:IMat, vv:Mat):GMat = update(Array(i1, i2, i3, i4), GMat(vv)); - override def update(i1:IMat, i2:IMat, i3:IMat, i4:IMat, i5:IMat, vv:Mat):GMat = update(Array(i1, i2, i3, i4, i5), GMat(vv)); - override def update(i1:IMat, i2:IMat, i3:IMat, i4:IMat, i5:IMat, i6:IMat, vv:Mat):GMat = update(Array(i1, i2, i3, i4, i5, i6), GMat(vv)); - */ - - override def colslice(a:Int, b:Int):GMat = colslice(a, b, null, 0); - - override def colslice(a:Int, b:Int, omat:Mat):GMat = colslice(a, b, omat, 0); - - override def colslice(a:Int, b:Int, omat:Mat, c:Int):GMat = { - val newdims = _dims.clone; - newdims(dims.length-1) = b-a+c; - val out = if (omat.asInstanceOf[AnyRef] != null && omat.isInstanceOf[GMat] && omat.ncols >= b-a+c && omat.nrows == nrows) { - omat.asInstanceOf[GMat] - } else { - GMat.newOrCheckGMat(newdims, omat, GUID, a, b, c, "colslice".##); - } - cudaMemcpy(out.pdata.withByteOffset(1L*c*nrows*Sizeof.FLOAT), pdata.withByteOffset(1L*a*nrows*Sizeof.FLOAT), 1L*(b-a)*nrows*Sizeof.FLOAT, cudaMemcpyDeviceToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError; - if (err != 0) throw new RuntimeException("GMat colslice() error " + cudaGetErrorString(err)); - out - } - - override def colslice(a:Int, b:Int, omat:Mat, c:Int, pb:Boolean):GMat = colslice(a, b, omat, c); - - val myGPU = SciFunctions.getGPU; - - /** reshaping */ - - override def reshape(newdims:Int*):GMat = reshape(newdims.toArray) - - override def reshape(newdims:Array[Int]):GMat = { - if (newdims.reduce(_*_) == length) { - val out = GMat.newOrCheckGMat(newdims, null, GUID, ND.hashInts(newdims), "reshape".##); - cudaMemcpy(out.pdata, pdata, 1L*llength*Sizeof.FLOAT, cudaMemcpyDeviceToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError; - if (err != 0) throw new RuntimeException("GMat reshape() error " + cudaGetErrorString(err)); - out - } else { - throw new RuntimeException("GMat reshape total length doesnt match") - } - } - - override def reshapeView(newdims:Int*):GMat = reshapeView(newdims.toArray) - - override def reshapeView(newdims:Array[Int]):GMat = { - if (newdims.reduce(_*_) == length) { - val out = new GMat(newdims, pdata, llength); - out.setGUID(MurmurHash3_x64_64(newdims.map(_.toLong) :+ GUID, "reshapeView".##)); - out - } else { - throw new RuntimeException("GMat reshapeView total length doesnt match") - } - } - - override def reshapeView(adims:IMat):GMat = reshapeView(adims.data); - - override def reshapeTrim(newdims:Int*):GMat = reshapeTrim(newdims.toArray) - - override def reshapeTrim(newdims:Array[Int]):GMat = { - if (newdims.reduce(_*_) <= realsize) { - val out = new GMat(newdims, pdata, realsize); - out.setGUID(MurmurHash3_x64_64(newdims.map(_.toLong) :+ GUID, "reshapeTrim".##)); - out - } else { - throw new RuntimeException("GMat reshapeTrim total length too large") - } - } - - override def reshapeTrim(adims:IMat):GMat = reshapeTrim(adims.data); - - /** transpose */ - override def transpose(dims:Array[Int]):GMat = _transpose(MatFunctions.irow(dims)); - - override def transpose(perm:IMat):GMat = _transpose(perm); - - override def _transpose(perm:IMat):GMat = { - val nd = _dims.length - if (perm.length != nd) { - throw new RuntimeException("GMat transpose bad permutation ") - } - if (ND.isIdentity(perm)) { - this - } else { - val xdims = MatFunctions.irow(_dims); - val iperm = MatFunctions.invperm(perm); - val pdims = xdims(perm).data; - var out = GMat.newOrCheckGMat(pdims, null, GUID, ND.hashInts(pdims), "transpose".##); - var out2 = GMat.newOrCheckGMat(pdims, null, GUID, ND.hashInts(pdims), "transpose1".##); - cudaMemcpy(out.pdata, pdata, 1L*Sizeof.FLOAT*length, cudaMemcpyDeviceToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - for (i <- (nd - 1) until 0 by -1) { - if (iperm(i) != i) { - val (d1, d2, d3) = ND.getDims(i, iperm, xdims); - if (d1 > 1 && d2 > 1) { - // println("spermute %d %d %d" format (d1,d2,d3)) - CUMAT.spermute(d1, d2, d3, out.pdata, out2.pdata); - val tmp = out2; - out2 = out; - out = tmp; - } - ND.rotate(i, iperm, xdims); - } - } - out; - } - } - - override def fromNHWCtoNCHW:GMat = { - if (dims.length != 4) throw new RuntimeException("fromNHWCtoNCHW ndims must be 4"); - transpose(MatFunctions.irow(1,2,0,3)).reshapeView(dims); - } - - override def fromNCHWtoNHWC:GMat = { - if (dims.length != 4) throw new RuntimeException("fromNCHWtoNHWC ndims must be 4"); - reshapeView(MatFunctions.irow(dims(1), dims(2), dims(0), dims(3))).transpose(MatFunctions.irow(2,0,1,3)); - } - - override def clear = { - cudaMemset(pdata, 0, Sizeof.FLOAT*length) - cudaStreamSynchronize(Mat.SyncMethod) - this - } - - override def set(v:Float):GMat = { - CUMAT.setval(pdata, v, length) - cudaStreamSynchronize(Mat.SyncMethod); - this - } - - - override def zeros(nr:Int, nc:Int) = GMat.zeros(nr, nc); - - override def zeros(nr:Int, nc:Int, nnz:Int) = GMat.zeros(nr, nc); - - override def zeros(dims:IMat):GMat = { - GMat.zeros(dims) - } - - override def ones(nr:Int, nc:Int) = GMat.ones(nr, nc); - - override def ones(dims:IMat) = GMat.ones(dims); - - override def zero = GMat.zeros(1, 1); - - override def one = GMat.ones(1, 1); - - override def izeros(m:Int, n:Int) = { - GIMat.izeros(m,n) - } - - override def izeros(dims:IMat) = { - GIMat.izeros(dims); - } - - override def iones(m:Int, n:Int) = { - GIMat.iones(m,n) - } - - override def iones(dims:IMat) = { - GIMat.iones(dims) - } - - def horzcat(aa:FMat, omat:Mat) = { - val a = GMat(aa); - if (nrows != a.nrows) - throw new RuntimeException("GMat \\ row dims not equal"); - val out = GMat.newOrCheckGMat(nrows, ncols+a.ncols, omat, GUID, a.GUID, "horzcat".##); - cudaMemcpy(out.pdata, pdata, 1L*length*Sizeof.FLOAT, cudaMemcpyKind.cudaMemcpyDeviceToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - var err = cudaGetLastError; - if (err != 0) throw new RuntimeException("GMat horzcat() error " + cudaGetErrorString(err)); - cudaMemcpy(out.pdata.withByteOffset(1L*length*Sizeof.FLOAT), a.pdata, 1L*a.length*Sizeof.FLOAT, cudaMemcpyKind.cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod); - err = cudaGetLastError; - if (err != 0) throw new RuntimeException("GMat horzcat() error " + cudaGetErrorString(err)); - out - } - - def vertcat(aa:FMat, omat:Mat) = { - val a = GMat(aa); - if (ncols != a.ncols) - throw new RuntimeException("GMat on row dims not equal"); - val out = GMat.newOrCheckGMat(nrows+a.nrows, ncols, omat, GUID, a.GUID, "vertcat".##); - cudaMemcpy2D(out.pdata, 1L*out.nrows*Sizeof.FLOAT, pdata, 1L*nrows*Sizeof.FLOAT, 1L*nrows*Sizeof.FLOAT, 1L*ncols, cudaMemcpyKind.cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod); - var err = cudaGetLastError; - if (err != 0) throw new RuntimeException("GMat vertcat() error " + cudaGetErrorString(err)); - cudaMemcpy2D(out.pdata.withByteOffset(1L*nrows*Sizeof.FLOAT), 1L*out.nrows*Sizeof.FLOAT, a.pdata, 1L*a.nrows*Sizeof.FLOAT, 1L*a.nrows*Sizeof.FLOAT, 1L*a.ncols, cudaMemcpyKind.cudaMemcpyDeviceToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - err = cudaGetLastError; - if (err != 0) throw new RuntimeException("GMat vertcat() error " + cudaGetErrorString(err)); - out - } - - def getHandle = { - val igpu = Array(0); - jcuda.runtime.JCuda.cudaGetDevice(igpu) - GFunctions.cublasHandles(igpu(0)).asInstanceOf[cublasHandle]; - } - - def GMult(aa:FMat, oldmat:Mat):GMat = { - val a = GMat(aa); - if (ncols == 1 && nrows == 1) { - val out = GMat.newOrCheckGMat(a.nrows, a.ncols, oldmat, GUID, a.GUID, "GMult1".##); - Mat.nflops += 1L * a.length; - val err = CUMAT.applyop(pdata, nrows, ncols, a.pdata, a.nrows, a.ncols, out.pdata, GMat.BinOp.op_mul); - if (err != 0) {throw new RuntimeException("GMult: CUDA kernel error in CUMAT.applyop " + cudaGetErrorString(err))}; - out - } else if (a.ncols == 1 && a.nrows == 1) { - val out = GMat.newOrCheckGMat(nrows, ncols, oldmat, GUID, a.GUID, "GMult2".##); - Mat.nflops += 1L * length; - val err = CUMAT.applyop(pdata, nrows, ncols, a.pdata, a.nrows, a.ncols, out.pdata, GMat.BinOp.op_mul); - if (err != 0) {throw new RuntimeException("GMult: CUDA kernel error in CUMAT.applyop " + cudaGetErrorString(err))} - out; - } else if (ncols == a.nrows) { - val out = GMat.newOrCheckGMat(nrows, a.ncols, oldmat, GUID, a.GUID, "GMult".##); - Mat.nflops += 2L * length * a.ncols; - if (nrows == 1) { - // cublasSgemv('t', a.nrows, a.ncols, 1.0f, a.pdata, nrows, pdata, 1, 0f, out.pdata, 1) - out.clear; - CUMAT.dmv(a.pdata, a.nrows, a.ncols, pdata, out.pdata, 1); - // Thread.sleep(0); - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError; - if (err != 0) {throw new RuntimeException("GMult: CUDA kernel error in CUMAT.dmv " + cudaGetErrorString(err))} - } else if (a.ncols == 1) { - // cublasSgemv('n', nrows, ncols, 1.0f, pdata, nrows, a.pdata, 1, 0f, out.pdata, 1) - out.clear; - CUMAT.dmv(pdata, nrows, ncols, a.pdata, out.pdata, 0); -// Thread.sleep(0) - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError; - if (err != 0) {throw new RuntimeException("GMult: CUDA kernel error in CUMAT.dmv " + cudaGetErrorString(err))} - } else { - cublasSgemm(getHandle, cublasOperation.CUBLAS_OP_N, cublasOperation.CUBLAS_OP_N, nrows, a.ncols, ncols, - GMat.pONE, pdata, nrows, a.pdata, a.nrows, GMat.pZERO, out.pdata, nrows); - if (length * a.ncols > GMat.multYieldSize) Thread.`yield`; - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError; - if (err != 0) { - println("device is %d" format SciFunctions.getGPU); - throw new RuntimeException("Cublas error in * "+err); - } - } - - out - } else throw new RuntimeException("dimensions mismatch (%d %d), (%d %d)" format (nrows, ncols, a.nrows, a.ncols)); - } - - override def mult(bb:SMat, c:FMat, at:Boolean, bt:Boolean):FMat = { - val b = GSMat(bb); - (at, bt) match { - case (false, false) => GSMult(b, c); - case (false, true) => GSMultT(b, c); - case _ => throw new RuntimeException("mult unsupported options SMat, FMat %b %b" format (at, bt)); - } - } - - override def mult(bb:FMat, c:FMat, at:Boolean, bt:Boolean):FMat = { - val b = GMat(bb); - (at, bt) match { - case (false, false) => GMult(b, c); - case (false, true) => GMultT(b, c); - case (true, false) => GTMult(b, c); - case _ => throw new RuntimeException("mult unsupported options FMat, FMat %b %b" format (at, bt)); - } - } - - override def mult(b:Mat, c:Mat, at:Boolean, bt:Boolean):Mat = { - (b, c) match { - case (bb:FMat, cc:FMat) => mult(bb, cc, at, bt); - case (bb:SMat, cc:FMat) => mult(bb, cc, at, bt); - case _ => throw new RuntimeException("mult unsupported types %s %s" format (b.mytype, c.mytype)); - } - c - } - - override def mult(b:Mat, c:Mat):Mat = mult(b, c, false, false); - - - - override def madd(bb:FMat, cc:FMat, at:Boolean, bt:Boolean):GMat = { - val b = GMat(bb); - val c = GMat(cc); - val (arows, acols, atrans) = if (at) (ncols, nrows, cublasOperation.CUBLAS_OP_T) else (nrows, ncols, cublasOperation.CUBLAS_OP_N); - val (brows, bcols, btrans) = if (bt) (b.ncols, b.nrows, cublasOperation.CUBLAS_OP_T) else (b.nrows, b.ncols, cublasOperation.CUBLAS_OP_N); - if (acols != brows || arows != c.nrows || bcols != c.ncols) { - throw new RuntimeException("madd bad dimensions (%d %d) (%d %d) (%d %d)" format (arows, acols, brows, bcols, c.nrows, c.ncols)); - } - Mat.nflops += 2L * arows * bcols * acols; - cublasSgemm(getHandle, atrans, btrans, arows, bcols, acols, GMat.pONE, pdata, nrows, b.pdata, b.nrows, GMat.pONE, c.pdata, c.nrows); - if (1L * arows * bcols * acols > GMat.multYieldSize) Thread.`yield`; - cudaStreamSynchronize(Mat.SyncMethod); - c - } - - override def madd(b:FMat, c:FMat):GMat = madd(b, c, false, false); - - override def madd(b:SMat, c:FMat, bt:Boolean, ct:Boolean):GMat = { - (bt, ct) match { - case (false, false) => madd(b, c); - case (false, true) => maddT(b, c); - case _ => throw new RuntimeException("madd unsupported options GSMat, GMat %b %b" format (bt, ct)); - } - } - - override def madd(b:FMat,c:TMat):TMat = madd(b,c,false,false) - - import BIDMat.IMatWildcard - - override def madd(bb:FMat,c:TMat,at:Boolean,bt:Boolean):TMat = { - val b = GMat(bb); - for (i <- 0 until c.tiles.length) { - val m = c.tiles(i); - if (!at) { - if (!bt) { - tileMult(m.nrows,m.ncols,ncols,c.y(i),0,b,0,c.x(i),m,0,0); - } else { - tileMultNT(m.nrows,m.ncols,ncols,c.y(i),0,b,c.x(i),0,m,0,0); - } - } else { - if (!bt) { - tileMultTN(m.nrows,m.ncols,nrows,0,c.y(i),b,0,c.x(i),m,0,0); - } else { - tileMultTT(m.nrows,m.ncols,nrows,0,c.y(i),b,c.x(i),0,m,0,0); - } - } - } - c; - } - - override def madd(b:Mat, c:Mat, at:Boolean, bt:Boolean):Mat = { - (b, c) match { - case (bb:FMat, cc:FMat) => madd(bb, cc, at, bt); - case (bb:SMat, cc:FMat) => madd(bb, cc, at, bt); - case (bb:FMat,cc:TMat) => madd(bb,cc,at,bt); - case (bb:SMat,cc:TMat) => madd(bb,cc,at,bt); - case _ => throw new RuntimeException("madd unsupported types %s %s" format (b.mytype, c.mytype)); - } - c - } - - override def madd(b:Mat, c:Mat):Mat = madd(b, c, false, false); - - override def blockmult(bb:FMat, cc:FMat, nblocks:Int, at:Boolean, bt:Boolean, cfact:Float):FMat = { - val b = GMat(bb); - val c = GMat(cc); - - val (anrows, ancols) = if (dims.length >= 3) { - (dims(0), dims(1)) - } else { - (nrows/nblocks, ncols) - } - val (bnrows, bncols) = if (b.dims.length >= 3) { - (b.dims(0), b.dims(1)) - } else { - (b.nrows/nblocks, b.ncols) - } - val (cnrows,cncols) = if (c.dims.length >= 3) { - (c.dims(0), c.dims(1)) - } else { - (c.nrows/nblocks, c.ncols) - } - blockGemm(if (at) 1 else 0, if (bt) 1 else 0, cnrows, cncols, if (at) anrows else ancols, 1f, 0, anrows, anrows*ancols, - b, 0, bnrows, bnrows*bncols, cfact, c, 0, cnrows, cnrows*cncols, nblocks); - c - } - - override def blockmult(b:FMat, c:FMat, nblocks:Int, at:Boolean, bt:Boolean):FMat = blockmult(b, c, nblocks, at, bt, 0f); - - override def blockmult(b:Mat, c:Mat, nblocks:Int):Mat = blockmult(b, c, nblocks, false, false); - - override def blockmult(b:Mat, c:Mat, nblocks:Int, at:Boolean, bt:Boolean):Mat = { - (b, c) match { - case (bb:FMat, cc:FMat) => blockmult(bb, cc, nblocks, at, bt); - case _ => throw new RuntimeException("blockmult unsupported types %s %s" format (b.mytype, c.mytype)); - } - c - } - - override def blockmult2(bb:FMat, cc:FMat, nblocks:Int, at:Boolean, bt:Boolean, cfact:Float):FMat = { - val b = GMat(bb); - val c = GMat(cc); - - val anrows = dims(0) - val astep = dims(1) - val ancols = dims(2) - val bnrows = b.dims(0) - val bstep = b.dims(1) - val bncols = b.dims(2) - val cnrows = c.dims(0) - val cstep = c.dims(1) - val cncols = c.dims(2) - if (dims.length == 3) { - blockGemm(if (at) 1 else 0, if (bt) 1 else 0, cnrows, cncols, if (at) anrows else ancols, 1f, 0, anrows*astep, anrows, - b, 0, bnrows*bstep, bnrows, cfact, c, 0, cnrows*cstep, cnrows, nblocks); - } else { - val reps2 = dims.data.slice(3, dims.length).reduce(_*_); - blockGemm4D(if (at) 1 else 0, if (bt) 1 else 0, cnrows, cncols, if (at) anrows else ancols, 1f, 0, anrows*astep, anrows, anrows*ancols*astep, - b, 0, bnrows*bstep, bnrows, bnrows*bncols*bstep, cfact, c, 0, cnrows*cstep, cnrows, cnrows*cncols*cstep, nblocks, reps2); - } - c - } - - override def blockmult2(b:FMat, c:FMat, nblocks:Int, at:Boolean, bt:Boolean):FMat = blockmult2(b, c, nblocks, at, bt, 0f); - - override def blockmult2(b:Mat, c:Mat, nblocks:Int):Mat = blockmult2(b, c, nblocks, false, false); - - override def blockmult2(b:Mat, c:Mat, nblocks:Int, at:Boolean, bt:Boolean):Mat = { - (b, c) match { - case (bb:FMat, cc:FMat) => blockmult2(bb, cc, nblocks, at, bt); - case _ => throw new RuntimeException("blockmult2 unsupported types %s %s" format (b.mytype, c.mytype)); - } - c - } - - override def blockmadd(b:FMat, c:FMat, nblocks:Int, at:Boolean, bt:Boolean):FMat = blockmult(b, c, nblocks, at, bt, 1f); - - override def blockmadd(b:Mat, c:Mat, nblocks:Int):Mat = blockmadd(b, c, nblocks, false, false); - - override def blockmadd(b:Mat, c:Mat, nblocks:Int, at:Boolean, bt:Boolean):Mat = { - (b, c) match { - case (bb:FMat, cc:FMat) => blockmadd(bb, cc, nblocks, at, bt); - case _ => throw new RuntimeException("blockmadd unsupported types %s %s" format (b.mytype, c.mytype)); - } - c - } - - override def blockmadd2(b:FMat, c:FMat, nblocks:Int, at:Boolean, bt:Boolean):FMat = blockmult2(b, c, nblocks, at, bt, 1f); - - override def blockmadd2(b:Mat, c:Mat, nblocks:Int):Mat = blockmadd2(b, c, nblocks, false, false); - - override def blockmadd2(b:Mat, c:Mat, nblocks:Int, at:Boolean, bt:Boolean):Mat = { - (b, c) match { - case (bb:FMat, cc:FMat) => blockmadd2(bb, cc, nblocks, at, bt); - case _ => throw new RuntimeException("blockmadd2 unsupported types %s %s" format (b.mytype, c.mytype)); - } - c - } - - - def GMultT(aa:FMat, oldmat:Mat):GMat = { - val a = GMat(aa); - if (ncols == a.ncols) { - val out = GMat.newOrCheckGMat(nrows, a.nrows, oldmat, GUID, a.GUID, "GMultT".##) - Mat.nflops += 2L * length * a.nrows - cublasSgemm(getHandle, cublasOperation.CUBLAS_OP_N, cublasOperation.CUBLAS_OP_T, nrows, a.nrows, ncols, - GMat.pONE, pdata, nrows, a.pdata, a.nrows, GMat.pZERO, out.pdata, nrows); - if (1L * length * a.nrows > GMat.multYieldSize) Thread.`yield`; - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cublas error in xT " + cudaGetErrorString(err)) - } - out - } else throw new RuntimeException("dimensions mismatch") - } - - /** - * Tile multiplication of a(this) * b into c. Tile coordinates are (r,c,height,width): - * a: (aroff, acoff, nr, kk) - * b: (broff, bcoff, kk, nc) - * c: (croff, ccoff, nr, nc) - * - * Note: c is not cleared by the kernel, and the result is added to it. - */ - - def tileMult(nr:Int, nc:Int, kk:Int, aroff:Int, acoff:Int, b:GMat, broff:Int, bcoff:Int, c:GMat, croff:Int, ccoff:Int):GMat = { - if (aroff < 0 || acoff < 0 || broff < 0 || bcoff < 0 || croff < 0 || ccoff < 0 || nr < 0 || nc < 0 || kk < 0) { - throw new RuntimeException("tileMul: cant have negative offsets or dimensions"); - } else if (aroff + nr > nrows || acoff + kk > ncols || broff + kk > b.nrows || bcoff + nc > b.ncols || croff + nr > c.nrows || ccoff + nc > c.ncols) { - throw new RuntimeException("tileMult: tile strays outside matrix dimensions"); - } else { - Mat.nflops += 2L * nr * nc * kk; - cublasSgemm(getHandle, cublasOperation.CUBLAS_OP_N, cublasOperation.CUBLAS_OP_N, nr, nc, kk, GMat.pONE, - pdata.withByteOffset(Sizeof.FLOAT.toLong*(aroff+acoff*nrows)), nrows, - b.pdata.withByteOffset(Sizeof.FLOAT.toLong*(broff+bcoff*b.nrows)), b.nrows, GMat.pONE, - c.pdata.withByteOffset(Sizeof.FLOAT.toLong*(croff+ccoff*c.nrows)), c.nrows); - cudaStreamSynchronize(Mat.SyncMethod); - c; - } - } - - def tileMultNT(nr:Int, nc:Int, kk:Int, aroff:Int, acoff:Int, b:GMat, broff:Int, bcoff:Int, c:GMat, croff:Int, ccoff:Int):GMat = { - if (aroff < 0 || acoff < 0 || broff < 0 || bcoff < 0 || croff < 0 || ccoff < 0 || nr < 0 || nc < 0 || kk < 0) { - throw new RuntimeException("tileMultNT: cant have negative offsets or dimensions"); - } else if (aroff + nr > nrows || acoff + kk > ncols || broff + nc > b.nrows || bcoff + kk > b.ncols || croff + nr > c.nrows || ccoff + nc > c.ncols) { - throw new RuntimeException("tileMultNT: tile strays outside matrix dimensions"); - } else { - Mat.nflops += 2L * nr * nc * kk; - cublasSgemm(getHandle, cublasOperation.CUBLAS_OP_N, cublasOperation.CUBLAS_OP_T, nr, nc, kk, GMat.pONE, - pdata.withByteOffset(Sizeof.FLOAT.toLong*(aroff+acoff*nrows)), nrows, - b.pdata.withByteOffset(Sizeof.FLOAT.toLong*(broff+bcoff*b.nrows)), b.nrows, GMat.pONE, - c.pdata.withByteOffset(Sizeof.FLOAT.toLong*(croff+ccoff*c.nrows)), c.nrows); - cudaStreamSynchronize(Mat.SyncMethod); - c; - } - } - - def tileMultTN(nr:Int, nc:Int, kk:Int, aroff:Int, acoff:Int, b:GMat, broff:Int, bcoff:Int, c:GMat, croff:Int, ccoff:Int):GMat = { - if (aroff < 0 || acoff < 0 || broff < 0 || bcoff < 0 || croff < 0 || ccoff < 0 || nr < 0 || nc < 0 || kk < 0) { - throw new RuntimeException("tileMultTN: cant have negative offsets or dimensions"); - } else if (aroff + kk > nrows || acoff + nr > ncols || broff + kk > b.nrows || bcoff + nc > b.ncols || croff + nr > c.nrows || ccoff + nc > c.ncols) { - throw new RuntimeException("tileMultTN: tile strays outside matrix dimensions"); - } else { - Mat.nflops += 2L * nr * nc * kk; - cublasSgemm(getHandle, cublasOperation.CUBLAS_OP_T, cublasOperation.CUBLAS_OP_N, nr, nc, kk, GMat.pONE, - pdata.withByteOffset(Sizeof.FLOAT.toLong*(aroff+acoff*nrows)), nrows, - b.pdata.withByteOffset(Sizeof.FLOAT.toLong*(broff+bcoff*b.nrows)), b.nrows, GMat.pONE, - c.pdata.withByteOffset(Sizeof.FLOAT.toLong*(croff+ccoff*c.nrows)), c.nrows); - cudaStreamSynchronize(Mat.SyncMethod); - c; - } - } - - def tileMult(nr:Int, nc:Int, kk:Int, aroff:Int, acoff:Int, b:GSMat, broff:Int, bcoff:Int, c:GMat, croff:Int, ccoff:Int):GMat = { - if (aroff < 0 || acoff < 0 || broff < 0 || bcoff < 0 || croff < 0 || ccoff < 0 || nr < 0 || nc < 0 || kk < 0) { - throw new RuntimeException("tileMult: cant have negative offsets or dimensions"); - } else if (aroff + nr > nrows || acoff + kk > ncols || broff + kk > b.nrows || bcoff + nc > b.ncols || croff + nr > c.nrows || ccoff + nc > c.ncols) { - throw new RuntimeException("tileMult: tile strays outside matrix dimensions"); - } else { - Mat.nflops += 2L * nr * b.nnz; - val err = CUMAT.dsmultTile(nr, nc, kk, b.nnz, - pdata.withByteOffset(Sizeof.FLOAT.toLong*(aroff+acoff*nrows)), nrows, - b.pdata, b.pir, b.pic, broff, bcoff, - c.pdata.withByteOffset(Sizeof.FLOAT.toLong*(croff+ccoff*c.nrows)), c.nrows, 0); - cudaStreamSynchronize(Mat.SyncMethod); - if (err != 0) { - throw new RuntimeException("CUMAT.tileMult error " + cudaGetErrorString(err)) - } - c; - } - } - - def tileMultNT(nr:Int, nc:Int, kk:Int, aroff:Int, acoff:Int, b:GSMat, broff:Int, bcoff:Int, c:GMat, croff:Int, ccoff:Int):GMat = { - if (aroff < 0 || acoff < 0 || broff < 0 || bcoff < 0 || croff < 0 || ccoff < 0 || nr < 0 || nc < 0 || kk < 0) { - throw new RuntimeException("tileMultNT: cant have negative offsets or dimensions"); - } else if (aroff + nr > nrows || acoff + kk > ncols || broff + nc > b.nrows || bcoff + kk > b.ncols || croff + nr > c.nrows || ccoff + nc > c.ncols) { - throw new RuntimeException("tileMultNT: tile strays outside matrix dimensions"); - } else { - Mat.nflops += 2L * nr * b.nnz * kk / b.ncols; - val err = CUMAT.dsmultTile(nr, nc, kk, b.nnz, - pdata.withByteOffset(Sizeof.FLOAT.toLong*(aroff+acoff*nrows)), nrows, - b.pdata, b.pir, b.pic, broff, bcoff, - c.pdata.withByteOffset(Sizeof.FLOAT.toLong*(croff+ccoff*c.nrows)), c.nrows, 1); - cudaStreamSynchronize(Mat.SyncMethod); - if (err != 0) { - throw new RuntimeException("CUMAT.tileMultT error " + cudaGetErrorString(err)) - } - c; - } - } - - override def tileMult(nr:Int, nc:Int, kk:Int, aroff:Int, acoff:Int, b:Mat, broff:Int, bcoff:Int, c:Mat, croff:Int, ccoff:Int):GMat = { - (b, c) match { - case (sb:GSMat, fc:GMat) => tileMult(nr, nc, kk, aroff, acoff, sb, broff, bcoff, fc, croff, ccoff); - case (fb:GMat, fc:GMat) => tileMult(nr, nc, kk, aroff, acoff, fb, broff, bcoff, fc, croff, ccoff); - case _ => throw new RuntimeException("tileMult couldnt match matrix types") - } - } - - override def tileMultNT(nr:Int, nc:Int, kk:Int, aroff:Int, acoff:Int, b:Mat, broff:Int, bcoff:Int, c:Mat, croff:Int, ccoff:Int):GMat = { - (b, c) match { - case (sb:GSMat, fc:GMat) => tileMultNT(nr, nc, kk, aroff, acoff, sb, broff, bcoff, fc, croff, ccoff); - case (fb:GMat, fc:GMat) => tileMultNT(nr, nc, kk, aroff, acoff, fb, broff, bcoff, fc, croff, ccoff); - case _ => throw new RuntimeException("tileMultT couldnt match matrix types") - } - } - - override def tileMultTN(nr:Int, nc:Int, kk:Int, aroff:Int, acoff:Int, b:Mat, broff:Int, bcoff:Int, c:Mat, croff:Int, ccoff:Int):GMat = { - (b, c) match { -// case (sb:GSMat, fc:GMat) => tileMultTN(nr, nc, kk, aroff, acoff, sb, broff, bcoff, fc, croff, ccoff); - case (fb:GMat, fc:GMat) => tileMultTN(nr, nc, kk, aroff, acoff, fb, broff, bcoff, fc, croff, ccoff); - case _ => throw new RuntimeException("tileMultT couldnt match matrix types") - } - } - - def GTMult(aa:FMat, oldmat:Mat):GMat = { - val a= GMat(aa); - if (nrows == a.nrows) { - val out = GMat.newOrCheckGMat(ncols, a.ncols, oldmat, GUID, a.GUID, "GMultT".##) - Mat.nflops += 2L * length * a.ncols - cublasSgemm(getHandle, cublasOperation.CUBLAS_OP_T, cublasOperation.CUBLAS_OP_N, ncols, a.ncols, nrows, - GMat.pONE, pdata, nrows, a.pdata, a.nrows, GMat.pZERO, out.pdata, out.nrows); - if (1L * length * a.ncols > GMat.multYieldSize) Thread.`yield`; - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cublas error in Tx " + cudaGetErrorString(err)) - } - out - } else throw new RuntimeException("dimensions mismatch") - } - - def GSMult(aa:SMat, oldmat:Mat):GMat = { - val a = GSMat(aa); - if (ncols != a.nrows) { - throw new RuntimeException("GSMult dimensions mismatch (%d %d) (%d %d)" format (nrows, ncols, a.nrows, a.ncols)) - } - val out = GMat.newOrCheckGMat(nrows, a.ncols, oldmat, GUID, a.GUID, "GSMult".##); - out.clear; - madd(a, out); - } - - override def madd(aa:SMat, oo:FMat):GMat = { - val a = GSMat(aa); - val out = GMat(oo); - if (ncols != a.nrows || nrows != out.nrows || a.ncols != out.ncols) { - throw new RuntimeException("GSMadd dimensions mismatch (%d %d) (%d %d) (%d %d)" format (nrows, ncols, a.nrows, a.ncols, out.nrows, out.ncols)) - } - Mat.nflops += 2L * nrows * a.nnz; -/* if (nrows == 1) { // Alas, throws "too many resources requested for launch" with large a.nrows - val handle = GSMat.getHandle // Also gives erroneous values - val descra = GSMat.getDescr - var err = JCusparse.cusparseScsrmv(handle, cusparseOperation.CUSPARSE_OPERATION_NON_TRANSPOSE, - ncols, a.ncols, 1.0f, descra, a.pdata, a.jc, a.ir, pdata, 0, out.pdata) - cudaStreamSynchronize(Mat.SyncMethod)() - if (err == 0) err = cudaGetLastError - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in GSMult " + cudaGetErrorString(err)) - } - } else { */ - val err = CUMAT.dsmult(nrows, a.ncols, a.nnz, pdata, a.pdata, a.pir, a.pic, out.pdata); - if (err != 0) throw new RuntimeException("GMult: CUDA kernel error in CUMAT.dsmult " + cudaGetErrorString(err)); - // } - out; - } - - def GSMultT(aa:SMat, oldmat:Mat):GMat = { - val a = GSMat(aa); - if (ncols != a.ncols) { - throw new RuntimeException("GSMult dimensions mismatch (%d %d) (%d %d)" format (nrows, ncols, a.ncols, a.nrows)) - } - val out = GMat.newOrCheckGMat(nrows, a.nrows, oldmat, GUID, a.GUID, "GSMultT".##); - out.clear; - maddT(a, out); - } - - override def maddT(aa:SMat, oo:FMat):GMat = { - val a = GSMat(aa); - val out = GMat(oo); - if (ncols != a.ncols || nrows != out.nrows || a.nrows != out.ncols) { - throw new RuntimeException("GSMadd dimensions mismatch (%d %d) (%d %d) (%d %d)" format (nrows, ncols, a.nrows, a.ncols, out.nrows, out.ncols)) - } - Mat.nflops += 2L * nrows * a.nnz; - val err = CUMAT.dsmultT(nrows, a.ncols, a.nnz, pdata, a.pdata, a.pir, a.pic, out.pdata); - if (err != 0) throw new RuntimeException("GMult: CUDA kernel error in CUMAT.dsmultT " + cudaGetErrorString(err)); - out - } - - def GMST(aa:FMat, oldmat:Mat):GMat = { - val a = GMat(aa); - if (ncols == a.ncols) { - val out = GMat.newOrCheckGMat(nrows, a.nrows, oldmat, GUID, a.GUID, "GMST".##) - Mat.nflops += 2L * nrows * a.nrows * ncols - out.clear - val err = CUMAT.maxsumx(pdata, nrows, a.pdata, a.nrows, out.pdata, nrows, ncols, nrows, a.nrows); - if (err != 0) throw new RuntimeException("GMult: CUDA kernel error in CUMAT.maxsumx " + cudaGetErrorString(err)) - out - } else throw new RuntimeException("dimensions mismatch") - } - - override def kron(aa:FMat, oldmat:Mat):GMat = { - val a = GMat(aa); - val out = GMat.newOrCheckGMat(nrows * a.nrows, ncols * a.ncols, oldmat, GUID, a.GUID, "kron".##); - Mat.nflops += 1L * out.nrows * out.ncols; - val err = CUMAT.kron(pdata, a.pdata, out.pdata, nrows, ncols, a.nrows, a.ncols); - if (err != 0) throw new RuntimeException("kron: CUDA kernel error in CUMAT.kron " + cudaGetErrorString(err)); - out; - } - - def gOp(aa:FMat, oldmat:Mat, op:Int):GMat = { - val a = GMat(aa); - val (nr, nc, nra, nca) = ND.compatibleGDims(_dims, aa._dims, "DenseMat Op"); - val dims = ND.maxDims(_dims, aa._dims); - val out = GMat.newOrCheckGMat(dims, oldmat, GUID, aa.GUID, op.hashCode); - Mat.nflops += scala.math.max(length, a.length); - val err = CUMAT.applyop(pdata, nr, nc, a.pdata, nra, nca, out.pdata, op); - if (err != 0) {throw new RuntimeException("CUDA kernel error %d in CUMAT.applyop" format err)} - out - } - - override def dot(aa:FMat, oldmat:Mat):GMat = { - val a = GMat(aa); - ND.checkDims("dot", dims, a.dims); - val odims = IMat.iones(1, dims.length); - odims(dims.length-1) = a.ncols; - val out = GMat.newOrCheckGMat(odims, oldmat, GUID, a.GUID, "dot".##); - Mat.nflops += 2L * length; - val err = CUMAT.reducebin1op(nrows, ncols, pdata, a.pdata, out.pdata, op_mul, op_add); - if (err != 0) {throw new RuntimeException("GMat dot: CUDA kernel error in CUMAT.reducebin1op " + cudaGetErrorString(err))} - out; - } - - override def dot (a:FMat):GMat = dot(a, null) - - override def dotr (aa:FMat, oldmat:Mat):GMat = { - val a = GMat(aa); - ND.checkDims("dotr", dims, a.dims); - val odims = a.dims.copy; - odims(odims.length-1) = 1; - val out = GMat.newOrCheckGMat(odims, oldmat, GUID, a.GUID, "dotr".##); - Mat.nflops += 2L * length; - val err = CUMAT.reducebin2op(nrows, ncols, pdata, a.pdata, out.pdata, op_mul, op_add); - if (err != 0) {throw new RuntimeException("GMat dotr: CUDA kernel error in CUMAT.reducebin2op " + cudaGetErrorString(err))} - out; - } - - override def dotr (a:FMat):GMat = dotr(a, null) - - override def ddot (a:Mat):Double = - if (nrows != a.nrows || ncols != a.ncols) { - throw new RuntimeException("ddot dims not compatible") - } else { - a match { - case aa:GMat => { - val result = Array(0f) - cublasSdot(getHandle, length, pdata, 1, aa.pdata, 1, Pointer.to(result)) - cudaStreamSynchronize(Mat.SyncMethod) - val err = cudaGetLastError - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cublas error in ddot " + cudaGetErrorString(err)) - } - result(0); - } - } - } - - override def ddot(a:FMat):Double = { - val am:Mat = a; - ddot(am); - } - - def reduceOp(oldmat:Mat, dir:Int, initval:Float, op:Int):GMat = { - if (dir == 1 || (dir == 0 && nrows > 1)) { - val out = GMat.newOrCheckGMat(1, ncols, oldmat, GUID, 1, op) - out.clear - val err = CUMAT.reduce1op(nrows, ncols, pdata, out.pdata, initval, op) - if (err != 0) {throw new RuntimeException("CUDA kernel error in CUMAT.reduce1op " + cudaGetErrorString(err))} - Mat.nflops += length - out - } else if (dir == 2 || dir == 0) { - val out = GMat.newOrCheckGMat(nrows, 1, oldmat, GUID, 2, op) - out.clear - val err = CUMAT.reduce2op(nrows, ncols, pdata, out.pdata, initval, op) - if (err != 0) {throw new RuntimeException("CUDA kernel error in CUMAT.reduce2op " + cudaGetErrorString(err))} - Mat.nflops += length - out - } else { - throw new RuntimeException("dimension must be 1 or 2") - } - } - - def toFMat(a:Mat):FMat = { - val out = FMat.newOrCheckFMat(nrows, ncols, a, GUID, "toFMat".##) - cublasGetVector(nrows*ncols, Sizeof.FLOAT, pdata, 1, Pointer.to(out.data), 1) - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cublas error in toFMat " + cudaGetErrorString(err)) - } - out - } - - def vecAdd(fromi:Int, b:GMat, toi:Int, n:Int):GMat = { - val bb = b.pdata.withByteOffset(toi*Sizeof.FLOAT); - CUMAT.applyop(pdata.withByteOffset(fromi*Sizeof.FLOAT), n, 1, bb, n, 1, bb, GMat.BinOp.op_add); - b; - } - - override def vecAdd(fromi:Int, b:Mat, toi:Int, n:Int):Mat = { - b match { - case bb:GMat => vecAdd(fromi, bb, toi, n); - } - } - - def tileCopy(fromrow:Int, fromcol:Int, to:GMat, torow:Int, tocol:Int, height:Int, width:Int):GMat = { - val toindx = torow + tocol * to.nrows; - val fromindx = fromrow + fromcol * nrows; - cudaMemcpy2D(to.pdata.withByteOffset(toindx * Sizeof.FLOAT), to.nrows*Sizeof.FLOAT, pdata.withByteOffset(fromindx * Sizeof.FLOAT), nrows*Sizeof.FLOAT, - height*Sizeof.FLOAT, width, cudaMemcpyKind.cudaMemcpyDeviceToDevice); - to - } - - override def tileCopy(fromrow:Int, fromcol:Int, to:Mat, torow:Int, tocol:Int, height:Int, width:Int):FMat = { - tileCopy(fromrow, fromcol, to.asInstanceOf[GMat], torow, tocol, height, width); - } - - def copyFrom(aa:FMat):GMat = { - ND.checkDims("GMat copyFrom FMat", dims, aa.dims); - aa match { - case in:GMat => cudaMemcpy(pdata, in.pdata, 1L*nrows*ncols*Sizeof.FLOAT, cudaMemcpyKind.cudaMemcpyDeviceToDevice); - case in:FMat => cudaMemcpy(pdata, Pointer.to(in.data), 1L*nrows*ncols*Sizeof.FLOAT, cudaMemcpyKind.cudaMemcpyHostToDevice); - } - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError; - if (err != 0) { - println("device is %d" format SciFunctions.getGPU); - throw new RuntimeException("Cublas error in copyFrom " + cudaGetErrorString(err)); - } - this - } - - override def copyTo(a:FMat):FMat = { - ND.checkDims("copyTo", dims, a.dims); -// val a = out.recycle(nrows, ncols, 0) - cublasGetVector(length, Sizeof.FLOAT, pdata, 1, Pointer.to(a.data), 1) - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError; - if (err != 0) { - println("device is %d" format SciFunctions.getGPU); - throw new RuntimeException("Cublas error in copyTo " + cudaGetErrorString(err)); - } - a; - } - - def copyTo(a:GIMat):GIMat = { - ND.checkDims("copyTo", dims, a.dims) - val err = CUMAT.floatToInt(pdata, a.pdata, length); - if (err != 0) { - println("device is %d" format SciFunctions.getGPU); - throw new RuntimeException("error in copyTo " + cudaGetErrorString(err)); - } - a - } - - def copyTo(a:GMat):GMat = { - ND.checkDims("GMat copyTo GMat", dims, a.dims); - cudaMemcpy(a.pdata, pdata, 1L*length*Sizeof.FLOAT, cudaMemcpyKind.cudaMemcpyDeviceToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError; - if (err != 0) { - println("device is %d" format SciFunctions.getGPU); - throw new RuntimeException("Cublas error in copyTo " + cudaGetErrorString(err)); - } - a - } - - def copyTo(a:GDMat):GDMat = { - ND.checkDims("GMat copyTo GMat", dims, a.dims); - CUMAT.floatToDouble(pdata, a.pdata, a.length); - val err = cudaGetLastError; - if (err != 0) { - println("device is %d" format SciFunctions.getGPU); - throw new RuntimeException("GMat error in copyTo " + cudaGetErrorString(err)); - } - a - } - - override def copyTo(out:Mat):Mat = { - out match { - case a:GMat => copyTo(a) - case a:GIMat => copyTo(a) - case a:GDMat => copyTo(a) - case a:FMat => copyTo(a) - case a:TMat => copyTo(a) - } - } - - override def copy() = { - val out = GMat.newOrCheckGMat(dims, null, GUID, "GMat.copy".##) - copyTo(out) - } - - - override def recycle(nr:Int, nc:Int, nnz:Int):GMat = { - if (nrows == nr && nc == ncols) { - this - } else if (realsize >= nr*nc) { - new GMat(nr, nc, pdata, realsize) - } else { -// free - GMat(nr, nc) - } - } - - override def free() = { - if (pdata == null) throw new RuntimeException("attempt to free a free'd matrix"); - cudaFree(pdata) - pdata = null; - this - } - - override def finalize = { -// if (pdata != null) free - } - - override def getdiag():GMat = { - if (nrows != ncols) throw new RuntimeException("getdiag requires a square matrix, but dims= %d %d" format (nrows, ncols)) - val out = GMat.newOrCheckGMat(nrows, 1, null, GUID, "getdiag".##) - cudaMemcpy2D(out.pdata, Sizeof.FLOAT, pdata, (nrows+1)*Sizeof.FLOAT, Sizeof.FLOAT, nrows, cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - val err = cudaGetLastError() - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in getdiag " + cudaGetErrorString(err)) - } - out - } - - - override def mkdiag():GMat = { - if (math.min(nrows, ncols) != 1) throw new RuntimeException("mkdiag requires a vector argument, but dims= %d %d" format (nrows, ncols)) - val size = math.max(nrows, ncols) - val out = GMat.newOrCheckGMat(size, size, null, GUID, "mkdiag".##) - out.clear - var err = cudaMemcpy2D(out.pdata, (nrows+1)*Sizeof.FLOAT, pdata, Sizeof.FLOAT, Sizeof.FLOAT, nrows, cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - if (err == 0) err = cudaGetLastError() - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in mkdiag " + cudaGetErrorString(err)) - } - out - } - - def blockGemm(transa:Int, transb:Int, nr:Int, nc:Int, k:Int, alpha:Float, aoff:Int, lda:Int, astep:Int, - b:GMat, boff:Int, ldb:Int, bstep:Int, beta:Float, c:GMat, coff:Int, ldc:Int, cstep:Int, nreps:Int):GMat = { - - Mat.nflops += 2L * nr * nc * k * nreps; - if (lda > astep || ldb > bstep || ldc > cstep) { - CUMAT.myCublasSgemmStridedBatched( - getHandle, transa, transb, - nr, nc, k, - alpha, - pdata.withByteOffset(1L * Sizeof.FLOAT * aoff), lda, astep, - b.pdata.withByteOffset(1L * Sizeof.FLOAT * boff), ldb, bstep, - beta, - c.pdata.withByteOffset(1L * Sizeof.FLOAT * coff), ldc, cstep, - nreps); - } else { - cublasSgemmStridedBatched( - getHandle, transa, transb, - nr, nc, k, - Pointer.to(Array(alpha)), - pdata.withByteOffset(1L * Sizeof.FLOAT * aoff), lda, astep, - b.pdata.withByteOffset(1L * Sizeof.FLOAT * boff), ldb, bstep, - Pointer.to(Array(beta)), - c.pdata.withByteOffset(1L * Sizeof.FLOAT * coff), ldc, cstep, - nreps); - } - cudaStreamSynchronize(Mat.SyncMethod) - - val err = cudaGetLastError() - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in GMat blockGemm " + cudaGetErrorString(err)) - } - c; - } - - override def blockGemm(transa:Int, transb:Int, nr:Int, nc:Int, k:Int, alpha:Float, aoff:Int, lda:Int, astep:Int, - b:Mat, boff:Int, ldb:Int, bstep:Int, beta:Float, c:Mat, coff:Int, ldc:Int, cstep:Int, nreps:Int):GMat = { - blockGemm(transa, transb, nr, nc, k, alpha, aoff, lda, astep, b.asInstanceOf[GMat], boff, ldb, bstep, - beta, c.asInstanceOf[GMat], coff, ldc, cstep, nreps); - } - - def blockGemm4D(transa:Int, transb:Int, nr:Int, nc:Int, k:Int, alpha:Float, - aoff:Int, lda:Int, astep1:Int, astep2:Int, - b:GMat, boff:Int, ldb:Int, bstep1:Int, bstep2:Int, beta:Float, - c:GMat, coff:Int, ldc:Int, cstep1:Int, cstep2:Int, nreps1:Int, nreps2:Int):GMat = { - - Mat.nflops += 2L * nr * nc * k * nreps1 * nreps2; - CUMAT.myCublasSgemmStridedBatched4D( - getHandle, transa, transb, nr, nc, k, alpha, - pdata.withByteOffset(1L * Sizeof.FLOAT * aoff), lda, astep1, astep2, - b.pdata.withByteOffset(1L * Sizeof.FLOAT * boff), ldb, bstep1, bstep2, beta, - c.pdata.withByteOffset(1L * Sizeof.FLOAT * coff), ldc, cstep1, cstep2, - nreps1, nreps2); - cudaStreamSynchronize(Mat.SyncMethod) - - val err = cudaGetLastError() - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in GMat blockGemm4D " + cudaGetErrorString(err)) - } - c; - } - - override def blockGemm4D(transa:Int, transb:Int, nr:Int, nc:Int, k:Int, alpha:Float, aoff:Int, lda:Int, astep1:Int, astep2:Int, - b:Mat, boff:Int, ldb:Int, bstep1:Int, bstep2:Int, beta:Float, c:Mat, coff:Int, ldc:Int, cstep1:Int, cstep2:Int, nreps1:Int, nreps2:Int):GMat = { - blockGemm4D(transa, transb, nr, nc, k, alpha, aoff, lda, astep1, astep2, b.asInstanceOf[GMat], boff, ldb, bstep1, bstep2, - beta, c.asInstanceOf[GMat], coff, ldc, cstep1, cstep2, nreps1, nreps2); - } - - override def cumsumByKey(fkeys:FMat, omat:Mat):GMat = { - val keys = GMat(fkeys); - if (nrows != keys.nrows || ncols != keys.ncols) - throw new RuntimeException("cumsumKey dimensions mismatch"); - val out = GMat.newOrCheckGMat(nrows, ncols, omat, GUID, keys.GUID, "cumsumKey".##); - Mat.nflops += 2L*length; - if (nrows == 1 || ncols == 1) { - val err = CUMAT.cumsumByKeyFF(pdata, keys.pdata, out.pdata, llength); - if (err != 0) { - throw new RuntimeException("CUMAT.cumsumByKey error " + cudaGetErrorString(err)) - } - } else { - val tmp = GLMat(nrows, ncols); - var err = CUMAT.embedmat2d(keys.pdata, tmp.pdata, nrows, ncols, 0); - if (err == 0) err = CUMAT.cumsumByKeyFL(pdata, tmp.pdata, out.pdata, llength); - if (err != 0) { - throw new RuntimeException("CUMAT.cumsumByKey error " + cudaGetErrorString(err)) - } - tmp.free; - } - out - } - - override def cumsumByKey(ikeys:IMat, omat:Mat):GMat = { - val keys = GIMat(ikeys); - if (nrows != keys.nrows || ncols != keys.ncols) - throw new RuntimeException("cumsumKey dimensions mismatch"); - val out = GMat.newOrCheckGMat(nrows, ncols, omat, GUID, keys.GUID, "cumsumKey".##); - Mat.nflops += 2L*length; - if (nrows == 1 || ncols == 1) { - val err = CUMAT.cumsumByKeyFI(pdata, keys.pdata, out.pdata, llength); - if (err != 0) { - throw new RuntimeException("CUMAT.cumsumByKey error " + cudaGetErrorString(err)) - } - } else { - val tmp = GLMat(nrows, ncols); - var err = CUMAT.embedmat2d(keys.pdata, tmp.pdata, nrows, ncols, 0); - if (err == 0) err = CUMAT.cumsumByKeyFL(pdata, tmp.pdata, out.pdata, llength); - if (err != 0) { - throw new RuntimeException("CUMAT.cumsumByKey error " + cudaGetErrorString(err)) - } - tmp.free; - } - out - } - - override def cumsumByKey(keys:FMat):GMat = cumsumByKey(keys, null); - - override def cumsumByKey(keys:IMat):GMat = cumsumByKey(keys, null); - - override def cummaxByKey(fkeys:FMat, omat:Mat):GMat = { - val keys = GMat(fkeys); - if (nrows != keys.nrows || ncols != keys.ncols) - throw new RuntimeException("cummaxKey dimensions mismatch"); - val out = GMat.newOrCheckGMat(nrows, ncols, omat, GUID, keys.GUID, "cummaxKey".##); - Mat.nflops += 2L*length; - if (nrows == 1 || ncols == 1) { - val err = CUMAT.cummaxByKeyFF(pdata, keys.pdata, out.pdata, llength); - if (err != 0) { - throw new RuntimeException("CUMAT.cummaxByKey error " + cudaGetErrorString(err)) - } - } else { - val tmp = GLMat(nrows, ncols); - var err = CUMAT.embedmat2d(keys.pdata, tmp.pdata, nrows, ncols, 0); - if (err == 0) err = CUMAT.cummaxByKeyFL(pdata, tmp.pdata, out.pdata, llength); - if (err != 0) { - throw new RuntimeException("CUMAT.cummaxByKey error " + cudaGetErrorString(err)) - } - tmp.free; - } - out - } - - override def cummaxByKey(ikeys:IMat, omat:Mat):GMat = { - val keys = GIMat(ikeys); - if (nrows != keys.nrows || ncols != keys.ncols) - throw new RuntimeException("cummaxKey dimensions mismatch"); - val out = GMat.newOrCheckGMat(nrows, ncols, omat, GUID, keys.GUID, "cummaxKey".##); - Mat.nflops += 2L*length; - if (nrows == 1 || ncols == 1) { - val err = CUMAT.cummaxByKeyFI(pdata, keys.pdata, out.pdata, llength); - if (err != 0) { - throw new RuntimeException("CUMAT.cummaxByKey error " + cudaGetErrorString(err)) - } - } else { - val tmp = GLMat(nrows, ncols); - var err = CUMAT.embedmat2d(keys.pdata, tmp.pdata, nrows, ncols, 0); - if (err == 0) err = CUMAT.cummaxByKeyFL(pdata, tmp.pdata, out.pdata, llength); - if (err != 0) { - throw new RuntimeException("CUMAT.cummaxByKey error " + cudaGetErrorString(err)) - } - tmp.free; - } - out - } - - override def cummaxByKey(keys:FMat):GMat = cummaxByKey(keys, null); - - override def cummaxByKey(keys:IMat):GMat = cummaxByKey(keys, null); - - override def cumminByKey(fkeys:FMat, omat:Mat):GMat = { - val keys = GMat(fkeys); - if (nrows != keys.nrows || ncols != keys.ncols) - throw new RuntimeException("cumminKey dimensions mismatch"); - val out = GMat.newOrCheckGMat(nrows, ncols, omat, GUID, keys.GUID, "cumminKey".##); - Mat.nflops += 2L*length; - if (nrows == 1 || ncols == 1) { - val err = CUMAT.cumminByKeyFF(pdata, keys.pdata, out.pdata, llength); - if (err != 0) { - throw new RuntimeException("CUMAT.cumminByKey error " + cudaGetErrorString(err)) - } - } else { - val tmp = GLMat(nrows, ncols); - var err = CUMAT.embedmat2d(keys.pdata, tmp.pdata, nrows, ncols, 0); - if (err == 0) err = CUMAT.cumminByKeyFL(pdata, tmp.pdata, out.pdata, llength); - if (err != 0) { - throw new RuntimeException("CUMAT.cumminByKey error " + cudaGetErrorString(err)) - } - tmp.free; - } - out - } - - override def cumminByKey(ikeys:IMat, omat:Mat):GMat = { - val keys = GIMat(ikeys); - if (nrows != keys.nrows || ncols != keys.ncols) - throw new RuntimeException("cumminKey dimensions mismatch"); - val out = GMat.newOrCheckGMat(nrows, ncols, omat, GUID, keys.GUID, "cumminKey".##); - Mat.nflops += 2L*length; - if (nrows == 1 || ncols == 1) { - val err = CUMAT.cumminByKeyFI(pdata, keys.pdata, out.pdata, llength); - if (err != 0) { - throw new RuntimeException("CUMAT.cumminByKey error " + cudaGetErrorString(err)) - } - } else { - val tmp = GLMat(nrows, ncols); - var err = CUMAT.embedmat2d(keys.pdata, tmp.pdata, nrows, ncols, 0); - if (err == 0) err = CUMAT.cumminByKeyFL(pdata, tmp.pdata, out.pdata, llength); - if (err != 0) { - throw new RuntimeException("CUMAT.cumminByKey error " + cudaGetErrorString(err)) - } - tmp.free; - } - out - } - - override def cumminByKey(keys:FMat):GMat = cumminByKey(keys, null); - - override def cumminByKey(keys:IMat):GMat = cumminByKey(keys, null); - - override def _reverse(omat:Mat):GMat = { - val out = GMat.newOrCheckGMat(nrows, ncols, omat, GUID, "reverse".##); - val err = CUMAT.reverse(pdata, out.pdata, llength); - if (err != 0) { - throw new RuntimeException("CUMAT.reverse error " + cudaGetErrorString(err)) - } - out - } - - override def reverse:GMat = _reverse(null); - - override def reverse(omat:Mat):GMat = _reverse(omat); - - // Reduce using a set of 0-based indices - def reduce(inds:Array[Int], fctn:(GMat,Int)=>GMat, opname:String):GMat = { - val alldims = MatFunctions.izeros(_dims.length,1); - val xinds = new IMat(inds.length, 1, inds); - val xdims = new IMat(_dims.length, 1, _dims); - alldims(xinds) = 1; - if (alldims.data.reduce(_+_) != inds.length) { - throw new RuntimeException(opname+ " indices arent a legal subset of dims"); - } - val restinds = MatFunctions.find(alldims == 0); - if (restinds(0) == 0) { - val tmp = transpose((restinds on xinds).data); - val tmpF = new GMat(xdims(restinds).data.reduce(_*_), xdims(xinds).data.reduce(_*_), tmp.pdata, length); - tmpF.setGUID(ND.hash3(ND.hashInts(inds), GUID, ("reduce"+opname).##)); - val reduced:GMat = fctn(tmpF, 2); - val pdims = xdims(restinds) on MatFunctions.iones(inds.length,1); - val out1 = new GMat(pdims.data, reduced.pdata, reduced.length); - out1.setGUID(ND.hash3(ND.hashInts(inds), GUID, ("reduce2"+opname).##)); - out1.transpose(MatFunctions.invperm(restinds on xinds).data) - } else { - val tmp = transpose((xinds on restinds).data); - val tmpF = new GMat(xdims(xinds).data.reduce(_*_), xdims(restinds).data.reduce(_*_), tmp.pdata, length); - tmpF.setGUID(ND.hash3(ND.hashInts(inds), GUID, ("reduce"+opname).##)); - val reduced:GMat = fctn(tmpF, 1); - val newdims = MatFunctions.iones(inds.length,1) on xdims(restinds); - val out1 = new GMat(newdims.data, reduced.pdata, reduced.length); - out1.setGUID(ND.hash3(ND.hashInts(inds), GUID, ("reduce2"+opname).##)); - out1.transpose(MatFunctions.invperm(xinds on restinds).data); - } - } - - // Reduce using a set of 0-based indices - def reduce(inds0:Array[Int], fctn:(GMat,Int)=>GMat, fred:(Pointer, Pointer, Int, Int, Int)=>Int, opname:String):GMat = { - var i = 1; - while (i < inds0.length) { - if (inds0(i-1) >= inds0(i)) { - throw new RuntimeException("GMat reduce bad index vector"); - } - i += 1; - } - var inmat = this; - var outmat = this; - var inds = MatFunctions.irow(inds0); - var nextinds = getNextInds(inds); - var restinds = inds; - while (nextinds.asInstanceOf[AnyRef] != null) { - restinds = if (restinds.length > nextinds.length) restinds.colslice(0, restinds.length-nextinds.length) else null; - val outdims = inmat.dims.copy; - outdims(nextinds) = 1; - var n = 1; - for (i <- nextinds(0) to nextinds(nextinds.length-1)) n *= inmat.dims(i); - var k = 1; - for (i <- (nextinds(nextinds.length-1)+1) until inmat.dims.length) k *= inmat.dims(i); - if (nextinds(0) == 0) { - val tmpin = inmat.reshapeView(n, k); - val tmpout = fctn(tmpin,1); - outmat = tmpout.reshapeView(outdims); - } else { - outmat = GMat.newOrCheckGMat(outdims, null, inmat.GUID, ND.hashInts(outdims.data), "GMat_reduce".##); - var m = 1; - for (i <- 0 until nextinds(0)) m *= inmat.dims(i); - fred(inmat.pdata, outmat.pdata, m, n, k); - Mat.nflops += inmat.length; - } - nextinds = getNextInds(restinds); - inmat = outmat; - } - outmat; - } - - /* - * Basic compute routines on pairs of GMats - */ - override def unary_-() = { - val minusOne = GMat.newOrCheckGMat(1,1,null,-1,"minusOne".##); - minusOne.set(-1f); - gOp(minusOne, null, op_mul) - } - - def + (a : GMat) = gOp(a, null, op_add) - def - (a : GMat) = gOp(a, null, op_sub) - def *@ (a : GMat) = gOp(a, null, op_mul) - def ∘ (a : GMat) = gOp(a, null, op_mul) - def / (a : GMat) = gOp(a, null, op_div) - def ^ (a : GMat) = gOp(a, null, op_pow) - def * (a : GMat) = GMult(a, null) - def * (a : GSMat) = GSMult(a, null) - def *^ (a : GMat) = GMultT(a, null) - def *^ (a : GSMat) = GSMultT(a, null) - def xT (a : GMat) = GMultT(a, null) - def xT (a : GSMat) = GSMultT(a, null) - def ^* (a : GMat) = GTMult(a, null) - def *+^ (a : GMat) = GMST(a, null) - def Tx (a : GMat) = GTMult(a, null) - def kron(a: GMat):GMat = kron(a, null) - def ⊗ (b : GMat) = kron(b, null) - def ∙ (a : GMat) = dot(a) - def ∙→ (a : GMat) = dotr(a) - - def > (b : GMat) = gOp(b, null, op_gt) - def < (b : GMat) = gOp(b, null, op_lt) - def == (b : GMat) = gOp(b, null, op_eq) - def === (b : GMat) = gOp(b, null, op_eq) - def >= (b : GMat) = gOp(b, null, op_ge) - def <= (b : GMat) = gOp(b, null, op_le) - def != (b : GMat) = gOp(b, null, op_ne) - - def max (b : GMat) = gOp(b, null, op_max) - def min (b : GMat) = gOp(b, null, op_min) - - override def sum(ind:Int):GMat = reduceOp(null, ind, 0f, op_add); - override def prod(ind:Int):GMat = reduceOp(null, ind, 1f, op_mul); - override def maxi(ind:Int):GMat = reduceOp(null, ind, Float.MinValue, op_max); - override def amin(ind:Int):GMat = reduceOp(null, ind, Float.MaxValue, op_min); - override def amax(ind:Int):GMat = reduceOp(null, ind, Float.MinValue, op_max); - override def mini(ind:Int):GMat = reduceOp(null, ind, Float.MaxValue, op_min); - override def mean(ind:Int):GMat = SciFunctions._mean(this, ind).asInstanceOf[GMat]; - override def variance(ind:Int):GMat = SciFunctions._variance(this, ind).asInstanceOf[GMat]; - - override def sum(inds:Array[Int]):FMat = reduce(inds, (a:GMat, dir:Int) => GFunctions.sum(a,dir,null), CUMAT.sumTensor, "sum"); - override def prod(inds:Array[Int]):FMat = reduce(inds, (a:GMat, dir:Int) => GFunctions.prod(a,dir,null), CUMAT.prodTensor, "prod"); - override def maxi(inds:Array[Int]):FMat = reduce(inds, (a:GMat, dir:Int) => GFunctions.maxi(a,dir,null), CUMAT.maxTensor, "maxi") - override def mini(inds:Array[Int]):FMat = reduce(inds, (a:GMat, dir:Int) => GFunctions.mini(a,dir,null), CUMAT.minTensor, "mini") - override def amax(inds:Array[Int]):FMat = reduce(inds, (a:GMat, dir:Int) => GFunctions.maxi(a,dir,null), CUMAT.maxTensor, "amax") - override def amin(inds:Array[Int]):FMat = reduce(inds, (a:GMat, dir:Int) => GFunctions.mini(a,dir,null), CUMAT.minTensor,"amin") - override def mean(inds:Array[Int]):FMat = reduce(inds, (a:GMat, dir:Int) => SciFunctions.mean(a,dir), "mean") - override def variance(inds:Array[Int]):FMat = reduce(inds, (a:GMat, dir:Int) => SciFunctions.variance(a,dir), "variance") - - override def sum(inds:IMat):FMat = reduce(inds.data, (a:GMat, dir:Int) => GFunctions.sum(a,dir,null), CUMAT.sumTensor, "sum"); - override def prod(inds:IMat):FMat = reduce(inds.data, (a:GMat, dir:Int) => GFunctions.prod(a,dir,null), CUMAT.prodTensor, "prod"); - override def maxi(inds:IMat):FMat = reduce(inds.data, (a:GMat, dir:Int) => GFunctions.maxi(a,dir,null), CUMAT.maxTensor, "maxi") - override def mini(inds:IMat):FMat = reduce(inds.data, (a:GMat, dir:Int) => GFunctions.mini(a,dir,null), CUMAT.minTensor, "mini") - override def amax(inds:IMat):FMat = reduce(inds.data, (a:GMat, dir:Int) => GFunctions.maxi(a,dir,null), CUMAT.maxTensor, "amax") - override def amin(inds:IMat):FMat = reduce(inds.data, (a:GMat, dir:Int) => GFunctions.mini(a,dir,null), CUMAT.minTensor,"amin") - - override def mean(inds:IMat):FMat = {val m = this.sum(inds); - m ~ m *@ (1f/SciFunctions.prod(this.dims(inds)).v); - m} - override def variance(inds:IMat):FMat = {val m = this.sum(inds); - val n = SciFunctions.prod(this.dims(inds)).v; - m ~ m *@ (1f/n) - val a = this - m; - a ~ a *@ a - val v = a.sum(inds); - v ~ v *@ (1f/n); - v} - - override def * (a : FMat) = GMult(GMat(a), null) - override def * (a : SMat) = GSMult(GSMat(a), null) - override def *^ (a : FMat) = GMultT(GMat(a), null) - override def *^ (a : SMat) = GSMultT(GSMat(a), null) - override def xT (a : FMat) = GMultT(GMat(a), null) - override def xT (a : SMat) = GSMultT(GSMat(a), null) - override def ^* (a : FMat) = GTMult(GMat(a), null) - def *+^ (a : FMat) = GMST(GMat(a), null) - override def Tx (a : FMat) = GTMult(GMat(a), null) - override def kron(a: FMat) = kron(GMat(a), null) - override def ⊗ (a : FMat) = kron(GMat(a), null) - override def + (a : FMat) = gOp(GMat(a), null, op_add) - override def - (a : FMat) = gOp(GMat(a), null, op_sub) - override def *@ (a : FMat) = gOp(GMat(a), null, op_mul) - override def ∘ (a : FMat) = gOp(GMat(a), null, op_mul) - override def / (a : FMat) = gOp(GMat(a), null, op_div) - override def ^ (a : FMat) = gOp(GMat(a), null, op_pow) - override def ∙ (a : FMat) = dot(a) - override def ∙→ (a : FMat) = dotr(a) - - override def > (a : FMat) = gOp(GMat(a), null, op_gt) - override def < (a : FMat) = gOp(GMat(a), null, op_lt) - override def == (a : FMat) = gOp(GMat(a), null, op_eq) - override def === (a : FMat) = gOp(GMat(a), null, op_eq) - override def >= (a : FMat) = gOp(GMat(a), null, op_ge) - override def <= (a : FMat) = gOp(GMat(a), null, op_le) - override def != (a : FMat) = gOp(GMat(a), null, op_ne) - - override def max (a : FMat) = gOp(GMat(a), null, op_max) - override def min (a : FMat) = gOp(GMat(a), null, op_min) - - override def + (a : Float) = gOp(GMat.elem(a), null, op_add) - override def - (a : Float) = gOp(GMat.elem(a), null, op_sub) - override def *@ (a : Float) = gOp(GMat.elem(a), null, op_mul) - override def * (a : Float) = gOp(GMat.elem(a), null, op_mul) - override def ∘ (a : Float) = gOp(GMat.elem(a), null, op_mul) - override def / (a : Float) = gOp(GMat.elem(a), null, op_div) - override def ^ (a : Float) = gOp(GMat.elem(a), null, op_pow) - - override def < (b : Float) = gOp(GMat.elem(b), null, op_lt); - override def > (b : Float) = gOp(GMat.elem(b), null, op_gt); - override def <= (b : Float) = gOp(GMat.elem(b), null, op_le); - override def >= (b : Float) = gOp(GMat.elem(b), null, op_ge); - override def == (b : Float) = gOp(GMat.elem(b), null, op_eq); - override def != (b : Float) = gOp(GMat.elem(b), null, op_ne); - - override def max (b : Float) = gOp(GMat.elem(b), null, op_max) - override def min (b : Float) = gOp(GMat.elem(b), null, op_min) - - - override def + (a : Double) = gOp(GMat(a.toFloat), null, op_add) - override def - (a : Double) = gOp(GMat(a.toFloat), null, op_sub) - override def *@ (a : Double) = gOp(GMat(a.toFloat), null, op_mul) - override def * (a : Double) = gOp(GMat(a.toFloat), null, op_mul) - override def ∘ (a : Double) = gOp(GMat(a.toFloat), null, op_mul) - override def / (a : Double) = gOp(GMat(a.toFloat), null, op_div) - override def ^ (a : Double) = gOp(GMat(a.toFloat), null, op_pow) - - override def < (b : Double) = gOp(GMat(b.toFloat), null, op_lt) - override def > (b : Double) = gOp(GMat(b.toFloat), null, op_gt) - override def <= (b : Double) = gOp(GMat(b.toFloat), null, op_le) - override def >= (b : Double) = gOp(GMat(b.toFloat), null, op_ge) - override def == (b : Double) = gOp(GMat(b.toFloat), null, op_eq) - override def != (b : Double) = gOp(GMat(b.toFloat), null, op_ne) - - override def max (b : Double) = gOp(GMat.elem(b), null, op_max) - override def min (b : Double) = gOp(GMat.elem(b), null, op_min) - - - override def + (a : Int) = gOp(GMat(a.toFloat), null, op_add) - override def - (a : Int) = gOp(GMat(a.toFloat), null, op_sub) - override def *@ (a : Int) = gOp(GMat(a.toFloat), null, op_mul) - override def * (a : Int) = gOp(GMat(a.toFloat), null, op_mul) - override def ∘ (a : Int) = gOp(GMat(a.toFloat), null, op_mul) - override def / (a : Int) = gOp(GMat(a.toFloat), null, op_div) - override def ^ (a : Int) = gOp(GMat(a.toFloat), null, op_pow) - - override def < (b : Int) = gOp(GMat(b.toFloat), null, op_lt) - override def > (b : Int) = gOp(GMat(b.toFloat), null, op_gt) - override def <= (b : Int) = gOp(GMat(b.toFloat), null, op_le) - override def >= (b : Int) = gOp(GMat(b.toFloat), null, op_ge) - override def == (b : Int) = gOp(GMat(b.toFloat), null, op_eq) - override def != (b : Int) = gOp(GMat(b.toFloat), null, op_ne) - - override def max (b : Int) = gOp(GMat.elem(b), null, op_max) - override def min (b : Int) = gOp(GMat.elem(b), null, op_min) - - - override def + (a : Long) = gOp(GMat(a.toFloat), null, op_add) - override def - (a : Long) = gOp(GMat(a.toFloat), null, op_sub) - override def *@ (a : Long) = gOp(GMat(a.toFloat), null, op_mul) - override def * (a : Long) = gOp(GMat(a.toFloat), null, op_mul) - override def ∘ (a : Long) = gOp(GMat(a.toFloat), null, op_mul) - override def / (a : Long) = gOp(GMat(a.toFloat), null, op_div) - override def ^ (a : Long) = gOp(GMat(a.toFloat), null, op_pow) - - override def < (b : Long) = gOp(GMat(b.toFloat), null, op_lt) - override def > (b : Long) = gOp(GMat(b.toFloat), null, op_gt) - override def <= (b : Long) = gOp(GMat(b.toFloat), null, op_le) - override def >= (b : Long) = gOp(GMat(b.toFloat), null, op_ge) - override def == (b : Long) = gOp(GMat(b.toFloat), null, op_eq) - override def != (b : Long) = gOp(GMat(b.toFloat), null, op_ne) - - override def max (b : Long) = gOp(GMat.elem(b), null, op_max) - override def min (b : Long) = gOp(GMat.elem(b), null, op_min) - - - def on(a : GMat) = vertcat(a, null) - def \ (a : GMat) = horzcat(a, null) - - /* - * Specialize to IMats to help the type system. - */ - override def * (b : IMat) = Mop_Times.op(this, b, null) - override def *^ (b : IMat) = Mop_TimesT.op(this, b, null) - override def xT (b : IMat) = Mop_TimesT.op(this, b, null) - override def Tx (b : IMat) = Mop_TTimes.op(this, b, null) - override def ^* (b : IMat) = Mop_TTimes.op(this, b, null) - override def + (b : IMat) = Mop_Plus.op(this, b, null) - override def - (b : IMat) = Mop_Minus.op(this, b, null) - override def *@ (b : IMat) = Mop_ETimes.op(this, b, null) - override def ∘ (b : IMat) = Mop_ETimes.op(this, b, null) - override def /< (b : IMat) = Mop_Div.op(this, b, null) - override def \\ (b : IMat) = Mop_RSolve.op(this, b, null) - override def ◁ (b : IMat) = Mop_Div.op(this, b, null) - override def ▷ (b : IMat) = Mop_RSolve.op(this, b, null) - override def / (b : IMat) = Mop_EDiv.op(this, b, null) - override def ^ (b : IMat) = Mop_Pow.op(this, b, null) - override def ∙ (b : IMat) = Mop_Dot.op(this, b, null) - override def ∙→ (b : IMat) = Mop_Dotr.op(this, b, null) - override def dot (b : IMat) = Mop_Dot.op(this, b, null) - override def dotr(b : IMat) = Mop_Dotr.op(this, b, null) - override def \ (b : IMat) = Mop_HCat.op(this, b, null) - override def on (b : IMat) = Mop_VCat.op(this, b, null) - - override def > (b : IMat) = Mop_GT.op(this, b, null) - override def < (b : IMat) = Mop_LT.op(this, b, null) - override def == (b : IMat) = Mop_EQ.op(this, b, null) - override def === (b : IMat) = Mop_EQ.op(this, b, null) - override def >= (b : IMat) = Mop_GE.op(this, b, null) - override def <= (b : IMat) = Mop_LE.op(this, b, null) - override def != (b : IMat) = Mop_NE.op(this, b, null) - - /* - * Specialize to DMats to help the type system. - */ - override def * (b : DMat) = Mop_Times.op(this, b, null) - override def *^ (b : DMat) = Mop_TimesT.op(this, b, null) - override def xT (b : DMat) = Mop_TimesT.op(this, b, null) - override def Tx (b : DMat) = Mop_TTimes.op(this, b, null) - override def ^* (b : DMat) = Mop_TTimes.op(this, b, null) - override def + (b : DMat) = Mop_Plus.op(this, b, null) - override def - (b : DMat) = Mop_Minus.op(this, b, null) - override def *@ (b : DMat) = Mop_ETimes.op(this, b, null) - override def ∘ (b : DMat) = Mop_ETimes.op(this, b, null) - override def /< (b : DMat) = Mop_Div.op(this, b, null) - override def \\ (b : DMat) = Mop_RSolve.op(this, b, null) - override def ◁ (b : DMat) = Mop_Div.op(this, b, null) - override def ▷ (b : DMat) = Mop_RSolve.op(this, b, null) - override def / (b : DMat) = Mop_EDiv.op(this, b, null) - override def ^ (b : DMat) = Mop_Pow.op(this, b, null) - override def ∙ (b : DMat) = Mop_Dot.op(this, b, null) - override def ∙→ (b : DMat) = Mop_Dotr.op(this, b, null) - override def dot (b : DMat) = Mop_Dot.op(this, b, null) - override def dotr(b : DMat) = Mop_Dotr.op(this, b, null) - override def \ (b : DMat) = Mop_HCat.op(this, b, null) - override def on (b : DMat) = Mop_VCat.op(this, b, null) - - override def > (b : DMat) = Mop_GT.op(this, b, null) - override def < (b : DMat) = Mop_LT.op(this, b, null) - override def == (b : DMat) = Mop_EQ.op(this, b, null) - override def === (b : DMat) = Mop_EQ.op(this, b, null) - override def >= (b : DMat) = Mop_GE.op(this, b, null) - override def <= (b : DMat) = Mop_LE.op(this, b, null) - override def != (b : DMat) = Mop_NE.op(this, b, null) - - - /* - * Operators whose second arg is generic. - */ - override def * (b : Mat) = Mop_Times.op(this, b, null) - override def *^ (b : Mat) = Mop_TimesT.op(this, b, null) - override def xT (b : Mat) = Mop_TimesT.op(this, b, null) - override def Tx (b : Mat) = Mop_TTimes.op(this, b, null) - override def ^* (b : Mat) = Mop_TTimes.op(this, b, null) - override def + (b : Mat) = Mop_Plus.op(this, b, null) - override def - (b : Mat) = Mop_Minus.op(this, b, null) - override def *@ (b : Mat) = Mop_ETimes.op(this, b, null) - override def ∘ (b : Mat) = Mop_ETimes.op(this, b, null) - override def / (b : Mat) = Mop_EDiv.op(this, b, null) - override def /< (b : Mat) = Mop_Div.op(this, b, null) - override def \\ (b : Mat) = Mop_RSolve.op(this, b, null) - override def ◁ (b : Mat) = Mop_Div.op(this, b, null) - override def ▷ (b : Mat) = Mop_RSolve.op(this, b, null) - override def ^ (b : Mat) = Mop_Pow.op(this, b, null) - override def ∙ (b : Mat) = Mop_Dot.op(this, b, null) - override def ∙→ (b : Mat) = Mop_Dotr.op(this, b, null) - override def dot (b : Mat) = Mop_Dot.op(this, b, null) - override def dotr (b : Mat) = Mop_Dotr.op(this, b, null) - override def \ (b : Mat) = Mop_HCat.op(this, b, null) - override def on (b : Mat) = Mop_VCat.op(this, b, null) - - override def > (b : Mat) = Mop_GT.op(this, b, null) - override def < (b : Mat) = Mop_LT.op(this, b, null) - override def >= (b : Mat) = Mop_GE.op(this, b, null) - override def <= (b : Mat) = Mop_LE.op(this, b, null) - override def == (b : Mat) = Mop_EQ.op(this, b, null) - override def === (b : Mat) = Mop_EQ.op(this, b, null) - override def != (b : Mat) = Mop_NE.op(this, b, null) - - /* - * Tilde operator - */ - def ~ (b: GMat) = new GPair(this, b) - def ~ (b: GSMat) = new GSPair(this, b) - override def ~ (b: FMat):FPair = new GPair(this, GMat(b)); - - override def ~ (b: Mat):Pair = - b match { - case t:TMat => new GTPair(this,t) - case bb:GMat => new GPair(this, bb) - case bb:GSMat => new GSPair(this, bb) - } - - /* - * @@ operator for DDS - */ - def @@ (b : GSMat) = new GDSPair(this, b) - def ^* (b : GDSPair) = MatFunctions.DDS(this, b.left, b.right, null) - def Tx (b : GDSPair) = MatFunctions.DDS(this, b.left, b.right, null) - override def ^* (b0 : DSPair) = {val b = b0.asInstanceOf[GDSPair]; MatFunctions.DDS(this, b.left, b.right, null)} - override def Tx (b0 : DSPair) = {val b = b0.asInstanceOf[GDSPair]; MatFunctions.DDS(this, b.left, b.right, null)} - -} - -class GTPair(val omat:GMat,val mat:TMat) extends Pair(omat, mat) { - override def * (a:Mat) = a match { - case g:GMat => mat.tMult(g,omat) - case g:GSMat => mat.tMult(g,omat) - } -} - -/* - * Result of a@@b for DDS - */ -class GDSPair(val left:GMat, val right:GSMat) extends DSPair {} - -/* - * GPair is the result of a~b - */ -class GPair(omat:Mat, override val mat:GMat) extends FPair(omat, mat) { - import GMat.BinOp._ - - override def t = { - val out = GMat.newOrCheckGMat(mat.ncols, mat.nrows, omat, mat.GUID, "pt".##) - CUMAT.transpose(mat.pdata, mat.nrows, out.pdata, mat.ncols, mat.nrows, mat.ncols) - out - } -/* - def * (a : GMat) = mat.GMult(a, omat) - def * (a : GSMat) = mat.GSMult(a, omat) - def *^ (a : GMat) = mat.GMultT(a, omat) - def *^ (a : GSMat) = mat.GSMultT(a, omat) - def xT (a : GMat) = mat.GMultT(a, omat) - def xT (a : GSMat) = mat.GSMultT(a, omat) - def ^* (a : GMat) = mat.GTMult(a, omat) - def *+^ (a : GMat) = mat.GMST(a, omat) - def Tx (a : GMat) = mat.GTMult(a, omat) - def kron(a: GMat):GMat = mat.kron(a, omat) - def ⊗ (b : GMat) = mat.kron(b, omat) - def + (a : GMat) = mat.gOp(a, omat, op_add) - def - (a : GMat) = mat.gOp(a, omat, op_sub) - def *@ (a : GMat) = mat.gOp(a, omat, op_mul) - def ∘ (a : GMat) = mat.gOp(a, omat, op_mul) - def / (a : GMat) = mat.gOp(a, omat, op_div) - def ^ (a : GMat) = mat.gOp(a, omat, op_pow) - def > (a : GMat) = mat.gOp(a, omat, op_gt) - def < (a : GMat) = mat.gOp(a, omat, op_lt) - def == (a : GMat) = mat.gOp(a, omat, op_eq) - def === (a : GMat) = mat.gOp(a, omat, op_eq) - def >= (a : GMat) = mat.gOp(a, omat, op_ge) - def <= (a : GMat) = mat.gOp(a, omat, op_le) - def != (a : GMat) = mat.gOp(a, omat, op_ne) - - def max (a : GMat) = mat.gOp(a, omat, op_max) - def min (a : GMat) = mat.gOp(a, omat, op_min) - - def dot (a :GMat) = mat.dot(a, omat) - def dotr (a :GMat) = mat.dotr(a, omat) - def ∙ (a :GMat) = mat.dot(a, omat) - def ∙→ (a :GMat) = mat.dotr(a, omat) - def on(a : GMat) = mat.vertcat(a, omat) - def \ (a : GMat) = mat.horzcat(a, omat) - */ - - def checkOne(a:Seq[Int], name:String):Int = { - if (a.length > 1) throw new RuntimeException("GMat %s only takes one argument" format name); - a(0); - } - - - override def * (a : FMat) = mat.GMult(GMat(a), omat); - override def * (a : SMat) = mat.GSMult(GSMat(a), omat); - override def *^ (a : FMat) = mat.GMultT(GMat(a), omat) - override def *^ (a : SMat) = mat.GSMultT(GSMat(a), omat) - override def xT (a : FMat) = mat.GMultT(GMat(a), omat) - override def xT (a : SMat) = mat.GSMultT(GSMat(a), omat) - override def ^* (a : FMat) = mat.GTMult(GMat(a), omat) - def *+^ (a : FMat) = mat.GMST(GMat(a), omat) - override def Tx (a : FMat) = mat.GTMult(GMat(a), omat) - def kron(a: FMat):FMat = mat.kron(GMat(a), omat) - override def ⊗ (b : FMat) = mat.kron(b, omat) - override def + (a : FMat) = mat.gOp(GMat(a), omat, op_add) - override def - (a : FMat) = mat.gOp(GMat(a), omat, op_sub) - override def *@ (a : FMat) = mat.gOp(GMat(a), omat, op_mul) - override def ∘ (a : FMat) = mat.gOp(GMat(a), omat, op_mul) - override def / (a : FMat) = mat.gOp(GMat(a), omat, op_div) - override def ^ (a : FMat) = mat.gOp(GMat(a), omat, op_pow) - override def > (a : FMat) = mat.gOp(GMat(a), omat, op_gt) - override def < (a : FMat) = mat.gOp(GMat(a), omat, op_lt) - override def == (a : FMat) = mat.gOp(GMat(a), omat, op_eq) - override def === (a : FMat) = mat.gOp(GMat(a), omat, op_eq) - override def >= (a : FMat) = mat.gOp(GMat(a), omat, op_ge) - override def <= (a : FMat) = mat.gOp(GMat(a), omat, op_le) - override def != (a : FMat) = mat.gOp(GMat(a), omat, op_ne) - - override def max (a : FMat) = mat.gOp(GMat(a), omat, op_max) - override def min (a : FMat) = mat.gOp(GMat(a), omat, op_min) - - override def dot (a :FMat) = mat.dot(GMat(a), omat) - override def dotr (a :FMat) = mat.dotr(GMat(a), omat) - override def ∙ (a :FMat) = mat.dot(GMat(a), omat) - override def ∙→ (a :FMat) = mat.dotr(GMat(a), omat) - def on(a : FMat) = mat.vertcat(GMat(a), omat) - def \ (a : FMat) = mat.horzcat(GMat(a), omat) - - override def * (b : Float) = mat.gOp(GMat(b), omat, op_mul) - override def *@ (b : Float) = mat.gOp(GMat(b), omat, op_mul) - override def ∘ (b : Float) = mat.gOp(GMat(b), omat, op_mul) - override def + (b : Float) = mat.gOp(GMat(b), omat, op_add) - override def - (b : Float) = mat.gOp(GMat(b), omat, op_sub) - override def / (b : Float) = mat.gOp(GMat(b), omat, op_div) - override def ^ (b : Float) = mat.gOp(GMat(b), omat, op_pow) - override def > (b : Float) = mat.gOp(GMat(b), omat, op_gt) - override def < (b : Float) = mat.gOp(GMat(b), omat, op_lt) - override def == (b : Float) = mat.gOp(GMat(b), omat, op_eq) - override def != (b : Float) = mat.gOp(GMat(b), omat, op_ne) - override def >= (b : Float) = mat.gOp(GMat(b), omat, op_ge) - override def <= (b : Float) = mat.gOp(GMat(b), omat, op_le) - override def max (b : Float) = mat.gOp(GMat(b), omat, op_max) - override def min (b : Float) = mat.gOp(GMat(b), omat, op_min) - - override def * (b : Double) = mat.gOp(GMat(b.toFloat), omat, op_mul) - override def *@ (b : Double) = mat.gOp(GMat(b.toFloat), omat, op_mul) - override def ∘ (b : Double) = mat.gOp(GMat(b.toFloat), omat, op_mul) - override def + (b : Double) = mat.gOp(GMat(b.toFloat), omat, op_add) - override def - (b : Double) = mat.gOp(GMat(b.toFloat), omat, op_sub) - override def / (b : Double) = mat.gOp(GMat(b.toFloat), omat, op_div) - override def ^ (b : Double) = mat.gOp(GMat(b.toFloat), omat, op_pow) - override def > (b : Double) = mat.gOp(GMat(b.toFloat), omat, op_gt) - override def < (b : Double) = mat.gOp(GMat(b.toFloat), omat, op_lt) - override def == (b : Double) = mat.gOp(GMat(b.toFloat), omat, op_eq) - override def != (b : Double) = mat.gOp(GMat(b.toFloat), omat, op_ne) - override def >= (b : Double) = mat.gOp(GMat(b.toFloat), omat, op_ge) - override def <= (b : Double) = mat.gOp(GMat(b.toFloat), omat, op_le) - override def max (b : Double) = mat.gOp(GMat(b), omat, op_max) - override def min (b : Double) = mat.gOp(GMat(b), omat, op_min) - - - override def * (b : Int) = mat.gOp(GMat(b.toFloat), omat, op_mul) - override def *@ (b : Int) = mat.gOp(GMat(b.toFloat), omat, op_mul) - override def ∘ (b : Int) = mat.gOp(GMat(b.toFloat), omat, op_mul) - override def + (b : Int) = mat.gOp(GMat(b.toFloat), omat, op_add) - override def - (b : Int) = mat.gOp(GMat(b.toFloat), omat, op_sub) - override def / (b : Int) = mat.gOp(GMat(b.toFloat), omat, op_div) - override def ^ (b : Int) = mat.gOp(GMat(b.toFloat), omat, op_pow) - override def > (b : Int) = mat.gOp(GMat(b.toFloat), omat, op_gt) - override def < (b : Int) = mat.gOp(GMat(b.toFloat), omat, op_lt) - override def == (b : Int) = mat.gOp(GMat(b.toFloat), omat, op_eq) - override def != (b : Int) = mat.gOp(GMat(b.toFloat), omat, op_ne) - override def >= (b : Int) = mat.gOp(GMat(b.toFloat), omat, op_ge) - override def <= (b : Int) = mat.gOp(GMat(b.toFloat), omat, op_le) - override def max (b : Int) = mat.gOp(GMat(b.toFloat), omat, op_max) - override def min (b : Int) = mat.gOp(GMat(b.toFloat), omat, op_min) - - - override def * (b : Long) = mat.gOp(GMat(b.toFloat), omat, op_mul) - override def *@ (b : Long) = mat.gOp(GMat(b.toFloat), omat, op_mul) - override def ∘ (b : Long) = mat.gOp(GMat(b.toFloat), omat, op_mul) - override def + (b : Long) = mat.gOp(GMat(b.toFloat), omat, op_add) - override def - (b : Long) = mat.gOp(GMat(b.toFloat), omat, op_sub) - override def / (b : Long) = mat.gOp(GMat(b.toFloat), omat, op_div) - override def ^ (b : Long) = mat.gOp(GMat(b.toFloat), omat, op_pow) - override def > (b : Long) = mat.gOp(GMat(b.toFloat), omat, op_gt) - override def < (b : Long) = mat.gOp(GMat(b.toFloat), omat, op_lt) - override def == (b : Long) = mat.gOp(GMat(b.toFloat), omat, op_eq) - override def != (b : Long) = mat.gOp(GMat(b.toFloat), omat, op_ne) - override def >= (b : Long) = mat.gOp(GMat(b.toFloat), omat, op_ge) - override def <= (b : Long) = mat.gOp(GMat(b.toFloat), omat, op_le) - override def max (b : Long) = mat.gOp(GMat(b.toFloat), omat, op_max) - override def min (b : Long) = mat.gOp(GMat(b.toFloat), omat, op_min) - - - def ^* (b : GDSPair) = MatFunctions.DDS(mat, b.left, b.right, omat) - def Tx (b : GDSPair) = MatFunctions.DDS(mat, b.left, b.right, omat) - /* - * Specialize to IMat - */ - override def * (b : IMat) = Mop_Times.op(mat, b, omat) - override def *^ (b : IMat) = Mop_TimesT.op(mat, b, omat) - override def xT (b : IMat) = Mop_TimesT.op(mat, b, omat) - override def Tx (b : IMat) = Mop_TTimes.op(mat, b, omat) - override def ^* (b : IMat) = Mop_TTimes.op(mat, b, omat) - override def + (b : IMat) = Mop_Plus.op(mat, b, omat) - override def - (b : IMat) = Mop_Minus.op(mat, b, omat) - override def *@ (b : IMat) = Mop_ETimes.op(mat, b, omat) - override def ∘ (b : IMat) = Mop_ETimes.op(mat, b, omat) - override def / (b : IMat) = Mop_EDiv.op(mat, b, omat) - override def ^ (b : IMat) = Mop_Pow.op(mat, b, omat) - override def ∙ (b : IMat) = Mop_Dot.op(mat, b, omat) - override def ∙→ (b : IMat) = Mop_Dotr.op(mat, b, omat) - override def dot (b : IMat) = Mop_Dot.op(mat, b, omat) - override def dotr(b : IMat) = Mop_Dotr.op(mat, b, omat) - override def \ (b : IMat) = Mop_HCat.op(mat, b, omat) - override def on (b : IMat) = Mop_VCat.op(mat, b, omat) - - override def > (b : IMat) = Mop_GT.op(mat, b, omat) - override def < (b : IMat) = Mop_LT.op(mat, b, omat) - override def == (b : IMat) = Mop_EQ.op(mat, b, omat) - override def === (b : IMat) = Mop_EQ.op(mat, b, omat) - override def >= (b : IMat) = Mop_GE.op(mat, b, omat) - override def <= (b : IMat) = Mop_LE.op(mat, b, omat) - override def != (b : IMat) = Mop_NE.op(mat, b, omat) - - /* - * Specialize to DMat - */ - override def * (b : DMat) = Mop_Times.op(mat, b, omat) - override def *^ (b : DMat) = Mop_TimesT.op(mat, b, omat) - override def xT (b : DMat) = Mop_TimesT.op(mat, b, omat) - override def Tx (b : DMat) = Mop_TTimes.op(mat, b, omat) - override def ^* (b : DMat) = Mop_TTimes.op(mat, b, omat) - override def + (b : DMat) = Mop_Plus.op(mat, b, omat) - override def - (b : DMat) = Mop_Minus.op(mat, b, omat) - override def *@ (b : DMat) = Mop_ETimes.op(mat, b, omat) - override def ∘ (b : DMat) = Mop_ETimes.op(mat, b, omat) - override def / (b : DMat) = Mop_EDiv.op(mat, b, omat) - override def ^ (b : DMat) = Mop_Pow.op(mat, b, omat) - override def ∙ (b : DMat) = Mop_Dot.op(mat, b, omat) - override def ∙→ (b : DMat) = Mop_Dotr.op(mat, b, omat) - override def dot (b : DMat) = Mop_Dot.op(mat, b, omat) - override def dotr(b : DMat) = Mop_Dotr.op(mat, b, omat) - override def \ (b : DMat) = Mop_HCat.op(mat, b, omat) - override def on (b : DMat) = Mop_VCat.op(mat, b, omat) - - override def > (b : DMat) = Mop_GT.op(mat, b, omat) - override def < (b : DMat) = Mop_LT.op(mat, b, omat) - override def == (b : DMat) = Mop_EQ.op(mat, b, omat) - override def === (b : DMat) = Mop_EQ.op(mat, b, omat) - override def >= (b : DMat) = Mop_GE.op(mat, b, omat) - override def <= (b : DMat) = Mop_LE.op(mat, b, omat) - override def != (b : DMat) = Mop_NE.op(mat, b, omat) - - /* - * Generics - */ - override def * (b : Mat):Mat = Mop_Times.op(mat, b, omat) - override def xT (b : Mat):Mat = Mop_TimesT.op(mat, b, omat) - override def *^ (b : Mat):Mat = Mop_TimesT.op(mat, b, omat) - override def Tx (b : Mat):Mat = Mop_TTimes.op(mat, b, omat) - override def ^* (b : Mat):Mat = Mop_TTimes.op(mat, b, omat) - override def + (b : Mat):Mat = Mop_Plus.op(mat, b, omat) - override def - (b : Mat):Mat = Mop_Minus.op(mat, b, omat) - override def *@ (b : Mat):Mat = Mop_ETimes.op(mat, b, omat) - override def ∘ (b : Mat):Mat = Mop_ETimes.op(mat, b, omat) - override def / (b : Mat):Mat = Mop_EDiv.op(mat, b, omat) - override def ^ (b : Mat):Mat = Mop_Pow.op(mat, b, omat) - override def /< (b : Mat):Mat = Mop_Div.op(mat, b, omat) - override def \\ (b : Mat):Mat = Mop_RSolve.op(mat, b, omat) - override def ◁ (b : Mat):Mat = Mop_Div.op(mat, b, omat) - override def ▷ (b : Mat):Mat = Mop_RSolve.op(mat, b, omat) - override def ∙ (b : Mat) = Mop_Dot.op(mat, b, omat) - override def ∙→ (b : Mat) = Mop_Dotr.op(mat, b, omat) - override def dot (b : Mat) = Mop_Dot.op(mat, b, omat) - override def dotr(b : Mat) = Mop_Dotr.op(mat, b, omat) - override def \ (b : Mat):Mat = Mop_HCat.op(mat, b, omat) - override def on (b : Mat):Mat = Mop_VCat.op(mat, b, omat) - - override def > (b : Mat):Mat = Mop_GT.op(mat, b, omat) - override def < (b : Mat):Mat = Mop_LT.op(mat, b, omat) - override def >= (b : Mat):Mat = Mop_GE.op(mat, b, omat) - override def <= (b : Mat):Mat = Mop_LE.op(mat, b, omat) - override def == (b : Mat):Mat = Mop_EQ.op(mat, b, omat) - override def === (b : Mat):Mat = Mop_EQ.op(mat, b, omat) - override def != (b : Mat):Mat = Mop_NE.op(mat, b, omat) -} - - object GMat { object BinOp { @@ -2303,372 +71,6 @@ object GMat { val atan2=0 val pow=1 } - - val nullPointer = new Pointer - - def zeros(nr:Int, nc:Int) = { - val out = GMat(nr, nc); - out.clear; - out; - } - - def zeros(dims:IMat) = { - val out = make(dims); - out.clear; - out - } - - def ones(nr:Int, nc:Int) = { - val out = GMat(nr, nc); - out.set(1f); - out - } - - def ones(dims:IMat) = { - val out = make(dims); - out.set(1f); - out - } - - val pONE = Pointer.to(Array(1f)); - - val pZERO = Pointer.to(Array(0f)); - - - def apply(nr:Int, nc:Int):GMat = { - val retv = new GMat(nr, nc, new Pointer(), 1L*nr*nc) - if (Mat.debugMem) { - println("GMat %d %d, %d %f" format (nr, nc, SciFunctions.getGPU, SciFunctions.GPUmem._1)) - if (nr*nc > Mat.debugMemThreshold) throw new RuntimeException("GMat alloc too large"); - } - var err = if (1L*nr*nc*Sizeof.FLOAT > Mat.hostAllocSize) { - cudaMallocHost(retv.pdata, 1L*nr*nc*Sizeof.FLOAT); - } else { - cudaMalloc(retv.pdata, 1L*nr*nc*Sizeof.FLOAT); - } - cudaStreamSynchronize(Mat.SyncMethod); - if (err == 0) err = cudaGetLastError(); - if (err != 0) throw new RuntimeException("CUDA alloc failed " + cudaGetErrorString(err)); - retv - } - - def make(dims:Array[Int]):GMat = { - var err = cudaGetLastError(); - if (err != 0) throw new RuntimeException("Weird previous error in GMat.make " + cudaGetErrorString(err)); - val len = dims.reduce(_*_); - val retv = new GMat(dims, new Pointer, len); - if (Mat.debugMem) { - println("GMat %d, %d %f" format (len, SciFunctions.getGPU, SciFunctions.GPUmem._1)) - if (len > Mat.debugMemThreshold) throw new RuntimeException("GMat alloc too large"); - } - err = if (1L*len*Sizeof.FLOAT > Mat.hostAllocSize) { - cudaMallocHost(retv.pdata, 1L*len*Sizeof.FLOAT); - } else { - cudaMalloc(retv.pdata, 1L*len*Sizeof.FLOAT); - } - cudaStreamSynchronize(Mat.SyncMethod); - if (err == 0) err = cudaGetLastError(); - if (err != 0) throw new RuntimeException("CUDA alloc failed " + cudaGetErrorString(err)); - retv - } - - def make(dims:IMat):GMat = make(dims.data); - - def apply(a:FMat):GMat = { - a match { - case g:GMat => g; - case _ => { - val rsize = a.nrows*a.ncols - val retv = GMat.newOrCheckGMat(a.dims, null, a.GUID, "GMat_FMat".##) - cudaMemcpy(retv.pdata, Pointer.to(a.data), 1L*rsize*Sizeof.FLOAT, cudaMemcpyKind.cudaMemcpyHostToDevice) - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError() - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("CUDA error in GMat() " + cudaGetErrorString(err)) - } - retv - } - } - } - - def apply(a:DMat):GMat = { - a match { - case g:GDMat => { - val rsize = a.length - val retv = GMat.newOrCheckGMat(a.dims, null, a.GUID, "GMat_FMat".##) - var err = CUMAT.doubleToFloat(g.pdata, retv.pdata, rsize) - if (err == 0) err = cudaGetLastError() - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("CUDA error in GMat() " + cudaGetErrorString(err)) - } - retv - } - case _ => GMat(FMat(a)) - } - } - - def apply(a:GIMat):GMat = { - - val rsize = a.nrows*a.ncols - val retv = GMat.newOrCheckGMat(a.dims, null, a.GUID, "GMat_GIMat".##) - var err = CUMAT.intToFloat(a.pdata, retv.pdata, a.length) - cudaStreamSynchronize(Mat.SyncMethod); - if (err == 0) err = cudaGetLastError() - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("GMat(GIMat) error " + cudaGetErrorString(err)) - } - retv - } - - def apply(a:Mat):GMat = a match { - case aa:GMat => aa - case aa:GIMat => GMat(aa) - case aa:FMat => GMat(aa) - case aa:DMat => GMat(aa) - case aa:IMat => GMat(FMat(aa)) - } - - def apply(a:Float):GMat = { - val out = GMat.newOrCheckGMat(1, 1, null, a.##, "GMat_Float".##) - out.set(a) - out - } - - def apply(a:Double):GMat = { - val out = GMat.newOrCheckGMat(1, 1, null, a.##, "GMat_Float".##) - out.set(a.toFloat) - out - } - - def elem(a:Float):GMat = { - val out = GMat.newOrCheckGMat(1, 1, null, a.##, "Gelem".##); - out.set(a) - out - } - - def elem(a:Double):GMat = { - val out = GMat.newOrCheckGMat(1, 1, null, a.##, "Gelem".##); - out.set(a.toFloat) - out - } - - def toFMat(a:GMat):FMat = a.toFMat(null) - - def fromFMat(a:FMat, b:GMat):GMat = { - val bb = GMat.newOrCheckGMat(a.dims, b, a.GUID, "GMat_fromFMat".##) - cudaMemcpy(bb.pdata, Pointer.to(a.data), a.length*1L*Sizeof.FLOAT, cudaMemcpyKind.cudaMemcpyHostToDevice) - cudaStreamSynchronize(Mat.SyncMethod); - var err = cudaGetLastError() - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("CUDA error in fromFMat " + cudaGetErrorString(err)) - } - bb - } - - - def GPUtoGPUarraycopy(a:Pointer, aoffset:Int, b:Pointer, boffset:Int, len:Int, msg:String ) = { - cudaMemcpy(b.withByteOffset(1L*boffset*Sizeof.FLOAT), a.withByteOffset(1L*aoffset*Sizeof.FLOAT), 1L*len*Sizeof.FLOAT, cudaMemcpyKind.cudaMemcpyDeviceToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError; - if (err != 0) throw new RuntimeException(msg +" error in memcpy "+ cudaGetErrorString(err)); - } - - def GPUtoCPUarraycopy(a:Pointer, aoffset:Int, b:Array[Float], boffset:Int, len:Int, msg:String ) = { - cudaMemcpy(Pointer.to(b).withByteOffset(1L*boffset*Sizeof.FLOAT), a.withByteOffset(1L*aoffset*Sizeof.FLOAT), 1L*len*Sizeof.FLOAT, cudaMemcpyKind.cudaMemcpyDeviceToHost); - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError; - if (err != 0) throw new RuntimeException(msg +" error in memcpy "+ cudaGetErrorString(err)); - } - - def CPUtoGPUarraycopy(a:Array[Float], aoffset:Int, b:Pointer, boffset:Int, len:Int, msg:String ) = { - cudaMemcpy(b.withByteOffset(1L*boffset*Sizeof.FLOAT), Pointer.to(a).withByteOffset(1L*aoffset*Sizeof.FLOAT), 1L*len*Sizeof.FLOAT, cudaMemcpyKind.cudaMemcpyHostToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - val err = cudaGetLastError; - if (err != 0) throw new RuntimeException(msg +" error in memcpy "+ cudaGetErrorString(err)); - } - - def newOrCheckGMat(nr:Int, nc:Int, outmat:Mat):GMat = { - if (outmat.asInstanceOf[AnyRef] == null || (outmat.nrows == 0 && outmat.ncols == 0)) { - GMat(nr, nc) - } else { - outmat match { - case omat:GMat => if (omat.nrows != nr || omat.ncols != nc) { - omat.recycle(nr, nc, 0) - } else { - omat - } - } - } - } - - def newOrCheckGMat(dims:Array[Int], out:Mat):GMat = { - if (out.asInstanceOf[AnyRef] != null && ND.compareDims(out.dims.data, dims)) { - out.asInstanceOf[GMat] - } else { - GMat.make(dims) - } - } - - def newOrCheckGMat(dims:IMat, out:Mat):GMat = newOrCheckGMat(dims.data, out); - - def newOrCheckGMat(nr:Int, nc:Int, outmat:Mat, matGuid:Long, opHash:Int):GMat = { - val m = if (outmat.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGMat(nr, nc, outmat) - } else { - val key = (matGuid, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache3(key) - if (res != null) { - newOrCheckGMat(nr, nc, res) - } else { - val omat = newOrCheckGMat(nr, nc, null) - Mat.cache3put(key, omat) - omat - } - } - if (m.myGPU != SciFunctions.getGPU) { - throw new RuntimeException("newOrCheckGMat1 problem with mat %d" format m.GUID) - } - m - } - - def newOrCheckGMat(dims:Array[Int], out:Mat, matGuid:Long, opHash:Int):GMat = { - if (out.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGMat(dims, out) - } else { - val key = (matGuid, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache3(key) - if (res != null) { - newOrCheckGMat(dims, res) - } else { - val omat = newOrCheckGMat(dims, null) - Mat.cache3put(key, omat) - omat - } - } - } - - def newOrCheckGMat(dims:IMat, out:Mat, g1:Long, opHash:Int):GMat = newOrCheckGMat(dims.data, out, g1, opHash); - - def newOrCheckGMat(nr:Int, nc:Int, outmat:Mat, guid1:Long, guid2:Long, opHash:Int):GMat = { - val m = if (outmat.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGMat(nr, nc, outmat) - } else { - val key = (guid1, guid2, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache4(key) - if (res != null) { - newOrCheckGMat(nr, nc, res) - } else { - val omat = newOrCheckGMat(nr, nc, null) - Mat.cache4put(key, omat) - omat - } - } - if (m.myGPU != SciFunctions.getGPU) { - throw new RuntimeException("newOrCheckGMat2 problem with mat %d" format m.GUID) - } - m - } - - def newOrCheckGMat(dims:Array[Int], out:Mat, guid1:Long, guid2:Long, opHash:Int):GMat = { - if (out.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGMat(dims, out) - } else { - val key = (guid1, guid2, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache4(key) - if (res != null) { - newOrCheckGMat(dims, res) - } else { - val omat = newOrCheckGMat(dims, null) - Mat.cache4put(key, omat) - omat - } - } - } - - def newOrCheckGMat(dims0:IMat, out:Mat, g1:Long, g2:Long, opHash:Int):GMat = { - newOrCheckGMat(dims0.data, out, g1, g2, opHash); - } - - def newOrCheckGMat(nr:Int, nc:Int, outmat:Mat, guid1:Long, guid2:Long, guid3:Long, opHash:Int):GMat = { - val m = if (outmat.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGMat(nr, nc, outmat) - } else { - val key = (guid1, guid2, guid3, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache5(key) - if (res != null) { - newOrCheckGMat(nr, nc, res) - } else { - val omat = newOrCheckGMat(nr, nc, null) - Mat.cache5put(key, omat) - omat - } - } - if (m.myGPU != SciFunctions.getGPU) { - throw new RuntimeException("newOrCheckGMat3 problem with mat %d" format m.GUID) - } - m - } - - def newOrCheckGMat(dims:Array[Int], out:Mat, g1:Long, g2:Long, g3:Long, opHash:Int):GMat = { - if (out.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGMat(dims, out) - } else { - val key = (g1, g2, g3, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache5(key) - if (res != null) { - newOrCheckGMat(dims, res) - } else { - val omat = newOrCheckGMat(dims, null) - Mat.cache5put(key, omat) - omat - } - } - } - - def newOrCheckGMat(dims:IMat, out:Mat, g1:Long, g2:Long, g3:Long, opHash:Int):GMat = newOrCheckGMat(dims.data, out, g1, g2, g3, opHash); - - def newOrCheckGMat(nr:Int, nc:Int, outmat:Mat, guid1:Long, guid2:Long, guid3:Long, guid4:Long, opHash:Int):GMat = { - val m = if (outmat.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGMat(nr, nc, outmat) - } else { - val key = (guid1, guid2, guid3, guid4, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache6(key) - if (res != null) { - newOrCheckGMat(nr, nc, res) - } else { - val omat = newOrCheckGMat(nr, nc, null) - Mat.cache6put(key, omat) - omat - } - } - if (m.myGPU != SciFunctions.getGPU) { - throw new RuntimeException("newOrCheckGMat3 problem with mat %d" format m.GUID) - } - m - } - - def newOrCheckGMat(dims:Array[Int], out:Mat, g1:Long, g2:Long, g3:Long, g4:Long, opHash:Int):GMat = { - if (out.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGMat(dims, out) - } else { - val key = (g1, g2, g3, g4, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache6(key) - if (res != null) { - newOrCheckGMat(dims, res) - } else { - val omat = newOrCheckGMat(dims, null) - Mat.cache6put(key, omat) - omat - } - } - } - - def newOrCheckGMat(dims:IMat, out:Mat, g1:Long, g2:Long, g3:Long, g4:Long, opHash:Int):GMat = newOrCheckGMat(dims.data, out, g1, g2, g3, g4, opHash); - } diff --git a/src/main/scala/BIDMat/GND.scalax b/src/main/scala/BIDMat/GND.scalax deleted file mode 100644 index 85d3e21b..00000000 --- a/src/main/scala/BIDMat/GND.scalax +++ /dev/null @@ -1,1128 +0,0 @@ -//-*-coding:utf-8-*- -// N-dimensional arrays with floating point contents -package BIDMat -import MatFunctions._ -import jcuda._ -import jcuda.runtime._ -import jcuda.runtime.JCuda._ -import jcuda.runtime.cudaMemcpyKind._ -import jcuda.jcublas._ -import jcuda.jcublas.JCublas._ -import edu.berkeley.bid.CUMAT -import GMat._ -import java.util.Arrays -import java.util.concurrent.atomic._ -import scala.concurrent.future -import scala.concurrent.ExecutionContext.Implicits.global -import scala.util.hashing.MurmurHash3 - - -case class GND(dims0:Array[Int], val data:Pointer) extends ND(dims0) { - - override def mytype = "GND" - - override def dv:Double = - if (nrows > 1 || ncols > 1) { - throw new RuntimeException("Matrix should be 1x1 to extract value") - } else { - toFND(null).data(0) - } - - override def fv:Float = - if (nrows > 1 || ncols > 1) { - throw new RuntimeException("Matrix should be 1x1 to extract value") - } else { - toFND(null).data(0) - } - - override def contents() = { - val out = new GMat(length, 1, data, length); - out.setGUID(MurmurHash3.mix(MurmurHash3.mix(length, 1), (GUID*7897889).toInt)); - out - } - - def applyf(indx:Int):Float = { - if (indx >= 0 && indx < length) { - val tmp = new Array[Float](1); - cudaMemcpy(Pointer.to(tmp), data.withByteOffset(1L*indx*Sizeof.FLOAT), Sizeof.FLOAT, cudaMemcpyKind.cudaMemcpyDeviceToHost); - var err = cudaGetLastError; - if (err != 0) throw new RuntimeException("GND apply() error " + cudaGetErrorString(err)); - tmp(0); - } else { - throw new RuntimeException("GND index out of range") - } - } - - - def apply(i:Int):Float = applyf(i); - - def apply(i1:Int, i2:Int):Float = apply(Array(i1, i2)) - def apply(i1:Int, i2:Int, i3:Int):Float = apply(Array(i1, i2, i3)) - def apply(i1:Int, i2:Int, i3:Int, i4:Int):Float = apply(Array(i1, i2, i3, i4)) - def apply(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int):Float = apply(Array(i1, i2, i3, i4, i5)) - def apply(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, i6:Int):Float = apply(Array(i1, i2, i3, i4, i5, i6)) - def apply(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, i6:Int, i7:Int):Float = apply(Array(i1, i2, i3, i4, i5, i6, i7)) - def apply(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, i6:Int, i7:Int, i8:Int):Float = apply(Array(i1, i2, i3, i4, i5, i6, i7, i8)) - - def apply(inds:List[Int]):Float = apply(inds.toArray) - - def apply(inds:Array[Int]):Float = { - val indx = ND.linearize(inds, dims); - val tmp = new Array[Float](1); - GPUtoCPUarraycopy(data, indx, tmp, 0, 1, "GND apply"); - tmp(0); - } - - def apply(inds:GIMat):GMat = { - inds match { - case aa:MatrixWildcard => { - val out = GMat.newOrCheckGMat(length, 1, null, GUID, inds.GUID, "apply(?)".##); - GPUtoGPUarraycopy(data, 0, out.data, 0, length, "GND apply(?)"); - out - } - case _ => { - val out = GMat.newOrCheckGMat(inds.nrows, inds.ncols, null, GUID, inds.GUID, "applyI".##); - val err = CUMAT.copyFromInds(data, out.data, inds.data, inds.llength); - if (err != 0) { - throw new RuntimeException("GND apply error " + cudaGetErrorString(err)) - } - out; - } - } - } - - def apply(inds0:List[GIMat]):GND = apply(inds0.toArray); - - def safePointer(ind:GIMat):Pointer = { - if (ind.asInstanceOf[AnyRef] == null) { - GMat.nullPointer; - } else { - ind.data; - } - } - - def apply(inds:Array[GIMat]):GND = { - val newdims = new Array[Int](_dims.length) - val newinds = new Array[GIMat](_dims.length) - var j = 0 - for (i <- 0 until _dims.length) { - inds(i) match { - case aa:MatrixWildcard => { - newdims(i) = _dims(i); - } - case _ => { - newdims(i) = inds(i).length; - newinds(i) = inds(i) - } - } - } - val out = GND.newOrCheckGND(newdims, null, GUID, ND.hashGUIDs(inds), "apply".##); - inds.length match { - case 2 => { - val omat = out.toGMatView(newdims(0), newdims(1)); - toGMatView(dims(0), dims(1)).applyx(inds(0), inds(1), omat); - } - case 3 => { - val err = CUMAT.copyFromInds3D(data, dims(0), dims(1), out.data, newdims(0), newdims(1), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1), safePointer(newinds(2)), newdims(2)); - if (err != 0) throw new RuntimeException("GND apply(I, J, K) error" + cudaGetErrorString(err)); - } - case 4 => { - val err = CUMAT.copyFromInds4D(data, dims(0), dims(1), dims(2), out.data, newdims(0), newdims(1), newdims(2), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1), safePointer(newinds(2)), newdims(2), safePointer(newinds(3)), newdims(3)); - if (err != 0) throw new RuntimeException("GND apply(I, J, K, L) error" + cudaGetErrorString(err)); - } - case _ => throw new RuntimeException("GND slice access with more than 4 indices not supported"); - } - out; - } - - def apply(i1:GIMat, i2:GIMat):GND = apply(Array(i1, i2)); - def apply(i1:GIMat, i2:GIMat, i3:GIMat):GND = apply(Array(i1, i2, i3)); - def apply(i1:GIMat, i2:GIMat, i3:GIMat, i4:GIMat):GND = apply(Array(i1, i2, i3, i4)); - - def apply (a1:IMat, a2:GIMat):GND = apply(GIMat(a1), a2); - def apply(a1:GIMat, a2:IMat):GND = apply(a1, GIMat(a2)); - def apply(a1:IMat, a2:IMat):GND = apply(GIMat(a1), GIMat(a2)); - - def apply(a1:IMat, a2:GIMat, a3:GIMat):GND = apply(GIMat(a1), a2, a3); - def apply(a1:GIMat, a2:IMat, a3:GIMat):GND = apply(a1, GIMat(a2), a3); - def apply(a1:IMat, a2:IMat, a3:GIMat):GND = apply(GIMat(a1), GIMat(a2), a3); - def apply(a1:GIMat, a2:GIMat, a3:IMat):GND = apply(a1, a2, GIMat(a3)); - def apply(a1:IMat, a2:GIMat, a3:IMat):GND = apply(GIMat(a1), a2, GIMat(a3)); - def apply(a1:GIMat, a2:IMat, a3:IMat):GND = apply(a1, GIMat(a2), GIMat(a3)); - def apply(a1:IMat, a2:IMat, a3:IMat):GND = apply(GIMat(a1), GIMat(a2), GIMat(a3)); - - def apply(a1:IMat, a2:GIMat, a3:GIMat, a4:GIMat):GND = apply(GIMat(a1), a2, a3, a4); - def apply(a1:GIMat, a2:IMat, a3:GIMat, a4:GIMat):GND = apply(a1, GIMat(a2), a3, a4); - def apply(a1:IMat, a2:IMat, a3:GIMat, a4:GIMat):GND = apply(GIMat(a1), GIMat(a2), a3, a4); - def apply(a1:GIMat, a2:GIMat, a3:IMat, a4:GIMat):GND = apply(a1, a2, GIMat(a3), a4); - def apply(a1:IMat, a2:GIMat, a3:IMat, a4:GIMat):GND = apply(GIMat(a1), a2, GIMat(a3), a4); - def apply(a1:GIMat, a2:IMat, a3:IMat, a4:GIMat):GND = apply(a1, GIMat(a2), GIMat(a3), a4); - def apply(a1:IMat, a2:IMat, a3:IMat, a4:GIMat):GND = apply(GIMat(a1), GIMat(a2), GIMat(a3), a4); - def apply(a1:GIMat, a2:GIMat, a3:GIMat, a4:IMat):GND = apply(a1, a2, a3, GIMat(a4)); - def apply(a1:IMat, a2:GIMat, a3:GIMat, a4:IMat):GND = apply(GIMat(a1), a2, a3, GIMat(a4)); - def apply(a1:GIMat, a2:IMat, a3:GIMat, a4:IMat):GND = apply(a1, GIMat(a2), a3, GIMat(a4)); - def apply(a1:IMat, a2:IMat, a3:GIMat, a4:IMat):GND = apply(GIMat(a1), GIMat(a2), a3, GIMat(a4)); - def apply(a1:GIMat, a2:GIMat, a3:IMat, a4:IMat):GND = apply(a1, a2, GIMat(a3), GIMat(a4)); - def apply(a1:IMat, a2:GIMat, a3:IMat, a4:IMat):GND = apply(GIMat(a1), a2, GIMat(a3), GIMat(a4)); - def apply(a1:GIMat, a2:IMat, a3:IMat, a4:IMat):GND = apply(a1, GIMat(a2), GIMat(a3), GIMat(a4)); - def apply(a1:IMat, a2:IMat, a3:IMat, a4:IMat):GND = apply(GIMat(a1), GIMat(a2), GIMat(a3), GIMat(a4)); - - override def apply(inds:Mat):Mat = { - inds match { - case ii:GIMat => apply(ii); - case ii:IMat => apply(GIMat(ii)); - } - } - - override def apply(i1:Mat, i2:Mat):ND = { - (i1, i2) match { - case (a1:GIMat, a2:GIMat) => apply(a1, a2); - case (a1:IMat, a2:GIMat) => apply(GIMat(a1), a2); - case (a1:GIMat, a2:IMat) => apply(a1, GIMat(a2)); - case (a1:IMat, a2:IMat) => apply(GIMat(a1), GIMat(a2)); - } - } - - override def apply(i1:Mat, i2:Mat, i3:Mat):ND = { - (i1, i2, i3) match { - case (a1:GIMat, a2:GIMat, a3:GIMat) => apply(a1, a2, a3); - case (a1:IMat, a2:GIMat, a3:GIMat) => apply(GIMat(a1), a2, a3); - case (a1:GIMat, a2:IMat, a3:GIMat) => apply(a1, GIMat(a2), a3); - case (a1:IMat, a2:IMat, a3:GIMat) => apply(GIMat(a1), GIMat(a2), a3); - case (a1:GIMat, a2:GIMat, a3:IMat) => apply(a1, a2, GIMat(a3)); - case (a1:IMat, a2:GIMat, a3:IMat) => apply(GIMat(a1), a2, GIMat(a3)); - case (a1:GIMat, a2:IMat, a3:IMat) => apply(a1, GIMat(a2), GIMat(a3)); - case (a1:IMat, a2:IMat, a3:IMat) => apply(GIMat(a1), GIMat(a2), GIMat(a3)); - } - } - - override def apply(i1:Mat, i2:Mat, i3:Mat, i4:Mat):ND = { - (i1, i2, i3, i4) match { - case (a1:GIMat, a2:GIMat, a3:GIMat, a4:GIMat) => apply(a1, a2, a3, a4); - case (a1:IMat, a2:GIMat, a3:GIMat, a4:GIMat) => apply(GIMat(a1), a2, a3, a4); - case (a1:GIMat, a2:IMat, a3:GIMat, a4:GIMat) => apply(a1, GIMat(a2), a3, a4); - case (a1:IMat, a2:IMat, a3:GIMat, a4:GIMat) => apply(GIMat(a1), GIMat(a2), a3, a4); - case (a1:GIMat, a2:GIMat, a3:IMat, a4:GIMat) => apply(a1, a2, GIMat(a3), a4); - case (a1:IMat, a2:GIMat, a3:IMat, a4:GIMat) => apply(GIMat(a1), a2, GIMat(a3), a4); - case (a1:GIMat, a2:IMat, a3:IMat, a4:GIMat) => apply(a1, GIMat(a2), GIMat(a3), a4); - case (a1:IMat, a2:IMat, a3:IMat, a4:GIMat) => apply(GIMat(a1), GIMat(a2), GIMat(a3), a4); - case (a1:GIMat, a2:GIMat, a3:GIMat, a4:IMat) => apply(a1, a2, a3, GIMat(a4)); - case (a1:IMat, a2:GIMat, a3:GIMat, a4:IMat) => apply(GIMat(a1), a2, a3, GIMat(a4)); - case (a1:GIMat, a2:IMat, a3:GIMat, a4:IMat) => apply(a1, GIMat(a2), a3, GIMat(a4)); - case (a1:IMat, a2:IMat, a3:GIMat, a4:IMat) => apply(GIMat(a1), GIMat(a2), a3, GIMat(a4)); - case (a1:GIMat, a2:GIMat, a3:IMat, a4:IMat) => apply(a1, a2, GIMat(a3), GIMat(a4)); - case (a1:IMat, a2:GIMat, a3:IMat, a4:IMat) => apply(GIMat(a1), a2, GIMat(a3), GIMat(a4)); - case (a1:GIMat, a2:IMat, a3:IMat, a4:IMat) => apply(a1, GIMat(a2), GIMat(a3), GIMat(a4)); - case (a1:IMat, a2:IMat, a3:IMat, a4:IMat) => apply(GIMat(a1), GIMat(a2), GIMat(a3), GIMat(a4)); - } - } - - - def reshape(newdims:Int*):GND = reshape(newdims.toArray) - - def reshape(newdims:Array[Int]):GND = { - if (newdims.reduce(_*_) == length) { - val out = GND.newOrCheckGND(newdims, null, GUID, ND.hashInts(newdims), "reshape".##) - GPUtoGPUarraycopy(data, 0, out.data, 0, length, "GND reshape"); - out - } else { - throw new RuntimeException("GND reshape total length doesnt match") - } - } - - def reshapeView(newdims:Int*):GND = reshapeView(newdims.toArray) - - def reshapeView(newdims:Array[Int]):GND = { - if (newdims.reduce(_*_) == length) { - val out = GND(newdims, data) - out - } else { - throw new RuntimeException("GND reshapeView total length doesnt match") - } - } - - def toGMat(nr:Int, nc:Int):GMat = toGMat(nr, nc, null); - - def toGMat(nr:Int, nc:Int, omat:Mat):GMat = { - if (nr*nc != length) { - throw new RuntimeException("GND and output GMat dims dont match") - } else { - val out = GMat.newOrCheckGMat(nr, nc, omat, GUID, nr, nc, "toGMat".##); - GPUtoGPUarraycopy(data, 0, out.data, 0, length, "GND toGMat"); - out - } - } - - def toFND(omat:FND):FND = { - val out = FND.newOrCheckFND(_dims, omat, GUID, "toFND".##); - GPUtoCPUarraycopy(data, 0, out.data, 0, length, "GND toFND"); - out - } - - def toGMatView(nr:Int, nc:Int):GMat = { - if (nr*nc != length) { - throw new RuntimeException("GND output GMat dims dont match") - } else { - val out = new GMat(nr, nc, data, nr * nc); - out.setGUID(edu.berkeley.bid.MurmurHash3.MurmurHash3_x64_64(Array(GUID, nr), "toGMatView".##)); - out; - } - } - - def update(indx:Int, v:Float) = { - if (indx < 0 || indx >= length) { - throw new RuntimeException("GND index out of range") - } else { - val tmp = Array[Float](v); - CPUtoGPUarraycopy(tmp, 0, data, indx, 1, "GND update"); - } - this - } - - def updatex(inds:GIMat, vv:GMat):GND = { - inds match { - case aa:MatrixWildcard => { - if (vv.length == length) { - GPUtoGPUarraycopy(vv.data, 0, data, 0, length, "GND udpate"); - this; - } else { - throw new RuntimeException("GND update(?) RHS dimension doesnt match") - } - } - case _ => { - if (inds.length != vv.length) { - throw new RuntimeException("GND updatex error: I and v have unequal lengths " + inds.length + " and " + vv.length + ", respectively.") - } - val err = CUMAT.copyToInds(data, vv.data, inds.data, inds.llength); - if (err != 0) { - throw new RuntimeException("GND update error " + cudaGetErrorString(err)) - } - this; - } - } - } - - def update(inds:GIMat, vv:GMat):GND = updatex(inds:GIMat, vv:GMat):GND; - - def update(inds:List[Int], v:Float):GND = update(inds.toArray, v) - - def update(inds:Array[Int], v:Float):GND = { - val indx = ND.linearize(inds, dims); - val tmp = Array[Float](v); - CPUtoGPUarraycopy(tmp, 0, data, indx, 1, "GND update"); - this - } - - def update(i1:Int, i2:Int, vv:Float):GND = update(Array(i1, i2), vv) - def update(i1:Int, i2:Int, i3:Int, vv:Float):GND = update(Array(i1, i2, i3), vv) - def update(i1:Int, i2:Int, i3:Int, i4:Int, vv:Float):GND = update(Array(i1, i2, i3, i4), vv) - - def update(inds:Array[GIMat], vv:GND):GND = { - if (inds.length != _dims.length) { - throw new RuntimeException("GND update wrong number of dims") - } - val newdims = new Array[Int](_dims.length) - val newinds = new Array[GIMat](_dims.length) - var j = 0 - for (i <- 0 until _dims.length) { - inds(i) match { - case aa:MatrixWildcard => { - newdims(i) = _dims(i); - } - case _ => { - newdims(i) = inds(i).length; - newinds(i) = inds(i) - } - } - } - ND.checkDims("GND update:", ND.trimDims(newdims), ND.trimDims(vv._dims)); - inds.length match { - case 2 => { - val omat = vv.toGMatView(newdims(0), newdims(1)); - toGMatView(dims(0), dims(1)).updatex(inds(0), inds(1), omat); - } - case 3 => { - val err = CUMAT.copyToInds3D(vv.data, vv.dims(0), vv.dims(1), data, dims(0), dims(1), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1), safePointer(newinds(2)), newdims(2)); - if (err != 0) throw new RuntimeException("GND apply(I, J, K) error" + cudaGetErrorString(err)); - } - case 4 => { - val err = CUMAT.copyToInds4D(vv.data, vv.dims(0), vv.dims(1), vv.dims(2), data, dims(0), dims(1), dims(2), - safePointer(newinds(0)), newdims(0), safePointer(newinds(1)), newdims(1), safePointer(newinds(2)), newdims(2), safePointer(newinds(3)), newdims(3)); - if (err != 0) throw new RuntimeException("GND apply(I, J, K, L) error" + cudaGetErrorString(err)); - } - case _ => throw new RuntimeException("GND slice access with more than 4 indices not supported"); - } - this - } - - def update(i1:GIMat, i2:GIMat, vv:GND):GND = update(Array(i1, i2), vv); - def update(i1:GIMat, i2:GIMat, i3:GIMat, vv:GND):GND = update(Array(i1, i2, i3), vv); - def update(i1:GIMat, i2:GIMat, i3:GIMat, i4:GIMat, vv:GND):GND = update(Array(i1, i2, i3, i4), vv); - - def update(i1:GIMat, i2:GIMat, vv:ND):GND = update(Array(i1, i2), vv.asInstanceOf[GND]); - def update(i1:GIMat, i2:GIMat, i3:GIMat, vv:ND):GND = update(Array(i1, i2, i3), vv.asInstanceOf[GND]); - def update(i1:GIMat, i2:GIMat, i3:GIMat, i4:GIMat, vv:ND):GND = update(Array(i1, i2, i3, i4), vv.asInstanceOf[GND]); - - def update(a1:IMat, a2:GIMat, uu:ND):GND = update(GIMat(a1), a2, uu); - def update(a1:GIMat, a2:IMat, uu:ND):GND = update(a1, GIMat(a2), uu); - def update(a1:IMat, a2:IMat, uu:ND):GND = update(GIMat(a1), GIMat(a2), uu); - - def update(a1:IMat, a2:GIMat, a3:GIMat, uu:ND):GND = update(GIMat(a1), a2, a3, uu); - def update(a1:GIMat, a2:IMat, a3:GIMat, uu:ND):GND = update(a1, GIMat(a2), a3, uu); - def update(a1:IMat, a2:IMat, a3:GIMat, uu:ND):GND = update(GIMat(a1), GIMat(a2), a3, uu); - def update(a1:GIMat, a2:GIMat, a3:IMat, uu:ND):GND = update(a1, a2, GIMat(a3), uu); - def update(a1:IMat, a2:GIMat, a3:IMat, uu:ND):GND = update(GIMat(a1), a2, GIMat(a3), uu); - def update(a1:GIMat, a2:IMat, a3:IMat, uu:ND):GND = update(a1, GIMat(a2), GIMat(a3), uu); - def update(a1:IMat, a2:IMat, a3:IMat, uu:ND):GND = update(GIMat(a1), GIMat(a2), GIMat(a3), uu); - - def update(a1:IMat, a2:GIMat, a3:GIMat, a4:GIMat, uu:ND):GND = update(GIMat(a1), a2, a3, a4, uu); - def update(a1:GIMat, a2:IMat, a3:GIMat, a4:GIMat, uu:ND):GND = update(a1, GIMat(a2), a3, a4, uu); - def update(a1:IMat, a2:IMat, a3:GIMat, a4:GIMat, uu:ND):GND = update(GIMat(a1), GIMat(a2), a3, a4, uu); - def update(a1:GIMat, a2:GIMat, a3:IMat, a4:GIMat, uu:ND):GND = update(a1, a2, GIMat(a3), a4, uu); - def update(a1:IMat, a2:GIMat, a3:IMat, a4:GIMat, uu:ND):GND = update(GIMat(a1), a2, GIMat(a3), a4, uu); - def update(a1:GIMat, a2:IMat, a3:IMat, a4:GIMat, uu:ND):GND = update(a1, GIMat(a2), GIMat(a3), a4, uu); - def update(a1:IMat, a2:IMat, a3:IMat, a4:GIMat, uu:ND):GND = update(GIMat(a1), GIMat(a2), GIMat(a3), a4, uu); - def update(a1:GIMat, a2:GIMat, a3:GIMat, a4:IMat, uu:ND):GND = update(a1, a2, a3, GIMat(a4), uu); - def update(a1:IMat, a2:GIMat, a3:GIMat, a4:IMat, uu:ND):GND = update(GIMat(a1), a2, a3, GIMat(a4), uu); - def update(a1:GIMat, a2:IMat, a3:GIMat, a4:IMat, uu:ND):GND = update(a1, GIMat(a2), a3, GIMat(a4), uu); - def update(a1:IMat, a2:IMat, a3:GIMat, a4:IMat, uu:ND):GND = update(GIMat(a1), GIMat(a2), a3, GIMat(a4), uu); - def update(a1:GIMat, a2:GIMat, a3:IMat, a4:IMat, uu:ND):GND = update(a1, a2, GIMat(a3), GIMat(a4), uu); - def update(a1:IMat, a2:GIMat, a3:IMat, a4:IMat, uu:ND):GND = update(GIMat(a1), a2, GIMat(a3), GIMat(a4), uu); - def update(a1:GIMat, a2:IMat, a3:IMat, a4:IMat, uu:ND):GND = update(a1, GIMat(a2), GIMat(a3), GIMat(a4), uu); - def update(a1:IMat, a2:IMat, a3:IMat, a4:IMat, uu:ND):GND = update(GIMat(a1), GIMat(a2), GIMat(a3), GIMat(a4), uu); - - def update(i1:Mat, vv:Mat):GND = { - (i1, vv) match { - case (a1:GIMat, uu:GMat) => update(a1, uu); - case (a1:GIMat, uu:FMat) => update(a1, GMat(uu)); - case (a1:IMat, uu:FMat) => update(GIMat(a1), GMat(uu)); - case (a1:IMat, uu:GMat) => update(GIMat(a1), uu); - } - } - - def update(i1:Mat, i2:Mat, vv:ND):GND = { - (i1, i2, vv) match { - case (a1:GIMat, a2:GIMat, uu:ND) => update(a1, a2, uu); - case (a1:IMat, a2:GIMat, uu:ND) => update(GIMat(a1), a2, uu); - case (a1:GIMat, a2:IMat, uu:ND) => update(a1, GIMat(a2), uu); - case (a1:IMat, a2:IMat, uu:ND) => update(GIMat(a1), GIMat(a2), uu); - } - } - - def update(i1:Mat, i2:Mat, i3:Mat, vv:ND):GND = { - (i1, i2, i3, vv) match { - case (a1:GIMat, a2:GIMat, a3:GIMat, uu:ND) => update(a1, a2, a3, uu); - case (a1:IMat, a2:GIMat, a3:GIMat, uu:ND) => update(GIMat(a1), a2, a3, uu); - case (a1:GIMat, a2:IMat, a3:GIMat, uu:ND) => update(a1, GIMat(a2), a3, uu); - case (a1:IMat, a2:IMat, a3:GIMat, uu:ND) => update(GIMat(a1), GIMat(a2), a3, uu); - case (a1:GIMat, a2:GIMat, a3:IMat, uu:ND) => update(a1, a2, GIMat(a3), uu); - case (a1:IMat, a2:GIMat, a3:IMat, uu:ND) => update(GIMat(a1), a2, GIMat(a3), uu); - case (a1:GIMat, a2:IMat, a3:IMat, uu:ND) => update(a1, GIMat(a2), GIMat(a3), uu); - case (a1:IMat, a2:IMat, a3:IMat, uu:ND) => update(GIMat(a1), GIMat(a2), GIMat(a3), uu); - } - } - - def update(i1:Mat, i2:Mat, i3:Mat, i4:Mat, vv:ND):GND = { - (i1, i2, i3, i4, vv) match { - case (a1:GIMat, a2:GIMat, a3:GIMat, a4:GIMat, uu:ND) => update(a1, a2, a3, a4, uu); - case (a1:IMat, a2:GIMat, a3:GIMat, a4:GIMat, uu:ND) => update(GIMat(a1), a2, a3, a4, uu); - case (a1:GIMat, a2:IMat, a3:GIMat, a4:GIMat, uu:ND) => update(a1, GIMat(a2), a3, a4, uu); - case (a1:IMat, a2:IMat, a3:GIMat, a4:GIMat, uu:ND) => update(GIMat(a1), GIMat(a2), a3, a4, uu); - case (a1:GIMat, a2:GIMat, a3:IMat, a4:GIMat, uu:ND) => update(a1, a2, GIMat(a3), a4, uu); - case (a1:IMat, a2:GIMat, a3:IMat, a4:GIMat, uu:ND) => update(GIMat(a1), a2, GIMat(a3), a4, uu); - case (a1:GIMat, a2:IMat, a3:IMat, a4:GIMat, uu:ND) => update(a1, GIMat(a2), GIMat(a3), a4, uu); - case (a1:IMat, a2:IMat, a3:IMat, a4:GIMat, uu:ND) => update(GIMat(a1), GIMat(a2), GIMat(a3), a4, uu); - case (a1:GIMat, a2:GIMat, a3:GIMat, a4:IMat, uu:ND) => update(a1, a2, a3, GIMat(a4), uu); - case (a1:IMat, a2:GIMat, a3:GIMat, a4:IMat, uu:ND) => update(GIMat(a1), a2, a3, GIMat(a4), uu); - case (a1:GIMat, a2:IMat, a3:GIMat, a4:IMat, uu:ND) => update(a1, GIMat(a2), a3, GIMat(a4), uu); - case (a1:IMat, a2:IMat, a3:GIMat, a4:IMat, uu:ND) => update(GIMat(a1), GIMat(a2), a3, GIMat(a4), uu); - case (a1:GIMat, a2:GIMat, a3:IMat, a4:IMat, uu:ND) => update(a1, a2, GIMat(a3), GIMat(a4), uu); - case (a1:IMat, a2:GIMat, a3:IMat, a4:IMat, uu:ND) => update(GIMat(a1), a2, GIMat(a3), GIMat(a4), uu); - case (a1:GIMat, a2:IMat, a3:IMat, a4:IMat, uu:ND) => update(a1, GIMat(a2), GIMat(a3), GIMat(a4), uu); - case (a1:IMat, a2:IMat, a3:IMat, a4:IMat, uu:ND) => update(GIMat(a1), GIMat(a2), GIMat(a3), GIMat(a4), uu); - } - } - - def update(inds:Array[GIMat], v:Float):GND = { - val newdims = new Array[Int](dims.length) - for (i <- 0 until dims.length) { - newdims(i) = inds(i) match {case aa:MatrixWildcard => _dims(i); case _ => inds(i).length} - } -// updateHelper(inds, v, 0, inds.length-1) - this - } - - def update(i1:GIMat, i2:GIMat, vv:Float):GND = update(Array(i1, i2), vv) - def update(i1:GIMat, i2:GIMat, i3:GIMat, vv:Float):GND = update(Array(i1, i2, i3), vv) - def update(i1:GIMat, i2:GIMat, i3:GIMat, i4:GIMat, vv:Float):GND = update(Array(i1, i2, i3, i4), vv) - - def update(i1:Mat, i2:Mat, vv:Float):ND = { - (i1, i2) match { - case (a1:GIMat, a2:GIMat) => update(a1, a2, vv); - } - } - - def update(i1:Mat, vv:Float):ND = { - (i1) match { - case (a1:GIMat) => update(a1, vv); - } - } - - def update(i1:Mat, i2:Mat, i3:Mat, vv:Float):ND = { - (i1, i2, i3) match { - case (a1:GIMat, a2:GIMat, a3:GIMat) => update(a1, a2, a3, vv); - } - } - - def update(i1:Mat, i2:Mat, i3:Mat, i4:Mat, vv:Float):ND = { - (i1, i2, i3, i4) match { - case (a1:GIMat, a2:GIMat, a3:GIMat, a4:GIMat) => update(a1, a2, a3, a4, vv); - } - } - - val asMat:GMat = { - val out = new GMat(nrows, ncols, data, nrows * ncols); - out.setGUID(edu.berkeley.bid.MurmurHash3.MurmurHash3_x64_64(Array(GUID), 0x45239234)); - out - } - - override def colslice(a:Int, b:Int, out:ND):GND = { - val dims0 = dims; - dims0(dims.length - 1) = b - a; - val outx = GND.newOrCheckGND(dims0, out, GUID, a, b, "colslice".##); - val omat = asMat.colslice(a, b, outx.asMat); - outx; - } - - override def colslice(a:Int, b:Int):GND = colslice(a, b, null) - - override def colslice(a:Int, b:Int, out:ND, c:Int):GND = { - asMat.colslice(a, b, out.asMat, c); - out.asInstanceOf[GND]; - } - - def copyTo(a:GND):GND = { - GMat.GPUtoGPUarraycopy(data, 0, a.data, 0, length, "GND copyTo"); - a - } - - def copyTo(a:FND):FND = { - GMat.GPUtoCPUarraycopy(data, 0, a.data, 0, length, "GND copyTo"); - a - } - - def copyTo(a:ND):ND = { - a match { - case aa:FND => copyTo(aa); - case aa:GND => copyTo(aa); - } - } - - override def zeros(nr:Int, nc:Int):GMat = GMat.zeros(nr, nc) - - override def zeros(dims0:IMat):GND = GND.zeros(dims0); - - override def zeros:GND = GND.zeros(dims); - - override def ones(dims0:IMat):GND = GND.ones(dims0); - - override def one() = { - GND.ones(iones(1,dims.length)) - } - - override def zero() = { - GND.zeros(iones(1,dims.length)) - } - - def clear:GND = { - cudaMemset(data, 0, Sizeof.FLOAT*length); - cudaDeviceSynchronize; - val err = cudaGetLastError - if (err != 0) throw new RuntimeException("GND clear error"); - this - } - - def set(v:Float):GND = { - asMat.set(v); - this - } - - def transpose(dims:Array[Int]):GND = transpose(irow(dims)) - - def transpose(perm:IMat):GND = { - val nd = _dims.length - if (perm.length != nd) { - throw new RuntimeException("GND transpose bad permutation ") - } - val xdims = irow(_dims); - val iperm = invperm(perm); - val pdims = xdims(perm).data; - var out = GND.newOrCheckGND(pdims, null, GUID, ND.hashInts(pdims), "transpose".##); - var out2 = GND.newOrCheckGND(pdims, null, GUID, ND.hashInts(pdims), "transpose1".##); - cudaMemcpy(out.data, data, 4L*length, cudaMemcpyDeviceToDevice); - for (i <- (nd - 1) until 0 by -1) { - if (iperm(i) != i) { - val (d1, d2, d3) = ND.getDims(i, iperm, xdims) - if (d1 > 1 && d2 > 1) { - // println("spermute %d %d %d" format (d1,d2,d3)) - CUMAT.spermute(d1, d2, d3, out.data, out2.data) - val tmp = out2 - out2 = out - out = tmp - } - ND.rotate(i, iperm, xdims) - } - } - out - } - - def transpose(i1:Int, i2:Int):GND = transpose(Array(i1, i2)) - def transpose(i1:Int, i2:Int, i3:Int):GND = transpose(Array(i1, i2, i3)) - def transpose(i1:Int, i2:Int, i3:Int, i4:Int):GND = transpose(Array(i1, i2, i3, i4)) - def transpose(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int):GND = transpose(Array(i1, i2, i3, i4, i5)) - def transpose(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, i6:Int):GND = transpose(Array(i1, i2, i3, i4, i5, i6)) - def transpose(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, i6:Int, i7:Int):GND = transpose(Array(i1, i2, i3, i4, i5, i6, i7)) - def transpose(i1:Int, i2:Int, i3:Int, i4:Int, i5:Int, i6:Int, i7:Int, i8:Int):GND = transpose(Array(i1, i2, i3, i4, i5, i6, i7, i8)) - - - - - def * (b : GND):GND = { - val dims0 = dims(0->(dims.length-1)); - val dims1 = b.dims(1->b.dims.length); - val x = toGMatView(SciFunctions.prod(dims0).v, dims(dims.length-1)); - val y = b.toGMatView(b.dims(0), SciFunctions.prod(dims1).v); - val zz = GND.newOrCheckGND((dims0 \ dims1).data, null, GUID, b.GUID, "*".##); - val z = zz.toGMatView(x.nrows, y.ncols); - z ~ x * y; - zz - } - - def unary_-():GND = { - val zz = GND.newOrCheckGND(dims.data, null, GUID, "-".##); - val a = toGMatView(length, 1); - val b = zz.toGMatView(length, 1); - val minusOne = GMat.newOrCheckGMat(1,1,null,-1,"minusOne".##); - minusOne.set(-1f); - a.gOp(minusOne, null, GMat.BinOp.op_mul); - zz; - } - - def + (mat:GND):GND = {val (a, b, c, d) = GND.asGMats(this, mat, null, "+"); c ~ a + b; d} - def - (mat:GND):GND = {val (a, b, c, d) = GND.asGMats(this, mat, null, "-"); c ~ a - b; d} - def *@ (mat:GND):GND = {val (a, b, c, d) = GND.asGMats(this, mat, null, "*@"); c ~ a *@ b; d} - def / (mat:GND):GND = {val (a, b, c, d) = GND.asGMats(this, mat, null, "/"); c ~ a / b; d} - def ^ (mat:GND):GND = {val (a, b, c, d) = GND.asGMats(this, mat, null, "^"); c ~ a ^ b; d} - - def > (mat:GND):GND = {val (a, b, c, d) = GND.asGMats(this, mat, null, ">"); c ~ a > b; d} - def < (mat:GND):GND = {val (a, b, c, d) = GND.asGMats(this, mat, null, "<"); c ~ a < b; d} - def >= (mat:GND):GND = {val (a, b, c, d) = GND.asGMats(this, mat, null, ">="); c ~ a >= b; d} - def <= (mat:GND):GND = {val (a, b, c, d) = GND.asGMats(this, mat, null, "<="); c ~ a <= b; d} - def != (mat:GND):GND = {val (a, b, c, d) = GND.asGMats(this, mat, null, "!="); c ~ a != b; d} - def == (mat:GND):GND = {val (a, b, c, d) = GND.asGMats(this, mat, null, "=="); c ~ a == b; d} - def === (mat:GND):GND = {val (a, b, c, d) = GND.asGMats(this, mat, null, "==="); c ~ a === b; d} - - def max (mat:GND):GND = {val (a, b, c, d) = GND.asGMats(this, mat, null, "max"); SciFunctions.max(a, b, c); d} - def max (mat:GND, omat:ND):GND = {val (a, b, c, d) = GND.asGMats(this, mat, omat, "max"); SciFunctions.max(a, b, c); d} - - def min (mat:GND):GND = {val (a, b, c, d) = GND.asGMats(this, mat, null, "min"); SciFunctions.min(a, b, c); d} - def min (mat:GND, omat:ND):GND = {val (a, b, c, d) = GND.asGMats(this, mat, omat, "min"); SciFunctions.min(a, b, c); d} - - - override def + (b:Float):GND = {val (a, c, d) = GND.asGMats(this, null, "+"); c ~ a + b; d} - override def - (b:Float):GND = {val (a, c, d) = GND.asGMats(this, null, "-"); c ~ a - b; d} - override def *@ (b:Float):GND = {val (a, c, d) = GND.asGMats(this, null, "*@"); c ~ a *@ b; d} - override def ∘ (b:Float):GND = {val (a, c, d) = GND.asGMats(this, null, "∘"); c ~ a *@ b; d} - override def * (b:Float):GND = {val (a, c, d) = GND.asGMats(this, null, "*"); c ~ a *@ b; d} - override def / (b:Float):GND = {val (a, c, d) = GND.asGMats(this, null, "/"); c ~ a / b; d} - override def ^ (b:Float):GND = {val (a, c, d) = GND.asGMats(this, null, "^"); c ~ a ^ b; d} - - override def > (b:Float):GND = {val (a, c, d) = GND.asGMats(this, null, ">"); c ~ a > b; d} - override def < (b:Float):GND = {val (a, c, d) = GND.asGMats(this, null, "<"); c ~ a < b; d} - override def >= (b:Float):GND = {val (a, c, d) = GND.asGMats(this, null, ">="); c ~ a >= b; d} - override def <= (b:Float):GND = {val (a, c, d) = GND.asGMats(this, null, "<="); c ~ a <= b; d} - override def != (b:Float):GND = {val (a, c, d) = GND.asGMats(this, null, "!="); c ~ a != b; d} - override def == (b:Float):GND = {val (a, c, d) = GND.asGMats(this, null, "=="); c ~ a == b; d} - override def === (b:Float):GND = {val (a, c, d) = GND.asGMats(this, null, "==="); c ~ a === b; d} - - override def max(b:Float):GND = {val (a, c, d) = GND.asGMats(this, null, "max"); SciFunctions.max(a, b, c); d} - override def min(b:Float):GND = {val (a, c, d) = GND.asGMats(this, null, "min"); SciFunctions.min(a, b, c); d} - def max(b:Float, omat:ND):GND = {val (a, c, d) = GND.asGMats(this, omat, "max"); SciFunctions.max(a, b, c); d} - def min(b:Float, omat:ND):GND = {val (a, c, d) = GND.asGMats(this, omat, "min"); SciFunctions.min(a, b, c); d} - - - - override def + (b:Double):GND = {val (a, c, d) = GND.asGMats(this, null, "+"); c ~ a + b; d} - override def - (b:Double):GND = {val (a, c, d) = GND.asGMats(this, null, "-"); c ~ a - b; d} - override def *@ (b:Double):GND = {val (a, c, d) = GND.asGMats(this, null, "*@"); c ~ a *@ b; d} - override def ∘ (b:Double):GND = {val (a, c, d) = GND.asGMats(this, null, "∘"); c ~ a *@ b; d} - override def / (b:Double):GND = {val (a, c, d) = GND.asGMats(this, null, "/"); c ~ a / b; d} - override def ^ (b:Double):GND = {val (a, c, d) = GND.asGMats(this, null, "^"); c ~ a ^ b; d} - override def * (b:Double):GND = {val (a, c, d) = GND.asGMats(this, null, "*"); c ~ a *@ b; d} - - override def > (b:Double):GND = {val (a, c, d) = GND.asGMats(this, null, ">"); c ~ a > b; d} - override def < (b:Double):GND = {val (a, c, d) = GND.asGMats(this, null, "<"); c ~ a < b; d} - override def >= (b:Double):GND = {val (a, c, d) = GND.asGMats(this, null, ">="); c ~ a >= b; d} - override def <= (b:Double):GND = {val (a, c, d) = GND.asGMats(this, null, "<="); c ~ a <= b; d} - override def != (b:Double):GND = {val (a, c, d) = GND.asGMats(this, null, "!="); c ~ a != b; d} - override def == (b:Double):GND = {val (a, c, d) = GND.asGMats(this, null, "=="); c ~ a == b; d} - override def === (b:Double):GND = {val (a, c, d) = GND.asGMats(this, null, "==="); c ~ a === b; d} - override def max(b:Double):GND = {val (a, c, d) = GND.asGMats(this, null, "max"); c ~ a max b; d} - override def min(b:Double):GND = {val (a, c, d) = GND.asGMats(this, null, "min"); c ~ a min b; d} - - - override def + (b:Int):GND = {val (a, c, d) = GND.asGMats(this, null, "+"); c ~ a + b; d} - override def - (b:Int):GND = {val (a, c, d) = GND.asGMats(this, null, "-"); c ~ a - b; d} - override def *@ (b:Int):GND = {val (a, c, d) = GND.asGMats(this, null, "*@"); c ~ a *@ b; d} - override def ∘ (b:Int):GND = {val (a, c, d) = GND.asGMats(this, null, "∘"); c ~ a *@ b; d} - override def / (b:Int):GND = {val (a, c, d) = GND.asGMats(this, null, "/"); c ~ a / b; d} - override def ^ (b:Int):GND = {val (a, c, d) = GND.asGMats(this, null, "^"); c ~ a ^ b; d} - override def * (b:Int):GND = {val (a, c, d) = GND.asGMats(this, null, "*"); c ~ a *@ b; d} - - override def > (b:Int):GND = {val (a, c, d) = GND.asGMats(this, null, ">"); c ~ a > b; d} - override def < (b:Int):GND = {val (a, c, d) = GND.asGMats(this, null, "<"); c ~ a < b; d} - override def >= (b:Int):GND = {val (a, c, d) = GND.asGMats(this, null, ">="); c ~ a >= b; d} - override def <= (b:Int):GND = {val (a, c, d) = GND.asGMats(this, null, "<="); c ~ a <= b; d} - override def != (b:Int):GND = {val (a, c, d) = GND.asGMats(this, null, "!="); c ~ a != b; d} - override def == (b:Int):GND = {val (a, c, d) = GND.asGMats(this, null, "=="); c ~ a == b; d} - override def === (b:Int):GND = {val (a, c, d) = GND.asGMats(this, null, "==="); c ~ a === b; d} - override def max(b:Int):GND = {val (a, c, d) = GND.asGMats(this, null, "max"); c ~ a max b; d} - override def min(b:Int):GND = {val (a, c, d) = GND.asGMats(this, null, "min"); c ~ a min b; d} - - override def + (b:Long):GND = {val (a, c, d) = GND.asGMats(this, null, "+"); c ~ a + b; d} - override def - (b:Long):GND = {val (a, c, d) = GND.asGMats(this, null, "-"); c ~ a - b; d} - override def *@ (b:Long):GND = {val (a, c, d) = GND.asGMats(this, null, "*@"); c ~ a *@ b; d} - override def ∘ (b:Long):GND = {val (a, c, d) = GND.asGMats(this, null, "∘"); c ~ a *@ b; d} - override def / (b:Long):GND = {val (a, c, d) = GND.asGMats(this, null, "/"); c ~ a / b; d} - override def ^ (b:Long):GND = {val (a, c, d) = GND.asGMats(this, null, "^"); c ~ a ^ b; d} - override def * (b:Long):GND = {val (a, c, d) = GND.asGMats(this, null, "*"); c ~ a *@ b; d} - - override def > (b:Long):GND = {val (a, c, d) = GND.asGMats(this, null, ">"); c ~ a > b; d} - override def < (b:Long):GND = {val (a, c, d) = GND.asGMats(this, null, "<"); c ~ a < b; d} - override def >= (b:Long):GND = {val (a, c, d) = GND.asGMats(this, null, ">="); c ~ a >= b; d} - override def <= (b:Long):GND = {val (a, c, d) = GND.asGMats(this, null, "<="); c ~ a <= b; d} - override def != (b:Long):GND = {val (a, c, d) = GND.asGMats(this, null, "!="); c ~ a != b; d} - override def == (b:Long):GND = {val (a, c, d) = GND.asGMats(this, null, "=="); c ~ a == b; d} - override def === (b:Long):GND = {val (a, c, d) = GND.asGMats(this, null, "==="); c ~ a === b; d} - override def max(b:Long):GND = {val (a, c, d) = GND.asGMats(this, null, "max"); c ~ a max b; d} - override def min(b:Long):GND = {val (a, c, d) = GND.asGMats(this, null, "min"); c ~ a min b; d} - - override def + (b : ND):ND = this + b.asInstanceOf[GND]; - override def - (b : ND):ND = this - b.asInstanceOf[GND]; - override def * (b : ND):ND = this * b.asInstanceOf[GND]; - override def *@ (b : ND):ND = this *@ b.asInstanceOf[GND]; - override def ∘ (b : ND):ND = this *@ b.asInstanceOf[GND]; - override def / (b : ND):ND = this / b.asInstanceOf[GND]; - override def ^ (b : ND):ND = this ^ b.asInstanceOf[GND]; - - override def > (b : ND):ND = this > b.asInstanceOf[GND]; - override def < (b : ND):ND = this < b.asInstanceOf[GND]; - override def >= (b : ND):ND = this >= b.asInstanceOf[GND]; - override def <= (b : ND):ND = this <= b.asInstanceOf[GND]; - override def == (b : ND):ND = this == b.asInstanceOf[GND]; - override def === (b : ND):ND = this === b.asInstanceOf[GND]; - override def != (b : ND):ND = this != b.asInstanceOf[GND]; - override def max (b : ND):ND = this max b.asInstanceOf[GND]; - override def min (b : ND):ND = this min b.asInstanceOf[GND]; - - override def \ (b : ND):ND = this \ b.asInstanceOf[GND]; - override def on (b : ND):ND = this on b.asInstanceOf[GND]; - - def reduce(inds:Array[Int], fctn:(GMat)=>GMat, opname:String):GND = { - val alldims = izeros(_dims.length,1); - val xinds = new IMat(inds.length, 1, inds); - val xdims = new IMat(_dims.length, 1, _dims); - alldims(xinds) = 1; - if (SciFunctions.sum(alldims).v != inds.length) { - throw new RuntimeException(opname+ " indices arent a legal subset of dims"); - } - val restdims = find(alldims == 0); - val tmp = transpose((xinds on restdims).data); - val dimprodx = SciFunctions.prod(xdims(xinds)).v; - val dimprody = SciFunctions.prod(xdims(restdims)).v; - val tmpF = new GMat(dimprodx, dimprody, tmp.data, dimprodx * dimprody); - val tmpSum:GMat = fctn(tmpF) - val out1 = new GND((iones(inds.length,1) on xdims(restdims)).data, tmpSum.data) - out1.transpose(invperm(xinds on restdims).data) - } - - def sum(inds:Array[Int]):GND = reduce(inds, SciFunctions.sum, "sum") - def prod(inds:Array[Int]):GND = reduce(inds, SciFunctions.prod, "prod") - def mean(inds:Array[Int]):GND = reduce(inds, SciFunctions.mean, "mean") - def variance(inds:Array[Int]):GND = reduce(inds, SciFunctions.variance, "variance") - def maxi(inds:Array[Int]):GND = reduce(inds, SciFunctions.maxi, "maxi") - def mini(inds:Array[Int]):GND = reduce(inds, SciFunctions.mini, "mini") - - override def sum(inds:Int*):GND = sum(inds.toArray) - override def prod(inds:Int*):GND = prod(inds.toArray) - override def mean(inds:Int*):GND = mean(inds.toArray) - override def variance(inds:Int*):GND = variance(inds.toArray) - override def maxi(inds:Int*):GND = maxi(inds.toArray) - override def mini(inds:Int*):GND = mini(inds.toArray) - - def ~ (b : GND):GNDPair = new GNDPair(this, b) - def ~ (b : ND):GNDPair = new GNDPair(this, b.asInstanceOf[GND]) - -} - -class GNDPair(val omat:ND, val amat:GND) extends NDPair { - - def * (b : GND):GND = { - val dims0 = amat.dims(0->(amat.dims.length-1)); - val dims1 = b.dims(1->b.dims.length); - val x = amat.toGMatView(SciFunctions.prod(dims0).v, amat.dims(amat.dims.length-1)); - val y = b.toGMatView(b.dims(0), SciFunctions.prod(dims1).v); - val odims = dims0 \ dims1; - ND.checkDims("GND *", odims, omat.dims); - val zz = GND.newOrCheckGND(odims.data, omat, amat.GUID, b.GUID, "*".##); - val z = zz.toGMatView(x.nrows, y.ncols); - z ~ x * y; - zz - } - - def *^ (b : GND):GND = { - omat match { - case gg:GFilter => {gg} - } - } - - override def *^ (b : ND):ND = { - (omat, b) match { - case (ff:GFilter, bb:GND) => {ff} - } - } - - def + (bmat:GND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat, omat, "+"); c ~ a + b; d} - def - (bmat:GND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat, omat, "-"); c ~ a - b; d} - def *@ (bmat:GND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat, omat, "*@"); c ~ a *@ b; d} - def ∘ (bmat:GND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat, omat, "∘"); c ~ a *@ b; d} - def / (bmat:GND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat, omat, "/"); c ~ a / b; d} - def ^ (bmat:GND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat, omat, "^"); c ~ a ^ b; d} - - def > (bmat:GND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat, omat, ">"); c ~ a > b; d} - def < (bmat:GND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat, omat, "<"); c ~ a < b; d} - def >= (bmat:GND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat, omat, ">="); c ~ a >= b; d} - def <= (bmat:GND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat, omat, "<="); c ~ a <= b; d} - def != (bmat:GND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat, omat, "!="); c ~ a != b; d} - def == (bmat:GND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat, omat, "=="); c ~ a == b; d} - def === (bmat:GND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat, omat, "==="); c ~ a === b; d} - - def max (bmat:GND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat, omat, "max"); c ~ a max b; d} - def min (bmat:GND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat, omat, "min"); c ~ a min b; d} - - - override def + (b:Float):GND = {val (a, c, d) = GND.asGMats(amat, omat, "+"); c ~ a + GMat(b); d} - override def - (b:Float):GND = {val (a, c, d) = GND.asGMats(amat, omat, "-"); c ~ a - GMat(b); d} - override def *@ (b:Float):GND = {val (a, c, d) = GND.asGMats(amat, omat, "*@"); c ~ a *@ GMat(b); d} - override def ∘ (b:Float):GND = {val (a, c, d) = GND.asGMats(amat, omat, "∘"); c ~ a *@ GMat(b); d} - override def * (b:Float):GND = {val (a, c, d) = GND.asGMats(amat, omat, "*"); c ~ a * GMat(b); d} - override def / (b:Float):GND = {val (a, c, d) = GND.asGMats(amat, omat, "/"); c ~ a / GMat(b); d} - override def ^ (b:Float):GND = {val (a, c, d) = GND.asGMats(amat, omat, "^"); c ~ a ^ GMat(b); d} - - override def > (b:Float):GND = {val (a, c, d) = GND.asGMats(amat, omat, ">"); c ~ a > GMat(b); d} - override def < (b:Float):GND = {val (a, c, d) = GND.asGMats(amat, omat, "<"); c ~ a < GMat(b); d} - override def >= (b:Float):GND = {val (a, c, d) = GND.asGMats(amat, omat, ">="); c ~ a >= GMat(b); d} - override def <= (b:Float):GND = {val (a, c, d) = GND.asGMats(amat, omat, "<="); c ~ a <= GMat(b); d} - override def != (b:Float):GND = {val (a, c, d) = GND.asGMats(amat, omat, "!="); c ~ a != GMat(b); d} - override def == (b:Float):GND = {val (a, c, d) = GND.asGMats(amat, omat, "=="); c ~ a == GMat(b); d} - override def === (b:Float):GND = {val (a, c, d) = GND.asGMats(amat, omat, "==="); c ~ a === GMat(b); d} - - override def max (b:Float):GND = {val (a, c, d) = GND.asGMats(amat, omat, "max"); c ~ a max GMat(b); d} - override def min (b:Float):GND = {val (a, c, d) = GND.asGMats(amat, omat, "min"); c ~ a min GMat(b); d} - - - override def + (b:Double):GND = {val (a, c, d) = GND.asGMats(amat, omat, "+"); c ~ a + GMat(b.toFloat); d} - override def - (b:Double):GND = {val (a, c, d) = GND.asGMats(amat, omat, "-"); c ~ a - GMat(b.toFloat); d} - override def *@ (b:Double):GND = {val (a, c, d) = GND.asGMats(amat, omat, "*@"); c ~ a *@ GMat(b.toFloat); d} - override def ∘ (b:Double):GND = {val (a, c, d) = GND.asGMats(amat, omat, "∘"); c ~ a *@ GMat(b.toFloat); d} - override def * (b:Double):GND = {val (a, c, d) = GND.asGMats(amat, omat, "*"); c ~ a * GMat(b.toFloat); d} - override def / (b:Double):GND = {val (a, c, d) = GND.asGMats(amat, omat, "/"); c ~ a / GMat(b.toFloat); d} - override def ^ (b:Double):GND = {val (a, c, d) = GND.asGMats(amat, omat, "^"); c ~ a ^ GMat(b.toFloat); d} - - override def > (b:Double):GND = {val (a, c, d) = GND.asGMats(amat, omat, ">"); c ~ a > GMat(b.toFloat); d} - override def < (b:Double):GND = {val (a, c, d) = GND.asGMats(amat, omat, "<"); c ~ a < GMat(b.toFloat); d} - override def >= (b:Double):GND = {val (a, c, d) = GND.asGMats(amat, omat, ">="); c ~ a >= GMat(b.toFloat); d} - override def <= (b:Double):GND = {val (a, c, d) = GND.asGMats(amat, omat, "<="); c ~ a <= GMat(b.toFloat); d} - override def != (b:Double):GND = {val (a, c, d) = GND.asGMats(amat, omat, "!="); c ~ a != GMat(b.toFloat); d} - override def == (b:Double):GND = {val (a, c, d) = GND.asGMats(amat, omat, "=="); c ~ a == GMat(b.toFloat); d} - override def === (b:Double):GND = {val (a, c, d) = GND.asGMats(amat, omat, "==="); c ~ a === GMat(b.toFloat); d} - - override def max (b:Double):GND = {val (a, c, d) = GND.asGMats(amat, omat, "max"); c ~ a max GMat(b); d} - override def min (b:Double):GND = {val (a, c, d) = GND.asGMats(amat, omat, "min"); c ~ a min GMat(b); d} - - - override def + (b:Int):GND = {val (a, c, d) = GND.asGMats(amat, omat, "+"); c ~ a + GMat(b.toFloat); d} - override def - (b:Int):GND = {val (a, c, d) = GND.asGMats(amat, omat, "-"); c ~ a - GMat(b.toFloat); d} - override def *@ (b:Int):GND = {val (a, c, d) = GND.asGMats(amat, omat, "*@"); c ~ a *@ GMat(b.toFloat); d} - override def ∘ (b:Int):GND = {val (a, c, d) = GND.asGMats(amat, omat, "∘"); c ~ a *@ GMat(b.toFloat); d} - override def * (b:Int):GND = {val (a, c, d) = GND.asGMats(amat, omat, "*"); c ~ a * GMat(b.toFloat); d} - override def / (b:Int):GND = {val (a, c, d) = GND.asGMats(amat, omat, "/"); c ~ a / GMat(b.toFloat); d} - override def ^ (b:Int):GND = {val (a, c, d) = GND.asGMats(amat, omat, "^"); c ~ a ^ GMat(b.toFloat); d} - - override def > (b:Int):GND = {val (a, c, d) = GND.asGMats(amat, omat, ">"); c ~ a > GMat(b.toFloat); d} - override def < (b:Int):GND = {val (a, c, d) = GND.asGMats(amat, omat, "<"); c ~ a < GMat(b.toFloat); d} - override def >= (b:Int):GND = {val (a, c, d) = GND.asGMats(amat, omat, ">="); c ~ a >= GMat(b.toFloat); d} - override def <= (b:Int):GND = {val (a, c, d) = GND.asGMats(amat, omat, "<="); c ~ a <= GMat(b.toFloat); d} - override def != (b:Int):GND = {val (a, c, d) = GND.asGMats(amat, omat, "!="); c ~ a != GMat(b.toFloat); d} - override def == (b:Int):GND = {val (a, c, d) = GND.asGMats(amat, omat, "=="); c ~ a == GMat(b.toFloat); d} - override def === (b:Int):GND = {val (a, c, d) = GND.asGMats(amat, omat, "==="); c ~ a === GMat(b.toFloat); d} - - override def max (b:Int):GND = {val (a, c, d) = GND.asGMats(amat, omat, "max"); c ~ a max GMat(b.toFloat); d} - override def min (b:Int):GND = {val (a, c, d) = GND.asGMats(amat, omat, "min"); c ~ a min GMat(b.toFloat); d} - - - override def + (b:Long):GND = {val (a, c, d) = GND.asGMats(amat, omat, "+"); c ~ a + GMat(b.toFloat); d} - override def - (b:Long):GND = {val (a, c, d) = GND.asGMats(amat, omat, "-"); c ~ a - GMat(b.toFloat); d} - override def *@ (b:Long):GND = {val (a, c, d) = GND.asGMats(amat, omat, "*@"); c ~ a *@ GMat(b.toFloat); d} - override def ∘ (b:Long):GND = {val (a, c, d) = GND.asGMats(amat, omat, "∘"); c ~ a *@ GMat(b.toFloat); d} - override def * (b:Long):GND = {val (a, c, d) = GND.asGMats(amat, omat, "*"); c ~ a * GMat(b.toFloat); d} - override def / (b:Long):GND = {val (a, c, d) = GND.asGMats(amat, omat, "/"); c ~ a / GMat(b.toFloat); d} - override def ^ (b:Long):GND = {val (a, c, d) = GND.asGMats(amat, omat, "^"); c ~ a ^ GMat(b.toFloat); d} - - override def > (b:Long):GND = {val (a, c, d) = GND.asGMats(amat, omat, ">"); c ~ a > GMat(b.toFloat); d} - override def < (b:Long):GND = {val (a, c, d) = GND.asGMats(amat, omat, "<"); c ~ a < GMat(b.toFloat); d} - override def >= (b:Long):GND = {val (a, c, d) = GND.asGMats(amat, omat, ">="); c ~ a >= GMat(b.toFloat); d} - override def <= (b:Long):GND = {val (a, c, d) = GND.asGMats(amat, omat, "<="); c ~ a <= GMat(b.toFloat); d} - override def != (b:Long):GND = {val (a, c, d) = GND.asGMats(amat, omat, "!="); c ~ a != GMat(b.toFloat); d} - override def == (b:Long):GND = {val (a, c, d) = GND.asGMats(amat, omat, "=="); c ~ a == GMat(b.toFloat); d} - override def === (b:Long):GND = {val (a, c, d) = GND.asGMats(amat, omat, "==="); c ~ a === GMat(b.toFloat); d} - - override def max (b:Long):GND = {val (a, c, d) = GND.asGMats(amat, omat, "max"); c ~ a max GMat(b.toFloat); d} - override def min (b:Long):GND = {val (a, c, d) = GND.asGMats(amat, omat, "min"); c ~ a min GMat(b.toFloat); d} - - - override def * (bmat:ND):GND = this * bmat.asInstanceOf[GND]; - override def + (bmat:ND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat.asInstanceOf[GND], omat, "+"); c ~ a + b; d} - override def - (bmat:ND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat.asInstanceOf[GND], omat, "-"); c ~ a - b; d} - override def *@ (bmat:ND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat.asInstanceOf[GND], omat, "*@"); c ~ a *@ b; d} - override def ∘ (bmat:ND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat.asInstanceOf[GND], omat, "∘"); c ~ a *@ b; d} - override def / (bmat:ND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat.asInstanceOf[GND], omat, "/"); c ~ a / b; d} - override def ^ (bmat:ND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat.asInstanceOf[GND], omat, "^"); c ~ a ^ b; d} - - override def > (bmat:ND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat.asInstanceOf[GND], omat, ">"); c ~ a > b; d} - override def < (bmat:ND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat.asInstanceOf[GND], omat, "<"); c ~ a < b; d} - override def >= (bmat:ND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat.asInstanceOf[GND], omat, ">="); c ~ a >= b; d} - override def <= (bmat:ND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat.asInstanceOf[GND], omat, "<="); c ~ a <= b; d} - override def != (bmat:ND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat.asInstanceOf[GND], omat, "!="); c ~ a != b; d} - override def == (bmat:ND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat.asInstanceOf[GND], omat, "=="); c ~ a == b; d} - override def === (bmat:ND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat.asInstanceOf[GND], omat, "==="); c ~ a === b; d} - - override def max (bmat:ND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat.asInstanceOf[GND], omat, "max"); c ~ a max b; d} - override def min (bmat:ND):GND = {val (a, b, c, d) = GND.asGMats(amat, bmat.asInstanceOf[GND], omat, "min"); c ~ a min b; d} - -} - -object GND { - - def scalar(v:Float, nd:Int):GND = { - val newdims = new Array[Int](nd); - Arrays.fill(newdims,1); - val out = GND(newdims); - val tmp = Array[Float](v); - CPUtoGPUarraycopy(tmp, 0, out.data, 0, 1, "GND scalar"); - out - } - - def apply(dims:Array[Int]):GND = { - val retv = new GND(dims, new Pointer); - val len = dims.reduce(_*_); - if (Mat.debugMem) { - println("GND %d, %d %f" format (len, SciFunctions.getGPU, SciFunctions.GPUmem._1)) - if (len > Mat.debugMemThreshold) throw new RuntimeException("GND alloc too large"); - } - var err = if (1L*len*Sizeof.FLOAT > Mat.hostAllocSize) { - cudaMallocHost(retv.data, 1L*len*Sizeof.FLOAT); - } else { - cudaMalloc(retv.data, 1L*len*Sizeof.FLOAT); - } - cudaDeviceSynchronize; - if (err == 0) err = cudaGetLastError(); - if (err != 0) throw new RuntimeException("CUDA alloc failed " + cudaGetErrorString(err)); - retv - } - - def apply(dims0:IMat):GND = apply(dims0.data); - - def apply(dims:Int*):GND = apply(dims.toArray); - - def apply(f:GMat):GND = { - val out = GND.newOrCheckGND(Array(f.nrows, f.ncols), null, f.GUID, "apply".##); - GPUtoGPUarraycopy(f.data, 0, out.data, 0, f.length, "GND apply"); - out - } - - def apply(f:FND):GND = { - val out = GND.newOrCheckGND(f.dims.data, null, f.GUID, "apply".##); - CPUtoGPUarraycopy(f.data, 0, out.data, 0, f.length, "GND apply"); - out - } - - def apply(f:FMat):GND = { - val out = GND.newOrCheckGND(Array(f.nrows, f.ncols), null, f.GUID, "apply".##); - CPUtoGPUarraycopy(f.data, 0, out.data, 0, f.length, "GND apply"); - out - } - - def zeros(dims0:IMat) = { - val out = GND(dims0) - cudaMemset(out.data, 0, Sizeof.FLOAT*out.length) - cudaDeviceSynchronize() - val err = cudaGetLastError() - if (err != 0) throw new RuntimeException("GPU "+SciFunctions.getGPU+": Cuda error in GND.zeros " + cudaGetErrorString(err)); - out - } - - def ones(dims0:IMat) = { - val out = GND(dims0) - CUMAT.setval(out.data, 1f, out.length) - cudaDeviceSynchronize() - val err = cudaGetLastError() - if (err != 0) throw new RuntimeException("GPU "+SciFunctions.getGPU+": Cuda error in GND.ones " + cudaGetErrorString(err)); - out - } - - def applyGNDfun(in:GND, omat:ND, opn:Int, kflops:Long):GND = { - val out = GND.newOrCheckGND(in.dims, omat, in.GUID, opn) - CUMAT.applygfun(in.data, out.data, in.length, opn) - jcuda.runtime.JCuda.cudaDeviceSynchronize() - Mat.nflops += kflops*in.length - out - } - - def applyGNDfun(in:GND, opn:Int, kflops:Long):GND = { - val out = GND.newOrCheckGND(in.dims, null, in.GUID, opn) - CUMAT.applygfun(in.data, out.data, in.length, opn) - jcuda.runtime.JCuda.cudaDeviceSynchronize() - Mat.nflops += kflops*in.length - out - } - - def applyGNDfun2(a:GND, b:GND, omat:ND, opn:Int, kflops:Long):GND = { - ND.checkDims("applyGNDfun2", a.dims, b.dims); - val out = GND.newOrCheckGND(a.dims, omat, a.GUID, b.GUID, opn); - CUMAT.applygfun2(a.data, b.data, out.data, a.nrows*a.ncols, opn); - jcuda.runtime.JCuda.cudaDeviceSynchronize(); - Mat.nflops += kflops*a.length; - out; - } - - def applyGNDfun2(a:GND, b:GND, opn:Int, kflops:Long):GND = { - ND.checkDims("applyGNDfun2", a.dims, b.dims); - val out = GND.newOrCheckGND(a.dims, null, a.GUID, b.GUID, opn); - CUMAT.applygfun2(a.data, b.data, out.data, a.nrows*a.ncols, opn); - jcuda.runtime.JCuda.cudaDeviceSynchronize(); - Mat.nflops += kflops*a.length; - out; - } - - def rand(out:GND):GND = { - GMat.rand(out.asMat); - out; - } - - def normrnd(mu:Float, sig:Float, out:GND):GND = { - GMat.normrnd(mu, sig, out.asMat); - out; - } - - def gamrnd(a:GND, b:GND, out:GND):GND = { - GMat.gamrnd(a.asMat, b.asMat, out.asMat); - out; - } - - - def asGMats(mat1:GND, mat2:GND, omat:ND, opname:String):(GMat, GMat, GMat, GND) = { - if (mat1._dims.length != mat2._dims.length) { - throw new RuntimeException("Operator "+opname+" inconsistent number of dims in operands") - } - val (nr1, nc1, nr2, nc2) = ND.compatibleDims(mat1._dims, mat2._dims, opname); - val a = new GMat(nr1, nc1, mat1.data, nr1*nc1); - val b = new GMat(nr2, nc2, mat2.data, nr2*nc2); - val nr3 = math.max(nr1, nr2) - val nc3 = math.max(nc1, nc2) - val xdims = if (mat1.length > mat2.length) mat1._dims else mat2._dims - val d = GND.newOrCheckGND(xdims, omat, mat1.GUID, mat2.GUID, opname.##) - val c = new GMat(nr3, nc3, d.data, nr3*nc3); - (a, b, c, d) - } - - def asGMats(mat1:GND, omat:ND, opname:String):(GMat, GMat, GND) = { - val d = GND.newOrCheckGND(mat1._dims, omat, mat1.GUID, opname.##) - val a = new GMat(mat1.length, 1, mat1.data, mat1.length); - val c = new GMat(mat1.length, 1, d.data, mat1.length); - (a, c, d) - } - - def newOrCheckGND(dims:Array[Int], out:ND):GND = { - if (out.asInstanceOf[AnyRef] != null && ND.checkDims("GND newOrCheckGND: ", out.dims.data, dims)) { - out.asInstanceOf[GND] - } else { - GND(dims) - } - } - - def newOrCheckGND(dims:IMat, out:ND):GND = newOrCheckGND(dims.data, out); - - def newOrCheckGND(dims:Array[Int], out:ND, matGuid:Long, opHash:Int):GND = { - if (out.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGND(dims, out) - } else { - val key = (matGuid, opHash) - val res = ND.cache2(key) - if (res != null) { - newOrCheckGND(dims, res) - } else { - val omat = newOrCheckGND(dims, null) - ND.cache2put(key, omat) - omat - } - } - } - - def newOrCheckGND(dims:IMat, out:ND, g1:Long, opHash:Int):GND = newOrCheckGND(dims.data, out, g1, opHash); - - def newOrCheckGND(dims:Array[Int], out:ND, guid1:Long, guid2:Long, opHash:Int):GND = { - if (out.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGND(dims, out) - } else { - val key = (guid1, guid2, opHash) - val res = ND.cache3(key) - if (res != null) { - newOrCheckGND(dims, res) - } else { - val omat = newOrCheckGND(dims, null) - ND.cache3put(key, omat) - omat - } - } - } - - def newOrCheckGND(dims:IMat, out:ND, g1:Long, g2:Long, opHash:Int):GND = newOrCheckGND(dims.data, out, g1, g2, opHash); - - def newOrCheckGND(dims:Array[Int], out:ND, g1:Long, g2:Long, g3:Long, opHash:Int):GND = { - if (out.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGND(dims, out) - } else { - val key = (g1, g2, g3, opHash) - val res = ND.cache4(key) - if (res != null) { - newOrCheckGND(dims, res) - } else { - val omat = newOrCheckGND(dims, null) - ND.cache4put(key, omat) - omat - } - } - } - - def newOrCheckGND(dims:IMat, out:ND, g1:Long, g2:Long, g3:Long, opHash:Int):GND = newOrCheckGND(dims.data, out, g1, g2, g3, opHash); -} - - - - - - diff --git a/src/main/scala/BIDMat/GSDMat.scala b/src/main/scala/BIDMat/GSDMat.scala deleted file mode 100755 index 4eb0fe4f..00000000 --- a/src/main/scala/BIDMat/GSDMat.scala +++ /dev/null @@ -1,725 +0,0 @@ -package BIDMat -import jcuda._ -import jcuda.jcublas._ -import jcuda.jcublas.JCublas._ -import jcuda.runtime.cudaMemcpyKind._ -import jcuda.jcusparse._ -import jcuda.runtime.JCuda._ -import jcuda.runtime.cudaError._ -import jcuda.runtime._ -import edu.berkeley.bid.CUMAT -import edu.berkeley.bid.CUMATD -import scala.util.hashing.MurmurHash3 -import GDMat._ -import GMat.BinOp -import java.io._ - -class GSDMat(nr0:Int, nc0:Int, nnz1:Int, @transient var pir:Pointer, @transient var pic:Pointer, @transient var pjc:Pointer, @transient var pdata:Pointer, val realnnz:Int) - extends SDMat(nr0, nc0, nnz1, null, null, null) { - - override def mytype = "GSDMat" - - override def nnz = nnz0 - - override def contents:GDMat = { - val out = new GDMat(nnz, 1, pdata, realnnz); - out.setGUID(MurmurHash3.mix(MurmurHash3.mix(nnz, 1), (GUID*7897889).toInt)); - out - } - - val myGPU = SciFunctions.getGPU - - var saveMe:SDMat = null - - private def writeObject(out:ObjectOutputStream):Unit = { - saveMe = SDMat(this); - out.defaultWriteObject(); - } - - private def readObject(in:ObjectInputStream):Unit = { - in.defaultReadObject(); - val gpu = SciFunctions.getGPU; - SciFunctions.setGPU(myGPU); - val tmp = GSDMat(saveMe); - pdata = tmp.pdata; - pir = tmp.pir; - pic = tmp.pic; - pjc = tmp.pjc; - SciFunctions.setGPU(gpu); - saveMe = null; - } - - override def toString:String = { - val nnz0 = scala.math.min(nnz,12) - val tmpcols = IMat(nnz0,1) - val tmprows = IMat(nnz0,1) - val tmpdata = DMat(nnz0,1) - cudaMemcpy(Pointer.to(tmprows.data), pir, 1L * nnz0 * Sizeof.INT, cudaMemcpyDeviceToHost) - cudaMemcpy(Pointer.to(tmpdata.data), pdata, 1L * nnz0 * Sizeof.DOUBLE, cudaMemcpyDeviceToHost) - cudaMemcpy(Pointer.to(tmpcols.data), pic, 1L * nnz0 * Sizeof.INT, cudaMemcpyDeviceToHost) - cudaStreamSynchronize(Mat.SyncMethod) - var err = cudaGetLastError - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in GSDMat.toString " + cudaGetErrorString(err)) - } - val ncolsn = SciFunctions.maxi(tmpcols).v + 1 - val tmpMat = SDMat(nrows, ncolsn, tmprows.data, tmpcols.data, tmpdata.data) - tmpMat.toString - } - - override def copy:GSDMat = { - val out = GSDMat.newOrCheckGSDMat(nrows, ncols, nnz, nnz, null, GUID, "GSDMat.copy".##) - cudaMemcpy(out.pjc, pjc, 1L * Sizeof.INT * (ncols+1), cudaMemcpyDeviceToDevice) - cudaMemcpy(out.pir, pir, 1L * Sizeof.INT * nnz, cudaMemcpyDeviceToDevice) - cudaMemcpy(out.pic, pic, 1L * Sizeof.INT * nnz, cudaMemcpyDeviceToDevice) - cudaMemcpy(out.pdata, pdata, 1L * Sizeof.DOUBLE * nnz, cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - var err = cudaGetLastError - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in GSDMat.toString " + cudaGetErrorString(err)) - } - out - } - - override def colslice(col1:Int, col2:Int, omat:Mat):GSDMat = { - val locs = IMat(2,1); - cudaMemcpy(Pointer.to(locs.data), pjc.withByteOffset(col1 * Sizeof.INT), Sizeof.INT, cudaMemcpyKind.cudaMemcpyDeviceToHost); - cudaMemcpy(Pointer.to(locs.data).withByteOffset(Sizeof.INT), pjc.withByteOffset(col2 * Sizeof.INT), Sizeof.INT, cudaMemcpyKind.cudaMemcpyDeviceToHost); - cudaStreamSynchronize(Mat.SyncMethod); - val starti = locs(0); - val endi = locs(1); - val newnnz = endi - starti; - val newncols = col2 - col1; - val out = GSDMat.newOrCheckGSDMat(nrows, newncols, newnnz, newnnz, omat, GUID, col1, col2, "colslice".##); - var err = cudaMemcpy(out.pjc, pjc.withByteOffset(col1 * Sizeof.INT), 1L * Sizeof.INT * (newncols+1), cudaMemcpyKind.cudaMemcpyDeviceToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - if (err == 0) err = cudaMemcpy(out.pir, pir.withByteOffset(starti*Sizeof.INT), 1L * Sizeof.INT * newnnz, cudaMemcpyKind.cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod); - if (err == 0) err = cudaMemcpy(out.pic, pic.withByteOffset(starti*Sizeof.INT), 1L * Sizeof.INT * newnnz, cudaMemcpyKind.cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod); - if (err == 0) err = cudaMemcpy(out.pdata, pdata.withByteOffset(starti*Sizeof.DOUBLE), 1L * Sizeof.DOUBLE * newnnz, cudaMemcpyKind.cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod); - val tjc = new GIMat(newncols+1, 1, out.pjc, newncols + 1); - tjc ~ tjc - starti; - val cc = new GIMat(newnnz, 1, out.pic, newnnz); - cc ~ cc - col1; - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in GSDMAT.colslice " + cudaGetErrorString(err)) - } - out - } - - override def colslice(col1:Int, col2:Int):GSDMat = colslice(col1, col2, null); - - def toSDMat():SDMat = { - val out = SDMat.newOrCheckSDMat(nrows, ncols, nnz, null, GUID, "toSMat".##) - val tmpcols = IMat.newOrCheckIMat(nnz, 1, null, GUID, "toSMat_tmp".##) - cudaMemcpy(Pointer.to(out.ir), pir, 1L * nnz * Sizeof.INT, cudaMemcpyDeviceToHost) - cudaMemcpy(Pointer.to(out.data), pdata, 1L * nnz * Sizeof.DOUBLE, cudaMemcpyDeviceToHost) - cudaMemcpy(Pointer.to(tmpcols.data), pic, 1L * nnz * Sizeof.INT, cudaMemcpyDeviceToHost) - cudaStreamSynchronize(Mat.SyncMethod) - var err = cudaGetLastError() - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in GSDMat.toSDMat " + cudaGetErrorString(err)) - } - SparseMat.compressInds(tmpcols.data, ncols, out.jc, nnz) - if (Mat.ioneBased == 1) { - SparseMat.incInds(out.ir, out.ir) - } - out - } - - override def clear = { - var err = cudaMemset(pdata, 0, 1L*Sizeof.DOUBLE*nnz) - cudaStreamSynchronize(Mat.SyncMethod) - if (err == 0) err = cudaGetLastError() - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in GSDMat.clear " + cudaGetErrorString(err)) - } - this - } - - override def izeros(m:Int, n:Int) = { - GIMat.izeros(m,n) - } - - override def iones(m:Int, n:Int) = { - GIMat.iones(m,n) - } - - override def zeros(m:Int, n:Int) = { - GDMat.zeros(m,n) - } - - override def zeros(m:Int, n:Int, nnz:Int) = { - new GSDMat(m, n, 0, new Pointer, new Pointer, new Pointer, new Pointer, 0); - } - - override def full(omat:Mat):GDMat = { - val out = GDMat.newOrCheckGDMat(nrows, ncols, omat, GUID, "full".##) - out.clear - var err = CUMATD.full(pir, pic, pdata, out.pdata, nrows, ncols, nnz) - cudaStreamSynchronize(Mat.SyncMethod) - if (err == 0) err = cudaGetLastError - if (err != 0) throw new RuntimeException(("GPU %d full kernel error "+cudaGetErrorString(err)) format SciFunctions.getGPU) - out - } - - override def full():GDMat = full(null):GDMat - - override def free() = { - JCublas.cublasFree(pdata) - JCublas.cublasFree(pic) - JCublas.cublasFree(pir) - JCublas.cublasFree(pjc) - cudaStreamSynchronize(Mat.SyncMethod) - this - } - - override def recycle(nr:Int, nc:Int, nnzx:Int):GSDMat = { - if (realnnz >= nnzx) { - new GSDMat(nr, nc, nnzx, pir, pic, pjc, pdata, realnnz) - } else { -// free - if (Mat.useGPUcache) { - val m = GSDMat(nr, nc, (Mat.recycleGrow * nnzx).toInt) - m.nnz0 = nnzx - m - } else { - GSDMat(nr, nc, nnzx) - } - } - } - - def sum(n:Int, oldmat:Mat) = { - val nn = if (n > 0) n else if (nrows == 1) 2 else 1 - val out = GDMat.newOrCheckGDMat(if (nn==1) 1 else nrows, if (nn==1) ncols else 1, oldmat, GUID, 0, "sum".##) - out.clear - Mat.nflops += nnz - val err = CUMATD.spsum(nrows, ncols, nnz, pir, pic, pdata, out.pdata, nn) - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in GSDMat.sum " + cudaGetErrorString(err)) - } - out - } - - // This works, but unfortunately is very slow. - - def SDMult(a:GDMat, omat:Mat):GDMat = { - if (ncols != a.nrows) { - throw new RuntimeException("SDMult dimensions mismatch") - } - val out = GDMat.newOrCheckGDMat(nrows, a.ncols, omat, GUID, a.GUID, "SDMult".##) - val handle = GSMat.getHandle; - val descra = GSMat.getDescr; - val zero = MatFunctions.dzeros(1,1); - val one = MatFunctions.dones(1,1); - var err = JCusparse.cusparseDcsrmm(handle, cusparseOperation.CUSPARSE_OPERATION_TRANSPOSE, - ncols, a.ncols, nrows, nnz, - Pointer.to(one.data), descra, pdata, pjc, pir, a.pdata, a.nrows, - Pointer.to(zero.data), out.pdata, out.nrows); - cudaStreamSynchronize(Mat.SyncMethod); - if (err == 0) err = cudaGetLastError; - if (err != 0) { - println("device is %d" format SciFunctions.getGPU); - throw new RuntimeException("Cuda error in GSDMat.SDMult " + cudaGetErrorString(err)); - } - Mat.nflops += 2L*nnz*a.ncols; - out; - } - - // This one is OK, but may throw CUDA resource errors with large nrows - - def SDTMult(a:GDMat, omat:Mat):GDMat = { - if (nrows != a.nrows) { - throw new RuntimeException("SDTMult dimensions mismatch"); - } - val out = GDMat.newOrCheckGDMat(ncols, a.ncols, omat, GUID, a.GUID, "SDMult".##); - val handle = GSMat.getHandle; - val descra = GSMat.getDescr; - val zero = MatFunctions.dzeros(1,1); - val one = MatFunctions.dones(1,1); - var err = JCusparse.cusparseDcsrmm(handle, cusparseOperation.CUSPARSE_OPERATION_NON_TRANSPOSE, - ncols, a.ncols, nrows, nnz, - Pointer.to(one.data), descra, pdata, pjc, pir, a.pdata, a.nrows, - Pointer.to(zero.data), out.pdata, out.nrows); - cudaStreamSynchronize(Mat.SyncMethod); - if (err == 0) err = cudaGetLastError; - if (err != 0) { - println("device is %d" format SciFunctions.getGPU); - throw new RuntimeException("Cuda error in GSDMat.SDTMult " + cudaGetErrorString(err)); - } - Mat.nflops += 2L*nnz*a.ncols; - out; - } - - def GSDop(b:GDMat, omat:Mat, op:Int):GSDMat = { - if (b.nrows > 1 && b.ncols > 1) { - throw new RuntimeException("Sorry only edge operators supported for GSDMat op GDMat") - } - if (b.nrows != nrows && b.ncols != ncols && b.length > 1) { - throw new RuntimeException("GSDMat op GDMat: dimensions mismatch") - } - Mat.nflops += nnz; - val out = if (omat.asInstanceOf[AnyRef] != null) { - omat.asInstanceOf[GSDMat] - } else { - copy; - } - if (b.ncols > 1) { - CUMAT.sdopdrow(nrows, ncols, nnz, out.pdata, out.pic, b.pdata, b.length, op); - } else { - CUMAT.sdopdcol(nrows, ncols, nnz, out.pdata, out.pir, b.pdata, b.length, op); - } - out - } - - def ^*(a:GDMat) = SDTMult(a, null) - def Tx(a:GDMat) = SDTMult(a, null) - - def ~ (b: GDMat) = new GDPair(this, b) - - // NOTE: GSDMat op GDMat is an *Edge or Scalar* operation only, and acts only on the non-zeros of the matrix - - def + (a:GDMat) = GSDop(a, null, BinOp.op_add); - def - (a:GDMat) = GSDop(a, null, BinOp.op_sub); - def *@ (a:GDMat) = GSDop(a, null, BinOp.op_mul); - def ∘ (a:GDMat) = GSDop(a, null, BinOp.op_mul); - def / (a:GDMat) = GSDop(a, null, BinOp.op_div); - - def != (a : GDMat):GSDMat = GSDop(a, null, BinOp.op_ne); - def > (a : GDMat):GSDMat = GSDop(a, null, BinOp.op_gt); - def < (a : GDMat):GSDMat = GSDop(a, null, BinOp.op_lt); - def <= (a : GDMat):GSDMat = GSDop(a, null, BinOp.op_le); - def >= (a : GDMat):GSDMat = GSDop(a, null, BinOp.op_ge); - def == (a : GDMat):GSDMat = GSDop(a, null, BinOp.op_eq); - def max (a : GDMat):GSDMat = GSDop(a, null, BinOp.op_max); - def min (a : GDMat):GSDMat = GSDop(a, null, BinOp.op_min); - - // Scalar operators are applied only to the non-zeros of the matrix - - override def + (b : Double):GSDMat = GSDop(GDMat(b), null, BinOp.op_add); - override def - (b : Double):GSDMat = GSDop(GDMat(b), null, BinOp.op_sub); - override def *@ (b : Double):GSDMat = GSDop(GDMat(b), null, BinOp.op_mul); - override def ∘ (b : Double):GSDMat = GSDop(GDMat(b), null, BinOp.op_mul); - override def / (b : Double):GSDMat = GSDop(GDMat(b), null, BinOp.op_div); - - override def != (b : Double):GSDMat = GSDop(GDMat(b), null, BinOp.op_ne); - override def > (b : Double):GSDMat = GSDop(GDMat(b), null, BinOp.op_gt); - override def < (b : Double):GSDMat = GSDop(GDMat(b), null, BinOp.op_lt); - override def <= (b : Double):GSDMat = GSDop(GDMat(b), null, BinOp.op_le); - override def >= (b : Double):GSDMat = GSDop(GDMat(b), null, BinOp.op_ge); - override def == (b : Double):GSDMat = GSDop(GDMat(b), null, BinOp.op_eq); - override def max (b : Double):GSDMat = GSDop(GDMat(b), null, BinOp.op_max); - override def min (b : Double):GSDMat = GSDop(GDMat(b), null, BinOp.op_min); - // Scalar operators are applied only to the non-zeros of the matrix - - override def + (b : Float):GSDMat = GSDop(GDMat(b), null, BinOp.op_add); - override def - (b : Float):GSDMat = GSDop(GDMat(b), null, BinOp.op_sub); - override def *@ (b : Float):GSDMat = GSDop(GDMat(b), null, BinOp.op_mul); - override def ∘ (b : Float):GSDMat = GSDop(GDMat(b), null, BinOp.op_mul); - override def / (b : Float):GSDMat = GSDop(GDMat(b), null, BinOp.op_div); - - override def != (b : Float):GSDMat = GSDop(GDMat(b), null, BinOp.op_ne); - override def > (b : Float):GSDMat = GSDop(GDMat(b), null, BinOp.op_gt); - override def < (b : Float):GSDMat = GSDop(GDMat(b), null, BinOp.op_lt); - override def <= (b : Float):GSDMat = GSDop(GDMat(b), null, BinOp.op_le); - override def >= (b : Float):GSDMat = GSDop(GDMat(b), null, BinOp.op_ge); - override def == (b : Float):GSDMat = GSDop(GDMat(b), null, BinOp.op_eq); - override def max (b : Float):GSDMat = GSDop(GDMat(b), null, BinOp.op_max); - override def min (b : Float):GSDMat = GSDop(GDMat(b), null, BinOp.op_min); - - // Scalar operators are applied only to the non-zeros of the matrix - - override def + (b : Int):GSDMat = GSDop(GDMat(b.toDouble), null, BinOp.op_add); - override def - (b : Int):GSDMat = GSDop(GDMat(b.toDouble), null, BinOp.op_sub); - override def *@ (b : Int):GSDMat = GSDop(GDMat(b.toDouble), null, BinOp.op_mul); - override def ∘ (b : Int):GSDMat = GSDop(GDMat(b.toDouble), null, BinOp.op_mul); - override def / (b : Int):GSDMat = GSDop(GDMat(b.toDouble), null, BinOp.op_div); - - override def != (b : Int):GSDMat = GSDop(GDMat(b.toDouble), null, BinOp.op_ne); - override def > (b : Int):GSDMat = GSDop(GDMat(b.toDouble), null, BinOp.op_gt); - override def < (b : Int):GSDMat = GSDop(GDMat(b.toDouble), null, BinOp.op_lt); - override def <= (b : Int):GSDMat = GSDop(GDMat(b.toDouble), null, BinOp.op_le); - override def >= (b : Int):GSDMat = GSDop(GDMat(b.toDouble), null, BinOp.op_ge); - override def == (b : Int):GSDMat = GSDop(GDMat(b.toDouble), null, BinOp.op_eq); - override def max (b : Int):GSDMat = GSDop(GDMat(b.toDouble), null, BinOp.op_max); - override def min (b : Int):GSDMat = GSDop(GDMat(b.toDouble), null, BinOp.op_min); - - override def * (b : Mat) = Mop_Times.op(this, b, null) - override def *^ (b : Mat) = Mop_TimesT.op(this, b, null) - override def xT (b : Mat) = Mop_TimesT.op(this, b, null) - override def ^* (b : Mat) = Mop_TTimes.op(this, b, null) - override def Tx (b : Mat) = Mop_TTimes.op(this, b, null) - override def + (b : Mat) = Mop_Plus.sop(this, b, null) - override def - (b : Mat) = Mop_Minus.sop(this, b, null) - override def *@ (b : Mat) = Mop_ETimes.sop(this, b, null) - override def ∘ (b : Mat) = Mop_ETimes.sop(this, b, null) - override def / (b : Mat) = Mop_EDiv.sop(this, b, null) - - override def > (b : Mat) = Mop_GT.sop(this, b, null) - override def < (b : Mat) = Mop_LT.sop(this, b, null) - override def >= (b : Mat) = Mop_GE.sop(this, b, null) - override def <= (b : Mat) = Mop_LE.sop(this, b, null) - override def == (b : Mat) = Mop_EQ.sop(this, b, null) - override def === (b : Mat) = Mop_EQ.sop(this, b, null) - override def != (b : Mat) = Mop_NE.sop(this, b, null) - -} - -class GSDPair (val omat:Mat, val mat:GSDMat) extends Pair(omat, mat) { - def * (a:GDMat) = mat.SDMult(a, omat) - def Tx(a:GDMat) = mat.SDTMult(a, omat) - def ^*(a:GDMat) = mat.SDTMult(a, omat) - - def + (a:GDMat) = mat.GSDop(a, omat, BinOp.op_add); - def - (a:GDMat) = mat.GSDop(a, omat, BinOp.op_sub); - def *@ (a:GDMat) = mat.GSDop(a, omat, BinOp.op_mul); - def ∘ (a:GDMat) = mat.GSDop(a, omat, BinOp.op_mul); - def / (a:GDMat) = mat.GSDop(a, omat, BinOp.op_div); - - def != (a : GDMat):GSDMat = mat.GSDop(a, omat, BinOp.op_ne); - def > (a : GDMat):GSDMat = mat.GSDop(a, omat, BinOp.op_gt); - def < (a : GDMat):GSDMat = mat.GSDop(a, omat, BinOp.op_lt); - def <= (a : GDMat):GSDMat = mat.GSDop(a, omat, BinOp.op_le); - def >= (a : GDMat):GSDMat = mat.GSDop(a, omat, BinOp.op_ge); - def == (a : GDMat):GSDMat = mat.GSDop(a, omat, BinOp.op_eq); - def max (a : GDMat):GSDMat = mat.GSDop(a, omat, BinOp.op_max); - def min (a : GDMat):GSDMat = mat.GSDop(a, omat, BinOp.op_min); - - // Scalar operators are applied only to the non-zeros of the matrix - - override def + (b : Double):GSDMat = mat.GSDop(GDMat(b), omat, BinOp.op_add); - override def - (b : Double):GSDMat = mat.GSDop(GDMat(b), omat, BinOp.op_sub); - override def *@ (b : Double):GSDMat = mat.GSDop(GDMat(b), omat, BinOp.op_mul); - override def ∘ (b : Double):GSDMat = mat.GSDop(GDMat(b), omat, BinOp.op_mul); - override def / (b : Double):GSDMat = mat.GSDop(GDMat(b), omat, BinOp.op_div); - - override def != (b : Double):GSDMat = mat.GSDop(GDMat(b), omat, BinOp.op_ne); - override def > (b : Double):GSDMat = mat.GSDop(GDMat(b), omat, BinOp.op_gt); - override def < (b : Double):GSDMat = mat.GSDop(GDMat(b), omat, BinOp.op_lt); - override def <= (b : Double):GSDMat = mat.GSDop(GDMat(b), omat, BinOp.op_le); - override def >= (b : Double):GSDMat = mat.GSDop(GDMat(b), omat, BinOp.op_ge); - override def == (b : Double):GSDMat = mat.GSDop(GDMat(b), omat, BinOp.op_eq); - override def max (b : Double):GSDMat = mat.GSDop(GDMat(b), omat, BinOp.op_max); - override def min (b : Double):GSDMat = mat.GSDop(GDMat(b), omat, BinOp.op_min); - // Scalar operators are applied only to the non-zeros of the matrix - - override def + (b : Float):GSDMat = mat.GSDop(GDMat(b), omat, BinOp.op_add); - override def - (b : Float):GSDMat = mat.GSDop(GDMat(b), omat, BinOp.op_sub); - override def *@ (b : Float):GSDMat = mat.GSDop(GDMat(b), omat, BinOp.op_mul); - override def ∘ (b : Float):GSDMat = mat.GSDop(GDMat(b), omat, BinOp.op_mul); - override def / (b : Float):GSDMat = mat.GSDop(GDMat(b), omat, BinOp.op_div); - - override def != (b : Float):GSDMat = mat.GSDop(GDMat(b), omat, BinOp.op_ne); - override def > (b : Float):GSDMat = mat.GSDop(GDMat(b), omat, BinOp.op_gt); - override def < (b : Float):GSDMat = mat.GSDop(GDMat(b), omat, BinOp.op_lt); - override def <= (b : Float):GSDMat = mat.GSDop(GDMat(b), omat, BinOp.op_le); - override def >= (b : Float):GSDMat = mat.GSDop(GDMat(b), omat, BinOp.op_ge); - override def == (b : Float):GSDMat = mat.GSDop(GDMat(b), omat, BinOp.op_eq); - override def max (b : Float):GSDMat = mat.GSDop(GDMat(b), omat, BinOp.op_max); - override def min (b : Float):GSDMat = mat.GSDop(GDMat(b), omat, BinOp.op_min); - - // Scalar operators are applied only to the non-zeros of the matrix - - override def + (b : Int):GSDMat = mat.GSDop(GDMat(b.toDouble), omat, BinOp.op_add); - override def - (b : Int):GSDMat = mat.GSDop(GDMat(b.toDouble), omat, BinOp.op_sub); - override def *@ (b : Int):GSDMat = mat.GSDop(GDMat(b.toDouble), omat, BinOp.op_mul); - override def ∘ (b : Int):GSDMat = mat.GSDop(GDMat(b.toDouble), omat, BinOp.op_mul); - override def / (b : Int):GSDMat = mat.GSDop(GDMat(b.toDouble), omat, BinOp.op_div); - - override def != (b : Int):GSDMat = mat.GSDop(GDMat(b.toDouble), omat, BinOp.op_ne); - override def > (b : Int):GSDMat = mat.GSDop(GDMat(b.toDouble), omat, BinOp.op_gt); - override def < (b : Int):GSDMat = mat.GSDop(GDMat(b.toDouble), omat, BinOp.op_lt); - override def <= (b : Int):GSDMat = mat.GSDop(GDMat(b.toDouble), omat, BinOp.op_le); - override def >= (b : Int):GSDMat = mat.GSDop(GDMat(b.toDouble), omat, BinOp.op_ge); - override def == (b : Int):GSDMat = mat.GSDop(GDMat(b.toDouble), omat, BinOp.op_eq); - override def max (b : Int):GSDMat = mat.GSDop(GDMat(b.toDouble), omat, BinOp.op_max); - override def min (b : Int):GSDMat = mat.GSDop(GDMat(b.toDouble), omat, BinOp.op_min); - - override def ^* (b : Mat):Mat = Mop_TTimes.op(mat, b, omat) - override def Tx (b : Mat):Mat = Mop_TTimes.op(mat, b, omat) - - override def * (b : Mat) = Mop_Times.op(mat, b, null) - override def *^ (b : Mat) = Mop_TimesT.op(mat, b, null) - override def xT (b : Mat) = Mop_TimesT.op(mat, b, null) - override def + (b : Mat) = Mop_Plus.sop(mat, b, null) - override def - (b : Mat) = Mop_Minus.sop(mat, b, null) - override def *@ (b : Mat) = Mop_ETimes.sop(mat, b, null) - override def ∘ (b : Mat) = Mop_ETimes.sop(mat, b, null) - override def / (b : Mat) = Mop_EDiv.sop(mat, b, null) - - override def > (b : Mat) = Mop_GT.sop(mat, b, null) - override def < (b : Mat) = Mop_LT.sop(mat, b, null) - override def >= (b : Mat) = Mop_GE.sop(mat, b, null) - override def <= (b : Mat) = Mop_LE.sop(mat, b, null) - override def == (b : Mat) = Mop_EQ.sop(mat, b, null) - override def === (b : Mat) = Mop_EQ.sop(mat, b, null) - override def != (b : Mat) = Mop_NE.sop(mat, b, null) -} - -object GSDMat { - - def apply(nr:Int, nc:Int, nnzx:Int, realnnzx:Int):GSDMat = { -// println("nr, nc, nnz = %d,%d,%d" format (nr,nc,nnz0)) - var err=0; - val realnnzy = math.max(1, realnnzx); - val out = new GSDMat(nr, nc, nnzx, new Pointer(), new Pointer(), new Pointer(), new Pointer(), realnnzy) - if (Mat.debugMem) println("GSDMat %d %d %d, %d %f" format (nr, nc, nnzx, SciFunctions.getGPU, SciFunctions.GPUmem._1)) - err = JCublas.cublasAlloc(out.nnz, Sizeof.INT, out.pir) - if (err == 0) err = JCublas.cublasAlloc(out.nnz, Sizeof.INT, out.pic) - if (err == 0) err = JCublas.cublasAlloc(out.ncols+1, Sizeof.INT, out.pjc) - if (err == 0) err = JCublas.cublasAlloc(out.nnz, Sizeof.DOUBLE, out.pdata) - cudaStreamSynchronize(Mat.SyncMethod) - if (err == 0) err = cudaGetLastError - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in GSDMat() " + cudaGetErrorString(err)) - } - out - } - - def apply(nr:Int, nc:Int, nnzx:Int):GSDMat = apply(nr, nc, nnzx, nnzx); - - def apply(a:SDMat):GSDMat = { - a match { - case g:GSDMat => g; - case _ => fromSDMat(a, null); - } - } - - def apply(a:SMat):GSDMat = fromSMat(a, null); - - def apply(a:GSMat):GSDMat = { - val out = GSDMat.newOrCheckGSDMat(a.nrows, a.ncols, a.nnz, a.nnz, null, a.GUID, "fromGSMat".##); - var err = cudaMemcpy(out.pir, a.pir, 1L * a.nnz*Sizeof.INT, cudaMemcpyDeviceToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - if (err == 0) err = cudaMemcpy(out.pic, a.pic, 1L * a.nnz*Sizeof.INT, cudaMemcpyDeviceToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - if (err == 0) err = CUMATD.FloatToDouble(a.pdata, out.pdata, a.nnz); - cudaStreamSynchronize(Mat.SyncMethod); - if (err == 0) err = cudaGetLastError(); - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("GSDMat(GSMat) error " + cudaGetErrorString(err)); - } - out; - } - - var myones:Array[GDMat] = null - var myzeros:Array[GDMat] = null - var zeroOnesInitialized = false - - def initZerosAndOnes = { - import SciFunctions._ - if (! zeroOnesInitialized) { - val thisGPU = getGPU; - val nGPUs = Mat.hasCUDA; - myzeros = new Array[GDMat](nGPUs); - myones = new Array[GDMat](nGPUs); - for (i <- 0 until nGPUs) { - setGPU(i); - myzeros(i) = GDMat.zeros(1,1); - myones(i) = GDMat.ones(1,1); - } - setGPU(thisGPU); - zeroOnesInitialized = true - } - } - - def fromSDMat(a:SDMat, b:GSDMat):GSDMat = { - val out = GSDMat.newOrCheckGSDMat(a.nrows, a.ncols, a.nnz, a.nnz, b, a.GUID, "fromSDMat".##) - out.nnz0 = a.nnz - val handle = GSMat.getHandle - cudaMemcpy(out.pdata, Pointer.to(a.data), 1L * a.nnz*Sizeof.DOUBLE, cudaMemcpyHostToDevice) - if (Mat.ioneBased == 1) { - cudaMemcpy(out.pir, Pointer.to(SparseMat.decInds(a.ir)), 1L * a.nnz*Sizeof.INT, cudaMemcpyHostToDevice) - cudaMemcpy(out.pjc, Pointer.to(SparseMat.decInds(a.jc)), 1L * (a.ncols+1)*Sizeof.INT, cudaMemcpyHostToDevice) - } else { - cudaMemcpy(out.pir, Pointer.to(a.ir), 1L * a.nnz*Sizeof.INT, cudaMemcpyHostToDevice) - cudaMemcpy(out.pjc, Pointer.to(a.jc), 1L * (a.ncols+1)*Sizeof.INT, cudaMemcpyHostToDevice) - } - cudaStreamSynchronize(Mat.SyncMethod) - var err = cudaGetLastError - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda copy error in GSDMat.fromSDMat " + cudaGetErrorString(err)) - } - if (err == 0) err = JCusparse.cusparseXcsr2coo(handle, out.pjc, out.nnz, out.ncols, out.pic, cusparseIndexBase.CUSPARSE_INDEX_BASE_ZERO) - cudaStreamSynchronize(Mat.SyncMethod) - if (err == 0) err = cudaGetLastError - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in GSDMat.fromSDMat " + cudaGetErrorString(err)) - } - out - } - - def fromSMat(a:SMat, b:GSDMat):GSDMat = { - val out = GSDMat.newOrCheckGSDMat(a.nrows, a.ncols, a.nnz, a.nnz, b, a.GUID, "fromSMat".##) - out.nnz0 = a.nnz - var err = 0 - val handle = GSMat.getHandle - val tmpdata = DMat(a.contents) - cudaMemcpy(out.pdata, Pointer.to(tmpdata.data), 1L*a.nnz*Sizeof.DOUBLE, cudaMemcpyHostToDevice) - if (Mat.ioneBased == 1) { - cudaMemcpy(out.pir, Pointer.to(SparseMat.decInds(a.ir)), 1L*a.nnz*Sizeof.INT, cudaMemcpyHostToDevice) - cudaMemcpy(out.pjc, Pointer.to(SparseMat.decInds(a.jc)), 1L*(a.ncols+1)*Sizeof.INT, cudaMemcpyHostToDevice) - } else { - cudaMemcpy(out.pir, Pointer.to(a.ir), 1L*a.nnz*Sizeof.INT, cudaMemcpyHostToDevice) - cudaMemcpy(out.pjc, Pointer.to(a.jc), 1L*(a.ncols+1)*Sizeof.INT, cudaMemcpyHostToDevice) - } - cudaStreamSynchronize(Mat.SyncMethod) - if (err == 0) err = cudaGetLastError - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda copy error in GSDMat.fromSDMat " + cudaGetErrorString(err)) - } - if (err == 0) err = JCusparse.cusparseXcsr2coo(handle, out.pjc, out.nnz, out.ncols, out.pic, cusparseIndexBase.CUSPARSE_INDEX_BASE_ZERO) - cudaStreamSynchronize(Mat.SyncMethod) - if (err == 0) err = cudaGetLastError - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in GSDMat.fromSDMat " + cudaGetErrorString(err)) - } - out - } - def DDS(A:GDMat, B:GDMat, C:GSDMat, oldmat:Mat):GSDMat = { - if (A.nrows != B.nrows || C.nrows != A.ncols || C.ncols != B.ncols) { - throw new RuntimeException("dimensions mismatch %d,%d %d,%d %d,%d" format (A.nrows, A.ncols, B.nrows, B.ncols, C.nrows, C.ncols)) - } -// println("DDS %d %d %d %d %f" format (C.nnz, C.GUID, C.myGPU, SciFunctions.getGPU, SciFunctions.GPUmem._1)) - val out = GSDMat.newOrCheckGSDMat(C.nrows, C.ncols, C.nnz, C.nnz, oldmat, A.GUID, B.GUID, C.GUID, "DDS".##) -// println("DDS1 %d %d %d %d %f" format (out.nnz, out.GUID, out.myGPU, SciFunctions.getGPU, SciFunctions.GPUmem._1)) - var err = cudaMemcpy(out.pir, C.pir, 1L * Sizeof.INT * C.nnz, cudaMemcpyKind.cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - if (err != 0) throw new RuntimeException(("GPU %d DDS row copy error "+cudaGetErrorString(err)) format SciFunctions.getGPU) - err = cudaMemcpy(out.pic, C.pic, 1L * Sizeof.INT * C.nnz, cudaMemcpyKind.cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - if (err != 0) throw new RuntimeException(("GPU %d DDS column copy error "+cudaGetErrorString(err)) format SciFunctions.getGPU) - out.clear; - err = CUMATD.dds(A.nrows, C.nnz, A.pdata, B.pdata, C.pir, C.pic, out.pdata) - if (err != 0) throw new RuntimeException(("GPU %d DDS kernel error "+cudaGetErrorString(err)) format SciFunctions.getGPU) - Mat.nflops += 2L * C.nnz * A.nrows - out - } - - def DDS0(A:GDMat, B:GDMat, C:GSDMat, oldmat:Mat):GSDMat = { - if (A.nrows != B.nrows || C.nrows != A.ncols || C.ncols != B.ncols) { - throw new RuntimeException("dimensions mismatch") - } -// println("DDS %d %d %d %d %f" format (C.nnz, C.GUID, C.myGPU, SciFunctions.getGPU, SciFunctions.GPUmem._1)) - val out = GSDMat.newOrCheckGSDMat(C.nrows, C.ncols, C.nnz, C.nnz, oldmat, A.GUID, B.GUID, C.GUID, "DDS".##) -// println("DDS1 %d %d %d %d %f" format (out.nnz, out.GUID, out.myGPU, SciFunctions.getGPU, SciFunctions.GPUmem._1)) - var err = cudaMemcpy(out.pir, C.pir, 1L * Sizeof.INT * C.nnz, cudaMemcpyKind.cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - if (err != 0) throw new RuntimeException(("GPU %d DDS row copy error "+cudaGetErrorString(err)) format SciFunctions.getGPU) - err = cudaMemcpy(out.pic, C.pic, 1L * Sizeof.INT * C.nnz, cudaMemcpyKind.cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod) - if (err != 0) throw new RuntimeException(("GPU %d DDS column copy error "+cudaGetErrorString(err)) format SciFunctions.getGPU) - out.clear; - err = CUMATD.dds0(A.nrows, C.ncols, A.pdata, B.pdata, C.pir, C.pjc, out.pdata) - if (err != 0) throw new RuntimeException(("GPU %d DDS kernel error "+cudaGetErrorString(err)) format SciFunctions.getGPU) - Mat.nflops += 2L * C.nnz * A.nrows - out - } - - def newOrCheckGSDMat(nrows:Int, ncols:Int, nnz:Int, realnnz:Int, oldmat:Mat):GSDMat = { - val m = if (oldmat.asInstanceOf[AnyRef] == null || (oldmat.nrows ==0 && oldmat.ncols == 0)) { - if (Mat.useGPUcache) { - val m = GSDMat(nrows, ncols, (Mat.recycleGrow*nnz).toInt) - m.nnz0 = nnz - m - } else { - GSDMat(nrows, ncols, nnz, realnnz) - } - } else { - oldmat match { - case omat:GSDMat => if (oldmat.nrows == nrows && oldmat.ncols == ncols && nnz <= omat.realnnz) { - omat.nnz0 = nnz - omat - } else { - val m = omat.recycle(nrows, ncols, nnz) - if (oldmat.nrows == nrows && oldmat.ncols == ncols) m.setGUID(omat.GUID) - m - } - } - } -// println("nOCGM %d %d %d %d %d %d %d %d" format (nrows, ncols, nnz, m.nnz, m.realnnz, m.GUID, if (oldmat != null) oldmat.GUID else 0, SciFunctions.getGPU)) - m - } - - def newOrCheckGSDMat(nrows:Int, ncols:Int, nnz:Int, realnnz:Int, outmat:Mat, guid1:Long, opHash:Int):GSDMat = { - val m = if (outmat.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGSDMat(nrows, ncols, nnz, realnnz, outmat) - } else { - val key = (guid1, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache3(key) - val omat = newOrCheckGSDMat(nrows, ncols, nnz, realnnz, res) - if (res != omat) Mat.cache3put(key, omat) - omat - } - if (m.myGPU != SciFunctions.getGPU) { - throw new RuntimeException("newOrCheckGSDMat1 wrong thread %d %d for mat %d" format (m.myGPU, SciFunctions.getGPU, m.GUID)) - } - if (Mat.debugMem) { - println("nOCGSM1: %d %d %d %d, %d %d %d, %d %f" format (nrows, ncols, nnz, m.realnnz, - if (outmat != null) outmat.GUID else 0, guid1, m.GUID, SciFunctions.getGPU, SciFunctions.GPUmem._1)) - } - m - } - - def newOrCheckGSDMat(nrows:Int, ncols:Int, nnz:Int, realnnz:Int, outmat:Mat, guid1:Long, guid2:Long, opHash:Int):GSDMat = { - val m = if (outmat.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGSDMat(nrows, ncols, nnz, realnnz, outmat) - } else { - val key = (guid1, guid2, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache4(key) - val omat = newOrCheckGSDMat(nrows, ncols, nnz, realnnz, res) - if (res != omat) Mat.cache4put(key, omat) - omat - } - if (m.myGPU != SciFunctions.getGPU) { - throw new RuntimeException("newOrCheckGSDMat2 wrong thread %d %d for mat %d" format (m.myGPU, SciFunctions.getGPU, m.GUID)) - } - if (Mat.debugMem) { - println("nOCGSM2: %d %d %d %d, %d %d %d %d, %d %f" format (nrows, ncols, nnz, m.realnnz, - if (outmat != null) outmat.GUID else 0, guid1, guid2, m.GUID, SciFunctions.getGPU, SciFunctions.GPUmem._1)) - } - m - } - - - def newOrCheckGSDMat(nrows:Int, ncols:Int, nnz:Int, realnnz:Int, outmat:Mat, guid1:Long, guid2:Long, guid3:Long, opHash:Int):GSDMat = { - val m = if (outmat.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGSDMat(nrows, ncols, nnz, realnnz, outmat) - } else { - val key = (guid1, guid2, guid3, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache5(key) - val omat = newOrCheckGSDMat(nrows, ncols, nnz, realnnz, res) - if (res != omat) Mat.cache5put(key, omat) - omat - } - if (m.myGPU != SciFunctions.getGPU) { - throw new RuntimeException("newOrCheckGSDMat3 wrong thread %d %d for mat %d" format (m.myGPU, SciFunctions.getGPU, m.GUID)) - } - if (Mat.debugMem) { - println("nOCGSM3: %d %d %d %d, %d %d %d %d %d, %d %f" format (nrows, ncols, nnz, m.realnnz, - if (outmat != null) outmat.GUID else 0, guid1, guid2, guid3, m.GUID, SciFunctions.getGPU, SciFunctions.GPUmem._1)) - } - m - } -} - - - - - - - diff --git a/src/main/scala/BIDMat/GSMat.scala b/src/main/scala/BIDMat/GSMat.scala deleted file mode 100755 index 1dd453bc..00000000 --- a/src/main/scala/BIDMat/GSMat.scala +++ /dev/null @@ -1,814 +0,0 @@ -package BIDMat -import jcuda._ -import jcuda.jcublas._ -import jcuda.jcublas.JCublas._ -import jcuda.jcusparse._ -import jcuda.runtime.JCuda._ -import jcuda.runtime.cudaError._ -import jcuda.runtime._ -import edu.berkeley.bid.CUMAT -import scala.util.hashing.MurmurHash3 -import GMat._ -import java.io._ - -class GSMat(nr0:Int, nc0:Int, nnz1:Int, @transient var pir:Pointer, @transient var pic:Pointer, @transient var pjc:Pointer, - @transient var pdata:Pointer, val realnnz:Int) extends SMat(nr0, nc0, nnz1, null, null, null) { - - override def mytype = "GSMat" - - override def nnz = nnz0 - - override def contents:GMat = { - val out = new GMat(nnz, 1, pdata, realnnz); - out.setGUID(MurmurHash3.mix(MurmurHash3.mix(nnz, 1), (GUID*7897889).toInt)); - out - } - - val myGPU = SciFunctions.getGPU - - var saveMe:SMat = null - - private def writeObject(out:ObjectOutputStream):Unit = { - saveMe = SMat(this); - out.defaultWriteObject(); - } - - private def readObject(in:ObjectInputStream):Unit = { - in.defaultReadObject(); - val gpu = SciFunctions.getGPU; - SciFunctions.setGPU(myGPU); - val tmp = GSMat(saveMe); - pdata = tmp.pdata; - pir = tmp.pir; - pic = tmp.pic; - pjc = tmp.pjc; - SciFunctions.setGPU(gpu); - saveMe = null; - } - - override def toString:String = { - val nnz0 = scala.math.min(nnz,12) - val tmpcols = IMat(nnz0,1) - val tmprows = IMat(nnz0,1) - val tmpdata = FMat(nnz0,1) - var err = JCublas.cublasGetVector(nnz0, Sizeof.INT, pir, 1, Pointer.to(tmprows.data), 1) - cudaStreamSynchronize(Mat.SyncMethod) - if (err == 0) err = cublasGetVector(nnz0, Sizeof.FLOAT, pdata, 1, Pointer.to(tmpdata.data), 1) - cudaStreamSynchronize(Mat.SyncMethod) - if (err == 0) err = cublasGetVector(nnz0, Sizeof.INT, pic, 1, Pointer.to(tmpcols.data), 1) - cudaStreamSynchronize(Mat.SyncMethod) - if (err == 0) err = cublasGetError() - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in GSMAT.toString " + cudaGetErrorString(err)) - } - val ncolsn = SciFunctions.maxi(tmpcols).v + 1 - val tmpMat = SMat(nrows, ncolsn, tmprows.data, tmpcols.data, tmpdata.data) - tmpMat.toString - } - - override def copy:GSMat = copy(null, "GSMat.copy".##, 0) - - def copy(omat:Mat, key1:Long, key2:Int):GSMat = { - val out = GSMat.newOrCheckGSMat(nrows, ncols, nnz, realnnz, omat, GUID, key1, key2) - copyTo(out) - } - - override def colslice(col1:Int, col2:Int, omat:Mat):GSMat = { - val locs = IMat(2,1); - cudaMemcpy(Pointer.to(locs.data), pjc.withByteOffset(col1 * Sizeof.INT), Sizeof.INT, cudaMemcpyKind.cudaMemcpyDeviceToHost); - cudaMemcpy(Pointer.to(locs.data).withByteOffset(Sizeof.INT), pjc.withByteOffset(col2 * Sizeof.INT), Sizeof.INT, cudaMemcpyKind.cudaMemcpyDeviceToHost); - cudaStreamSynchronize(Mat.SyncMethod); - val starti = locs(0); - val endi = locs(1); - val newnnz = endi - starti; - val newncols = col2 - col1; - val out = GSMat.newOrCheckGSMat(nrows, newncols, newnnz, newnnz, omat, GUID, col1, col2, "colslice".##); - var err = cudaMemcpy(out.pjc, pjc.withByteOffset(col1 * Sizeof.INT), 1L * Sizeof.INT * (newncols+1), cudaMemcpyKind.cudaMemcpyDeviceToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - if (err == 0) err = cudaMemcpy(out.pir, pir.withByteOffset(starti*Sizeof.INT), 1L * Sizeof.INT * newnnz, cudaMemcpyKind.cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod); - if (err == 0) err = cudaMemcpy(out.pic, pic.withByteOffset(starti*Sizeof.INT), 1L * Sizeof.INT * newnnz, cudaMemcpyKind.cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod); - if (err == 0) err = cudaMemcpy(out.pdata, pdata.withByteOffset(starti*Sizeof.FLOAT), 1L * Sizeof.FLOAT * newnnz, cudaMemcpyKind.cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod); - val tjc = new GIMat(newncols+1, 1, out.pjc, newncols + 1); - tjc ~ tjc - starti; - val cc = new GIMat(newnnz, 1, out.pic, newnnz); - cc ~ cc - col1; - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in GSMAT.colslice " + cudaGetErrorString(err)) - } - out - } - - override def colslice(col1:Int, col2:Int):GSMat = colslice(col1, col2, null); - - def toSMat():SMat = { - val out = SMat.newOrCheckSMat(nrows, ncols, nnz, null, GUID, "toSMat".##) - val tmpcols = IMat.newOrCheckIMat(nnz, 1, null, GUID, "toSMat_tmp".##).data - var err = JCublas.cublasGetVector(nnz, Sizeof.INT, pir, 1, Pointer.to(out.ir), 1) - cudaStreamSynchronize(Mat.SyncMethod) - if (err == 0) err = JCublas.cublasGetVector(nnz, Sizeof.FLOAT, pdata, 1, Pointer.to(out.data), 1) - cudaStreamSynchronize(Mat.SyncMethod) - if (err == 0) JCublas.cublasGetVector(nnz, Sizeof.INT, pic, 1, Pointer.to(tmpcols), 1) - cudaStreamSynchronize(Mat.SyncMethod) - if (err == 0) err = cublasGetError() - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in GSMAT.toSMat " + cudaGetErrorString(err)) - } - SparseMat.compressInds(tmpcols, ncols, out.jc, nnz) - if (Mat.ioneBased == 1) { - SparseMat.incInds(out.ir, out.ir) - } - out - } - - override def copyTo(out:SMat) = { - if (nrows != out.nrows && ncols != out.ncols && nnz != out.nnz) { - throw new RuntimeException("GSMAT.copyTo dimensions mismatch") - } - val tmpcols = IMat.newOrCheckIMat(nnz, 1, null, GUID, "copyTo_tmp".##).data - var err = JCublas.cublasGetVector(nnz, Sizeof.INT, pir, 1, Pointer.to(out.ir), 1) - cudaStreamSynchronize(Mat.SyncMethod) - if (err == 0) err = JCublas.cublasGetVector(nnz, Sizeof.FLOAT, pdata, 1, Pointer.to(out.data), 1) - cudaStreamSynchronize(Mat.SyncMethod) - if (err == 0) JCublas.cublasGetVector(nnz, Sizeof.INT, pic, 1, Pointer.to(tmpcols), 1) - cudaStreamSynchronize(Mat.SyncMethod) - if (err == 0) err = cublasGetError() - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in GSMAT.copyTo " + cudaGetErrorString(err)) - } - SparseMat.compressInds(tmpcols, ncols, out.jc, nnz) - if (Mat.ioneBased == 1) { - SparseMat.incInds(out.ir, out.ir) - } - out - } - - override def copyTo(out:GSMat) = { - var err = cudaMemcpy(out.pjc, pjc, 1L * Sizeof.INT * (ncols+1), cudaMemcpyKind.cudaMemcpyDeviceToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - if (err == 0) err = cudaMemcpy(out.pir, pir, 1L * Sizeof.INT * nnz, cudaMemcpyKind.cudaMemcpyDeviceToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - if (err == 0) err = cudaMemcpy(out.pic, pic, 1L * Sizeof.INT * nnz, cudaMemcpyKind.cudaMemcpyDeviceToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - if (err == 0) err = cudaMemcpy(out.pdata, pdata, 1L * Sizeof.FLOAT * nnz, cudaMemcpyKind.cudaMemcpyDeviceToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in GSMAT.copyTo " + cudaGetErrorString(err)) - } - out - } - - override def copyTo(omat:Mat) = { - omat match { - case gs:GSMat => copyTo(gs) - case s:SMat => copyTo(s) - } - } - - override def clear = { - var err = cudaMemset(pdata, 0, Sizeof.FLOAT*nnz) - cudaStreamSynchronize(Mat.SyncMethod) - if (err == 0) err = cudaGetLastError() - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in GSMAT.clear " + cudaGetErrorString(err)) - } - this - } - - override def izeros(m:Int, n:Int) = { - GIMat.izeros(m,n) - } - - override def zeros(m:Int, n:Int) = { - GMat.zeros(m,n) - } - - override def zeros(dims:IMat):GMat = { - GMat.zeros(dims) - } - - override def zeros(m:Int, n:Int, nnz:Int) = { - GSMat(m, n, nnz) - } - - override def iones(m:Int, n:Int) = { - GIMat.iones(m,n) - } - - override def full(omat:Mat):GMat = { - val out = GMat.newOrCheckGMat(nrows, ncols, omat, GUID, "full".##) - out.clear - var err = CUMAT.full(pir, pic, pdata, out.pdata, nrows, ncols, nnz) - cudaStreamSynchronize(Mat.SyncMethod); - if (err == 0) err = cudaGetLastError - if (err != 0) throw new RuntimeException(("GPU %d full kernel error "+cudaGetErrorString(err)) format SciFunctions.getGPU) - out - } - - override def full():GMat = full(null):GMat - - var cacheT:GSMat = null - override def t():GSMat = { - cacheT = GSMat.newOrCheckGSMat(ncols,nrows,nnz,realnnz,cacheT) - GSMat.fromSMat(toSMat().t,cacheT) - cacheT - } - - override def free() = { - JCublas.cublasFree(pdata) - JCublas.cublasFree(pic) - JCublas.cublasFree(pir) - JCublas.cublasFree(pjc) - cudaStreamSynchronize(Mat.SyncMethod) - this - } - - override def recycle(nr:Int, nc:Int, nnzx:Int):GSMat = { - //println("Being recycle") - if (realnnz >= nnzx) { - new GSMat(nr, nc, nnzx, pir, pic, pjc, pdata, realnnz) - } else { -// free - if (Mat.useGPUcache) { - val m = GSMat(nr, nc, (Mat.recycleGrow * nnzx).toInt) - m.nnz0 = nnzx - m - } else { - GSMat(nr, nc, nnzx) - } - } - } - - def sum(n:Int, oldmat:Mat) = { - val nn = if (n > 0) n else if (nrows == 1) 2 else 1 - val out = GMat.newOrCheckGMat(if (nn==1) 1 else nrows, if (nn==1) ncols else 1, oldmat, GUID, n, "sum".##) - out.clear - Mat.nflops += nnz - val err = CUMAT.spsum(nrows, ncols, nnz, pir, pic, pdata, out.pdata, nn) - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in GSMAT.sum " + cudaGetErrorString(err)) - } - out - } - - // This works, but unfortunately is very slow. - - def SDMult(aa:FMat, omat:Mat):GMat = { - val a = GMat(aa); - if (ncols != a.nrows) { - throw new RuntimeException("SDMult dimensions mismatch") - } - val out = GMat.newOrCheckGMat(nrows, a.ncols, omat, GUID, a.GUID, "SDMult".##); - val handle = GSMat.getHandle; - val descra = GSMat.getDescr; - val zero = FMat.zeros(1,1); - val one = FMat.ones(1,1); - var err = JCusparse.cusparseScsrmm(handle, cusparseOperation.CUSPARSE_OPERATION_TRANSPOSE, - ncols, a.ncols, nrows, nnz, - Pointer.to(one.data), descra, pdata, pjc, pir, a.pdata, a.nrows, - Pointer.to(zero.data), out.pdata, out.nrows); - cudaStreamSynchronize(Mat.SyncMethod); - if (err == 0) err = cudaGetLastError; - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in GSMAT.SDMult " + cudaGetErrorString(err)) - } - Mat.nflops += 2L*nnz*a.ncols - out - } - - // This one is OK, but may throw CUDA resource errors with large nrows - - def SDTMult(aa:FMat, omat:Mat):GMat = { - val a = GMat(aa); - if (nrows != a.nrows) { - throw new RuntimeException("SDTMult dimensions mismatch") - } - val out = GMat.newOrCheckGMat(ncols, a.ncols, omat, GUID, a.GUID, "SDTMult".##) - val handle = GSMat.getHandle - val descra = GSMat.getDescr - val zero = FMat.zeros(1,1); - val one = FMat.ones(1,1); - var err = JCusparse.cusparseScsrmm(handle, cusparseOperation.CUSPARSE_OPERATION_NON_TRANSPOSE, - ncols, a.ncols, nrows, nnz, - Pointer.to(one.data), descra, pdata, pjc, pir, a.pdata, a.nrows, - Pointer.to(zero.data), out.pdata, out.nrows) - cudaStreamSynchronize(Mat.SyncMethod) - if (err == 0) err = cudaGetLastError - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in GSMAT.SDTMult " + cudaGetErrorString(err)) - } - Mat.nflops += 2L*nnz*a.ncols - out - } - - def GSDop(b:GMat, omat:Mat, op:Int):GSMat = { - if (b.nrows > 1 && b.ncols > 1) { - throw new RuntimeException("Sorry only edge operators supported for GSMat op GMat") - } - if (b.nrows != nrows && b.ncols != ncols && b.length > 1) { - throw new RuntimeException("GSMat op GMat: dimensions mismatch") - } - Mat.nflops += nnz; - val out = copy(omat, b.GUID, op); - if (b.ncols > 1) { - CUMAT.sdoprow(nrows, ncols, nnz, out.pdata, out.pic, b.pdata, b.length, op); - } else { - CUMAT.sdopcol(nrows, ncols, nnz, out.pdata, out.pir, b.pdata, b.length, op); - } - out - } - - def ~ (b: GMat) = new GPair(this, b) - - override def *(a:FMat) = SDMult(GMat(a), null); - override def ^*(a:FMat) = SDTMult(GMat(a), null); - override def Tx(a:FMat) = SDTMult(GMat(a), null); - - // NOTE: GSMat op GMat is an *Edge or Scalar* operation only, and acts only on the non-zeros of the matrix - - override def + (a : FMat):GSMat = GSDop(GMat(a), null, BinOp.op_add); - override def - (a : FMat):GSMat = GSDop(GMat(a), null, BinOp.op_sub); - override def *@ (a : FMat):GSMat = GSDop(GMat(a), null, BinOp.op_mul); - override def ∘ (a : FMat):GSMat = GSDop(GMat(a), null, BinOp.op_mul); - override def / (a : FMat):GSMat = GSDop(GMat(a), null, BinOp.op_div); - - override def != (a : FMat):GSMat = GSDop(GMat(a), null, BinOp.op_ne); - override def > (a : FMat):GSMat = GSDop(GMat(a), null, BinOp.op_gt); - override def < (a : FMat):GSMat = GSDop(GMat(a), null, BinOp.op_lt); - override def <= (a : FMat):GSMat = GSDop(GMat(a), null, BinOp.op_le); - override def >= (a : FMat):GSMat = GSDop(GMat(a), null, BinOp.op_ge); - override def == (a : FMat):GSMat = GSDop(GMat(a), null, BinOp.op_eq); - - override def max (a : FMat):GSMat = GSDop(GMat(a), null, BinOp.op_max); - override def min (a : FMat):GSMat = GSDop(GMat(a), null, BinOp.op_min) - - override def + (b : Float):GSMat = GSDop(GMat(b), null, BinOp.op_add); - override def - (b : Float):GSMat = GSDop(GMat(b), null, BinOp.op_sub); - override def *@ (b : Float):GSMat = GSDop(GMat(b), null, BinOp.op_mul); - override def ∘ (b : Float):GSMat = GSDop(GMat(b), null, BinOp.op_mul); - override def / (b : Float):GSMat = GSDop(GMat(b), null, BinOp.op_div); - - override def != (b : Float):GSMat = GSDop(GMat(b), null, BinOp.op_ne); - override def > (b : Float):GSMat = GSDop(GMat(b), null, BinOp.op_gt); - override def < (b : Float):GSMat = GSDop(GMat(b), null, BinOp.op_lt); - override def <= (b : Float):GSMat = GSDop(GMat(b), null, BinOp.op_le); - override def >= (b : Float):GSMat = GSDop(GMat(b), null, BinOp.op_ge); - override def == (b : Float):GSMat = GSDop(GMat(b), null, BinOp.op_eq); - override def max (b : Float):GSMat = GSDop(GMat(b), null, BinOp.op_max); - override def min (b : Float):GSMat = GSDop(GMat(b), null, BinOp.op_min); - - override def + (b : Double):GSMat = GSDop(GMat(b), null, BinOp.op_add); - override def - (b : Double):GSMat = GSDop(GMat(b), null, BinOp.op_sub); - override def *@ (b : Double):GSMat = GSDop(GMat(b), null, BinOp.op_mul); - override def ∘ (b : Double):GSMat = GSDop(GMat(b), null, BinOp.op_mul); - override def / (b : Double):GSMat = GSDop(GMat(b), null, BinOp.op_div); - - override def != (b : Double):GSMat = GSDop(GMat(b), null, BinOp.op_ne); - override def > (b : Double):GSMat = GSDop(GMat(b), null, BinOp.op_gt); - override def < (b : Double):GSMat = GSDop(GMat(b), null, BinOp.op_lt); - override def <= (b : Double):GSMat = GSDop(GMat(b), null, BinOp.op_le); - override def >= (b : Double):GSMat = GSDop(GMat(b), null, BinOp.op_ge); - override def == (b : Double):GSMat = GSDop(GMat(b), null, BinOp.op_eq); - override def max (b : Double):GSMat = GSDop(GMat(b), null, BinOp.op_max); - override def min (b : Double):GSMat = GSDop(GMat(b), null, BinOp.op_min); - - override def + (b : Int):GSMat = GSDop(GMat(b), null, BinOp.op_add); - override def - (b : Int):GSMat = GSDop(GMat(b), null, BinOp.op_sub); - override def *@ (b : Int):GSMat = GSDop(GMat(b), null, BinOp.op_mul); - override def ∘ (b : Int):GSMat = GSDop(GMat(b), null, BinOp.op_mul); - override def / (b : Int):GSMat = GSDop(GMat(b), null, BinOp.op_div); - - override def != (b : Int):GSMat = GSDop(GMat(b.toFloat), null, BinOp.op_ne); - override def > (b : Int):GSMat = GSDop(GMat(b.toFloat), null, BinOp.op_gt); - override def < (b : Int):GSMat = GSDop(GMat(b.toFloat), null, BinOp.op_lt); - override def <= (b : Int):GSMat = GSDop(GMat(b.toFloat), null, BinOp.op_le); - override def >= (b : Int):GSMat = GSDop(GMat(b.toFloat), null, BinOp.op_ge); - override def == (b : Int):GSMat = GSDop(GMat(b.toFloat), null, BinOp.op_eq); - override def max (b : Int):GSMat = GSDop(GMat(b.toFloat), null, BinOp.op_max); - override def min (b : Int):GSMat = GSDop(GMat(b.toFloat), null, BinOp.op_min); - - override def * (b : Mat) = Mop_Times.op(this, b, null) - override def *^ (b : Mat) = Mop_TimesT.op(this, b, null) - override def xT (b : Mat) = Mop_TimesT.op(this, b, null) - override def ^* (b : Mat) = Mop_TTimes.op(this, b, null) - override def Tx (b : Mat) = Mop_TTimes.op(this, b, null) - override def + (b : Mat) = Mop_Plus.sop(this, b, null) - override def - (b : Mat) = Mop_Minus.sop(this, b, null) - override def *@ (b : Mat) = Mop_ETimes.sop(this, b, null) - override def ∘ (b : Mat) = Mop_ETimes.sop(this, b, null) - override def / (b : Mat) = Mop_EDiv.sop(this, b, null) - - override def > (b : Mat) = Mop_GT.sop(this, b, null) - override def < (b : Mat) = Mop_LT.sop(this, b, null) - override def >= (b : Mat) = Mop_GE.sop(this, b, null) - override def <= (b : Mat) = Mop_LE.sop(this, b, null) - override def == (b : Mat) = Mop_EQ.sop(this, b, null) - override def === (b : Mat) = Mop_EQ.sop(this, b, null) - override def != (b : Mat) = Mop_NE.sop(this, b, null) - -} - -class GSPair (val omat:Mat, val mat:GSMat) extends Pair(omat, mat) { - def * (a:FMat) = mat.SDMult(GMat(a), omat) - def Tx (a:FMat) = mat.SDTMult(GMat(a), omat) - def ^* (a:FMat) = mat.SDTMult(GMat(a), omat) - - def + (a:FMat) = mat.GSDop(GMat(a), omat, BinOp.op_add); - def - (a:FMat) = mat.GSDop(GMat(a), omat, BinOp.op_sub); - def *@ (a:FMat) = mat.GSDop(GMat(a), omat, BinOp.op_mul); - def ∘ (a:FMat) = mat.GSDop(GMat(a), omat, BinOp.op_mul); - def / (a:FMat) = mat.GSDop(GMat(a), omat, BinOp.op_div); - - def != (a : FMat):GSMat = mat.GSDop(GMat(a), omat, BinOp.op_ne); - def > (a : FMat):GSMat = mat.GSDop(GMat(a), omat, BinOp.op_gt); - def < (a : FMat):GSMat = mat.GSDop(GMat(a), omat, BinOp.op_lt); - def <= (a : FMat):GSMat = mat.GSDop(GMat(a), omat, BinOp.op_le); - def >= (a : FMat):GSMat = mat.GSDop(GMat(a), omat, BinOp.op_ge); - def == (a : FMat):GSMat = mat.GSDop(GMat(a), omat, BinOp.op_eq); - def max (a : FMat):GSMat = mat.GSDop(GMat(a), omat, BinOp.op_max); - def min (a : FMat):GSMat = mat.GSDop(GMat(a), omat, BinOp.op_min); - - override def + (a:Float) = mat.GSDop(GMat(a), omat, BinOp.op_add); - override def - (a:Float) = mat.GSDop(GMat(a), omat, BinOp.op_sub); - override def *@ (a:Float) = mat.GSDop(GMat(a), omat, BinOp.op_mul); - override def ∘ (a:Float) = mat.GSDop(GMat(a), omat, BinOp.op_mul); - override def / (a:Float) = mat.GSDop(GMat(a), omat, BinOp.op_div); - - override def != (a : Float):GSMat = mat.GSDop(GMat(a), omat, BinOp.op_ne); - override def > (a : Float):GSMat = mat.GSDop(GMat(a), omat, BinOp.op_gt); - override def < (a : Float):GSMat = mat.GSDop(GMat(a), omat, BinOp.op_lt); - override def <= (a : Float):GSMat = mat.GSDop(GMat(a), omat, BinOp.op_le); - override def >= (a : Float):GSMat = mat.GSDop(GMat(a), omat, BinOp.op_ge); - override def == (a : Float):GSMat = mat.GSDop(GMat(a), omat, BinOp.op_eq); - override def max (a : Float):GSMat = mat.GSDop(GMat(a), omat, BinOp.op_max); - override def min (a : Float):GSMat = mat.GSDop(GMat(a), omat, BinOp.op_min); - - - override def + (a:Int) = mat.GSDop(GMat(a.toFloat), omat, BinOp.op_add); - override def - (a:Int) = mat.GSDop(GMat(a.toFloat), omat, BinOp.op_sub); - override def *@ (a:Int) = mat.GSDop(GMat(a.toFloat), omat, BinOp.op_mul); - override def ∘ (a:Int) = mat.GSDop(GMat(a.toFloat), omat, BinOp.op_mul); - override def / (a:Int) = mat.GSDop(GMat(a.toFloat), omat, BinOp.op_div); - - override def != (a : Int):GSMat = mat.GSDop(GMat(a.toFloat), omat, BinOp.op_ne); - override def > (a : Int):GSMat = mat.GSDop(GMat(a.toFloat), omat, BinOp.op_gt); - override def < (a : Int):GSMat = mat.GSDop(GMat(a.toFloat), omat, BinOp.op_lt); - override def <= (a : Int):GSMat = mat.GSDop(GMat(a.toFloat), omat, BinOp.op_le); - override def >= (a : Int):GSMat = mat.GSDop(GMat(a.toFloat), omat, BinOp.op_ge); - override def == (a : Int):GSMat = mat.GSDop(GMat(a.toFloat), omat, BinOp.op_eq); - override def max (a : Int):GSMat = mat.GSDop(GMat(a.toFloat), omat, BinOp.op_max); - override def min (a : Int):GSMat = mat.GSDop(GMat(a.toFloat), omat, BinOp.op_min); - - - override def + (a:Double) = mat.GSDop(GMat(a.toFloat), omat, BinOp.op_add); - override def - (a:Double) = mat.GSDop(GMat(a.toFloat), omat, BinOp.op_sub); - override def *@ (a:Double) = mat.GSDop(GMat(a.toFloat), omat, BinOp.op_mul); - override def ∘ (a:Double) = mat.GSDop(GMat(a.toFloat), omat, BinOp.op_mul); - override def / (a:Double) = mat.GSDop(GMat(a.toFloat), omat, BinOp.op_div); - - override def != (a : Double):GSMat = mat.GSDop(GMat(a.toFloat), omat, BinOp.op_ne); - override def > (a : Double):GSMat = mat.GSDop(GMat(a.toFloat), omat, BinOp.op_gt); - override def < (a : Double):GSMat = mat.GSDop(GMat(a.toFloat), omat, BinOp.op_lt); - override def <= (a : Double):GSMat = mat.GSDop(GMat(a.toFloat), omat, BinOp.op_le); - override def >= (a : Double):GSMat = mat.GSDop(GMat(a.toFloat), omat, BinOp.op_ge); - override def == (a : Double):GSMat = mat.GSDop(GMat(a.toFloat), omat, BinOp.op_eq); - override def max (a : Double):GSMat = mat.GSDop(GMat(a.toFloat), omat, BinOp.op_max); - override def min (a : Double):GSMat = mat.GSDop(GMat(a.toFloat), omat, BinOp.op_min); - - override def ^* (b : Mat) = Mop_TTimes.op(mat, b, omat) - override def Tx (b : Mat) = Mop_TTimes.op(mat, b, omat) - override def * (b : Mat) = Mop_Times.op(mat, b, omat) - override def *^ (b : Mat) = Mop_TimesT.op(mat, b, omat) - override def xT (b : Mat) = Mop_TimesT.op(mat, b, omat) - override def + (b : Mat) = Mop_Plus.sop(mat, b, null) - override def - (b : Mat) = Mop_Minus.sop(mat, b, null) - override def *@ (b : Mat) = Mop_ETimes.sop(mat, b, null) - override def ∘ (b : Mat) = Mop_ETimes.sop(mat, b, null) - override def / (b : Mat) = Mop_EDiv.sop(mat, b, null) - - override def > (b : Mat) = Mop_GT.sop(mat, b, null) - override def < (b : Mat) = Mop_LT.sop(mat, b, null) - override def >= (b : Mat) = Mop_GE.sop(mat, b, null) - override def <= (b : Mat) = Mop_LE.sop(mat, b, null) - override def == (b : Mat) = Mop_EQ.sop(mat, b, null) - override def === (b : Mat) = Mop_EQ.sop(mat, b, null) - override def != (b : Mat) = Mop_NE.sop(mat, b, null) - - -} - -object GSMat { - - def apply(nr:Int, nc:Int, nnzx:Int, realnnzx:Int):GSMat = { -// println("nr, nc, nnz = %d,%d,%d" format (nr,nc,nnz0)) - var err=0 - val realnnzy = math.max(1, realnnzx); - val out = new GSMat(nr, nc, nnzx, new Pointer(), new Pointer(), new Pointer(), new Pointer(), realnnzy) - if (Mat.debugMem) println("GSMat %d %d %d, %d %f" format (nr, nc, nnzx, SciFunctions.getGPU, SciFunctions.GPUmem._1)) - err = JCublas.cublasAlloc(out.realnnz, Sizeof.INT, out.pir) - if (err == 0) err = JCublas.cublasAlloc(out.realnnz, Sizeof.INT, out.pic) - if (err == 0) err = JCublas.cublasAlloc(out.ncols+1, Sizeof.INT, out.pjc) - if (err == 0) err = JCublas.cublasAlloc(out.realnnz, Sizeof.FLOAT, out.pdata) - cudaStreamSynchronize(Mat.SyncMethod) - if (err == 0) err = cudaGetLastError - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in GSMat() " + cudaGetErrorString(err)) - } - out - } - - def apply(nr:Int, nc:Int, nnzx:Int):GSMat = apply(nr, nc, nnzx, nnzx) - - def apply(a:SMat):GSMat = { - a match { - case g:GSMat => g; - case _ => { - fromSMat(a, null); - } - } - } - - var cusparseContexts:Array[cusparseHandle] = null - var cusparseMatDescrs:Array[cusparseMatDescr] = null - var myones:Array[GMat] = null - var myzeros:Array[GMat] = null - var cusparseContextsInitialized = false - var cusparseDescrsInitialized = false - var zeroOnesInitialized = false - - def initHandles = { - import BIDMat.SciFunctions._ - import jcuda.jcusparse.JCusparse._ - GSMat.synchronized { - if (!cusparseContextsInitialized) { - val thisGPU = getGPU - val nGPUs = Mat.hasCUDA - cusparseContexts = new Array[cusparseHandle](nGPUs) - for (i <- 0 until nGPUs) { - setGPU(i) - cusparseContexts(i) = new cusparseHandle(); - cusparseCreate(cusparseContexts(i)); - cusparseSetStream(cusparseContexts(i), Mat.SyncMethod); - } - setGPU(thisGPU) - cusparseContextsInitialized = true - } - } - } - - def initZerosAndOnes = { - import SciFunctions._ - if (! zeroOnesInitialized) { - val thisGPU = getGPU; - val nGPUs = Mat.hasCUDA; - myzeros = new Array[GMat](nGPUs); - myones = new Array[GMat](nGPUs); - for (i <- 0 until nGPUs) { - setGPU(i); - myzeros(i) = GMat.zeros(1,1); - myones(i) = GMat.ones(1,1); - } - setGPU(thisGPU); - zeroOnesInitialized = true - } - } - - def initDescrs = { - import BIDMat.SciFunctions._ - import jcuda.jcusparse.JCusparse._ - GSMat.synchronized { - if (!cusparseDescrsInitialized) { - val thisGPU = getGPU - val nGPUs = Mat.hasCUDA - cusparseMatDescrs = new Array[cusparseMatDescr](nGPUs) - for (i <- 0 until nGPUs) { - setGPU(i) - val descra = new cusparseMatDescr() - cusparseCreateMatDescr(descra); - cusparseSetMatType(descra, cusparseMatrixType.CUSPARSE_MATRIX_TYPE_GENERAL) - cusparseSetMatIndexBase(descra, cusparseIndexBase.CUSPARSE_INDEX_BASE_ZERO) - cusparseMatDescrs(i) = descra - } - setGPU(thisGPU) - cusparseDescrsInitialized = true - } - } - } - - def getHandle = { - if (!cusparseContextsInitialized) initHandles - cusparseContexts(SciFunctions.getGPU) - } - - def getDescr = { - if (!cusparseDescrsInitialized) initDescrs - cusparseMatDescrs(SciFunctions.getGPU) - } - - def fromSMat(a:SMat, b:GSMat):GSMat = { - val out = GSMat.newOrCheckGSMat(a.nrows, a.ncols, a.nnz, a.nnz, b, a.GUID, "fromSMat".##); - out.nnz0 = a.nnz; - var err = 0; - val handle = GSMat.getHandle; - cudaMemcpy(out.pdata, Pointer.to(a.data), 1L*a.nnz*Sizeof.FLOAT, cudaMemcpyKind.cudaMemcpyHostToDevice); - if (Mat.ioneBased == 1) { - cudaMemcpy(out.pir, Pointer.to(SparseMat.decInds(a.ir)), 1L*a.nnz*Sizeof.INT, cudaMemcpyKind.cudaMemcpyHostToDevice); - cudaMemcpy(out.pjc, Pointer.to(SparseMat.decInds(a.jc)), 1L*(a.ncols+1)*Sizeof.INT, cudaMemcpyKind.cudaMemcpyHostToDevice); - } else { - cudaMemcpy(out.pir, Pointer.to(a.ir), 1L*a.nnz*Sizeof.INT, cudaMemcpyKind.cudaMemcpyHostToDevice); - cudaMemcpy(out.pjc, Pointer.to(a.jc), 1L*(a.ncols+1)*Sizeof.INT, cudaMemcpyKind.cudaMemcpyHostToDevice); - } - cudaStreamSynchronize(Mat.SyncMethod); - if (err == 0) err = cudaGetLastError; - if (err != 0) { - println("device is %d" format SciFunctions.getGPU); - throw new RuntimeException("Cuda copy error in GSMAT.fromSMat " + cudaGetErrorString(err)); - } - if (err == 0) err = JCusparse.cusparseXcsr2coo(handle, out.pjc, out.nnz, out.ncols, out.pic, cusparseIndexBase.CUSPARSE_INDEX_BASE_ZERO) - cudaStreamSynchronize(Mat.SyncMethod) - if (err == 0) err = cudaGetLastError - if (err != 0) { - println("device is %d" format SciFunctions.getGPU) - throw new RuntimeException("Cuda error in GSMAT.fromSMat " + cudaGetErrorString(err)) - } - out - } - - def DDS(A:GMat, B:GMat, C:GSMat, oldmat:Mat):GSMat = { - if (A.nrows != B.nrows || C.nrows != A.ncols || C.ncols != B.ncols) { - throw new RuntimeException("dimensions mismatch %d,%d %d,%d %d,%d" format (A.nrows, A.ncols, B.nrows, B.ncols, C.nrows, C.ncols)) - } -// println("DDS %d %d %d %d %f" format (C.nnz, C.GUID, C.myGPU, SciFunctions.getGPU, SciFunctions.GPUmem._1)) - val out = GSMat.newOrCheckGSMat(C.nrows, C.ncols, C.nnz, C.realnnz, oldmat, A.GUID, B.GUID, C.GUID, "DDS".##) -// println("DDS1 %d %d %d %d %f" format (out.nnz, out.GUID, out.myGPU, SciFunctions.getGPU, SciFunctions.GPUmem._1)) - var err = cudaMemcpy(out.pir, C.pir, 1L * Sizeof.INT * C.nnz, cudaMemcpyKind.cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod); - if (err != 0) throw new RuntimeException(("GPU %d DDS row copy error "+cudaGetErrorString(err)) format SciFunctions.getGPU) - err = cudaMemcpy(out.pic, C.pic, 1L * Sizeof.INT * C.nnz, cudaMemcpyKind.cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod); - if (err != 0) throw new RuntimeException(("GPU %d DDS column copy error "+cudaGetErrorString(err)) format SciFunctions.getGPU) - out.clear; - err = CUMAT.dds(A.nrows, C.nnz, A.pdata, B.pdata, C.pir, C.pic, out.pdata) - if (err != 0) throw new RuntimeException(("GPU %d DDS kernel error "+cudaGetErrorString(err)) format SciFunctions.getGPU) - Mat.nflops += 2L * C.nnz * A.nrows - out - } - - def DDS0(A:GMat, B:GMat, C:GSMat, oldmat:Mat):GSMat = { - if (A.nrows != B.nrows || C.nrows != A.ncols || C.ncols != B.ncols) { - throw new RuntimeException("dimensions mismatch") - } -// println("DDS %d %d %d %d %f" format (C.nnz, C.GUID, C.myGPU, SciFunctions.getGPU, SciFunctions.GPUmem._1)) - val out = GSMat.newOrCheckGSMat(C.nrows, C.ncols, C.nnz, C.realnnz, oldmat, A.GUID, B.GUID, C.GUID, "DDS".##) -// println("DDS1 %d %d %d %d %f" format (out.nnz, out.GUID, out.myGPU, SciFunctions.getGPU, SciFunctions.GPUmem._1)) - var err = cudaMemcpy(out.pir, C.pir, 1L * Sizeof.INT * C.nnz, cudaMemcpyKind.cudaMemcpyDeviceToDevice) - cudaStreamSynchronize(Mat.SyncMethod); - if (err != 0) throw new RuntimeException(("GPU %d DDS column copy error "+cudaGetErrorString(err)) format SciFunctions.getGPU) - out.clear; - err = CUMAT.dds0(A.nrows, C.ncols, A.pdata, B.pdata, C.pir, C.pjc, out.pdata) - if (err != 0) throw new RuntimeException(("GPU %d DDS kernel error "+cudaGetErrorString(err)) format SciFunctions.getGPU) - Mat.nflops += 2L * C.nnz * A.nrows - out - } - - def oneHot(c:GIMat, ncats0:Int):GSMat = { - val ncats = if (ncats0 == 0) (SciFunctions.maxi(c).dv.toInt + 1) else ncats0; - val out = GSMat.newOrCheckGSMat(ncats, c.length, c.length, c.length, null, c.GUID, ncats, "oneHot".##); - var err = cudaMemcpy(out.pir, c.pdata, 1L * Sizeof.INT * c.length, cudaMemcpyKind.cudaMemcpyDeviceToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - if (err == 0) err = cudaGetLastError(); - if (err != 0) throw new RuntimeException(("GPU %d oneHot row copy error "+cudaGetErrorString(err)) format SciFunctions.getGPU); - err = CUMAT.setval(out.pdata, 1f, c.length); - if (err != 0) throw new RuntimeException(("GPU %d oneHot set error "+cudaGetErrorString(err)) format SciFunctions.getGPU); - err = CUMAT.initSeq(out.pic, 1, c.length, 0); - if (err != 0) throw new RuntimeException(("GPU %d oneHot col set error "+cudaGetErrorString(err)) format SciFunctions.getGPU); - val handle = GSMat.getHandle; - if (err == 0) err = JCusparse.cusparseXcoo2csr(handle, out.pic, out.nnz, out.ncols, out.pjc, cusparseIndexBase.CUSPARSE_INDEX_BASE_ZERO); - cudaStreamSynchronize(Mat.SyncMethod); - if (err == 0) err = cudaGetLastError; - out - } - - def nHot(c:GIMat, ncats0:Int):GSMat = { - val ncats = if (ncats0 == 0) (SciFunctions.maxi(c.contents).dv.toInt + 1) else ncats0; - val out = GSMat.newOrCheckGSMat(ncats, c.ncols, c.length, c.length, null, c.GUID, ncats, "nHot".##); - var err = cudaMemcpy(out.pir, c.pdata, 1L * Sizeof.INT * c.length, cudaMemcpyKind.cudaMemcpyDeviceToDevice); - cudaStreamSynchronize(Mat.SyncMethod); - if (err == 0) err = cudaGetLastError(); - if (err != 0) throw new RuntimeException(("GPU %d nHot row copy error "+cudaGetErrorString(err)) format SciFunctions.getGPU); - err = CUMAT.setval(out.pdata, 1f, c.length); - if (err != 0) throw new RuntimeException(("GPU %d nHot set error "+cudaGetErrorString(err)) format SciFunctions.getGPU); - err = CUMAT.initSeq(out.pic, c.nrows, c.ncols, 0); - if (err != 0) throw new RuntimeException(("GPU %d nHot col set error "+cudaGetErrorString(err)) format SciFunctions.getGPU); - val handle = GSMat.getHandle; - if (err == 0) err = JCusparse.cusparseXcoo2csr(handle, out.pic, out.nnz, out.ncols, out.pjc, cusparseIndexBase.CUSPARSE_INDEX_BASE_ZERO); - cudaStreamSynchronize(Mat.SyncMethod); - if (err == 0) err = cudaGetLastError; - out - } - - def newOrCheckGSMat(nrows:Int, ncols:Int, nnz:Int, realnnz:Int, oldmat:Mat):GSMat = { - val m = if (oldmat.asInstanceOf[AnyRef] == null || (oldmat.nrows ==0 && oldmat.ncols == 0)) { - if (Mat.useGPUcache) { - val size = math.max((Mat.recycleGrow*nnz).toInt, realnnz) - val m = GSMat(nrows, ncols, nnz, size) - m - } else { - GSMat(nrows, ncols, nnz, realnnz) - } - } else { - oldmat match { - case omat:GSMat => if (oldmat.nrows == nrows && oldmat.ncols == ncols && nnz <= omat.realnnz) { - omat.nnz0 = nnz - omat - } else { - val m = omat.recycle(nrows, ncols, nnz) - if (oldmat.nrows == nrows && oldmat.ncols == ncols) m.setGUID(omat.GUID) - m - } - } - } -// println("nOCGM %d %d %d %d %d %d %d %d" format (nrows, ncols, nnz, m.nnz, m.realnnz, m.GUID, if (oldmat != null) oldmat.GUID else 0, SciFunctions.getGPU)) - m - } - - def newOrCheckGSMat(nrows:Int, ncols:Int, nnz:Int, realnnz:Int, outmat:Mat, guid1:Long, opHash:Int):GSMat = { - val m = if (outmat.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGSMat(nrows, ncols, nnz, realnnz, outmat) - } else { - val key = (guid1, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache3(key) - val omat = newOrCheckGSMat(nrows, ncols, nnz, realnnz, res) - if (res != omat) Mat.cache3put(key, omat) - omat - } - if (m.myGPU != SciFunctions.getGPU) { - throw new RuntimeException("newOrCheckGSMat1 wrong thread %d %d for mat %d" format (m.myGPU, SciFunctions.getGPU, m.GUID)) - } - if (Mat.debugMem) { - println("nOCGSM1: %d %d %d %d, %d %d %d, %d %f" format (nrows, ncols, nnz, m.realnnz, - if (outmat != null) outmat.GUID else 0, guid1, m.GUID, SciFunctions.getGPU, SciFunctions.GPUmem._1)) - } - m - } - - def newOrCheckGSMat(nrows:Int, ncols:Int, nnz:Int, realnnz:Int, outmat:Mat, guid1:Long, guid2:Long, opHash:Int):GSMat = { - val m = if (outmat.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGSMat(nrows, ncols, nnz, realnnz, outmat) - } else { - val key = (guid1, guid2, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache4(key) - val omat =newOrCheckGSMat(nrows, ncols, nnz, realnnz, res) - if (res != omat) Mat.cache4put(key, omat) - omat - } - if (m.myGPU != SciFunctions.getGPU) { - throw new RuntimeException("newOrCheckGSMat2 wrong thread %d %d for mat %d" format (m.myGPU, SciFunctions.getGPU, m.GUID)) - } - if (Mat.debugMem) { - println("nOCGSM2: %d %d %d %d, %d %d %d %d, %d %f" format (nrows, ncols, nnz, m.realnnz, - if (outmat != null) outmat.GUID else 0, guid1, guid2, m.GUID, SciFunctions.getGPU, SciFunctions.GPUmem._1)) - } - m - } - - - def newOrCheckGSMat(nrows:Int, ncols:Int, nnz:Int, realnnz:Int, outmat:Mat, guid1:Long, guid2:Long, guid3:Long, opHash:Int):GSMat = { - val m = if (outmat.asInstanceOf[AnyRef] != null || !Mat.useGPUcache) { - newOrCheckGSMat(nrows, ncols, nnz, realnnz, outmat) - } else { - val key = (guid1, guid2, guid3, opHash.toLong, SciFunctions.getGPU) - val res = Mat.cache5(key) - val omat = newOrCheckGSMat(nrows, ncols, nnz, realnnz, res) - if (res != omat) Mat.cache5put(key, omat) - omat - } - if (m.myGPU != SciFunctions.getGPU) { - throw new RuntimeException("newOrCheckGSMat3 wrong thread %d %d for mat %d" format (m.myGPU, SciFunctions.getGPU, m.GUID)) - } - if (Mat.debugMem) { - println("nOCGSM3: %d %d %d %d, %d %d %d %d %d, %d %f" format (nrows, ncols, nnz, m.realnnz, - if (outmat != null) outmat.GUID else 0, guid1, guid2, guid3, m.GUID, SciFunctions.getGPU, SciFunctions.GPUmem._1)) - } - m - } -} - - - - - - - diff --git a/src/main/scala/BIDMat/HMat.scala b/src/main/scala/BIDMat/HMat.scala index 4a0e98fc..0e7f7cfb 100755 --- a/src/main/scala/BIDMat/HMat.scala +++ b/src/main/scala/BIDMat/HMat.scala @@ -9,7 +9,6 @@ import scala.collection.mutable._ import scala.concurrent.Future import scala.concurrent.ExecutionContext.Implicits.global import MatFunctions._ -import MatHDF5._ import edu.berkeley.bid.UTILS._ import net.jpountz.lz4._ @@ -56,7 +55,7 @@ object HMat { } def checkHDFSloaded = { - if (HDFSIOinstance == null) { + if (HDFSIOinstance == null) { val classLoader = HMat.getClass.getClassLoader(); val HDFSIOclass = classLoader.loadClass("BIDMat.HDFSIO"); HDFSIOinstance = HDFSIOclass.newInstance().asInstanceOf[HDFSIOtrait]; diff --git a/src/main/scala/BIDMat/IDict.scala b/src/main/scala/BIDMat/IDict.scala index 922b1bf7..6bcc0128 100755 --- a/src/main/scala/BIDMat/IDict.scala +++ b/src/main/scala/BIDMat/IDict.scala @@ -1,8 +1,6 @@ package BIDMat import scala.collection.mutable.{Map,SynchronizedMap,HashMap} import MatFunctions._ -import edu.berkeley.bid.CUMAT - class IDict(val grams:IMat) extends Serializable { @@ -24,7 +22,7 @@ class IDict(val grams:IMat) extends Serializable { } sortedMat } - } + } @inline def cmp(a:IMat, b:IMat, ia:Int, ib:Int):Int = { var i = 0 @@ -100,7 +98,7 @@ object IDict { val (outy, ia, ib) = uniquerows(grams) val countsy = accum(ib, if (counts == null) drow(1.0) else counts, outy.nrows, 1) if (countsort) { - val (countsz, ip) = GFunctions.sortdown2(countsy) + val (countsz, ip) = DFunctions.sortdown2(countsy) IDict(outy(ip, ?), countsz) } else { IDict(outy, countsy) diff --git a/src/main/scala/BIDMat/IFunctions.scala b/src/main/scala/BIDMat/IFunctions.scala index c11f1846..7b69fc86 100644 --- a/src/main/scala/BIDMat/IFunctions.scala +++ b/src/main/scala/BIDMat/IFunctions.scala @@ -21,8 +21,8 @@ object IFunctions { /** Accumulate (row, col, value) tuples from inds \\ vals. nr and nc are row and column bounds */ def accum(inds:IMat, vals:IMat, nr:Int, nc:Int):IMat = { (inds, vals) match { - case (ginds:GIMat, fvals:IMat) => GIFunctions.accum(ginds, GIMat(fvals), null, nr, nc); - case (finds:IMat, gvals:GIMat) => GIFunctions.accum(GIMat(finds), gvals, null, nr, nc); +// case (ginds:GIMat, fvals:IMat) => GIFunctions.accum(ginds, GIMat(fvals), null, nr, nc); +// case (finds:IMat, gvals:GIMat) => GIFunctions.accum(GIMat(finds), gvals, null, nr, nc); case _ => IMat(DenseMat.accum(inds, vals, nr, nc)) } } @@ -37,7 +37,7 @@ object IFunctions { /** Accumulate (row, col, value) tuples from inds \\ vals. nr and nc are row and column bounds */ def accum(inds:IMat, v:Int, nr:Int, nc:Int) = { inds match { - case ginds:GIMat => GIFunctions.accum(ginds, v, null, nr, nc); +// case ginds:GIMat => GIFunctions.accum(ginds, v, null, nr, nc); case _ => IMat(DenseMat.accum(inds, IMat.ielem(v), nr, nc)); } } @@ -50,58 +50,58 @@ object IFunctions { def min(a:IMat, b:IMat, out:Mat) = { (a, b) match { - case (aa:GIMat, bb:IMat) => GIFunctions.min(aa, GIMat(b), out); - case (aa:IMat, bb:GIMat) => GIFunctions.min(GIMat(a), bb, out); +// case (aa:GIMat, bb:IMat) => GIFunctions.min(aa, GIMat(b), out); +// case (aa:IMat, bb:GIMat) => GIFunctions.min(GIMat(a), bb, out); case _ => a.iiMatOpv(b, IMat.vecMinFun, op_min, out); } } def max(a:IMat, b:IMat, out:Mat) = { (a, b) match { - case (aa:GIMat, bb:IMat) => GIFunctions.max(aa, GIMat(b), out); - case (aa:IMat, bb:GIMat) => GIFunctions.max(GIMat(a), bb, out); +// case (aa:GIMat, bb:IMat) => GIFunctions.max(aa, GIMat(b), out); +// case (aa:IMat, bb:GIMat) => GIFunctions.max(GIMat(a), bb, out); case _ => a.iiMatOpv(b, IMat.vecMaxFun, op_max, out); } } def min(a:IMat, b:Int, out:Mat) = { a match { - case aa:GIMat=> GIFunctions.min(aa, GIMat.elem(b), out); +// case aa:GIMat=> GIFunctions.min(aa, GIMat.elem(b), out); case _ => a.iiMatOpScalarv(b, IMat.vecMinFun, out); } } def max(a:IMat, b:Int, out:Mat) = { a match { - case aa:GIMat=> GIFunctions.max(aa, GIMat.elem(b), out); +// case aa:GIMat=> GIFunctions.max(aa, GIMat.elem(b), out); case _ => a.iiMatOpScalarv(b, IMat.vecMaxFun, out); } } def maxi(a:IMat, n:Int, out:Mat) = { a match { - case aa:GIMat => GIFunctions.maxi(aa, n, out); +// case aa:GIMat => GIFunctions.maxi(aa, n, out); case _ => a.iiReduceOpv(n, IMat.idFun, IMat.vecMaxFun, out); } } def mini(a:IMat, n:Int, out:Mat) = { a match { - case aa:GIMat => GIFunctions.mini(aa, n, out); +// case aa:GIMat => GIFunctions.mini(aa, n, out); case _ => a.iiReduceOpv(n, IMat.idFun, IMat.vecMinFun, out); } } def sum(a:IMat, n:Int, out:Mat) = { a match { - case aa:GIMat => GIFunctions.sum(aa, n, out); +// case aa:GIMat => GIFunctions.sum(aa, n, out); case _ => a.iiReduceOpv(n, IMat.idFun, IMat.vecAddFun, out); } } def prod(a:IMat, n:Int, out:Mat) = { a match { - case aa:GIMat => GIFunctions.prod(aa, n, out); +// case aa:GIMat => GIFunctions.prod(aa, n, out); case _ => a.iiReduceOpv(n, IMat.idFun, IMat.vecMulFun, out); } } @@ -116,7 +116,7 @@ object IFunctions { def maxi2(a:IMat,d:Int):(IMat,IMat) = { a match { - case aa:GIMat => GIFunctions.maxi2(aa, null, null, d); +// case aa:GIMat => GIFunctions.maxi2(aa, null, null, d); case _ => { val (m,ii)=a.ggOpt2(d,IMat.gtPred); (IMat(m), ii) @@ -126,7 +126,7 @@ object IFunctions { def mini2(a:IMat,d:Int):(IMat,IMat) = { a match { - case aa:GIMat => GIFunctions.mini2(aa, null, null, d); +// case aa:GIMat => GIFunctions.mini2(aa, null, null, d); case _ => { val (m,ii)=a.ggOpt2(d,IMat.ltPred); (IMat(m), ii) diff --git a/src/main/scala/BIDMat/IMat.scala b/src/main/scala/BIDMat/IMat.scala index 556ddc16..3f27cd03 100755 --- a/src/main/scala/BIDMat/IMat.scala +++ b/src/main/scala/BIDMat/IMat.scala @@ -70,16 +70,16 @@ case class IMat(dims0:Array[Int], val data:Array[Int]) extends DenseMat[Int](dim def horzcat(b: IMat):IMat = { (this, b) match { - case (aa:IMat, bb:GIMat) => GIMat(aa).horzcat(bb, null); - case (aa:GIMat, bb:IMat) => aa.horzcat(GIMat(bb), null); +// case (aa:IMat, bb:GIMat) => GIMat(aa).horzcat(bb, null); +// case (aa:GIMat, bb:IMat) => aa.horzcat(GIMat(bb), null); case _ => IMat(ghorzcat(b)); } } def vertcat(b: IMat):IMat = { (this, b) match { - case (aa:IMat, bb:GIMat) => GIMat(aa).vertcat(bb, null); - case (aa:GIMat, bb:IMat) => aa.vertcat(GIMat(bb), null); +// case (aa:IMat, bb:GIMat) => GIMat(aa).vertcat(bb, null); +// case (aa:GIMat, bb:IMat) => aa.vertcat(GIMat(bb), null); case _ => IMat(gvertcat(b)); } } @@ -541,8 +541,8 @@ case class IMat(dims0:Array[Int], val data:Array[Int]) extends DenseMat[Int](dim def iiMatOpv(b: Mat, f:(Array[Int],Int,Int,Array[Int],Int,Int,Array[Int],Int,Int,Int) => Int, optype:Int, out:Mat):IMat = (this, b) match { - case (aa:GIMat, bb:IMat) => aa.GIop(bb, out, optype); - case (aa:IMat, bb:GIMat) => GIMat(this).GIop(bb, out, optype); +// case (aa:GIMat, bb:IMat) => aa.GIop(bb, out, optype); +// case (aa:IMat, bb:GIMat) => GIMat(this).GIop(bb, out, optype); case (aa:IMat, bb:IMat) => IMat(ggMatOpv(bb, f, out)); case _ => throw new RuntimeException("unsupported operation "+f+" on "+this+" and "+b) } @@ -567,7 +567,7 @@ case class IMat(dims0:Array[Int], val data:Array[Int]) extends DenseMat[Int](dim override def copyTo(a:Mat) = { a match { - case aa:GIMat => aa.copyFrom(this); +// case aa:GIMat => aa.copyFrom(this); case out:IMat => System.arraycopy(data, 0, out.data, 0, length); case ff:FMat => {Mat.copyToFloatArray(data, 0, ff.data, 0, length)} case ii:LMat => {Mat.copyToLongArray(data, 0, ii.data, 0, ii.length)} @@ -746,7 +746,7 @@ case class IMat(dims0:Array[Int], val data:Array[Int]) extends DenseMat[Int](dim def cumsumByKey(keys:IMat, omat:Mat):IMat = { (this, keys) match { - case (gme:GIMat, gkeys:GIMat) => gme.cumsumByKey(gkeys, omat); +// case (gme:GIMat, gkeys:GIMat) => gme.cumsumByKey(gkeys, omat); case _ => { if (nrows != keys.nrows || ncols != keys.ncols) throw new RuntimeException("cumsumKey dimensions mismatch"); @@ -782,7 +782,7 @@ case class IMat(dims0:Array[Int], val data:Array[Int]) extends DenseMat[Int](dim def cummaxByKey(keys:IMat, omat:Mat):IMat = { (this, keys) match { - case (gme:GIMat, gkeys:GIMat) => gme.cummaxByKey(gkeys, omat); +// case (gme:GIMat, gkeys:GIMat) => gme.cummaxByKey(gkeys, omat); case _ => { if (nrows != keys.nrows || ncols != keys.ncols) throw new RuntimeException("cummaxKey dimensions mismatch"); @@ -817,7 +817,7 @@ case class IMat(dims0:Array[Int], val data:Array[Int]) extends DenseMat[Int](dim def cumminByKey(keys:IMat, omat:Mat):IMat = { (this, keys) match { - case (gme:GIMat, gkeys:GIMat) => gme.cumminByKey(gkeys, omat); +// case (gme:GIMat, gkeys:GIMat) => gme.cumminByKey(gkeys, omat); case _ => { if (nrows != keys.nrows || ncols != keys.ncols) throw new RuntimeException("cumminKey dimensions mismatch"); @@ -1171,7 +1171,8 @@ case class IMat(dims0:Array[Int], val data:Array[Int]) extends DenseMat[Int](dim /* * Specialize to GMats to help the type system. - */ + */ + /* def * (b : GMat) = Mop_Times.op(this, b, null) def *^ (b : GMat) = Mop_TimesT.op(this, b, null) def xT (b : GMat) = Mop_TimesT.op(this, b, null) @@ -1203,7 +1204,7 @@ case class IMat(dims0:Array[Int], val data:Array[Int]) extends DenseMat[Int](dim def >= (b : GMat) = Mop_GE.op(this, b, null) def <= (b : GMat) = Mop_LE.op(this, b, null) def != (b : GMat) = Mop_NE.op(this, b, null) - + */ /* * Operators whose second arg is generic. */ @@ -1518,13 +1519,13 @@ object IMat { def apply(x:Mat):IMat = { val out:IMat = x match { - case _:GIMat | _:DMat | _:FMat | _:LMat | _:BMat => IMat.newOrCheckIMat(x.dims, null, x.GUID, "IMat".##); + case _:DMat | _:FMat | _:LMat | _:BMat => IMat.newOrCheckIMat(x.dims, null, x.GUID, "IMat".##); case ff:IMat => ff; case dd:DenseMat[Int] @ unchecked => {val out = new IMat(dd.dims.data, dd._data); out.setGUID(dd.GUID); out} case _ => throw new RuntimeException("IMat apply unknown argument"); } x match { - case gg:GIMat => GIMat.GPUtoCPUarraycopy(gg.pdata, 0, out.data, 0, gg.length, "IMat apply"); +// case gg:GIMat => GIMat.GPUtoCPUarraycopy(gg.pdata, 0, out.data, 0, gg.length, "IMat apply"); case dd:DMat => {Mat.copyToIntArray(dd.data, 0, out.data, 0, dd.length)}; case ff:FMat => {Mat.copyToIntArray(ff.data, 0, out.data, 0, ff.length)}; case ff:LMat => {Mat.copyToIntArray(ff.data, 0, out.data, 0, ff.length)}; diff --git a/src/main/scala/BIDMat/LDict.scala b/src/main/scala/BIDMat/LDict.scala index 9f393d51..87cf86f3 100755 --- a/src/main/scala/BIDMat/LDict.scala +++ b/src/main/scala/BIDMat/LDict.scala @@ -1,7 +1,6 @@ package BIDMat import scala.collection.mutable.{Map,SynchronizedMap,HashMap} import MatFunctions._ -import edu.berkeley.bid.CUMAT /** * Note: this is still alpha code. Needs native code infill for long sort routines. @@ -18,15 +17,15 @@ class LDict(val grams:LMat) extends Serializable { var perm:IMat = null def makeSorted:LMat = { - this.synchronized { - if (sortedMat.asInstanceOf[AnyRef] == null) { + this.synchronized { + if (sortedMat.asInstanceOf[AnyRef] == null) { sortedMat = grams.copy perm = icol(0->grams.nrows) sortlexInds(sortedMat, perm) } sortedMat } - } + } @inline def cmp(a:LMat, b:LMat, ia:Int, ib:Int):Int = { var i = 0 @@ -102,7 +101,7 @@ object LDict { val (outy, ia, ib) = uniquerows(grams) val countsy = accum(ib, if (counts == null) drow(1.0) else counts, outy.nrows, 1) if (countsort) { - val (countsz, ip) = GFunctions.sortdown2(countsy) + val (countsz, ip) = DFunctions.sortdown2(countsy) LDict(outy(ip, ?), countsz) } else { LDict(outy, countsy) diff --git a/src/main/scala/BIDMat/LFunctions.scala b/src/main/scala/BIDMat/LFunctions.scala index e7b7a0fa..a5308983 100644 --- a/src/main/scala/BIDMat/LFunctions.scala +++ b/src/main/scala/BIDMat/LFunctions.scala @@ -21,8 +21,8 @@ object LFunctions { /** Accumulate (row, col, value) tuples from inds \\ vals. nr and nc are row and column bounds */ def accum(inds:IMat, vals:LMat, nr:Int, nc:Int):LMat = { (inds, vals) match { - case (ginds:GIMat, fvals:LMat) => GLFunctions.accum(ginds, GLMat(fvals), null, nr, nc); - case (finds:IMat, gvals:GLMat) => GLFunctions.accum(GIMat(finds), gvals, null, nr, nc); +// case (ginds:GIMat, fvals:LMat) => GLFunctions.accum(ginds, GLMat(fvals), null, nr, nc); +// case (finds:IMat, gvals:GLMat) => GLFunctions.accum(GIMat(finds), gvals, null, nr, nc); case _ => LMat(DenseMat.accum(inds, vals, nr, nc)) } } @@ -37,7 +37,7 @@ object LFunctions { /** Accumulate (row, col, value) tuples from inds \\ vals. nr and nc are row and column bounds */ def accum(inds:IMat, v:Long, nr:Int, nc:Int) = { inds match { - case ginds:GIMat => GLFunctions.accum(ginds, v, null, nr, nc); +// case ginds:GIMat => GLFunctions.accum(ginds, v, null, nr, nc); case _ => LMat(DenseMat.accum(inds, LMat.lelem(v), nr, nc)); } } @@ -50,58 +50,58 @@ object LFunctions { def min(a:LMat, b:LMat, out:Mat) = { (a, b) match { - case (aa:GLMat, bb:LMat) => GLFunctions.min(aa, GLMat(b), out); - case (aa:LMat, bb:GLMat) => GLFunctions.min(GLMat(a), bb, out); +// case (aa:GLMat, bb:LMat) => GLFunctions.min(aa, GLMat(b), out); +// case (aa:LMat, bb:GLMat) => GLFunctions.min(GLMat(a), bb, out); case _ => a.iiMatOpv(b, LMat.vecMinFun, op_min, out); } } def max(a:LMat, b:LMat, out:Mat) = { (a, b) match { - case (aa:GLMat, bb:LMat) => GLFunctions.max(aa, GLMat(b), out); - case (aa:LMat, bb:GLMat) => GLFunctions.max(GLMat(a), bb, out); +// case (aa:GLMat, bb:LMat) => GLFunctions.max(aa, GLMat(b), out); +// case (aa:LMat, bb:GLMat) => GLFunctions.max(GLMat(a), bb, out); case _ => a.iiMatOpv(b, LMat.vecMaxFun, op_max, out); } } def min(a:LMat, b:Int, out:Mat) = { a match { - case aa:GLMat=> GLFunctions.min(aa, GLMat.elem(b), out); +// case aa:GLMat=> GLFunctions.min(aa, GLMat.elem(b), out); case _ => a.iiMatOpScalarv(b, LMat.vecMinFun, out); } } def max(a:LMat, b:Int, out:Mat) = { a match { - case aa:GLMat=> GLFunctions.max(aa, GLMat.elem(b), out); +// case aa:GLMat=> GLFunctions.max(aa, GLMat.elem(b), out); case _ => a.iiMatOpScalarv(b, LMat.vecMaxFun, out); } } def maxi(a:LMat, n:Int, out:Mat) = { a match { - case aa:GLMat => GLFunctions.maxi(aa, n, out); +// case aa:GLMat => GLFunctions.maxi(aa, n, out); case _ => a.iiReduceOpv(n, LMat.idFun, LMat.vecMaxFun, out); } } def mini(a:LMat, n:Int, out:Mat) = { a match { - case aa:GLMat => GLFunctions.mini(aa, n, out); +// case aa:GLMat => GLFunctions.mini(aa, n, out); case _ => a.iiReduceOpv(n, LMat.idFun, LMat.vecMinFun, out); } } def sum(a:LMat, n:Int, out:Mat) = { a match { - case aa:GLMat => GLFunctions.sum(aa, n, out); +// case aa:GLMat => GLFunctions.sum(aa, n, out); case _ => a.iiReduceOpv(n, LMat.idFun, LMat.vecAddFun, out); } } def prod(a:LMat, n:Int, out:Mat) = { a match { - case aa:GLMat => GLFunctions.prod(aa, n, out); +// case aa:GLMat => GLFunctions.prod(aa, n, out); case _ => a.iiReduceOpv(n, LMat.idFun, LMat.vecMulFun, out); } } @@ -116,7 +116,7 @@ object LFunctions { def maxi2(a:LMat,d:Int):(LMat,IMat) = { a match { - case aa:GLMat => GLFunctions.maxi2(aa, null, null, d); +// case aa:GLMat => GLFunctions.maxi2(aa, null, null, d); case _ => { val (m,ii)=a.ggOpt2(d,LMat.gtPred); (LMat(m), ii) @@ -126,7 +126,7 @@ object LFunctions { def mini2(a:LMat,d:Int):(LMat,IMat) = { a match { - case aa:GLMat => GLFunctions.mini2(aa, null, null, d); +// case aa:GLMat => GLFunctions.mini2(aa, null, null, d); case _ => { val (m,ii)=a.ggOpt2(d,LMat.ltPred); (LMat(m), ii) diff --git a/src/main/scala/BIDMat/LMat.scala b/src/main/scala/BIDMat/LMat.scala index ac433ebf..599659b3 100755 --- a/src/main/scala/BIDMat/LMat.scala +++ b/src/main/scala/BIDMat/LMat.scala @@ -538,8 +538,8 @@ case class LMat(dims0:Array[Int], val data:Array[Long]) extends DenseMat[Long](d def iiMatOpv(b: Mat, f:(Array[Long],Int,Int,Array[Long],Int,Int,Array[Long],Int,Int,Int) => Long, optype:Int, out:Mat):LMat = (this, b) match { - case (aa:GLMat, bb:LMat) => aa.GIop(bb, out, optype); - case (aa:LMat, bb:GLMat) => GLMat(this).GIop(bb, out, optype); +// case (aa:GLMat, bb:LMat) => aa.GIop(bb, out, optype); +// case (aa:LMat, bb:GLMat) => GLMat(this).GIop(bb, out, optype); case (aa:LMat, bb:LMat) => LMat(ggMatOpv(bb, f, out)); case _ => throw new RuntimeException("unsupported operation "+f+" on "+this+" and "+b) } @@ -1124,7 +1124,8 @@ case class LMat(dims0:Array[Int], val data:Array[Long]) extends DenseMat[Long](d /* * Specialize to GMats to help the type system. - */ + */ + /* def * (b : GMat) = Mop_Times.op(this, b, null) def *^ (b : GMat) = Mop_TimesT.op(this, b, null) def xT (b : GMat) = Mop_TimesT.op(this, b, null) @@ -1156,7 +1157,7 @@ case class LMat(dims0:Array[Int], val data:Array[Long]) extends DenseMat[Long](d def >= (b : GMat) = Mop_GE.op(this, b, null) def <= (b : GMat) = Mop_LE.op(this, b, null) def != (b : GMat) = Mop_NE.op(this, b, null) - + */ /* * Operators whose second arg is generic. */ @@ -1361,6 +1362,7 @@ class LPair(val omat:Mat, val mat:LMat) extends BIDMat.Pair(omat, mat) { /* * Specialize to GMat */ + /* def * (b : GMat) = Mop_Times.op(mat, b, omat) def *^ (b : GMat) = Mop_TimesT.op(mat, b, omat) def xT (b : GMat) = Mop_TimesT.op(mat, b, omat) @@ -1388,7 +1390,7 @@ class LPair(val omat:Mat, val mat:LMat) extends BIDMat.Pair(omat, mat) { def >= (b : GMat) = Mop_GE.op(mat, b, omat) def <= (b : GMat) = Mop_LE.op(mat, b, omat) def != (b : GMat) = Mop_NE.op(mat, b, omat) - + */ /* * Generics */ @@ -1459,7 +1461,7 @@ object LMat { def apply(a:Long) = lelem(a) - def apply(a:GLMat) = a.toLMat +// def apply(a:GLMat) = a.toLMat def lzeros(m:Int, n:Int) = { val out = LMat(m,n) @@ -1487,14 +1489,14 @@ object LMat { def apply(x:Mat):LMat = { val out:LMat = x match { - case _:GIMat | _:GLMat | _:DMat | _:FMat | _:IMat | _:BMat => LMat.newOrCheckLMat(x.dims, null, x.GUID, "LMat".##); + case _:DMat | _:FMat | _:IMat | _:BMat => LMat.newOrCheckLMat(x.dims, null, x.GUID, "LMat".##); case ff:LMat => ff; case dd:DenseMat[Long] @ unchecked => {val out = new LMat(dd.dims.data, dd._data); out.setGUID(dd.GUID); out} case _ => throw new RuntimeException("IMat apply unknown argument"); } x match { - case gg:GIMat => gg.toLMat(out); - case gg:GLMat => gg.toLMat(out); +// case gg:GIMat => gg.toLMat(out); +// case gg:GLMat => gg.toLMat(out); case dd:DMat => {Mat.copyToLongArray(dd.data, 0, out.data, 0, dd.length)}; case ff:FMat => {Mat.copyToLongArray(ff.data, 0, out.data, 0, ff.length)}; case ff:BMat => {Mat.copyToLongArray(ff.data, 0, out.data, 0, ff.length)}; diff --git a/src/main/scala/BIDMat/Mat.scala b/src/main/scala/BIDMat/Mat.scala index 5d63b1ea..0579fe27 100755 --- a/src/main/scala/BIDMat/Mat.scala +++ b/src/main/scala/BIDMat/Mat.scala @@ -1,7 +1,6 @@ package BIDMat import scala.collection.mutable.HashMap; import java.lang.ref._; -import jcuda.NativePointerObject; import edu.berkeley.bid.UTILS; import java.util.logging.Level; import java.util.logging.Logger; @@ -413,15 +412,15 @@ class Mat(val _dims:Array[Int]) extends ND with Serializable { def ^* (b : DSPair):Mat = notImplemented0("^*") def Tx (b : DSPair):Mat = notImplemented0("Tx") def @@ (b : Mat):DSPair = (this, b) match { - case (aa:GMat, bb:GSMat) => new GDSPair(aa, bb) + // case (aa:GMat, bb:GSMat) => new GDSPair(aa, bb) case (aa:FMat, bb:SMat) => new FDSPair(aa, bb) } def ~ (b : Mat):Pair = b match { - case bb:GMat => new GPair(this, bb) - case bb:GIMat => new GIPair(this, bb) - case bb:GDMat => new GDPair(this, bb) - case bb:GLMat => new GLPair(this, bb) + // case bb:GMat => new GPair(this, bb) + // case bb:GIMat => new GIPair(this, bb) + // case bb:GDMat => new GDPair(this, bb) + // case bb:GLMat => new GLPair(this, bb) case bb:FMat => new FPair(this, bb) case bb:DMat => new DPair(this, bb) case bb:IMat => new IPair(this, bb) @@ -643,7 +642,7 @@ object Mat { var logger:Logger = consoleLogger; - final val SyncMethod = jcuda.runtime.JCuda.cudaStreamPerThread; +// final val SyncMethod = jcuda.runtime.JCuda.cudaStreamPerThread; // final val SyncMethod = jcuda.runtime.JCuda.cudaStreamLegacy; var termWidth = 80; @@ -673,11 +672,11 @@ object Mat { var recycleGrow = 1.2 // For caching, amount to grow re-allocated matrices - var hasCUDA = 0 // Number of available CUDA GPUs +// var hasCUDA = 0 // Number of available CUDA GPUs - var hasCUDNN = false; // Is CUDNN available? +// var hasCUDNN = false; // Is CUDNN available? - var cudartVersion = 0; +// var cudartVersion = 0; var useBLAS = true; @@ -867,12 +866,12 @@ object Mat { val keys = _cache2.keySet keys.foreach((key:Tuple2[Long,Int]) => { val toremove:Boolean = _cache2.get(key).get match { - case aa:GMat => (aa.myGPU == ithread) - case aa:GDMat => (aa.myGPU == ithread) - case aa:GIMat => (aa.myGPU == ithread) - case aa:GLMat => (aa.myGPU == ithread) - case aa:GSMat => (aa.myGPU == ithread) - case aa:GSDMat => (aa.myGPU == ithread) + // case aa:GMat => (aa.myGPU == ithread) + // case aa:GDMat => (aa.myGPU == ithread) +// case aa:GIMat => (aa.myGPU == ithread) +// case aa:GLMat => (aa.myGPU == ithread) +// case aa:GSMat => (aa.myGPU == ithread) +// case aa:GSDMat => (aa.myGPU == ithread) case _ => false } if (toremove) _cache2.remove(key) @@ -885,12 +884,12 @@ object Mat { val keys = _cache3.keySet keys.foreach((key:Tuple3[Long,Long,Int]) => { val toremove:Boolean = _cache3.get(key).get match { - case aa:GMat => (aa.myGPU == ithread) - case aa:GDMat => (aa.myGPU == ithread) - case aa:GIMat => (aa.myGPU == ithread) - case aa:GLMat => (aa.myGPU == ithread) - case aa:GSMat => (aa.myGPU == ithread) - case aa:GSDMat => (aa.myGPU == ithread) + // case aa:GMat => (aa.myGPU == ithread) + // case aa:GDMat => (aa.myGPU == ithread) + // case aa:GIMat => (aa.myGPU == ithread) + // case aa:GLMat => (aa.myGPU == ithread) + // case aa:GSMat => (aa.myGPU == ithread) + // case aa:GSDMat => (aa.myGPU == ithread) case _ => false } if (toremove) _cache3.remove(key) @@ -903,12 +902,12 @@ object Mat { val keys = _cache4.keySet keys.foreach((key:Tuple4[Long,Long,Long,Int]) => { val toremove:Boolean = _cache4.get(key).get match { - case aa:GMat => (aa.myGPU == ithread) - case aa:GDMat => (aa.myGPU == ithread) - case aa:GIMat => (aa.myGPU == ithread) - case aa:GLMat => (aa.myGPU == ithread) - case aa:GSMat => (aa.myGPU == ithread) - case aa:GSDMat => (aa.myGPU == ithread) + // case aa:GMat => (aa.myGPU == ithread) + // case aa:GDMat => (aa.myGPU == ithread) + // case aa:GIMat => (aa.myGPU == ithread) + // case aa:GLMat => (aa.myGPU == ithread) + // case aa:GSMat => (aa.myGPU == ithread) + // case aa:GSDMat => (aa.myGPU == ithread) case _ => false } if (toremove) _cache4.remove(key) @@ -921,12 +920,12 @@ object Mat { val keys = _cache5.keySet keys.foreach((key:Tuple5[Long,Long,Long,Long,Int]) => { val toremove:Boolean = _cache5.get(key).get match { - case aa:GMat => (aa.myGPU == ithread) - case aa:GDMat => (aa.myGPU == ithread) - case aa:GIMat => (aa.myGPU == ithread) - case aa:GLMat => (aa.myGPU == ithread) - case aa:GSMat => (aa.myGPU == ithread) - case aa:GSDMat => (aa.myGPU == ithread) + // case aa:GMat => (aa.myGPU == ithread) + // case aa:GDMat => (aa.myGPU == ithread) + // case aa:GIMat => (aa.myGPU == ithread) + // case aa:GLMat => (aa.myGPU == ithread) + // case aa:GSMat => (aa.myGPU == ithread) + // case aa:GSDMat => (aa.myGPU == ithread) case _ => false } if (toremove) _cache5.remove(key) @@ -939,12 +938,12 @@ object Mat { val keys = _cache6.keySet keys.foreach((key:Tuple6[Long,Long,Long,Long,Long,Int]) => { val toremove:Boolean = _cache6.get(key).get match { - case aa:GMat => (aa.myGPU == ithread) - case aa:GDMat => (aa.myGPU == ithread) - case aa:GIMat => (aa.myGPU == ithread) - case aa:GLMat => (aa.myGPU == ithread) - case aa:GSMat => (aa.myGPU == ithread) - case aa:GSDMat => (aa.myGPU == ithread) + // case aa:GMat => (aa.myGPU == ithread) + // case aa:GDMat => (aa.myGPU == ithread) + // case aa:GIMat => (aa.myGPU == ithread) + // case aa:GLMat => (aa.myGPU == ithread) + // case aa:GSMat => (aa.myGPU == ithread) + // case aa:GSDMat => (aa.myGPU == ithread) case _ => false } if (toremove) _cache6.remove(key) @@ -994,9 +993,9 @@ object Mat { } } - def checkCUDA:Unit = checkCUDA(false); +// def checkCUDA:Unit = checkCUDA(false); - def checkCUDA(verbose:Boolean):Unit = { +/* def checkCUDA(verbose:Boolean):Unit = { if (hasCUDA == 0) { val os = System.getProperty("os.name"); try { @@ -1079,7 +1078,7 @@ object Mat { GFunctions.initCUDNN(); } } - +*/ def checkOpenCL():Unit = checkOpenCL(false) def checkOpenCL(verbose: Boolean):Unit = { diff --git a/src/main/scala/BIDMat/MatFunctions.scala b/src/main/scala/BIDMat/MatFunctions.scala index 3b68ecf9..89bd74b0 100755 --- a/src/main/scala/BIDMat/MatFunctions.scala +++ b/src/main/scala/BIDMat/MatFunctions.scala @@ -139,14 +139,14 @@ object MatFunctions { /** Convert to the corresponding integral type */ def int(a:FMat):IMat = { a match { - case aa:GMat => GIMat(aa); +// case aa:GMat => GIMat(aa); case _ => IMat(a); } } def int(a:DMat):IMat = { a match { - case aa:GDMat => GIMat(aa); +// case aa:GDMat => GIMat(aa); case _ => IMat(a); } } @@ -155,18 +155,18 @@ object MatFunctions { a } - def int(a:GMat):GIMat = { - GIMat(a); - } +// def int(a:GMat):GIMat = { +// GIMat(a); +// } - def int(a:GIMat):GIMat = { - a; - } +// def int(a:GIMat):GIMat = { +// a; +// } def int(a:Mat):IMat = { a match { - case ga:GMat => GIMat(ga); - case gi:GIMat => gi; +// case ga:GMat => GIMat(ga); +// case gi:GIMat => gi; case fa:FMat => IMat(fa); case da:DMat => IMat(da); case ia:IMat => ia; @@ -211,35 +211,35 @@ object MatFunctions { /** Convert to the corresponding long type */ def long(a:FMat):LMat = { a match { - case aa:GMat => GLMat(aa); +// case aa:GMat => GLMat(aa); case _ => LMat(a); } } def long(a:DMat):LMat = { a match { - case aa:GDMat => GLMat(aa); +// case aa:GDMat => GLMat(aa); case _ => LMat(a); } } def long(a:IMat):LMat = { a match { - case aa:GIMat => GLMat(aa); +// case aa:GIMat => GLMat(aa); case _ => LMat(a); } } - def long(a:GMat):LMat = GLMat(a); +// def long(a:GMat):LMat = GLMat(a); - def long(a:GIMat):LMat = GLMat(a); +// def long(a:GIMat):LMat = GLMat(a); def long(a:Mat):LMat = { a match { - case aa:GMat => GLMat(aa); - case aa:GIMat => GLMat(aa); - case aa:GDMat => GLMat(aa); - case aa:GLMat => aa; +// case aa:GMat => GLMat(aa); +// case aa:GIMat => GLMat(aa); +// case aa:GDMat => GLMat(aa); +// case aa:GLMat => aa; case _ => LMat(a); } } @@ -247,40 +247,40 @@ object MatFunctions { /** Convert to the corresponding float type */ def float(a:IMat):FMat = { a match { - case aa:GIMat => GMat(aa); +// case aa:GIMat => GMat(aa); case _ => FMat(a); } } def float(a:DMat):FMat = { a match { - case aa:GDMat => GMat(aa); +// case aa:GDMat => GMat(aa); case _ => FMat(a); } } - def float(a:GDMat):GMat = { - GMat(a); - } +// def float(a:GDMat):GMat = { +// GMat(a); +// } def float(a:FMat):FMat = { a } - def float(a:GMat):GMat = { - a - } +// def float(a:GMat):GMat = { +// a +// } - def float(a:GIMat):GMat = { - GMat(a); - } +// def float(a:GIMat):GMat = { +// GMat(a); +// } def float(a:Mat):FMat = { a match { - case ga:GMat => ga; - case gi:GIMat => GMat(gi); - case gg:GDMat => GMat(gg); - case gg:GLMat => GMat(gg); +// case ga:GMat => ga; +// case gi:GIMat => GMat(gi); +// case gg:GDMat => GMat(gg); +// case gg:GLMat => GMat(gg); case fa:FMat => fa; case _ => FMat(a); } @@ -289,12 +289,12 @@ object MatFunctions { /** Convert to a CPU matrix */ def cpu(a:Mat):Mat = { a match { - case b:GMat => FMat(b); - case b:GDMat => DMat(b); - case b:GIMat => IMat(b); - case b:GLMat => LMat(b); - case b:GSMat => SMat(b); - case b:GSDMat => SDMat(b); +// case b:GMat => FMat(b); +// case b:GDMat => DMat(b); +// case b:GIMat => IMat(b); +// case b:GLMat => LMat(b); +// case b:GSMat => SMat(b); +// case b:GSDMat => SDMat(b); case b:FMat => b; case b:DMat => b; case b:IMat => b; @@ -309,24 +309,24 @@ object MatFunctions { } /** Convert to a GPU matrix */ - def gpu(a:Mat):Mat = { - a match { - case b:GMat => b; - case b:GDMat => b; - case b:GIMat => b; - case b:GLMat => b; - case b:GSMat => b; - case b:GSDMat => b; - case b:FMat => GMat(b); - case b:DMat => GDMat(b); - case b:IMat => GIMat(b); - case b:LMat => GLMat(b); - case b:SMat => GSMat(b); - case b:SDMat => GSDMat(b); - - case b:TMat => b.toGPU; - } - } +// def gpu(a:Mat):Mat = { +// a match { +// case b:GMat => b; +// case b:GDMat => b; +// case b:GIMat => b; +// case b:GLMat => b; +// case b:GSMat => b; +// case b:GSDMat => b; +// case b:FMat => GMat(b); +// case b:DMat => GDMat(b); +// case b:IMat => GIMat(b); +// case b:LMat => GLMat(b); +// case b:SMat => GSMat(b); +// case b:SDMat => GSDMat(b); + +// case b:TMat => b.toGPU; +// } +// } // TODO Document def threadPool(n:Int = Mat.numThreads):scala.concurrent.ExecutionContextExecutor = { @@ -345,11 +345,11 @@ object MatFunctions { def recycleTry(a:Mat, nr:Int, nc:Int, b:SDMat, nnz:Int):SDMat = recycleTry(a, nr, nc, b:Mat, nnz).asInstanceOf[SDMat] - def recycleTry(a:Mat, nr:Int, nc:Int, b:GMat, nnz:Int):GMat = recycleTry(a, nr, nc, b:Mat, nnz).asInstanceOf[GMat] +// def recycleTry(a:Mat, nr:Int, nc:Int, b:GMat, nnz:Int):GMat = recycleTry(a, nr, nc, b:Mat, nnz).asInstanceOf[GMat] - def recycleTry(a:Mat, nr:Int, nc:Int, b:GIMat, nnz:Int):GIMat = recycleTry(a, nr, nc, b:Mat, nnz).asInstanceOf[GIMat] +// def recycleTry(a:Mat, nr:Int, nc:Int, b:GIMat, nnz:Int):GIMat = recycleTry(a, nr, nc, b:Mat, nnz).asInstanceOf[GIMat] - def recycleTry(a:Mat, nr:Int, nc:Int, b:GSMat, nnz:Int):GSMat = recycleTry(a, nr, nc, b:Mat, nnz).asInstanceOf[GSMat] +// def recycleTry(a:Mat, nr:Int, nc:Int, b:GSMat, nnz:Int):GSMat = recycleTry(a, nr, nc, b:Mat, nnz).asInstanceOf[GSMat] // TODO Document all of these recycleTry methods. def recycleTry(a:Mat, nr:Int, nc:Int, b:Mat, nnz:Int):Mat = { @@ -372,11 +372,11 @@ object MatFunctions { def recycleTry(a:Mat, b:SDMat):SDMat = recycleTry(a, b.nrows, b.ncols, b:Mat, b.nnz).asInstanceOf[SDMat] - def recycleTry(a:Mat, b:GMat):GMat = recycleTry(a, b.nrows, b.ncols, b:Mat, b.nnz).asInstanceOf[GMat] +// def recycleTry(a:Mat, b:GMat):GMat = recycleTry(a, b.nrows, b.ncols, b:Mat, b.nnz).asInstanceOf[GMat] - def recycleTry(a:Mat, b:GIMat):GIMat = recycleTry(a, b.nrows, b.ncols, b:Mat, b.nnz).asInstanceOf[GIMat] +// def recycleTry(a:Mat, b:GIMat):GIMat = recycleTry(a, b.nrows, b.ncols, b:Mat, b.nnz).asInstanceOf[GIMat] - def recycleTry(a:Mat, b:GSMat):GSMat = recycleTry(a, b.nrows, b.ncols, b:Mat, b.nnz).asInstanceOf[GSMat] +// def recycleTry(a:Mat, b:GSMat):GSMat = recycleTry(a, b.nrows, b.ncols, b:Mat, b.nnz).asInstanceOf[GSMat] def recycleTry(a:Mat, b:Mat):Mat = recycleTry(a, b.nrows, b.ncols, b, b.nnz) @@ -395,14 +395,14 @@ object MatFunctions { def recycleTry(a:Mat, b:SDMat, c:SDMat):SDMat = recycleTry(a, math.max(b.nrows, c.nrows), math.max(b.ncols, c.ncols), b:Mat, b.nnz).asInstanceOf[SDMat]; - def recycleTry(a:Mat, b:GMat, c:GMat):GMat = - recycleTry(a, math.max(b.nrows, c.nrows), math.max(b.ncols, c.ncols), b:Mat, b.nnz).asInstanceOf[GMat]; +// def recycleTry(a:Mat, b:GMat, c:GMat):GMat = +// recycleTry(a, math.max(b.nrows, c.nrows), math.max(b.ncols, c.ncols), b:Mat, b.nnz).asInstanceOf[GMat]; - def recycleTry(a:Mat, b:GIMat, c:GIMat):GIMat = - recycleTry(a, math.max(b.nrows, c.nrows), math.max(b.ncols, c.ncols), b:Mat, b.nnz).asInstanceOf[GIMat]; +// def recycleTry(a:Mat, b:GIMat, c:GIMat):GIMat = +// recycleTry(a, math.max(b.nrows, c.nrows), math.max(b.ncols, c.ncols), b:Mat, b.nnz).asInstanceOf[GIMat]; - def recycleTry(a:Mat, b:GSMat, c:GSMat):GSMat = - recycleTry(a, math.max(b.nrows, c.nrows), math.max(b.ncols, c.ncols), b:Mat, b.nnz).asInstanceOf[GSMat]; +// def recycleTry(a:Mat, b:GSMat, c:GSMat):GSMat = +// recycleTry(a, math.max(b.nrows, c.nrows), math.max(b.ncols, c.ncols), b:Mat, b.nnz).asInstanceOf[GSMat]; def recycleTry(a:Mat, b:Mat, c:Mat):Mat = recycleTry(a, math.max(b.nrows, c.nrows), math.max(b.ncols, c.ncols), b, b.nnz) @@ -737,17 +737,17 @@ object MatFunctions { /** Find non-zero (row, col, value) tuples */ def find3(a:SMat) = a.find3 - def sort(keys:GMat):GMat = GFunctions.sort(keys) +// def sort(keys:GMat):GMat = GFunctions.sort(keys) - def sort2(keys:GMat):(GMat, GIMat) = GFunctions.sort2(keys) +// def sort2(keys:GMat):(GMat, GIMat) = GFunctions.sort2(keys) - def sortdown(keys:GMat):GMat = GFunctions.sortdown(keys) +// def sortdown(keys:GMat):GMat = GFunctions.sortdown(keys) - def sortdown2(keys:GMat):(GMat, GIMat) = GFunctions.sortdown2(keys) +// def sortdown2(keys:GMat):(GMat, GIMat) = GFunctions.sortdown2(keys) def sort(keys:Mat):Mat = { keys match { - case a:GMat => sort(a); +// case a:GMat => sort(a); case a:FMat => sort(a); case a:IMat => sort(a); case a:DMat => sort(a); @@ -757,7 +757,7 @@ object MatFunctions { def sortdown(keys:Mat):Mat = { keys match { - case a:GMat => sortdown(a); +// case a:GMat => sortdown(a); case a:FMat => sortdown(a); case a:IMat => sortdown(a); case a:DMat => sortdown(a); @@ -767,7 +767,7 @@ object MatFunctions { def sort2(keys:Mat):(Mat, IMat) = { keys match { - case a:GMat => sort2(a); +// case a:GMat => sort2(a); case a:FMat => sort2(a); case a:IMat => sort2(a); case a:DMat => sort2(a); @@ -777,7 +777,7 @@ object MatFunctions { def sortdown2(keys:Mat):(Mat, IMat) = { keys match { - case a:GMat => sortdown2(a); +// case a:GMat => sortdown2(a); case a:FMat => sortdown2(a); case a:IMat => sortdown2(a); case a:DMat => sortdown2(a); @@ -789,10 +789,10 @@ object MatFunctions { /** Accumulate (row, col, value) tuples from inds \\ vals (generic version) into omat. nr and nc are row and column bounds */ def accum(inds:Mat, vals:Mat, omat:Mat, nrows:Int, ncols:Int):Mat = { (inds, vals) match { - case (ginds:GIMat, gvals:GMat) => GFunctions.accum(ginds, gvals, omat, nrows, ncols):GMat - case (ginds:GIMat, gvals:GDMat) => GDFunctions.accum(ginds, gvals, omat, nrows, ncols):GDMat - case (ginds:GIMat, gvals:GIMat) => GIFunctions.accum(ginds, gvals, omat, nrows, ncols):GIMat - case (ginds:GIMat, gvals:GLMat) => GLFunctions.accum(ginds, gvals, omat, nrows, ncols):GLMat +// case (ginds:GIMat, gvals:GMat) => GFunctions.accum(ginds, gvals, omat, nrows, ncols):GMat +// case (ginds:GIMat, gvals:GDMat) => GDFunctions.accum(ginds, gvals, omat, nrows, ncols):GDMat +// case (ginds:GIMat, gvals:GIMat) => GIFunctions.accum(ginds, gvals, omat, nrows, ncols):GIMat +// case (ginds:GIMat, gvals:GLMat) => GLFunctions.accum(ginds, gvals, omat, nrows, ncols):GLMat case (iinds:IMat, fvals:FMat) => accum(iinds, fvals, nrows, ncols):FMat case (iinds:IMat, ivals:IMat) => accum(iinds, ivals, nrows, ncols):IMat case (iinds:IMat, ivals:LMat) => accum(iinds, ivals, nrows, ncols):LMat @@ -806,7 +806,7 @@ object MatFunctions { /** Accumulate (row, col, fval) tuples from inds (generic version) into omat. nr and nc are row and column bounds */ def accum(inds:Mat, fval:Float, omat:Mat, nrows:Int, ncols:Int):Mat = { inds match { - case ginds:GIMat => GFunctions.accum(ginds, fval, omat, nrows, ncols):GMat; +// case ginds:GIMat => GFunctions.accum(ginds, fval, omat, nrows, ncols):GMat; case iinds:IMat => accum(iinds, fval, nrows, ncols):FMat; } } @@ -817,7 +817,7 @@ object MatFunctions { /** Accumulate (row, col, ival) tuples from inds (generic version) into omat. nr and nc are row and column bounds */ def accum(inds:Mat, ival:Int, omat:Mat, nrows:Int, ncols:Int):Mat = { inds match { - case ginds:GIMat => GIFunctions.accum(ginds, ival, omat, nrows, ncols):GIMat; +// case ginds:GIMat => GIFunctions.accum(ginds, ival, omat, nrows, ncols):GIMat; case iinds:IMat => accum(iinds, ival, nrows, ncols):IMat; } } @@ -865,104 +865,42 @@ object MatFunctions { } out } - - /** - * Lexicographic sort of a matrix '''mat''' and a set of indices '''inds'''. - * Side-effects both matrices, i.e. both '''mat''' and '''inds''' are modified. + + /** + * Lexicographic sort of a matrix '''mat''' and a set of indices '''inds'''. + * Side-effects both matrices, i.e. both '''mat''' and '''inds''' are modified. */ - def sortlexInds(mat:IMat, inds:IMat) = _sortlexInds(mat, inds, true) - + + def sortlexInds(mat:IMat, inds:IMat) = _sortlexInds(mat, inds, true) + def _sortlexInds(mat:IMat, inds:IMat, asc:Boolean) { - if (if (Mat.useGPUsort && Mat.hasCUDA > 0) { - val (dmy, freebytes, allbytes) = SciFunctions.GPUmem - if ((mat.length+inds.length)*12L < freebytes) { - if (mat.ncols == 1) { - GIFunctions.isortlexIndsGPU(mat, inds, asc) - false - } else if (mat.ncols == 2) { - GIFunctions.i2sortlexIndsGPU(mat, inds, asc) - false - } else if (mat.ncols == 3) { - GIFunctions.i3sortlexIndsGPU(mat, inds, asc) - false - } else true - } else true - } else true) { - val perm = IMat.isortlex(mat, asc) - val indsp = inds(perm) - inds <-- indsp - val matp = mat(perm, ?) - mat <-- matp - } + val perm = IMat.isortlex(mat, asc) + val indsp = inds(perm) + inds <-- indsp + val matp = mat(perm, ?) + mat <-- matp } - - def sortlexInds(mat:LMat, inds:IMat) = _sortlexInds(mat, inds, true) - + + def sortlexInds(mat:LMat, inds:IMat) = _sortlexInds(mat, inds, true) + def _sortlexInds(mat:LMat, inds:IMat, asc:Boolean) { - if (if (Mat.useGPUsort && Mat.hasCUDA > 0) { - val (dmy, freebytes, allbytes) = SciFunctions.GPUmem - if ((mat.length+inds.length)*12L < freebytes) { - if (mat.ncols == 1) { - GLMat.isortlexIndsGPU(mat, inds, asc) - false - } else if (mat.ncols == 2) { - GLMat.i2sortlexIndsGPU(mat, inds, asc) - false - } else if (mat.ncols == 3) { - GLMat.i3sortlexIndsGPU(mat, inds, asc) - false - } else true - } else true - } else true) { - val perm = LMat.isortlex(mat, asc) - val indsp = inds(perm) - inds <-- indsp - val matp = mat(perm, ?) - mat <-- matp - } + val perm = LMat.isortlex(mat, asc) + val indsp = inds(perm) + inds <-- indsp + val matp = mat(perm, ?) + mat <-- matp } - - /** - * Lexicographic sort of a matrix '''mat'''. Side-effects '''mat'''. - */ + def sortlex(mat:IMat) = _sortlex(mat, true) - + def _sortlex(mat:IMat, asc:Boolean):Unit = { - if (if (Mat.useGPUsort && Mat.hasCUDA > 0) { - val (dmy, freebytes, allbytes) = SciFunctions.GPUmem - if ((mat.length)*12L < freebytes) { - if (mat.ncols == 2) { - GIFunctions.i2sortlexGPU(mat, asc) - false - } else true - } else true - } else true) { - val perm = IMat.isortlex(mat, asc) - val matp = mat(perm, ?) - mat <-- matp - } - } - - /** - * Lexicographic sort of a matrix '''mat''' with order '''asc''' (boolean true for ascending order). Side-effects '''mat'''. - */ - def isortlexfast(mat:IMat, asc:Boolean):IMat = { - if (Mat.useGPUsort && Mat.hasCUDA > 0 && { - val (dmy, freebytes, allbytes) = SciFunctions.GPUmem; - (mat.nrows*(mat.ncols+1)*12L < freebytes) - }) - { - val inds = icol(0->mat.nrows) - val tmat = mat.copy - if (mat.ncols == 2) { - GIFunctions.i2sortlexIndsGPU(tmat, inds, asc) - inds - } else if (mat.ncols == 3) { - GIFunctions.i3sortlexIndsGPU(tmat, inds, asc) - inds - } else IMat.isortlex(mat, asc) - } else IMat.isortlex(mat, asc) + val perm = IMat.isortlex(mat, asc) + val matp = mat(perm, ?) + mat <-- matp } + + + /** * Count distinct elements in a sorted array of rows. Returns (bptrs, iptrs), where bptrs points to a @@ -1429,39 +1367,39 @@ object MatFunctions { def sdzeros(nr:Int, nc:Int):SDMat = SDMat(nr, nc, 0) /** Make a single-precision matrix of zeros in GPU memory. */ - def gzeros(nr:Int, nc:Int):GMat = GMat.zeros(nr, nc) +// def gzeros(nr:Int, nc:Int):GMat = GMat.zeros(nr, nc) - def gzeros(dims:IMat):GMat = GMat.zeros(dims); +// def gzeros(dims:IMat):GMat = GMat.zeros(dims); /** Make a double-precision matrix of zeros in GPU memory. */ - def gdzeros(nr:Int, nc:Int):GDMat = GDMat.zeros(nr, nc); +// def gdzeros(nr:Int, nc:Int):GDMat = GDMat.zeros(nr, nc); - def gdzeros(dims:IMat):GDMat = GDMat.zeros(dims); +// def gdzeros(dims:IMat):GDMat = GDMat.zeros(dims); /** Make an integer matrix of zeros in GPU memory. */ - def gizeros(nr:Int, nc:Int):GIMat = GIMat.izeros(nr, nc); +// def gizeros(nr:Int, nc:Int):GIMat = GIMat.izeros(nr, nc); - def gizeros(dims:IMat):GIMat = GIMat.izeros(dims); +// def gizeros(dims:IMat):GIMat = GIMat.izeros(dims); /** Make a long matrix of zeros in GPU memory. */ - def glzeros(nr:Int, nc:Int):GLMat = GLMat.lzeros(nr, nc); +// def glzeros(nr:Int, nc:Int):GLMat = GLMat.lzeros(nr, nc); - def glzeros(dims:IMat):GLMat = GLMat.lzeros(dims); +// def glzeros(dims:IMat):GLMat = GLMat.lzeros(dims); /** Make a float matrix of ones in GPU memory. */ - def gones(nr:Int, nc:Int):GMat = GMat.ones(nr,nc); +// def gones(nr:Int, nc:Int):GMat = GMat.ones(nr,nc); - def gones(dims:IMat):GMat = GMat.ones(dims); +// def gones(dims:IMat):GMat = GMat.ones(dims); /** Make an integer matrix of ones in GPU memory. */ - def giones(nr:Int, nc:Int):GIMat = GIMat.iones(nr,nc); +// def giones(nr:Int, nc:Int):GIMat = GIMat.iones(nr,nc); - def giones(dims:IMat):GIMat = GIMat.iones(dims); +// def giones(dims:IMat):GIMat = GIMat.iones(dims); /** Make a long matrix of ones in GPU memory. */ - def glones(nr:Int, nc:Int):GLMat = GLMat.lones(nr,nc); +// def glones(nr:Int, nc:Int):GLMat = GLMat.lones(nr,nc); - def glones(dims:IMat):GLMat = GLMat.lones(dims); +// def glones(dims:IMat):GLMat = GLMat.lones(dims); /** Legacy function to make a placeholder matrix with no storage. */ def blank = new Mat(0,0) @@ -1485,13 +1423,13 @@ object MatFunctions { def sdblank = new SDMat(0,0,0,null,null,null) /** Legacy function to make a placeholder matrix with no storage. */ - def gblank = new GMat(0,0,null,0) +// def gblank = new GMat(0,0,null,0) /** Legacy function to make a placeholder matrix with no storage. */ - def giblank = new GIMat(0,0,null,0) +// def giblank = new GIMat(0,0,null,0) /** Legacy function to make a placeholder matrix with no storage. */ - def gsblank = new GSMat(0,0,0,null,null,null,null,0) +// def gsblank = new GSMat(0,0,0,null,null,null,null,0) /** Convert a dense double-precision sparse matrix to sparse. */ def sparse(a:DMat):SDMat = { @@ -1573,16 +1511,16 @@ object MatFunctions { def full(ss:SMat):FMat = FMat(ss.full) /** Convert from GPU float sparse to GPU float dense. */ - def full(ss:GSMat):GMat = ss.full +// def full(ss:GSMat):GMat = ss.full /** Convert from GPU double sparse to GPU double dense. */ - def full(ss:GSDMat):GDMat = ss.full +// def full(ss:GSDMat):GDMat = ss.full def full(a:Mat):Mat = a match { - case aa:GSMat => aa.full:GMat - case aa:GSDMat => aa.full:GDMat - case aa:GMat => a - case aa:GDMat => a +// case aa:GSMat => aa.full:GMat +// case aa:GSDMat => aa.full:GDMat +// case aa:GMat => a +// case aa:GDMat => a case aa:DMat => a case aa:FMat => a case aa:IMat => a @@ -1619,7 +1557,7 @@ object MatFunctions { def DDS(a:FMat,b:FMat,c:SMat,omat:Mat):SMat = { (a,b,c) match { - case (aa:GMat, bb:GMat, cc:GSMat) => GSMat.DDS(aa,bb,cc,omat); +// case (aa:GMat, bb:GMat, cc:GSMat) => GSMat.DDS(aa,bb,cc,omat); case _ => { if (a.nrows != b.nrows) { throw new RuntimeException("nrows of dense A and B must match") @@ -1653,21 +1591,21 @@ object MatFunctions { def DDS(a:FMat,b:FMat,c:SMat):SMat = DDS(a, b, c, null) - def DDS(a:GMat,b:GMat,c:GSMat,omat:Mat):GSMat = GSMat.DDS(a,b,c,omat) +// def DDS(a:GMat,b:GMat,c:GSMat,omat:Mat):GSMat = GSMat.DDS(a,b,c,omat) - def DDS(a:GMat,b:GMat,c:GSMat):GSMat = GSMat.DDS(a, b, c, null) +// def DDS(a:GMat,b:GMat,c:GSMat):GSMat = GSMat.DDS(a, b, c, null) def DDS(a:FMat, b:FMat, c:FMat):FMat = a.t * b - def DDS(a:GMat, b:GMat, c:GMat):GMat = a.t * b +// def DDS(a:GMat, b:GMat, c:GMat):GMat = a.t * b - def DDS(a:GDMat, b:GDMat, c:GDMat):GDMat = a.t * b +// def DDS(a:GDMat, b:GDMat, c:GDMat):GDMat = a.t * b def DDS(a:Mat, b:Mat, c:Mat, omat:Mat=null):Mat = { (a, b, c) match { - case (a:GMat, b:GMat, c:GSMat) => GSMat.DDS(a, b, c, omat):GSMat - case (a:GDMat, b:GDMat, c:GSDMat) => GSDMat.DDS(a, b, c, omat):GSDMat - case (a:GMat, b:GMat, c:GMat) => a.t * b +// case (a:GMat, b:GMat, c:GSMat) => GSMat.DDS(a, b, c, omat):GSMat +// case (a:GDMat, b:GDMat, c:GSDMat) => GSDMat.DDS(a, b, c, omat):GSDMat +// case (a:GMat, b:GMat, c:GMat) => a.t * b case (a:FMat, b:FMat, c:SMat) => DDS(a, b, c, omat):SMat case (a:FMat, b:FMat, c:FMat) => a.t * b } @@ -1727,7 +1665,7 @@ object MatFunctions { def oneHot(c:IMat, ncats:Int):SMat = { c match { - case cc:GIMat => GSMat.oneHot(cc, ncats); +// case cc:GIMat => GSMat.oneHot(cc, ncats); case _ => cat2sparse(c, ncats); } } @@ -1736,7 +1674,7 @@ object MatFunctions { def oneHot(c:Mat, ncats:Int):Mat = { c match { - case cc:GIMat => oneHot(cc, ncats); +// case cc:GIMat => oneHot(cc, ncats); case cc:IMat => oneHot(cc, ncats); } } @@ -1765,12 +1703,12 @@ object MatFunctions { def nHot(c:IMat):SMat = nHot(c, 0); - def nHot(c:GIMat, ncats:Int):GSMat = GSMat.nHot(c, ncats); - def nHot(c:GIMat):GSMat = GSMat.nHot(c, 0); +// def nHot(c:GIMat, ncats:Int):GSMat = GSMat.nHot(c, ncats); +// def nHot(c:GIMat):GSMat = GSMat.nHot(c, 0); def nHot(c:Mat, ncats:Int):Mat = { c match { - case cc:GIMat => nHot(cc, ncats); +// case cc:GIMat => nHot(cc, ncats); case cc:IMat => nHot(cc, ncats); } } @@ -1816,13 +1754,13 @@ object MatFunctions { /** Creates a diagonal, square CMat matrix with elements of '''a''' in the diagonal. */ def mkdiag(a:CMat) = CMat(a.mkdiag) /** Creates a diagonal, square GMat matrix with elements of '''a''' in the diagonal. */ - def mkdiag(a:GMat) = GMat(a.mkdiag) +// def mkdiag(a:GMat) = GMat(a.mkdiag) /** Creates a diagonal, square GDMat matrix with elements of '''a''' in the diagonal. */ - def mkdiag(a:GDMat) = GDMat(a.mkdiag) +// def mkdiag(a:GDMat) = GDMat(a.mkdiag) /** Creates a diagonal, square GIMat matrix with elements of '''a''' in the diagonal. */ - def mkdiag(a:GIMat) = GIMat(a.mkdiag) +// def mkdiag(a:GIMat) = GIMat(a.mkdiag) /** Creates a diagonal, square GLMat matrix with elements of '''a''' in the diagonal. */ - def mkdiag(a:GLMat) = GLMat(a.mkdiag) +// def mkdiag(a:GLMat) = GLMat(a.mkdiag) /** * Creates a diagonal, square matrix with elements of '''a''' in the diagonal. Works on most matrix types. @@ -1846,10 +1784,10 @@ object MatFunctions { */ def mkdiag(a:Mat):Mat = { a match { - case aa:GMat => mkdiag(aa):GMat; - case aa:GDMat => mkdiag(aa):GDMat; - case aa:GIMat => mkdiag(aa):GIMat; - case aa:GLMat => mkdiag(aa):GLMat; +// case aa:GMat => mkdiag(aa):GMat; +// case aa:GDMat => mkdiag(aa):GDMat; +// case aa:GIMat => mkdiag(aa):GIMat; +// case aa:GLMat => mkdiag(aa):GLMat; case aa:DMat => mkdiag(aa):DMat; case aa:FMat => mkdiag(aa):FMat; case aa:IMat => mkdiag(aa):IMat; @@ -1869,13 +1807,13 @@ object MatFunctions { /** Gets the leading diagonal of CMat '''a''' as a CMat vector. */ def getdiag(a:CMat) = CMat(a.getdiag) /** Gets the leading diagonal of GMat '''a''' as a GMat vector. */ - def getdiag(a:GMat) = GMat(a.getdiag) +// def getdiag(a:GMat) = GMat(a.getdiag) /** Gets the leading diagonal of GDMat '''a''' as a GDMat vector. */ - def getdiag(a:GDMat) = GDMat(a.getdiag) +// def getdiag(a:GDMat) = GDMat(a.getdiag) /** Gets the leading diagonal of GIMat '''a''' as a GIMat vector. */ - def getdiag(a:GIMat) = GIMat(a.getdiag) +// def getdiag(a:GIMat) = GIMat(a.getdiag) /** Gets the leading diagonal of GLMat '''a''' as a GLMat vector. */ - def getdiag(a:GLMat) = GLMat(a.getdiag) +// def getdiag(a:GLMat) = GLMat(a.getdiag) /** * Gets the leading diagonal of '''a''' matrix as a vector. Works on most matrix types. @@ -1897,10 +1835,10 @@ object MatFunctions { */ def getdiag(a:Mat):Mat = { a match { - case aa:GMat => getdiag(aa):GMat; - case aa:GDMat => getdiag(aa):GDMat; - case aa:GIMat => getdiag(aa):GIMat; - case aa:GLMat => getdiag(aa):GLMat; +// case aa:GMat => getdiag(aa):GMat; +// case aa:GDMat => getdiag(aa):GDMat; +// case aa:GIMat => getdiag(aa):GIMat; +// case aa:GLMat => getdiag(aa):GLMat; case aa:DMat => getdiag(aa):DMat; case aa:FMat => getdiag(aa):FMat; case aa:IMat => getdiag(aa):IMat; @@ -1942,17 +1880,17 @@ object MatFunctions { */ def kron(a:Mat, b:Mat, omat:Mat) : Mat = { (a, b) match { - case (a:GMat,b:GMat) => a.kron(b, omat) - case (a:GMat,b:GIMat) => a.kron(GMat(b), omat) - case (a:GIMat,b:GMat) => GMat(a).kron(b, omat) - case (a:GIMat,b:GIMat) => a.kron(b, omat) - case (a:GIMat,b:GSMat) => GMat(a).kron(full(b), omat) - case (a:GMat,b:GSMat) => a.kron(full(b), omat) +// case (a:GMat,b:GMat) => a.kron(b, omat) +// case (a:GMat,b:GIMat) => a.kron(GMat(b), omat) +// case (a:GIMat,b:GMat) => GMat(a).kron(b, omat) +// case (a:GIMat,b:GIMat) => a.kron(b, omat) +// case (a:GIMat,b:GSMat) => GMat(a).kron(full(b), omat) +// case (a:GMat,b:GSMat) => a.kron(full(b), omat) - case (a:FMat,b:GMat) => GMat(a).kron(b, omat) - case (a:FMat,b:GIMat) => GMat(a).kron(GMat(b), omat) - case (a:GMat,b:IMat) => a.kron(GMat(b), omat) - case (a:GIMat,b:IMat) => a.kron(GIMat(b), omat) +// case (a:FMat,b:GMat) => GMat(a).kron(b, omat) +// case (a:FMat,b:GIMat) => GMat(a).kron(GMat(b), omat) +// case (a:GMat,b:IMat) => a.kron(GMat(b), omat) +// case (a:GIMat,b:IMat) => a.kron(GIMat(b), omat) case (a:FMat,b:FMat) => a.kron(b, omat) case (a:FMat,b:SMat) => a.kron(full(b), omat) @@ -2075,7 +2013,7 @@ object MatFunctions { } } - def checkCUDAerrors() = { +/* def checkCUDAerrors() = { var err = 0 jcuda.runtime.JCuda.cudaDeviceSynchronize if (err == 0) err = jcuda.runtime.JCuda.cudaGetLastError @@ -2083,7 +2021,7 @@ object MatFunctions { val g = SciFunctions.getGPU throw new RuntimeException("GPU "+g+": Cuda error: " + jcuda.runtime.JCuda.cudaGetErrorString(err)) } - } + }*/ def cols2sparse(rows:IMat, cols:IMat, values:FMat, issorted:Boolean):SMat = cols2sparse(rows, cols, values, issorted, 0) @@ -2091,20 +2029,20 @@ object MatFunctions { def union(dd:Dict*) = Dict.union(dd:_*); - def h5list(fname:String) = MatHDF5.h5list(fname) +// def h5list(fname:String) = MatHDF5.h5list(fname) - def load[T](fname:String, vname:String):T = MatHDF5.hload(fname, vname).asInstanceOf[T] +// def load[T](fname:String, vname:String):T = MatHDF5.hload(fname, vname).asInstanceOf[T] - def load[A,B](fname:String, v1:String, v2:String):(A,B) = { - val a = MatHDF5.hload(fname, List(v1, v2)); - (a(0).asInstanceOf[A], a(1).asInstanceOf[B]) - } +// def load[A,B](fname:String, v1:String, v2:String):(A,B) = { +// val a = MatHDF5.hload(fname, List(v1, v2)); +// (a(0).asInstanceOf[A], a(1).asInstanceOf[B]) +// } - def loadx(fname:String, vnames:String*):List[AnyRef] = MatHDF5.hload(fname, vnames.toList) +// def loadx(fname:String, vnames:String*):List[AnyRef] = MatHDF5.hload(fname, vnames.toList) - def saveAsHDF5(fname:String, args:AnyRef*) = MatHDF5.hsaveAsHDF5(fname, args.toList) +// def saveAsHDF5(fname:String, args:AnyRef*) = MatHDF5.hsaveAsHDF5(fname, args.toList) - def saveAs(fname:String, args:AnyRef*) = MatHDF5.hsaveAs(fname, args.toList) +// def saveAs(fname:String, args:AnyRef*) = MatHDF5.hsaveAs(fname, args.toList) def loadMat(fname:String) = HMat.loadMat(fname, null, 0); def loadMat(fname:String, omat:Mat) = HMat.loadMat(fname, omat, 0); diff --git a/src/main/scala/BIDMat/MatHDF5.scala b/src/main/scala/BIDMat/MatHDF5.scala index af40f661..ece927ba 100755 --- a/src/main/scala/BIDMat/MatHDF5.scala +++ b/src/main/scala/BIDMat/MatHDF5.scala @@ -1,597 +1,7 @@ package BIDMat -import hdf.hdf5lib.structs._ -import hdf.hdf5lib.H5._ -import hdf.hdf5lib.HDF5Constants._ import scala.reflect._ object MatHDF5 { - - var refcount:Long = -1; - - def setCompressionPlist(dplist_id:Int, dims:Array[Long]) = { - if (Mat.compressType > 0) { - if (dims.length == 1) { - if (dims(0) > 1024) { - val cdims = new Array[Long](1); - cdims(0) = math.max(1, math.min(dims(0), Mat.chunkSize)); - H5Pset_chunk(dplist_id, 1, cdims); - if (Mat.compressType == 1) { - H5Pset_deflate(dplist_id, Mat.compressionLevel) - } else { - H5Pset_szip(dplist_id, H5_SZIP_EC_OPTION_MASK, Mat.szipBlock); - } - } - } else { - if (dims(0)*dims(1) > 1024) { - val cdims = new Array[Long](2); - cdims(0) = math.max(1, math.min(dims(0), 1+Mat.chunkSize/dims(1))); - cdims(1) = math.max(1, math.min(Mat.chunkSize, dims(1))); - H5Pset_chunk(dplist_id, 2, cdims); - if (Mat.compressType == 1) { - H5Pset_deflate(dplist_id, Mat.compressionLevel); - } else { - H5Pset_szip(dplist_id, H5_SZIP_EC_OPTION_MASK, Mat.szipBlock); - } - } - } - } - } - - def getStringAttr(id:Int, obj_name:String, attr_name:String):String = { - val attr_id = H5Aopen_by_name(id, obj_name, attr_name, H5P_DEFAULT, H5P_DEFAULT); - val attr_type_id = H5Aget_type(attr_id); - val attr_type_size = H5Tget_size(attr_type_id); - val sbuf = new Array[Byte](attr_type_size + 1); - H5Aread(attr_id, attr_type_id, sbuf); - H5Tclose(attr_type_id); - H5Aclose(attr_id); - new String(sbuf).trim(); - } - - def putStringAttr(id:Int, attr_name:String, attr_val:String) = { - val space_id = H5Screate(H5S_SCALAR); - val memtype_id = H5Tcopy(H5T_FORTRAN_S1); - H5Tset_size(memtype_id, attr_val.length()); - val attr_id = H5Acreate(id, attr_name, memtype_id, space_id, H5P_DEFAULT, H5P_DEFAULT); - H5Awrite(attr_id, memtype_id, attr_val.getBytes()); - H5Tclose(memtype_id); - H5Aclose(attr_id); - H5Sclose(space_id); - } - - def getLongAttr(id:Int, obj_name:String, attr_name:String):Long = { - val attr_id = H5Aopen_by_name(id, obj_name, attr_name, H5P_DEFAULT, H5P_DEFAULT); - val attr_type_id = H5Aget_type(attr_id); - val attr_type_size = H5Tget_size(attr_type_id); - val sbuf = new Array[Long](attr_type_size/8); - H5Aread(attr_id, attr_type_id, sbuf); - H5Tclose(attr_type_id); - H5Aclose(attr_id); - sbuf(0) - } - - def putIntAttr(id:Int, attr_name:String, attr_val:Int) = { - val space_id = H5Screate(H5S_SCALAR); - val attr_id = H5Acreate(id, attr_name, H5T_NATIVE_INT, space_id, H5P_DEFAULT, H5P_DEFAULT); - val lbuf = Array[Int](1); - lbuf(0) = attr_val; - H5Awrite(attr_id, H5T_NATIVE_INT, lbuf); - H5Aclose(attr_id); - H5Sclose(space_id); - } - - def putLongAttr(id:Int, attr_name:String, attr_val:Long) = { - val space_id = H5Screate(H5S_SCALAR); - val attr_id = H5Acreate(id, attr_name, H5T_NATIVE_LLONG, space_id, H5P_DEFAULT, H5P_DEFAULT); - val lbuf = Array[Long](1); - lbuf(0) = attr_val; - H5Awrite(attr_id, H5T_NATIVE_LLONG, lbuf); - H5Aclose(attr_id); - H5Sclose(space_id) - } - - def putByteAttr(id:Int, attr_name:String, attr_val:Byte) = { - val space_id = H5Screate(H5S_SCALAR); - val attr_id = H5Acreate(id, attr_name, H5T_NATIVE_UCHAR, space_id, H5P_DEFAULT, H5P_DEFAULT); - val lbuf = Array[Byte](1); - lbuf(0) = attr_val; - H5Awrite(attr_id, H5T_NATIVE_UCHAR, lbuf); - H5Aclose(attr_id); - H5Sclose(space_id) - } - - def getMatDims(data_id:Int):Array[Long] = { - val space_id = H5Dget_space(data_id); - val ndims = H5Sget_simple_extent_ndims(space_id); - val dims = new Array[Long](ndims); - val ok = H5Sget_simple_extent_dims(space_id, dims, null); - H5Sclose(space_id); - dims; - } - - def readMatDims(fid:Int, varname:String):Array[Long] = { - val data_id = H5Dopen(fid, varname, H5P_DEFAULT); - val dims = getMatDims(data_id); - H5Dclose(data_id); - dims; - } - - def readMat2Dims(fname:String, varname:String):(Long, Long) = { - val fid = H5Fopen(fname,H5F_ACC_RDONLY,H5P_DEFAULT); - val dims = readMatDims(fid, varname); - H5Fclose(fid); - if (dims(1) == 0) { - (dims(0), dims(1)); - } else { - (dims(1), dims(0)); - } - } - - def getDenseMat[T : ClassTag](fid:Int, varname:String, h5class:Int, dsize:Int):DenseMat[T] = { - val data_id = H5Dopen(fid, varname, H5P_DEFAULT); - val data_type_id = H5Dget_type(data_id); - val data_class = H5Tget_class(data_type_id); - val data_size = H5Tget_size(data_type_id); - val dims = getMatDims(data_id); - var mdata:DenseMat[T] = null; - if (data_class == h5class && data_size == dsize) { - mdata = new DenseMat[T](dims(1).intValue, dims(0).intValue); - H5Dread(data_id, data_type_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, mdata._data); - } else { - throw new RuntimeException("Bad class or data size "+data_class+" "+data_size); - } - H5Tclose(data_type_id); - H5Dclose(data_id); - mdata; - } - - def getFND(fid:Int, varname:String, h5class:Int, dsize:Int):FMat = { - val data_id = H5Dopen(fid, varname, H5P_DEFAULT); - val data_type_id = H5Dget_type(data_id); - val data_class = H5Tget_class(data_type_id); - val data_size = H5Tget_size(data_type_id); - val dims = getMatDims(data_id); - val idims = new Array[Int](dims.length); - for (i <- 0 until dims.length) {idims(i) = dims(dims.length - i - 1).toInt} - var mdata:FMat = null; - if (data_class == h5class && data_size == dsize) { - mdata = FMat.make(idims); - H5Dread(data_id, data_type_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, mdata._data); - } else { - throw new RuntimeException("Bad class or data size "+data_class+" "+data_size); - } - H5Tclose(data_type_id); - H5Dclose(data_id); - mdata; - } - - def getCellMat(fid:Int, varname:String):CSMat = { - val data_id = H5Dopen(fid, varname, H5P_DEFAULT); - val data_type_id = H5Dget_type(data_id); - val data_class = H5Tget_class(data_type_id); - val data_size = H5Tget_size(data_type_id); - val dims = getMatDims(data_id); - var mdata:CSMat = null; - mdata = CSMat(dims(1).intValue, dims(0).intValue); - val bdata = new Array[Array[Byte]]((dims(0)*dims(1)).intValue); - for (i <- 0 until bdata.length) { - bdata(i) = new Array[Byte](data_size); - } - H5Dread(data_id, data_type_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, bdata); - val obj_type_out = new Array[Int](1); - obj_type_out(0) = 1; - for (i <- 0 until bdata.length) { - val item_id = H5Rdereference(fid, H5R_OBJECT, bdata(i)); - mdata.data(i) = getMat(item_id, ".").asInstanceOf[String]; - H5Oclose(item_id); - } - H5Tclose(data_type_id); - H5Dclose(data_id); - mdata - } - - def getMatString(fid:Int, varname:String):String = { - val data_id = H5Dopen(fid, varname, H5P_DEFAULT); - val data_type_id = H5Dget_type(data_id); - val data_class = H5Tget_class(data_type_id); - val data_size = H5Tget_size(data_type_id); - val dims = getMatDims(data_id); - val nrows = dims(0).intValue; - val ncols = dims(1).intValue; - val sbuf = new Array[Byte](data_size*nrows*ncols); - H5Dread(data_id, data_type_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, sbuf); - H5Tclose(data_type_id); - H5Dclose(data_id); - new String(sbuf, "UTF_16LE").trim(); - } - - def getSparseMat[T](fid:Int, varname:String)(implicit manifest:Manifest[T], numeric:Numeric[T]):SparseMat[T] = { - val nrows = getLongAttr(fid, varname, "MATLAB_sparse").intValue; - val jc_id = H5Dopen(fid, varname+"/jc", H5P_DEFAULT); - val ncols = getMatDims(jc_id)(0).intValue - 1; - val data_id = H5Dopen(fid, varname+"/data", H5P_DEFAULT); - val data_type_id = H5Dget_type(data_id); - val nnz = getMatDims(data_id)(0).intValue; - var ir_id = -1; - try { - ir_id = H5Dopen(fid, varname+"/ir", H5P_DEFAULT); - } catch { - case _:Throwable => {} - } - val sdata = if (ir_id >= 0) { - SparseMat(nrows, ncols, nnz) ; - } else { - SparseMat.noRows(nrows, ncols, nnz); - } - val convert_ints = H5Tcopy(H5T_NATIVE_INT); - H5Dread_int(jc_id, convert_ints, H5S_ALL, H5S_ALL, H5P_DEFAULT, sdata.jc); - addOne(sdata.jc); - H5Dclose(jc_id); - if (ir_id >= 0) { - H5Dread_int(ir_id, convert_ints, H5S_ALL, H5S_ALL, H5P_DEFAULT, sdata.ir); - addOne(sdata.ir); - H5Dclose(ir_id); - } - H5Tclose(convert_ints); - H5Dread(data_id, data_type_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, sdata._data); - H5Tclose(data_type_id); - H5Dclose(data_id); - sdata - } - - def getMat(fid:Int, varname:String):AnyRef = { - if (fid > 0) { - if (H5Aexists_by_name(fid, varname, "MATLAB_class", H5P_DEFAULT)) { - val attr_class = getStringAttr(fid, varname, "MATLAB_class"); - if (attr_class.equals("double")) { - if (H5Aexists_by_name(fid, varname, "MATLAB_sparse", H5P_DEFAULT)) { - SDMat(getSparseMat[Double](fid, varname)); - } else { - DMat(getDenseMat[Double](fid, varname, H5T_FLOAT, 8)); - } - } else if (attr_class.equals("single")) { - if (H5Aexists_by_name(fid, varname, "MATLAB_sparse", H5P_DEFAULT)) { - SMat(getSparseMat[Float](fid, varname)); - } else { - if (readMatDims(fid, varname).length <= 2) { - FMat(getDenseMat[Float](fid, varname, H5T_FLOAT, 4)); - } else { - getFND(fid, varname, H5T_FLOAT, 4); - } - } - } else if (attr_class.equals("int32")) { - if (H5Aexists_by_name(fid, varname, "MATLAB_sparse", H5P_DEFAULT)) { - throw new RuntimeException("Sparse arrays of ints unsupported"); - } else { - IMat(getDenseMat[Int](fid, varname, H5T_INTEGER, 4)); - } - } else if (attr_class.equals("int8")) { - if (H5Aexists_by_name(fid, varname, "MATLAB_sparse", H5P_DEFAULT)) { - SBMat(getSparseMat[Byte](fid, varname)); - } else { - throw new RuntimeException("Dense arrays of bytes unsupported"); - } - } else if (attr_class.equals("char")) { - if (H5Aexists_by_name(fid, varname, "MATLAB_sparse", H5P_DEFAULT)) { - throw new RuntimeException("Sparse arrays of char unsupported"); - } else { - getMatString(fid, varname); - } - } else if (attr_class.equals("cell")) { - if (H5Aexists_by_name(fid, varname, "MATLAB_sparse", H5P_DEFAULT)) { - throw new RuntimeException("Sparse cell arrays unsupported"); - } else { - getCellMat(fid, varname); - } - } else throw new RuntimeException("Couldnt read MATLAB storage class "+attr_class); - } else { // Its a non-matlab HDF5 file - val did = H5Dopen(fid, varname, H5P_DEFAULT); - val tid = H5Dget_type(did); - val data_type = H5Tget_class(tid); - val data_size = H5Tget_size(tid); - H5Tclose(tid); - H5Dclose(did); - data_type match { - case H5T_FLOAT => - if (data_size == 4) { - if (readMatDims(fid, varname).length <= 2) { - FMat(getDenseMat[Float](fid, varname, H5T_FLOAT, 4)); - } else { - getFND(fid, varname, H5T_FLOAT, 4); - } - } else if (data_size == 8) { - DMat(getDenseMat[Double](fid, varname, H5T_FLOAT, 8)); - } else { - throw new RuntimeException("load: HDF5 %d-byte float type not supported" format data_size) - } - case H5T_INTEGER => - if (data_size == 4) { - IMat(getDenseMat[Int](fid, varname, H5T_INTEGER, 4)); - } else if (data_size == 8) { - LMat(getDenseMat[Long](fid, varname, H5T_INTEGER, 8)); - } else { - throw new RuntimeException("load: HDF5 %d-byte int type not supported" format data_size) - } - case H5T_STRING => getMatString(fid, varname); - } - } - } else { - throw new RuntimeException("getMat couldnt open file"); - } - - } - - def writeMatHeader(fname:String) = { - val ff = new java.io.RandomAccessFile(fname,"rws"); - val sp = new scala.sys.SystemProperties(); - val hstring = "MATLAB 7.3 MAT-file, Platform: "+sp.get("os.arch").get+" "+sp.get("os.name").get+" "+sp.get("os.version").get+ " "+ - "Created by BIDMat on "+(new java.text.SimpleDateFormat("EEE MMM d HH:mm:ss yyyy")).format(new java.util.Date())+ - " HDF5 Schema 1.0 ."; - val hb = hstring.getBytes(); - val hbytes = new Array[Byte](512); - for (i <- 0 until 116) hbytes(i) = 32; - System.arraycopy(hb, 0, hbytes, 0, math.min(hstring.length(), 116)); - val version:Byte = 2; - hbytes(125) = version; - hbytes(126) = 0x49; - hbytes(127) = 0x4D; - ff.write(hbytes); - // ff.write(emptyHDF5file) - ff.close(); - } - - def putDenseMat[T](fid:Int, a:DenseMat[T], aname:String, h5class:Int, matclass:String):Array[Byte] = { - val dims = new Array[Long](2); - dims(0) = a.ncols; - dims(1) = a.nrows; - val filespace_id = H5Screate_simple(2, dims, null); - val dplist_id = H5Pcreate(H5P_DATASET_CREATE); - setCompressionPlist(dplist_id, dims) - val dataset_id = H5Dcreate(fid, "/"+aname, h5class, filespace_id, H5P_DEFAULT, dplist_id, H5P_DEFAULT); - H5Dwrite(dataset_id, h5class, H5S_ALL, H5S_ALL, H5P_DEFAULT, a._data); - H5Pclose(dplist_id); - putStringAttr(dataset_id, "MATLAB_class", matclass); - val ref = H5Rcreate(dataset_id, ".", H5R_OBJECT, -1); - H5Dclose(dataset_id); - H5Sclose(filespace_id); - ref - } - - def putFND(fid:Int, a:FMat, aname:String, h5class:Int, matclass:String):Array[Byte] = { - val dims = a.dims.data.map(_.toLong); - val filespace_id = H5Screate_simple(dims.length, dims, null); - val dplist_id = H5Pcreate(H5P_DATASET_CREATE); - setCompressionPlist(dplist_id, dims); - val dataset_id = H5Dcreate(fid, "/"+aname, h5class, filespace_id, H5P_DEFAULT, dplist_id, H5P_DEFAULT); - H5Dwrite(dataset_id, h5class, H5S_ALL, H5S_ALL, H5P_DEFAULT, a.data); - H5Pclose(dplist_id); - putStringAttr(dataset_id, "MATLAB_class", matclass); - val ref = H5Rcreate(dataset_id, ".", H5R_OBJECT, -1); - H5Dclose(dataset_id); - H5Sclose(filespace_id); - ref - } - - def putEmptyRef(id:Int):Array[Byte] = { - val dims = new Array[Long](1); - dims(0) = 2; - val tmp = Array[Long](2); - val dmatspace_id = H5Screate_simple(1, dims, null); - val dmat_id = H5Dcreate(id, "0", H5T_NATIVE_ULLONG, dmatspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); - H5Dwrite(dmat_id, H5T_NATIVE_ULLONG, H5S_ALL, H5S_ALL, H5P_DEFAULT, tmp); - putStringAttr(dmat_id, "MATLAB_class", "canonical empty"); - putByteAttr(dmat_id, "MATLAB_empty", 1); - val ref = H5Rcreate(dmat_id, ".", H5R_OBJECT, -1); - H5Dclose(dmat_id); - H5Sclose(dmatspace_id); - ref - } - - def putCellMat(fid:Int, varname:String, a:CSMat) = { - var group_id = 0; - if (refcount < 0) { - group_id = H5Gcreate(fid, "/#refs#", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); - putEmptyRef(group_id); - refcount = 1; - } else { - group_id = H5Gopen(fid, "/#refs#", H5P_DEFAULT); - } - val refIds = new Array[Array[Byte]](a.length); - for (i <- 0 until a.length) { - val newname = "%x" format refcount; - refcount += 1; - refIds(i) = putMat(group_id, a.data(i), newname); - } - val dims = new Array[Long](2); - dims(0) = a.ncols; - dims(1) = a.nrows; - val dplist_id = H5Pcreate(H5P_DATASET_CREATE); - setCompressionPlist(dplist_id, dims); - val refspace_id = H5Screate_simple(2, dims, null); - val refs_id = H5Dcreate(fid, varname, H5T_STD_REF_OBJ, refspace_id, H5P_DEFAULT, dplist_id, H5P_DEFAULT); - H5Dwrite(refs_id, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, refIds); - putStringAttr(refs_id, "MATLAB_class", "cell"); - val ref = H5Rcreate(refs_id, ".", H5R_OBJECT, -1); - H5Dclose(refs_id); - H5Sclose(refspace_id); - H5Pclose(dplist_id); - H5Gclose(group_id); - ref - } - - def putSparseMat[T](fid:Int, a:SparseMat[T], varname:String, nativeClass:Int, className:String):Array[Byte] = { - val dims = new Array[Long](1); - val group_id = H5Gcreate(fid, "/"+varname, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); - putStringAttr(group_id, "MATLAB_class", className); - putLongAttr(group_id, "MATLAB_sparse", a.nrows); - val convert_ints = H5Tcopy(H5T_NATIVE_INT); - dims(0) = a.ncols + 1; - var dplist_id = H5Pcreate(H5P_DATASET_CREATE); - setCompressionPlist(dplist_id, dims); - val jcs_id = H5Screate_simple(1, dims, null); - val jc_id = H5Dcreate(group_id, "jc", H5T_NATIVE_LLONG, jcs_id, H5P_DEFAULT, dplist_id, H5P_DEFAULT); - subOne(a.jc); - try { - H5Dwrite(jc_id, convert_ints, H5S_ALL, H5S_ALL, H5P_DEFAULT, a.jc); - } catch { - case e:Throwable => { - addOne(a.jc); - throw new RuntimeException("Error writing sparse mat "+e); - } - } - addOne(a.jc); - H5Dclose(jc_id); - H5Sclose(jcs_id); - H5Pclose(dplist_id); - - dims(0) = a.nnz; - dplist_id = H5Pcreate(H5P_DATASET_CREATE); - setCompressionPlist(dplist_id, dims); - if (a.ir != null) { - val irs_id = H5Screate_simple(1, dims, null); - val ir_id = H5Dcreate(group_id, "ir", H5T_NATIVE_LLONG, irs_id, H5P_DEFAULT, dplist_id, H5P_DEFAULT); - subOne(a.ir); - try { - H5Dwrite(ir_id, convert_ints, H5S_ALL, H5S_ALL, H5P_DEFAULT, a.ir); - } catch { - case e:Throwable => { - addOne(a.ir); - throw new RuntimeException("Error writing sparse mat "+e); - } - } - addOne(a.ir); - H5Dclose(ir_id); - H5Sclose(irs_id); - } - - val dataspace_id = H5Screate_simple(1, dims, null); - val data_id = H5Dcreate(group_id, "data", nativeClass, dataspace_id, H5P_DEFAULT, dplist_id, H5P_DEFAULT); - H5Dwrite(data_id, nativeClass, H5S_ALL, H5S_ALL, H5P_DEFAULT, a._data); - H5Dclose(data_id); - H5Sclose(dataspace_id); - H5Pclose(dplist_id); - H5Tclose(convert_ints); - val ref = H5Rcreate(group_id, ".", H5R_OBJECT, -1); - H5Gclose(group_id); - ref - } - - def putMatString(id:Int, varname:String, str:String):Array[Byte] = { - val dims = new Array[Long](2); - dims(0) = str.length; - dims(1) = 1; - val dplist_id = H5Pcreate(H5P_DATASET_CREATE); - setCompressionPlist(dplist_id, dims); - val sbytes = str.getBytes("UTF_16LE"); - val strspace_id = H5Screate_simple(2, dims, null); - val str_id = H5Dcreate(id, varname, H5T_NATIVE_USHORT, strspace_id, H5P_DEFAULT, dplist_id, H5P_DEFAULT); - putStringAttr(str_id, "MATLAB_class", "char"); - putIntAttr(str_id, "MATLAB_int_decode", 2); - H5Dwrite(str_id, H5T_NATIVE_USHORT, H5S_ALL, H5S_ALL, H5P_DEFAULT, sbytes); - val ref = H5Rcreate(str_id, ".", H5R_OBJECT, -1); - H5Dclose(str_id); - H5Sclose(strspace_id); - H5Pclose(dplist_id); - ref - } - - def putMat(fid:Int, a:AnyRef, aname:String):Array[Byte] = { - a match { - case aa:DMat => putDenseMat[Double](fid, aa, aname, H5T_NATIVE_DOUBLE, "double") - case aa:FMat => { - if (aa.dims.length == 2) { - putDenseMat[Float](fid, aa, aname, H5T_NATIVE_FLOAT, "single") - } else { - putFND(fid, aa, aname, H5T_NATIVE_FLOAT, "single") - } - } - case aa:IMat => putDenseMat[Int](fid, aa, aname, H5T_NATIVE_INT, "int32") - case aa:SBMat => putSparseMat[Byte](fid, aa, aname, H5T_NATIVE_CHAR, "int8") - case aa:SMat => putSparseMat[Float](fid, aa, aname, H5T_NATIVE_FLOAT, "single") - case aa:SDMat => putSparseMat[Double](fid, aa, aname, H5T_NATIVE_DOUBLE, "double") - case aa:CSMat => putCellMat(fid, aname, aa) - case aa:String => putMatString(fid, aname, aa) - - case _ => throw new RuntimeException("unsupported matrix type to save") - } - } - - def h5list(fname:String):CSMat = { - val fapl = H5Pcreate(H5P_FILE_ACCESS); - val fid = H5Fopen(fname,H5F_ACC_RDONLY,fapl); - val info = H5Gget_info_by_name(fid, "/", H5P_DEFAULT); - val n = info.nlinks.toInt; - val cs = CSMat(n, 1); - for (i <- 0 until n) { - cs(i) = H5Lget_name_by_idx(fid, "/", H5_INDEX_NAME, H5_ITER_INC, i, H5P_DEFAULT) - } - H5Fclose(fid); - H5Pclose(fapl); - cs - } - - def hload(fname:String, vname:String):AnyRef = { - val fapl = H5Pcreate(H5P_FILE_ACCESS); - // H5Pset_fapl_core(fapl, 16*1024*1024, false); println("core driver") - if (Mat.useStdio) H5Pset_fapl_stdio(fapl); //println("stdio driver"); // Not working on windows - val fid = H5Fopen(fname,H5F_ACC_RDONLY,fapl); - H5Pclose(fapl); - val mat = getMat(fid, vname); - H5Fclose(fid); - mat - } - - def hload(fname:String, vnames:List[String]):List[AnyRef] = { - val fapl = H5Pcreate(H5P_FILE_ACCESS); - // H5Pset_fapl_core(fapl, 32*1024*1024, false); println("core driver") - if (Mat.useStdio) H5Pset_fapl_stdio(fapl); //println("stdio driver") - val fid = H5Fopen(fname,H5F_ACC_RDONLY,fapl); - H5Pclose(fapl); - val mats = vnames.map((vname) => getMat(fid, vname)); - H5Fclose(fid); - mats - } - - def hsaveAsHDF5(fname:String, args:List[AnyRef]) = { - refcount = -1; - val fapl_id = H5Pcreate (H5P_FILE_ACCESS); - if (Mat.useStdio) H5Pset_fapl_stdio(fapl_id); - val fid = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, fapl_id); - H5Pclose(fapl_id); - saveAsImpl(fid, args); - H5Fclose(fid); - } - - def hsaveAs(fname:String, args:List[AnyRef]) = { - refcount = -1; - val fapl_id = H5Pcreate (H5P_FILE_ACCESS); - if (Mat.useStdio) H5Pset_fapl_stdio(fapl_id); - val fcplist_id = H5Pcreate(H5P_FILE_CREATE); - H5Pset_userblock(fcplist_id, 512); - val fid = H5Fcreate(fname, H5F_ACC_TRUNC, fcplist_id, fapl_id); - H5Pclose(fcplist_id); - H5Pclose(fapl_id); - saveAsImpl(fid, args); - H5Fclose(fid); - writeMatHeader(fname); - } - - def saveAsImpl(fid:Int, argList:List[AnyRef]) = { - var i = 0; - while (i < argList.length) { - argList(i) match { - case a:Mat => { - argList(i+1) match { - case str:String => putMat(fid, a, str); - case _ => throw new RuntimeException("odd numbered args must be String variable names"); - } - } - case _ => throw new RuntimeException("even numbered args must be Mat variables"); - } - i += 2; - } - } - def addOne(ii:Array[Int]) = { if (Mat.ioneBased == 1) { var i = 0; diff --git a/src/main/scala/BIDMat/Operators.scala b/src/main/scala/BIDMat/Operators.scala index 30743afe..d5934735 100644 --- a/src/main/scala/BIDMat/Operators.scala +++ b/src/main/scala/BIDMat/Operators.scala @@ -34,8 +34,8 @@ trait Mop { def top(a:TMat, b:FMat, c:Mat):Mat = {notImplemented(myname, a, b); a} def top(a:TMat, b:SMat, c:Mat):Mat = {notImplemented(myname, a, b); a} - def top(a:TMat, b:GMat, c:Mat):Mat = {notImplemented(myname, a, b); a} - def top(a:TMat, b:GSMat, c:Mat):Mat = {notImplemented(myname, a, b); a} +// def top(a:TMat, b:GMat, c:Mat):Mat = {notImplemented(myname, a, b); a} +// def top(a:TMat, b:GSMat, c:Mat):Mat = {notImplemented(myname, a, b); a} /* * Default coercions when first mat is an FMat. These can be overridden. @@ -179,8 +179,8 @@ trait Mop { def op(a:TMat, b:Mat, c:Mat):Mat = { b match { - case bb:GMat => top(a, bb, c); - case bb:GSMat => top(a, bb, c); +// case bb:GMat => top(a, bb, c); +// case bb:GSMat => top(a, bb, c); case bb:FMat => top(a, bb, c); case bb:SMat => top(a, bb, c); } @@ -219,12 +219,12 @@ trait Mop { def getSPair(c:Mat, a:SMat):SPair = new SPair(c, a) def getSDPair(c:Mat, a:SDMat):SDPair = new SDPair(c, a) - def getGPair(c:Mat, a:GMat):GPair = new GPair(c, a) - def getGDPair(c:Mat, a:GDMat):GDPair = new GDPair(c, a) - def getGIPair(c:Mat, a:GIMat):GIPair = new GIPair(c, a) - def getGLPair(c:Mat, a:GLMat):GLPair = new GLPair(c, a) - def getGSPair(c:Mat, a:GSMat):GSPair = new GSPair(c, a) - def getGSDPair(c:Mat, a:GSDMat):GSDPair = new GSDPair(c, a) +// def getGPair(c:Mat, a:GMat):GPair = new GPair(c, a) +// def getGDPair(c:Mat, a:GDMat):GDPair = new GDPair(c, a) +// def getGIPair(c:Mat, a:GIMat):GIPair = new GIPair(c, a) +// def getGLPair(c:Mat, a:GLMat):GLPair = new GLPair(c, a) +// def getGSPair(c:Mat, a:GSMat):GSPair = new GSPair(c, a) +// def getGSDPair(c:Mat, a:GSDMat):GSDPair = new GSDPair(c, a) def getTPair(c:Mat, a:TMat):TPair = new TPair(c, a) } diff --git a/src/main/scala/BIDMat/SDMat.scala b/src/main/scala/BIDMat/SDMat.scala index 5ce76528..593662b2 100755 --- a/src/main/scala/BIDMat/SDMat.scala +++ b/src/main/scala/BIDMat/SDMat.scala @@ -419,7 +419,7 @@ object SDMat { } def apply(a:Mat) = a match { - case aa:GSMat => aa.toSMat.toSDMat +// case aa:GSMat => aa.toSMat.toSDMat case aa:SMat => aa.toSDMat case aa:SDMat => aa } diff --git a/src/main/scala/BIDMat/SFunctions.scala b/src/main/scala/BIDMat/SFunctions.scala index ad21c9fe..57b88e7c 100644 --- a/src/main/scala/BIDMat/SFunctions.scala +++ b/src/main/scala/BIDMat/SFunctions.scala @@ -9,7 +9,7 @@ object SFunctions { def sum(a:SMat, n:Int, omat:Mat):FMat = { a match { - case aa:GSMat => aa.sum(n, omat); +// case aa:GSMat => aa.sum(n, omat); case _ => a.ssReduceOp(n, FMat.idFun, FMat.sumFun, omat); } } @@ -29,7 +29,7 @@ object SFunctions { def min(a:SMat, b:Float, omat:Mat):SMat = { a match { - case aa:GSMat => aa.GSDop(GMat(b), omat, op_min); +// case aa:GSMat => aa.GSDop(GMat(b), omat, op_min); case _ => a.ssMatOpScalar(b, FMat.minFun, omat) } } @@ -40,7 +40,7 @@ object SFunctions { def max(a:SMat, b:Float, omat:Mat):SMat = { a match { - case aa:GSMat => aa.GSDop(GMat(b), omat, op_max); +// case aa:GSMat => aa.GSDop(GMat(b), omat, op_max); case _ => a.ssMatOpScalar(b, FMat.maxFun, omat) } } diff --git a/src/main/scala/BIDMat/SMat.scala b/src/main/scala/BIDMat/SMat.scala index 3d5ee15d..24038943 100755 --- a/src/main/scala/BIDMat/SMat.scala +++ b/src/main/scala/BIDMat/SMat.scala @@ -154,24 +154,24 @@ case class SMat(nr:Int, nc:Int, nnz1:Int, ir0:Array[Int], jc0:Array[Int], val da def ssMatOpS(b: SMat, f:(Float, Float) => Float, op:Int, omat:Mat):SMat = { b match { - case bb:GSMat => SMat(SMat(this).sgMatOp(SMat(b), f, omat)); +// case bb:GSMat => SMat(SMat(this).sgMatOp(SMat(b), f, omat)); case _ => SMat(SMat(this).sgMatOp(b, f, omat)); } } def ssMatOpD(b: FMat, f:(Float, Float) => Float, op:Int, omat:Mat):SMat = { (this, b) match { - case (aa:SMat, bb:GMat) => GSMat(this).GSDop(bb, null, op); - case (aa:GSMat, bb:FMat) => aa.GSDop(GMat(bb), null, op); +// case (aa:SMat, bb:GMat) => GSMat(this).GSDop(bb, null, op); +// case (aa:GSMat, bb:FMat) => aa.GSDop(GMat(bb), null, op); case _ => SMat(sgMatOpD(b, f, omat)); } } def ssMatOp(b: Mat, f:(Float, Float) => Float, op:Int, omat:Mat):Mat = { (this, b) match { - case (aa:GSMat, bb:FMat) => aa.GSDop(GMat(bb), null, op) - case (aa:GSMat, bb:SMat) => SMat(sgMatOp(SMat(bb), f, omat)); - case (aa:SMat, bb:GMat) => GSMat(this).GSDop(bb, null, op); +// case (aa:GSMat, bb:FMat) => aa.GSDop(GMat(bb), null, op) +// case (aa:GSMat, bb:SMat) => SMat(sgMatOp(SMat(bb), f, omat)); +// case (aa:SMat, bb:GMat) => GSMat(this).GSDop(bb, null, op); case (aa:SMat, bb:SMat) => SMat(sgMatOp(SMat(bb), f, omat)); case (aa:SMat, bb:FMat) => SMat(sgMatOpD(bb, f, omat)); } @@ -189,8 +189,8 @@ case class SMat(nr:Int, nc:Int, nnz1:Int, ir0:Array[Int], jc0:Array[Int], val da def SMult(b:Mat, outmat:Mat):FMat = { (this, b) match { - case (aa:GSMat, bb:FMat) => aa.SDMult(bb, outmat); - case (aa:SMat, bb:GMat) => GSMat(aa).SDMult(bb, outmat); +// case (aa:GSMat, bb:FMat) => aa.SDMult(bb, outmat); +// case (aa:SMat, bb:GMat) => GSMat(aa).SDMult(bb, outmat); case _ => SMultF(b, outmat); } } @@ -269,8 +269,8 @@ case class SMat(nr:Int, nc:Int, nnz1:Int, ir0:Array[Int], jc0:Array[Int], val da def Tmult(b:FMat, outmat:Mat):FMat = { (this, b) match { - case (aa:GSMat, bb:FMat) => aa.SDTMult(b, outmat); - case (aa:SMat, bb:GMat) => GSMat(aa).SDTMult(bb, outmat); +// case (aa:GSMat, bb:FMat) => aa.SDTMult(b, outmat); +// case (aa:SMat, bb:GMat) => GSMat(aa).SDTMult(bb, outmat); case _ => TmultF(b, outmat); } } @@ -680,12 +680,12 @@ case class SMat(nr:Int, nc:Int, nnz1:Int, ir0:Array[Int], jc0:Array[Int], val da out } - def copyTo(g:GSMat) = GSMat.fromSMat(this, g) +// def copyTo(g:GSMat) = GSMat.fromSMat(this, g) override def copyTo(m:Mat):Mat = { if (m == null) copyTo(null):SMat else m match { - case ss:GSMat => GSMat.fromSMat(this, ss) +// case ss:GSMat => GSMat.fromSMat(this, ss) case ss:SMat => copyTo(ss):SMat } } @@ -838,7 +838,7 @@ object SMat { } def apply(a:Mat) = a match { - case aa:GSMat => aa.toSMat +// case aa:GSMat => aa.toSMat case aa:SMat => aa case aa:FMat => MatFunctions.sparse(aa) case aa:SDMat => aa.toSMat diff --git a/src/main/scala/BIDMat/SciFunctions.scala b/src/main/scala/BIDMat/SciFunctions.scala index f83b6a58..8ba4ab36 100755 --- a/src/main/scala/BIDMat/SciFunctions.scala +++ b/src/main/scala/BIDMat/SciFunctions.scala @@ -16,47 +16,47 @@ import org.apache.commons.math3.random.RandomDataGenerator; object SciFunctions { - if (Mat.hasCUDA > 0) { - GFunctions.initJCUDA - initCUDArngs - } +// if (Mat.hasCUDA > 0) { +// GFunctions.initJCUDA +// initCUDArngs +// } def BIDMatInit { Mat.checkMKL(false); - Mat.checkCUDA; +// Mat.checkCUDA; } - def initCUDArngs = GFunctions.initCUDArngs; +// def initCUDArngs = GFunctions.initCUDArngs; - def initCUDArng(igpu:Int) = GFunctions.initCUDArng(igpu); +// def initCUDArng(igpu:Int) = GFunctions.initCUDArng(igpu); - def resetGPU = GFunctions.resetGPU; +// def resetGPU = GFunctions.resetGPU; - def resetGPUs = GFunctions.resetGPUs; +// def resetGPUs = GFunctions.resetGPUs; - def initJCUDA = GFunctions.initJCUDA; +// def initJCUDA = GFunctions.initJCUDA; - def setGPU(i:Int) = GFunctions.setGPU(i); +// def setGPU(i:Int) = GFunctions.setGPU(i); - def getGPU:Int = GFunctions.getGPU; +// def getGPU:Int = GFunctions.getGPU; - def connect(i:Int) = GFunctions.connect(i); +// def connect(i:Int) = GFunctions.connect(i); - def disconnect(i:Int) = GFunctions.disconnect(i); +// def disconnect(i:Int) = GFunctions.disconnect(i); - def canconnect(i:Int) = GFunctions.canconnect(i); +// def canconnect(i:Int) = GFunctions.canconnect(i); - def GPUmem = GFunctions.GPUmem; +// def GPUmem = GFunctions.GPUmem; - def GPUmemory = GFunctions.GPUmemory; +// def GPUmemory = GFunctions.GPUmemory; def setNumThreads(n:Int) = edu.berkeley.bid.UTILS.setnumthreads(n); def getNumThreads = edu.berkeley.bid.UTILS.getnumthreads(); - def ncclAllReduce(from:Array[GMat], to:Array[GMat], op:Int) = GFunctions.ncclAllReduce(from, to, op); +// def ncclAllReduce(from:Array[GMat], to:Array[GMat], op:Int) = GFunctions.ncclAllReduce(from, to, op); - def ncclAllReduce(from:Array[GMat], to:Array[GMat]) = GFunctions.ncclAllReduce(from, to, 0); +// def ncclAllReduce(from:Array[GMat], to:Array[GMat]) = GFunctions.ncclAllReduce(from, to, 0); def setseed(seed:Int):Unit = { myrand.setSeed(seed) @@ -65,34 +65,34 @@ object SciFunctions { vslNewStream(stream, BRNG, seed); rand(1,10); } - if (Mat.hasCUDA > 0) { - val thisGPU = getGPU - for (i <- 0 until Mat.hasCUDA) { - setseed(seed, i); - } - setGPU(thisGPU) - } +// if (Mat.hasCUDA > 0) { +// val thisGPU = getGPU +// for (i <- 0 until Mat.hasCUDA) { +// setseed(seed, i); +// } +// setGPU(thisGPU) +// } } - def setseed(seed:Int, igpu:Int):Unit = GFunctions.setseed(seed, igpu); +// def setseed(seed:Int, igpu:Int):Unit = GFunctions.setseed(seed, igpu); def norm(a:FMat):Double = { a match { - case aa:GMat => GFunctions.norm(aa); +// case aa:GMat => GFunctions.norm(aa); case aa:FMat => FFunctions.norm(aa); } } def norm(a:DMat):Double = { a match { - case aa:GDMat => GDFunctions.norm(aa); +// case aa:GDMat => GDFunctions.norm(aa); case aa:DMat => DFunctions.norm(aa); } } - def norm(a:GMat):Double = GFunctions.norm(a); +// def norm(a:GMat):Double = GFunctions.norm(a); - def norm(a:GDMat):Double = GDFunctions.norm(a); +// def norm(a:GDMat):Double = GDFunctions.norm(a); def norm (a:Mat):Double = { a match { @@ -122,18 +122,18 @@ object SciFunctions { def drand(dims:IMat):DMat = drand(dims, 0, 1); def rand(out:DMat):DMat = rand(0.0, 1.0, out); - def rand(out:GMat):GMat = GFunctions.rand(out); - def grand(nr:Int, nc:Int):GMat = GFunctions.rand(Array(nr, nc)); - def grand(dims:IMat):GMat = GFunctions.rand(dims); +// def rand(out:GMat):GMat = GFunctions.rand(out); +// def grand(nr:Int, nc:Int):GMat = GFunctions.rand(Array(nr, nc)); +// def grand(dims:IMat):GMat = GFunctions.rand(dims); - def rand(out:GDMat):GDMat = GDFunctions.rand(out); - def gdrand(nr:Int, nc:Int):GDMat = GDFunctions.rand(Array(nr, nc)); - def gdrand(dims:IMat):GDMat = GDFunctions.rand(dims); +// def rand(out:GDMat):GDMat = GDFunctions.rand(out); +// def gdrand(nr:Int, nc:Int):GDMat = GDFunctions.rand(Array(nr, nc)); +// def gdrand(dims:IMat):GDMat = GDFunctions.rand(dims); def rand(mat:Mat):Mat = { mat match { - case g:GMat => GFunctions.rand(g); - case gd:GDMat => GDFunctions.rand(gd); +// case g:GMat => GFunctions.rand(g); +// case gd:GDMat => GDFunctions.rand(gd); case a:FMat => rand(a); case d:DMat => rand(d); } @@ -151,18 +151,18 @@ object SciFunctions { def cnormrnd(mu:Float, sig:Float, m:Int, n:Int):CMat = normrnd(mu, sig, CMat(m, n)); def cnormrnd(mu:Float, sig:Float, dims:IMat):CMat = normrnd(mu, sig, CMat.make(dims)); - def normrnd(mu:Float, sig:Float, out:GMat):GMat = GFunctions.normrnd(mu, sig, out) - def gnormrnd(mu:Float, sig:Float, nr:Int, nc:Int):GMat = GFunctions.normrnd(mu, sig, GMat(nr, nc)); - def gnormrnd(mu:Float, sig:Float, dims:IMat):GMat = GFunctions.normrnd(mu, sig, GMat.make(dims)); +// def normrnd(mu:Float, sig:Float, out:GMat):GMat = GFunctions.normrnd(mu, sig, out) +// def gnormrnd(mu:Float, sig:Float, nr:Int, nc:Int):GMat = GFunctions.normrnd(mu, sig, GMat(nr, nc)); +// def gnormrnd(mu:Float, sig:Float, dims:IMat):GMat = GFunctions.normrnd(mu, sig, GMat.make(dims)); - def normrnd(mu:Double, sig:Double, out:GDMat):GDMat = GDFunctions.normrnd(mu, sig, out); - def gdnormrnd(mu:Double, sig:Double, nr:Int, nc:Int):GDMat = GDFunctions.normrnd(mu, sig, GDMat(nr, nc)); - def gdnormrnd(mu:Float, sig:Float, dims:IMat):GDMat = GDFunctions.normrnd(mu, sig, GDMat.make(dims)); +// def normrnd(mu:Double, sig:Double, out:GDMat):GDMat = GDFunctions.normrnd(mu, sig, out); +// def gdnormrnd(mu:Double, sig:Double, nr:Int, nc:Int):GDMat = GDFunctions.normrnd(mu, sig, GDMat(nr, nc)); +// def gdnormrnd(mu:Float, sig:Float, dims:IMat):GDMat = GDFunctions.normrnd(mu, sig, GDMat.make(dims)); def normrnd(mu:Double, sig:Double, out:Mat):Mat = { out match { - case a:GMat => normrnd(mu.toFloat, sig.toFloat, a); - case a:GDMat => normrnd(mu, sig, a); +// case a:GMat => normrnd(mu.toFloat, sig.toFloat, a); +// case a:GDMat => normrnd(mu, sig, a); case a:FMat => normrnd(mu.toFloat, sig.toFloat, a); case a:DMat => normrnd(mu, sig, a); case a:CMat => normrnd(mu, sig, a); @@ -174,16 +174,16 @@ object SciFunctions { def poissrnd(lambda:Float, out:IMat):IMat = FFunctions.poissrnd(lambda, out); - def gpoissrnd(mu:Float, nr:Int, nc:Int):GIMat = GFunctions.poissrnd(mu, GIMat(nr, nc)); - def gpoissrnd(mu:Float, dims:IMat):GIMat = GFunctions.poissrnd(mu, GIMat.make(dims)); +// def gpoissrnd(mu:Float, nr:Int, nc:Int):GIMat = GFunctions.poissrnd(mu, GIMat(nr, nc)); +// def gpoissrnd(mu:Float, dims:IMat):GIMat = GFunctions.poissrnd(mu, GIMat.make(dims)); - def poissrnd(mu:GMat, out:GIMat):GIMat = GFunctions.poissrnd(mu, out); - def poissrnd(mu:GMat):GIMat = GFunctions.poissrnd(mu, GIMat.make(mu.dims)); - def poissrnd(mu:Float, out:GIMat):GIMat = GFunctions.poissrnd(mu, out); +// def poissrnd(mu:GMat, out:GIMat):GIMat = GFunctions.poissrnd(mu, out); +// def poissrnd(mu:GMat):GIMat = GFunctions.poissrnd(mu, GIMat.make(mu.dims)); +// def poissrnd(mu:Float, out:GIMat):GIMat = GFunctions.poissrnd(mu, out); def poissrnd(lambda:Mat, out:Mat):Mat = { (lambda, out) match { - case (a:GMat, b:GIMat) => poissrnd(a, b); +// case (a:GMat, b:GIMat) => poissrnd(a, b); case (a:FMat, b:IMat) => poissrnd(a, b); } } @@ -213,14 +213,14 @@ object SciFunctions { def gamrnd(a:Mat, b:Mat, out:Mat):Mat = { (a,b) match { - case (a:GMat, b:GMat) => GFunctions.gamrnd(a,b,out.asInstanceOf[GMat]) +// case (a:GMat, b:GMat) => GFunctions.gamrnd(a,b,out.asInstanceOf[GMat]) case (a:FMat, b:FMat) => FFunctions.gamrnd(a,b,out.asInstanceOf[FMat]); case _ => throw new RuntimeException("Error in gamrnd, arguments do not match any of the cases") } } - def gamrnd(a:GMat, b:GMat, out:GMat):GMat = GFunctions.gamrnd(a, b, out); - def gamrnd(a:GMat, b:GMat):GMat = GFunctions.gamrnd(a, b, GMat(a.nrows, a.ncols)); +// def gamrnd(a:GMat, b:GMat, out:GMat):GMat = GFunctions.gamrnd(a, b, out); +// def gamrnd(a:GMat, b:GMat):GMat = GFunctions.gamrnd(a, b, GMat(a.nrows, a.ncols)); def laprnd(a:Float, b:Float, out:FMat):FMat = FFunctions.laprnd(a, b, out); def laprnd(a:Float, b:Float, m:Int, n:Int):FMat = laprnd(a, b, FMat(m, n)); @@ -632,29 +632,29 @@ object SciFunctions { /** min, max, sum, prod, cumsum, maxi, mini for GMats with no output matrix*/ - def cumsumg(a:GMat, jc:GIMat, omat:Mat):GMat = GFunctions.cumsumg(a, jc, omat) - def maxg(a:GMat, jc:GIMat, omat:Mat, omati:Mat):(GMat,GIMat) = GFunctions.maxg(a, jc, omat, omati) - def ming(a:GMat, jc:GIMat, omat:Mat, omati:Mat):(GMat,GIMat) = GFunctions.maxg(a, jc, omat, omati) +// def cumsumg(a:GMat, jc:GIMat, omat:Mat):GMat = GFunctions.cumsumg(a, jc, omat) +// def maxg(a:GMat, jc:GIMat, omat:Mat, omati:Mat):(GMat,GIMat) = GFunctions.maxg(a, jc, omat, omati) +// def ming(a:GMat, jc:GIMat, omat:Mat, omati:Mat):(GMat,GIMat) = GFunctions.maxg(a, jc, omat, omati) - def cumsumg(a:GIMat, jc:GIMat, omat:Mat):GIMat = GIFunctions.cumsumg(a, jc, omat) - def maxg(a:GIMat, jc:GIMat, omat:Mat, omati:Mat):(GIMat,GIMat) = GIFunctions.maxg(a, jc, omat, omati) - def ming(a:GIMat, jc:GIMat, omat:Mat, omati:Mat):(GIMat,GIMat) = GIFunctions.ming(a, jc, omat, omati) +// def cumsumg(a:GIMat, jc:GIMat, omat:Mat):GIMat = GIFunctions.cumsumg(a, jc, omat) +// def maxg(a:GIMat, jc:GIMat, omat:Mat, omati:Mat):(GIMat,GIMat) = GIFunctions.maxg(a, jc, omat, omati) +// def ming(a:GIMat, jc:GIMat, omat:Mat, omati:Mat):(GIMat,GIMat) = GIFunctions.ming(a, jc, omat, omati) - def cumsumg(a:GMat, jc:GIMat):GMat = GFunctions.cumsumg(a, jc, null) - def maxg(a:GMat, jc:GIMat) = GFunctions.maxg(a, jc, null, null) - def ming(a:GMat, jc:GIMat) = GFunctions.ming(a, jc, null, null) +// def cumsumg(a:GMat, jc:GIMat):GMat = GFunctions.cumsumg(a, jc, null) +// def maxg(a:GMat, jc:GIMat) = GFunctions.maxg(a, jc, null, null) +// def ming(a:GMat, jc:GIMat) = GFunctions.ming(a, jc, null, null) - def cumsumg(a:GIMat, jc:GIMat):GIMat = GIFunctions.cumsumg(a, jc, null) - def maxg(a:GIMat, jc:GIMat) = GIFunctions.maxg(a, jc, null, null) - def ming(a:GIMat, jc:GIMat) = GIFunctions.ming(a, jc, null, null) +// def cumsumg(a:GIMat, jc:GIMat):GIMat = GIFunctions.cumsumg(a, jc, null) +// def maxg(a:GIMat, jc:GIMat) = GIFunctions.maxg(a, jc, null, null) +// def ming(a:GIMat, jc:GIMat) = GIFunctions.ming(a, jc, null, null) - def cumsumg(a:GDMat, jc:GIMat, omat:Mat):GDMat = GDFunctions.cumsumg(a, jc, omat) - def maxg(a:GDMat, jc:GIMat, omat:Mat, omati:Mat):(GDMat,GIMat) = GDFunctions.maxg(a, jc, omat, omati) - def ming(a:GDMat, jc:GIMat, omat:Mat, omati:Mat):(GDMat,GIMat) = GDFunctions.maxg(a, jc, omat, omati) +// def cumsumg(a:GDMat, jc:GIMat, omat:Mat):GDMat = GDFunctions.cumsumg(a, jc, omat) +// def maxg(a:GDMat, jc:GIMat, omat:Mat, omati:Mat):(GDMat,GIMat) = GDFunctions.maxg(a, jc, omat, omati) +// def ming(a:GDMat, jc:GIMat, omat:Mat, omati:Mat):(GDMat,GIMat) = GDFunctions.maxg(a, jc, omat, omati) - def cumsumg(a:GDMat, jc:GIMat):GDMat = GDFunctions.cumsumg(a, jc, null) - def maxg(a:GDMat, jc:GIMat) = GDFunctions.maxg(a, jc, null, null) - def ming(a:GDMat, jc:GIMat) = GDFunctions.ming(a, jc, null, null) +// def cumsumg(a:GDMat, jc:GIMat):GDMat = GDFunctions.cumsumg(a, jc, null) +// def maxg(a:GDMat, jc:GIMat) = GDFunctions.maxg(a, jc, null, null) +// def ming(a:GDMat, jc:GIMat) = GDFunctions.ming(a, jc, null, null) // import GMat.BinOp @@ -858,17 +858,17 @@ object SciFunctions { def cumsumByKey(a:FMat, b:IMat):FMat = a.cumsumByKey(b); - def cumsumByKey(a:GMat, b:GMat):GMat = a.cumsumByKey(b); +// def cumsumByKey(a:GMat, b:GMat):GMat = a.cumsumByKey(b); - def cumsumByKey(a:GMat, b:GIMat):GMat = a.cumsumByKey(b); +// def cumsumByKey(a:GMat, b:GIMat):GMat = a.cumsumByKey(b); - def cumsumByKey(a:GIMat, b:GIMat):GIMat = a.cumsumByKey(b); +// def cumsumByKey(a:GIMat, b:GIMat):GIMat = a.cumsumByKey(b); - def cumsumByKey(a:GIMat, b:GMat):GIMat = a.cumsumByKey(b); +// def cumsumByKey(a:GIMat, b:GMat):GIMat = a.cumsumByKey(b); - def cumsumByKey(a:GDMat, b:GDMat):GDMat = a.cumsumByKey(b); +// def cumsumByKey(a:GDMat, b:GDMat):GDMat = a.cumsumByKey(b); - def cumsumByKey(a:GLMat, b:GLMat):GLMat = a.cumsumByKey(b); +// def cumsumByKey(a:GLMat, b:GLMat):GLMat = a.cumsumByKey(b); def cumsumByKey(a:Mat, b:Mat):Mat = { (a, b) match { @@ -885,17 +885,17 @@ object SciFunctions { def cumsumByKey(a:FMat, b:IMat, omat:Mat):FMat = a.cumsumByKey(b, omat); - def cumsumByKey(a:GMat, b:GMat, omat:Mat):GMat = a.cumsumByKey(b, omat); +// def cumsumByKey(a:GMat, b:GMat, omat:Mat):GMat = a.cumsumByKey(b, omat); - def cumsumByKey(a:GMat, b:GIMat, omat:Mat):GMat = a.cumsumByKey(b, omat); +// def cumsumByKey(a:GMat, b:GIMat, omat:Mat):GMat = a.cumsumByKey(b, omat); - def cumsumByKey(a:GIMat, b:GIMat, omat:Mat):GIMat = a.cumsumByKey(b, omat); +// def cumsumByKey(a:GIMat, b:GIMat, omat:Mat):GIMat = a.cumsumByKey(b, omat); - def cumsumByKey(a:GIMat, b:GMat, omat:Mat):GIMat = a.cumsumByKey(b, omat); +// def cumsumByKey(a:GIMat, b:GMat, omat:Mat):GIMat = a.cumsumByKey(b, omat); - def cumsumByKey(a:GDMat, b:GDMat, omat:Mat):GDMat = a.cumsumByKey(b, omat); +// def cumsumByKey(a:GDMat, b:GDMat, omat:Mat):GDMat = a.cumsumByKey(b, omat); - def cumsumByKey(a:GLMat, b:GLMat, omat:Mat):GLMat = a.cumsumByKey(b, omat); +// def cumsumByKey(a:GLMat, b:GLMat, omat:Mat):GLMat = a.cumsumByKey(b, omat); def cumsumByKey(a:Mat, b:Mat, omat:Mat):Mat = { (a, b) match { @@ -911,17 +911,17 @@ object SciFunctions { def cummaxByKey(a:IMat, b:IMat):IMat = a.cummaxByKey(b); - def cummaxByKey(a:GMat, b:GMat):GMat = a.cummaxByKey(b); +// def cummaxByKey(a:GMat, b:GMat):GMat = a.cummaxByKey(b); - def cummaxByKey(a:GMat, b:GIMat):GMat = a.cummaxByKey(b); +// def cummaxByKey(a:GMat, b:GIMat):GMat = a.cummaxByKey(b); - def cummaxByKey(a:GIMat, b:GIMat):GIMat = a.cummaxByKey(b); +// def cummaxByKey(a:GIMat, b:GIMat):GIMat = a.cummaxByKey(b); - def cummaxByKey(a:GIMat, b:GMat):GIMat = a.cummaxByKey(b); +// def cummaxByKey(a:GIMat, b:GMat):GIMat = a.cummaxByKey(b); - def cummaxByKey(a:GDMat, b:GDMat):GDMat = a.cummaxByKey(b); +// def cummaxByKey(a:GDMat, b:GDMat):GDMat = a.cummaxByKey(b); - def cummaxByKey(a:GLMat, b:GLMat):GLMat = a.cummaxByKey(b); +// def cummaxByKey(a:GLMat, b:GLMat):GLMat = a.cummaxByKey(b); def cummaxByKey(a:Mat, b:Mat):Mat = { (a, b) match { @@ -938,17 +938,17 @@ object SciFunctions { def cummaxByKey(a:IMat, b:IMat, omat:Mat):IMat = a.cummaxByKey(b, omat); - def cummaxByKey(a:GMat, b:GMat, omat:Mat):GMat = a.cummaxByKey(b, omat); +// def cummaxByKey(a:GMat, b:GMat, omat:Mat):GMat = a.cummaxByKey(b, omat); - def cummaxByKey(a:GMat, b:GIMat, omat:Mat):GMat = a.cummaxByKey(b, omat); +// def cummaxByKey(a:GMat, b:GIMat, omat:Mat):GMat = a.cummaxByKey(b, omat); - def cummaxByKey(a:GIMat, b:GIMat, omat:Mat):GIMat = a.cummaxByKey(b, omat); +// def cummaxByKey(a:GIMat, b:GIMat, omat:Mat):GIMat = a.cummaxByKey(b, omat); - def cummaxByKey(a:GIMat, b:GMat, omat:Mat):GIMat = a.cummaxByKey(b, omat); +// def cummaxByKey(a:GIMat, b:GMat, omat:Mat):GIMat = a.cummaxByKey(b, omat); - def cummaxByKey(a:GDMat, b:GDMat, omat:Mat):GDMat = a.cummaxByKey(b, omat); +// def cummaxByKey(a:GDMat, b:GDMat, omat:Mat):GDMat = a.cummaxByKey(b, omat); - def cummaxByKey(a:GLMat, b:GLMat, omat:Mat):GLMat = a.cummaxByKey(b, omat); +// def cummaxByKey(a:GLMat, b:GLMat, omat:Mat):GLMat = a.cummaxByKey(b, omat); def cummaxByKey(a:Mat, b:Mat, omat:Mat):Mat = { (a, b) match { @@ -965,17 +965,17 @@ object SciFunctions { def cumminByKey(a:IMat, b:IMat):IMat = a.cumminByKey(b); - def cumminByKey(a:GMat, b:GMat):GMat = a.cumminByKey(b); +// def cumminByKey(a:GMat, b:GMat):GMat = a.cumminByKey(b); - def cumminByKey(a:GMat, b:GIMat):GMat = a.cumminByKey(b); +// def cumminByKey(a:GMat, b:GIMat):GMat = a.cumminByKey(b); - def cumminByKey(a:GIMat, b:GIMat):GIMat = a.cumminByKey(b); +// def cumminByKey(a:GIMat, b:GIMat):GIMat = a.cumminByKey(b); - def cumminByKey(a:GIMat, b:GMat):GIMat = a.cumminByKey(b); +// def cumminByKey(a:GIMat, b:GMat):GIMat = a.cumminByKey(b); - def cumminByKey(a:GDMat, b:GDMat):GDMat = a.cumminByKey(b); +// def cumminByKey(a:GDMat, b:GDMat):GDMat = a.cumminByKey(b); - def cumminByKey(a:GLMat, b:GLMat):GLMat = a.cumminByKey(b); +// def cumminByKey(a:GLMat, b:GLMat):GLMat = a.cumminByKey(b); def cumminByKey(a:Mat, b:Mat):Mat = { (a, b) match { @@ -992,17 +992,17 @@ object SciFunctions { def cumminByKey(a:IMat, b:IMat, omat:Mat):IMat = a.cumminByKey(b, omat); - def cumminByKey(a:GMat, b:GMat, omat:Mat):GMat = a.cumminByKey(b, omat); +// def cumminByKey(a:GMat, b:GMat, omat:Mat):GMat = a.cumminByKey(b, omat); - def cumminByKey(a:GMat, b:GIMat, omat:Mat):GMat = a.cumminByKey(b, omat); +// def cumminByKey(a:GMat, b:GIMat, omat:Mat):GMat = a.cumminByKey(b, omat); - def cumminByKey(a:GIMat, b:GIMat, omat:Mat):GIMat = a.cumminByKey(b, omat); +// def cumminByKey(a:GIMat, b:GIMat, omat:Mat):GIMat = a.cumminByKey(b, omat); - def cumminByKey(a:GIMat, b:GMat, omat:Mat):GIMat = a.cumminByKey(b, omat); +// def cumminByKey(a:GIMat, b:GMat, omat:Mat):GIMat = a.cumminByKey(b, omat); - def cumminByKey(a:GDMat, b:GDMat, omat:Mat):GDMat = a.cumminByKey(b, omat); +// def cumminByKey(a:GDMat, b:GDMat, omat:Mat):GDMat = a.cumminByKey(b, omat); - def cumminByKey(a:GLMat, b:GLMat, omat:Mat):GLMat = a.cumminByKey(b, omat); +// def cumminByKey(a:GLMat, b:GLMat, omat:Mat):GLMat = a.cumminByKey(b, omat); def cumminByKey(a:Mat, b:Mat, omat:Mat):Mat = { (a, b) match { @@ -1020,9 +1020,9 @@ object SciFunctions { def reverse(a:LMat):LMat = a._reverse(null); - def reverse(a:GMat):GMat = a._reverse(null); +// def reverse(a:GMat):GMat = a._reverse(null); - def reverse(a:GIMat):GIMat = a._reverse(null); +// def reverse(a:GIMat):GIMat = a._reverse(null); def reverse(a:Mat, omat:Mat):Mat = { a match { @@ -1041,9 +1041,9 @@ object SciFunctions { def reverse(a:LMat, omat:Mat):LMat = a._reverse(omat); - def reverse(a:GMat, omat:Mat):GMat = a._reverse(omat); +// def reverse(a:GMat, omat:Mat):GMat = a._reverse(omat); - def reverse(a:GIMat, omat:Mat):GIMat = a._reverse(omat); +// def reverse(a:GIMat, omat:Mat):GIMat = a._reverse(omat); def reverse(a:Mat):Mat = { a match { @@ -1086,7 +1086,7 @@ object SciFunctions { _mean(a, 0).asInstanceOf[CMat] } - def mean(a:GMat, dim0:Int):GMat = { +/* def mean(a:GMat, dim0:Int):GMat = { _mean(a, dim0).asInstanceOf[GMat] } @@ -1101,7 +1101,7 @@ object SciFunctions { def mean(a:GDMat):GDMat = { _mean(a, 0).asInstanceOf[GDMat] } - + */ def mean(a:Mat, b:Int):Mat = _mean(a,b) def mean(a:Mat):Mat = _mean(a, 0):Mat @@ -1146,7 +1146,7 @@ object SciFunctions { def variance(a:CMat):CMat = { _variance(a, 0).asInstanceOf[CMat] } - +/* def variance(a:GMat, dim0:Int):GMat = { _variance(a, dim0).asInstanceOf[GMat] } @@ -1162,7 +1162,7 @@ object SciFunctions { def variance(a:GDMat):GDMat = { _variance(a, 0).asInstanceOf[GDMat] } - + */ def variance(a:Mat, dim:Int) = _variance(a, dim) def variance(a:Mat):Mat = _variance(a, 0) @@ -1752,9 +1752,9 @@ object SciFunctions { def exppsi(in:TMat):TMat = in.tFn(null, TFuncs.exppsi, 1L) - def LXdist(a:FMat, b:FMat, omat:FMat, p:Float):FMat = GFunctions.LXdist(a, b, omat, p) +// def LXdist(a:FMat, b:FMat, omat:FMat, p:Float):FMat = GFunctions.LXdist(a, b, omat, p) - def LXdist(a:FMat, b:FMat, p:Float):FMat = GFunctions.LXdist(a, b, null, p) +// def LXdist(a:FMat, b:FMat, p:Float):FMat = GFunctions.LXdist(a, b, null, p) def abs(a:Mat, b:Mat):Mat = { a match { diff --git a/src/main/scala/BIDMat/Solvers.scala b/src/main/scala/BIDMat/Solvers.scala index d2b846a0..2c2b0e6a 100755 --- a/src/main/scala/BIDMat/Solvers.scala +++ b/src/main/scala/BIDMat/Solvers.scala @@ -1,11 +1,6 @@ package BIDMat import edu.berkeley.bid.CBLAS._ import edu.berkeley.bid.LAPACK._ -import jcuda._ -import jcuda.runtime._ -import jcuda.runtime.JCuda._ -import jcuda.runtime.cudaMemcpyKind._ -import jcuda.jcublas.JCublas._ import MatFunctions._ import SciFunctions._ @@ -13,7 +8,7 @@ object Solvers { def inv(a:FMat, omat:Mat):FMat = { a match { - case aa:GMat => inv(aa, omat); +// case aa:GMat => inv(aa, omat); case _ => { Mat.nflops += 4L*a.nrows*a.nrows*a.nrows/3 if (a.nrows != a.ncols) { @@ -32,7 +27,7 @@ object Solvers { def inv(a:DMat, omat:Mat):DMat = { a match { - case aa:GDMat => inv(aa, omat); +// case aa:GDMat => inv(aa, omat); case _ => { Mat.nflops += 4L*a.nrows*a.nrows*a.nrows/3 if (a.nrows != a.ncols) { @@ -277,7 +272,7 @@ object Solvers { def QRdecomp(a:FMat, qin:Mat, rin:Mat):(FMat, FMat) = { a match { - case aa:GMat => QRdecomp(aa, qin, rin); +// case aa:GMat => QRdecomp(aa, qin, rin); case _ => { Mat.nflops += 4L*a.nrows*a.ncols*math.min(a.nrows, a.ncols) val m = a.nrows @@ -298,7 +293,7 @@ object Solvers { def QRdecomp(a:DMat, qin:Mat, rin:Mat):(DMat, DMat) = { a match { - case aa:GDMat => QRdecomp(aa, qin, rin); +// case aa:GDMat => QRdecomp(aa, qin, rin); case _ => { Mat.nflops += 4L*a.nrows*a.ncols*math.min(a.nrows, a.ncols) val m = a.nrows @@ -351,7 +346,7 @@ object Solvers { def QRdecompt(a:FMat, qin:Mat, rin:Mat):(FMat, FMat) = { a match { - case aa:GMat => QRdecompt(aa, qin, rin); +// case aa:GMat => QRdecompt(aa, qin, rin); case _ => { val m = a.nrows val n = a.ncols @@ -369,7 +364,7 @@ object Solvers { def QRdecompt(a:DMat, qin:Mat, rin:Mat):(DMat, DMat) = { a match { - case aa:GDMat => QRdecompt(aa, qin, rin); +// case aa:GDMat => QRdecompt(aa, qin, rin); case _ => { val m = a.nrows val n = a.ncols @@ -400,7 +395,7 @@ object Solvers { (q, r) } - def QRdecompt(a:GMat, qin:Mat, rin:Mat):(GMat, GMat) = { +/* def QRdecompt(a:GMat, qin:Mat, rin:Mat):(GMat, GMat) = { val m = a.nrows val n = a.ncols val q = GMat.newOrCheckGMat(a.nrows, a.ncols, qin, a.GUID, "QRdecompt_q".##); @@ -428,13 +423,13 @@ object Solvers { q <-- a cublasDtrsm('R', 'U', 'N', 'N', a.nrows, a.ncols, 1.0, r.pdata, r.nrows, q.pdata, q.nrows) (q, r) - } + }*/ def QRdecompt(a:FMat):(FMat, FMat) = QRdecompt(a, null, null); def QRdecompt(a:DMat):(DMat, DMat) = QRdecompt(a, null, null); def QRdecompt(a:CMat):(CMat, CMat) = QRdecompt(a, null, null); - def QRdecompt(a:GMat):(GMat, GMat) = QRdecompt(a, null, null); - def QRdecompt(a:GDMat):(GDMat, GDMat) = QRdecompt(a, null, null); +// def QRdecompt(a:GMat):(GMat, GMat) = QRdecompt(a, null, null); +// def QRdecompt(a:GDMat):(GDMat, GDMat) = QRdecompt(a, null, null); def QRdecompt(a:Mat, q:Mat, r:Mat):(Mat, Mat) = a match { case af:FMat => QRdecompt(af, q, r):(FMat, FMat) @@ -453,7 +448,7 @@ object Solvers { def trisolve(a:FMat, r:FMat, omat:Mat, mode:String):FMat = { (a, r) match { - case (aa:GMat, bb:GMat) => trisolve(aa, bb, omat, mode); +// case (aa:GMat, bb:GMat) => trisolve(aa, bb, omat, mode); case _ => { if (a.nrows != a.ncols) { throw new RuntimeException("trisolve A must be square") @@ -472,7 +467,7 @@ object Solvers { def trisolve(a:DMat, r:DMat, omat:Mat, mode:String):DMat = { (a, r) match { - case (aa:GDMat, bb:GDMat) => trisolve(aa, bb, omat, mode); +// case (aa:GDMat, bb:GDMat) => trisolve(aa, bb, omat, mode); case _ => { if (a.nrows != a.ncols) { throw new RuntimeException("trisolve A must be square") @@ -503,7 +498,7 @@ object Solvers { out } - def trisolve(a:GMat, r:GMat, omat:Mat, mode:String):GMat = { +/* def trisolve(a:GMat, r:GMat, omat:Mat, mode:String):GMat = { import jcuda.jcublas.JCublas._ if (a.nrows != a.ncols) { throw new RuntimeException("trisolve A must be square") @@ -521,9 +516,9 @@ object Solvers { val alpha = 1.0f; cublasStrsm(side, uplo, trans, diag, a.nrows, r.ncols, alpha, a.pdata, a.nrows, out.pdata, out.nrows) out - } + }*/ - def trisolve(a:GDMat, r:GDMat, omat:Mat, mode:String):GDMat = { +/* def trisolve(a:GDMat, r:GDMat, omat:Mat, mode:String):GDMat = { import jcuda.jcublas.JCublas._ if (a.nrows != a.ncols) { throw new RuntimeException("trisolve A must be square") @@ -541,18 +536,18 @@ object Solvers { val alpha = 1.0f; cublasDtrsm(side, uplo, trans, diag, a.nrows, r.ncols, alpha, a.pdata, a.nrows, out.pdata, out.nrows) out - } + }*/ def trisolve(a:DMat, r:DMat):DMat = trisolve(a, r, null, "UNN") def trisolve(a:FMat, r:FMat):FMat = trisolve(a, r, null, "UNN") def trisolve(a:CMat, r:CMat):CMat = trisolve(a, r, null, "UNN") - def trisolve(a:GMat, r:GMat):GMat = trisolve(a, r, null, "UNN") - def trisolve(a:GDMat, r:GDMat):GDMat = trisolve(a, r, null, "UNN") +// def trisolve(a:GMat, r:GMat):GMat = trisolve(a, r, null, "UNN") +// def trisolve(a:GDMat, r:GDMat):GDMat = trisolve(a, r, null, "UNN") def trisolve(a:DMat, r:DMat, omat:Mat):DMat = trisolve(a, r, omat, "UNN") def trisolve(a:FMat, r:FMat, omat:Mat):FMat = trisolve(a, r, omat, "UNN") def trisolve(a:CMat, r:CMat, omat:Mat):CMat = trisolve(a, r, omat, "UNN") - def trisolve(a:GMat, r:GMat, omat:Mat):GMat = trisolve(a, r, omat, "UNN") - def trisolve(a:GDMat, r:GDMat, omat:Mat):GDMat = trisolve(a, r, omat, "UNN") +// def trisolve(a:GMat, r:GMat, omat:Mat):GMat = trisolve(a, r, omat, "UNN") +// def trisolve(a:GDMat, r:GDMat, omat:Mat):GDMat = trisolve(a, r, omat, "UNN") def trisolve(a:Mat, r:Mat, omat:Mat, mode:String):Mat = (a, r) match { case (af:FMat, rf:FMat) => trisolve(af, rf, omat, mode) @@ -570,7 +565,7 @@ object Solvers { def triinv(a:FMat, omat:Mat, mode:String):FMat = { a match { - case aa:GMat => triinv(aa, omat, mode); +// case aa:GMat => triinv(aa, omat, mode); case _ => { if (a.nrows != a.ncols) { throw new RuntimeException("triinv: a must be square") @@ -586,7 +581,7 @@ object Solvers { def triinv(a:DMat, omat:Mat, mode:String):DMat = { a match { - case aa:GDMat => triinv(aa, omat, mode); +// case aa:GDMat => triinv(aa, omat, mode); case _ => { if (a.nrows != a.ncols) { throw new RuntimeException("triinv: a must be square") @@ -611,7 +606,7 @@ object Solvers { out } - def triinv(a:GMat, omat:Mat, mode:String):GMat = { +/* def triinv(a:GMat, omat:Mat, mode:String):GMat = { if (a.nrows != a.ncols) { throw new RuntimeException("triinv a must be square") } @@ -647,18 +642,18 @@ object Solvers { cublasDtrsm(side, uplo, trans, diag, a.nrows, a.ncols, alpha, a.pdata, a.nrows, out.pdata, out.nrows) cudaStreamSynchronize(Mat.SyncMethod); out - } + }*/ def triinv(a:DMat):DMat = triinv(a, null, "UN") def triinv(a:FMat):FMat = triinv(a, null, "UN") def triinv(a:CMat):CMat = triinv(a, null, "UN") - def triinv(a:GMat):GMat = triinv(a, null, "UN") - def triinv(a:GDMat):GDMat = triinv(a, null, "UN") +// def triinv(a:GMat):GMat = triinv(a, null, "UN") +// def triinv(a:GDMat):GDMat = triinv(a, null, "UN") def triinv(a:DMat, omat:Mat):DMat = triinv(a, omat, "UN") def triinv(a:FMat, omat:Mat):FMat = triinv(a, omat, "UN") def triinv(a:CMat, omat:Mat):CMat = triinv(a, omat, "UN") - def triinv(a:GMat, omat:Mat):GMat = triinv(a, omat, "UN") - def triinv(a:GDMat, omat:Mat):GDMat = triinv(a, omat, "UN") +// def triinv(a:GMat, omat:Mat):GMat = triinv(a, omat, "UN") +// def triinv(a:GDMat, omat:Mat):GDMat = triinv(a, omat, "UN") def triinv(a:Mat, omat:Mat, mode:String):Mat = a match { case af:FMat => triinv(af, omat, mode) case ad:DMat => triinv(ad, omat, mode) diff --git a/src/main/scala/BIDMat/TMat.scala b/src/main/scala/BIDMat/TMat.scala index f5eb2bd2..b7b56fda 100644 --- a/src/main/scala/BIDMat/TMat.scala +++ b/src/main/scala/BIDMat/TMat.scala @@ -13,13 +13,6 @@ import scala.reflect._ import scala.math.Numeric._ import scala.collection.mutable.ArrayBuffer -import jcuda._ -import jcuda.runtime._ -import jcuda.runtime.JCuda._ -import jcuda.runtime.cudaMemcpyKind._ -import jcuda.jcublas._ -import jcuda.jcublas.JCublas._ -import jcuda.jcusparse._ /* @@ -126,8 +119,8 @@ class TMat def tOp(a : Mat, omat : Mat, op : (Mat,Mat,Mat) => Mat) : TMat = { a match { case aa : TMat => tOp(aa,omat,op); - case aa : GMat => tOpM(aa,omat,op); - case aa : GDMat => tOpM(aa,omat,op); +// case aa : GMat => tOpM(aa,omat,op); +// case aa : GDMat => tOpM(aa,omat,op); case aa : FMat => tOpM(aa,omat,op); case aa : DMat => tOpM(aa,omat,op); @@ -144,7 +137,7 @@ class TMat out; } - def toGPU:TMat = { +/* def toGPU:TMat = { val t0 = MatFunctions.gpu(tiles(0)); val out = TMat.newOrCheckTMat(nrows,ncols,y,x,tiles.map(_.nrows),tiles.map(_.ncols),t0,null,GUID,"toGPU".##); out.tiles(0) = t0; @@ -152,7 +145,7 @@ class TMat out.tiles(i) <-- tiles(i); } out; - } + } */ override def set(f:Float) = { for (i <- 0 until tiles.length) { @@ -351,26 +344,26 @@ class TMat def * (a : FMat) = this.tMult(a,null); - def * (a : GMat) = this.tMult(a,null); +// def * (a : GMat) = this.tMult(a,null); def * (a : SMat) = this.tMult(a,null); - def * (a : GSMat) = this.tMult(a,null); +// def * (a : GSMat) = this.tMult(a,null); def *^ (a : FMat) = this.tMultNT(a,null); - def *^ (a : GMat) = this.tMultNT(a,null); +// def *^ (a : GMat) = this.tMultNT(a,null); def *^ (a : SMat) = this.tMultNT(a,null); - def *^ (a : GSMat) = this.tMultNT(a,null); +// def *^ (a : GSMat) = this.tMultNT(a,null); override def * (a : Mat) = a match { - case aa:GSMat => this.tMult(a,null); - case aa:GMat => this.tMult(a,null); +// case aa:GSMat => this.tMult(a,null); +// case aa:GMat => this.tMult(a,null); case aa:FMat => this.tMult(a,null); case aa:SMat => this.tMult(a,null); case _ => throw new RuntimeException("no match in tMult"); } override def *^ (a : Mat) = a match { - case aa:GMat => this.tMultNT(a,null); - case aa:GSMat => this.tMultNT(a,null); +// case aa:GMat => this.tMultNT(a,null); +// case aa:GSMat => this.tMultNT(a,null); case aa:FMat => this.tMultNT(a,null); case aa:SMat => this.tMultNT(a,null); case _ => throw new RuntimeException("no match in tMultT"); @@ -633,10 +626,10 @@ object TMat { def newOrCheckMat(nr:Int, nc:Int, mat:Mat, omat:Mat):Mat = { mat match { - case m:GMat => GMat.newOrCheckGMat(nr, nc, omat); - case m:GSMat => GMat.newOrCheckGMat(nr, nc, omat); - case m:GDMat => GDMat.newOrCheckGDMat(nr, nc, omat); - case m:GSDMat => GDMat.newOrCheckGDMat(nr, nc, omat); +// case m:GMat => GMat.newOrCheckGMat(nr, nc, omat); +// case m:GSMat => GMat.newOrCheckGMat(nr, nc, omat); +// case m:GDMat => GDMat.newOrCheckGDMat(nr, nc, omat); +// case m:GSDMat => GDMat.newOrCheckGDMat(nr, nc, omat); case m:FMat => FMat.newOrCheckFMat(nr, nc, omat); case m:SMat => FMat.newOrCheckFMat(nr, nc, omat); case m:DMat => DMat.newOrCheckDMat(nr, nc, omat); @@ -651,10 +644,10 @@ object TMat { def newOrCheckMat(nr:Int, nc:Int, mat:Mat, omat:Mat, matGUID:Long, opHash:Int):Mat = { mat match { - case m:GMat => GMat.newOrCheckGMat(nr, nc, omat, matGUID, opHash); - case m:GSMat => GMat.newOrCheckGMat(nr, nc, omat, matGUID, opHash); - case m:GDMat => GDMat.newOrCheckGDMat(nr, nc, omat, matGUID, opHash); - case m:GSDMat => GDMat.newOrCheckGDMat(nr, nc, omat, matGUID, opHash); +// case m:GMat => GMat.newOrCheckGMat(nr, nc, omat, matGUID, opHash); +// case m:GSMat => GMat.newOrCheckGMat(nr, nc, omat, matGUID, opHash); +// case m:GDMat => GDMat.newOrCheckGDMat(nr, nc, omat, matGUID, opHash); +// case m:GSDMat => GDMat.newOrCheckGDMat(nr, nc, omat, matGUID, opHash); case m:FMat => FMat.newOrCheckFMat(nr, nc, omat, matGUID, opHash); case m:SMat => FMat.newOrCheckFMat(nr, nc, omat, matGUID, opHash); case m:DMat => DMat.newOrCheckDMat(nr, nc, omat, matGUID, opHash); @@ -668,10 +661,10 @@ object TMat { def newOrCheckMat(nr:Int, nc:Int, mat:Mat, omat:Mat, guid1:Long, guid2:Long, opHash:Int):Mat = { mat match { - case m:GMat => GMat.newOrCheckGMat(nr, nc, omat, guid1, guid2, opHash); - case m:GSMat => GMat.newOrCheckGMat(nr, nc, omat, guid1, guid2, opHash); - case m:GDMat => GDMat.newOrCheckGDMat(nr, nc, omat, guid1, guid2, opHash); - case m:GSDMat => GDMat.newOrCheckGDMat(nr, nc, omat, guid1, guid2, opHash); +// case m:GMat => GMat.newOrCheckGMat(nr, nc, omat, guid1, guid2, opHash); +// case m:GSMat => GMat.newOrCheckGMat(nr, nc, omat, guid1, guid2, opHash); +// case m:GDMat => GDMat.newOrCheckGDMat(nr, nc, omat, guid1, guid2, opHash); +// case m:GSDMat => GDMat.newOrCheckGDMat(nr, nc, omat, guid1, guid2, opHash); case m:FMat => FMat.newOrCheckFMat(nr, nc, omat, guid1, guid2, opHash); case m:SMat => FMat.newOrCheckFMat(nr, nc, omat, guid1, guid2, opHash); case m:DMat => DMat.newOrCheckDMat(nr, nc, omat, guid1, guid2, opHash); @@ -682,10 +675,10 @@ object TMat { def newOrCheckMat(nr:Int, nc:Int, mat:Mat, omat:Mat, guid1:Long, guid2:Long, guid3:Long, opHash:Int):Mat = { mat match { - case m:GMat => GMat.newOrCheckGMat(nr, nc, omat, guid1, guid2, guid3, opHash); - case m:GSMat => GMat.newOrCheckGMat(nr, nc, omat, guid1, guid2, guid3, opHash); - case m:GDMat => GDMat.newOrCheckGDMat(nr, nc, omat, guid1, guid2, guid3, opHash); - case m:GSDMat => GDMat.newOrCheckGDMat(nr, nc, omat, guid1, guid2, guid3, opHash); +// case m:GMat => GMat.newOrCheckGMat(nr, nc, omat, guid1, guid2, guid3, opHash); +// case m:GSMat => GMat.newOrCheckGMat(nr, nc, omat, guid1, guid2, guid3, opHash); +// case m:GDMat => GDMat.newOrCheckGDMat(nr, nc, omat, guid1, guid2, guid3, opHash); +// case m:GSDMat => GDMat.newOrCheckGDMat(nr, nc, omat, guid1, guid2, guid3, opHash); case m:FMat => FMat.newOrCheckFMat(nr, nc, omat, guid1, guid2, guid3, opHash); case m:SMat => FMat.newOrCheckFMat(nr, nc, omat, guid1, guid2, guid3, opHash); case m:DMat => DMat.newOrCheckDMat(nr, nc, omat, guid1, guid2, guid3, opHash); diff --git a/src/test/scala/BIDMat/GDMatTest.scala b/src/test/scala/BIDMat/GDMatTest.scala deleted file mode 100644 index 80ac9b96..00000000 --- a/src/test/scala/BIDMat/GDMatTest.scala +++ /dev/null @@ -1,931 +0,0 @@ -package BIDMat - -import Mat._ -import MatFunctions._ -import SciFunctions._ -import org.scalatest._; -import org.scalatest.junit._; -import org.scalatest.prop._; -import org.junit.runner.RunWith - -@RunWith(classOf[JUnitRunner]) -class GDMatTest extends BIDMatSpec { - val nr = 10; - val nc = 20; - val nk = 30; - val nl = 40; - - override def beforeAll { - Mat.checkMKL(false) - Mat.checkCUDA(true) - } - - def checkSimilar(aa:DMat, bb:DMat, eps:Double = 1e-6f) = { - val a = DMat(aa); - val b = DMat(bb); - a.dims.length should equal (b.dims.length) ; - a.dims.data should equal (b.dims.data); - assert_approx_eq_double(a.data, b.data, eps); - } - - "A GDMat" should "support matrix transpose" in { - assume(Mat.hasCUDA > 0); - val a = drand(nr, nc); - val aa = GDMat(a); - val b = dzeros(nc, nr); - val cc = aa.t; - for (i <- 0 until nr) { - for (j <- 0 until nc) { - b.data(j + i * nc) = a.data(i + j * nr); - } - } - cc.mytype should equal ("GDMat"); - checkSimilar(cc, b); - } - - - it should "support matrix multiplication" in { - assume(Mat.hasCUDA > 0); - val a = drand(nr, nk); - val b = drand(nk, nc); - val d = dzeros(nr, nc); - val aa = GDMat(a); - val bb = GDMat(b); - val cc = aa * bb; - for (i <- 0 until nr) { - for (j <- 0 until nc) { - var sum = 0.0; - for (k <- 0 until nk) { - sum += a.data(i + k * nr) * b.data(k + j * nk); - } - d.data(i + j * nr) = sum; - } - } - cc.mytype should equal ("GDMat"); - checkSimilar(cc, d) - } - - it should "support matrix *^" in { - assume(Mat.hasCUDA > 0); - val a = drand(nr, nk); - val b = drand(nc, nk); - val aa = GDMat(a); - val bb = GDMat(b); - val c = a * (b.t); - val dd = aa *^ bb; - dd.mytype should equal ("GDMat"); - checkSimilar(c, dd) - } - - it should "support matrix ^*" in { - assume(Mat.hasCUDA > 0); - val a = drand(nk, nr); - val b = drand(nk, nc); - val aa = GDMat(a); - val bb = GDMat(b); - val c = (a.t) * b; - val dd = aa ^* bb; - dd.mytype should equal ("GDMat"); - checkSimilar(c, dd) - } - - it should "support matrix dot" in { - assume(Mat.hasCUDA > 0); - val a = drand(nr, nc); - val b = drand(nr, nc); - val aa = GDMat(a); - val bb = GDMat(b); - val c = a dot b; - val dd = aa dot bb; - dd.mytype should equal ("GDMat"); - checkSimilar(c, dd) - } - - it should "support matrix dotr" in { - assume(Mat.hasCUDA > 0); - val a = drand(nr, nc); - val b = drand(nr, nc); - val aa = GDMat(a); - val bb = GDMat(b); - val c = a dotr b; - val dd = aa dotr bb; - dd.mytype should equal ("GDMat"); - checkSimilar(c, dd) - } - - def testEwise(nr:Int, nc:Int, mop:(DMat,DMat)=>DMat, op:(Double,Double)=>Double, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = drand(nr, nc); - val b = drand(nr, nc); - val aa = GDMat(a); - val bb = GDMat(b); - val cc = mop(aa,bb); - val d = dzeros(nr, nc); - for (i <- 0 until nc) { - for (j <- 0 until nr) { - d.data(j + nr * i) = op(a.data(j + nr * i), b.data(j + nr * i)); - } - } - cc.mytype should equal ("GDMat"); - checkSimilar(cc, d); - } - } - - testEwise(nr, nc, (a:DMat, b:DMat) => a + b, (x:Double, y:Double)=>x+y, "support elementwise addition"); - - testEwise(nr, nc, (a:DMat, b:DMat) => a *@ b, (x:Double, y:Double)=>x*y, "support elementwise multiplication"); - - testEwise(nr, nc, (a:DMat, b:DMat) => a - b, (x:Double, y:Double)=>x-y, "support elementwise subtraction"); - - testEwise(nr, nc, (a:DMat, b:DMat) => a / b, (x:Double, y:Double)=>x/y, "support elementwise division"); - - testEwise(nr, nc, (a:DMat, b:DMat) => min(a,b), (x:Double, y:Double)=> math.min(x,y), "support elementwise min"); - - testEwise(nr, nc, (a:DMat, b:DMat) => max(a,b), (x:Double, y:Double)=> math.max(x,y), "support elementwise max"); - - def testBcastRows(nr:Int, nc:Int, mop:(DMat,DMat)=>DMat, op:(Double,Double)=>Double, msg:String, reverse:Boolean = true) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = drand(nr, nc); - val b = drand(1, nc); - val aa = GDMat(a); - val bb = GDMat(b); - val d = dzeros(nr, nc); - for (i <- 0 until nc) { - for (j <- 0 until nr) { - d.data(j + i * nr) = op(a.data(j + i * nr), b.data(i)); - } - } - val cc = mop(aa, bb); - cc.mytype should equal ("GDMat"); - checkSimilar(cc, d); - if (reverse) { - val ee = mop(bb, aa); - ee.mytype should equal ("GDMat"); - checkSimilar(ee, d); - } - } - } - - testBcastRows(nr, nc, (a:DMat, b:DMat) => a + b, (x:Double, y:Double)=>x+y, "support addition with broadcast over rows"); - - testBcastRows(nr, nc, (a:DMat, b:DMat) => a *@ b, (x:Double, y:Double)=>x*y, "support multiplication with broadcast over rows"); - - testBcastRows(nr, nc, (a:DMat, b:DMat) => a - b, (x:Double, y:Double)=>x-y, "support subtraction with broadcast over rows", false); - - testBcastRows(nr, nc, (a:DMat, b:DMat) => a / b, (x:Double, y:Double)=>x/y, "support division with broadcast over rows", false); - - testBcastRows(nr, nc, (a:DMat, b:DMat) => min(a,b), (x:Double, y:Double)=> math.min(x,y), "support min with broadcast over rows"); - - testBcastRows(nr, nc, (a:DMat, b:DMat) => max(a,b), (x:Double, y:Double)=> math.max(x,y), "support max with broadcast over rows"); - - def testBcastRows4D(nr:Int, nc:Int, mop:(DMat,DMat)=>DMat, op:(Double,Double)=>Double, msg:String, reverse:Boolean = true) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = drand(nr \ nc \ nk \ nl); - val b = drand(1 \ 1 \ nk \ nl); - val aa = GDMat(a); - val bb = GDMat(b); - val d = dzeros(a.dims); - for (i <- 0 until nr) { - for (j <- 0 until nc) { - for (k <- 0 until nk) { - for (l <- 0 until nl) { - d.data(i + nr * (j + nc * (k + nk * l))) = op(a.data(i + nr * (j + nc * (k + nk * l))), b.data(k + nk * l)); - } - } - } - } - val cc = mop(aa, bb); - cc.mytype should equal ("GDMat"); - checkSimilar(cc, d); - if (reverse) { - val ee = mop(bb, aa); - checkSimilar(ee, d); - } - } - } - - testBcastRows4D(nr, nc, (a:DMat, b:DMat) => a + b, (x:Double, y:Double)=>x+y, "support addition with broadcast over rows 4D"); - - testBcastRows4D(nr, nc, (a:DMat, b:DMat) => a *@ b, (x:Double, y:Double)=>x*y, "support multiplication with broadcast over rows 4D"); - - testBcastRows4D(nr, nc, (a:DMat, b:DMat) => a - b, (x:Double, y:Double)=>x-y, "support subtraction with broadcast over rows 4D", false); - - testBcastRows4D(nr, nc, (a:DMat, b:DMat) => a / b, (x:Double, y:Double)=>x/y, "support division with broadcast over rows 4D", false); - - testBcastRows4D(nr, nc, (a:DMat, b:DMat) => min(a,b), (x:Double, y:Double)=> math.min(x,y), "support min with broadcast over rows 4D"); - - testBcastRows4D(nr, nc, (a:DMat, b:DMat) => max(a,b), (x:Double, y:Double)=> math.max(x,y), "support max with broadcast over rows 4D"); - - def testBcastCols(nr:Int, nc:Int, mop:(DMat,DMat)=>DMat, op:(Double,Double)=>Double, msg:String, reverse:Boolean = true) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = drand(nr, nc); - val b = drand(nr, 1); - val aa = GDMat(a); - val bb = GDMat(b); - val d = dzeros(nr, nc); - for (i <- 0 until nc) { - for (j <- 0 until nr) { - d.data(j + i * nr) = op(a.data(j + i * nr), b.data(j)); - } - } - val cc = mop(aa, bb); - cc.mytype should equal ("GDMat"); - checkSimilar(cc, d); - if (reverse) { - val ee = mop(bb, aa); - ee.mytype should equal ("GDMat"); - checkSimilar(ee, d); - } - } - } - - - testBcastCols(nr, nc, (a:DMat, b:DMat) => a + b, (x:Double, y:Double)=>x+y, "support addition with broadcast over cols"); - - testBcastCols(nr, nc, (a:DMat, b:DMat) => a *@ b, (x:Double, y:Double)=>x*y, "support multiplication with broadcast over cols"); - - testBcastCols(nr, nc, (a:DMat, b:DMat) => a - b, (x:Double, y:Double)=>x-y, "support subtraction with broadcast over cols", false); - - testBcastCols(nr, nc, (a:DMat, b:DMat) => a / b, (x:Double, y:Double)=>x/y, "support division with broadcast over cols", false); - - testBcastCols(nr, nc, (a:DMat, b:DMat) => min(a,b), (x:Double, y:Double)=> math.min(x,y), "support min with broadcast over cols"); - - testBcastCols(nr, nc, (a:DMat, b:DMat) => max(a,b), (x:Double, y:Double)=> math.max(x,y), "support max with broadcast over cols"); - - def testScalar1(nr:Int, nc:Int, mop:(Double,DMat)=>DMat, op:(Double,Double)=>Double, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = drand(1, 1).fv; - val b = drand(nr, nc); - val bb = GDMat(b); - val d = dzeros(nr, nc); - for (i <- 0 until nc) { - for (j <- 0 until nr) { - d.data(j + i * nr) = op(a, b.data(j + i * nr)); - } - } - val cc = mop(a, bb); - cc.mytype should equal ("GDMat"); - checkSimilar(cc, d); - } - } - - def testScalar2(nr:Int, nc:Int, mop:(DMat,Double)=>DMat, op:(Double,Double)=>Double, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = drand(nr, nc); - val b = drand(1, 1).fv; - val aa = GDMat(a); - val d = dzeros(nr, nc); - for (i <- 0 until nc) { - for (j <- 0 until nr) { - d.data(j + i * nr) = op(a.data(j + i * nr), b); - } - } - val cc = mop(aa, b); - cc.mytype should equal ("GDMat"); - checkSimilar(cc, d); - } - } - - testScalar1(nr, nc, (a:Double, b:DMat) => a + b, (x:Double, y:Double)=>x+y, "support addition of scalar 1"); - - testScalar1(nr, nc, (a:Double, b:DMat) => a *@ b, (x:Double, y:Double)=>x*y, "support multiplication of scalar 1"); - - testScalar1(nr, nc, (a:Double, b:DMat) => min(a, b), (x:Double, y:Double)=>math.min(x,y), "support min of scalar 1"); - - testScalar1(nr, nc, (a:Double, b:DMat) => max(a, b), (x:Double, y:Double)=>math.max(x,y), "support max of scalar 1"); - - testScalar2(nr, nc, (a:DMat, b:Double) => a + b, (x:Double, y:Double)=>x+y, "support addition of scalar 2"); - - testScalar2(nr, nc, (a:DMat, b:Double) => a *@ b, (x:Double, y:Double)=>x*y, "support multiplication of scalar 2"); - - testScalar2(nr, nc, (a:DMat, b:Double) => a - b, (x:Double, y:Double)=>x-y, "support subtraction of scalar 2"); - - testScalar2(nr, nc, (a:DMat, b:Double) => a / b, (x:Double, y:Double)=>x / y, "support division of scalar 2"); - - testScalar2(nr, nc, (a:DMat, b:Double) => min(a, b), (x:Double, y:Double)=> math.min(x,y), "support min of scalar 2"); - - testScalar2(nr, nc, (a:DMat, b:Double) => max(a, b), (x:Double, y:Double)=> math.max(x,y), "support max of scalar 2"); - - - def testScalar1ND(nr:Int, nc:Int, mop:(Double,DMat)=>DMat, op:(Double,Double)=>Double, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = drand(1, 1).fv; - val b = drand(nr \ nc \ nk); - val bb = GDMat(b); - val d = dzeros(nr \ nc \ nk); - for (i <- 0 until nr) { - for (j <- 0 until nc) { - for (k <- 0 until nk) { - d.data(i + nr * (j + nc * k)) = op(a, b.data(i + nr * (j + nc * k))); - } - } - } - val cc = mop(a, bb); - cc.mytype should equal ("GDMat"); - checkSimilar(cc, d); - } - } - - def testScalar2ND(nr:Int, nc:Int, mop:(DMat,Double)=>DMat, op:(Double,Double)=>Double, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = drand(nr \ nc \ nk); - val b = drand(1, 1).fv; - val aa = GDMat(a); - val d = dzeros(nr \ nc \ nk); - for (i <- 0 until nr) { - for (j <- 0 until nc) { - for (k <- 0 until nk) { - d.data(i + nr * (j + nc * k)) = op(a.data(i + nr * (j + nc * k)), b); - } - } - } - val cc = mop(aa, b); - cc.mytype should equal ("GDMat"); - checkSimilar(cc, d); - } - } - - testScalar1ND(nr, nc, (a:Double, b:DMat) => a + b, (x:Double, y:Double)=>x+y, "support addition of scalar 1 ND"); - - testScalar1ND(nr, nc, (a:Double, b:DMat) => a *@ b, (x:Double, y:Double)=>x*y, "support multiplication of scalar 1 ND"); - - testScalar1ND(nr, nc, (a:Double, b:DMat) => min(a,b), (x:Double, y:Double)=>math.min(x,y), "support min of scalar 1 3D"); - - testScalar1ND(nr, nc, (a:Double, b:DMat) => max(a,b), (x:Double, y:Double)=>math.max(x,y), "support max of scalar 1 3D"); - - - testScalar2ND(nr, nc, (a:DMat, b:Double) => a + b, (x:Double, y:Double)=>x+y, "support addition of scalar 2 ND"); - - testScalar2ND(nr, nc, (a:DMat, b:Double) => a *@ b, (x:Double, y:Double)=>x*y, "support multiplication of scalar 2 ND"); - - testScalar2ND(nr, nc, (a:DMat, b:Double) => a - b, (x:Double, y:Double)=>x-y, "support subtraction of scalar 2 ND"); - - testScalar2ND(nr, nc, (a:DMat, b:Double) => a / b, (x:Double, y:Double)=>x / y, "support division of scalar 2 ND"); - - testScalar2ND(nr, nc, (a:DMat, b:Double) => min(a,b), (x:Double, y:Double)=>math.min(x,y), "support min of scalar 2 3D"); - - testScalar2ND(nr, nc, (a:DMat, b:Double) => max(a,b), (x:Double, y:Double)=>math.max(x,y), "support max of scalar 2 3D"); - - it should "support 1D element access" in { - assume(Mat.hasCUDA > 0); - val a = drand(nr, nc); - val aa = GDMat(a); - aa.mytype should equal ("GDMat"); - assert_approx_eq_double(Array(aa(5)), Array(a.data(5))); - } - - it should "support 2D element access" in { - assume(Mat.hasCUDA > 0); - val a = drand(nr, nc); - val aa = GDMat(a); - aa.mytype should equal ("GDMat"); - assert_approx_eq_double(Array(aa(2,3)), Array(a.data(2 + 3 * nr))); - } - - it should "support 3D element access" in { - assume(Mat.hasCUDA > 0); - val a = drand(nr \ nc \ nk); - val aa = GDMat(a); - aa.mytype should equal ("GDMat"); - assert_approx_eq_double(Array(aa(2, 3, 4)), Array(a.data(2 + 3 * nr + 4 * nr * nc))); - } - - it should "support 4D element access" in { - assume(Mat.hasCUDA > 0); - val a = drand(nr \ nc \ nk \ nl); - val aa = GDMat(a); - aa.mytype should equal ("GDMat"); - assert_approx_eq_double(Array(aa(2, 3, 4, 5)), Array(a.data(2 + nr * (3 + nc * (4 + nk * 5))))); - } - - it should "support 2D vertical stacking and slicing" in { - assume(Mat.hasCUDA > 0); - val a = drand(nr, nc); - val b = drand(nr, nk); - val c = drand(nr, nc); - val aa = GDMat(a); - val bb = GDMat(b); - val cc = GDMat(c); - val dd = aa \ bb \ cc; - val inds = irow(nc -> (nc + nk)); - val ee = dd(?, inds); - ee.mytype should equal ("GDMat"); - checkSimilar(ee, b); - } - - it should "support 2D vertical stacking and colslice" in { - assume(Mat.hasCUDA > 0); - val a = drand(nr, nc); - val b = drand(nr, nk); - val c = drand(nr, nc); - val aa = GDMat(a); - val bb = GDMat(b); - val cc = GDMat(c); - val dd = aa \ bb \ cc; - val ee = dd.colslice(nc, nc+nk); - ee.mytype should equal ("GDMat"); - checkSimilar(ee, b); - } - - it should "support 2D horizontal stacking and slicing" in { - assume(Mat.hasCUDA > 0); - val a = drand(nr, nc); - val b = drand(nk, nc); - val c = drand(nr, nc); - val aa = GDMat(a); - val bb = GDMat(b); - val cc = GDMat(c); - val dd = aa on bb on cc; - val inds = irow(nr -> (nr + nk)); - val ee = dd(inds, ?); - ee.mytype should equal ("GDMat"); - checkSimilar(ee, b); - } - - it should "support IMat indexing" in { - assume(Mat.hasCUDA > 0); - val a = drand(nr, nc); - val aa = GDMat(a); - val ii = iones(nc, 1) * irow(0->nr) + icol(0->nc) * nr; - val bb = aa(ii); - bb.mytype should equal ("GDMat"); - val c = a.t; - checkSimilar(c, bb); - } - - it should "support contents and GDMat linear wildcard" in { - assume(Mat.hasCUDA > 0); - val a = drand(nr \ nc \ nk); - val aa = GDMat(a); - val b = a.contents; - val cc = aa(?); - cc.mytype should equal ("GDMat"); - checkSimilar(cc, b); - } - - it should "support contents and linear wildcard" in { - assume(Mat.hasCUDA > 0); - val a = drand(nr \ nc \ nk); - val aa = GDMat(a); - val bb = aa.contents; - val c = a(?); - bb.mytype should equal ("GDMat"); - checkSimilar(c, bb); - } - - it should "support IMat product access" in { - assume(Mat.hasCUDA > 0); - val a = drand(3 \ 4 \ 5); - val aa = GDMat(a); - val i1 = 1 \ 2; - val i2 = 2 \ 3; - val i3 = 4 \ 3; - val b = dzeros(i1.length \ i2.length \ i3.length); - for (i <- 0 until i1.length) { - for (j <- 0 until i2.length) { - for (k <- 0 until i3.length) { - b.data(i + i1.length * (j + i2.length * k)) = a.data(i1.data(i) + a.dims(0) * (i2.data(j) + a.dims(1) * i3.data(k))); - } - } - } - val cc = aa(i1, i2, i3); - cc.mytype should equal ("GDMat"); - checkSimilar(cc, b); - } - - it should "support IMat product access with wildcard" in { - assume(Mat.hasCUDA > 0); - val a = drand(3 \ 4 \ 5); - val aa = GDMat(a); - val i1 = 1 \ 2; - val i2 = ? - val i3 = 4 \ 3; - val b = dzeros(i1.length \ a.dims(1) \ i3.length); - for (i <- 0 until i1.length) { - for (j <- 0 until a.dims(1)) { - for (k <- 0 until i3.length) { - b.data(i + i1.length * (j + a.dims(1) * k)) = a.data(i1.data(i) + a.dims(0) * (j + a.dims(1) * i3.data(k))); - } - } - } - val cc = aa(i1, i2, i3); - cc.mytype should equal ("GDMat"); - checkSimilar(cc, b); - } - - - it should "support 3D IMat wildcard constant update" in { - assume(Mat.hasCUDA > 0); - val a = drand(3 \ 4 \ 5); - val b = dzeros(a.dims); - val aa = GDMat(a); - val c = 2.0; - for (i <- 0 until a.dims(0)) { - for (j <- 0 until a.dims(1)) { - for (k <- 0 until a.dims(2)) { - val ii = i+ a.dims(0) * (j + a.dims(1) * k); - b.data(ii) = c; - } - } - } - aa(?) = c; - aa.mytype should equal ("GDMat"); - checkSimilar(aa, b); - } - - it should "support 3D IMat wildcard column update" in { - assume(Mat.hasCUDA > 0); - val a = drand(3 \ 4 \ 5); - val b = dzeros(a.dims); - val c = col(0->a.length); - val aa = GDMat(a); - val cc = GDMat(c); - for (i <- 0 until a.dims(0)) { - for (j <- 0 until a.dims(1)) { - for (k <- 0 until a.dims(2)) { - val ii = i+ a.dims(0) * (j + a.dims(1) * k); - b.data(ii) = c.data(ii); - } - } - } - aa(?) = cc; - checkSimilar(aa, b); - } - - it should "support 3D IMat product update" in { - assume(Mat.hasCUDA > 0); - val a = drand(3 \ 4 \ 5); - val c = a + 0.0; - val cc = GDMat(c); - val i1 = 1 \ 2; - val i2 = 2 \ 3; - val i3 = 4 \ 3; - val b = dzeros(i1.length \ i2.length \ i3.length); - b(?) = col(0->b.length); - val bb = GDMat(b); - for (i <- 0 until i1.length) { - for (j <- 0 until i2.length) { - for (k <- 0 until i3.length) { - a.data(i1.data(i) + a.dims(0) * (i2.data(j) + a.dims(1) * i3.data(k))) = b.data(i + i1.length * (j + i2.length * k)); - } - } - } - cc(i1, i2, i3) = bb; - checkSimilar(a, cc); - } - - it should "support 3D IMat product update with wildcard" in { - assume(Mat.hasCUDA > 0); - val a = drand(3 \ 4 \ 5); - val c = a + 0.0; - val cc = GDMat(c); - val i1 = 1 \ 2; - val i2 = ? - val i3 = 4 \ 3; - val b = dzeros(i1.length \ a.dims(1) \ i3.length); - b(?) = col(0->b.length); - val bb = GDMat(b); - for (i <- 0 until i1.length) { - for (j <- 0 until a.dims(1)) { - for (k <- 0 until i3.length) { - a.data(i1.data(i) + a.dims(0) * (j + a.dims(1) * i3.data(k))) = b.data(i + i1.length * (j + a.dims(1) * k)); - } - } - } - cc(i1, i2, i3) = bb; - checkSimilar(a, cc); - } - - it should "support 2D IMat product update" in { - assume(Mat.hasCUDA > 0); - val a = drand(3 \ 4 \ 5); - val cc = GDMat(a); - val i1 = 1 \ 2; - val i2 = 2 \ 3; - val b = dzeros(i1.length \ i2.length); - b(?) = col(0->b.length); - for (i <- 0 until i1.length) { - for (j <- 0 until i2.length) { - a.data(i1.data(i) + a.nrows * i2.data(j)) = b.data(i + i1.length * j); - } - } - cc(i1, i2) = b; - checkSimilar(a, cc); - } - - it should "support 2D IMat product update with wildcard" in { - assume(Mat.hasCUDA > 0); - val a = drand(3 \ 4 \ 5); - val cc = GDMat(a); - val i1 = 1 \ 2; - val i2 = ? - val b = dzeros(i1.length \ a.ncols); - b(?) = col(0->b.length); - for (i <- 0 until b.nrows) { - for (j <- 0 until b.ncols) { - a.data(i1.data(i) + a.nrows * j) = b.data(i + b.nrows * j); - } - } - cc(i1, i2) = b; - checkSimilar(a, cc); - } - - def testReduce2D(reducer:(DMat, Int)=>DMat, fn:(Double, Double)=>Double, axis:Int, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = drand(nr, nc); - val aa = GDMat(a); - val b = if (axis <= 1) { - dzeros(1, nc); - } else { - dzeros(nr, 1); - } - for (i <- 0 until nr) { - for (j <- 0 until nc) { - if (axis <= 1) { - if (i == 0) { - b.data(j) = a.data(i + nr * j); - } else { - b.data(j) = fn(b.data(j), a.data(i + nr * j)); - } - } else { - if (j == 0) { - b.data(i) = a.data(i + nr * j); - } else { - b.data(i) = fn(b.data(i), a.data(i + nr * j)); - } - } - } - } - val cc = reducer(aa, axis); - cc.mytype should equal ("GDMat"); - checkSimilar(b, cc); - } - } - - testReduce2D((a:DMat, n:Int) => sum(a, n), (x:Double, y:Double)=>x+y, 1, "support 2D column sum"); - - testReduce2D((a:DMat, n:Int) => prod(a, n), (x:Double, y:Double)=>x*y, 1, "support 2D column product"); - - testReduce2D((a:DMat, n:Int) => amax(a, n), (x:Double, y:Double)=>math.max(x,y), 1, "support 2D column max"); - - testReduce2D((a:DMat, n:Int) => amin(a, n), (x:Double, y:Double)=>math.min(x,y), 1, "support 2D column min"); - - testReduce2D((a:DMat, n:Int) => sum(a, n), (x:Double, y:Double)=>x+y, 2, "support 2D row sum"); - - testReduce2D((a:DMat, n:Int) => prod(a, n), (x:Double, y:Double)=>x*y, 2, "support 2D row product"); - - testReduce2D((a:DMat, n:Int) => amax(a, n), (x:Double, y:Double)=>math.max(x,y), 2, "support 2D row max"); - - testReduce2D((a:DMat, n:Int) => amin(a, n), (x:Double, y:Double)=>math.min(x,y), 2, "support 2D row min"); - - def testReduce4D(reducer:(DMat, IMat)=>DMat, fn:(Double, Double)=>Double, dims:IMat, msg:String, eps:Double = 1e-4f) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val adims = nr \ nc \ nk \ nl; - val bdims = adims.copy; - bdims(dims) = 1; - val a = drand(adims); - val aa = GDMat(a); - val b = dzeros(bdims); - for (i <- 0 until nr) { - for (j <- 0 until nc) { - for (k <- 0 until nk) { - for (l <- 0 until nl) { - val i0 = if (bdims(0) == 1) 0 else i; - val j0 = if (bdims(1) == 1) 0 else j; - val k0 = if (bdims(2) == 1) 0 else k; - val l0 = if (bdims(3) == 1) 0 else l; - val bi = i0 + bdims(0) * (j0 + bdims(1) * (k0 + bdims(2) * l0)); - val ai = i + nr * (j + nc * (k + nk * l)); - if (((i == 0) || bdims(0) > 1) && ((j == 0) || bdims(1) > 1) && ((k == 0) || bdims(2) > 1) && ((l == 0) || bdims(3) > 1)) { - b.data(bi) = a.data(ai); - } else { - b.data(bi) = fn(b.data(bi), a.data(ai)); - } - } - } - } - } - val cc = reducer(aa, dims); - cc.mytype should equal ("GDMat"); - checkSimilar(b, cc, eps); - } - } - - testReduce4D((a:DMat, n:IMat) => a.sum(n), (x:Double, y:Double)=>x+y, 1\3, "support 4D sum", 1e-2f); - - testReduce4D((a:DMat, n:IMat) => a.amax(n), (x:Double, y:Double)=>math.max(x,y), 1\2, "support 4D max"); - - testReduce4D((a:DMat, n:IMat) => a.amin(n), (x:Double, y:Double)=>math.min(x,y), 0\3, "support 4D min"); - - - it should "support 2D vector accum" in { - assume(Mat.hasCUDA > 0); - val nr = 100; - val nc = 10; - val ne = 1000; - val inds = int(rand(ne,2)*@row(nr,nc)); - val vals = drand(ne,1); - val ginds = GIMat(inds); - val gvals = GDMat(vals); - val c = dzeros(nr, nc); - for (i <- 0 until ne) { - val ii = inds(i, 0); - val jj = inds(i, 1); - val vv = vals(i, 0); - c(ii, jj) = c(ii, jj) + vv; - } - val bb = accum(ginds, gvals, nr, nc); - bb.mytype should equal ("GDMat"); - checkSimilar(bb, c); - } - - it should "support 2D scalar accum" in { - assume(Mat.hasCUDA > 0); - val nr = 100; - val nc = 10; - val ne = 1000; - val inds = int(rand(ne,2)*@row(nr,nc)); - val ginds = GIMat(inds); - val vv = 0.234 - val c = dzeros(nr, nc); - for (i <- 0 until ne) { - val ii = inds(i, 0); - val jj = inds(i, 1); - c(ii, jj) = c(ii, jj) + vv; - } - val bb = accum(ginds, vv, nr, nc); - bb.mytype should equal ("GDMat"); - checkSimilar(bb, c); - } - - it should "support 1D vector accum" in { - assume(Mat.hasCUDA > 0); - val nr = 100; - val ne = 1000; - val inds = int(rand(ne,1)*nr); - val vals = rand(ne,1); - val ginds = GIMat(inds); - val gvals = GDMat(vals); - val c = dzeros(nr, 1); - for (i <- 0 until ne) { - val ii = inds(i, 0); - val vv = vals(i, 0); - c(ii, 0) = c(ii, 0) + vv; - } - val bb = accum(ginds, gvals, nr); - bb.mytype should equal ("GDMat"); - checkSimilar(bb, c); - } - - it should "support 1D scalar accum" in { - assume(Mat.hasCUDA > 0); - val nr = 100; - val ne = 1000; - val inds = int(rand(ne,1)*@nr); - val ginds = GIMat(inds); - val vv = 0.234 - val c = dzeros(nr, 1); - for (i <- 0 until ne) { - val ii = inds(i, 0); - c(ii, 0) = c(ii, 0) + vv; - } - val bb = accum(ginds, vv, nr); - bb.mytype should equal ("GDMat"); - checkSimilar(bb, c); - } - - - it should "support DMat conversion" in { - assume(Mat.hasCUDA > 0); - val nr = 10; - val nc = 20; - val a = drand(nr, nc); - val aa = GDMat(a); - val b = DMat(aa); - aa.mytype should equal ("GDMat"); - b.mytype should equal ("DMat"); - checkSimilar(a, b); - } - - - it should "support GMat conversion" in { - assume(Mat.hasCUDA > 0); - val nr = 10; - val nc = 20; - val a = drand(nr, nc); - val aa = GDMat(a); - val bb = GMat(aa); - val cc = GDMat(bb); - aa.mytype should equal ("GDMat"); - bb.mytype should equal ("GMat"); - checkSimilar(a, cc, 1e-3); - } - - - import org.apache.commons.math3.analysis._ - - import org.apache.commons.math3.analysis.function._ - - import org.apache.commons.math3.special._ - - import org.apache.commons.math3.distribution._ - - def testFunction2D(mop:(DMat)=>DMat, op:(Double)=>Double, offset:Double, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = drand(nr \ nc); - a ~ a + offset; - val aa = GDMat(a); - val b = dzeros(nr \ nc); - for (i <- 0 until a.length) { - b.data(i) = op(a.data(i)); - } - val cc = mop(aa); - cc.mytype should equal ("GDMat"); - checkSimilar(b, cc); - } - } - - def testFunction2Dclass(mop:(DMat)=>DMat, fnclass:UnivariateFunction, offset:Double, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = drand(nr \ nc); - a ~ a + offset; - val aa = GDMat(a); - val b = dzeros(nr \ nc); - for (i <- 0 until a.length) { - b.data(i) = fnclass.value(a.data(i)); - } - val cc = mop(aa); - checkSimilar(b, cc); - } - } - - testFunction2D((a:DMat) => abs(a), (x:Double)=>math.abs(x), -0.5, "support 2D abs function"); - - testFunction2D((a:DMat) => sign(a), (x:Double)=>math.signum(x), -0.5, "support 2D sign function"); - - testFunction2D((a:DMat) => exp(a), (x:Double)=>math.exp(x), -0.5, "support 2D exp function"); - - testFunction2D((a:DMat) => expm1(a), (x:Double)=>math.expm1(x), -0.5, "support 2D expm1 function"); - - testFunction2D((a:DMat) => sqrt(a), (x:Double)=>math.sqrt(x), 0.0, "support 2D sqrt function"); - - testFunction2D((a:DMat) => ln(a), (x:Double)=>math.log(x), 0.0, "support 2D log function"); - - testFunction2D((a:DMat) => log10(a), (x:Double)=>math.log10(x), 0.0, "support 2D log10 function"); - - testFunction2D((a:DMat) => log1p(a), (x:Double)=>math.log1p(x), -0.5, "support 2D log1p function"); - - testFunction2D((a:DMat) => cos(a), (x:Double)=>math.cos(x), -0.5, "support 2D cos function"); - - testFunction2D((a:DMat) => sin(a), (x:Double)=>math.sin(x), -0.5, "support 2D sin function"); - - testFunction2D((a:DMat) => tan(a), (x:Double)=>math.tan(x), -0.5, "support 2D tan function"); - - testFunction2D((a:DMat) => cosh(a), (x:Double)=>math.cosh(x), -0.5, "support 2D cosh function"); - - testFunction2D((a:DMat) => sinh(a), (x:Double)=>math.sinh(x), -0.5, "support 2D sinh function"); - - testFunction2D((a:DMat) => tanh(a), (x:Double)=>math.tanh(x), -0.5, "support 2D tanh function"); - - testFunction2D((a:DMat) => acos(a), (x:Double)=>math.acos(x), -0.5, "support 2D acos function"); - - testFunction2D((a:DMat) => asin(a), (x:Double)=>math.asin(x), -0.5, "support 2D asin function"); - - testFunction2D((a:DMat) => atan(a), (x:Double)=>math.atan(x), -0.5, "support 2D atan function"); - - testFunction2Dclass((a:DMat) => acosh(a), new Acosh(), 1.0, "support 2D acosh function"); - - testFunction2Dclass((a:DMat) => asinh(a), new Asinh(), -0.5, "support 2D asinh function"); - - testFunction2Dclass((a:DMat) => atanh(a), new Atanh(), -0.5, "support 2D atanh function"); - - testFunction2D((a:DMat) => erf(a), (x:Double)=>Erf.erf(x), -0.5, "support 2D erf function"); - - testFunction2D((a:DMat) => erfinv(a), (x:Double)=>Erf.erfInv(x), -0.5, "support 2D erfinv function"); - - testFunction2D((a:DMat) => erfc(a), (x:Double)=>Erf.erfc(x), -0.5, "support 2D erfc function"); - - testFunction2D((a:DMat) => gamma(a), (x:Double)=>Gamma.gamma(x), 0.0, "support 2D gamma function"); - - testFunction2D((a:DMat) => gammaln(a), (x:Double)=>Gamma.logGamma(x), 0.0, "support 2D gammaln function"); - - val _normalDistribution = new NormalDistribution(); - - testFunction2D((a:DMat) => normcdf(a), (x:Double)=>_normalDistribution.cumulativeProbability(x), -0.5, "support 2D normcdf function"); - - testFunction2D((a:DMat) => normcdfinv(a), (x:Double)=>_normalDistribution.inverseCumulativeProbability(x), 0.0, "support 2D normcdfinv function"); - -} - diff --git a/src/test/scala/BIDMat/GIMatTest.scala b/src/test/scala/BIDMat/GIMatTest.scala deleted file mode 100755 index 2de3b81f..00000000 --- a/src/test/scala/BIDMat/GIMatTest.scala +++ /dev/null @@ -1,1019 +0,0 @@ -package BIDMat - -import Mat._ -import MatFunctions._ -import SciFunctions._ -import org.scalatest._; -import org.scalatest.junit._; -import org.scalatest.prop._; -import org.junit.runner.RunWith - -@RunWith(classOf[JUnitRunner]) -class GIMatTest extends BIDMatSpec { - val nr = 10; - val nc = 20; - val nk = 30; - val nl = 40; - - override def beforeAll { - Mat.checkMKL(false) - Mat.checkCUDA(true) - } - - def checkSimilar(aa:IMat, bb:IMat) = { - val a=IMat(aa); - val b=IMat(bb); - a.dims.length should equal (b.dims.length) ; - a.dims.data should equal (b.dims.data); - a.data should equal (b.data); - } - - def irand(nr:Int, nc:Int) = int(100*rand(nr,nc)); - def irand(dims:IMat) = int(100*rand(dims)); - - "A GIMat" should "support matrix transpose" in { - assume(Mat.hasCUDA > 0); - val a = irand(nr, nc); - val b = izeros(nc, nr); - val aa = GIMat(a); - val cc = aa.t; - for (i <- 0 until nr) { - for (j <- 0 until nc) { - b.data(j + i * nc) = a.data(i + j * nr); - } - } - cc.mytype should equal ("GIMat"); - checkSimilar(cc, b); - } - - it should "support matrix transpose in place" in { - val a = irand(nr, nc); - val b = izeros(nc, nr); - val aa = GIMat(a); - val cc = gizeros(nc, nr); - (cc ~ aa).t; - for (i <- 0 until nr) { - for (j <- 0 until nc) { - b.data(j + i * nc) = a.data(i + j * nr); - } - } - checkSimilar(cc, b); - } - - def testEwise(nr:Int, nc:Int, mop:(IMat,IMat)=>IMat, op:(Int,Int)=>Int, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = irand(nr, nc); - val b = irand(nr, nc) + 1; - val aa = GIMat(a); - val bb = GIMat(b); - val cc = mop(aa,bb); - val d = izeros(nr, nc); - for (i <- 0 until nc) { - for (j <- 0 until nr) { - d.data(j + nr * i) = op(a.data(j + nr * i), b.data(j + nr * i)); - } - } - cc.mytype should equal ("GIMat"); - checkSimilar(cc, d); - } - } - - testEwise(nr, nc, (a:IMat, b:IMat) => a + b, (x:Int, y:Int)=>x+y, "support elementwise addition"); - - testEwise(nr, nc, (a:IMat, b:IMat) => a *@ b, (x:Int, y:Int)=>x*y, "support elementwise multiplication"); - - testEwise(nr, nc, (a:IMat, b:IMat) => a - b, (x:Int, y:Int)=>x-y, "support elementwise subtraction"); - - testEwise(nr, nc, (a:IMat, b:IMat) => a / b, (x:Int, y:Int)=>x/y, "support elementwise division"); - - testEwise(nr, nc, (a:IMat, b:IMat) => min(a,b), (x:Int, y:Int)=> math.min(x,y), "support elementwise min"); - - testEwise(nr, nc, (a:IMat, b:IMat) => max(a,b), (x:Int, y:Int)=> math.max(x,y), "support elementwise max"); - - - def testEwiseInPlace(nr:Int, nc:Int, mop:(IMat,IMat,IMat)=>IMat, op:(Int,Int)=>Int, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = irand(nr, nc); - val b = irand(nr, nc) + 1; - val aa = GIMat(a); - val bb = GIMat(b); - val cc = gizeros(nr, nc); - mop(cc,aa,bb); - val d = izeros(nr, nc); - for (i <- 0 until nc) { - for (j <- 0 until nr) { - d.data(j + nr * i) = op(a.data(j + nr * i), b.data(j + nr * i)); - } - } - checkSimilar(cc, d); - } - } - - testEwiseInPlace(nr, nc, (c:IMat, a:IMat, b:IMat) => c ~ a + b, (x:Int, y:Int)=>x+y, "support elementwise addition in place"); - - testEwiseInPlace(nr, nc, (c:IMat, a:IMat, b:IMat) => c ~ a *@ b, (x:Int, y:Int)=>x*y, "support elementwise multiplication in place"); - - testEwiseInPlace(nr, nc, (c:IMat, a:IMat, b:IMat) => c ~ a - b, (x:Int, y:Int)=>x-y, "support elementwise subtraction in place"); - - testEwiseInPlace(nr, nc, (c:IMat, a:IMat, b:IMat) => c ~ a / b, (x:Int, y:Int)=>x/y, "support elementwise division in place"); - - - def testBcastRows(nr:Int, nc:Int, mop:(IMat,IMat)=>IMat, op:(Int,Int)=>Int, msg:String, reverse:Boolean = true) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = irand(nr, nc) + 1; - val b = irand(1, nc) + 1; - val aa = GIMat(a); - val bb = GIMat(b); - val d = izeros(nr, nc); - for (i <- 0 until nc) { - for (j <- 0 until nr) { - d.data(j + i * nr) = op(a.data(j + i * nr), b.data(i)); - } - } - val cc = mop(aa, bb); - cc.mytype should equal ("GIMat"); - checkSimilar(cc, d); - if (reverse) { - val ee = mop(bb, aa); - ee.mytype should equal ("GIMat"); - checkSimilar(ee, d); - } - } - } - - testBcastRows(nr, nc, (a:IMat, b:IMat) => a + b, (x:Int, y:Int)=>x+y, "support addition with broadcast over rows"); - - testBcastRows(nr, nc, (a:IMat, b:IMat) => a *@ b, (x:Int, y:Int)=>x*y, "support multiplication with broadcast over rows"); - - testBcastRows(nr, nc, (a:IMat, b:IMat) => a - b, (x:Int, y:Int)=>x-y, "support subtraction with broadcast over rows", false); - - testBcastRows(nr, nc, (a:IMat, b:IMat) => a / b, (x:Int, y:Int)=>x/y, "support division with broadcast over rows", false); - - testBcastRows(nr, nc, (a:IMat, b:IMat) => min(a,b), (x:Int, y:Int)=> math.min(x,y), "support min with broadcast over rows"); - - testBcastRows(nr, nc, (a:IMat, b:IMat) => max(a,b), (x:Int, y:Int)=> math.max(x,y), "support max with broadcast over rows"); - - def testBcastRowsInPlace(nr:Int, nc:Int, mop:(IMat,IMat,IMat)=>IMat, op:(Int,Int)=>Int, msg:String, reverse:Boolean = true) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = irand(nr, nc) + 1; - val b = irand(1, nc) + 1; - val aa = GIMat(a); - val bb = GIMat(b); - val d = izeros(nr, nc); - val cc = gizeros(nr, nc); - val ee = gizeros(nr, nc); - for (i <- 0 until nc) { - for (j <- 0 until nr) { - d.data(j + i * nr) = op(a.data(j + i * nr), b.data(i)); - } - } - mop(cc, aa, bb); - checkSimilar(cc, d); - if (reverse) { - mop(ee, bb, aa); - checkSimilar(ee, d); - } - } - } - - testBcastRowsInPlace(nr, nc, (c:IMat, a:IMat, b:IMat) => c ~ a + b, (x:Int, y:Int)=>x+y, "support addition with broadcast over rows in place"); - - testBcastRowsInPlace(nr, nc, (c:IMat, a:IMat, b:IMat) => c ~ a *@ b, (x:Int, y:Int)=>x*y, "support multiplication with broadcast over rows in place"); - - testBcastRowsInPlace(nr, nc, (c:IMat, a:IMat, b:IMat) => c ~ a - b, (x:Int, y:Int)=>x-y, "support subtraction with broadcast over rows in place", false); - - testBcastRowsInPlace(nr, nc, (c:IMat, a:IMat, b:IMat) => c ~ a / b, (x:Int, y:Int)=>x/y, "support division with broadcast over rows in place", false); - - def testBcastRows4D(nr:Int, nc:Int, mop:(IMat,IMat)=>IMat, op:(Int,Int)=>Int, msg:String, reverse:Boolean = true) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = irand(nr \ nc \ nk \ nl) + 1; - val b = irand(1 \ 1 \ nk \ nl) + 1; - val aa = GIMat(a); - val bb = GIMat(b); - val d = izeros(a.dims); - for (i <- 0 until nr) { - for (j <- 0 until nc) { - for (k <- 0 until nk) { - for (l <- 0 until nl) { - d.data(i + nr * (j + nc * (k + nk * l))) = op(a.data(i + nr * (j + nc * (k + nk * l))), b.data(k + nk * l)); - } - } - } - } - val cc = mop(aa, bb); - cc.mytype should equal ("GIMat"); - checkSimilar(cc, d); - if (reverse) { - val ee = mop(bb, aa); - ee.mytype should equal ("GIMat"); - checkSimilar(ee, d); - } - } - } - - testBcastRows4D(nr, nc, (a:IMat, b:IMat) => a + b, (x:Int, y:Int)=>x+y, "support addition with broadcast over rows 4D"); - - testBcastRows4D(nr, nc, (a:IMat, b:IMat) => a *@ b, (x:Int, y:Int)=>x*y, "support multiplication with broadcast over rows 4D"); - - testBcastRows4D(nr, nc, (a:IMat, b:IMat) => a - b, (x:Int, y:Int)=>x-y, "support subtraction with broadcast over rows 4D", false); - - testBcastRows4D(nr, nc, (a:IMat, b:IMat) => a / b, (x:Int, y:Int)=>x/y, "support division with broadcast over rows 4D", false); - - testBcastRows4D(nr, nc, (a:IMat, b:IMat) => min(a,b), (x:Int, y:Int)=> math.min(x,y), "support min with broadcast over rows 4D"); - - testBcastRows4D(nr, nc, (a:IMat, b:IMat) => max(a,b), (x:Int, y:Int)=> math.max(x,y), "support max with broadcast over rows 4D"); - - - def testBcastRows4DinPlace(nr:Int, nc:Int, mop:(IMat,IMat,IMat)=>IMat, op:(Int,Int)=>Int, msg:String, reverse:Boolean = true) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = irand(nr \ nc \ nk \ nl) + 1; - val b = irand(1 \ 1 \ nk \ nl) + 1; - val aa= GIMat(a); - val bb = GIMat(b); - val d = izeros(a.dims); - val cc = gizeros(a.dims); - val ee = gizeros(a.dims); - for (i <- 0 until nr) { - for (j <- 0 until nc) { - for (k <- 0 until nk) { - for (l <- 0 until nl) { - d.data(i + nr * (j + nc * (k + nk * l))) = op(a.data(i + nr * (j + nc * (k + nk * l))), b.data(k + nk * l)); - } - } - } - } - mop(cc, aa, bb); - checkSimilar(cc, d); - if (reverse) { - mop(ee, bb, aa); - checkSimilar(ee, d); - } - } - } - - testBcastRows4DinPlace(nr, nc, (c:IMat, a:IMat, b:IMat) => c ~ a + b, (x:Int, y:Int)=>x+y, "support addition with broadcast over rows 4D in place"); - - testBcastRows4DinPlace(nr, nc, (c:IMat, a:IMat, b:IMat) => c ~ a *@ b, (x:Int, y:Int)=>x*y, "support multiplication with broadcast over rows 4D in place"); - - testBcastRows4DinPlace(nr, nc, (c:IMat, a:IMat, b:IMat) => c ~ a - b, (x:Int, y:Int)=>x-y, "support subtraction with broadcast over rows 4D in place", false); - - testBcastRows4DinPlace(nr, nc, (c:IMat, a:IMat, b:IMat) => c ~ a / b, (x:Int, y:Int)=>x/y, "support division with broadcast over rows 4D in place", false); - - - - def testBcastCols(nr:Int, nc:Int, mop:(IMat,IMat)=>IMat, op:(Int,Int)=>Int, msg:String, reverse:Boolean = true) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = irand(nr, nc) + 1; - val b = irand(nr, 1) + 1; - val aa = GIMat(a); - val bb = GIMat(b); - val d = izeros(nr, nc); - for (i <- 0 until nc) { - for (j <- 0 until nr) { - d.data(j + i * nr) = op(a.data(j + i * nr), b.data(j)); - } - } - val cc = mop(aa, bb); - cc.mytype should equal ("GIMat"); - checkSimilar(cc, d); - if (reverse) { - val ee = mop(bb, aa); - ee.mytype should equal ("GIMat"); - checkSimilar(ee, d); - } - } - } - - - testBcastCols(nr, nc, (a:IMat, b:IMat) => a + b, (x:Int, y:Int)=>x+y, "support addition with broadcast over cols"); - - testBcastCols(nr, nc, (a:IMat, b:IMat) => a *@ b, (x:Int, y:Int)=>x*y, "support multiplication with broadcast over cols"); - - testBcastCols(nr, nc, (a:IMat, b:IMat) => a - b, (x:Int, y:Int)=>x-y, "support subtraction with broadcast over cols", false); - - testBcastCols(nr, nc, (a:IMat, b:IMat) => a / b, (x:Int, y:Int)=>x/y, "support division with broadcast over cols", false); - - testBcastCols(nr, nc, (a:IMat, b:IMat) => min(a,b), (x:Int, y:Int)=> math.min(x,y), "support min with broadcast over cols"); - - testBcastCols(nr, nc, (a:IMat, b:IMat) => max(a,b), (x:Int, y:Int)=> math.max(x,y), "support max with broadcast over cols"); - - - - def testBcastColsInPlace(nr:Int, nc:Int, mop:(IMat,IMat,IMat)=>IMat, op:(Int,Int)=>Int, msg:String, reverse:Boolean = true) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = irand(nr, nc) + 1; - val b = irand(nr, 1) + 1; - val aa = GIMat(a); - val bb = GIMat(b); - val d = izeros(nr, nc); - val cc = gizeros(nr, nc); - val ee = gizeros(nr, nc); - for (i <- 0 until nc) { - for (j <- 0 until nr) { - d.data(j + i * nr) = op(a.data(j + i * nr), b.data(j)); - } - } - mop(cc, aa, bb); - checkSimilar(cc, d); - if (reverse) { - mop(ee, bb, aa); - checkSimilar(ee, d); - } - } - } - - - testBcastColsInPlace(nr, nc, (c:IMat, a:IMat, b:IMat) => c ~ a + b, (x:Int, y:Int)=>x+y, "support addition with broadcast over cols in place"); - - testBcastColsInPlace(nr, nc, (c:IMat, a:IMat, b:IMat) => c ~ a *@ b, (x:Int, y:Int)=>x*y, "support multiplication with broadcast over cols in place"); - - testBcastColsInPlace(nr, nc, (c:IMat, a:IMat, b:IMat) => c ~ a - b, (x:Int, y:Int)=>x-y, "support subtraction with broadcast over cols in place", false); - - testBcastColsInPlace(nr, nc, (c:IMat, a:IMat, b:IMat) => c ~ a / b, (x:Int, y:Int)=>x/y, "support division with broadcast over cols in place", false); - - def testScalar1(nr:Int, nc:Int, mop:(Int,IMat)=>IMat, op:(Int,Int)=>Int, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = irand(1, 1).v; - val b = irand(nr, nc); - val bb = GIMat(b); - val d = izeros(nr, nc); - for (i <- 0 until nc) { - for (j <- 0 until nr) { - d.data(j + i * nr) = op(a, b.data(j + i * nr)); - } - } - val cc = mop(a, bb); - cc.mytype should equal ("GIMat"); - checkSimilar(cc, d); - } - } - - def testScalar2(nr:Int, nc:Int, mop:(IMat,Int)=>IMat, op:(Int,Int)=>Int, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = irand(nr, nc); - val b = irand(1, 1).v + 1; - val aa = GIMat(a); - val d = izeros(nr, nc); - for (i <- 0 until nc) { - for (j <- 0 until nr) { - d.data(j + i * nr) = op(a.data(j + i * nr), b); - } - } - val cc = mop(aa, b); - cc.mytype should equal ("GIMat"); - checkSimilar(cc, d); - } - } - - testScalar1(nr, nc, (a:Int, b:IMat) => a + b, (x:Int, y:Int)=>x+y, "support addition of scalar 1"); - - testScalar1(nr, nc, (a:Int, b:IMat) => a *@ b, (x:Int, y:Int)=>x*y, "support multiplication of scalar 1"); - - testScalar1(nr, nc, (a:Int, b:IMat) => min(a, b), (x:Int, y:Int)=>math.min(x,y), "support min of scalar 1"); - - testScalar1(nr, nc, (a:Int, b:IMat) => max(a, b), (x:Int, y:Int)=>math.max(x,y), "support max of scalar 1"); - - testScalar2(nr, nc, (a:IMat, b:Int) => a + b, (x:Int, y:Int)=>x+y, "support addition of scalar 2"); - - testScalar2(nr, nc, (a:IMat, b:Int) => a *@ b, (x:Int, y:Int)=>x*y, "support multiplication of scalar 2"); - - testScalar2(nr, nc, (a:IMat, b:Int) => a - b, (x:Int, y:Int)=>x-y, "support subtraction of scalar 2"); - - testScalar2(nr, nc, (a:IMat, b:Int) => a / b, (x:Int, y:Int)=>x / y, "support division of scalar 2"); - - testScalar2(nr, nc, (a:IMat, b:Int) => min(a, b), (x:Int, y:Int)=> math.min(x,y), "support min of scalar 2"); - - testScalar2(nr, nc, (a:IMat, b:Int) => max(a, b), (x:Int, y:Int)=> math.max(x,y), "support max of scalar 2"); - - - def testScalar1inPlace(nr:Int, nc:Int, mop:(IMat,Int,IMat)=>IMat, op:(Int,Int)=>Int, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = irand(1, 1).v; - val b = irand(nr, nc); - val bb = GIMat(b); - val cc = gizeros(nr, nc); - val d = izeros(nr, nc); - for (i <- 0 until nc) { - for (j <- 0 until nr) { - d.data(j + i * nr) = op(a, b.data(j + i * nr)); - } - } - mop(cc, a, bb); - checkSimilar(cc, d); - } - } - - def testScalar2inPlace(nr:Int, nc:Int, mop:(IMat,IMat,Int)=>IMat, op:(Int,Int)=>Int, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = irand(nr, nc); - val b = irand(1, 1).v + 1; - val aa = GIMat(a); - val cc = gizeros(nr, nc); - val d = izeros(nr, nc); - for (i <- 0 until nc) { - for (j <- 0 until nr) { - d.data(j + i * nr) = op(a.data(j + i * nr), b); - } - } - mop(cc, aa, b); - checkSimilar(cc, d); - } - } - - testScalar1inPlace(nr, nc, (c:IMat, a:Int, b:IMat) => c ~ a + b, (x:Int, y:Int)=>x+y, "support addition of scalar 1 in place"); - - testScalar1inPlace(nr, nc, (c:IMat, a:Int, b:IMat) => c ~ a *@ b, (x:Int, y:Int)=>x*y, "support multiplication of scalar 1 in place"); - - testScalar2inPlace(nr, nc, (c:IMat, a:IMat, b:Int) => c ~ a + b, (x:Int, y:Int)=>x+y, "support addition of scalar 2 in place"); - - testScalar2inPlace(nr, nc, (c:IMat, a:IMat, b:Int) => c ~ a *@ b, (x:Int, y:Int)=>x*y, "support multiplication of scalar 2 in place"); - - testScalar2inPlace(nr, nc, (c:IMat, a:IMat, b:Int) => c ~ a - b, (x:Int, y:Int)=>x-y, "support subtraction of scalar 2 in place"); - - testScalar2inPlace(nr, nc, (c:IMat, a:IMat, b:Int) => c ~ a / b, (x:Int, y:Int)=>x / y, "support division of scalar 2 in place"); - - - def testScalar1ND(nr:Int, nc:Int, mop:(Int,IMat)=>IMat, op:(Int,Int)=>Int, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = irand(1, 1).v; - val b = irand(nr \ nc \ nk); - val bb = GIMat(b); - val d = izeros(nr \ nc \ nk); - for (i <- 0 until nr) { - for (j <- 0 until nc) { - for (k <- 0 until nk) { - d.data(i + nr * (j + nc * k)) = op(a, b.data(i + nr * (j + nc * k))); - } - } - } - val cc = mop(a, bb); - cc.mytype should equal ("GIMat"); - checkSimilar(cc, d); - } - } - - def testScalar2ND(nr:Int, nc:Int, mop:(IMat,Int)=>IMat, op:(Int,Int)=>Int, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = irand(nr \ nc \ nk); - val b = irand(1, 1).v; - val aa = GIMat(a); - val d = izeros(nr \ nc \ nk); - for (i <- 0 until nr) { - for (j <- 0 until nc) { - for (k <- 0 until nk) { - d.data(i + nr * (j + nc * k)) = op(a.data(i + nr * (j + nc * k)), b); - } - } - } - val cc = mop(aa, b); - cc.mytype should equal ("GIMat"); - checkSimilar(cc, d); - } - } - - testScalar1ND(nr, nc, (a:Int, b:IMat) => a + b, (x:Int, y:Int)=>x+y, "support addition of scalar 1 3D"); - - testScalar1ND(nr, nc, (a:Int, b:IMat) => a *@ b, (x:Int, y:Int)=>x*y, "support multiplication of scalar 1 3D"); - - testScalar1ND(nr, nc, (a:Int, b:IMat) => min(a,b), (x:Int, y:Int)=>math.min(x,y), "support min of scalar 1 3D"); - - testScalar1ND(nr, nc, (a:Int, b:IMat) => max(a,b), (x:Int, y:Int)=>math.max(x,y), "support max of scalar 1 3D"); - - testScalar2ND(nr, nc, (a:IMat, b:Int) => a + b, (x:Int, y:Int)=>x+y, "support addition of scalar 2 3D"); - - testScalar2ND(nr, nc, (a:IMat, b:Int) => a *@ b, (x:Int, y:Int)=>x*y, "support multiplication of scalar 2 3D"); - - testScalar2ND(nr, nc, (a:IMat, b:Int) => a - b, (x:Int, y:Int)=>x-y, "support subtraction of scalar 2 3D"); - - testScalar2ND(nr, nc, (a:IMat, b:Int) => a / b, (x:Int, y:Int)=>x / y, "support division of scalar 2 3D"); - - testScalar2ND(nr, nc, (a:IMat, b:Int) => min(a,b), (x:Int, y:Int)=>math.min(x,y), "support min of scalar 2 3D"); - - testScalar2ND(nr, nc, (a:IMat, b:Int) => max(a,b), (x:Int, y:Int)=>math.max(x,y), "support max of scalar 2 3D"); - - - - def testScalar1NDinPlace(nr:Int, nc:Int, mop:(IMat,Int,IMat)=>IMat, op:(Int,Int)=>Int, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = irand(1, 1).v; - val b = irand(nr \ nc \ nk); - val bb = GIMat(b); - val cc = gizeros(b.dims); - val d = izeros(nr \ nc \ nk); - for (i <- 0 until nr) { - for (j <- 0 until nc) { - for (k <- 0 until nk) { - d.data(i + nr * (j + nc * k)) = op(a, b.data(i + nr * (j + nc * k))); - } - } - } - mop(cc, a, bb); - checkSimilar(cc, d); - } - } - - def testScalar2NDinPlace(nr:Int, nc:Int, mop:(IMat,IMat,Int)=>IMat, op:(Int,Int)=>Int, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = irand(nr \ nc \ nk); - val b = irand(1, 1).v; - val aa = GIMat(a); - val cc = gizeros(a.dims); - val d = izeros(nr \ nc \ nk); - for (i <- 0 until nr) { - for (j <- 0 until nc) { - for (k <- 0 until nk) { - d.data(i + nr * (j + nc * k)) = op(a.data(i + nr * (j + nc * k)), b); - } - } - } - mop(cc, aa, b); - checkSimilar(cc, d); - } - } - - testScalar1NDinPlace(nr, nc, (c:IMat, a:Int, b:IMat) => c ~ a + b, (x:Int, y:Int)=>x+y, "support addition of scalar 1 3D in place"); - - testScalar1NDinPlace(nr, nc, (c:IMat, a:Int, b:IMat) => c ~ a *@ b, (x:Int, y:Int)=>x*y, "support multiplication of scalar 1 3D in place"); - - testScalar2NDinPlace(nr, nc, (c:IMat, a:IMat, b:Int) => c ~ a + b, (x:Int, y:Int)=>x+y, "support addition of scalar 2 3D in place"); - - testScalar2NDinPlace(nr, nc, (c:IMat, a:IMat, b:Int) => c ~ a *@ b, (x:Int, y:Int)=>x*y, "support multiplication of scalar 2 3D in place"); - - testScalar2NDinPlace(nr, nc, (c:IMat, a:IMat, b:Int) => c ~ a - b, (x:Int, y:Int)=>x-y, "support subtraction of scalar 2 3D in place"); - - testScalar2NDinPlace(nr, nc, (c:IMat, a:IMat, b:Int) => c ~ a / b, (x:Int, y:Int)=>x / y, "support division of scalar 2 3D in place"); - - - it should "support 1D element access" in { - assume(Mat.hasCUDA > 0); - val a = irand(nr, nc); - val aa = GIMat(a); - aa(5) should equal (a.data(5)); - } - - it should "support 2D element access" in { - assume(Mat.hasCUDA > 0); - val a = irand(nr, nc); - val aa = GIMat(a); - aa(2,3) should equal (a.data(2 + 3 * nr)); - } - - it should "support 3D element access" in { - assume(Mat.hasCUDA > 0); - val a = irand(nr \ nc \ nk); - val aa = GIMat(a); - aa(2, 3, 4) should equal (a.data(2 + 3 * nr + 4 * nr * nc)); - } - - it should "support 4D element access" in { - assume(Mat.hasCUDA > 0); - val a = irand(nr \ nc \ nk \ nl); - val aa = GIMat(a); - aa(2, 3, 4, 5) should equal (a.data(2 + nr * (3 + nc * (4 + nk * 5)))); - } - - it should "support 2D vertical stacking and slicing" in { - assume(Mat.hasCUDA > 0); - val a = irand(nr, nc); - val b = irand(nr, nk); - val c = irand(nr, nc); - val aa = GIMat(a); - val bb = GIMat(b); - val cc = GIMat(c); - val dd = aa \ bb \ cc; - val inds = irow(nc -> (nc + nk)); - val ee = dd(?, inds); - ee.mytype should equal ("GIMat"); - checkSimilar(ee, b); - } - - it should "support 2D vertical stacking and colslice" in { - assume(Mat.hasCUDA > 0); - val a = irand(nr, nc); - val b = irand(nr, nk); - val c = irand(nr, nc); - val aa = GIMat(a); - val bb = GIMat(b); - val cc = GIMat(c); - val dd = aa \ bb \ cc; - val ee = dd.colslice(nc, nc+nk); - ee.mytype should equal ("GIMat"); - checkSimilar(ee, b); - } - - it should "support 2D horizontal stacking and slicing" in { - assume(Mat.hasCUDA > 0); - val a = irand(nr, nc); - val b = irand(nk, nc); - val c = irand(nr, nc); - val aa = GIMat(a); - val bb = GIMat(b); - val cc = GIMat(c); - val dd = aa on bb on cc; - val inds = irow(nr -> (nr + nk)); - val ee = dd(inds, ?); - ee.mytype should equal ("GIMat"); - checkSimilar(ee, b); - } - - it should "support single IMat indexing" in { - assume(Mat.hasCUDA > 0); - val a = irand(nr, nc); - val ii = iones(nc, 1) * irow(0->nr) + icol(0->nc) * nr; - val aa = GIMat(a); - val bb = aa(ii); - val c = a.t; - bb.mytype should equal ("GIMat"); - checkSimilar(c, bb); - } - - it should "support contents and linear wildcard" in { - assume(Mat.hasCUDA > 0); - val a = irand(nr \ nc \ nk); - val aa = GIMat(a); - val bb = aa.contents; - val cc = aa(?); - cc.mytype should equal ("GIMat"); - checkSimilar(cc, bb); - } - - it should "support IMat product access" in { - assume(Mat.hasCUDA > 0); - val a = irand(3 \ 4 \ 5); - val aa = GIMat(a); - val i1 = 1 \ 2; - val i2 = 2 \ 3; - val i3 = 4 \ 3; - val b = izeros(i1.length \ i2.length \ i3.length); - for (i <- 0 until i1.length) { - for (j <- 0 until i2.length) { - for (k <- 0 until i3.length) { - b.data(i + i1.length * (j + i2.length * k)) = a.data(i1.data(i) + a.dims(0) * (i2.data(j) + a.dims(1) * i3.data(k))); - } - } - } - val cc = aa(i1, i2, i3); - cc.mytype should equal ("GIMat"); - checkSimilar(cc, b); - } - - it should "support IMat product access with wildcard" in { - assume(Mat.hasCUDA > 0); - val a = irand(3 \ 4 \ 5); - val aa = GIMat(a); - val i1 = 1 \ 2; - val i2 = ? - val i3 = 4 \ 3; - val b = izeros(i1.length \ a.dims(1) \ i3.length); - for (i <- 0 until i1.length) { - for (j <- 0 until a.dims(1)) { - for (k <- 0 until i3.length) { - b.data(i + i1.length * (j + a.dims(1) * k)) = a.data(i1.data(i) + a.dims(0) * (j + a.dims(1) * i3.data(k))); - } - } - } - val cc = aa(i1, i2, i3); - cc.mytype should equal ("GIMat"); - checkSimilar(cc, b); - } - - it should "support IMat wildcard constant update" in { - assume(Mat.hasCUDA > 0); - val a = irand(3 \ 4 \ 5); - val aa = GIMat(a); - val b = izeros(a.dims); - val c = 2; - for (i <- 0 until a.dims(0)) { - for (j <- 0 until a.dims(1)) { - for (k <- 0 until a.dims(2)) { - val ii = i+ a.dims(0) * (j + a.dims(1) * k); - b.data(ii) = c; - } - } - } - aa(?) = c; - aa.mytype should equal ("GIMat"); - checkSimilar(aa, b); - } - - it should "support IMat wildcard column update" in { - assume(Mat.hasCUDA > 0); - val a = irand(3 \ 4 \ 5); - val b = izeros(a.dims); - val c = icol(0->a.length); - val aa = GIMat(a); - val cc = GIMat(c); - for (i <- 0 until a.dims(0)) { - for (j <- 0 until a.dims(1)) { - for (k <- 0 until a.dims(2)) { - val ii = i+ a.dims(0) * (j + a.dims(1) * k); - b.data(ii) = c.data(ii); - } - } - } - aa(?) = cc; - aa.mytype should equal ("GIMat"); - checkSimilar(aa, b); - } - - it should "support 3D IMat product update" in { - assume(Mat.hasCUDA > 0); - val a = irand(3 \ 4 \ 5); - val c = a + 0; - val i1 = 1 \ 2; - val i2 = 2 \ 3; - val i3 = 4 \ 3; - val b = izeros(i1.length \ i2.length \ i3.length); - b(?) = icol(0->b.length); - val bb = GIMat(b); - val cc = GIMat(c); - for (i <- 0 until i1.length) { - for (j <- 0 until i2.length) { - for (k <- 0 until i3.length) { - a.data(i1.data(i) + a.dims(0) * (i2.data(j) + a.dims(1) * i3.data(k))) = b.data(i + i1.length * (j + i2.length * k)); - } - } - } - cc(i1, i2, i3) = bb; - cc.mytype should equal ("GIMat"); - checkSimilar(a, cc); - } - - it should "support 3D IMat product update with wildcard" in { - assume(Mat.hasCUDA > 0); - val a = irand(3 \ 4 \ 5); - val c = a + 0; - val i1 = 1 \ 2; - val i2 = ? - val i3 = 4 \ 3; - val b = izeros(i1.length \ a.dims(1) \ i3.length); - b(?) = icol(0->b.length); - val bb = GIMat(b); - val cc = GIMat(c); - for (i <- 0 until i1.length) { - for (j <- 0 until a.dims(1)) { - for (k <- 0 until i3.length) { - a.data(i1.data(i) + a.dims(0) * (j + a.dims(1) * i3.data(k))) = b.data(i + i1.length * (j + a.dims(1) * k)); - } - } - } - cc(i1, i2, i3) = bb; - cc.mytype should equal ("GIMat"); - checkSimilar(a, cc); - } - - it should "support 2D IMat product update" in { - assume(Mat.hasCUDA > 0); - val a = irand(3 \ 4 \ 5); - val c = a + 0; - val i1 = 1 \ 2; - val i2 = 2 \ 3; - val b = izeros(i1.length \ i2.length); - b(?) = icol(0->b.length); - val bb = GIMat(b); - val cc = GIMat(c); - for (i <- 0 until i1.length) { - for (j <- 0 until i2.length) { - a.data(i1.data(i) + a.nrows * i2.data(j)) = b.data(i + i1.length * j); - } - } - cc(i1, i2) = bb; - cc.mytype should equal ("GIMat"); - checkSimilar(a, cc); - } - - it should "support 2D IMat product update with wildcard" in { - assume(Mat.hasCUDA > 0); - val a = irand(3 \ 4 \ 5); - val c = a + 0; - val i1 = 1 \ 2; - val i2 = ? - val b = izeros(i1.length \ a.ncols); - b(?) = icol(0->b.length); - val bb = GIMat(b); - val cc = GIMat(c); - for (i <- 0 until i1.length) { - for (j <- 0 until a.ncols) { - a.data(i1.data(i) + a.nrows * j) = b.data(i + i1.length * j); - } - } - cc(i1, i2) = bb; - cc.mytype should equal ("GIMat"); - checkSimilar(a, cc); - } - - it should "support 2D vector accum" in { - assume(Mat.hasCUDA > 0); - val nr = 100; - val nc = 10; - val ne = 1000; - val inds = int(rand(ne,2)*@row(nr,nc)); - val vals = irand(ne,1); - val ginds = GIMat(inds); - val gvals = GIMat(vals); - val c = izeros(nr, nc); - for (i <- 0 until ne) { - val ii = inds(i, 0); - val jj = inds(i, 1); - val vv = vals(i, 0); - c(ii, jj) = c(ii, jj) + vv; - } - val bb = accum(ginds, gvals, nr, nc); - bb.mytype should equal ("GIMat"); - checkSimilar(bb, c); - } - - it should "support 2D scalar accum" in { - assume(Mat.hasCUDA > 0); - val nr = 100; - val nc = 10; - val ne = 1000; - val inds = int(rand(ne,2)*@row(nr,nc)); - val ginds = GIMat(inds); - val vv = 17 - val c = izeros(nr, nc); - for (i <- 0 until ne) { - val ii = inds(i, 0); - val jj = inds(i, 1); - c(ii, jj) = c(ii, jj) + vv; - } - val bb = accum(ginds, vv, nr, nc); - bb.mytype should equal ("GIMat"); - checkSimilar(bb, c); - } - - it should "support 1D vector accum" in { - assume(Mat.hasCUDA > 0); - val nr = 100; - val ne = 1000; - val inds = int(rand(ne,1)*nr); - val vals = irand(ne,1); - val ginds = GIMat(inds); - val gvals = GIMat(vals); - val c = izeros(nr, 1); - for (i <- 0 until ne) { - val ii = inds(i, 0); - val vv = vals(i, 0); - c(ii, 0) = c(ii, 0) + vv; - } - val bb = accum(ginds, gvals, nr); - bb.mytype should equal ("GIMat"); - checkSimilar(bb, c); - } - - it should "support 1D scalar accum" in { - assume(Mat.hasCUDA > 0); - val nr = 100; - val ne = 1000; - val inds = int(rand(ne,1)*@nr); - val ginds = GIMat(inds); - val vv = 19 - val c = izeros(nr, 1); - for (i <- 0 until ne) { - val ii = inds(i, 0); - c(ii, 0) = c(ii, 0) + vv; - } - val bb = accum(ginds, vv, nr); - bb.mytype should equal ("GIMat"); - checkSimilar(bb, c); - } - - def testFunction2D(mop:(IMat)=>IMat, op:(Int)=>Int, offset:Int, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = irand(nr \ nc); - a ~ a + offset; - val b = izeros(nr \ nc); - for (i <- 0 until a.length) { - b.data(i) = op(a.data(i)); - } - val c = mop(a); - checkSimilar(b, c); - } - } - - def testReduce2D(reducer:(IMat, Int)=>IMat, fn:(Int, Int)=>Int, axis:Int, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = irand(nr, nc); - val b = if (axis <= 1) { - izeros(1, nc); - } else { - izeros(nr, 1); - } - for (i <- 0 until nr) { - for (j <- 0 until nc) { - if (axis <= 1) { - if (i == 0) { - b.data(j) = a.data(i + nr * j); - } else { - b.data(j) = fn(b.data(j), a.data(i + nr * j)); - } - } else { - if (j == 0) { - b.data(i) = a.data(i + nr * j); - } else { - b.data(i) = fn(b.data(i), a.data(i + nr * j)); - } - } - } - } - val c = reducer(a, axis); - checkSimilar(b, c); - } - } - - testReduce2D((a:IMat, n:Int) => sum(a, n), (x:Int, y:Int)=>x+y, 1, "support 2D column sum"); - - testReduce2D((a:IMat, n:Int) => prod(a, n), (x:Int, y:Int)=>x*y, 1, "support 2D column product"); - - testReduce2D((a:IMat, n:Int) => amax(a, n), (x:Int, y:Int)=>math.max(x,y), 1, "support 2D column max"); - - testReduce2D((a:IMat, n:Int) => amin(a, n), (x:Int, y:Int)=>math.min(x,y), 1, "support 2D column min"); - - testReduce2D((a:IMat, n:Int) => sum(a, n), (x:Int, y:Int)=>x+y, 2, "support 2D row sum"); - - testReduce2D((a:IMat, n:Int) => prod(a, n), (x:Int, y:Int)=>x*y, 2, "support 2D row product"); - - testReduce2D((a:IMat, n:Int) => amax(a, n), (x:Int, y:Int)=>math.max(x,y), 2, "support 2D row max"); - - testReduce2D((a:IMat, n:Int) => amin(a, n), (x:Int, y:Int)=>math.min(x,y), 2, "support 2D row min"); - - def testReduce4D(reducer:(IMat, IMat)=>IMat, fn:(Int, Int)=>Int, dims:IMat, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val adims = nr \ nc \ nk \ nl; - val bdims = adims.copy; - bdims(dims) = 1; - val a = irand(adims); - val b = izeros(bdims); - for (i <- 0 until nr) { - for (j <- 0 until nc) { - for (k <- 0 until nk) { - for (l <- 0 until nl) { - val i0 = if (bdims(0) == 1) 0 else i; - val j0 = if (bdims(1) == 1) 0 else j; - val k0 = if (bdims(2) == 1) 0 else k; - val l0 = if (bdims(3) == 1) 0 else l; - val bi = i0 + bdims(0) * (j0 + bdims(1) * (k0 + bdims(2) * l0)); - val ai = i + nr * (j + nc * (k + nk * l)); - if (((i == 0) || bdims(0) > 1) && ((j == 0) || bdims(1) > 1) && ((k == 0) || bdims(2) > 1) && ((l == 0) || bdims(3) > 1)) { - b.data(bi) = a.data(ai); - } else { - b.data(bi) = fn(b.data(bi), a.data(ai)); - } - } - } - } - } - val c = reducer(a, dims); - checkSimilar(b, c); - } - } - - testReduce4D((a:IMat, n:IMat) => a.sum(n), (x:Int, y:Int)=>x+y, 1\3, "support 4D sum"); - - testReduce4D((a:IMat, n:IMat) => a.amax(n), (x:Int, y:Int)=>math.max(x,y), 1\2, "support 4D max"); - - testReduce4D((a:IMat, n:IMat) => a.amin(n), (x:Int, y:Int)=>math.min(x,y), 0\3, "support 4D min"); - - it should "support FMat conversion" in { - assume(Mat.hasCUDA > 0); - val nr = 10; - val nc = 20; - val a = irand(nr, nc); - val b = FMat(a); - val c = IMat(b); - b.mytype should equal ("FMat"); - c.mytype should equal ("IMat"); - checkSimilar(a, c); - } - - it should "support DMat conversion" in { - val nr = 10; - val nc = 20; - val a = irand(nr, nc); - val b = DMat(a); - val c = IMat(b); - b.mytype should equal ("DMat"); - c.mytype should equal ("IMat"); - checkSimilar(a, c); - } - -} \ No newline at end of file diff --git a/src/test/scala/BIDMat/GMatTest.scala b/src/test/scala/BIDMat/GMatTest.scala deleted file mode 100644 index f23636d9..00000000 --- a/src/test/scala/BIDMat/GMatTest.scala +++ /dev/null @@ -1,1344 +0,0 @@ -package BIDMat - -import Mat._ -import MatFunctions._ -import SciFunctions._ -import org.scalatest._; -import org.scalatest.junit._; -import org.scalatest.prop._; -import org.junit.runner.RunWith - -@RunWith(classOf[JUnitRunner]) -class GMatTest extends BIDMatSpec { - val nr = 10; - val nc = 20; - val nk = 30; - val nl = 40; - - override def beforeAll { - Mat.checkMKL(false) - Mat.checkCUDA(true) - } - - def checkSimilar(aa:FMat, bb:FMat, eps:Float = 1e-4f) = { - val a = FMat(aa); - val b = FMat(bb); - a.dims.length should equal (b.dims.length) ; - a.dims.data should equal (b.dims.data); - assert_approx_eq(a.data, b.data, eps); - } - - "A GMat" should "support matrix transpose" in { - assume(Mat.hasCUDA > 0); - val a = rand(nr, nc); - val aa = GMat(a); - val b = zeros(nc, nr); - val cc = aa.t; - for (i <- 0 until nr) { - for (j <- 0 until nc) { - b.data(j + i * nc) = a.data(i + j * nr); - } - } - cc.mytype should equal ("GMat"); - checkSimilar(cc, b); - } - - - it should "support matrix multiplication" in { - assume(Mat.hasCUDA > 0); - val a = rand(nr, nk); - val b = rand(nk, nc); - val d = zeros(nr, nc); - val aa = GMat(a); - val bb = GMat(b); - val cc = aa * bb; - for (i <- 0 until nr) { - for (j <- 0 until nc) { - var sum = 0f; - for (k <- 0 until nk) { - sum += a.data(i + k * nr) * b.data(k + j * nk); - } - d.data(i + j * nr) = sum; - } - } - cc.mytype should equal ("GMat"); - checkSimilar(cc, d) - } - - it should "support matrix *^" in { - assume(Mat.hasCUDA > 0); - val a = rand(nr, nk); - val b = rand(nc, nk); - val aa = GMat(a); - val bb = GMat(b); - val c = a * (b.t); - val dd = aa *^ bb; - dd.mytype should equal ("GMat"); - checkSimilar(c, dd) - } - - it should "support matrix ^*" in { - assume(Mat.hasCUDA > 0); - val a = rand(nk, nr); - val b = rand(nk, nc); - val aa = GMat(a); - val bb = GMat(b); - val c = (a.t) * b; - val dd = aa ^* bb; - dd.mytype should equal ("GMat"); - checkSimilar(c, dd) - } - - it should "support matrix dot" in { - assume(Mat.hasCUDA > 0); - val a = rand(nk, nc); - val b = rand(nk, nc); - val aa = GMat(a); - val bb = GMat(b); - val c = a dot b; - val dd = aa dot bb; - dd.mytype should equal ("GMat"); - checkSimilar(c, dd) - } - - it should "support matrix dotr" in { - assume(Mat.hasCUDA > 0); - val a = rand(nk, nc); - val b = rand(nk, nc); - val aa = GMat(a); - val bb = GMat(b); - val c = a dotr b; - val dd = aa dotr bb; - dd.mytype should equal ("GMat"); - checkSimilar(c, dd) - } - - it should "support matrix transpose in place" in { - assume(Mat.hasCUDA > 0); - val a = rand(nr, nc); - val b = rand(nc, nr); - val cc = gzeros(nc, nr); - val aa = GMat(a); - (cc ~ aa) . t; - for (i <- 0 until nr) { - for (j <- 0 until nc) { - b.data(j + i * nc) = a.data(i + j * nr); - } - } - checkSimilar(cc, b); - } - - it should "support matrix multiplication in place" in { - assume(Mat.hasCUDA > 0); - val a = rand(nr, nk); - val b = rand(nk, nc); - val cc = gzeros(nr, nc); - val d = zeros(nr, nc); - val aa = GMat(a); - val bb = GMat(b); - cc ~ aa * bb; - for (i <- 0 until nr) { - for (j <- 0 until nc) { - var sum = 0f; - for (k <- 0 until nk) { - sum += a.data(i + k * nr) * b.data(k + j * nk); - } - d.data(i + j * nr) = sum; - } - } - checkSimilar(cc, d) - } - - it should "support matrix *^ in place" in { - assume(Mat.hasCUDA > 0); - val a = rand(nr, nk); - val b = rand(nc, nk); - val aa = GMat(a); - val bb = GMat(b); - val dd = gzeros(nr, nc); - val c = a * (b.t); - dd ~ aa *^ bb; - checkSimilar(c, dd) - } - - it should "support matrix ^* in place" in { - assume(Mat.hasCUDA > 0); - val a = rand(nk, nr); - val b = rand(nk, nc); - val aa = GMat(a); - val bb = GMat(b); - val dd = gzeros(nr, nc); - val c = (a.t) * b; - dd ~ aa ^* bb; - checkSimilar(c, dd) - } - - def testEwise(nr:Int, nc:Int, mop:(FMat,FMat)=>FMat, op:(Float,Float)=>Float, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = rand(nr, nc); - val b = rand(nr, nc); - val aa = GMat(a); - val bb = GMat(b); - val cc = mop(aa,bb); - val d = zeros(nr, nc); - for (i <- 0 until nc) { - for (j <- 0 until nr) { - d.data(j + nr * i) = op(a.data(j + nr * i), b.data(j + nr * i)); - } - } - cc.mytype should equal ("GMat"); - checkSimilar(cc, d); - } - } - - testEwise(nr, nc, (a:FMat, b:FMat) => a + b, (x:Float, y:Float)=>x+y, "support elementwise addition"); - - testEwise(nr, nc, (a:FMat, b:FMat) => a *@ b, (x:Float, y:Float)=>x*y, "support elementwise multiplication"); - - testEwise(nr, nc, (a:FMat, b:FMat) => a - b, (x:Float, y:Float)=>x-y, "support elementwise subtraction"); - - testEwise(nr, nc, (a:FMat, b:FMat) => a / b, (x:Float, y:Float)=>x/y, "support elementwise division"); - - testEwise(nr, nc, (a:FMat, b:FMat) => min(a,b), (x:Float, y:Float)=> math.min(x,y), "support elementwise min"); - - testEwise(nr, nc, (a:FMat, b:FMat) => max(a,b), (x:Float, y:Float)=> math.max(x,y), "support elementwise max"); - - - def testEwiseInPlace(nr:Int, nc:Int, mop:(FMat,FMat,FMat)=>FMat, op:(Float,Float)=>Float, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = rand(nr, nc); - val b = rand(nr, nc) + 0.01f; - val aa = GMat(a); - val bb = GMat(b); - val cc = gzeros(nr, nc); - mop(cc,aa,bb); - val d = zeros(nr, nc); - for (i <- 0 until nc) { - for (j <- 0 until nr) { - d.data(j + nr * i) = op(a.data(j + nr * i), b.data(j + nr * i)); - } - } - checkSimilar(cc, d); - } - } - - testEwiseInPlace(nr, nc, (c:FMat, a:FMat, b:FMat) => c ~ a + b, (x:Float, y:Float)=>x+y, "support elementwise addition in place"); - - testEwiseInPlace(nr, nc, (c:FMat, a:FMat, b:FMat) => c ~ a *@ b, (x:Float, y:Float)=>x*y, "support elementwise multiplication in place"); - - testEwiseInPlace(nr, nc, (c:FMat, a:FMat, b:FMat) => c ~ a - b, (x:Float, y:Float)=>x-y, "support elementwise subtraction in place"); - - testEwiseInPlace(nr, nc, (c:FMat, a:FMat, b:FMat) => c ~ a / b, (x:Float, y:Float)=>x/y, "support elementwise division in place"); - - - def testBcastRows(nr:Int, nc:Int, mop:(FMat,FMat)=>FMat, op:(Float,Float)=>Float, msg:String, reverse:Boolean = true) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = rand(nr, nc); - val b = rand(1, nc); - val aa = GMat(a); - val bb = GMat(b); - val d = zeros(nr, nc); - for (i <- 0 until nc) { - for (j <- 0 until nr) { - d.data(j + i * nr) = op(a.data(j + i * nr), b.data(i)); - } - } - val cc = mop(aa, bb); - cc.mytype should equal ("GMat"); - checkSimilar(cc, d); - if (reverse) { - val ee = mop(bb, aa); - ee.mytype should equal ("GMat"); - checkSimilar(ee, d); - } - } - } - - testBcastRows(nr, nc, (a:FMat, b:FMat) => a + b, (x:Float, y:Float)=>x+y, "support addition with broadcast over rows"); - - testBcastRows(nr, nc, (a:FMat, b:FMat) => a *@ b, (x:Float, y:Float)=>x*y, "support multiplication with broadcast over rows"); - - testBcastRows(nr, nc, (a:FMat, b:FMat) => a - b, (x:Float, y:Float)=>x-y, "support subtraction with broadcast over rows", false); - - testBcastRows(nr, nc, (a:FMat, b:FMat) => a / b, (x:Float, y:Float)=>x/y, "support division with broadcast over rows", false); - - testBcastRows(nr, nc, (a:FMat, b:FMat) => min(a,b), (x:Float, y:Float)=> math.min(x,y), "support min with broadcast over rows"); - - testBcastRows(nr, nc, (a:FMat, b:FMat) => max(a,b), (x:Float, y:Float)=> math.max(x,y), "support max with broadcast over rows"); - - - def testBcastRowsInPlace(nr:Int, nc:Int, mop:(FMat,FMat,FMat)=>FMat, op:(Float,Float)=>Float, msg:String, reverse:Boolean = true) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = rand(nr, nc) + 0.01f; - val b = rand(1, nc) + 0.01f; - val aa = GMat(a); - val bb = GMat(b); - val d = zeros(nr, nc); - val cc = gzeros(nr, nc); - val ee = gzeros(nr, nc); - for (i <- 0 until nc) { - for (j <- 0 until nr) { - d.data(j + i * nr) = op(a.data(j + i * nr), b.data(i)); - } - } - mop(cc, aa, bb); - checkSimilar(cc, d); - if (reverse) { - mop(ee, bb, aa); - checkSimilar(ee, d); - } - } - } - - testBcastRowsInPlace(nr, nc, (c:FMat, a:FMat, b:FMat) => c ~ a + b, (x:Float, y:Float)=>x+y, "support addition with broadcast over rows in place"); - - testBcastRowsInPlace(nr, nc, (c:FMat, a:FMat, b:FMat) => c ~ a *@ b, (x:Float, y:Float)=>x*y, "support multiplication with broadcast over rows in place"); - - testBcastRowsInPlace(nr, nc, (c:FMat, a:FMat, b:FMat) => c ~ a - b, (x:Float, y:Float)=>x-y, "support subtraction with broadcast over rows in place", false); - - testBcastRowsInPlace(nr, nc, (c:FMat, a:FMat, b:FMat) => c ~ a / b, (x:Float, y:Float)=>x/y, "support division with broadcast over rows in place", false); - - - def testBcastRows4D(nr:Int, nc:Int, mop:(FMat,FMat)=>FMat, op:(Float,Float)=>Float, msg:String, reverse:Boolean = true) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = rand(nr \ nc \ nk \ nl) + 0.01f; - val b = rand(1 \ 1 \ nk \ nl) + 0.01f; - val aa = GMat(a); - val bb = GMat(b); - val d = zeros(a.dims); - for (i <- 0 until nr) { - for (j <- 0 until nc) { - for (k <- 0 until nk) { - for (l <- 0 until nl) { - d.data(i + nr * (j + nc * (k + nk * l))) = op(a.data(i + nr * (j + nc * (k + nk * l))), b.data(k + nk * l)); - } - } - } - } - val cc = mop(aa, bb); - cc.mytype should equal ("GMat"); - checkSimilar(cc, d); - if (reverse) { - val ee = mop(bb, aa); - checkSimilar(ee, d); - } - } - } - - testBcastRows4D(nr, nc, (a:FMat, b:FMat) => a + b, (x:Float, y:Float)=>x+y, "support addition with broadcast over rows 4D"); - - testBcastRows4D(nr, nc, (a:FMat, b:FMat) => a *@ b, (x:Float, y:Float)=>x*y, "support multiplication with broadcast over rows 4D"); - - testBcastRows4D(nr, nc, (a:FMat, b:FMat) => a - b, (x:Float, y:Float)=>x-y, "support subtraction with broadcast over rows 4D", false); - - testBcastRows4D(nr, nc, (a:FMat, b:FMat) => a / b, (x:Float, y:Float)=>x/y, "support division with broadcast over rows 4D", false); - - testBcastRows4D(nr, nc, (a:FMat, b:FMat) => min(a,b), (x:Float, y:Float)=> math.min(x,y), "support min with broadcast over rows 4D"); - - testBcastRows4D(nr, nc, (a:FMat, b:FMat) => max(a,b), (x:Float, y:Float)=> math.max(x,y), "support max with broadcast over rows 4D"); - - def testBcastRows4DinPlace(nr:Int, nc:Int, mop:(FMat,FMat,FMat)=>FMat, op:(Float,Float)=>Float, msg:String, reverse:Boolean = true) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = rand(nr \ nc \ nk \ nl) + 0.01f; - val b = rand(1 \ 1 \ nk \ nl) + 0.01f; - val aa = GMat(a); - val bb = GMat(b); - val d = zeros(a.dims); - val cc = gzeros(a.dims); - val ee = gzeros(a.dims); - for (i <- 0 until nr) { - for (j <- 0 until nc) { - for (k <- 0 until nk) { - for (l <- 0 until nl) { - d.data(i + nr * (j + nc * (k + nk * l))) = op(a.data(i + nr * (j + nc * (k + nk * l))), b.data(k + nk * l)); - } - } - } - } - mop(cc, aa, bb); - checkSimilar(cc, d); - if (reverse) { - mop(ee, bb, aa); - checkSimilar(ee, d); - } - } - } - - testBcastRows4DinPlace(nr, nc, (c:FMat, a:FMat, b:FMat) => c ~ a + b, (x:Float, y:Float)=>x+y, "support addition with broadcast over rows 4D in place"); - - testBcastRows4DinPlace(nr, nc, (c:FMat, a:FMat, b:FMat) => c ~ a *@ b, (x:Float, y:Float)=>x*y, "support multiplication with broadcast over rows 4D in place"); - - testBcastRows4DinPlace(nr, nc, (c:FMat, a:FMat, b:FMat) => c ~ a - b, (x:Float, y:Float)=>x-y, "support subtraction with broadcast over rows 4D in place", false); - - testBcastRows4DinPlace(nr, nc, (c:FMat, a:FMat, b:FMat) => c ~ a / b, (x:Float, y:Float)=>x/y, "support division with broadcast over rows 4D in place", false); - - - - def testBcastCols(nr:Int, nc:Int, mop:(FMat,FMat)=>FMat, op:(Float,Float)=>Float, msg:String, reverse:Boolean = true) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = rand(nr, nc); - val b = rand(nr, 1); - val aa = GMat(a); - val bb = GMat(b); - val d = zeros(nr, nc); - for (i <- 0 until nc) { - for (j <- 0 until nr) { - d.data(j + i * nr) = op(a.data(j + i * nr), b.data(j)); - } - } - val cc = mop(aa, bb); - cc.mytype should equal ("GMat"); - checkSimilar(cc, d); - if (reverse) { - val ee = mop(bb, aa); - ee.mytype should equal ("GMat"); - checkSimilar(ee, d); - } - } - } - - testBcastCols(nr, nc, (a:FMat, b:FMat) => a + b, (x:Float, y:Float)=>x+y, "support addition with broadcast over cols"); - - testBcastCols(nr, nc, (a:FMat, b:FMat) => a *@ b, (x:Float, y:Float)=>x*y, "support multiplication with broadcast over cols"); - - testBcastCols(nr, nc, (a:FMat, b:FMat) => a - b, (x:Float, y:Float)=>x-y, "support subtraction with broadcast over cols", false); - - testBcastCols(nr, nc, (a:FMat, b:FMat) => a / b, (x:Float, y:Float)=>x/y, "support division with broadcast over cols", false); - - testBcastCols(nr, nc, (a:FMat, b:FMat) => min(a,b), (x:Float, y:Float)=> math.min(x,y), "support min with broadcast over cols"); - - testBcastCols(nr, nc, (a:FMat, b:FMat) => max(a,b), (x:Float, y:Float)=> math.max(x,y), "support max with broadcast over cols"); - - - def testBcastColsInPlace(nr:Int, nc:Int, mop:(FMat,FMat,FMat)=>FMat, op:(Float,Float)=>Float, msg:String, reverse:Boolean = true) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = rand(nr, nc) + 0.01f; - val b = rand(nr, 1) + 0.01f; - val aa = GMat(a); - val bb = GMat(b); - val d = zeros(nr, nc); - val cc = gzeros(nr, nc); - val ee = gzeros(nr, nc); - for (i <- 0 until nc) { - for (j <- 0 until nr) { - d.data(j + i * nr) = op(a.data(j + i * nr), b.data(j)); - } - } - mop(cc, aa, bb); - checkSimilar(cc, d); - if (reverse) { - mop(ee, bb, aa); - checkSimilar(ee, d); - } - } - } - - - testBcastColsInPlace(nr, nc, (c:FMat, a:FMat, b:FMat) => c ~ a + b, (x:Float, y:Float)=>x+y, "support addition with broadcast over cols in place"); - - testBcastColsInPlace(nr, nc, (c:FMat, a:FMat, b:FMat) => c ~ a *@ b, (x:Float, y:Float)=>x*y, "support multiplication with broadcast over cols in place"); - - testBcastColsInPlace(nr, nc, (c:FMat, a:FMat, b:FMat) => c ~ a - b, (x:Float, y:Float)=>x-y, "support subtraction with broadcast over cols in place", false); - - testBcastColsInPlace(nr, nc, (c:FMat, a:FMat, b:FMat) => c ~ a / b, (x:Float, y:Float)=>x/y, "support division with broadcast over cols in place", false); - - - def testScalar1(nr:Int, nc:Int, mop:(Float,FMat)=>FMat, op:(Float,Float)=>Float, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = rand(1, 1).fv; - val b = rand(nr, nc); - val bb = GMat(b); - val d = zeros(nr, nc); - for (i <- 0 until nc) { - for (j <- 0 until nr) { - d.data(j + i * nr) = op(a, b.data(j + i * nr)); - } - } - val cc = mop(a, bb); - cc.mytype should equal ("GMat"); - checkSimilar(cc, d); - } - } - - def testScalar2(nr:Int, nc:Int, mop:(FMat,Float)=>FMat, op:(Float,Float)=>Float, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = rand(nr, nc); - val b = rand(1, 1).fv; - val aa = GMat(a); - val d = zeros(nr, nc); - for (i <- 0 until nc) { - for (j <- 0 until nr) { - d.data(j + i * nr) = op(a.data(j + i * nr), b); - } - } - val cc = mop(aa, b); - cc.mytype should equal ("GMat"); - checkSimilar(cc, d); - } - } - - testScalar1(nr, nc, (a:Float, b:FMat) => a + b, (x:Float, y:Float)=>x+y, "support addition of scalar 1"); - - testScalar1(nr, nc, (a:Float, b:FMat) => a *@ b, (x:Float, y:Float)=>x*y, "support multiplication of scalar 1"); - - testScalar1(nr, nc, (a:Float, b:FMat) => min(a, b), (x:Float, y:Float)=>math.min(x,y), "support min of scalar 1"); - - testScalar1(nr, nc, (a:Float, b:FMat) => max(a, b), (x:Float, y:Float)=>math.max(x,y), "support max of scalar 1"); - - testScalar2(nr, nc, (a:FMat, b:Float) => a + b, (x:Float, y:Float)=>x+y, "support addition of scalar 2"); - - testScalar2(nr, nc, (a:FMat, b:Float) => a *@ b, (x:Float, y:Float)=>x*y, "support multiplication of scalar 2"); - - testScalar2(nr, nc, (a:FMat, b:Float) => a - b, (x:Float, y:Float)=>x-y, "support subtraction of scalar 2"); - - testScalar2(nr, nc, (a:FMat, b:Float) => a / b, (x:Float, y:Float)=>x / y, "support division of scalar 2"); - - testScalar2(nr, nc, (a:FMat, b:Float) => min(a, b), (x:Float, y:Float)=> math.min(x,y), "support min of scalar 2"); - - testScalar2(nr, nc, (a:FMat, b:Float) => max(a, b), (x:Float, y:Float)=> math.max(x,y), "support max of scalar 2"); - - - def testScalar1inPlace(nr:Int, nc:Int, mop:(FMat,Float,FMat)=>FMat, op:(Float,Float)=>Float, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = rand(1, 1).fv; - val b = rand(nr, nc); - val bb = GMat(b); - val cc = gzeros(nr, nc); - val d = zeros(nr, nc); - for (i <- 0 until nc) { - for (j <- 0 until nr) { - d.data(j + i * nr) = op(a, b.data(j + i * nr)); - } - } - mop(cc, a, bb); - checkSimilar(cc, d); - } - } - - def testScalar2inPlace(nr:Int, nc:Int, mop:(FMat,FMat,Float)=>FMat, op:(Float,Float)=>Float, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = rand(nr, nc); - val b = rand(1, 1).fv; - val aa = GMat(a); - val cc = gzeros(nr, nc); - val d = zeros(nr, nc); - for (i <- 0 until nc) { - for (j <- 0 until nr) { - d.data(j + i * nr) = op(a.data(j + i * nr), b); - } - } - mop(cc, aa, b); - checkSimilar(cc, d); - } - } - - testScalar1inPlace(nr, nc, (c:FMat, a:Float, b:FMat) => c ~ a + b, (x:Float, y:Float)=>x+y, "support addition of scalar 1 in place"); - - testScalar1inPlace(nr, nc, (c:FMat, a:Float, b:FMat) => c ~ a *@ b, (x:Float, y:Float)=>x*y, "support multiplication of scalar 1 in place"); - - testScalar2inPlace(nr, nc, (c:FMat, a:FMat, b:Float) => c ~ a + b, (x:Float, y:Float)=>x+y, "support addition of scalar 2 in place"); - - testScalar2inPlace(nr, nc, (c:FMat, a:FMat, b:Float) => c ~ a *@ b, (x:Float, y:Float)=>x*y, "support multiplication of scalar 2 in place"); - - testScalar2inPlace(nr, nc, (c:FMat, a:FMat, b:Float) => c ~ a - b, (x:Float, y:Float)=>x-y, "support subtraction of scalar 2 in place"); - - testScalar2inPlace(nr, nc, (c:FMat, a:FMat, b:Float) => c ~ a / b, (x:Float, y:Float)=>x / y, "support division of scalar 2 in place"); - - def testScalar1ND(nr:Int, nc:Int, mop:(Float,FMat)=>FMat, op:(Float,Float)=>Float, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = rand(1, 1).fv; - val b = rand(nr \ nc \ nk); - val bb = GMat(b); - val d = zeros(nr \ nc \ nk); - for (i <- 0 until nr) { - for (j <- 0 until nc) { - for (k <- 0 until nk) { - d.data(i + nr * (j + nc * k)) = op(a, b.data(i + nr * (j + nc * k))); - } - } - } - val cc = mop(a, bb); - cc.mytype should equal ("GMat"); - checkSimilar(cc, d); - } - } - - def testScalar2ND(nr:Int, nc:Int, mop:(FMat,Float)=>FMat, op:(Float,Float)=>Float, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = rand(nr \ nc \ nk); - val b = rand(1, 1).fv; - val aa = GMat(a); - val d = zeros(nr \ nc \ nk); - for (i <- 0 until nr) { - for (j <- 0 until nc) { - for (k <- 0 until nk) { - d.data(i + nr * (j + nc * k)) = op(a.data(i + nr * (j + nc * k)), b); - } - } - } - val cc = mop(aa, b); - cc.mytype should equal ("GMat"); - checkSimilar(cc, d); - } - } - - testScalar1ND(nr, nc, (a:Float, b:FMat) => a + b, (x:Float, y:Float)=>x+y, "support addition of scalar 1 ND"); - - testScalar1ND(nr, nc, (a:Float, b:FMat) => a *@ b, (x:Float, y:Float)=>x*y, "support multiplication of scalar 1 ND"); - - testScalar1ND(nr, nc, (a:Float, b:FMat) => min(a,b), (x:Float, y:Float)=>math.min(x,y), "support min of scalar 1 3D"); - - testScalar1ND(nr, nc, (a:Float, b:FMat) => max(a,b), (x:Float, y:Float)=>math.max(x,y), "support max of scalar 1 3D"); - - - testScalar2ND(nr, nc, (a:FMat, b:Float) => a + b, (x:Float, y:Float)=>x+y, "support addition of scalar 2 ND"); - - testScalar2ND(nr, nc, (a:FMat, b:Float) => a *@ b, (x:Float, y:Float)=>x*y, "support multiplication of scalar 2 ND"); - - testScalar2ND(nr, nc, (a:FMat, b:Float) => a - b, (x:Float, y:Float)=>x-y, "support subtraction of scalar 2 ND"); - - testScalar2ND(nr, nc, (a:FMat, b:Float) => a / b, (x:Float, y:Float)=>x / y, "support division of scalar 2 ND"); - - testScalar2ND(nr, nc, (a:FMat, b:Float) => min(a,b), (x:Float, y:Float)=>math.min(x,y), "support min of scalar 2 3D"); - - testScalar2ND(nr, nc, (a:FMat, b:Float) => max(a,b), (x:Float, y:Float)=>math.max(x,y), "support max of scalar 2 3D"); - - - def testScalar1NDinPlace(nr:Int, nc:Int, mop:(FMat,Float,FMat)=>FMat, op:(Float,Float)=>Float, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = rand(1, 1).fv; - val b = rand(nr \ nc \ nk); - val bb = GMat(b); - val cc = gzeros(b.dims); - val d = zeros(nr \ nc \ nk); - for (i <- 0 until nr) { - for (j <- 0 until nc) { - for (k <- 0 until nk) { - d.data(i + nr * (j + nc * k)) = op(a, b.data(i + nr * (j + nc * k))); - } - } - } - mop(cc, a, bb); - checkSimilar(cc, d); - } - } - - def testScalar2NDinPlace(nr:Int, nc:Int, mop:(FMat,FMat,Float)=>FMat, op:(Float,Float)=>Float, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = rand(nr \ nc \ nk); - val b = rand(1, 1).fv; - val aa = GMat(a); - val cc = gzeros(a.dims); - val d = zeros(nr \ nc \ nk); - for (i <- 0 until nr) { - for (j <- 0 until nc) { - for (k <- 0 until nk) { - d.data(i + nr * (j + nc * k)) = op(a.data(i + nr * (j + nc * k)), b); - } - } - } - mop(cc, aa, b); - checkSimilar(cc, d); - } - } - - testScalar1NDinPlace(nr, nc, (c:FMat, a:Float, b:FMat) => c ~ a + b, (x:Float, y:Float)=>x+y, "support addition of scalar 1 3D in place"); - - testScalar1NDinPlace(nr, nc, (c:FMat, a:Float, b:FMat) => c ~ a *@ b, (x:Float, y:Float)=>x*y, "support multiplication of scalar 1 3D in place"); - - testScalar2NDinPlace(nr, nc, (c:FMat, a:FMat, b:Float) => c ~ a + b, (x:Float, y:Float)=>x+y, "support addition of scalar 2 3D in place"); - - testScalar2NDinPlace(nr, nc, (c:FMat, a:FMat, b:Float) => c ~ a *@ b, (x:Float, y:Float)=>x*y, "support multiplication of scalar 2 3D in place"); - - testScalar2NDinPlace(nr, nc, (c:FMat, a:FMat, b:Float) => c ~ a - b, (x:Float, y:Float)=>x-y, "support subtraction of scalar 2 3D in place"); - - testScalar2NDinPlace(nr, nc, (c:FMat, a:FMat, b:Float) => c ~ a / b, (x:Float, y:Float)=>x / y, "support division of scalar 2 3D in place"); - - - it should "support 1D element access" in { - assume(Mat.hasCUDA > 0); - val a = rand(nr, nc); - val aa = GMat(a); - aa.mytype should equal ("GMat"); - assert_approx_eq(Array(aa(5)), Array(a.data(5))); - } - - it should "support 2D element access" in { - assume(Mat.hasCUDA > 0); - val a = rand(nr, nc); - val aa = GMat(a); - aa.mytype should equal ("GMat"); - assert_approx_eq(Array(aa(2,3)), Array(a.data(2 + 3 * nr))); - } - - it should "support 3D element access" in { - assume(Mat.hasCUDA > 0); - val a = rand(nr \ nc \ nk); - val aa = GMat(a); - aa.mytype should equal ("GMat"); - assert_approx_eq(Array(aa(2, 3, 4)), Array(a.data(2 + 3 * nr + 4 * nr * nc))); - } - - it should "support 4D element access" in { - assume(Mat.hasCUDA > 0); - val a = rand(nr \ nc \ nk \ nl); - val aa = GMat(a); - aa.mytype should equal ("GMat"); - assert_approx_eq(Array(aa(2, 3, 4, 5)), Array(a.data(2 + nr * (3 + nc * (4 + nk * 5))))); - } - - it should "support 2D vertical stacking and slicing" in { - assume(Mat.hasCUDA > 0); - val a = rand(nr, nc); - val b = rand(nr, nk); - val c = rand(nr, nc); - val aa = GMat(a); - val bb = GMat(b); - val cc = GMat(c); - val dd = aa \ bb \ cc; - val inds = irow(nc -> (nc + nk)); - val ee = dd(?, inds); - ee.mytype should equal ("GMat"); - checkSimilar(ee, b); - } - - it should "support 2D vertical stacking and colslice" in { - assume(Mat.hasCUDA > 0); - val a = rand(nr, nc); - val b = rand(nr, nk); - val c = rand(nr, nc); - val aa = GMat(a); - val bb = GMat(b); - val cc = GMat(c); - val dd = aa \ bb \ cc; - val ee = dd.colslice(nc, nc+nk); - ee.mytype should equal ("GMat"); - checkSimilar(ee, b); - } - - it should "support 2D horizontal stacking and slicing" in { - assume(Mat.hasCUDA > 0); - val a = rand(nr, nc); - val b = rand(nk, nc); - val c = rand(nr, nc); - val aa = GMat(a); - val bb = GMat(b); - val cc = GMat(c); - val dd = aa on bb on cc; - val inds = irow(nr -> (nr + nk)); - val ee = dd(inds, ?); - ee.mytype should equal ("GMat"); - checkSimilar(ee, b); - } - - it should "support IMat indexing" in { - assume(Mat.hasCUDA > 0); - val a = rand(nr, nc); - val aa = GMat(a); - val ii = iones(nc, 1) * irow(0->nr) + icol(0->nc) * nr; - val bb = aa(ii); - bb.mytype should equal ("GMat"); - val c = a.t; - checkSimilar(c, bb); - } - - it should "support contents and GMat linear wildcard" in { - assume(Mat.hasCUDA > 0); - val a = rand(nr \ nc \ nk); - val aa = GMat(a); - val b = a.contents; - val cc = aa(?); - cc.mytype should equal ("GMat"); - checkSimilar(cc, b); - } - - it should "support contents and linear wildcard" in { - assume(Mat.hasCUDA > 0); - val a = rand(nr \ nc \ nk); - val aa = GMat(a); - val bb = aa.contents; - val c = a(?); - bb.mytype should equal ("GMat"); - checkSimilar(c, bb); - } - - it should "support IMat product access" in { - assume(Mat.hasCUDA > 0); - val a = rand(3 \ 4 \ 5); - val aa = GMat(a); - val i1 = 1 \ 2; - val i2 = 2 \ 3; - val i3 = 4 \ 3; - val b = zeros(i1.length \ i2.length \ i3.length); - for (i <- 0 until i1.length) { - for (j <- 0 until i2.length) { - for (k <- 0 until i3.length) { - b.data(i + i1.length * (j + i2.length * k)) = a.data(i1.data(i) + a.dims(0) * (i2.data(j) + a.dims(1) * i3.data(k))); - } - } - } - val cc = aa(i1, i2, i3); - cc.mytype should equal ("GMat"); - checkSimilar(cc, b); - } - - it should "support IMat product access with wildcard" in { - assume(Mat.hasCUDA > 0); - val a = rand(3 \ 4 \ 5); - val aa = GMat(a); - val i1 = 1 \ 2; - val i2 = ? - val i3 = 4 \ 3; - val b = zeros(i1.length \ a.dims(1) \ i3.length); - for (i <- 0 until i1.length) { - for (j <- 0 until a.dims(1)) { - for (k <- 0 until i3.length) { - b.data(i + i1.length * (j + a.dims(1) * k)) = a.data(i1.data(i) + a.dims(0) * (j + a.dims(1) * i3.data(k))); - } - } - } - val cc = aa(i1, i2, i3); - cc.mytype should equal ("GMat"); - checkSimilar(cc, b); - } - - - it should "support 3D IMat wildcard constant update" in { - assume(Mat.hasCUDA > 0); - val a = rand(3 \ 4 \ 5); - val b = zeros(a.dims); - val aa = GMat(a); - val c = 2f; - for (i <- 0 until a.dims(0)) { - for (j <- 0 until a.dims(1)) { - for (k <- 0 until a.dims(2)) { - val ii = i+ a.dims(0) * (j + a.dims(1) * k); - b.data(ii) = c; - } - } - } - aa(?) = c; - aa.mytype should equal ("GMat"); - checkSimilar(aa, b); - } - - it should "support 3D IMat wildcard column update" in { - assume(Mat.hasCUDA > 0); - val a = rand(3 \ 4 \ 5); - val b = zeros(a.dims); - val c = col(0->a.length); - val aa = GMat(a); - val cc = GMat(c); - for (i <- 0 until a.dims(0)) { - for (j <- 0 until a.dims(1)) { - for (k <- 0 until a.dims(2)) { - val ii = i+ a.dims(0) * (j + a.dims(1) * k); - b.data(ii) = c.data(ii); - } - } - } - aa(?) = cc; - checkSimilar(aa, b); - } - - it should "support 3D IMat product update" in { - assume(Mat.hasCUDA > 0); - val a = rand(3 \ 4 \ 5); - val c = a + 0f; - val cc = GMat(c); - val i1 = 1 \ 2; - val i2 = 2 \ 3; - val i3 = 4 \ 3; - val b = zeros(i1.length \ i2.length \ i3.length); - b(?) = col(0->b.length); - val bb = GMat(b); - for (i <- 0 until i1.length) { - for (j <- 0 until i2.length) { - for (k <- 0 until i3.length) { - a.data(i1.data(i) + a.dims(0) * (i2.data(j) + a.dims(1) * i3.data(k))) = b.data(i + i1.length * (j + i2.length * k)); - } - } - } - cc(i1, i2, i3) = bb; - checkSimilar(a, cc); - } - - it should "support 3D IMat product update with wildcard" in { - assume(Mat.hasCUDA > 0); - val a = rand(3 \ 4 \ 5); - val c = a + 0f; - val cc = GMat(c); - val i1 = 1 \ 2; - val i2 = ? - val i3 = 4 \ 3; - val b = zeros(i1.length \ a.dims(1) \ i3.length); - b(?) = col(0->b.length); - val bb = GMat(b); - for (i <- 0 until i1.length) { - for (j <- 0 until a.dims(1)) { - for (k <- 0 until i3.length) { - a.data(i1.data(i) + a.dims(0) * (j + a.dims(1) * i3.data(k))) = b.data(i + i1.length * (j + a.dims(1) * k)); - } - } - } - cc(i1, i2, i3) = bb; - checkSimilar(a, cc); - } - - it should "support 2D IMat product update" in { - assume(Mat.hasCUDA > 0); - val a = rand(3 \ 4 \ 5); - val cc = GMat(a); - val i1 = 1 \ 2; - val i2 = 2 \ 3; - val b = zeros(i1.length \ i2.length); - b(?) = col(0->b.length); - for (i <- 0 until i1.length) { - for (j <- 0 until i2.length) { - a.data(i1.data(i) + a.nrows * i2.data(j)) = b.data(i + i1.length * j); - } - } - cc(i1, i2) = b; - checkSimilar(a, cc); - } - - it should "support 2D IMat product update with wildcard" in { - assume(Mat.hasCUDA > 0); - val a = rand(3 \ 4 \ 5); - val cc = GMat(a); - val i1 = 1 \ 2; - val i2 = ? - val b = zeros(i1.length \ a.ncols); - b(?) = col(0->b.length); - for (i <- 0 until b.nrows) { - for (j <- 0 until b.ncols) { - a.data(i1.data(i) + a.nrows * j) = b.data(i + b.nrows * j); - } - } - cc(i1, i2) = b; - checkSimilar(a, cc); - } - - def testReduce2D(reducer:(FMat, Int)=>FMat, fn:(Float, Float)=>Float, axis:Int, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = rand(nr, nc); - val aa = GMat(a); - val b = if (axis <= 1) { - zeros(1, nc); - } else { - zeros(nr, 1); - } - for (i <- 0 until nr) { - for (j <- 0 until nc) { - if (axis <= 1) { - if (i == 0) { - b.data(j) = a.data(i + nr * j); - } else { - b.data(j) = fn(b.data(j), a.data(i + nr * j)); - } - } else { - if (j == 0) { - b.data(i) = a.data(i + nr * j); - } else { - b.data(i) = fn(b.data(i), a.data(i + nr * j)); - } - } - } - } - val cc = reducer(aa, axis); - cc.mytype should equal ("GMat"); - checkSimilar(b, cc); - } - } - - testReduce2D((a:FMat, n:Int) => sum(a, n), (x:Float, y:Float)=>x+y, 1, "support 2D column sum"); - - testReduce2D((a:FMat, n:Int) => prod(a, n), (x:Float, y:Float)=>x*y, 1, "support 2D column product"); - - testReduce2D((a:FMat, n:Int) => amax(a, n), (x:Float, y:Float)=>math.max(x,y), 1, "support 2D column max"); - - testReduce2D((a:FMat, n:Int) => amin(a, n), (x:Float, y:Float)=>math.min(x,y), 1, "support 2D column min"); - - testReduce2D((a:FMat, n:Int) => sum(a, n), (x:Float, y:Float)=>x+y, 2, "support 2D row sum"); - - testReduce2D((a:FMat, n:Int) => prod(a, n), (x:Float, y:Float)=>x*y, 2, "support 2D row product"); - - testReduce2D((a:FMat, n:Int) => amax(a, n), (x:Float, y:Float)=>math.max(x,y), 2, "support 2D row max"); - - testReduce2D((a:FMat, n:Int) => amin(a, n), (x:Float, y:Float)=>math.min(x,y), 2, "support 2D row min"); - - def testReduce4D(reducer:(FMat, IMat)=>FMat, fn:(Float, Float)=>Float, dims:IMat, msg:String, eps:Float = 1e-4f) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val adims = nr \ nc \ nk \ nl; - val bdims = adims.copy; - bdims(dims) = 1; - val a = rand(adims); - val aa = GMat(a); - val b = zeros(bdims); - for (i <- 0 until nr) { - for (j <- 0 until nc) { - for (k <- 0 until nk) { - for (l <- 0 until nl) { - val i0 = if (bdims(0) == 1) 0 else i; - val j0 = if (bdims(1) == 1) 0 else j; - val k0 = if (bdims(2) == 1) 0 else k; - val l0 = if (bdims(3) == 1) 0 else l; - val bi = i0 + bdims(0) * (j0 + bdims(1) * (k0 + bdims(2) * l0)); - val ai = i + nr * (j + nc * (k + nk * l)); - if (((i == 0) || bdims(0) > 1) && ((j == 0) || bdims(1) > 1) && ((k == 0) || bdims(2) > 1) && ((l == 0) || bdims(3) > 1)) { - b.data(bi) = a.data(ai); - } else { - b.data(bi) = fn(b.data(bi), a.data(ai)); - } - } - } - } - } - val cc = reducer(aa, dims); - cc.mytype should equal ("GMat"); - checkSimilar(b, cc, eps); - } - } - - testReduce4D((a:FMat, n:IMat) => a.sum(n), (x:Float, y:Float)=>x+y, 1\3, "support 4D sum", 1e-2f); - - testReduce4D((a:FMat, n:IMat) => a.amax(n), (x:Float, y:Float)=>math.max(x,y), 1\2, "support 4D max"); - - testReduce4D((a:FMat, n:IMat) => a.amin(n), (x:Float, y:Float)=>math.min(x,y), 0\3, "support 4D min"); - - it should "support 2D vector accum" in { - assume(Mat.hasCUDA > 0); - val nr = 100; - val nc = 10; - val ne = 1000; - val inds = int(rand(ne,2)*@row(nr,nc)); - val vals = rand(ne,1); - val ginds = GIMat(inds); - val gvals = GMat(vals); - val c = zeros(nr, nc); - for (i <- 0 until ne) { - val ii = inds(i, 0); - val jj = inds(i, 1); - val vv = vals(i, 0); - c(ii, jj) = c(ii, jj) + vv; - } - val bb = accum(ginds, gvals, nr, nc); - bb.mytype should equal ("GMat"); - checkSimilar(bb, c); - } - - it should "support 2D scalar accum" in { - assume(Mat.hasCUDA > 0); - val nr = 100; - val nc = 10; - val ne = 1000; - val inds = int(rand(ne,2)*@row(nr,nc)); - val ginds = GIMat(inds); - val vv = 0.234f - val c = zeros(nr, nc); - for (i <- 0 until ne) { - val ii = inds(i, 0); - val jj = inds(i, 1); - c(ii, jj) = c(ii, jj) + vv; - } - val bb = accum(ginds, vv, nr, nc); - bb.mytype should equal ("GMat"); - checkSimilar(bb, c); - } - - it should "support 1D vector accum" in { - assume(Mat.hasCUDA > 0); - val nr = 100; - val ne = 1000; - val inds = int(rand(ne,1)*nr); - val vals = rand(ne,1); - val ginds = GIMat(inds); - val gvals = GMat(vals); - val c = zeros(nr, 1); - for (i <- 0 until ne) { - val ii = inds(i, 0); - val vv = vals(i, 0); - c(ii, 0) = c(ii, 0) + vv; - } - val bb = accum(ginds, gvals, nr); - bb.mytype should equal ("GMat"); - checkSimilar(bb, c); - } - - it should "support 1D scalar accum" in { - assume(Mat.hasCUDA > 0); - val nr = 100; - val ne = 1000; - val inds = int(rand(ne,1)*@nr); - val ginds = GIMat(inds); - val vv = 0.234f - val c = zeros(nr, 1); - for (i <- 0 until ne) { - val ii = inds(i, 0); - c(ii, 0) = c(ii, 0) + vv; - } - val bb = accum(ginds, vv, nr); - bb.mytype should equal ("GMat"); - checkSimilar(bb, c); - } - - - it should "support 2D cumsum in columns" in { - assume(Mat.hasCUDA > 0); - val nr = 10; - val nc = 20; - val a = rand(nr, nc); - val c = zeros(nr, nc); - val aa = GMat(a); - for (j <- 0 until nc) { - c(0, j) = a(0, j); - for (i <- 1 until nr) { - c(i, j) = c(i-1, j) + a(i, j); - } - } - val bb = cumsum(aa, 1); - bb.mytype should equal ("GMat"); - checkSimilar(bb, c); - } - - def randomizeCols(a:FMat):FMat = { - val b = a.copy; - val r = rand(a.nrows, a.ncols); - for (j <- 0 until a.ncols) { - for (i <- 0 until a.nrows-1) { - val indx = i + math.min(a.nrows - i - 1, math.floor((b.nrows - i) * r(i, j))).toInt; - val tmp = b(i, j); - b(i, j) = b(indx, j); - b(indx, j) = tmp; - } - } - b; - } - - it should "support 2D sort in columns" in { - assume(Mat.hasCUDA > 0); - val nr = 10; - val nc = 20; - val a = rand(nr, nc); - val b = cumsum(a, 1); - val c = randomizeCols(b); - val cc = GMat(c); - val dd = sort(cc); - dd.mytype should equal ("GMat"); - checkSimilar(b, dd); - } - - def randomizeColsAndInds(a:FMat):(FMat, IMat) = { - val b = a.copy; - val bi = icol(0->b.nrows) * iones(1, b.ncols); - val r = rand(a.nrows, a.ncols); - for (j <- 0 until a.ncols) { - for (i <- 0 until a.nrows-1) { - val indx = i + math.min(a.nrows - i - 1, math.floor((b.nrows - i) * r(i, j))).toInt; - val tmp = b(i, j); - b(i, j) = b(indx, j); - b(indx, j) = tmp; - val itmp = bi(i, j); - bi(i, j) = bi(indx, j); - bi(indx, j) = itmp; - } - } - (b, bi); - } - - it should "support 2D sort2 in columns" in { - assume(Mat.hasCUDA > 0); - val nr = 10; - val nc = 20; - val a = rand(nr, nc); - val b = cumsum(a, 1); - val (c, ci) = randomizeColsAndInds(b); - val cc = GMat(c); - val (dd, ddi) = sort2(cc); - dd.mytype should equal ("GMat"); - val di = IMat(ddi); - checkSimilar(b, dd); - var matches = true; - for (j <- 0 until nc) { - for (i <- 0 until nr) { - matches = matches && (di(ci(i, j), j) == i); - } - } - matches should equal (true); - } - - it should "support FMat conversion" in { - assume(Mat.hasCUDA > 0); - val nr = 10; - val nc = 20; - val a = rand(nr, nc); - val aa = GMat(a); - val b = FMat(a); - aa.mytype should equal ("GMat"); - b.mytype should equal ("FMat"); - checkSimilar(a, b); - } - - - import org.apache.commons.math3.analysis._ - - import org.apache.commons.math3.analysis.function._ - - import org.apache.commons.math3.special._ - - import org.apache.commons.math3.distribution._ - - def testFunction2D(mop:(FMat)=>FMat, op:(Float)=>Float, offset:Float, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = rand(nr \ nc); - a ~ a + offset; - val aa = GMat(a); - val b = zeros(nr \ nc); - for (i <- 0 until a.length) { - b.data(i) = op(a.data(i)); - } - val cc = mop(aa); - cc.mytype should equal ("GMat"); - checkSimilar(b, cc); - } - } - - def testFunction2Dclass(mop:(FMat)=>FMat, fnclass:UnivariateFunction, offset:Float, msg:String) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = rand(nr \ nc); - a ~ a + offset; - val aa = GMat(a); - val b = zeros(nr \ nc); - for (i <- 0 until a.length) { - b.data(i) = fnclass.value(a.data(i)).toFloat; - } - val cc = mop(aa); - checkSimilar(b, cc); - } - } - - def testFunction2Dg(mop:(FMat)=>FMat, offset:Float, msg:String, eps:Float=1e-4f) = { - it should msg in { - assume(Mat.hasCUDA > 0); - val a = rand(nr \ nc); - a ~ a + offset; - val aa = GMat(a); - val b = mop(a); - val bb = mop(aa); - bb.mytype should equal ("GMat"); - checkSimilar(b, bb, eps); - } - } - - testFunction2D((a:FMat) => abs(a), (x:Float)=>math.abs(x), -0.5f, "support 2D abs function"); - - testFunction2D((a:FMat) => sign(a), (x:Float)=>math.signum(x), -0.5f, "support 2D sign function"); - - testFunction2D((a:FMat) => exp(a), (x:Float)=>math.exp(x).toFloat, -0.5f, "support 2D exp function"); - - testFunction2D((a:FMat) => expm1(a), (x:Float)=>math.expm1(x).toFloat, -0.5f, "support 2D expm1 function"); - - testFunction2D((a:FMat) => sqrt(a), (x:Float)=>math.sqrt(x).toFloat, 0f, "support 2D sqrt function"); - - testFunction2D((a:FMat) => ln(a), (x:Float)=>math.log(x).toFloat, 0f, "support 2D log function"); - - testFunction2D((a:FMat) => log10(a), (x:Float)=>math.log10(x).toFloat, 0f, "support 2D log10 function"); - - testFunction2D((a:FMat) => log1p(a), (x:Float)=>math.log1p(x).toFloat, -0.5f, "support 2D log1p function"); - - testFunction2D((a:FMat) => cos(a), (x:Float)=>math.cos(x).toFloat, -0.5f, "support 2D cos function"); - - testFunction2D((a:FMat) => sin(a), (x:Float)=>math.sin(x).toFloat, -0.5f, "support 2D sin function"); - - testFunction2D((a:FMat) => tan(a), (x:Float)=>math.tan(x).toFloat, -0.5f, "support 2D tan function"); - - testFunction2D((a:FMat) => cosh(a), (x:Float)=>math.cosh(x).toFloat, -0.5f, "support 2D cosh function"); - - testFunction2D((a:FMat) => sinh(a), (x:Float)=>math.sinh(x).toFloat, -0.5f, "support 2D sinh function"); - - testFunction2D((a:FMat) => tanh(a), (x:Float)=>math.tanh(x).toFloat, -0.5f, "support 2D tanh function"); - - testFunction2D((a:FMat) => acos(a), (x:Float)=>math.acos(x).toFloat, -0.5f, "support 2D acos function"); - - testFunction2D((a:FMat) => asin(a), (x:Float)=>math.asin(x).toFloat, -0.5f, "support 2D asin function"); - - testFunction2D((a:FMat) => atan(a), (x:Float)=>math.atan(x).toFloat, -0.5f, "support 2D atan function"); - - testFunction2Dclass((a:FMat) => acosh(a), new Acosh(), 1.0f, "support 2D acosh function"); - - testFunction2Dclass((a:FMat) => asinh(a), new Asinh(), -0.5f, "support 2D asinh function"); - - testFunction2Dclass((a:FMat) => atanh(a), new Atanh(), -0.5f, "support 2D atanh function"); - - testFunction2D((a:FMat) => erf(a), (x:Float)=>Erf.erf(x).toFloat, -0.5f, "support 2D erf function"); - - testFunction2D((a:FMat) => erfinv(a), (x:Float)=>Erf.erfInv(x).toFloat, -0.5f, "support 2D erfinv function"); - - testFunction2D((a:FMat) => erfc(a), (x:Float)=>Erf.erfc(x).toFloat, -0.5f, "support 2D erfc function"); - - testFunction2D((a:FMat) => gamma(a), (x:Float)=>Gamma.gamma(x).toFloat, 0f, "support 2D gamma function"); - - testFunction2D((a:FMat) => gammaln(a), (x:Float)=>Gamma.logGamma(x).toFloat, 0f, "support 2D gammaln function"); - - val _normalDistribution = new NormalDistribution(); - - testFunction2D((a:FMat) => normcdf(a), (x:Float)=>_normalDistribution.cumulativeProbability(x).toFloat, -0.5f, "support 2D normcdf function"); - - testFunction2D((a:FMat) => normcdfinv(a), (x:Float)=>_normalDistribution.inverseCumulativeProbability(x).toFloat, 0f, "support 2D normcdfinv function"); - - testFunction2Dg((a:FMat)=> psi(a), 0f, "support 2D psi function", 1e-1f) - - testFunction2Dg((a:FMat)=> psiinv(a), 0f, "support 2D psiinv function", 1e-1f) - - it should "support 4D convolution with NHWC tensors" in { - assume(Mat.hasCUDA > 0); - val a = rand(8\16\16\8); - val b = FFilter2Ddn(3,3,8,8,1,1); - b.xavier; - val aa = GMat(a); - val bb = GFilter(b); - val c = b * a; - val dd = bb * aa; - dd.mytype should equal ("GMat"); - checkSimilar(c, dd); - } - - it should "support 4D convolution with NCHW tensors" in { - assume(Mat.hasCUDA > 0); - val a = rand(8\16\16\8); - val b = FFilter2Ddn(3,3,8,8,1,1); - b.xavier; - val aa = GMat(a); - val bb = GFilter(b); - val aax = aa.fromNHWCtoNCHW; - val bbx = bb.toNCHW; - val c = b * a; - val ddx = (bbx * aax); - val dd = ddx.fromNCHWtoNHWC; - dd.mytype should equal ("GMat"); - checkSimilar(c, dd); - } - - -} - diff --git a/src/test/scala/BIDMat/GSMatTest.scala b/src/test/scala/BIDMat/GSMatTest.scala deleted file mode 100755 index f9dfd3d1..00000000 --- a/src/test/scala/BIDMat/GSMatTest.scala +++ /dev/null @@ -1,318 +0,0 @@ -package BIDMat - -import Mat._ -import MatFunctions._ -import SciFunctions._ -import org.scalatest._; -import org.scalatest.junit._; -import org.scalatest.prop._; -import org.junit.runner.RunWith - -@RunWith(classOf[JUnitRunner]) -class GSMatTest extends BIDMatSpec { - val nr = 10; - val nc = 20; - val nk = 30; - val nl = 40; - - override def beforeAll { - Mat.checkMKL(false) - Mat.checkCUDA(true) - } - - def checkSimilar(a:FMat, b:FMat, eps:Float = 1e-4f):Unit = { - val aa = FMat(a); - val bb = FMat(b); - a.dims.length should equal (b.dims.length) ; - a.dims.data should equal (b.dims.data); - assert_approx_eq(aa.data, bb.data, eps); - } - - def checkSimilar(a:SMat, b:FMat):Unit = { - val aa = SMat(a); - val bb = FMat(b); - aa.check; - checkSimilar(full(aa), bb); - } - - def checkSimilar(a:SMat, b:SMat):Unit = { - val aa = SMat(a); - val bb = SMat(b); - aa.check; - bb.check; - checkSimilar(full(aa), full(bb)); - } - - "A GSMat" should "support matrix transpose" in { - val a = sprand(nr, nc, 0.1f); - val aa = GSMat(a); - val bb = aa.t; - bb.mytype should equal ("GSMat"); - val c = full(a).t; - checkSimilar(SMat(bb), c); - } - - it should "support matrix multiplication" in { - val a = sprand(nr, nk, 0.2f); - val b = rand(nk, nc); - val aa = GSMat(a); - val bb = GMat(b); - val cc = aa * bb; - cc.mytype should equal ("GMat"); - val d = full(a) * b; - checkSimilar(cc, d); - val bt = b.t; - val at = a.t; - val aat = GSMat(at); - val bbt = GMat(bt); - val cct = bbt * aat; - val dt = bt * full(at); - cct.mytype should equal ("GMat"); - checkSimilar(cct, dt); - } - - it should "support matrix *^" in { - val a = rand(nr, nk); - val b = sprand(nc, nk, 0.2f); - val aa = GMat(a); - val bb = GSMat(b); - val cc = aa *^ bb; - val d = a *^ full(b); - cc.mytype should equal ("GMat"); - checkSimilar(cc, d) - } - - it should "support matrix ^*" in { - val a = sprand(nk, nr, 0.2f); - val b = rand(nk, nc); - val aa = GSMat(a); - val bb = GMat(b); - val cc = aa ^* bb; - val d = (full(a).t) * b; - cc.mytype should equal ("GMat"); - checkSimilar(cc, d) - } - - - def testEwise(nr:Int, nc:Int, mop:(SMat,SMat)=>SMat, op:(Float,Float)=>Float, msg:String) = { - it should msg in { - val a = sprand(nr, nc, 0.2f); - val aa = GSMat(a); - val b = a.copy - b.contents <-- rand(b.nnz,1); - val bb = GSMat(b); - val cc = mop(aa,bb); // Sparse-sparse op will remove zeros... - val d = a.copy; - for (i <- 0 until a.nnz) { - d.data(i) = op(a.data(i), b.data(i)); - } - val dd = SMat(d.sparseTrim); - cc.mytype should equal ("SMat"); - checkSimilar(cc, dd); - } - } - - testEwise(nr, nc, (a:SMat, b:SMat) => a + b, (x:Float, y:Float)=>x+y, "support elementwise addition"); - - testEwise(nr, nc, (a:SMat, b:SMat) => a *@ b, (x:Float, y:Float)=>x*y, "support elementwise multiplication"); - - testEwise(nr, nc, (a:SMat, b:SMat) => a - b, (x:Float, y:Float)=>x-y, "support elementwise subtraction"); - - testEwise(nr, nc, (a:SMat, b:SMat) => a / b, (x:Float, y:Float)=>x/y, "support elementwise division"); - - testEwise(nr, nc, (a:SMat, b:SMat) => a > b, (x:Float, y:Float)=> if (x > y) 1.0f else 0f, "support elementwise gt"); - - testEwise(nr, nc, (a:SMat, b:SMat) => a < b, (x:Float, y:Float)=> if (x < y) 1.0f else 0f, "support elementwise lt"); - - testEwise(nr, nc, (a:SMat, b:SMat) => a >= b, (x:Float, y:Float)=> if (x >= y) 1.0f else 0f, "support elementwise ge"); - - testEwise(nr, nc, (a:SMat, b:SMat) => a <= b, (x:Float, y:Float)=> if (x <= y) 1.0f else 0f, "support elementwise le"); - - testEwise(nr, nc, (a:SMat, b:SMat) => a == b, (x:Float, y:Float)=> if (x == y) 1.0f else 0f, "support elementwise eq"); - - testEwise(nr, nc, (a:SMat, b:SMat) => a != b, (x:Float, y:Float)=> if (x != y) 1.0f else 0f, "support elementwise ne"); - - testEwise(nr, nc, (a:SMat, b:SMat) => min(a,b), (x:Float, y:Float)=> math.min(x,y), "support elementwise min"); - - testEwise(nr, nc, (a:SMat, b:SMat) => max(a,b), (x:Float, y:Float)=> math.max(x,y), "support elementwise max"); - - - - def testBcastRows(nr:Int, nc:Int, mop:(SMat,FMat)=>SMat, op:(Float,Float)=>Float, msg:String) = { - it should msg in { - val a = sprand(nr, nc, 0.2f); - val b = rand(1, nc) + 0.01f; - val aa = GSMat(a); - val bb = GMat(b); - val d = a.copy; - for (i <- 0 until nc) { - val j0 = a.jc(i)-Mat.ioneBased; - val j1 = a.jc(i+1)-Mat.ioneBased; - for (j <- j0 until j1) { - d.data(j) = op(a.data(j), b.data(i)); - } - } - val cc = mop(aa, bb); - val dd = SMat(d.sparseTrim); - cc.mytype should equal ("GSMat"); - checkSimilar(cc, dd); - } - } - - testBcastRows(nr, nc, (a:SMat, b:FMat) => a + b, (x:Float, y:Float)=>x+y, "support addition with broadcast over rows"); - - testBcastRows(nr, nc, (a:SMat, b:FMat) => a *@ b, (x:Float, y:Float)=>x*y, "support multiplication with broadcast over rows"); - - testBcastRows(nr, nc, (a:SMat, b:FMat) => a - b, (x:Float, y:Float)=>x-y, "support subtraction with broadcast over rows"); - - testBcastRows(nr, nc, (a:SMat, b:FMat) => a / b, (x:Float, y:Float)=>x/y, "support division with broadcast over rows"); - - testBcastRows(nr, nc, (a:SMat, b:FMat) => a > b, (x:Float, y:Float)=> if (x > y) 1f else 0f, "support > with broadcast over rows"); - - testBcastRows(nr, nc, (a:SMat, b:FMat) => a < b, (x:Float, y:Float)=> if (x < y) 1f else 0f, "support < with broadcast over rows"); - - testBcastRows(nr, nc, (a:SMat, b:FMat) => a >= b, (x:Float, y:Float)=> if (x >= y) 1f else 0f, "support >= with broadcast over rows"); - - testBcastRows(nr, nc, (a:SMat, b:FMat) => a <= b, (x:Float, y:Float)=> if (x <= y) 1f else 0f, "support <= with broadcast over rows"); - - testBcastRows(nr, nc, (a:SMat, b:FMat) => a == b, (x:Float, y:Float)=> if (x == y) 1f else 0f, "support == with broadcast over rows"); - - testBcastRows(nr, nc, (a:SMat, b:FMat) => a != b, (x:Float, y:Float)=> if (x != y) 1f else 0f, "support != with broadcast over rows"); - - - def testBcastCols(nr:Int, nc:Int, mop:(SMat,FMat)=>SMat, op:(Float,Float)=>Float, msg:String, reverse:Boolean = true) = { - it should msg in { - val a = sprand(nr, nc, 0.2f); - val b = rand(nr, 1) + 0.01f; - val aa = GSMat(a); - val bb = GMat(b); - val d = a.copy; - for (i <- 0 until nc) { - val j0 = a.jc(i)-Mat.ioneBased; - val j1 = a.jc(i+1)-Mat.ioneBased; - for (j <- j0 until j1) { - val irow = a.ir(j)-Mat.ioneBased; - d.data(j) = op(a.data(j), b.data(irow)); - } - } - val cc = mop(aa, bb); - val dd = SMat(d.sparseTrim); - cc.mytype should equal ("GSMat"); - checkSimilar(cc, dd); - } - } - - - testBcastCols(nr, nc, (a:SMat, b:FMat) => a + b, (x:Float, y:Float)=>x+y, "support addition with broadcast over cols"); - - testBcastCols(nr, nc, (a:SMat, b:FMat) => a *@ b, (x:Float, y:Float)=>x*y, "support multiplication with broadcast over cols"); - - testBcastCols(nr, nc, (a:SMat, b:FMat) => a - b, (x:Float, y:Float)=>x-y, "support subtraction with broadcast over cols", false); - - testBcastCols(nr, nc, (a:SMat, b:FMat) => a / b, (x:Float, y:Float)=>x/y, "support division with broadcast over cols", false); - - testBcastCols(nr, nc, (a:SMat, b:FMat) => a > b, (x:Float, y:Float)=> if (x > y) 1f else 0f, "support > with broadcast over cols"); - - testBcastCols(nr, nc, (a:SMat, b:FMat) => a < b, (x:Float, y:Float)=> if (x < y) 1f else 0f, "support < with broadcast over cols"); - - testBcastCols(nr, nc, (a:SMat, b:FMat) => a >= b, (x:Float, y:Float)=> if (x >= y) 1f else 0f, "support >= with broadcast over cols"); - - testBcastCols(nr, nc, (a:SMat, b:FMat) => a <= b, (x:Float, y:Float)=> if (x <= y) 1f else 0f, "support <= with broadcast over cols"); - - testBcastCols(nr, nc, (a:SMat, b:FMat) => a == b, (x:Float, y:Float)=> if (x == y) 1f else 0f, "support == with broadcast over cols"); - - testBcastCols(nr, nc, (a:SMat, b:FMat) => a != b, (x:Float, y:Float)=> if (x != y) 1f else 0f, "support != with broadcast over cols"); - - - - def testScalar1(nr:Int, nc:Int, mop:(Float,SMat)=>SMat, op:(Float,Float)=>Float, msg:String) = { - it should msg in { - val a = rand(1, 1).fv; - val b = sprand(nr, nc, 0.2f); - val bb = GSMat(b); - - val d = b.copy; - for (i <- 0 until b.nnz) { - d.data(i) = op(a, b.data(i)); - } - val cc = mop(a, bb); - val dd = SMat(d.sparseTrim); - cc.mytype should equal ("GSMat"); - checkSimilar(cc, dd); - } - } - - def testScalar2(nr:Int, nc:Int, mop:(SMat,Float)=>SMat, op:(Float,Float)=>Float, msg:String) = { - it should msg in { - val a = sprand(nr, nc, 0.2f); - val b = rand(1, 1).fv; - val aa = GSMat(a); - val d = a.copy; - for (i <- 0 until a.nnz) { - d.data(i) = op(a.data(i), b); - } - val cc = mop(aa, b); - val dd = SMat(d.sparseTrim); - cc.mytype should equal ("GSMat"); - checkSimilar(cc, dd); - } - } - -// testScalar1(nr, nc, (a:Float, b:SMat) => a + b, (x:Float, y:Float)=>x+y, "support addition of scalar 1"); - -// testScalar1(nr, nc, (a:Float, b:SMat) => a *@ b, (x:Float, y:Float)=>x*y, "support multiplication of scalar 1"); - - testScalar1(nr, nc, (a:Float, b:SMat) => min(a, b), (x:Float, y:Float)=>math.min(x,y), "support min of scalar 1"); - - testScalar1(nr, nc, (a:Float, b:SMat) => max(a, b), (x:Float, y:Float)=>math.max(x,y), "support max of scalar 1"); - - - testScalar2(nr, nc, (a:SMat, b:Float) => a + b, (x:Float, y:Float)=>x+y, "support addition of scalar 2"); - - testScalar2(nr, nc, (a:SMat, b:Float) => a *@ b, (x:Float, y:Float)=>x*y, "support multiplication of scalar 2"); - - testScalar2(nr, nc, (a:SMat, b:Float) => a - b, (x:Float, y:Float)=>x-y, "support subtraction of scalar 2"); - - testScalar2(nr, nc, (a:SMat, b:Float) => a / b, (x:Float, y:Float)=>x / y, "support division of scalar 2"); - - testScalar2(nr, nc, (a:SMat, b:Float) => min(a, b), (x:Float, y:Float)=> math.min(x,y), "support min of scalar 2"); - - testScalar2(nr, nc, (a:SMat, b:Float) => max(a, b), (x:Float, y:Float)=> math.max(x,y), "support max of scalar 2"); - - - def testReduce2D(reducer:(SMat, Int)=>FMat, fn:(Float, Float)=>Float, axis:Int, initval:Float, msg:String) = { - it should msg in { - val a = sprand(nr, nc, 0.2f); - val aa = GSMat(a); - val b = if (axis <= 1) { - zeros(1, nc); - } else { - zeros(nr, 1); - } -// b.set(initval); - for (i <- 0 until nc) { - val j0 = a.jc(i)-Mat.ioneBased; - val j1 = a.jc(i+1)-Mat.ioneBased; - if (axis <= 1) { - if (j1 > j0) b.data(i) = a.data(j0); - for (j <- j0+1 until j1) { - b.data(i) = fn(b.data(i), a.data(j)); - } - } else { - for (j <- j0 until j1) { - val irow = a.ir(j)-Mat.ioneBased; - b.data(irow) = fn(b.data(irow), a.data(j)); - } - } - } - val cc = reducer(aa, axis); - cc.mytype should equal ("GMat"); - checkSimilar(cc, b); - } - } - - testReduce2D((a:SMat, n:Int) => sum(a, n), (x:Float, y:Float)=>x+y, 1, 0f, "support 2D column sum"); - - testReduce2D((a:SMat, n:Int) => sum(a, n), (x:Float, y:Float)=>x+y, 2, 0f, "support 2D row sum"); - -} diff --git a/src/test/scala/BIDMat/TestHDF5.scala b/src/test/scala/BIDMat/TestHDF5.scala deleted file mode 100755 index 8abed633..00000000 --- a/src/test/scala/BIDMat/TestHDF5.scala +++ /dev/null @@ -1,39 +0,0 @@ -package BIDMat - - -import MatFunctions._ -import SciFunctions._ -import CMat._ - - - -object TestHDF5 { - def main(args: Array[String]) : Unit = { - val n = 50000 - val k = 10 - val l = 1 - val a = rand(n,k) - val b = IMat(l,n) - val c = sprand(10,10,0.1) - val d = CSMat(1,2) - d(0,0) = "test" - d(0,1) = "try" - val fname = "d:\\sentiment\\tmp\\mtest.mat" -// println(a.toString) -// println(b.toString) - saveAs(fname, d, "c") -/* val fid = new java.io.FileInputStream(fname) - var next:Int = 0 - var i = 0 - while (next >= 0) { - var next = fid.read() - if (next > 0) println("buf("+i+")=("+(next + (if (next > 127) -256 else 0))+")") - i += 1 - } - fid.close() */ - val e:CMat = load(fname, "c").asInstanceOf[CMat] - println(e(0,0).asInstanceOf[String]) - println(e(0,1).asInstanceOf[String]) - } -} - From 4b4c355e07dca1bc68a5313eb5d24df7877494bb Mon Sep 17 00:00:00 2001 From: John Canny Date: Fri, 19 Feb 2021 13:21:52 -0800 Subject: [PATCH 2/3] fix startup msgs --- src/main/scala/BIDMat/Mat.scala | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/scala/BIDMat/Mat.scala b/src/main/scala/BIDMat/Mat.scala index 0579fe27..90dbfdfa 100755 --- a/src/main/scala/BIDMat/Mat.scala +++ b/src/main/scala/BIDMat/Mat.scala @@ -983,14 +983,14 @@ object Mat { useBLAS = useMKL; if (useMKL) useMKLRand = (UTILS.hasMKL() == 1); useSTLRand = useMKL & !useMKLRand; - try { - edu.berkeley.bid.LibUtils.loadLibrary("jhdf5") +// try { +// edu.berkeley.bid.LibUtils.loadLibrary("jhdf5") // System.loadLibrary("jhdf5") - } catch { - case _:Throwable => { - println("Cant find native HDF5 library") - } - } +// } catch { +// case _:Throwable => { +// println("Cant find native HDF5 library") +// } +// } } // def checkCUDA:Unit = checkCUDA(false); From 6942294a4173904ec0b4d45699650a42c9425222 Mon Sep 17 00:00:00 2001 From: John Canny Date: Tue, 23 Feb 2021 18:59:42 -0800 Subject: [PATCH 3/3] added ultrasound script --- pom.xml | 5 +++++ scripts/ultra.sc | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 scripts/ultra.sc diff --git a/pom.xml b/pom.xml index 5fc33307..09c78507 100755 --- a/pom.xml +++ b/pom.xml @@ -193,6 +193,11 @@ akka-kryo-serialization_2.11 0.5.0 + + com.pi4j + pi4j-core + 1.3 +