Skip to content

Commit

Permalink
Add init/read timing for Java
Browse files Browse the repository at this point in the history
Upgrade to TornadoVM 0.15 API
  • Loading branch information
tom91136 committed Oct 7, 2023
1 parent 971d1e8 commit 3de019c
Show file tree
Hide file tree
Showing 14 changed files with 210 additions and 83 deletions.
8 changes: 4 additions & 4 deletions src/java/java-stream/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<junit.version>5.7.2</junit.version>
<junit.version>5.9.2</junit.version>
</properties>

<repositories>
Expand All @@ -27,19 +27,19 @@
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.81</version>
<version>1.82</version>
</dependency>

<dependency>
<groupId>tornado</groupId>
<artifactId>tornado-api</artifactId>
<version>0.9</version>
<version>0.15.1</version>
</dependency>

<dependency>
<groupId>com.aparapi</groupId>
<artifactId>aparapi</artifactId>
<version>2.0.0</version>
<version>3.0.0</version>
<exclusions>
<!-- don't pull in the entire Scala ecosystem! -->
<exclusion>
Expand Down
14 changes: 11 additions & 3 deletions src/java/java-stream/src/main/java/javastream/JavaStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected JavaStream(Config<T> config) {

protected abstract T dot();

protected abstract Data<T> data();
protected abstract Data<T> readArrays();

public static class EnumeratedStream<T> extends JavaStream<T> {

Expand Down Expand Up @@ -113,8 +113,8 @@ public T dot() {
}

@Override
public Data<T> data() {
return actual.data();
public Data<T> readArrays() {
return actual.readArrays();
}
}

Expand All @@ -140,6 +140,14 @@ private static Duration timed(Runnable f) {
return Duration.ofNanos(end - start);
}

final Duration runInitArrays() {
return timed(this::initArrays);
}

final SimpleImmutableEntry<Duration, Data<T>> runReadArrays() {
return timed(this::readArrays);
}

final SimpleImmutableEntry<Timings<Duration>, T> runAll(int times) {
Timings<Duration> timings = new Timings<>();
T lastSum = null;
Expand Down
93 changes: 69 additions & 24 deletions src/java/java-stream/src/main/java/javastream/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,40 @@ static final class Implementation {
}
}

@SuppressWarnings("unchecked")
static void showInit(
int totalBytes, double megaScale, Options opt, Duration init, Duration read) {
List<Entry<String, Double>> setup =
Arrays.asList(
new SimpleImmutableEntry<>("Init", durationToSeconds(init)),
new SimpleImmutableEntry<>("Read", durationToSeconds(read)));
if (opt.csv) {
tabulateCsv(
true,
setup.stream()
.map(
x ->
Arrays.asList(
new SimpleImmutableEntry<>("function", x.getKey()),
new SimpleImmutableEntry<>("n_elements", opt.arraysize + ""),
new SimpleImmutableEntry<>("sizeof", totalBytes + ""),
new SimpleImmutableEntry<>(
"max_m" + (opt.mibibytes ? "i" : "") + "bytes_per_sec",
((megaScale * (double) totalBytes / x.getValue())) + ""),
new SimpleImmutableEntry<>("runtime", x.getValue() + "")))
.toArray(List[]::new));
} else {
for (Entry<String, Double> e : setup) {
System.out.printf(
"%s: %.5f s (%.5f M%sBytes/sec)%n",
e.getKey(),
e.getValue(),
megaScale * (double) totalBytes / e.getValue(),
opt.mibibytes ? "i" : "");
}
}
}

static <T extends Number> boolean run(
String name, Config<T> config, Function<Config<T>, JavaStream<T>> mkStream) {

Expand Down Expand Up @@ -183,35 +217,46 @@ static <T extends Number> boolean run(

JavaStream<T> stream = mkStream.apply(config);

stream.initArrays();

Duration init = stream.runInitArrays();
final boolean ok;
switch (config.benchmark) {
case ALL:
Entry<Timings<Duration>, T> results = stream.runAll(opt.numtimes);
ok = checkSolutions(stream.data(), config, Optional.of(results.getValue()));
Timings<Duration> timings = results.getKey();
tabulateCsv(
opt.csv,
mkCsvRow(timings.copy, "Copy", 2 * arrayBytes, megaScale, opt),
mkCsvRow(timings.mul, "Mul", 2 * arrayBytes, megaScale, opt),
mkCsvRow(timings.add, "Add", 3 * arrayBytes, megaScale, opt),
mkCsvRow(timings.triad, "Triad", 3 * arrayBytes, megaScale, opt),
mkCsvRow(timings.dot, "Dot", 2 * arrayBytes, megaScale, opt));
break;
{
Entry<Timings<Duration>, T> results = stream.runAll(opt.numtimes);
SimpleImmutableEntry<Duration, Data<T>> read = stream.runReadArrays();
showInit(totalBytes, megaScale, opt, init, read.getKey());
ok = checkSolutions(read.getValue(), config, Optional.of(results.getValue()));
Timings<Duration> timings = results.getKey();
tabulateCsv(
opt.csv,
mkCsvRow(timings.copy, "Copy", 2 * arrayBytes, megaScale, opt),
mkCsvRow(timings.mul, "Mul", 2 * arrayBytes, megaScale, opt),
mkCsvRow(timings.add, "Add", 3 * arrayBytes, megaScale, opt),
mkCsvRow(timings.triad, "Triad", 3 * arrayBytes, megaScale, opt),
mkCsvRow(timings.dot, "Dot", 2 * arrayBytes, megaScale, opt));
break;
}
case NSTREAM:
List<Duration> nstreamResults = stream.runNStream(opt.numtimes);
ok = checkSolutions(stream.data(), config, Optional.empty());
tabulateCsv(opt.csv, mkCsvRow(nstreamResults, "Nstream", 4 * arrayBytes, megaScale, opt));
break;
{
List<Duration> nstreamResults = stream.runNStream(opt.numtimes);
SimpleImmutableEntry<Duration, Data<T>> read = stream.runReadArrays();
showInit(totalBytes, megaScale, opt, init, read.getKey());
ok = checkSolutions(read.getValue(), config, Optional.empty());
tabulateCsv(opt.csv, mkCsvRow(nstreamResults, "Nstream", 4 * arrayBytes, megaScale, opt));
break;
}
case TRIAD:
Duration triadResult = stream.runTriad(opt.numtimes);
ok = checkSolutions(stream.data(), config, Optional.empty());
int triadTotalBytes = 3 * arrayBytes * opt.numtimes;
double bandwidth = megaScale * (triadTotalBytes / durationToSeconds(triadResult));
System.out.printf("Runtime (seconds): %.5f", durationToSeconds(triadResult));
System.out.printf("Bandwidth (%s/s): %.3f ", gigaSuffix, bandwidth);
break;
{
Duration triadResult = stream.runTriad(opt.numtimes);
SimpleImmutableEntry<Duration, Data<T>> read = stream.runReadArrays();
showInit(totalBytes, megaScale, opt, init, read.getKey());
ok = checkSolutions(read.getValue(), config, Optional.empty());
int triadTotalBytes = 3 * arrayBytes * opt.numtimes;
double bandwidth = megaScale * (triadTotalBytes / durationToSeconds(triadResult));
System.out.printf("Runtime (seconds): %.5f", durationToSeconds(triadResult));
System.out.printf("Bandwidth (%s/s): %.3f ", gigaSuffix, bandwidth);
break;
}
default:
throw new AssertionError();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public T dot() {
}

@Override
public Data<T> data() {
public Data<T> readArrays() {
return kernels.syncAndDispose();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public T dot() {
}

@Override
public Data<T> data() {
public Data<T> readArrays() {
return new Data<>(a, b, c);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public T dot() {
}

@Override
public Data<T> data() {
public Data<T> readArrays() {
return new Data<>(a, b, c);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public Double dot() {
}

@Override
public Data<Double> data() {
public Data<Double> readArrays() {
return new Data<>(boxed(a), boxed(b), boxed(c));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public Float dot() {
}

@Override
public Data<Float> data() {
public Data<Float> readArrays() {
return new Data<>(boxed(a), boxed(b), boxed(c));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public Double dot() {
}

@Override
public Data<Double> data() {
public Data<Double> readArrays() {
return new Data<>(boxed(a), boxed(b), boxed(c));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public Float dot() {
}

@Override
public Data<Float> data() {
public Data<Float> readArrays() {
return new Data<>(boxed(a), boxed(b), boxed(c));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@
import java.util.stream.Collectors;
import javastream.JavaStream;
import javastream.Main.Config;
import uk.ac.manchester.tornado.api.TaskSchedule;
import uk.ac.manchester.tornado.api.TornadoRuntimeCI;
import uk.ac.manchester.tornado.api.TornadoExecutionPlan;
import uk.ac.manchester.tornado.api.TornadoRuntimeInterface;
import uk.ac.manchester.tornado.api.common.TornadoDevice;
import uk.ac.manchester.tornado.api.runtime.TornadoRuntime;

abstract class GenericTornadoVMStream<T> extends JavaStream<T> {

protected final TornadoDevice device;

protected TaskSchedule copyTask;
protected TaskSchedule mulTask;
protected TaskSchedule addTask;
protected TaskSchedule triadTask;
protected TaskSchedule nstreamTask;
protected TaskSchedule dotTask;
protected TornadoExecutionPlan copyTask;
protected TornadoExecutionPlan mulTask;
protected TornadoExecutionPlan addTask;
protected TornadoExecutionPlan triadTask;
protected TornadoExecutionPlan nstreamTask;
protected TornadoExecutionPlan dotTask;

GenericTornadoVMStream(Config<T> config) {
super(config);

try {
TornadoRuntimeCI runtime = TornadoRuntime.getTornadoRuntime();
TornadoRuntimeInterface runtime = TornadoRuntime.getTornadoRuntime();
List<TornadoDevice> devices = TornadoVMStreams.enumerateDevices(runtime);
device = devices.get(config.options.device);

Expand All @@ -42,10 +42,6 @@ abstract class GenericTornadoVMStream<T> extends JavaStream<T> {
}
}

protected static TaskSchedule mkSchedule() {
return new TaskSchedule("");
}

@Override
public List<String> listDevices() {
return TornadoVMStreams.enumerateDevices(TornadoRuntime.getTornadoRuntime()).stream()
Expand All @@ -55,12 +51,12 @@ public List<String> listDevices() {

@Override
public void initArrays() {
this.copyTask.warmup();
this.mulTask.warmup();
this.addTask.warmup();
this.triadTask.warmup();
this.nstreamTask.warmup();
this.dotTask.warmup();
this.copyTask.withWarmUp();
this.mulTask.withWarmUp();
this.addTask.withWarmUp();
this.triadTask.withWarmUp();
this.nstreamTask.withWarmUp();
this.dotTask.withWarmUp();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import java.util.Arrays;
import javastream.Main.Config;
import uk.ac.manchester.tornado.api.TaskGraph;
import uk.ac.manchester.tornado.api.TornadoExecutionPlan;
import uk.ac.manchester.tornado.api.annotations.Parallel;
import uk.ac.manchester.tornado.api.annotations.Reduce;
import uk.ac.manchester.tornado.api.enums.DataTransferMode;

final class SpecialisedDouble extends GenericTornadoVMStream<Double> {

Expand Down Expand Up @@ -49,7 +52,7 @@ private static void dot_(
private final double[] a, b, c;
private final double[] dotSum;

@SuppressWarnings({"PrimitiveArrayArgumentToVarargsMethod", "DuplicatedCode"})
@SuppressWarnings({"DuplicatedCode"})
SpecialisedDouble(Config<Double> config) {
super(config);
final int size = config.options.arraysize;
Expand All @@ -58,12 +61,43 @@ private static void dot_(
b = new double[size];
c = new double[size];
dotSum = new double[1];
this.copyTask = mkSchedule().task("", SpecialisedDouble::copy, size, a, c);
this.mulTask = mkSchedule().task("", SpecialisedDouble::mul, size, b, c, scalar);
this.addTask = mkSchedule().task("", SpecialisedDouble::add, size, a, b, c);
this.triadTask = mkSchedule().task("", SpecialisedDouble::triad, size, a, b, c, scalar);
this.nstreamTask = mkSchedule().task("", SpecialisedDouble::nstream, size, a, b, c, scalar);
this.dotTask = mkSchedule().task("", SpecialisedDouble::dot_, a, b, dotSum).streamOut(dotSum);
this.copyTask =
new TornadoExecutionPlan(
new TaskGraph("copy")
.task("copy", SpecialisedDouble::copy, size, a, c)
.transferToDevice(DataTransferMode.FIRST_EXECUTION, a, c)
.snapshot());
this.mulTask =
new TornadoExecutionPlan(
new TaskGraph("mul")
.task("mul", SpecialisedDouble::mul, size, b, c, scalar)
.transferToDevice(DataTransferMode.FIRST_EXECUTION, b, c)
.snapshot());
this.addTask =
new TornadoExecutionPlan(
new TaskGraph("add")
.task("add", SpecialisedDouble::add, size, a, b, c)
.transferToDevice(DataTransferMode.FIRST_EXECUTION, a, b, c)
.snapshot());
this.triadTask =
new TornadoExecutionPlan(
new TaskGraph("triad")
.task("triad", SpecialisedDouble::triad, size, a, b, c, scalar)
.transferToDevice(DataTransferMode.FIRST_EXECUTION, a, b, c)
.snapshot());
this.nstreamTask =
new TornadoExecutionPlan(
new TaskGraph("nstream")
.task("nstream", SpecialisedDouble::nstream, size, a, b, c, scalar)
.transferToDevice(DataTransferMode.FIRST_EXECUTION, a, b, c)
.snapshot());
this.dotTask =
new TornadoExecutionPlan(
new TaskGraph("dot")
.task("dot", SpecialisedDouble::dot_, a, b, dotSum)
.transferToDevice(DataTransferMode.FIRST_EXECUTION, a, b)
.transferToHost(DataTransferMode.EVERY_EXECUTION, new Object[] {dotSum})
.snapshot());
}

@Override
Expand All @@ -72,7 +106,7 @@ public void initArrays() {
Arrays.fill(a, config.initA);
Arrays.fill(b, config.initB);
Arrays.fill(c, config.initC);
TornadoVMStreams.xferToDevice(device, a, b, c);
TornadoVMStreams.allocAndXferToDevice(device, a, b, c);
}

@Override
Expand All @@ -81,7 +115,7 @@ protected Double getSum() {
}

@Override
public Data<Double> data() {
public Data<Double> readArrays() {
TornadoVMStreams.xferFromDevice(device, a, b, c);
return new Data<>(boxed(a), boxed(b), boxed(c));
}
Expand Down
Loading

0 comments on commit 3de019c

Please sign in to comment.