Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mhammons committed Sep 17, 2017
0 parents commit 14c98ed
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
@@ -0,0 +1,3 @@
Requires SBT to use

sbt "jmh:run -i 10 -wi 20 -f1 -t1 -r 15" is used to perform the benchmarking at the moment
5 changes: 5 additions & 0 deletions build.sbt
@@ -0,0 +1,5 @@
enablePlugins(JmhPlugin)

fork := true

scalaVersion := "2.12.3"
1 change: 1 addition & 0 deletions project/build.properties
@@ -0,0 +1 @@
sbt.version=0.13.16
1 change: 1 addition & 0 deletions project/plugins.sbt
@@ -0,0 +1 @@
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.2.27")
127 changes: 127 additions & 0 deletions src/main/java/org/debian/alioth/benchmarksgame/binarytrees.java
@@ -0,0 +1,127 @@
package org.debian.alioth.benchmarksgame;

/**
* The Computer Language Benchmarks Game
* http://benchmarksgame.alioth.debian.org/
*
* based on Jarkko Miettinen's Java program
* contributed by Tristan Dupont
* *reset*
*/

import java.io.File;
import java.io.PrintStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.openjdk.jmh.annotations.*;


public class binarytrees {

@State(Scope.Thread)
public static class binarytreesState {
public final int MIN_DEPTH = 4;
public ExecutorService EXECUTOR_SERVICE;

@TearDown(Level.Invocation)
public void teardown() {
try {
printStream.close();
} catch (Exception e) {

}
}

@Setup(Level.Invocation)
public void setup() {
try {
EXECUTOR_SERVICE = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
printStream = new PrintStream(new File("/dev/null"));
} catch (Exception e) {

}
}


public PrintStream printStream;

}


@Benchmark
@BenchmarkMode({Mode.SingleShotTime, Mode.SampleTime, Mode.AverageTime}) @OutputTimeUnit(TimeUnit.MILLISECONDS)
public void main(final binarytreesState state) throws Exception {
int n = 21;

final int maxDepth = n < (state.MIN_DEPTH + 2) ? state.MIN_DEPTH + 2 : n;
final int stretchDepth = maxDepth + 1;

state.printStream.println("stretch tree of depth " + stretchDepth + "\t check: "
+ bottomUpTree( stretchDepth).itemCheck());

final TreeNode longLivedTree = bottomUpTree(maxDepth);

final String[] results = new String[(maxDepth - state.MIN_DEPTH) / 2 + 1];

for (int d = state.MIN_DEPTH; d <= maxDepth; d += 2) {
final int depth = d;
state.EXECUTOR_SERVICE.execute(() -> {
int check = 0;

final int iterations = 1 << (maxDepth - depth + state.MIN_DEPTH);
for (int i = 1; i <= iterations; ++i) {
final TreeNode treeNode1 = bottomUpTree(depth);
check += treeNode1.itemCheck();
}
results[(depth - state.MIN_DEPTH) / 2] =
iterations + "\t trees of depth " + depth + "\t check: " + check;
});
}

state.EXECUTOR_SERVICE.shutdown();
state.EXECUTOR_SERVICE.awaitTermination(125L, TimeUnit.SECONDS);



for (final String str : results) {
state.printStream.println(str);
}

state.printStream.println("long lived tree of depth " + maxDepth +
"\t check: " + longLivedTree.itemCheck());
}

private TreeNode bottomUpTree(final int depth) {
if (0 < depth) {
return new TreeNode(bottomUpTree(depth - 1), bottomUpTree(depth - 1));
}
return new TreeNode();
}

private final class TreeNode {

private final TreeNode left;
private final TreeNode right;

private TreeNode(final TreeNode left, final TreeNode right) {
this.left = left;
this.right = right;
}

private TreeNode() {
this(null, null);
}

private int itemCheck() {
// if necessary deallocate here
if (null == left) {
return 1;
}
return 1 + left.itemCheck() + right.itemCheck();
}

}

}
85 changes: 85 additions & 0 deletions src/main/java/org/debian/alioth/benchmarksgame/mandelbrot.java
@@ -0,0 +1,85 @@
package org.debian.alioth.benchmarksgame;
/* The Computer Language Benchmarks Game
* http://benchmarksgame.alioth.debian.org/
*
* contributed by Stefan Krause
* slightly modified by Chad Whipkey
* parallelized by Colin D Bennett 2008-10-04
* reduce synchronization cost by The Anh Tran
* optimizations and refactoring by Enotus 2010-11-11
* optimization by John Stalcup 2012-2-19
*/


