public void copyHtoD() { if (System.getProperty("use_cuda").equals("true")) { if (this.persist == false) this.persist = true; if (this.cPointer != null) { SimpleCuBlas.free(this.cPointer); // JCublas.cublasInit(); // JCublas.cublasFree(this.cPointer); this.cPointer = null; } this.cPointer = SimpleCuBlas.alloc(this.data()); } }
public DMatrix sub(DMatrix other) { assert this.length() == other.length() : System.out.printf("Length is not equal. %d - %d\n", this.length(), other.length()); DMatrix m = new CUDAMatrix(this.rows(), this.columns(), this.toArray()); SimpleCuBlas.axpy(-1.0, other, m); return m; }
public void copyDtoH() { if (System.getProperty("use_cuda").equals("true")) { if (this.cPointer != null) { // JCublas.cublasInit(); SimpleCuBlas.getData(this, this.cPointer, Pointer.to(this.data())); } } }
public static DMatrix randn(int r, int c, boolean _persist) { DMatrix m = new CUDAMatrix(r, c); m.persist = _persist; for (int i = 0; i < r * c; i++) m.put(i, RandomUtils.nextGaussian()); if (m.persist) { m.cPointer = SimpleCuBlas.alloc(m.data()); } return m; }
public void close() { // System.err.printf("close() in CUDAMatrix\n"); if (this.cPointer != null) { // System.out.printf("Releasing the CUDA Pointer\n"); this.persist = false; SimpleCuBlas.free(this.cPointer); // JCublas.cublasInit(); // JCublas.cublasFree(this.cPointer); this.cPointer = null; } }
// result = this*other public DMatrix mmul(boolean tA, boolean tB, DMatrix B, DMatrix C) { int m = tA ? this.columns() : this.rows(); int n = tB ? B.rows() : B.columns(); int k = tA ? this.rows() : this.columns(); int kB = tB ? B.columns() : B.rows(); assert (k == kB); if (C.rows != m || C.columns != n) { if (C != this && C != B) { C.resize(m, n); } else { System.err.printf( "[ALERT] Should not resize result matrix because it is used in-place. But doing it anyway.\n"); } } if (C == this || C == B) { /* actually, blas cannot do multiplications in-place. Therefore, we will fake by * * allocating a temporary object on the side and copy the result later. * */ DMatrix temp = new CUDAMatrix(m, n); if (m == 1) { SimpleCuBlas.gemv(tB, B, this, temp, 1.0, 0.0); } else { SimpleCuBlas.gemm(tA, tB, this, B, temp, 1.0, 0.0); } if (temp.rows() == C.rows() && temp.columns() == C.columns()) SimpleCuBlas.copy(temp, C); else { C.resize(m, n); SimpleCuBlas.copy(temp, C); } } else { if (m == 1) { SimpleCuBlas.gemv(tB, B, this, C, 1.0, 0.0); } else { SimpleCuBlas.gemm(tA, tB, this, B, C, 1.0, 0.0); } } return C; }
public DMatrix sumRows(int startRow, int howMany) { DMatrix sum = DMath.createMatrix(1, this.columns()); DMatrix multiplier = DMath.createOnesMatrix(1, howMany); SimpleCuBlas.cust_gemv(false, this, multiplier, sum, 1.0, 0.0, startRow, howMany); return sum; }
public DMatrix sumRows() { DMatrix sum = DMath.createZerosMatrix(1, this.columns()); DMatrix multiplier = DMath.createOnesMatrix(1, this.rows); SimpleCuBlas.gemv(false, this, multiplier, sum, 1.0, 0.0); return sum; }
public DMatrix fillWithArray(DMatrix other) { assert (this.length() % other.length() == 0); SimpleCuBlas.fillWithArray(other, this); return this; }
public DMatrix invi() { SimpleCuBlas.inverseElements(this); return this; }
public DMatrix addi(DMatrix other) { assert (this.length() == other.length()); // System.out.printf("Using cuda blas\n"); SimpleCuBlas.axpy(1.0, other, this); return this; }
public DMatrix mulRowsi(DMatrix colVector) { assert (this.rows() == colVector.rows() && colVector.columns() == 1); SimpleCuBlas.mulRows(colVector, this); return this; }
public DMatrix muli(DMatrix other) { assert (this.length() == other.length()); SimpleCuBlas.mul(this, other, this); return this; }
public DMatrix mul(DMatrix other) { assert (this.length() == other.length()); DMatrix m = new CUDAMatrix(this.rows(), this.columns()); SimpleCuBlas.mul(this, other, m); return m; }
public DMatrix sqrti() { SimpleCuBlas.sqrt(this); return this; }
public DMatrix sqrt() { DMatrix m = new CUDAMatrix(this.rows, this.columns(), this.toArray()); SimpleCuBlas.sqrt(m); return m; }
public DMatrix subi(DMatrix other) { assert (this.length() == other.length()); SimpleCuBlas.axpy(-1.0, other, this); return this; }
// y = a*X+b public DMatrix addi(double a, DMatrix other) { SimpleCuBlas.axpy(a, other, this); return this; }
public DMatrix sumColumns() { DMatrix sum = DMath.createMatrix(this.rows, 1); DMatrix multiplier = DMath.createOnesMatrix(this.columns, 1); SimpleCuBlas.gemm(false, false, this, multiplier, sum, 1.0, 0.0); return sum; }
public DMatrix mulRows(DMatrix colVector) { assert (this.rows() == colVector.rows() && colVector.columns() == 1); DMatrix m = new CUDAMatrix(this.rows(), this.columns(), this.toArray()); SimpleCuBlas.mulRows(colVector, m); return m; }
public void updateDeviceData() { if (this.cPointer != null) SimpleCuBlas.updateData(this.cPointer, this.data); }
// y = 1*x+y public DMatrix add(DMatrix other) { assert (this.length() == other.length()); DMatrix m = new CUDAMatrix(this.rows, this.columns, this.data()); SimpleCuBlas.axpy(1.0, other, m); return m; }
public DMatrix powi(double v) { SimpleCuBlas.pow(this, v); return this; }
public DMatrix muli(double v) { SimpleCuBlas.scal(this, v); return this; }
public DMatrix inv() { DMatrix m = DMath.createMatrix(this.rows(), this.columns(), this.toArray()); SimpleCuBlas.inverseElements(m); return m; }
public void updateDeviceData(double[] newData) { if (this.cPointer != null) SimpleCuBlas.updateData(this.cPointer, newData); }
public DMatrix pow(double v) { DMatrix m = new CUDAMatrix(this.rows(), this.columns(), this.toArray()); SimpleCuBlas.pow(m, v); return m; }