The Computer Language
24.04 Benchmarks Game

spectral-norm VW Smalltalk #2 program

source code

"* The Computer Language Benchmarks Game
    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
      do: aBlock
      with: (first collect: [:each| anArray]) 
      with: first 
      with: last.!

multiplyAtAv: anArray
   ^self reduce: 
         map: [:w :i :j | w multiplyAtvFrom: i to: j ] 
            (self reduce: 
                   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)

   | 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].

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'!

   self nextPut: Character lf!

print: number digits: decimalPlaces
   self nextPutAll: 
      ((number asFixedPoint: decimalPlaces) printString copyWithout: $s)! !


notes, command-line, and program output

64-bit Ubuntu quad core
VisualWorks® 8.3
Aug 19 2017

 Tue, 05 Mar 2024 07:44:44 GMT

cp /opt/src/vw8.3pul/image/
/opt/src/vw8.3pul/bin/visual -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:
BenchmarksGame class<initialize-release
BenchmarksGame class<instance creation
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/all-benchmarksgame/benchmarksgame_i53330/spectralnorm/tmp/ created at March 4, 2024 11:44:21 PM
7.71s to complete and log all make actions

 /opt/src/vw8.3pul/bin/visual -nogui -evaluate "BenchmarksGame do: 5500"