Skip to content

Latest commit

 

History

History
148 lines (134 loc) · 5.46 KB

core.org

File metadata and controls

148 lines (134 loc) · 5.46 KB

Contents

thi.ng.ndarray.test.core

Main tests

(deftest test-3d
  (let [shape [3 3 3]
        raw   (range (apply * shape))
        a     (nd/ndarray :int8 raw shape)]
    (is (satisfies? nd/PNDArray a))
    #?(:cljs (is (a/typed-array? (nd/data a))))
    (is (= shape (nd/shape a)))
    (is (= [9 3 1] (nd/stride a)))
    (is (= (apply * shape) (nd/size a)))
    (is (= 3 (nd/dimension a)))
    (is (= raw (seq a)))
    (is (= [2 2 2]
           (-> a (nd/truncate-h 2 2 2) nd/shape)))
    (is (= [0 1 3 4 9 10 12 13]
           (-> a (nd/truncate-h 2 2 2) seq)))
    (is (= (-> a (nd/truncate-h 2 2 2) seq)
           (-> a (nd/truncate-h -1 -1 -1) seq)))
    (is (= [2 2 2]
           (-> a (nd/truncate-l 1 1 1) nd/shape)))
    (is (= [13 14 16 17 22 23 25 26]
           (-> a (nd/truncate-l 1 1 1) seq)))
    (is (= [13]
           (-> a (nd/truncate-h 2 2 2) (nd/truncate-l 1 1 1) seq)))
    (is (= [0 1 2 3 4 5 6 7 8 18 19 20 21 22 23 24 25 26]
           (-> a (nd/step 2 nil nil) seq)))
    (is (= [0 1 2 6 7 8 9 10 11 15 16 17 18 19 20 24 25 26]
           (-> a (nd/step nil 2 nil) seq)))
    (is (= [0 2 3 5 6 8 9 11 12 14 15 17 18 20 21 23 24 26]
           (-> a (nd/step nil nil 2) seq)))
    (is (= [9 11 12 14 15 17 18 20 21 23 24 26]
           (-> a (nd/truncate-l 1 0 0) (nd/step nil nil 2) seq)))
    (is (= [3 5 6 8 12 14 15 17 21 23 24 26]
           (-> a (nd/truncate-l 0 1 0) (nd/step nil nil 2) seq)))
    (is (= [1 4 7 10 13 16 19 22 25]
           (-> a (nd/truncate-l 0 0 1) (nd/step nil nil 2) seq)))
    (is (= [18 19 20 21 22 23 24 25 26 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7 8]
           (-> a (nd/step -1 nil nil) seq)))
    (is (= [6 7 8 3 4 5 0 1 2 15 16 17 12 13 14 9 10 11 24 25 26 21 22 23 18 19 20]
           (-> a (nd/step nil -1 nil) seq)))
    (is (= [2 1 0 5 4 3 8 7 6 11 10 9 14 13 12 17 16 15 20 19 18 23 22 21 26 25 24]
           (-> a (nd/step nil nil -1) seq)))
    (is (= [0 9 18 3 12 21 6 15 24 1 10 19 4 13 22 7 16 25 2 11 20 5 14 23 8 17 26]
           (-> a (nd/transpose 2 1 0) seq)))
    (is (= [0 3 6 9 12 15 18 21 24 1 4 7 10 13 16 19 22 25 2 5 8 11 14 17 20 23 26]
           (-> a (nd/transpose 2 0 1) seq)))
    (is (= (reduce + (seq a)) (reduce + a)))
    (is (= (reduce + 0 (seq a)) (reduce + 0 a)))
    (is (= (seq (nd/truncate-l a 1 1 1)) (reduce conj [] (nd/truncate-l a 1 1 1))))
    (is (= (seq (nd/transpose a 2 1 0)) (reduce conj [] (nd/transpose a 2 1 0))))
    (is (= (seq (nd/step a 2 nil -1)) (reduce conj [] (nd/step a 2 nil -1))))
    ))
;; :noweb-ref test
(deftest bench1
  (time
   (dotimes [i 16]
     (let [shape [64 64 64]
           [nx ny nz] (map dec shape)
           a (nd/ndarray :float64 (double-array (apply * shape)) shape)
           b (nd/ndarray :float64 (double-array (apply * shape)) shape)
           fa (fn [i v] (+ (+ v (nd/get-at-index b i)) 0.1))
           fb (fn [i v] (- v (* (nd/get-at-index a i) 0.5)))]
       (time
        (loop [idx (nd/index-seq a)]
          (if idx
            (let [i (first idx)]
              (nd/update-at-index a i fa)
              (nd/update-at-index b i fb)
              (recur (next idx))))))))))

Complete namespace definition

(ns thi.ng.ndarray.test.core
  (:require
   [thi.ng.ndarray.core :as nd]
   #?@(:clj
       [[clojure.test :refer :all]]
       :cljs
       [[thi.ng.typedarrays.core :as a]
        [cemerick.cljs.test :as t :refer-macros [is deftest]]])))

#?(:cljs (enable-console-print!))

<<test>>

#?(:cljs
   (if (a/typed-arrays-supported?)
     (t/run-all-tests)
     (prn "Can't test - typed arrays not supported!")))

Scratch (ignore)

(use 'criterium.core)
(set! *unchecked-math* true)

;; 937.259675 µs [16 16 16]
;; 51.299623 ms  [64 64 64]
;; 1.418434 ms   [2 2 2048]
(with-progress-reporting
  (quick-bench
   (let [shape [2 2 2048]
         [nx ny nz] (map dec shape)
         ^doubles ad (double-array (apply * shape))
         ^doubles bd (double-array (apply * shape))
         a (nd/ndarray :float64 ad shape)
         b (nd/ndarray :float64 bd shape)]
     (loop [idx (nd/index-seq a)]
       (if idx
         (let [i (first idx)]
           (aset ad i (+ (+ (aget ad i) (aget bd i)) 0.1))
           (aset bd i (- (aget bd i) (* (aget ad i) 0.5)))
           (recur (next idx))))))
   :verbose))

(with-progress-reporting
  (quick-bench
   (let [shape [16 16 16]
         a (nd/ndarray :float64 (double-array (apply * shape)) shape)
         b (nd/ndarray :float64 (double-array (apply * shape)) shape)
         fa (fn [i v] (+ (+ v (nd/get-at-index b i)) 0.1))
         fb (fn [i v] (- v (* (nd/get-at-index a i) 0.5)))]
     (loop [idx (nd/index-seq a)]
       (if idx
         (let [i (first idx)]
           (nd/update-at-index a i fa)
           (nd/update-at-index b i fb)
           (recur (next idx))))))
   :verbose))