Commit 5b0e4547 authored by HankG's avatar HankG

Added straight translation of spectralnorm

parent 12105f3b
/*
The Computer Language Benchmarks Game
http://benchmarksgame.alioth.debian.org/
Based on the Java (2) version by Jarkko Miettinen
and The Anh Tran
Contributed by Hank Grabowski
*/
import java.text.DecimalFormat
import java.util.concurrent.CyclicBarrier
object spectralnorm {
private val formatter = DecimalFormat("#.000000000")
@JvmStatic
fun main(args: Array<String>) {
var n = 1000
if (args.size > 0) n = Integer.parseInt(args[0])
println(formatter.format(spectralnormGame(n)))
}
private fun spectralnormGame(n: Int): Double {
// create unit vector
val u = DoubleArray(n)
val v = DoubleArray(n)
val tmp = DoubleArray(n)
for (i in 0..n - 1)
u[i] = 1.0
// get available processor, then set up syn object
val nthread = Runtime.getRuntime().availableProcessors()
Approximate.barrier = CyclicBarrier(nthread)
val chunk = n / nthread
val ap = arrayOfNulls<Approximate>(nthread)
for (i in 0..nthread - 1) {
val r1 = i * chunk
val r2 = if (i < nthread - 1) r1 + chunk else n
ap[i] = Approximate(u, v, tmp, r1, r2)
}
var vBv = 0.0
var vv = 0.0
for (i in 0..nthread - 1) {
try {
ap[i]!!.join()
vBv += ap[i]!!.m_vBv
vv += ap[i]!!.m_vv
} catch (e: Exception) {
e.printStackTrace()
}
}
return Math.sqrt(vBv / vv)
}
private class Approximate(private val _u: DoubleArray, private val _v: DoubleArray, private val _tmp: DoubleArray, private val range_begin: Int, private val range_end: Int) : Thread() {
var m_vBv = 0.0
var m_vv = 0.0
init {
start()
}
override fun run() {
// 20 steps of the power method
for (i in 0..9) {
MultiplyAtAv(_u, _tmp, _v)
MultiplyAtAv(_v, _tmp, _u)
}
for (i in range_begin..range_end - 1) {
m_vBv += _u[i] * _v[i]
m_vv += _v[i] * _v[i]
}
}
/* multiply vector v by matrix A, each thread evaluate its range only */
private fun MultiplyAv(v: DoubleArray, Av: DoubleArray) {
for (i in range_begin..range_end - 1) {
var sum = 0.0
for (j in v.indices)
sum += eval_A(i, j) * v[j]
Av[i] = sum
}
}
/* multiply vector v by matrix A transposed */
private fun MultiplyAtv(v: DoubleArray, Atv: DoubleArray) {
for (i in range_begin..range_end - 1) {
var sum = 0.0
for (j in v.indices)
sum += eval_A(j, i) * v[j]
Atv[i] = sum
}
}
/* multiply vector v by matrix A and then by matrix A transposed */
private fun MultiplyAtAv(v: DoubleArray, tmp: DoubleArray, AtAv: DoubleArray) {
try {
MultiplyAv(v, tmp)
// all thread must syn at completion
barrier!!.await()
MultiplyAtv(tmp, AtAv)
// all thread must syn at completion
barrier!!.await()
} catch (e: Exception) {
e.printStackTrace()
}
}
companion object {
var barrier: CyclicBarrier? = null
/* return element i,j of infinite matrix A */
private fun eval_A(i: Int, j: Int): Double {
val div = ((i + j) * (i + j + 1)).ushr(1) + i + 1
return 1.0 / div
}
}
}
}
......@@ -75,6 +75,7 @@ onlydirs =
pidigits
regexredux
revcomp
spectralnorm
......@@ -134,6 +135,7 @@ nbody = 10000 20000 30000 40000 50000
regexdna = 10000
binarytrees = 1 2 5 10 15 20
pidigits = 1 100 1000 2500 5000 10000
spectralnorm = 1000 2000 3000 5000 8000
......
Revised BSD license
This is a specific instance of the Open Source Initiative (OSI) BSD license template
http://www.opensource.org/licenses/bsd-license.php
Copyright © 2004-2008 Brent Fulgham, 2005-2017 Isaac Gouy
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of "The Computer Language Benchmarks Game" nor the name of "The Computer Language Shootout Benchmarks" nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/*
The Computer Language Benchmarks Game
http://benchmarksgame.alioth.debian.org/
Based on C# entry by Isaac Gouy
contributed by Jarkko Miettinen
Parallel by The Anh Tran
*/
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.concurrent.CyclicBarrier;
public class spectralnorm
{
private static final NumberFormat formatter = new DecimalFormat ("#.000000000");
public static void main (String[] args)
{
int n = 1000;
if (args.length > 0) n = Integer.parseInt (args[0]);
System.out.println (formatter.format (spectralnormGame (n)) );
}
private static final double spectralnormGame (int n)
{
// create unit vector
double[] u = new double[n];
double[] v = new double[n];
double[] tmp = new double[n];
for (int i = 0; i < n; i++)
u[i] = 1.0;
// get available processor, then set up syn object
int nthread = Runtime.getRuntime ().availableProcessors ();
Approximate.barrier = new CyclicBarrier (nthread);
int chunk = n / nthread;
Approximate[] ap = new Approximate[nthread];
for (int i = 0; i < nthread; i++)
{
int r1 = i * chunk;
int r2 = (i < (nthread -1)) ? r1 + chunk : n;
ap[i] = new Approximate (u, v, tmp, r1, r2);
}
double vBv = 0, vv = 0;
for (int i = 0; i < nthread; i++)
{
try
{
ap[i].join ();
vBv += ap[i].m_vBv;
vv += ap[i].m_vv;
}
catch (Exception e)
{
e.printStackTrace ();
}
}
return Math.sqrt (vBv/vv);
}
private static class Approximate extends Thread
{
private static CyclicBarrier barrier;
private double[] _u;
private double[] _v;
private double[] _tmp;
private int range_begin, range_end;
private double m_vBv = 0, m_vv = 0;
public Approximate (double[] u, double[] v, double[] tmp, int rbegin, int rend)
{
super ();
_u = u;
_v = v;
_tmp = tmp;
range_begin = rbegin;
range_end = rend;
start ();
}
public void run ()
{
// 20 steps of the power method
for (int i = 0; i < 10; i++)
{
MultiplyAtAv (_u, _tmp, _v);
MultiplyAtAv (_v, _tmp, _u);
}
for (int i = range_begin; i < range_end; i++)
{
m_vBv += _u[i] * _v[i];
m_vv += _v[i] * _v[i];
}
}
/* return element i,j of infinite matrix A */
private final static double eval_A (int i, int j)
{
int div = ( ((i+j) * (i+j+1) >>> 1) +i+1 );
return 1.0 / div;
}
/* multiply vector v by matrix A, each thread evaluate its range only */
private final void MultiplyAv (final double[] v, double[] Av)
{
for (int i = range_begin; i < range_end; i++)
{
double sum = 0;
for (int j = 0; j < v.length; j++)
sum += eval_A (i, j) * v[j];
Av[i] = sum;
}
}
/* multiply vector v by matrix A transposed */
private final void MultiplyAtv (final double[] v, double[] Atv)
{
for (int i = range_begin; i < range_end; i++)
{
double sum = 0;
for (int j = 0; j < v.length; j++)
sum += eval_A (j, i) * v[j];
Atv[i] = sum;
}
}
/* multiply vector v by matrix A and then by matrix A transposed */
private final void MultiplyAtAv (final double[] v, double[] tmp, double[] AtAv)
{
try
{
MultiplyAv (v, tmp);
// all thread must syn at completion
barrier.await ();
MultiplyAtv (tmp, AtAv);
// all thread must syn at completion
barrier.await ();
}
catch (Exception e)
{
e.printStackTrace ();
}
}
}
}
/*
The Computer Language Benchmarks Game
http://benchmarksgame.alioth.debian.org/
Based on the Java (2) version by Jarkko Miettinen
and The Anh Tran
Contributed by Hank Grabowski
*/
import java.text.DecimalFormat
import java.util.concurrent.CyclicBarrier
object spectralnorm {
private val formatter = DecimalFormat("#.000000000")
@JvmStatic
fun main(args: Array<String>) {
var n = 1000
if (args.size > 0) n = Integer.parseInt(args[0])
println(formatter.format(spectralnormGame(n)))
}
private fun spectralnormGame(n: Int): Double {
// create unit vector
val u = DoubleArray(n)
val v = DoubleArray(n)
val tmp = DoubleArray(n)
for (i in 0..n - 1)
u[i] = 1.0
// get available processor, then set up syn object
val nthread = Runtime.getRuntime().availableProcessors()
Approximate.barrier = CyclicBarrier(nthread)
val chunk = n / nthread
val ap = arrayOfNulls<Approximate>(nthread)
for (i in 0..nthread - 1) {
val r1 = i * chunk
val r2 = if (i < nthread - 1) r1 + chunk else n
ap[i] = Approximate(u, v, tmp, r1, r2)
}
var vBv = 0.0
var vv = 0.0
for (i in 0..nthread - 1) {
try {
ap[i]!!.join()
vBv += ap[i]!!.m_vBv
vv += ap[i]!!.m_vv
} catch (e: Exception) {
e.printStackTrace()
}
}
return Math.sqrt(vBv / vv)
}
private class Approximate(private val _u: DoubleArray, private val _v: DoubleArray, private val _tmp: DoubleArray, private val range_begin: Int, private val range_end: Int) : Thread() {
var m_vBv = 0.0
var m_vv = 0.0
init {
start()
}
override fun run() {
// 20 steps of the power method
for (i in 0..9) {
MultiplyAtAv(_u, _tmp, _v)
MultiplyAtAv(_v, _tmp, _u)
}
for (i in range_begin..range_end - 1) {
m_vBv += _u[i] * _v[i]
m_vv += _v[i] * _v[i]
}
}
/* multiply vector v by matrix A, each thread evaluate its range only */
private fun MultiplyAv(v: DoubleArray, Av: DoubleArray) {
for (i in range_begin..range_end - 1) {
var sum = 0.0
for (j in v.indices)
sum += eval_A(i, j) * v[j]
Av[i] = sum
}
}
/* multiply vector v by matrix A transposed */
private fun MultiplyAtv(v: DoubleArray, Atv: DoubleArray) {
for (i in range_begin..range_end - 1) {
var sum = 0.0
for (j in v.indices)
sum += eval_A(j, i) * v[j]
Atv[i] = sum
}
}
/* multiply vector v by matrix A and then by matrix A transposed */
private fun MultiplyAtAv(v: DoubleArray, tmp: DoubleArray, AtAv: DoubleArray) {
try {
MultiplyAv(v, tmp)
// all thread must syn at completion
barrier!!.await()
MultiplyAtv(tmp, AtAv)
// all thread must syn at completion
barrier!!.await()
} catch (e: Exception) {
e.printStackTrace()
}
}
companion object {
var barrier: CyclicBarrier? = null
/* return element i,j of infinite matrix A */
private fun eval_A(i: Int, j: Int): Double {
val div = ((i + j) * (i + j + 1)).ushr(1) + i + 1
return 1.0 / div
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment