-
Notifications
You must be signed in to change notification settings - Fork 1
/
context.clj
59 lines (43 loc) · 1.23 KB
/
context.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
(ns tech.compute.context
"Compute context allows someone to setup the default driver, device, and stream to
use in function calls."
(:require [tech.compute.registry :as registry]
[tech.compute.driver :as drv]))
(def ^:dynamic *context {})
(defn default-driver
[]
(or (:driver *context)
(registry/driver @registry/*cpu-driver-name*)))
(defn default-device
[]
(let [retval
(or (:device *context)
(first (drv/get-devices (default-driver))))]
(when-not retval
(throw (Exception.
(format "%s: No devices found"
(drv/driver-name (default-driver))))))
retval))
(defn default-stream
[]
(or (:stream *context)
(drv/default-stream (default-device))))
(defmacro with-context
[context & body]
`(with-bindings {#'*context ~context}
~@body))
(defmacro with-merged-context
[context & body]
`(with-context
(merge *context ~context)
~@body))
(defn default-context
[]
{:driver (default-driver)
:device (default-device)
:stream (default-stream)})
(defn options->context
"Given an options map, return a augmented map that always includes
device, driver, and stream."
[opt-map]
(merge (default-context) opt-map))