Ejemplo n.º 1
0
  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());
    }
  }
Ejemplo n.º 2
0
 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;
 }
Ejemplo n.º 3
0
 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()));
     }
   }
 }
Ejemplo n.º 4
0
 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;
 }
Ejemplo n.º 5
0
 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;
   }
 }
Ejemplo n.º 6
0
  // 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;
  }
Ejemplo n.º 7
0
 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;
 }
Ejemplo n.º 8
0
 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;
 }
Ejemplo n.º 9
0
 public DMatrix fillWithArray(DMatrix other) {
   assert (this.length() % other.length() == 0);
   SimpleCuBlas.fillWithArray(other, this);
   return this;
 }
Ejemplo n.º 10
0
 public DMatrix invi() {
   SimpleCuBlas.inverseElements(this);
   return this;
 }
Ejemplo n.º 11
0
 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;
 }
Ejemplo n.º 12
0
 public DMatrix mulRowsi(DMatrix colVector) {
   assert (this.rows() == colVector.rows() && colVector.columns() == 1);
   SimpleCuBlas.mulRows(colVector, this);
   return this;
 }
Ejemplo n.º 13
0
 public DMatrix muli(DMatrix other) {
   assert (this.length() == other.length());
   SimpleCuBlas.mul(this, other, this);
   return this;
 }
Ejemplo n.º 14
0
 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;
 }
Ejemplo n.º 15
0
 public DMatrix sqrti() {
   SimpleCuBlas.sqrt(this);
   return this;
 }
Ejemplo n.º 16
0
 public DMatrix sqrt() {
   DMatrix m = new CUDAMatrix(this.rows, this.columns(), this.toArray());
   SimpleCuBlas.sqrt(m);
   return m;
 }
Ejemplo n.º 17
0
 public DMatrix subi(DMatrix other) {
   assert (this.length() == other.length());
   SimpleCuBlas.axpy(-1.0, other, this);
   return this;
 }
Ejemplo n.º 18
0
 // y = a*X+b
 public DMatrix addi(double a, DMatrix other) {
   SimpleCuBlas.axpy(a, other, this);
   return this;
 }
Ejemplo n.º 19
0
 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;
 }
Ejemplo n.º 20
0
 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;
 }
Ejemplo n.º 21
0
 public void updateDeviceData() {
   if (this.cPointer != null) SimpleCuBlas.updateData(this.cPointer, this.data);
 }
Ejemplo n.º 22
0
 // 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;
 }
Ejemplo n.º 23
0
 public DMatrix powi(double v) {
   SimpleCuBlas.pow(this, v);
   return this;
 }
Ejemplo n.º 24
0
 public DMatrix muli(double v) {
   SimpleCuBlas.scal(this, v);
   return this;
 }
Ejemplo n.º 25
0
 public DMatrix inv() {
   DMatrix m = DMath.createMatrix(this.rows(), this.columns(), this.toArray());
   SimpleCuBlas.inverseElements(m);
   return m;
 }
Ejemplo n.º 26
0
 public void updateDeviceData(double[] newData) {
   if (this.cPointer != null) SimpleCuBlas.updateData(this.cPointer, newData);
 }
Ejemplo n.º 27
0
 public DMatrix pow(double v) {
   DMatrix m = new CUDAMatrix(this.rows(), this.columns(), this.toArray());
   SimpleCuBlas.pow(m, v);
   return m;
 }