Skip to content

Commit

Permalink
Support for jdk-21
Browse files Browse the repository at this point in the history
  • Loading branch information
cnuernber committed Feb 20, 2024
1 parent 6415407 commit 844df69
Show file tree
Hide file tree
Showing 10 changed files with 574 additions and 101 deletions.
123 changes: 61 additions & 62 deletions src/tech/v3/datatype/casting.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns tech.v3.datatype.casting
(:refer-clojure :exclude [cast])
(:require [clojure.set :as c-set]
[ham-fisted.lazy-noncaching :as lznc]
[clj-commons.primitive-math :as pmath]
[tech.v3.datatype.protocols :as dtype-proto]
[tech.v3.datatype.errors :as errors])
Expand All @@ -15,7 +16,9 @@
(set! *warn-on-reflection* true)
(set! *unchecked-math* :warn-on-boxed)

(declare rebuild-valid-datatypes!)

(defonce ^{:tag Set} valid-datatype-set (ConcurrentHashMap/newKeySet))
(defn- add-valid-datatype [dtype] (.add valid-datatype-set dtype))

(defonce aliased-datatypes (ConcurrentHashMap.))

Expand All @@ -24,7 +27,7 @@
"Alias a new datatype to a base datatype. Only useful for primitive datatypes"
[new-dtype old-dtype]
(.put ^Map aliased-datatypes new-dtype old-dtype)
(rebuild-valid-datatypes!))
(add-valid-datatype new-dtype))


