Skip to content

Commit

Permalink
Speed up Vector.randomFilled (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
gyrdym committed Jun 7, 2024
1 parent f396f2e commit a6c00a9
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 19 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.5
- `Vector.randomFilled`:
- factory-constructor speed-up

## 13.12.4
- `Vector.filled`:
- factory-constructor speed-up
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Approx. 1.7 second (MacBook Pro 2019), Dart version: 2.16.0
// Approx. 2.5 sec (MacBook Air mid 2017), Dart VM version: 2.16.0
// Approx. 2.8 second (MacBook Pro 2019), Dart version: 3.2.4

import 'package:benchmark_harness/benchmark_harness.dart';
import 'package:ml_linalg/vector.dart';
Expand Down
22 changes: 14 additions & 8 deletions lib/src/vector/float32x4_vector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import 'package:ml_linalg/src/vector/serialization/vector_to_json.dart';
import 'package:ml_linalg/src/vector/vector_cache_keys.dart';
import 'package:ml_linalg/vector.dart';

const _bytesPerElement = Float32List.bytesPerElement;
const _bytesPerSimdElement = Float32x4List.bytesPerElement;
const _bucketSize =
Float32x4List.bytesPerElement ~/ Float32List.bytesPerElement;
Expand All @@ -32,12 +31,12 @@ class Float32x4Vector with IterableMixin<double> implements Vector {
: length = source.length {
_numOfBuckets = _getNumOfBuckets(source.length, _bucketSize);

final typedList = Float32List(_numOfBuckets * _bucketSize);
final list = Float32List(_numOfBuckets * _bucketSize);

_buffer = typedList.buffer;
_buffer = list.buffer;

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

Expand All @@ -54,16 +53,21 @@ class Float32x4Vector with IterableMixin<double> implements Vector {
'Argument `min` should be less than `max`, min: $min, max: $max');
}

if (length < 0) {
throw ArgumentError('Length cannot be negative');
}

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

final list = Float32List(_numOfBuckets * _bucketSize);

_buffer = list.buffer;

final generator = math.Random(seed);
final diff = max - min;

for (var i = 0; i < length; i++) {
byteData.setFloat32(_bytesPerElement * i,
generator.nextDouble() * diff + min, Endian.host);
list[i] = generator.nextDouble() * diff + min;
}
}

Expand All @@ -74,7 +78,9 @@ class Float32x4Vector with IterableMixin<double> implements Vector {
}

_numOfBuckets = _getNumOfBuckets(length, _bucketSize);

final list = Float32x4List(_numOfBuckets);

_buffer = list.buffer;

final simdValue = Float32x4.splat(value.toDouble());
Expand Down
22 changes: 14 additions & 8 deletions lib/src/vector/float64x2_vector.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import 'package:ml_linalg/src/vector/serialization/vector_to_json.dart';
import 'package:ml_linalg/src/vector/vector_cache_keys.dart';
import 'package:ml_linalg/vector.dart';

const _bytesPerElement = Float64List.bytesPerElement;
const _bytesPerSimdElement = Float64x2List.bytesPerElement;
const _bucketSize =
Float64x2List.bytesPerElement ~/ Float64List.bytesPerElement;
Expand All @@ -35,12 +34,12 @@ class Float64x2Vector with IterableMixin<double> implements Vector {
: length = source.length {
_numOfBuckets = _getNumOfBuckets(source.length, _bucketSize);

final typedList = Float64List(_numOfBuckets * _bucketSize);
final list = Float64List(_numOfBuckets * _bucketSize);

_buffer = typedList.buffer;
_buffer = list.buffer;

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

Expand All @@ -57,16 +56,21 @@ class Float64x2Vector with IterableMixin<double> implements Vector {
'Argument `min` should be less than `max`, min: $min, max: $max');
}

if (length < 0) {
throw ArgumentError('Length cannot be negative');
}

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

final list = Float64List(_numOfBuckets * _bucketSize);

_buffer = list.buffer;

final generator = math.Random(seed);
final diff = max - min;

for (var i = 0; i < length; i++) {
byteData.setFloat64(_bytesPerElement * i,
generator.nextDouble() * diff + min, Endian.host);
list[i] = generator.nextDouble() * diff + min;
}
}

Expand All @@ -77,7 +81,9 @@ class Float64x2Vector with IterableMixin<double> implements Vector {
}

_numOfBuckets = _getNumOfBuckets(length, _bucketSize);

final list = Float64x2List(_numOfBuckets);

_buffer = list.buffer;

final simdValue = Float64x2.splat(value.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.4
version: 13.12.5
homepage: https://github.com/gyrdym/ml_linalg

environment:
Expand Down

0 comments on commit a6c00a9

Please sign in to comment.