Skip to content

Commit

Permalink
Removed JNA usage
Browse files Browse the repository at this point in the history
  • Loading branch information
yukoba committed Apr 5, 2020
1 parent cef020c commit 114bdbc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
34 changes: 28 additions & 6 deletions llvm/samples/MatMulBenchmark/MatMulBenchmark.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import org.bytedeco.javacpp.BytePointer;
import org.bytedeco.javacpp.Pointer;
import org.bytedeco.javacpp.PointerPointer;
import org.bytedeco.javacpp.*;
import org.bytedeco.javacpp.annotation.Allocator;
import org.bytedeco.javacpp.annotation.Platform;
import org.bytedeco.javacpp.tools.Builder;
import org.bytedeco.llvm.LLVM.*;

import java.util.Random;
Expand All @@ -18,6 +19,7 @@
* Warning: This code is slower than this.
* clang -O3 -march=native -mllvm -polly -mllvm -polly-vectorizer=stripmine
*/
@Platform
public class MatMulBenchmark {
static final int M = 2000, N = 2000, K = 2000;
static final boolean usePolly = true;
Expand Down Expand Up @@ -62,6 +64,14 @@ static void benchmarkPureJava(float[] a, float[] b, float[] c) {
}

static void initialize() {
try {
Class<MatMulBenchmark> clazz = MatMulBenchmark.class;
new Builder().classesOrPackages(clazz.getName()).build();
Loader.load(clazz);
} catch (Exception e) {
throw new RuntimeException(e);
}

if (usePolly) {
if (usePollyParallel) {
LLVMLoadLibraryPermanently("libgomp.so.1"); // This file name depends on your machine
Expand All @@ -88,6 +98,16 @@ static void initialize() {
llvmFloatPointerType = LLVMPointerType(llvmFloatType, 0);
}

@Allocator
static class MatMulFunction extends FunctionPointer {
public MatMulFunction(Pointer p) { super(p); }
public MatMulFunction() { allocate(); }
private native void allocate();
public native void call(float[] a, float[] b, float[] c);
public native Pointer get();
public native MatMulFunction put(Pointer address);
}

static void benchmarkLLVM(float[] a, float[] b, float[] c) {
assert a.length == M * K;
assert b.length == K * N;
Expand All @@ -100,10 +120,12 @@ static void benchmarkLLVM(float[] a, float[] b, float[] c) {
verify(module, false);
jitCompile(engine, module);

long fnAddr = LLVMGetFunctionAddress(engine, "matmul");
com.sun.jna.Function func = com.sun.jna.Function.getFunction(new com.sun.jna.Pointer(fnAddr));
final long fnAddr = LLVMGetFunctionAddress(engine, "matmul");
MatMulFunction func = new MatMulFunction().put(new Pointer() {{
address = fnAddr;
}});
long start = System.nanoTime();
func.invoke(Void.class, new Object[]{a, b, c});
func.call(a, b, c);
long end = System.nanoTime();
System.out.printf("LLVM%s: %fms. c[0] = %f\n",
usePolly ? " with Polly" : " without Polly",
Expand Down
5 changes: 0 additions & 5 deletions llvm/samples/MatMulBenchmark/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
<artifactId>llvm-platform</artifactId>
<version>10.0.0-1.5.3-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.5.0</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>.</sourceDirectory>
Expand Down

0 comments on commit 114bdbc

Please sign in to comment.