spectral-norm JRuby #2 program

source code

```# The Computer Language Benchmarks Game
# https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
#
# contributed by Rick Branson

def eval_a(i, j)
1.0/((i+j)*(i+j+1.0)/2.0+i+1.0)
end

class Barrier
def initialize(count)
@mutex = Mutex.new
@count = count
reset_generation
end

def reset_generation
@generation = { :waiting => 0 }
end

def wait
generation = nil

@mutex.synchronize do
generation = @generation
end

@mutex.synchronize do
generation[:waiting] += 1

if generation[:waiting] == @count
reset_generation
end
end

loop do
@mutex.synchronize do
if generation[:waiting] == @count
return
end
end

end
end
end

class SpectralNorm
class Worker
def initialize(sn, range)
@u, @v, @mtx, @tmp, @range = sn.u, sn.v, sn.mtx, sn.tmp, range

for i in (1..10)
multiply_at_av(@u, @tmp, @v)
multiply_at_av(@v, @tmp, @u)
end

@vBv = 0
@vv  = 0

for i in @range
@vBv += @u[i] * @v[i]
@vv  += @v[i] * @v[i]
end
end

def values
[ @vBv, @vv ]
end

private

def multiply_atv(v, atv)
for i in @range
sum = 0.0

for j in (0 .. (v.size - 1))
sum += eval_a(j, i) * v[j]
end

atv[i] = sum
end
end

def multiply_av(v, av)
for i in @range
sum = 0.0

for j in (0 .. (v.size - 1))
sum += eval_a(i, j) * v[j]
end

av[i] = sum
end
end

def multiply_at_av(v, tmp, at_av)
multiply_av(v, tmp)
@mtx.wait
multiply_atv(tmp, at_av)
@mtx.wait
end
end

@n        = n
@u        = [1.0] * n
@v        = Array.new(n)
@tmp      = Array.new(n)
end

def run
vBv = 0
vv  = 0
ths = []

r = ((i * chk) .. ((i < (@threads - 1) ? (i * chk) + chk : @n) - 1))

end
end

ths.each do |t|
t.join
t_vBv, t_vv = t[:worker].values
vBv += t_vBv
vv  += t_vv
end

Math.sqrt(vBv / vv)
end
end

print "%0.9f" % SpectralNorm.new(ARGV[0].to_i).run, "\n"
```

notes, command-line, and program output

```NOTES:
jruby 9.2.4.0 (2.5.0) 2018-11-13 8faff06 OpenJDK 64-Bit Server VM 11+28 on 11+28 +jit [linux-x86_64]

Fri, 23 Nov 2018 02:17:47 GMT

MAKE:
mv spectralnorm.jruby-2.jruby spectralnorm.rb

0.10s to complete and log all make actions

COMMAND LINE:
/opt/src/jruby-9.2.4.0/bin/jruby -Xcompile.fastest=true -Xcompile.invokedynamic=true -J-server -J-Xmn512m -J-Xms2048m -J-Xmx2048m spectralnorm.rb 5500

PROGRAM OUTPUT:
1.274224153
```