Skip to content

Commit

Permalink
fixes to build, saveLibSVM, roc2
Browse files Browse the repository at this point in the history
  • Loading branch information
jcanny committed Nov 15, 2014
1 parent c2405c7 commit 0194a25
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 71 deletions.
40 changes: 20 additions & 20 deletions src/main/scala/BIDMat/GMat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ class GMat(nr:Int, nc:Int, var data:Pointer, val realsize:Int) extends Mat(nr, n
} else {
cublasSgemm('n', 'n', nrows, a.ncols, ncols, 1.0f, data, nrows, a.data, a.nrows, 0f, out.data, nrows)
cudaDeviceSynchronize()
val err = cublasGetError
val err = cudaGetLastError
if (err != 0) {
println("device is %d" format SciFunctions.getGPU)
throw new RuntimeException("Cublas error in * "+err)
Expand All @@ -333,7 +333,7 @@ class GMat(nr:Int, nc:Int, var data:Pointer, val realsize:Int) extends Mat(nr, n
Mat.nflops += 2L * length * a.nrows
cublasSgemm('n', 't', nrows, a.nrows, ncols, 1.0f, data, nrows, a.data, a.nrows, 0f, out.data, nrows)
cudaDeviceSynchronize()
val err = cublasGetError
val err = cudaGetLastError
if (err != 0) {
println("device is %d" format SciFunctions.getGPU)
throw new RuntimeException("Cublas error in xT " + cudaGetErrorString(err))
Expand All @@ -348,7 +348,7 @@ class GMat(nr:Int, nc:Int, var data:Pointer, val realsize:Int) extends Mat(nr, n
Mat.nflops += 2L * length * a.ncols
cublasSgemm('t', 'n', ncols, a.ncols, nrows, 1.0f, data, nrows, a.data, a.nrows, 0f, out.data, out.nrows)
cudaDeviceSynchronize()
val err = cublasGetError
val err = cudaGetLastError
if (err != 0) {
println("device is %d" format SciFunctions.getGPU)
throw new RuntimeException("Cublas error in Tx " + cudaGetErrorString(err))
Expand Down Expand Up @@ -451,7 +451,7 @@ class GMat(nr:Int, nc:Int, var data:Pointer, val realsize:Int) extends Mat(nr, n
case aa:GMat => {
val v = cublasSdot(length, data, 1, aa.data, 1)
cudaDeviceSynchronize()
val err = cublasGetError
val err = cudaGetLastError
if (err != 0) {
println("device is %d" format SciFunctions.getGPU)
throw new RuntimeException("Cublas error in ddot " + cudaGetErrorString(err))
Expand Down Expand Up @@ -485,7 +485,7 @@ class GMat(nr:Int, nc:Int, var data:Pointer, val realsize:Int) extends Mat(nr, n
val out = FMat.newOrCheckFMat(nrows, ncols, a, GUID, "toFMat".##)
cublasGetVector(nrows*ncols, Sizeof.FLOAT, data, 1, Pointer.to(out.data), 1)
cudaDeviceSynchronize()
val err = cublasGetError
val err = cudaGetLastError
if (err != 0) {
println("device is %d" format SciFunctions.getGPU)
throw new RuntimeException("Cublas error in toFMat " + cudaGetErrorString(err))
Expand All @@ -497,7 +497,7 @@ class GMat(nr:Int, nc:Int, var data:Pointer, val realsize:Int) extends Mat(nr, n
// val a = out.recycle(nrows, ncols, 0)
cublasGetVector(nrows*ncols, Sizeof.FLOAT, data, 1, Pointer.to(a.data), 1)
cudaDeviceSynchronize()
val err = cublasGetError
val err = cudaGetLastError
if (err != 0) {
println("device is %d" format SciFunctions.getGPU)
throw new RuntimeException("Cublas error in copyTo " + cudaGetErrorString(err))
Expand All @@ -517,9 +517,9 @@ class GMat(nr:Int, nc:Int, var data:Pointer, val realsize:Int) extends Mat(nr, n
}

def copyFrom(in:FMat):GMat = {
cudaMemcpy(data, Pointer.to(in.data), nrows*ncols*Sizeof.FLOAT, cudaMemcpyKind.cudaMemcpyDeviceToDevice)
cudaMemcpy(data, Pointer.to(in.data), 1L*nrows*ncols*Sizeof.FLOAT, cudaMemcpyKind.cudaMemcpyHostToDevice)
cudaDeviceSynchronize()
val err = cublasGetError
val err = cudaGetLastError
if (err != 0) {
println("device is %d" format SciFunctions.getGPU)
throw new RuntimeException("Cublas error in copyFrom " + cudaGetErrorString(err))
Expand All @@ -529,9 +529,9 @@ class GMat(nr:Int, nc:Int, var data:Pointer, val realsize:Int) extends Mat(nr, n

def copyTo(a:GMat):GMat = {
// val a = out.recycle(nrows, ncols, 0)
cudaMemcpy(a.data, data, length*Sizeof.FLOAT, cudaMemcpyKind.cudaMemcpyDeviceToDevice)
cudaMemcpy(a.data, data, 1L*length*Sizeof.FLOAT, cudaMemcpyKind.cudaMemcpyDeviceToDevice)
cudaDeviceSynchronize()
val err = cublasGetError
val err = cudaGetLastError
if (err != 0) {
println("device is %d" format SciFunctions.getGPU)
throw new RuntimeException("Cublas error in copyTo " + cudaGetErrorString(err))
Expand Down Expand Up @@ -1067,8 +1067,8 @@ object GMat {
cudaDeviceSynchronize()
val err = cudaGetLastError()
if (err != 0) {
println("device is %d" format SciFunctions.getGPU)
throw new RuntimeException("Cuda error in gzeros " + cudaGetErrorString(err))
val gpu = SciFunctions.getGPU
throw new RuntimeException("GPU "+gpu+": Cuda error in gzeros " + cudaGetErrorString(err))
}
out
}
Expand All @@ -1078,7 +1078,7 @@ object GMat {
val one = GMat(FMat.elem(1))
cublasScopy(out.length, one.data, 0, out.data, 1)
cudaDeviceSynchronize()
val err = cublasGetError()
val err = cudaGetLastError()
if (err != 0) {
println("device is %d" format SciFunctions.getGPU)
throw new RuntimeException("Cublas error in gones " + cudaGetErrorString(err))
Expand All @@ -1091,7 +1091,7 @@ object GMat {
if (Mat.debugMem) println("GMat %d %d, %d %f" format (nr, nc, SciFunctions.getGPU, SciFunctions.GPUmem._1))
var err = cublasAlloc(nr*nc, Sizeof.FLOAT, retv.data)
cudaDeviceSynchronize
if (err == 0) err = cublasGetError()
if (err == 0) err = cudaGetLastError()
if (err != 0) throw new RuntimeException("CUDA alloc failed " + cudaGetErrorString(err))
retv
}
Expand All @@ -1101,10 +1101,10 @@ object GMat {
val retv = GMat.newOrCheckGMat(a.nrows, a.ncols, null, a.GUID, SciFunctions.getGPU, "GMat_FMat".##)
cudaMemcpy(retv.data, Pointer.to(a.data), rsize*Sizeof.FLOAT, cudaMemcpyKind.cudaMemcpyHostToDevice)
cudaDeviceSynchronize()
val err = cublasGetError()
val err = cudaGetLastError()
if (err != 0) {
println("device is %d" format SciFunctions.getGPU)
throw new RuntimeException("Cublas error in GMat() " + cudaGetErrorString(err))
throw new RuntimeException("CUDA error in GMat() " + cudaGetErrorString(err))
}
retv
}
Expand Down Expand Up @@ -1146,12 +1146,12 @@ object GMat {

def fromFMat(a:FMat, b:GMat):GMat = {
val bb = GMat.newOrCheckGMat(a.nrows, a.ncols, b, a.GUID, SciFunctions.getGPU, "GMat_fromFMat".##)
cudaMemcpy(bb.data, Pointer.to(a.data), a.length*Sizeof.FLOAT, cudaMemcpyKind.cudaMemcpyHostToDevice)
cudaMemcpy(bb.data, Pointer.to(a.data), a.length*1L*Sizeof.FLOAT, cudaMemcpyKind.cudaMemcpyHostToDevice)
cudaDeviceSynchronize()
var err = cublasGetError()
var err = cudaGetLastError()
if (err != 0) {
println("device is %d" format SciFunctions.getGPU)
throw new RuntimeException("Cublas error in fromFMat " + cudaGetErrorString(err))
throw new RuntimeException("CUDA error in fromFMat " + cudaGetErrorString(err))
}
bb
}
Expand Down Expand Up @@ -1424,7 +1424,7 @@ object GMat {
cublasSgemm('n', if (btrans) 't' else 'n', ni, nj, nk, 1.0f, aa, garows, bb, gbrows, if (k==0) 0f else 1f, cc, gcrows)

cudaDeviceSynchronize
err = cublasGetError
err = cudaGetLastError
if (err != 0) throw new RuntimeException("Cublas error in xG, sgemm "+err)
k += gacols
}
Expand Down
40 changes: 31 additions & 9 deletions src/main/scala/BIDMat/GSMat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ case class GSMat(nr:Int, nc:Int, var nnz0:Int, val ir:Pointer, val ic:Pointer, v
}
val out = GMat.newOrCheckGMat(nrows, a.ncols, omat, GUID, a.GUID, "SDMult".##)
val handle = GSMat.getHandle
val descra = GSMat.getDescr
val descra = GSMat.getDescr
GSMat.initZerosAndOnes
val one = GSMat.myones(SciFunctions.getGPU)
val zero = GSMat.myzeros(SciFunctions.getGPU)
var err = JCusparse.cusparseScsrmm(handle, cusparseOperation.CUSPARSE_OPERATION_TRANSPOSE,
Expand All @@ -179,7 +180,8 @@ case class GSMat(nr:Int, nc:Int, var nnz0:Int, val ir:Pointer, val ic:Pointer, v
}
val out = GMat.newOrCheckGMat(ncols, a.ncols, omat, GUID, a.GUID, "SDMult".##)
val handle = GSMat.getHandle
val descra = GSMat.getDescr
val descra = GSMat.getDescr
GSMat.initZerosAndOnes
val one = GSMat.myones(SciFunctions.getGPU)
val zero = GSMat.myzeros(SciFunctions.getGPU)
var err = JCusparse.cusparseScsrmm(handle, cusparseOperation.CUSPARSE_OPERATION_NON_TRANSPOSE,
Expand Down Expand Up @@ -218,14 +220,15 @@ object GSMat {

def apply(nr:Int, nc:Int, nnzx:Int):GSMat = {
// println("nr, nc, nnz = %d,%d,%d" format (nr,nc,nnz0))
var err=0
val out = new GSMat(nr, nc, nnzx, new Pointer(), new Pointer(), new Pointer(), new Pointer(), nnzx)
if (Mat.debugMem) println("GSMat %d %d %d, %d %f" format (nr, nc, nnzx, SciFunctions.getGPU, SciFunctions.GPUmem._1))
var err = JCublas.cublasAlloc(out.nnz, Sizeof.INT, out.ir)
err = JCublas.cublasAlloc(out.nnz, Sizeof.INT, out.ir)
if (err == 0) err = JCublas.cublasAlloc(out.nnz, Sizeof.INT, out.ic)
if (err == 0) err = JCublas.cublasAlloc(out.ncols+1, Sizeof.INT, out.jc)
if (err == 0) err = JCublas.cublasAlloc(out.nnz, Sizeof.FLOAT, out.data)
cudaDeviceSynchronize
if (err == 0) err = cublasGetError
if (err == 0) err = cudaGetLastError
if (err != 0) {
println("device is %d" format SciFunctions.getGPU)
throw new RuntimeException("Cuda error in GSMat() " + cudaGetErrorString(err))
Expand All @@ -241,6 +244,7 @@ object GSMat {
var myzeros:Array[GMat] = null
var cusparseContextsInitialized = false
var cusparseDescrsInitialized = false
var zeroOnesInitialized = false

def initHandles = {
import BIDMat.SciFunctions._
Expand All @@ -250,21 +254,34 @@ object GSMat {
val thisGPU = getGPU
val nGPUs = Mat.hasCUDA
cusparseContexts = new Array[cusparseHandle](nGPUs)
myzeros = new Array[GMat](nGPUs)
myones = new Array[GMat](nGPUs)
for (i <- 0 until nGPUs) {
setGPU(i)
cusparseContexts(i) = new cusparseHandle()
cusparseCreate(cusparseContexts(i))
myzeros(i) = GMat.zeros(0,0)
myones(i) = GMat.ones(0,0)
}
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._
Expand Down Expand Up @@ -300,8 +317,8 @@ object GSMat {
def fromSMat(a:SMat, b:GSMat):GSMat = {
val out = GSMat.newOrCheckGSMat(a.nrows, a.ncols, a.nnz, b, a.GUID, SciFunctions.getGPU, "fromSMat".##)
out.nnz0 = a.nnz
val handle = GSMat.getHandle
var err = 0
val handle = GSMat.getHandle
cudaMemcpy(out.data, Pointer.to(a.data), a.nnz*Sizeof.FLOAT, cudaMemcpyKind.cudaMemcpyHostToDevice)
if (Mat.ioneBased == 1) {
cudaMemcpy(out.ir, Pointer.to(SparseMat.decInds(a.ir)), a.nnz*Sizeof.INT, cudaMemcpyKind.cudaMemcpyHostToDevice)
Expand All @@ -311,6 +328,11 @@ object GSMat {
cudaMemcpy(out.jc, Pointer.to(a.jc), (a.ncols+1)*Sizeof.INT, cudaMemcpyKind.cudaMemcpyHostToDevice)
}
cudaDeviceSynchronize
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.jc, out.nnz, out.ncols, out.ic, cusparseIndexBase.CUSPARSE_INDEX_BASE_ZERO)
cudaDeviceSynchronize
if (err == 0) err = cudaGetLastError
Expand Down
27 changes: 27 additions & 0 deletions src/main/scala/BIDMat/HMat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,34 @@ object HMat {
fin.close();
(datamat, cmat, wmat);
}

def saveLibSVM(fname:String, data:SMat, labels:SMat):Unit = saveLibSVM(fname, data, labels, null, 0);

def saveLibSVM(fname:String, data:SMat, labels:SMat, weights:FMat):Unit = saveLibSVM(fname, data, labels, weights, 0);

def saveLibSVM(fname:String, data:SMat, labels:SMat, weights:FMat, compressed:Int):Unit = {
val fout = new BufferedWriter (new OutputStreamWriter (getOutputStream(fname, compressed)));
val (ilab, jlab) = find2(labels);
val jc = data.jc;
val ioffset = Mat.ioneBased;
var i = 0;
while (i < ilab.length) {
fout.write(ilab.data(i).toString);
val icol = jlab.data(i);
if (weights.asInstanceOf[AnyRef] != null) {
fout.write(":"+weights.data(icol))
}
var j = jc(icol) - ioffset
while (j < jc(icol+1) - ioffset) {
fout.write(" "+(data.ir(j)-ioffset).toString + ":" + data.data(j).toString);
j += 1
}
fout.write("\n")
i += 1
}
fout.close()
}

def testLoad(fname:String, varname:String, n:Int) = {
val a = new Array[SMat](n)
var ndone = izeros(n,1)
Expand Down
14 changes: 14 additions & 0 deletions src/main/scala/BIDMat/MatFunctions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,16 @@ object MatFunctions {
}
}

def checkCUDAerrors() = {
var err = 0
jcuda.runtime.JCuda.cudaDeviceSynchronize
if (err == 0) err = jcuda.runtime.JCuda.cudaGetLastError
if (err != 0) {
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)

def cols2sparse(rows:IMat, cols:IMat, values:FMat):SMat = cols2sparse(rows, cols, values, true, 0)
Expand Down Expand Up @@ -1594,6 +1604,10 @@ object MatFunctions {

def loadLibSVM(fname:String, nrows:Int, compressed:Int) = HMat.loadLibSVM(fname, nrows, compressed)
def loadLibSVM(fname:String, nrows:Int) = HMat.loadLibSVM(fname, nrows, 0)

def saveLibSVM(fname:String, data:SMat, cats:SMat, weights:FMat, compressed:Int):Unit = HMat.saveLibSVM(fname, data, cats, weights, compressed)
def saveLibSVM(fname:String, data:SMat, cats:SMat, weights:FMat):Unit = HMat.saveLibSVM(fname, data, cats, weights, 0)
def saveLibSVM(fname:String, data:SMat, cats:SMat):Unit = HMat.saveLibSVM(fname, data, cats, null, 0)

final val ? = new IMatWildcard
}
Expand Down
Loading

0 comments on commit 0194a25

Please sign in to comment.