1
1
import { writeFile } from 'node:fs/promises' ;
2
2
import path from 'node:path' ;
3
3
4
- import { bench , do_not_optimize , lineplot , run } from 'mitata' ;
4
+ import { run , bench , do_not_optimize , lineplot } from 'mitata' ;
5
+ import { Matrix } from 'ml-matrix' ;
5
6
import { xSequentialFillFromStep } from 'ml-spectra-processing' ;
6
7
7
8
import { SparseMatrix } from '../src/index.js' ;
8
9
9
10
import { randomMatrix } from './utils/randomMatrix.js' ;
10
-
11
+ import { mmulMediumDensity } from '../src/utils/mmulMediumDensity.js' ;
12
+ import { mmulSmall } from '../src/utils/mmulSmall.js' ;
13
+ import { mmulLowDensity } from '../src/utils/mmulLowDensity.js' ;
14
+
15
+ /* eslint
16
+ func-names: 0
17
+ camelcase: 0
18
+ */
11
19
// Prepare matrices once
12
20
const cardinalities = Array . from (
13
- xSequentialFillFromStep ( { from : 10 , step : 5 , size : 2 } ) ,
21
+ xSequentialFillFromStep ( { from : 10 , step : 50 , size : 9 } ) ,
14
22
) ;
15
23
16
24
// const dimensions = Array.from(
17
- // xSequentialFillFromStep({ from: 700 , step: 100, size: 13 }),
25
+ // xSequentialFillFromStep({ from: 30 , step: 100, size: 20 }),
18
26
// );
19
27
20
- const dimensions = [ 512 ] ;
28
+ const dimensions = [ 32 ] ;
21
29
lineplot ( ( ) => {
22
30
bench ( 'hibrid($cardinality,$dimension)' , function * ( ctx ) {
23
31
const cardinality = ctx . get ( 'cardinality' ) ;
24
32
const size = ctx . get ( 'dimension' ) ;
25
33
// Prepare matrices once
26
34
let A = new SparseMatrix ( randomMatrix ( size , size , cardinality ) ) ;
27
35
let B = new SparseMatrix ( randomMatrix ( size , size , cardinality ) ) ;
28
-
36
+ A . mmul ( B ) ;
29
37
// Benchmark the multiplication
30
38
yield ( ) => do_not_optimize ( A . mmul ( B ) ) ;
31
39
do_not_optimize ( A ) ;
@@ -41,9 +49,9 @@ lineplot(() => {
41
49
// Prepare matrices once
42
50
let A = new SparseMatrix ( randomMatrix ( size , size , cardinality ) ) ;
43
51
let B = new SparseMatrix ( randomMatrix ( size , size , cardinality ) ) ;
44
-
52
+ mmulSmall ( A , B ) ;
45
53
// Benchmark the multiplication
46
- yield ( ) => do_not_optimize ( A . _mmulSmall ( B ) ) ;
54
+ yield ( ) => do_not_optimize ( mmulSmall ( A , B ) ) ;
47
55
// Explicit cleanup
48
56
do_not_optimize ( A ) ;
49
57
do_not_optimize ( B ) ;
@@ -58,9 +66,26 @@ lineplot(() => {
58
66
// Prepare matrices once
59
67
let A = new SparseMatrix ( randomMatrix ( size , size , cardinality ) ) ;
60
68
let B = new SparseMatrix ( randomMatrix ( size , size , cardinality ) ) ;
69
+ mmulLowDensity ( A , B ) ;
70
+ // Benchmark the multiplication
71
+ yield ( ) => do_not_optimize ( mmulLowDensity ( A , B ) ) ;
72
+ // Explicit cleanup
73
+ do_not_optimize ( A ) ;
74
+ do_not_optimize ( B ) ;
75
+ } )
76
+ . gc ( 'inner' )
77
+ . args ( 'cardinality' , cardinalities ) //.range('size', 32, 1024, 2); //.args('size', sizes);
78
+ . args ( 'dimension' , dimensions ) ;
61
79
80
+ bench ( 'dense($cardinality,$dimension)' , function * ( ctx ) {
81
+ const cardinality = ctx . get ( 'cardinality' ) ;
82
+ const size = ctx . get ( 'dimension' ) ;
83
+ // Prepare matrices once
84
+ let A = new Matrix ( randomMatrix ( size , size , cardinality ) ) ;
85
+ let B = new Matrix ( randomMatrix ( size , size , cardinality ) ) ;
86
+ A . mmul ( B ) ;
62
87
// Benchmark the multiplication
63
- yield ( ) => do_not_optimize ( A . _mmulLowDensity ( B ) ) ;
88
+ yield ( ) => do_not_optimize ( A . mmul ( B ) ) ;
64
89
// Explicit cleanup
65
90
do_not_optimize ( A ) ;
66
91
do_not_optimize ( B ) ;
@@ -75,10 +100,10 @@ lineplot(() => {
75
100
// Prepare matrices once
76
101
let A = new SparseMatrix ( randomMatrix ( size , size , cardinality ) ) ;
77
102
let B = new SparseMatrix ( randomMatrix ( size , size , cardinality ) ) ;
78
-
103
+ mmulMediumDensity ( A , B ) ;
79
104
// Benchmark the multiplication
80
105
yield ( ) => {
81
- do_not_optimize ( A . _mmulMediumDensity ( B ) ) ;
106
+ do_not_optimize ( mmulMediumDensity ( A , B ) ) ;
82
107
} ;
83
108
84
109
// Explicit cleanup
@@ -94,19 +119,19 @@ lineplot(() => {
94
119
const results = await run ( {
95
120
// Force GC between every benchmark
96
121
gc : true ,
97
- // // More samples for statistical significance
98
- // min_samples: 20,
99
- // max_samples: 200,
100
- // // Longer warmup to stabilize CPU state
101
- // warmup_samples: 10,
102
- // warmup_threshold: 100, // ms
122
+ // More samples for statistical significance
123
+ min_samples : 20 ,
124
+ max_samples : 200 ,
125
+ // Longer warmup to stabilize CPU state
126
+ warmup_samples : 10 ,
127
+ warmup_threshold : 100 , // ms
103
128
// Longer minimum time for stable measurements
104
- // min_cpu_time: 2000, // 2 seconds minimum
105
- // // Batch settings to reduce variance
106
- // batch_samples: 5,
107
- // batch_threshold: 10, // ms
108
- // // Enable colors
109
- // colors: true,
129
+ min_cpu_time : 2000 , // 2 seconds minimum
130
+ // Batch settings to reduce variance
131
+ batch_samples : 5 ,
132
+ batch_threshold : 10 , // ms
133
+ // Enable colors
134
+ colors : true ,
110
135
} ) ;
111
136
112
137
// Process and store results
@@ -134,6 +159,6 @@ for (const benchmark of results.benchmarks) {
134
159
135
160
// Save results to JSON file
136
161
await writeFile (
137
- path . join ( import . meta. dirname , `benchmark-results.json` ) ,
162
+ path . join ( import . meta. dirname , `benchmark-results- ${ dimensions [ 0 ] } .json` ) ,
138
163
JSON . stringify ( processedResults , null , 2 ) ,
139
164
) ;
0 commit comments