Skip to content

Commit

Permalink
WIP: matrix data manager refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
gyrdym committed Apr 7, 2019
1 parent 0c88322 commit 86f904a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 55 deletions.
57 changes: 6 additions & 51 deletions lib/src/matrix/base_matrix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ abstract class BaseMatrix with
@override
int get columnsNum => _dataManager.columnsNum;

List<Vector> get _columnsCache => _dataManager.columnsCache;

List<Vector> get _rowsCache => _dataManager.rowsCache;

@override
Iterator<Iterable<double>> get iterator => _dataManager.dataIterator;

Expand Down Expand Up @@ -99,32 +95,12 @@ abstract class BaseMatrix with
}

@override
Vector getRow(int index, {bool tryCache = true, bool mutable = false}) {
if (tryCache) {
_rowsCache[index] ??= Vector.from(this[index], isMutable: mutable,
dtype: dtype);
return _rowsCache[index];
} else {
return Vector.from(this[index], isMutable: mutable, dtype: dtype);
}
}
Vector getRow(int index, {bool tryCache = true, bool mutable = false}) =>
_dataManager.getRow(index, tryCache: tryCache, mutable: mutable);

@override
Vector getColumn(int index, {bool tryCache = true, bool mutable = false}) {
if (_columnsCache[index] == null || !tryCache) {
final result = List<double>(rowsNum);
for (int i = 0; i < rowsNum; i++) {
//@TODO: find a more efficient way to get the single value
result[i] = _dataManager.getValues(i * columnsNum + index, 1).first;
}
final column = Vector.from(result, isMutable: mutable, dtype: dtype);
if (!tryCache) {
return column;
}
_columnsCache[index] = column;
}
return _columnsCache[index];
}
Vector getColumn(int index, {bool tryCache = true, bool mutable = false}) =>
_dataManager.getColumn(index, tryCache: tryCache, mutable: mutable);

@override
Matrix submatrix({ZRange rows, ZRange columns}) {
Expand Down Expand Up @@ -236,29 +212,8 @@ abstract class BaseMatrix with
}

@override
void setColumn(int columnNum, Iterable<double> columnValues) {
if (columnNum >= columnsNum) {
throw RangeError.range(columnNum, 0, columnsNum - 1, 'Wrong column '
'number');
}
if (columnValues.length != rowsNum) {
throw Exception('New column has length ${columnValues.length}, but the '
'matrix rows number is $rowsNum');
}
// clear rows cache
_rowsCache.fillRange(0, rowsNum, null);
_columnsCache[columnNum] = columnValues is Vector
? columnValues : Vector.from(columnValues);
final values = columnValues.toList(growable: false);
for (int i = 0, j = 0; i < rowsNum * columnsNum; i++) {
if (i == 0 && columnNum != 0) {
continue;
}
if (i == columnNum || i % (j * columnsNum + columnNum) == 0) {
_dataManager.update(i, values[j++]);
}
}
}
void setColumn(int columnNum, Iterable<double> columnValues) =>
_dataManager.setColumn(columnNum, columnValues);

@override
Iterable<Vector> get rows => _generateVectors(_rowIndices, getRow);
Expand Down
3 changes: 3 additions & 0 deletions lib/src/matrix/byte_data_storage/data_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ abstract class DataManager {
Iterator<Iterable<double>> get dataIterator;
List<Vector> get rowsCache;
List<Vector> get columnsCache;
Vector getColumn(int index, {bool tryCache = true, bool mutable = false});
void setColumn(int columnNum, Iterable<double> columnValues);
Vector getRow(int index, {bool tryCache = true, bool mutable = false});

void update(int idx, double value);
void updateAll(int idx, Iterable<double> values);
Expand Down
62 changes: 58 additions & 4 deletions lib/src/matrix/byte_data_storage/float32_data_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Float32DataManager implements DataManager {
columnsCache = List<Vector>(source.first.length),
_data = ByteData(source.length * source.first.length *
Float32List.bytesPerElement) {
final flattened = flatten2dimList(source, (i, j) => i * columnsNum + j);
final flattened = _flatten2dimList(source, (i, j) => i * columnsNum + j);
updateAll(0, flattened);
}

Expand All @@ -23,7 +23,7 @@ class Float32DataManager implements DataManager {
columnsCache = List<Vector>(source.first.length),
_data = ByteData(source.length * source.first.length *
Float32List.bytesPerElement) {
final flattened = flatten2dimList(source, (i, j) => i * columnsNum + j);
final flattened = _flatten2dimList(source, (i, j) => i * columnsNum + j);
updateAll(0, flattened);
}

Expand All @@ -34,7 +34,7 @@ class Float32DataManager implements DataManager {
columnsCache = source.toList(growable: false),
_data = ByteData(source.length * source.first.length *
Float32List.bytesPerElement) {
final flattened = flatten2dimList(source, (i, j) => j * columnsNum + i);
final flattened = _flatten2dimList(source, (i, j) => j * columnsNum + i);
updateAll(0, flattened);
}

Expand Down Expand Up @@ -86,7 +86,61 @@ class Float32DataManager implements DataManager {
_data.buffer.asFloat32List().setAll(0, values);
}

List<double> flatten2dimList(
@override
Vector getRow(int index, {bool tryCache = true, bool mutable = false}) {
if (tryCache) {
rowsCache[index] ??= Vector.from(getValues(index * columnsNum,
columnsNum), isMutable: mutable, dtype: Float32x4);
return rowsCache[index];
} else {
return Vector.from(getValues(index * columnsNum, columnsNum),
isMutable: mutable, dtype: Float32x4);
}
}

@override
Vector getColumn(int index, {bool tryCache = true, bool mutable = false}) {
if (columnsCache[index] == null || !tryCache) {
final result = List<double>(rowsNum);
for (int i = 0; i < rowsNum; i++) {
//@TODO: find a more efficient way to get the single value
result[i] = getValues(i * columnsNum + index, 1).first;
}
final column = Vector.from(result, isMutable: mutable, dtype: Float32x4);
if (!tryCache) {
return column;
}
columnsCache[index] = column;
}
return columnsCache[index];
}

@override
void setColumn(int columnNum, Iterable<double> columnValues) {
if (columnNum >= columnsNum) {
throw RangeError.range(columnNum, 0, columnsNum - 1, 'Wrong column '
'number');
}
if (columnValues.length != rowsNum) {
throw Exception('New column has length ${columnValues.length}, but the '
'matrix rows number is $rowsNum');
}
// clear rows cache
rowsCache.fillRange(0, rowsNum, null);
columnsCache[columnNum] = columnValues is Vector
? columnValues : Vector.from(columnValues);
final values = columnValues.toList(growable: false);
for (int i = 0, j = 0; i < rowsNum * columnsNum; i++) {
if (i == 0 && columnNum != 0) {
continue;
}
if (i == columnNum || i % (j * columnsNum + columnNum) == 0) {
update(i, values[j++]);
}
}
}

List<double> _flatten2dimList(
Iterable<Iterable<double>> rows, int accessor(int i, int j)) {
int i = 0;
int j = 0;
Expand Down

0 comments on commit 86f904a

Please sign in to comment.