use of org.apache.sysml.runtime.matrix.data.DenseBlock in project systemml by apache.
the class ReaderTextCSV method readCSVMatrixFromInputStream.
private static long readCSVMatrixFromInputStream(InputStream is, String srcInfo, MatrixBlock dest, MutableInt rowPos, long rlen, long clen, int brlen, int bclen, boolean hasHeader, String delim, boolean fill, double fillValue, boolean first) throws IOException {
boolean sparse = dest.isInSparseFormat();
String value = null;
int row = rowPos.intValue();
double cellValue = 0;
long lnnz = 0;
BufferedReader br = new BufferedReader(new InputStreamReader(is));
if (first && hasHeader)
// ignore header
br.readLine();
// Read the data
boolean emptyValuesFound = false;
try {
if (// SPARSE<-value
sparse) {
while (// foreach line
(value = br.readLine()) != null) {
String cellStr = value.toString().trim();
emptyValuesFound = false;
String[] parts = IOUtilFunctions.split(cellStr, delim);
int col = 0;
for (// foreach cell
String part : // foreach cell
parts) {
part = part.trim();
if (part.isEmpty()) {
emptyValuesFound = true;
cellValue = fillValue;
} else {
cellValue = UtilFunctions.parseToDouble(part);
}
if (cellValue != 0) {
dest.appendValue(row, col, cellValue);
lnnz++;
}
col++;
}
// sanity checks for empty values and number of columns
IOUtilFunctions.checkAndRaiseErrorCSVEmptyField(cellStr, fill, emptyValuesFound);
IOUtilFunctions.checkAndRaiseErrorCSVNumColumns(srcInfo, cellStr, parts, clen);
row++;
}
} else // DENSE<-value
{
DenseBlock a = dest.getDenseBlock();
while ((value = br.readLine()) != null) {
// foreach line
String cellStr = value.toString().trim();
emptyValuesFound = false;
String[] parts = IOUtilFunctions.split(cellStr, delim);
int col = 0;
for (String part : parts) {
// foreach cell
part = part.trim();
if (part.isEmpty()) {
emptyValuesFound = true;
cellValue = fillValue;
} else {
cellValue = UtilFunctions.parseToDouble(part);
}
if (cellValue != 0) {
a.set(row, col, cellValue);
lnnz++;
}
col++;
}
// sanity checks for empty values and number of columns
IOUtilFunctions.checkAndRaiseErrorCSVEmptyField(cellStr, fill, emptyValuesFound);
IOUtilFunctions.checkAndRaiseErrorCSVNumColumns(srcInfo, cellStr, parts, clen);
row++;
}
}
} finally {
IOUtilFunctions.closeSilently(br);
}
rowPos.setValue(row);
return lnnz;
}
use of org.apache.sysml.runtime.matrix.data.DenseBlock in project systemml by apache.
the class ReaderTextCell method readRawTextCellMatrixFromInputStream.
private static void readRawTextCellMatrixFromInputStream(InputStream is, MatrixBlock dest, long rlen, long clen, int brlen, int bclen, boolean matrixMarket) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
boolean sparse = dest.isInSparseFormat();
String value = null;
int row = -1;
int col = -1;
// Read the header lines, if reading from a matrixMarket file
if (matrixMarket) {
// header line
value = br.readLine();
if (value == null || !value.startsWith("%%")) {
throw new IOException("Error while reading file in MatrixMarket format. Expecting a header line, but encountered, \"" + value + "\".");
}
// skip until end-of-comments
while ((value = br.readLine()) != null && value.charAt(0) == '%') {
// do nothing just skip comments
}
// the first line after comments is the one w/ matrix dimensions
// validate (rlen clen nnz)
String[] fields = value.trim().split("\\s+");
long mm_rlen = Long.parseLong(fields[0]);
long mm_clen = Long.parseLong(fields[1]);
if (rlen != mm_rlen || clen != mm_clen) {
throw new IOException("Unexpected matrix dimensions while reading file in MatrixMarket format. Expecting dimensions [" + rlen + " rows, " + clen + " cols] but encountered [" + mm_rlen + " rows, " + mm_clen + "cols].");
}
}
try {
FastStringTokenizer st = new FastStringTokenizer(' ');
if (// SPARSE<-value
sparse) {
while ((value = br.readLine()) != null) {
// reinit tokenizer
st.reset(value);
row = st.nextInt() - 1;
col = st.nextInt() - 1;
if (row == -1 || col == -1)
continue;
double lvalue = st.nextDouble();
dest.appendValue(row, col, lvalue);
}
dest.sortSparseRows();
} else // DENSE<-value
{
DenseBlock a = dest.getDenseBlock();
while ((value = br.readLine()) != null) {
// reinit tokenizer
st.reset(value);
row = st.nextInt() - 1;
col = st.nextInt() - 1;
if (row == -1 || col == -1)
continue;
double lvalue = st.nextDouble();
a.set(row, col, lvalue);
}
}
} catch (Exception ex) {
// post-mortem error handling and bounds checking
if (row < 0 || row + 1 > rlen || col < 0 || col + 1 > clen)
throw new IOException("Matrix cell [" + (row + 1) + "," + (col + 1) + "] " + "out of overall matrix range [1:" + rlen + ",1:" + clen + "].", ex);
else
throw new IOException("Unable to read matrix in raw text cell format.", ex);
} finally {
IOUtilFunctions.closeSilently(br);
}
}
use of org.apache.sysml.runtime.matrix.data.DenseBlock in project incubator-systemml by apache.
the class ResultMergeRemoteSparkWCompare method call.
@Override
public Tuple2<MatrixIndexes, MatrixBlock> call(Tuple2<MatrixIndexes, Tuple2<Iterable<MatrixBlock>, MatrixBlock>> arg) throws Exception {
MatrixIndexes ixin = arg._1();
Iterator<MatrixBlock> din = arg._2()._1().iterator();
MatrixBlock cin = arg._2()._2();
// create compare array
DenseBlock compare = DataConverter.convertToDenseBlock(cin, false);
// merge all blocks into compare block
MatrixBlock out = new MatrixBlock(cin);
while (din.hasNext()) mergeWithComp(out, din.next(), compare);
// create output tuple
return new Tuple2<>(new MatrixIndexes(ixin), out);
}
use of org.apache.sysml.runtime.matrix.data.DenseBlock in project incubator-systemml by apache.
the class StagingFileUtils method readCellList2BlockFromLocal.
public static MatrixBlock readCellList2BlockFromLocal(String fname, int brlen, int bclen, boolean sparse) throws IOException, DMLRuntimeException {
MatrixBlock tmp = new MatrixBlock(brlen, bclen, sparse);
if (!sparse)
tmp.allocateDenseBlockUnsafe(brlen, bclen);
FileInputStream fis = new FileInputStream(fname);
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
FastStringTokenizer st = new FastStringTokenizer(' ');
try {
String value = null;
if (sparse) {
while ((value = in.readLine()) != null) {
// reset tokenizer
st.reset(value);
int row = st.nextInt();
int col = st.nextInt();
double lvalue = st.nextDouble();
tmp.quickSetValue(row, col, lvalue);
}
} else {
DenseBlock a = tmp.getDenseBlock();
while ((value = in.readLine()) != null) {
// reset tokenizer
st.reset(value);
int row = st.nextInt();
int col = st.nextInt();
double lvalue = st.nextDouble();
a.set(row, col, lvalue);
}
tmp.recomputeNonZeros();
}
} finally {
IOUtilFunctions.closeSilently(in);
}
// finally change internal representation if required
tmp.examSparsity();
return tmp;
}
use of org.apache.sysml.runtime.matrix.data.DenseBlock in project incubator-systemml by apache.
the class LinearAlgebraUtils method copyNonZerosToUpperTriangle.
public static void copyNonZerosToUpperTriangle(MatrixBlock ret, MatrixBlock tmp, int ix) {
double[] a = tmp.getDenseBlockValues();
DenseBlock c = ret.getDenseBlock();
for (int i = 0; i < tmp.getNumColumns(); i++) if (a[i] != 0)
c.set((ix < i) ? ix : i, (ix < i) ? i : ix, a[i]);
}
Aggregations