Skip to content

Commit

Permalink
Matrix: 'fromList' and 'fromFlattenedList' speed up (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
gyrdym committed Jun 8, 2024
1 parent a6c00a9 commit 928db80
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 13.12.6
- `Matrix.fromList`, `Matrix.fromFlattenedList`:
- speed up

## 13.12.5
- `Vector.randomFilled`:
- factory-constructor speed-up
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Approx. 2.8 seconds (MacBook Air mid 2017), Dart VM version: 2.5.0
// Approx. 1.1 second (MacBook Pro mid 2019), Dart VM version: 2.16.0
// Approx. 0.6 second (MacBook Pro mid 2019), Dart VM version: 3.2.4

import 'package:benchmark_harness/benchmark_harness.dart';
import 'package:ml_linalg/dtype.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Approx. 86 microsecond (MacBook Pro mid 2019), Dart VM version: 3.2.4

import 'dart:typed_data';

import 'package:benchmark_harness/benchmark_harness.dart';
import 'package:ml_linalg/dtype.dart';
import 'package:ml_linalg/matrix.dart';
import 'package:ml_linalg/vector.dart';

const numOfRows = 10000;
const numOfColumns = 1000;

class Float32MatrixFromFlattenedFloat32ListBenchmark extends BenchmarkBase {
Float32MatrixFromFlattenedFloat32ListBenchmark()
: super(
'Matrix initialization (fromFlattenedList, source list is Float32List)');

late Float32List _source;

static void main() {
Float32MatrixFromFlattenedFloat32ListBenchmark().report();
}

@override
void run() {
Matrix.fromFlattenedList(_source, numOfRows, numOfColumns,
dtype: DType.float32);
}

@override
void setup() {
_source = Float32List.fromList(Vector.randomFilled(numOfRows * numOfColumns,
min: -1000, max: 1000, seed: 12)
.toList(growable: false));
}
}

void main() {
Float32MatrixFromFlattenedFloat32ListBenchmark.main();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Approx. 1 second (MacBook Air mid 2017), Dart VM version: 2.5.0
// Approx. 0.5 second (MacBook Pro mid 2019), Dart VM version: 2.16.0
// Approx. 0.38 second (MacBook Pro mid 2019), Dart VM version: 3.2.4

import 'package:benchmark_harness/benchmark_harness.dart';
import 'package:ml_linalg/dtype.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Approx. 1.2 seconds (MacBook Air mid 2017), Dart VM version: 2.5.0
// Approx. 0.8 second (MacBook Pro mid 2019), Dart VM version: 2.16.0
// Approx. 0.8 second (MacBook Pro mid 2019), Dart VM version: 3.2.4

import 'package:benchmark_harness/benchmark_harness.dart';
import 'package:ml_linalg/dtype.dart';
Expand Down
18 changes: 12 additions & 6 deletions lib/src/matrix/float32_matrix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ class Float32Matrix
_flattenedList =
Float32List(source.length * getLengthOfFirstOrZero(source)) {
for (var i = 0; i < source.length; i++) {
if (source[i].length != columnCount) {
throw Exception('Wrong nested list length: ${source[i].length}, '
final column = source[i];
if (column.length != columnCount) {
throw Exception('Wrong nested list length: ${column.length}, '
'expected length: $columnCount');
}

for (var j = 0; j < source[i].length; j++) {
_flattenedList[i * columnCount + j] = source[i][j];
for (var j = 0; j < column.length; j++) {
_flattenedList[i * columnCount + j] = column[j];
}
}
}
Expand Down Expand Up @@ -112,12 +112,18 @@ class Float32Matrix
_rowCache = List<Vector?>.filled(rowCount, null),
_colCache = List<Vector?>.filled(colCount, null),
_flattenedList =
source is Float32List ? source : Float32List.fromList(source) {
source is Float32List ? source : Float32List(source.length) {
if (source.length < rowCount * colCount) {
throw Exception('Invalid matrix dimension has been provided - '
'$rowCount x $colCount, but given a collection of length '
'${source.length}');
}

if (source is! Float32List) {
for (var i = 0; i < _flattenedList.length; i++) {
_flattenedList[i] = source[i];
}
}
}

Float32Matrix.fromByteData(ByteData source, int rowCount, int colCount)
Expand Down
18 changes: 12 additions & 6 deletions lib/src/matrix/float64_matrix.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ class Float64Matrix
_flattenedList =
Float64List(source.length * getLengthOfFirstOrZero(source)) {
for (var i = 0; i < source.length; i++) {
if (source[i].length != columnCount) {
throw Exception('Wrong nested list length: ${source[i].length}, '
final column = source[i];
if (column.length != columnCount) {
throw Exception('Wrong nested list length: ${column.length}, '
'expected length: $columnCount');
}

for (var j = 0; j < source[i].length; j++) {
_flattenedList[i * columnCount + j] = source[i][j];
for (var j = 0; j < column.length; j++) {
_flattenedList[i * columnCount + j] = column[j];
}
}
}
Expand Down Expand Up @@ -115,12 +115,18 @@ class Float64Matrix
_rowCache = List<Vector?>.filled(rowCount, null),
_colCache = List<Vector?>.filled(colCount, null),
_flattenedList =
source is Float64List ? source : Float64List.fromList(source) {
source is Float64List ? source : Float64List(source.length) {
if (source.length < rowCount * colCount) {
throw Exception('Invalid matrix dimension has been provided - '
'$rowCount x $colCount, but given a collection of length '
'${source.length}');
}

if (source is! Float64List) {
for (var i = 0; i < _flattenedList.length; i++) {
_flattenedList[i] = source[i];
}
}
}

Float64Matrix.fromByteData(ByteData source, int rowCount, int colCount)
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: ml_linalg
description: SIMD-based linear algebra and statistics, efficient manipulation with numeric data
version: 13.12.5
version: 13.12.6
homepage: https://github.com/gyrdym/ml_linalg

environment:
Expand Down

0 comments on commit 928db80

Please sign in to comment.