Skip to content

Commit

Permalink
Graal native ffi (#20)
Browse files Browse the repository at this point in the history
* Stuck at compiler issues.

* Have a minimal call into foreign working.

* Graal native support for ffi v1.
  • Loading branch information
cnuernber authored Mar 19, 2021
1 parent cfe6b81 commit 4a16f22
Show file tree
Hide file tree
Showing 11 changed files with 377 additions and 290 deletions.
17 changes: 15 additions & 2 deletions java/tech/v3/datatype/ClojureHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,26 @@
import clojure.lang.Namespace;
import clojure.lang.IFn;
import clojure.lang.Var;
import clojure.lang.RT;

public class ClojureHelper
{
static boolean initialized = false;
public static boolean initialize() {
//unsynchronized guard. RT.init is synchronized so
//this is faster but still safe.
if (initialized == false) {
initialized = true;
RT.init();
return true;
}
return false;
}
public static IFn findFn(String nsName, String varName) {
try {
return (IFn) ((Var) Compiler.resolveIn(Namespace.find(Symbol.intern(nsName)),
Symbol.intern(varName), false)).deref();
//Without RT initialization .var can return nonsense.
initialize();
return RT.var(nsName, varName);
} catch (Throwable e) {
throw new RuntimeException("Failed to find \"" + nsName + "/" + varName + "\"",
e);
Expand Down
44 changes: 31 additions & 13 deletions native_test/tech/v3/datatype/main.clj
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
(ns tech.v3.datatype.main
(:require [tech.v3.datatype :as dtype]
[tech.v3.datatype.datetime :as dtype-dt]
[tech.v3.tensor :as dtt]
[tech.v3.jna :as jna])
(:import [tech.v3.datatype DirectMapped])
[tech.v3.datatype.native-buffer :as native-buffer]
[tech.v3.datatype.ffi :as dt-ffi]
[tech.v3.datatype.ffi.graalvm-runtime :as graalvm-runtime])
(:import [clojure.lang RT])
(:gen-class))


(set! *warn-on-reflection* true)


(comment
(do
(require '[tech.v3.datatype.ffi.graalvm :as graalvm])
(def lib-def
(with-bindings {#'*compile-path* "generated_classes"}
(do (graalvm/define-library
{:memset {:rettype :pointer
:argtypes [['buffer :pointer]
['byte-value :int32]
['n-bytes :size-t]]}}
[:M_PI]
{:header-files ["<string.h>" "<math.h>"]
:classname 'tech.v3.datatype.GraalNativeGen
:instantiate? true})))))
)

(import 'tech.v3.datatype.GraalNativeGen)

(defn -main
[& args]
(println "loading jna jni lib")
(System/load (str (System/getProperty "user.dir") "/libs/libjnidispatch.so"))
(println "attempting to load native c lib")
(let [ld (dtype-dt/local-date)
native-lib (jna/load-library "c")
data (byte-array 10)]
(com.sun.jna.Native/register DirectMapped native-lib)
(DirectMapped/memset (java.nio.ByteBuffer/wrap data) -1 10)
(println (vec data)))
(let [nbuf (dtype/make-container :native-heap :float32 (range 10))
inst (GraalNativeGen.)]
(.memset inst nbuf 0 40)
(println (graalvm-runtime/ptr-value nbuf))
(println nbuf)
(println (dt-ffi/string->c "hey")))

0)
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
[com.taoensso/nippy "3.1.0-RC1"]]}
:uberjar
{:aot [tech.v3.datatype.main]
:source-paths ["src" "native_test"]
:source-paths ["src" "native_test" "generated_classes"]
:jvm-opts ["-Dclojure.compiler.direct-linking=true" "-Dtech.v3.datatype.graal-native=true"]
:uberjar-name "dtype-next.jar"
:main tech.v3.datatype.main}}
Expand Down
6 changes: 6 additions & 0 deletions scripts/activate-graal
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#!/bin/bash

if [ ! -e graalvm ]; then
get-graal
fi

export GRAALVM_HOME="$(pwd)/graalvm"
export PATH="$(pwd)/graalvm/bin:$PATH"
export JAVA_HOME="$(pwd)/graalvm"
14 changes: 12 additions & 2 deletions src/tech/v3/datatype/ffi.clj
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,12 @@ user> test-buf
[0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, ]
user>
```"
[fn-defs & [options]]
((:define-library (ffi-impl)) fn-defs options))
([fn-defs symbols options]
((:define-library (ffi-impl)) fn-defs symbols options))
([fn-defs options]
((:define-library (ffi-impl)) fn-defs nil options))
([fn-defs]
((:define-library (ffi-impl)) fn-defs nil nil)))


(defn instantiate-library
Expand Down Expand Up @@ -472,6 +476,9 @@ user>
iterative development.")
(library-singleton-set! [lib-singleton libpath]
"Set the library path, generate the library and create a new instance.")
(library-singleton-set-instance!
[lib-singleton libinst]
"In some cases such as graal native pathways you have to hard-set the definition and instance.")
(library-singleton-find-fn [lib-singleton fn-name]
"Find a bound function in the library. Returns an implementation of
clojure.lang.IFn that takes only the specific arguments.")
Expand Down Expand Up @@ -503,6 +510,9 @@ clojure.lang.IFn that takes only the specific arguments.")
(library-singleton-set! [this libpath]
(set! library-path {:libpath libpath})
(library-singleton-reset! this))
(library-singleton-set-instance!
[lib-singleton libinst]
(set! library-instance libinst))
(library-singleton-find-fn [this fn-kwd]
(errors/when-not-errorf
library-instance
Expand Down
Loading

0 comments on commit 4a16f22

Please sign in to comment.