<commit_before>package BIDMat

import edu.berkeley.bid.SPBLAS._
import edu.berkeley.bid.UTILS._

case class SMat(nr:Int, nc:Int, nnz1:Int, ir0:Array[Int], jc0:Array[Int], data0:Array[Float]) extends SparseMat[Float](nr, nc, nnz1, ir0, jc0, data0) {

  def getdata() = data;	
  
  override def t:SMat = SMat(gt)
  
  override def mytype = "SMat"
  
  def horzcat(b: SMat) = SMat(super.horzcat(b))
  
  def vertcat(b: SMat) = SMat(super.vertcat(b))
  
  def find:IMat = IMat(gfind)
  
  def find2:(IMat, IMat) = { val (ii, jj) = gfind2 ; (IMat(ii), IMat(jj)) }
  
  def find3:(IMat, IMat, FMat) = { val (ii, jj, vv) = gfind3 ; (IMat(ii), IMat(jj), FMat(vv)) }	
  
  override def contents:FMat = FMat(nnz, 1, this.data)
  
  override def apply(a:IMat, b:IMat):SMat = SMat(gapply(a, b))	
  
  override def apply(a:Int, b:IMat):SMat = SMat(gapply(a, b))	
  
  override def apply(a:IMat, b:Int):SMat = SMat(gapply(a, b))	
  
  override def colslice(a:Int, b:Int, out:Mat) = SMat(gcolslice(a, b, out))
     
  override def colslice(col1:Int, col2:Int, omat:Mat, there:Int) = {
  		omat match {
  		case (mms:SMat) => {
  			val ioff = Mat.ioneBased
  			val newnnz = jc(col2) - jc(col1) + mms.jc(there) - ioff
  			val ms = SMat.newOrCheckSMat(nrows, mms.ncols, newnnz, mms)
  			if (ms != mms) {
  			  mms.colslice(0, there, ms)
  			}
  			if (there == 0) ms.jc(0) = ioff
  			val todo = jc(col2) - jc(col1)
  			var i = col1
  			while (i < col2) {
  				ms.jc(i+there-col1+1) = ms.jc(i+there-col1) + jc(i+1) - jc(i)
  				i += 1
  			}
  			val ibase = there + col2 - col1
  			i = ibase
  			while (i < ms.ncols) {
  				ms.jc(i+1) = ms.jc(ibase)
  				i += 1
  			}
  			ms.nnz0 = ms.jc(ibase)-ioff
  			System.arraycopy(data, jc(col1) - ioff, ms.data, ms.jc(there) - ioff, todo)
  			System.arraycopy(ir, jc(col1) - ioff, ms.ir, ms.jc(there) - ioff, todo)
  			ms
  		}
    }
  }
       
  def ssMatOp(b: SMat, f:(Float, Float) => Float, omat:Mat) = SMat(sgMatOp(b, f, omat))
  
  def ssMatOpD(b: FMat, f:(Float, Float) => Float, omat:Mat) = SMat(sgMatOpD(b, f, omat))
  
  def ssMatOpScalar(b: Float, f:(Float, Float) => Float, omat:Mat) = SMat(sgMatOpScalar(b, f, omat))
  
  def ssReduceOp(n:Int, f1:(Float) => Float, f2:(Float, Float) => Float, omat:Mat) = FMat(sgReduceOp(n, f1, f2, omat))
  
  def horzcat(a:FMat):FMat = FMat(MatFunctions.full(this).ghorzcat(a))
  
  def vertcat(a:FMat):FMat = FMat(MatFunctions.full(this).gvertcat(a))

  def SMult(a:Mat, omat:Mat):FMat = {
  		val ioff = Mat.ioneBased
  		if (ncols != a.nrows) {
  			throw new RuntimeException("dimensions mismatch")
  		} else {
  			a match {
  			case aa:SMat => {
  				val out = FMat.newOrCheckFMat(nrows, a.ncols, omat, GUID, a.GUID, "SMult".##)
  				if (omat.asInstanceOf[AnyRef] != null) out.clear
  				var i = 0
  				var myflops = 0L
  				while (i < a.ncols) {
  					var j =aa.jc(i)-ioff
  					while (j < aa.jc(i+1)-ioff) {
  						val dval = aa.data(j)
  						var k = jc(aa.ir(j)-ioff)-ioff
  						var k1 = jc(aa.ir(j)+1-ioff)-ioff
  						myflops += 2*(k1-k)
  						while (k < k1) {
  							out.data(ir(k)-ioff+nrows*i) +=  data(k) * dval
  							k += 1
  						}
  						j += 1
  					}
  					i += 1
  				}
  				Mat.nflops += myflops
  				out
  			}
  			case dd:FMat => {
  				val out = FMat.newOrCheckFMat(nrows, a.ncols, omat, GUID, a.GUID, "SMult".##)
  				if (omat.asInstanceOf[AnyRef] != null) out.clear
  				Mat.nflops += 2L * nnz * a.ncols
  				if (Mat.noMKL) {
  					var i = 0
  					while (i < dd.ncols) {
  						var j = 0
  						while (j < ncols) {
  							val dval = dd.data(j + i*dd.nrows)
  							var k = jc(j)-ioff
  							while (k < jc(j+1)-ioff) {
  								out.data(ir(k)-ioff + i*nrows) += dval * data(k);
  								k += 1
  							}
  							j += 1
  						}
  						i += 1
  					}
  				} else {
  					val nc = dd.ncols
  					var jc0 = jc
  					var ir0 = ir
  					if (ioff == 0) {
  						jc0 = SparseMat.incInds(jc)
  						ir0 = SparseMat.incInds(ir)
  					}
  	  	    if (dd.ncols == 1) {
  					// Seg faults in linux and windows - fixed to use one thread	
  	  	      setnumthreads(1)
  	  	    	scscmv("N", nrows, ncols, 1.0f, "GLNF", data, ir, jc, dd.data, 0f, out.data)
  	  	    	setnumthreads(Mat.numOMPthreads)
  	  	    } else {
  	  	    	scscmm("N", nrows, nc, ncols, 1.0f, "GLNF", data, ir0, jc0, dd.data, ncols, 0f, out.data, out.nrows)
  	  	    }
  				}
  				out
  			}
  			case _ => throw new RuntimeException("unsupported arg")
  			}
  		}	
  }
  
  def Tmult(a:FMat, omat:Mat):FMat = {
	  val out = FMat.newOrCheckFMat(ncols, a.ncols, omat, GUID, a.GUID, "TMult".##)
	  if (omat.asInstanceOf[AnyRef] != null) out.clear
	  var jc0 = jc
	  var ir0 = ir
	  if (Mat.ioneBased == 0) {
	  	jc0 = SparseMat.incInds(jc)
	  	ir0 = SparseMat.incInds(ir)
	  }
	  scscmm("T", nrows, a.ncols, ncols, 1.0f, "GLNF", data, ir0, jc0, a.data, a.nrows, 0f, out.data, out.nrows) 
	  Mat.nflops += 2L * nnz * a.ncols
	  out
  }
  

  def SSMult(a:SMat, omat:Mat):SMat = 
  	if (ncols != a.nrows) {
  		throw new RuntimeException("dimensions mismatch")
  	} else {
  		val ioff = Mat.ioneBased<commit_msg>spair ^* missing<commit_after>package BIDMat

import edu.berkeley.bid.SPBLAS._
import edu.berkeley.bid.UTILS._

case class SMat(nr:Int, nc:Int, nnz1:Int, ir0:Array[Int], jc0:Array[Int], data0:Array[Float]) extends SparseMat[Float](nr, nc, nnz1, ir0, jc0, data0) {

  def getdata() = data;	
  
  override def t:SMat = SMat(gt)
  
  override def mytype = "SMat"
  
  def horzcat(b: SMat) = SMat(super.horzcat(b))
  
  def vertcat(b: SMat) = SMat(super.vertcat(b))
  
  def find:IMat = IMat(gfind)
  
  def find2:(IMat, IMat) = { val (ii, jj) = gfind2 ; (IMat(ii), IMat(jj)) }
  
  def find3:(IMat, IMat, FMat) = { val (ii, jj, vv) = gfind3 ; (IMat(ii), IMat(jj), FMat(vv)) }	
  
  override def contents:FMat = FMat(nnz, 1, this.data)
  
  override def apply(a:IMat, b:IMat):SMat = SMat(gapply(a, b))	
  
  override def apply(a:Int, b:IMat):SMat = SMat(gapply(a, b))	
  
  override def apply(a:IMat, b:Int):SMat = SMat(gapply(a, b))	
  
  override def colslice(a:Int, b:Int, out:Mat) = SMat(gcolslice(a, b, out))
     
  override def colslice(col1:Int, col2:Int, omat:Mat, there:Int) = {
  		omat match {
  		case (mms:SMat) => {
  			val ioff = Mat.ioneBased
  			val newnnz = jc(col2) - jc(col1) + mms.jc(there) - ioff
  			val ms = SMat.newOrCheckSMat(nrows, mms.ncols, newnnz, mms)
  			if (ms != mms) {
  			  mms.colslice(0, there, ms)
  			}
  			if (there == 0) ms.jc(0) = ioff
  			val todo = jc(col2) - jc(col1)
  			var i = col1
  			while (i < col2) {
  				ms.jc(i+there-col1+1) = ms.jc(i+there-col1) + jc(i+1) - jc(i)
  				i += 1
  			}
  			val ibase = there + col2 - col1
  			i = ibase
  			while (i < ms.ncols) {
  				ms.jc(i+1) = ms.jc(ibase)
  				i += 1
  			}
  			ms.nnz0 = ms.jc(ibase)-ioff
  			System.arraycopy(data, jc(col1) - ioff, ms.data, ms.jc(there) - ioff, todo)
  			System.arraycopy(ir, jc(col1) - ioff, ms.ir, ms.jc(there) - ioff, todo)
  			ms
  		}
    }
  }
       
  def ssMatOp(b: SMat, f:(Float, Float) => Float, omat:Mat) = SMat(sgMatOp(b, f, omat))
  
  def ssMatOpD(b: FMat, f:(Float, Float) => Float, omat:Mat) = SMat(sgMatOpD(b, f, omat))
  
  def ssMatOpScalar(b: Float, f:(Float, Float) => Float, omat:Mat) = SMat(sgMatOpScalar(b, f, omat))
  
  def ssReduceOp(n:Int, f1:(Float) => Float, f2:(Float, Float) => Float, omat:Mat) = FMat(sgReduceOp(n, f1, f2, omat))
  
  def horzcat(a:FMat):FMat = FMat(MatFunctions.full(this).ghorzcat(a))
  
  def vertcat(a:FMat):FMat = FMat(MatFunctions.full(this).gvertcat(a))

  def SMult(a:Mat, omat:Mat):FMat = {
  		val ioff = Mat.ioneBased
  		if (ncols != a.nrows) {
  			throw new RuntimeException("dimensions mismatch")
  		} else {
  			a match {
  			case aa:SMat => {
  				val out = FMat.newOrCheckFMat(nrows, a.ncols, omat, GUID, a.GUID, "SMult".##)
  				if (omat.asInstanceOf[AnyRef] != null) out.clear
  				var i = 0
  				var myflops = 0L
  				while (i < a.ncols) {
  					var j =aa.jc(i)-ioff
  					while (j < aa.jc(i+1)-ioff) {
  						val dval = aa.data(j)
  						var k = jc(aa.ir(j)-ioff)-ioff
  						var k1 = jc(aa.ir(j)+1-ioff)-ioff
  						myflops += 2*(k1-k)
  						while (k < k1) {
  							out.data(ir(k)-ioff+nrows*i) +=  data(k) * dval
  							k += 1
  						}
  						j += 1
  					}
  					i += 1
  				}
  				Mat.nflops += myflops
  				out
  			}
  			case dd:FMat => {
  				val out = FMat.newOrCheckFMat(nrows, a.ncols, omat, GUID, a.GUID, "SMult".##)
  				if (omat.asInstanceOf[AnyRef] != null) out.clear
  				Mat.nflops += 2L * nnz * a.ncols
  				if (Mat.noMKL) {
  					var i = 0
  					while (i < dd.ncols) {
  						var j = 0
  						while (j < ncols) {
  							val dval = dd.data(j + i*dd.nrows)
  							var k = jc(j)-ioff
  							while (k < jc(j+1)-ioff) {
  								out.data(ir(k)-ioff + i*nrows) += dval * data(k);
  								k += 1
  							}
  							j += 1
  						}
  						i += 1
  					}
  				} else {
  					val nc = dd.ncols
  					var jc0 = jc
  					var ir0 = ir
  					if (ioff == 0) {
  						jc0 = SparseMat.incInds(jc)
  						ir0 = SparseMat.incInds(ir)
  					}
  	  	    if (dd.ncols == 1) {
  					// Seg faults in linux and windows - fixed to use one thread	
  	  	      setnumthreads(1)
  	  	    	scscmv("N", nrows, ncols, 1.0f, "GLNF", data, ir, jc, dd.data, 0f, out.data)
  	  	    	setnumthreads(Mat.numOMPthreads)
  	  	    } else {
  	  	    	scscmm("N", nrows, nc, ncols, 1.0f, "GLNF", data, ir0, jc0, dd.data, ncols, 0f, out.data, out.nrows)
  	  	    }
  				}
  				out
  			}
  			case _ => throw new RuntimeException("unsupported arg")
  			}
  		}	
  }
  
  def Tmult(a:FMat, omat:Mat):FMat = {
	  val out = FMat.newOrCheckFMat(ncols, a.ncols, omat, GUID, a.GUID, "TMult".##)
	  if (omat.asInstanceOf[AnyRef] != null) out.clear
	  var jc0 = jc
	  var ir0 = ir
	  if (Mat.ioneBased == 0) {
	  	jc0 = SparseMat.incInds(jc)
	  	ir0 = SparseMat.incInds(ir)
	  }
	  if (a.ncols == 1) {
	    scscmv("T", nrows, ncols, 1.0f, "GLNF", data, ir0, jc0, a.data, 0f, out.data)
	  } else {
	  	scscmm("T", nrows, a.ncols, ncols, 1.0f, "GLNF", data, ir0, jc0, a.data, a.nrows, 0f, out.data, out.nrows) 
	  }
	  Mat.nflops += 2L * nnz * a.ncols
	  out
  }
  

  def SSMult(a:SMat, omat:Mat):SMat = 
  	if (ncols != a.nrows) {
  		throw new RuntimeException("dimensions mismatch")
  	} else {
  		val ioff = Mat.ioneBased
{'fraction_non_alphanumeric': 0.16476455356286565, 'fraction_numerical': 0.016687446053514913, 'mean_word_length': 2.214549938347719, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4206626', 'n_tokens_mistral': 5512, 'n_tokens_neox': 4939, 'n_words': 967}