Skip to content

Commit

Permalink
Speed up Vector.fromList (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
gyrdym authored Jun 4, 2024
1 parent 027ddfa commit 05bb8e7
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 8 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.3
- `Vector.fromList`:
- factory-constructor speed-up

## 13.12.2
- `README`:
- Add link to `solve` method api docs
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Approx. 1.1 second (MacBook Pro 2019), Dart version: 2.16.0
// Approx. 0.62 second (MacBook Pro 2019), Dart version: 2.16.0
// Approx. 3.7 second (MacBook Air mid 2017), Dart VM version: 2.16.0

import 'package:benchmark_harness/benchmark_harness.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Approx. 1.5 second (MacBook Pro 2019), Dart version: 3.0.0

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

const amountOfElements = 10000000;

class Float32x4VectorFloatPowBenchmark extends BenchmarkBase {
Float32x4VectorFloatPowBenchmark()
: super(
'Vector `pow` method, floating-point power; $amountOfElements elements');

late Vector vector;

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

@override
void run() {
vector.pow(1234);
}

@override
void setup() {
vector = Vector.randomFilled(
amountOfElements,
seed: 1,
min: -1000,
max: 1000,
dtype: DType.float32,
);
}
}

void main() {
Float32x4VectorFloatPowBenchmark.main();
}
7 changes: 4 additions & 3 deletions lib/src/vector/float32x4_vector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ class Float32x4Vector with IterableMixin<double> implements Vector {
Float32x4Vector.fromList(List<num> source, this._cache, this._simdHelper)
: length = source.length {
_numOfBuckets = _getNumOfBuckets(source.length, _bucketSize);
_buffer = ByteData(_numOfBuckets * _bytesPerSimdElement).buffer;

final asTypedList = _buffer.asFloat32List();
final typedList = Float32List(_numOfBuckets * _bucketSize);

_buffer = typedList.buffer;

for (var i = 0; i < length; i++) {
asTypedList[i] = source[i].toDouble();
typedList[i] = source[i].toDouble();
}
}

Expand Down
7 changes: 4 additions & 3 deletions lib/src/vector/float64x2_vector.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ class Float64x2Vector with IterableMixin<double> implements Vector {
Float64x2Vector.fromList(List<num> source, this._cache, this._simdHelper)
: length = source.length {
_numOfBuckets = _getNumOfBuckets(source.length, _bucketSize);
_buffer = ByteData(_numOfBuckets * _bytesPerSimdElement).buffer;

final asTypedList = _buffer.asFloat64List();
final typedList = Float64List(_numOfBuckets * _bucketSize);

_buffer = typedList.buffer;

for (var i = 0; i < length; i++) {
asTypedList[i] = source[i].toDouble();
typedList[i] = source[i].toDouble();
}
}

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.2
version: 13.12.3
homepage: https://github.com/gyrdym/ml_linalg

environment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,29 @@ void vectorFromListConstructorTestGroupFactory(DType dtype) =>
expect(vector.length, 0);
expect(vector.dtype, dtype);
});

test(
'should provide correct size of vectors when summing up, length is 3',
() {
final vector1 = Vector.fromList([1, 2, 3], dtype: dtype);
final vector2 = Vector.fromList([3, 4, 5], dtype: dtype);
final vector = vector1 + vector2;

expect(vector, equals(<double>[4, 6, 8]));
expect(vector.length, 3);
expect(vector.dtype, dtype);
});

test(
'should provide correct size of vectors when summing up, length is 5',
() {
final vector1 = Vector.fromList([1, 2, 3, 4, 5], dtype: dtype);
final vector2 = Vector.fromList([3, 4, 5, 6, 7], dtype: dtype);
final vector = vector1 + vector2;

expect(vector, equals(<double>[4, 6, 8, 10, 12]));
expect(vector.length, 5);
expect(vector.dtype, dtype);
});
});
});

0 comments on commit 05bb8e7

Please sign in to comment.