Skip to content

Commit

Permalink
Speed up Vector.filled (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
gyrdym committed Jun 6, 2024
1 parent 05bb8e7 commit f396f2e
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 9 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.4
- `Vector.filled`:
- factory-constructor speed-up

## 13.12.3
- `Vector.fromList`:
- factory-constructor speed-up
Expand Down
25 changes: 25 additions & 0 deletions benchmark/vector/float32/vector_initializing/float32x4_filled.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Approx. 0.23 second (MacBook Pro 2019), Dart version: 2.16.0

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

const amountOfElements = 10000000;

class Float32x4VectorFilledBenchmark extends BenchmarkBase {
Float32x4VectorFilledBenchmark()
: super('Vector initialization (filled), '
'$amountOfElements elements');

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

@override
void run() {
Vector.filled(amountOfElements, 10000);
}
}

void main() {
Float32x4VectorFilledBenchmark.main();
}
14 changes: 10 additions & 4 deletions lib/src/vector/float32x4_vector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,18 @@ class Float32x4Vector with IterableMixin<double> implements Vector {

Float32x4Vector.filled(
this.length, num value, this._cache, this._simdHelper) {
if (length < 0) {
throw ArgumentError('Length cannot be negative');
}

_numOfBuckets = _getNumOfBuckets(length, _bucketSize);
final byteData = ByteData(_numOfBuckets * _bytesPerSimdElement);
_buffer = byteData.buffer;
final list = Float32x4List(_numOfBuckets);
_buffer = list.buffer;

for (var i = 0; i < length; i++) {
byteData.setFloat32(_bytesPerElement * i, value.toDouble(), Endian.host);
final simdValue = Float32x4.splat(value.toDouble());

for (var i = 0; i < _numOfBuckets; i++) {
list[i] = simdValue;
}
}

Expand Down
14 changes: 10 additions & 4 deletions lib/src/vector/float64x2_vector.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,18 @@ class Float64x2Vector with IterableMixin<double> implements Vector {

Float64x2Vector.filled(
this.length, num value, this._cache, this._simdHelper) {
if (length < 0) {
throw ArgumentError('Length cannot be negative');
}

_numOfBuckets = _getNumOfBuckets(length, _bucketSize);
final byteData = ByteData(_numOfBuckets * _bytesPerSimdElement);
_buffer = byteData.buffer;
final list = Float64x2List(_numOfBuckets);
_buffer = list.buffer;

for (var i = 0; i < length; i++) {
byteData.setFloat64(_bytesPerElement * i, value.toDouble(), Endian.host);
final simdValue = Float64x2.splat(value.toDouble());

for (var i = 0; i < _numOfBuckets; i++) {
list[i] = simdValue;
}
}

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

environment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,17 @@ void vectorFilledConstructorTestGroupFactory(DType dtype) =>
expect(vector.length, equals(10));
expect(vector.dtype, dtype);
});

test(
'should provide correct size of vectors when summing up, length is 5',
() {
final vector1 = Vector.filled(5, 2.0, dtype: dtype);
final vector2 = Vector.filled(5, 3.0, dtype: dtype);
final vector = vector1 + vector2;

expect(vector, equals([5.0, 5.0, 5.0, 5.0, 5.0]));
expect(vector.length, equals(5));
expect(vector.dtype, dtype);
});
});
});

0 comments on commit f396f2e

Please sign in to comment.