1 /**
2     This package contains the framework for constructing and executing operation graphs.
3 
4     $(UL
5         $(LI $(D dopt.core.ops) provides functions for constructing nodes in the operation graph.)
6         $(LI $(D dopt.core.grads) provides functions for computing the derivatives of operations.)
7         $(LI $(D dopt.core.cpu) contains a backend that executes operation graphs using the CPU.)
8         $(LI $(D dopt.core.cuda) contains a backend that executes operation graphs using a CUDA enabled GPU.)
9     )
10 
11     Authors: Henry Gouk
12 */
13 module dopt.core;
14 
15 public
16 {
17     import dopt.core.cpu;
18     import dopt.core.cuda;
19     import dopt.core.grads;
20     import dopt.core.ops;
21     import dopt.core.types;
22 }
23 
24 alias Evaluator = Buffer[] delegate(Operation[] ops, Buffer[Operation] args);
25 
26 __gshared Evaluator defaultEvaluator;
27 
28 shared static this()
29 {
30     import std.functional : toDelegate;
31 
32     dopt.core.ops.initialize();
33     dopt.core.grads.initialize();
34     dopt.core.cpu.initialize();
35 
36     try
37     {
38         dopt.core.cuda.initialize();
39         defaultEvaluator = toDelegate(&evaluateCUDA);
40     }
41     catch(Exception e)
42     {
43         defaultEvaluator = toDelegate(&evaluateCPU);
44     }
45 }
46 
47 /**
48     Evaluates a several nodes from the operation graph.
49 
50     Params:
51         ops = The nodes of the operation graph that values should be computed for.
52         args = A set of variable assignments.
53 
54     Returns:
55         An array of $(D Buffer) objects, each containing the value of the corresponding element in $(D ops).
56 */
57 Buffer[] evaluate(Operation[] ops, Buffer[Operation] args = null)
58 {
59     return defaultEvaluator(ops, args);
60 }
61 
62 /**
63     Evaluates an operation graph with a single root node.
64 
65     This overload is here for convenience. Internally, the multi-output version of evaluate is called.
66 
67     Params:
68         op = The root node of the operation graph.
69         args = A set of variable assignments.
70 
71     Returns:
72         A $(D Buffer) containing the result of the computation.
73 */
74 Buffer evaluate(Operation op, Buffer[Operation] args = null)
75 {
76     return evaluate([op], args)[0];
77 }