import mhammons.cnrs.Benchable;
import org.openjdk.jmh.annotations.Scope;

import java.io.*;
import java.util.concurrent.atomic.*;


public class mandelbrot {
byte[][] out;
AtomicInteger yCt;
double[] Crb;
double[] Cib;

OutputStream output = System.out;

int getByte(int x, int y){
int res=0;
for(int i=0;i<8;i+=2){
double Zr1=Crb[x+i];
double Zi1=Cib[y];

double Zr2=Crb[x+i+1];
double Zi2=Cib[y];

int b=0;
int j=49;do{
double nZr1=Zr1*Zr1-Zi1*Zi1+Crb[x+i];
double nZi1=Zr1*Zi1+Zr1*Zi1+Cib[y];
Zr1=nZr1;Zi1=nZi1;

double nZr2=Zr2*Zr2-Zi2*Zi2+Crb[x+i+1];
double nZi2=Zr2*Zi2+Zr2*Zi2+Cib[y];
Zr2=nZr2;Zi2=nZi2;

if(Zr1*Zr1+Zi1*Zi1>4){b|=2;if(b==3)break;}
if(Zr2*Zr2+Zi2*Zi2>4){b|=1;if(b==3)break;}
}while(--j>0);
res=(res<<2)+b;
}
return res^-1;
}

void putLine(int y, byte[] line){
for (int xb=0; xb<line.length; xb++)
line[xb]=(byte)getByte(xb*8,y);
}

public void main(String[] args) throws Exception {
int N=6000;
if (args.length>=1) N=Integer.parseInt(args[0]);

Crb=new double[N+7]; Cib=new double[N+7];
double invN=2.0/N; for(int i=0;i<N;i++){ Cib[i]=i*invN-1.0; Crb[i]=i*invN-1.5; }
yCt=new AtomicInteger();
out=new byte[N][(N+7)/8];

Thread[] pool=new Thread[2*Runtime.getRuntime().availableProcessors()];
for (int i=0;i<pool.length;i++)
pool[i]=new Thread(){
public void run() {
int y; while((y=yCt.getAndIncrement())<out.length) putLine(y,out[y]);
}
};
for (Thread t:pool) t.start();
for (Thread t:pool) t.join();

OutputStream stream = new BufferedOutputStream(output);
stream.write(("P4\n"+N+" "+N+"\n").getBytes());
for(int i=0;i<N;i++) stream.write(out[i]);
stream.close();
}
}
10 changes: 10 additions & 0 deletions src/main/scala/mhammons/cnrs/Benchable.scala
@@ -0,0 +1,10 @@
package mhammons.cnrs

import java.io.{OutputStream, PrintStream}

trait Benchable {
def setOut(ps: PrintStream): Unit

@throws(classOf[Exception])
def main(args: Array[String]): Unit
}
19 changes: 19 additions & 0 deletions src/main/scala/mhammons/cnrs/BenchmarkState.scala
@@ -0,0 +1,19 @@
package mhammons.cnrs

import java.io.{File, PrintStream}

import org.openjdk.jmh.annotations.{Level, Setup, TearDown}

trait BenchmarkState {
val devNull = new File("/dev/null")
var ps: PrintStream = null

def doSetup(): Unit = {
ps = new PrintStream(devNull)
}

def doTeardown(): Unit = {
ps.close()
ps = null
}
}
15 changes: 15 additions & 0 deletions src/main/scala/mhammons/cnrs/Run.scala
@@ -0,0 +1,15 @@
package mhammons.cnrs

import java.io.{File, PrintStream}
import java.util.concurrent.TimeUnit

import org.debian.alioth.benchmarksgame.{binarytrees, mandelbrot}
import org.openjdk.jmh.annotations._
import org.openjdk.jmh.runner.ForkedRunner


class Run {


}

0 comments on commit 14c98ed

Please sign in to comment.