Back
float-array (clj)
(source)function
(float-array size-or-seq)
(float-array size init-val-or-seq)
Creates an array of floats
Examples
mikera/core.matrix
(ns clojure.core.matrix.test-arrays
(:refer-clojure :exclude [vector?])
(:require [clojure.core.matrix.compliance-tester :as compliance]
[clojure.core.matrix :as m]
;[clojure.core.matrix.utils :refer :all]
[clojure.test :refer [deftest testing is]]
[clojure.core.matrix.macros :refer [array?]]))
(deftest compliance-tests
(compliance/instance-test (int-array [1 2 3]))
(compliance/instance-test (float-array [1 2 3]))
(compliance/instance-test (long-array []))
(compliance/instance-test (char-array [\a \b \c]))
(compliance/instance-test (object-array [(double-array [1 2 3])]))
(compliance/instance-test (object-array [(short-array (map short [1 2 3]))]))
(compliance/instance-test (into-array (Class/forName "[D")
[(double-array [1 2])
(double-array [3 4])])))
uncomplicate/fluokitten
(ns uncomplicate.fluokitten.core-test
(:use [uncomplicate.fluokitten algo jvm core test utils])
(:use [midje.sweet :exclude [just]])
(:require [clojure.string :as s]
[clojure.core.reducers :as r]))
(functor-law2 inc (partial * 100) (float-array [1 -199 9]) (float-array [1 -66 9]))
(facts "Primitive arrays as functors"
(fmap inc (long-array [1 2 3])) => (check-eq (long-array [2 3 4]))
(fmap + (float-array [1 2 3]) (float-array [4 5 6])) => (check-eq (float-array [5 7 9]))
(fmap + (byte-array [1 2 3]) (byte-array [4 5 6]) (byte-array [7 8 9]))
=> (check-eq (byte-array [12 15 18])))
(applicative-law1 + (float-array [1 -2 5]) (float-array [8 98 0]))
(monad-law3-associativity (comp (pure (float-array 0)) inc)
(comp (pure (float-array 0)) (partial * 10))
(float-array [1 -3 -88]))
(magma-op-keeps-type (float-array [1 2]) (float-array [3 4]) (float-array [5 6]))
(facts "How Foldable arrays work."
(fold (double-array [1 2 3 4 5])) => 15.0
(fold (long-array (range 999))) => 498501
(fold (short-array [])) => 0
(fold (int-array [1 2 3 4 5]))= > 15
(fold (float-array [1 2 3 4 5])) => 15.0
(fold + (double-array [1 2 3 4])) => 10.0
(fold * (float-array [1 2 3 4])) => 24.0
(fold + 0 (int-array [1 2 3]) (int-array [4 5 6])) => 21
(fold * 1 (short-array [1 2 3]) (short-array [4 5 6])) => 315
(fold * 2 (long-array [1 2 3]) (long-array [4 5 6]) (long-array [7 8 9])
(long-array [1 2 1]) (long-array [3 2 1])) => (throws UnsupportedOperationException)
(fold * 2 (double-array [1 2 3 4]) (double-array [4 5 6]) (double-array [7 8 9])
(double-array [1 2 1]) (double-array [3 2 1])) => (throws UnsupportedOperationException)
(foldmap inc (double-array [1 2 3 4 5])) => 20.0
(foldmap inc (float-array [])) => 0.0
(foldmap inc (double-array [1 2 3 4 5])) => 20.0
uncomplicate/clojurecl
(ns uncomplicate.clojurecl.core-test
(:require [midje.sweet :refer :all]
[uncomplicate.commons
[core :refer [release with-release info]]
[utils :refer [direct-buffer put-float get-float]]]
[uncomplicate.fluokitten.core :refer [fmap extract]]
[uncomplicate.clojurecl
[core :refer :all]
[info :refer [reference-count mem-base-addr-align opencl-c-version queue-context]]
[toolbox :refer [decent-platform]]]
[uncomplicate.clojurecl.internal
[protocols :refer [size ptr byte-buffer wrap]]
[impl :refer :all]]
[clojure.core.async :refer [go >! <! <!! chan]])
(:import java.nio.ByteBuffer
[org.jocl CL Pointer cl_device_id cl_context_properties cl_mem cl_event EventCallbackFunction]
uncomplicate.clojurecl.internal.impl.EventCallback
clojure.lang.ExceptionInfo))
(facts
"cl-buffer and cl-sub-buffer reading/writing tests."
(let [alignment (mem-base-addr-align
(first (filter #(<= 2.0 (double (:version (opencl-c-version %))))
(devices (decent-platform (platforms))))))]
(with-release [cl-buf (cl-buffer (* 4 alignment Float/BYTES))
cl-subbuf (cl-sub-buffer cl-buf (* alignment Float/BYTES) (* alignment Float/BYTES))]
(cl-buffer? cl-subbuf) => true
(let [data-arr (float-array (range (* 4 alignment)))
buf-arr (float-array (* 4 alignment))
subbuf-arr (float-array alignment)]
(enq-write! cl-buf data-arr)
(enq-read! cl-buf buf-arr)
(enq-read! cl-subbuf subbuf-arr)
(vec buf-arr) => (vec data-arr)
(vec subbuf-arr) => (map float (range alignment (* 2 alignment)))))))
(let [wsize (work-size [cnt])
data (float-array (range cnt))
copy-data (float-array cnt)]
(facts
"enq-kernel!, enq-read!, enq-write!, enq-copy! enq-fill tests"
(enq-write! cl-data data) => *command-queue*
(enq-kernel! dumb-kernel wsize) => *command-queue*
(enq-read! cl-data data) => *command-queue*
(vec data) => [84.0 86.0 88.0 90.0 92.0 94.0 96.0 98.0]
(enq-copy! cl-data cl-copy-data) => *command-queue*
(enq-read! cl-copy-data copy-data) => *command-queue*
(vec copy-data) => (vec data)
(enq-fill! cl-data (float-array [1 2 3 4])) => *command-queue*
(enq-read! cl-data data) => *command-queue*
(vec data) => [1.0 2.0 3.0 4.0 1.0 2.0 3.0 4.0]))))))
(vec (let [res (float-array cnt)] (.get (.asFloatBuffer ^ByteBuffer data) res) res))
=> [168.0 171.0 174.0 177.0 180.0 183.0 186.0 189.0]
uncomplicate/clojurecl
(ns uncomplicate.clojurecl.examples.openclinaction.ch04
(:require [midje.sweet :refer :all]
[clojure.java.io :as io]
[clojure.core.async :refer [chan <!!]]
[uncomplicate.commons
[core :refer [with-release info]]
[utils :refer [direct-buffer]]]
[uncomplicate.clojurecl
[core :refer :all]
[info :refer [endian-little]]
[toolbox :refer [decent-platform]]]))
(facts
"Section 4.2, Page 72."
(let [host-a (float-array [10])
host-b (float-array [2])
host-out (float-array 1)
work-sizes (work-size [1])
program-source
(slurp (io/resource "examples/openclinaction/ch04/double-test.cl"))]
(with-release [cl-a (cl-buffer ctx (* 2 Float/BYTES) :read-only)
cl-b (cl-buffer ctx (* 2 Float/BYTES) :read-only)
cl-out (cl-buffer ctx (* 2 Float/BYTES) :write-only)
prog (build-program! (program-with-source ctx [program-source])
(if (contains? (info dev :extensions)
"cl_khr_fp64")
"-DFP_64"
"")
notifications)
double-test (kernel prog "double_test")]
uncomplicate/clojurecl
(ns uncomplicate.clojurecl.examples.openclinaction.ch10
(:require [midje.sweet :refer :all]
[clojure.java.io :as io]
[clojure.core.async :refer [chan <!!]]
[uncomplicate.commons
[core :refer [with-release info]]
[utils :refer [direct-buffer]]]
[uncomplicate.clojurecl
[core :refer :all]
[info :refer [durations profiling-info]]
[toolbox :refer [decent-platform]]]))
(facts
"Listing on page 225."
(let [program-source
(slurp (io/resource "examples/openclinaction/ch10/reduction.cl"))
num-items (Math/pow 2 20)
bytesize (* num-items Float/BYTES)
workgroup-size 256
notifications (chan)
follow (register notifications)
data (let [d (direct-buffer bytesize)]
(dotimes [n num-items]
(.putFloat ^java.nio.ByteBuffer d (* n Float/BYTES) 1.0))
d)
cl-partial-sums (* workgroup-size 4 Float/BYTES)
partial-output (float-array (/ num-items workgroup-size))
output (float-array 1)]
(with-release [cl-data (cl-buffer ctx bytesize :read-only)
cl-output (cl-buffer ctx Float/BYTES :write-only)
cl-partial-output (cl-buffer ctx (/ bytesize workgroup-size)
:read-write)
prog (build-program! (program-with-source ctx [program-source]))
naive-reduction (kernel prog "naive_reduction")
reduction-scalar (kernel prog "reduction_scalar")
reduction-vector (kernel prog "reduction_vector")
profile-event (event)
profile-event1 (event)
profile-event2 (event)]
(facts
;; ============ Naive reduction ======================================
(set-args! naive-reduction cl-data cl-output) => naive-reduction
(enq-write! cqueue cl-data data) => cqueue
(enq-kernel! cqueue naive-reduction (work-size [1]) nil profile-event)
=> cqueue
(follow profile-event) => notifications
(enq-read! cqueue cl-output output) => cqueue
(finish! cqueue) => cqueue
(println "Naive reduction time:"
(-> (<!! notifications) :data profiling-info durations :end))
(aget output 0) => num-items
;; ============= Scalar reduction ====================================
(set-args! reduction-scalar cl-data cl-partial-sums cl-partial-output)
=> reduction-scalar
(enq-kernel! cqueue reduction-scalar
(work-size [num-items] [workgroup-size])
nil profile-event)
(follow profile-event)
(enq-read! cqueue cl-partial-output partial-output)
(finish! cqueue)
(println "Scalar reduction time:"
(-> (<!! notifications) :data profiling-info durations :end))
(long (first partial-output)) => workgroup-size
;; =============== Vector reduction ==================================
(set-args! reduction-vector cl-data cl-partial-sums cl-partial-output)
=> reduction-vector
(enq-kernel! cqueue reduction-vector
(work-size [(/ num-items 4)] [workgroup-size])
nil profile-event1)
(follow profile-event1)
(set-args! reduction-vector cl-partial-output cl-partial-sums cl-partial-output)
=> reduction-vector
(enq-kernel! cqueue reduction-vector
(work-size [(/ num-items 4 workgroup-size 4)] [workgroup-size])
nil profile-event2)
(follow profile-event2)
(enq-read! cqueue cl-partial-output partial-output)
(finish! cqueue)
(println "Vector reduction time:"
(-> (<!! notifications) :data profiling-info durations :end)
(-> (<!! notifications) :data profiling-info durations :end))
(first partial-output) => num-items)))))