Skip to content

Commit

Permalink
Working through system to define library symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
cnuernber committed Mar 21, 2021
1 parent 8fb29ce commit 274d850
Show file tree
Hide file tree
Showing 7 changed files with 304 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ pom.xml.asc
graalvm*
tech.v3.datatype.main
resources
jdk-16
jdk-16
library_test
53 changes: 53 additions & 0 deletions native_test/tech/v3/datatype/expose_fn.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
(ns tech.v3.datatype.expose-fn
(:require [tech.v3.datatype :as dtype]
[tech.v3.datatype.native-buffer :as native-buffer])
(:import [tech.v3.datatype.ffi Pointer]))


(defn add-two-nums
[num-a num-b]
(+ num-a num-b))


(defn allocate-buffer
[len]
;;setup buffer to be manually freed.
(when-not (== 0 len)
(dtype/make-container :native-heap :int8 {:resource-type nil} len)))


(defn set-buffer
[buffer len val]
(-> (native-buffer/wrap-address (.address ^Pointer buffer) len nil)
(dtype/set-constant! val)))


(defn free-buffer
[buffer]
(when (and buffer (not= 0 (.address ^Pointer buffer)))
(native-buffer/free (native-buffer/wrap-address buffer 1 nil))))



(comment
(do
(require '[tech.v3.datatype.ffi.graalvm :as graalvm])
(def cls-def
(with-bindings {#'*compile-path* "library_test/classes"}
(graalvm/expose-clojure-functions
{#'add-two-nums {:rettype :int64
:argtypes [['lhs :int64]
['rhs :int64]]}
#'allocate-buffer {:rettype :pointer?
:argtypes [['len :int64]]}
#'set-buffer {:rettype :void
:argtypes [['buffer :pointer]
['len :int64]
['val :int8]]}
#'free-buffer {:rettype :void
:argtypes [['buffer :pointer?]]}

}
'libdtype.ExposeFnTest
{:instantiate? true})))
))
13 changes: 1 addition & 12 deletions native_test/tech/v3/datatype/main.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
(ns tech.v3.datatype.main
(:require [tech.v3.datatype :as dtype]
[tech.v3.datatype.native-buffer :as native-buffer]
[tech.v3.datatype.ffi :as dt-ffi]
(:require [tech.v3.datatype.expose-fn]
[tech.v3.datatype.ffi.graalvm-runtime :as graalvm-runtime])
(:import [clojure.lang RT])
(:gen-class))
Expand All @@ -26,15 +24,6 @@
:instantiate? true})))))
)

(import 'tech.v3.datatype.GraalNativeGen)

(defn -main
[& args]
(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)
6 changes: 3 additions & 3 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
:url "http://github.com/cnuernber/dtype-next"
:license {:name "EPL-2.0"
:url "https://www.eclipse.org/legal/epl-2.0/"}
:dependencies [[org.clojure/clojure "1.10.2-alpha1"]
:dependencies [[org.clojure/clojure "1.10.2"]
[org.ow2.asm/asm "9.0"]
[insn "0.5.1"]
[insn "0.5.2-SNAPSHOT"]
[camel-snake-kebab "0.4.2"]
[primitive-math "0.1.6"]
[it.unimi.dsi/fastutil "8.2.1"]
Expand Down Expand Up @@ -64,7 +64,7 @@
:dependencies [[org.clojure/core.async "1.3.610"]
[com.taoensso/nippy "3.1.0-RC1"]]}
:uberjar
{:aot [tech.v3.datatype.main]
{:aot [tech.v3.datatype.main tech.v3.datatype.expose-fn]
: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"
Expand Down
24 changes: 24 additions & 0 deletions scripts/compile-shared
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

set -e

lein clean
lein uberjar

pushd library_test

$GRAALVM_HOME/bin/native-image \
--report-unsupported-elements-at-runtime \
--initialize-at-build-time \
--no-fallback \
--no-server \
-H:+ReportExceptionStackTraces \
-J-Dclojure.spec.skip-macros=true \
-J-Dclojure.compiler.direct-linking=true \
-J-Dtech.v3.datatype.graal-native=true \
-jar ../target/dtype-next.jar \
-cp classes \
-H:Name=libdtype \
--shared

popd
11 changes: 7 additions & 4 deletions scripts/get-graal
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#!/bin/bash

wget https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-20.2.0/graalvm-ce-java8-linux-amd64-20.2.0.tar.gz
tar -xvzf graalvm-ce-java8-linux-amd64-20.2.0.tar.gz
ln -s "$(pwd)/graalvm-ce-java8-20.2.0" "$(pwd)/graalvm"
rm graalvm-ce-java8-linux-amd64-20.2.0.tar.gz

GRAAL_VERSION="21.0.0.2"

wget https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-$GRAAL_VERSION/graalvm-ce-java8-linux-amd64-$GRAAL_VERSION.tar.gz
tar -xvzf graalvm-ce-java8-linux-amd64-$GRAAL_VERSION.tar.gz
ln -s "$(pwd)/graalvm-ce-java8-$GRAAL_VERSION" "$(pwd)/graalvm"
rm graalvm-ce-java8-linux-amd64-$GRAAL_VERSION.tar.gz
graalvm/bin/gu install native-image
Loading

0 comments on commit 274d850

Please sign in to comment.