spectral-norm VW Smalltalk #2 program
source code
"* The Computer Language Benchmarks Game
https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
contributed by Paolo Bonzini
reworked for multicore by Isaac Gouy *"!
Smalltalk.Core defineClass: #BenchmarksGame
superclass: #{Core.Object}
indexedType: #none
private: false
instanceVariableNames: 'n workers first last '
classInstanceVariableNames: ''
imports: ''
category: ''!
!Core.BenchmarksGame class methodsFor: 'initialize-release'!
do: n
Stdout print: (self spectralnorm: n) digits: 9; nl.
^''! !
!Core.BenchmarksGame class methodsFor: 'instance creation'!
spectralnorm: anInteger
^super new spectralnorm: anInteger! !
!Core.BenchmarksGame methodsFor: 'initialize-release'!
spectralnorm: anInteger
| nprocs chunkSize |
n := anInteger.
nprocs := (ExternalProcess shOne: 'nproc') asNumber.
workers := MatriX.VirtualMachines new: nprocs.
[
chunkSize := anInteger // nprocs + 1.
first := (0 to: (nprocs - 1)) collect: [:each| each * chunkSize + 1].
last := first collect: [:each| (each + chunkSize - 1) min: n].
^self spectralnorm.
] ensure: [workers release].! !
!Core.BenchmarksGame methodsFor: 'private'!
map: aBlock with: anArray
^workers
do: aBlock
with: (first collect: [:each| anArray])
with: first
with: last.!
multiplyAtAv: anArray
^self reduce:
(self
map: [:w :i :j | w multiplyAtvFrom: i to: j ]
with:
(self reduce:
(self
map: [:w :i :j | w multiplyAvFrom: i to: j ]
with: anArray
)
)
)!
reduce: aCollection
| a |
a := Array new: n.
aCollection keysDo: [:index|
a replaceElementsFrom: (first at: index)
to: (last at: index)
withSequenceableCollection: (aCollection at: index)
startingAt: (first at: index)
].
^a!
spectralnorm
| u v vBv vv |
u := Array new: n withAll: 1.0d0.
10 timesRepeat: [
v := self multiplyAtAv: u.
u := self multiplyAtAv: v.
].
vBv := 0.0d0.
vv := 0.0d0.
1 to: n do:
[:i |
vBv := vBv + ((u at: i) * (v at: i)).
vv := vv + ((v at: i) * (v at: i))].
^(vBv / vv) sqrt! !
!Core.SmallInteger methodsFor: 'benchmarks game'!
matrixA: anInteger
^1.0d0 / ((self + anInteger - 2) * (self + anInteger - 1) /2 + self)! !
!Core.Array methodsFor: 'benchmarks game'!
multiplyAvFrom: first to: last
| n av sum |
n := self size.
av := Array new: n.
first to: last do: [:i|
sum := 0.0d0.
1 to: n do: [:j|
sum := sum + ((i matrixA: j) * (self at: j)) ].
av at: i put: sum].
^av!
multiplyAtvFrom: first to: last
| n atv sum |
n := self size.
atv := Array new: n.
first to: last do: [:i|
sum := 0.0d0.
1 to: n do: [:j|
sum := sum + ((j matrixA: i) * (self at: j)) ].
atv at: i put: sum].
^atv! !
!Core.Stream methodsFor: 'benchmarks game'!
nl
self nextPut: Character lf!
print: number digits: decimalPlaces
self nextPutAll:
((number asFixedPoint: decimalPlaces) printString copyWithout: $s)! !
notes, command-line, and program output
NOTES:
64-bit Ubuntu quad core
VisualWorks® 8.3 Aug 19 2017
Sun, 10 May 2020 04:40:14 GMT
MAKE:
cp /opt/src/vw8.3pul/image/visualnc64.im spectralnorm.vw-2.vw_run.im
/opt/src/vw8.3pul/bin/visual spectralnorm.vw-2.vw_run.im -nogui -pcl MatriX -filein spectralnorm.vw-2.vw -doit 'ObjectMemory snapshotThenQuit'
Autoloading MatriX from $(VISUALWORKS)/preview/matrix/MatriX.pcl
Autoloading Xtreams-Support from $(VISUALWORKS)/xtreams/Xtreams-Support.pcl
Autoloading Xtreams-Core from $(VISUALWORKS)/xtreams/Xtreams-Core.pcl
Autoloading Xtreams-Terminals from $(VISUALWORKS)/xtreams/Xtreams-Terminals.pcl
Autoloading Xtreams-Transforms from $(VISUALWORKS)/xtreams/Xtreams-Transforms.pcl
Autoloading Xtreams-Substreams from $(VISUALWORKS)/xtreams/Xtreams-Substreams.pcl
Autoloading Xtreams-Multiplexing from $(VISUALWORKS)/xtreams/Xtreams-Multiplexing.pcl
Filing in from:
spectralnorm.vw-2.vw
BenchmarksGame class<initialize-release
BenchmarksGame class<instance creation
BenchmarksGame<initialize-release
BenchmarksGame<private
SmallInteger<benchmarks game
Do you want to add Root.Smalltalk.Core.SmallInteger>>matrixA: to the previously unchanged package, Magnitude-Numbers
OK to continue?
Array<benchmarks game
Do you want to add Root.Smalltalk.Core.Array>>multiplyAvFrom:to: to the previously unchanged package, Collections-Arrayed
OK to continue?
Stream<benchmarks game
Do you want to add Root.Smalltalk.Core.Stream>>nl to the previously unchanged package, Collections-Streams
OK to continue?
/home/dunham/benchmarksgame_quadcore/spectralnorm/tmp/spectralnorm.vw-2.vw_run.im created at May 9, 2020 9:39:39 PM
10.09s to complete and log all make actions
COMMAND LINE:
/opt/src/vw8.3pul/bin/visual spectralnorm.vw-2.vw_run.im -nogui -evaluate "BenchmarksGame do: 5500"
PROGRAM OUTPUT:
1.274224153