(defn un-alias-datatype
Expand All @@ -43,7 +46,7 @@
([datatype klass implement-protocols?]
(.put datatype->class-map datatype klass)
(.put class->datatype-map klass datatype)
(rebuild-valid-datatypes!)
(add-valid-datatype datatype)
(when implement-protocols?
(clojure.core/extend klass
dtype-proto/PElemwiseDatatype
Expand Down Expand Up @@ -73,36 +76,25 @@
:int32 :uint32
:int64 :uint64})

(def unsigned-signed (c-set/map-invert signed-unsigned))

(def float-types #{:float32 :float64})

(def int-types (set (concat (flatten (seq signed-unsigned)))))
(defmacro cdef
"Eval the code at compile time and just output constant - saves initialization time."
[name code]
(let [v (eval code)]
`(def ~name ~v)))

(def signed-int-types (set (keys signed-unsigned)))
(cdef unsigned-signed (c-set/map-invert signed-unsigned))

(def unsigned-int-types (set (vals signed-unsigned)))
(cdef float-types #{:float32 :float64})

(def host-numeric-types (set (concat signed-int-types float-types)))
(cdef int-types (set (concat (flatten (seq signed-unsigned)))))

(def numeric-types (set (concat host-numeric-types unsigned-int-types)))
(cdef signed-int-types (set (keys signed-unsigned)))

(cdef unsigned-int-types (set (vals signed-unsigned)))

(defonce valid-datatype-set (atom nil))
(cdef host-numeric-types (set (concat signed-int-types float-types)))

(defn ->hash-set
[data]
(doto (HashSet.)
(.addAll data)))

(defn rebuild-valid-datatypes!
[]
(reset! valid-datatype-set
(->> (concat (keys datatype->class-map)
(keys aliased-datatypes)
numeric-types
[:object :boolean :char])
(->hash-set))))
(cdef numeric-types (set (concat host-numeric-types unsigned-int-types)))


(defn base-host-datatype?
Expand All @@ -121,8 +113,24 @@

(defn valid-datatype?
[datatype]
(or (base-host-datatype? datatype)
(.contains ^Set @valid-datatype-set datatype)))
(case datatype
:int64 true
:float64 true
:int32 true
:int8 true
:int16 true
:uint8 true
:uint16 true
:uint32 true
:uint64 true
:char true
:float32 true
:boolean true
:object true
:string true
:keyword true
:uuid true
(.contains valid-datatype-set datatype)))


(defn ensure-valid-datatype
Expand All @@ -131,7 +139,7 @@
(throw (Exception. (format "Invalid datatype: %s" datatype)))))


(def primitive-types (set (concat numeric-types [:boolean])))
(cdef primitive-types (set (concat numeric-types [:boolean])))


(defn- set->hash-set
Expand All @@ -141,16 +149,16 @@
retval))


(def base-host-datatypes (set (concat host-numeric-types
(cdef base-host-datatypes (set (concat host-numeric-types
[:object :boolean])))


(def base-datatypes (set (concat host-numeric-types
(cdef base-datatypes (set (concat host-numeric-types
unsigned-int-types
[:boolean :char :object])))

(def ^{:tag HashSet} base-host-datatypes-hashset
(set->hash-set base-host-datatypes))
(cdef ^{:tag HashSet} base-host-datatypes-hashset
(set->hash-set base-host-datatypes))


(defn int-width
Expand Down Expand Up @@ -407,9 +415,9 @@
[]
(keys @*cast-table*))

(def all-host-datatypes
(set (concat host-numeric-types
[:boolean :object])))
(cdef all-host-datatypes
(set (concat host-numeric-types
[:boolean :object])))


(alias-datatype! :double :float64)
Expand Down Expand Up @@ -573,38 +581,29 @@



(def type-tree (atom {}))


(defn- set-conj
[container item]
(if container
(conj container item)
#{item}))


(defn add-type-pair
[child-type parent-type]
(swap! type-tree update child-type set-conj parent-type))


(def default-type-pairs
[[:boolean :int8]
[:int8 :int16]
[:uint8 :int16]
[:int16 :int32]
[:uint16 :int32]
[:int32 :int64]
[:uint32 :int64]
[:int64 :float64]
[:uint64 :float64]
[:float32 :float64]
[:int16 :float32]
[:uint16 :float32]])


(doseq [[child parent] default-type-pairs]
(add-type-pair child parent))
(cdef type-tree
(reduce (fn [m [child-type parent-type]]
(update m child-type set-conj parent-type))
{}
[[:boolean :int8]
[:int8 :int16]
[:uint8 :int16]
[:int16 :int32]
[:uint16 :int32]
[:int32 :int64]
[:uint32 :int64]
[:int64 :float64]
[:uint64 :float64]
[:float32 :float64]
[:int16 :float32]
[:uint16 :float32]]))


(def type-path->root
Expand All @@ -621,7 +620,7 @@
([datatype]
(->>
(conj
(type-path->root datatype [] @type-tree)
(type-path->root datatype [] type-tree)
:object)
(reverse)
(distinct)
Expand Down
4 changes: 1 addition & 3 deletions src/tech/v3/datatype/datetime_api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ A general outline is:
- datetime->milliseconds, milliseconds->datetime
- plus-temporal-amount, minus-temporal-amount, long-temporal-amount
- between"
(:require [tech.v3.datatype.datetime.constants]
[tech.v3.datatype.datetime.operations]
[tech.v3.datatype.export-symbols
(:require [tech.v3.datatype.export-symbols
:refer [export-symbols] :as export-symbols]))


Expand Down
17 changes: 11 additions & 6 deletions src/tech/v3/datatype/ffi.clj
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,8 @@ Finally - on my favorite topic, efficiency, dtype-next has extremely fast copies
* :jna - namespace `''tech.v3.datatype.ffi.jna/ffi-fns` - available if JNA version 5+ is in the classpath."
[ffi-kwd]
(case ffi-kwd
:jdk (reset! ffi-impl* @(requiring-resolve 'tech.v3.datatype.ffi.mmodel-jdk19/ffi-fns))
:jdk-21 (reset! ffi-impl* @(requiring-resolve 'tech.v3.datatype.ffi.mmodel-jdk21/ffi-fns))
:jdk-19 (reset! ffi-impl* @(requiring-resolve 'tech.v3.datatype.ffi.mmodel-jdk19/ffi-fns))
:jdk-pre-19 (reset! ffi-impl* @(requiring-resolve 'tech.v3.datatype.ffi.mmodel/ffi-fns))
:jna (reset! ffi-impl* @(requiring-resolve 'tech.v3.datatype.ffi.jna/ffi-fns))))

Expand All @@ -422,15 +423,19 @@ Finally - on my favorite topic, efficiency, dtype-next has extremely fast copies
(catch Throwable e
(log/debugf e "Failed to load JNA FFI implementation")
(try
(set-ffi-impl! :jdk)
(set-ffi-impl! :jdk-21)
(catch Throwable e
(log/debugf "Failed to load JDK 19 FFI implementation: %s" e)
(log/debugf "Failed to load JDK 21 FFI implementation: %s" e)
(try
(set-ffi-impl! :jdk-pre-19)
(set-ffi-impl! :jdk-19)
(catch Throwable e
(log/error e "Failed to find a suitable FFI implementation.
(log/debugf "Failed to load JDK 19 FFI implementation: %s" e)
(try
(set-ffi-impl! :jdk-pre-19)
(catch Throwable e
(log/error e "Failed to find a suitable FFI implementation.
Attempted :jdk, :jdk-pre-19, and :jna -- call set-ffi-impl! from the repl to see specific failure.)")
(reset! ffi-impl* :failed))))))))
(reset! ffi-impl* :failed))))))))))
@ffi-impl*)


Expand Down
Loading

0 comments on commit 844df69

Please sign in to comment.