Skip to content

Commit be51414

Browse files
committed
Fix errors found in the benchmarking and fix several issues with documentation.
1 parent c0f6a84 commit be51414

11 files changed

Lines changed: 108 additions & 54 deletions

File tree

‎graph_docs/Comparison.png‎

16.3 KB
Loading

‎graph_docs/code_performance.dox‎

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,16 @@
4545
* @f{equation}{\frac{\partial\vec{v}}{\partial t} = dt\vec{v}\times\vec{B}@f}
4646
* @f{equation}{\frac{\partial\vec{x}}{\partial t} = dt\vec{v}@f}
4747
*
48-
* We compared the graph framework against the MLX framework since it supports
49-
* Apple GPUs and JAX due to it's popularity. Source codes for this benchmark
50-
* case is available in the appendix. Figure \ref{fig:compare} shows the through put of
51-
* pushing $10^{8}$ particles for $10^{3}$ time steps. The graph framework
52-
* consistently shows the best throughput on both CPUs and GPUs. Note MLX CPU
53-
* throughput could by improved by splitting the problem to multiple threads.
48+
* We compared the graph framework against the
49+
* <a href="https://ml-explore.github.io/mlx/build/html/index.html">MLX</a>
50+
* framework since it supports Apple GPUs,
51+
* <a href="https://docs.jax.dev/en/latest/">JAX</a> due to it's popularity,
52+
* and <a href="https://kokkos.org">Kokkos</a> for its performance
53+
* portability. Source codes for this benchmark case is available in the
54+
* appendix. Figure \ref{fig:compare} shows the through put of pushing $10^{8}$
55+
* particles for $10^{3}$ time steps. The graph framework consistently shows the
56+
* best throughput on both CPUs and GPUs. Note MLX CPU throughput could by
57+
* improved by splitting the problem to multiple threads.
5458
*
5559
* @subsection code_performance_comparison_codes Source codes for throughput benchmark comparison
5660
* @subsubsection code_performance_comparison_graph Graph Framework
@@ -93,7 +97,7 @@ for (size_t i = 0, ie = threads.size(); i < ie; i++) {
9397
auto v_next = v + dt*lorentz;
9498
auto pos_next = pos + dt*v_next;
9599

96-
workflow::manager<float> work(0);
100+
workflow::manager<float> work(thread_number);
97101
work.add_item({
98102
graph::variable_cast(x),
99103
graph::variable_cast(y),
@@ -177,7 +181,7 @@ const auto total_time = end - start;
177181
def push(x, y, z, vx, vy, vz):
178182
dt = 0.000001
179183
vx_next = vx + dt*(vy*1 - vz*0)
180-
vy_next = vy + dt*(vz*0 - vy*1)
184+
vy_next = vy + dt*(vz*0 - vx*1)
181185
vz_next = vz + dt*(vx*0 - vy*0)
182186
return vx_next, vy_next, vz_next,
183187
x + dt*vx_next, y + dt*vy_next, z + dt*vz_next
@@ -201,6 +205,48 @@ jax.block_until_ready([x, y, z, vx, vy, vz])
201205
end = time.time()
202206

203207
print(end - start)
208+
@endcode
209+
*
210+
* @subsubsection code_performance_comparison_kokkos Kokkos
211+
* @code
212+
const size_t size = 100000000;
213+
const size_t steps = 1000;
214+
215+
using ViewVectorType = Kokkos::View<float *, Kokkos::SharedSpace>;
216+
ViewVectorType x("x", size);
217+
ViewVectorType y("y", size);
218+
ViewVectorType z("z", size);
219+
220+
ViewVectorType vx("vx", size);
221+
ViewVectorType vy("vy", size);
222+
ViewVectorType vz("vz", size);
223+
224+
Kokkos::parallel_for(size, KOKKOS_LAMBDA(const int64_t index) {
225+
vx[index] = 1;
226+
vz[index] = 1;
227+
});
228+
229+
const std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
230+
231+
for (size_t i = 0; i < steps; i++) {
232+
Kokkos::parallel_for(size, KOKKOS_LAMBDA(const int64_t index) {
233+
const float dt = 0.000001;
234+
const float vx_next = vx[index] + dt*(vy[index]*1 - vz[index]*0);
235+
const float vy_next = vy[index] + dt*(vz[index]*0 - vx[index]*1);
236+
const float vz_next = vz[index] + dt*(vx[index]*0 - vy[index]*0);
237+
x[index] += dt*vx_next;
238+
y[index] += dt*vy_next;
239+
z[index] += dt*vz_next;
240+
vx[index] = vx_next;
241+
vy[index] = vy_next;
242+
vz[index] = vz_next;
243+
});
244+
}
245+
246+
Kokkos::fence();
247+
248+
std::chrono::high_resolution_clock::time_point end = std::chrono::high_resolution_clock::now();
249+
const auto total_time = end - start;
204250
@endcode
205251
*/
206252

‎graph_docs/discription.dox‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* @section discription_introduction Introduction
66
* The basic functionality of this framework is to build expression graphs
77
* representing mathematical equations. Reduce those graphs to simpler forms.
8-
* Transform those graph to take derivatives. Just-In-Time (JIT) compile them to
9-
* available compute device kernels. Then run those kernels in workflow. The
8+
* Transform those graphs to take derivatives. Just-In-Time (JIT) compile them
9+
* to available compute device kernels. Then run those kernels in workflows. The
1010
* code is written in using C++23 features. To simplify embedding into legacy
1111
* codes, there are additional language bindings for C and Fortran.
1212
*
@@ -48,9 +48,10 @@
4848
* be reduced to a single constant by calling the evaluate method. Sub-graph
4949
* expressions are combined, factored out, or moved to enable better reductions
5050
* on subsequent passes. As new ways of reducing the graph are implemented,
51-
* current and existing code built using this framework benefit from improved
52-
* speed. The figure above shows a visualization of the tree data structure for
53-
* the equation of a line, the derivative, and the subsequent reductions.
51+
* current and existing code built using this framework will benefit from
52+
* improved speed. The figure above shows a visualization of the tree data
53+
* structure for the equation of a line, the derivative, and the subsequent
54+
* reductions.
5455
*
5556
* @subsubsection discription_graphs_builds Building Graphs
5657
* As an example building an expression of line @f$y=mx+b@f$ accomplished by
@@ -79,8 +80,8 @@ auto dydmx = y->df(0.5*x);
7980
* running them in order. One @ref workflow::manager is created for each device
8081
* or thread. The user is responsible for creating threads. Each kernel is
8182
* generated through a @ref workflow::work_item. A work item is defined by
82-
* kernel @ref graph::input_nodes, @ref graph::output_nodes and
83-
* @ref graph::map_nodes. Map items are used to take the results of kernel and
83+
* kernel @ref graph::input_nodes, @ref graph::output_nodes, and
84+
* @ref graph::map_nodes. Map items are used to take the results of a kernel and
8485
* update an input buffer. Using our example of line equation, we can create a
8586
* workflow to compute @f$y@f$ and @f$\frac{\partial y}{\partial x}@f$.
8687
* @code
@@ -99,7 +100,7 @@ work.add_item({
99100
* elements in the inputs. Multiple work items can be created and will be
100101
* executed in order of creation.
101102
*
102-
* Once the work items are defined that can be JIT compiled to a backend device.
103+
* Once the work items are defined they can be JIT compiled to a backend device.
103104
* The graph framework supports back ends for generic CPUs, Apple Metal GPUs,
104105
* Nvidia Cuda GPUs, and initial HIP support of AMD GPUs. Each back end supplies
105106
* relevant driver code to build the kernel source, compile the kernel, build

‎graph_docs/general.dox‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
* as either variables @f$x@f$ or constants @f$m,b@f$. These nodes are connected
4040
* by nodes for multiply and addition operations. The output @f$y@f$ represents
4141
* the entire graph of operations.
42-
* @image{} html line_graph.png "The graph structure for @f$y=mx+b@f$."
42+
* @image{} html line_graph.png "The graph structure for y = mx + b."
4343
* Evaluation of graphs start from the top most node in this case the @f$+@f$
4444
* operation. Evaluation of a node is not performed until all sub-nodes are
4545
* evaluated starting with the left operand. Evaluation starts by recursively
@@ -58,9 +58,10 @@
5858
* graphs of a function derivative. For an example of taking derivatives see the
5959
* @ref tutorial_derivatives "auto differentiation tutorial". Lets say that we
6060
* want to take the derivative of @f$\frac{\partial y}{\partial x}@f$. This is
61-
* achieved by evaluating the until bottom left most node is reached. Then a new
62-
* graph is build starting with @f$\frac{\partial m}{\partial x}=0@f$. Applying
63-
* the first half of the chain rule we build a new graph for @f$0x@f$
61+
* achieved by evaluating the graph until the bottom left most node is reached.
62+
* Then a new graph is constucted starting with
63+
* @f$\frac{\partial m}{\partial x}=0@f$. Applying the first half of the chain
64+
* rule we build a new graph for @f$0x@f$
6465
* @image{} html line_graph_dydf1.png ""
6566
* Then we take the derivative of the right operand and apply the second half
6667
* of the chain rule to build a new graph for @f$0x=0@f$.
@@ -73,8 +74,8 @@
7374
* The final expression for @f$\frac{\partial y}{\partial x}@f$ contains many
7475
* unnecessary nodes in the graph. Instead of building full graphs, we can
7576
* simplify and eliminate nodes as we build them. For instance, when the
76-
* expression @f$0x@f$ this created can be immediately reduce it to a single
77-
* node.
77+
* expression @f$0\times x@f$ is created, this can be immediately reduced to a
78+
* single node @f$0@f$.
7879
* @image{} html line_graph_reduce1.png ""
7980
* Applying all possible reductions reduces the final expression to
8081
* @f$\frac{\partial y}{\partial x}=m@f$.
@@ -109,7 +110,7 @@
109110
* @subsection general_concepts_compile_maps Maps
110111
* Maps enable the results of an output node to be stored in an input node. This
111112
* is used for a wide varity of cases. For instance take a gradient decent step.
112-
* @f{equation}{y = y + \frac{\partial f}{\partial x}@f}
113+
* @f{equation}{y_{i+1} = y_{i} + \frac{\partial f}{\partial x}@f}
113114
* In this case the output of the expression
114115
* @f$y + \frac{\partial f}{\partial x}@f$
115116
* can be mapped to update @f$y@f$.
@@ -122,7 +123,7 @@
122123
* <hr>
123124
* @section general_concepts_safe_math Safe Math
124125
* There are some conditions where mathematically, a graph should evaluate to a
125-
* normal number. However, when evaluated suing floating point precision, can
126+
* normal number. However, when evaluated using floating point precision, can
126127
* lead to <tt>Inf</tt> or <tt>NaN</tt>. An example of this the
127128
* @f$\exp\left(x\right)@f$ function. For large argument values,
128129
* @f$\exp\left(x\right)@f$ overflows the maximum floating point precision and

‎graph_docs/tutorial.dox‎

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* executable target which can be used to test out the API's of this framework.
1414
* The playground starts with a blank main function.
1515
* @code
16-
#include "../graph_framework/jit.hpp"
16+
#include "graph_framework.hpp"
1717

1818
int main(int argc, const char * argv[]) {
1919
START_GPU
@@ -30,7 +30,7 @@ int main(int argc, const char * argv[]) {
3030
* main. This will allow us to play with different floating point types. For now
3131
* we will start with a simple float type.
3232
* @code
33-
#include "../graph_framework/jit.hpp"
33+
#include "graph_framework.hpp"
3434

3535
template<jit::float_scalar T>
3636
void run_tutorial() {
@@ -84,16 +84,16 @@ void run_tutorial() {
8484
* so all method are called using the <tt>-></tt> operator.
8585
*
8686
* @subsection tutorial_constant Constant Nodes
87-
* Next we want to define a constant. There are two method to define constants
87+
* Next we want to define a constant. There are two methods to define constants
8888
* explicitly or implicitly.
8989
* @code
9090
template<jit::float_scalar T>
9191
void run_tutorial() {
9292
auto x = graph::variable(1000, "x");
9393

94-
// Define explicit constant.
94+
// Define explicit constant.
9595
auto m = graph::constant<T> (0.4);
96-
// Define implicit constant.
96+
// Define implicit constant.
9797
const T b = 0.6;
9898
}
9999
@endcode
@@ -110,9 +110,9 @@ template<jit::float_scalar T>
110110
void run_tutorial() {
111111
auto x = graph::variable(1000, "x");
112112

113-
// Define explicit constant.
113+
// Define explicit constant.
114114
auto m = graph::constant<T> (0.4);
115-
// Define implicit constant.
115+
// Define implicit constant.
116116
const T b = 0.6;
117117

118118
// Equation of a line
@@ -133,15 +133,15 @@ template<jit::float_scalar T>
133133
void run_tutorial() {
134134
auto x = graph::variable(1000, "x");
135135

136-
// Define explicit constant.
136+
// Define explicit constant.
137137
auto m = graph::constant<T> (0.4);
138-
// Define implicit constant.
138+
// Define implicit constant.
139139
const T b = 0.6;
140140

141-
// Equation of a line
141+
// Equation of a line
142142
auto y = m*x + b;
143143

144-
// Auto differentiation.
144+
// Auto differentiation.
145145
auto dydx = y->df(x);
146146
dydx->to_latex();
147147
std::cout << std::endl;
@@ -168,18 +168,18 @@ template<jit::float_scalar T>
168168
void run_tutorial() {
169169
auto x = graph::variable(3, "x");
170170

171-
// Define explicit constant.
171+
// Define explicit constant.
172172
auto m = graph::constant<T> (0.4);
173-
// Define implicit constant.
173+
// Define implicit constant.
174174
const T b = 0.6;
175175

176-
// Equation of a line
176+
// Equation of a line
177177
auto y = m*x + b;
178178

179-
// Auto differentiation.
179+
// Auto differentiation.
180180
auto dydx = y->df(x);
181181

182-
// Create a workflow manager.
182+
// Create a workflow manager.
183183
workflow::manager<T> work(0);
184184
}
185185
@endcode
@@ -322,13 +322,13 @@ void run_tutorial() {
322322
auto x = graph::variable<T> (3, "x");
323323
x->set({1.0, 2.0, 3.0});
324324

325-
// Define an objective function.
325+
// Define an objective function.
326326
auto f = 0.2*x*x*x + 0.6*x*x + 0.4*x + 0.5;
327327

328328
// Define a step update.
329329
auto x_new = x - f/f->df(x);
330330

331-
// Create a workflow manager.
331+
// Create a workflow manager.
332332
workflow::manager<T> work(0);
333333
work.add_item({
334334
graph::variable_cast(x)
@@ -372,7 +372,7 @@ void run_tutorial() {
372372
* a reduction on the host side and transferring the entire array to the host.
373373
* To improve this we can use a converge item instead.
374374
* @code
375-
// Create a workflow manager.
375+
// Create a workflow manager.
376376
workflow::manager<T> work(0);
377377
work.add_converge_item({
378378
graph::variable_cast(x)

‎graph_docs/use_cases.dox‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
* @subsection use_cases_rf RF Ray tracing
1212
* Geometric optics is a set of asymptotic approximation methods to solve wave
1313
* equations. The physics of the particular wave determines an algebraic
14-
* relation between $\omega$ and $\vec{k}$ called a dispersion relation,
15-
* @f$D\left(\omega,\vec{k}\right)=0@f$. Since the parameter $t$ does not appear
16-
* explicitly in the dispersion relation, the function
14+
* relation between @f$\omega@f$ and @f$\vec{k}@f$ called a dispersion relation,
15+
* @f$D\left(\omega,\vec{k}\right)=0@f$. Since the parameter @f$t@f$ does not
16+
* appear explicitly in the dispersion relation, the function
1717
* @f$\omega\left(\vec{k}\left(t\right),\vec{x}\left(t\right)\right)@f$ is
1818
* constant along the ray trajectory
1919
* @f{equation}{\frac{\partial\omega}{\partial t}=\frac{\partial\omega}{\partial\vec{x}}\cdot\frac{\partial\vec{x}}{\partial t}+\frac{\partial\omega}{\partial\vec{k}}\cdot\frac{\partial\vec{k}}{\partial t}\equiv 0@f}
@@ -41,7 +41,7 @@
4141
* by relatively simple dispersion relations in plane stratified plasmas, that
4242
* is plasma with spatial variation only in the @f$x@f$ direction. In a
4343
* spatially varying medium, at a given frequency, there may be regions in which
44-
* the solution of the dispersion relation, $\vec{k}$, is real, and the wave
44+
* the solution of the dispersion relation, @f$\vec{k}@f$, is real, and the wave
4545
* propagates. In other regions @f$\vec{k}@f$ is imaginary and the wave does not
4646
* propagate, referred to as evanescent. The boundary between a region of
4747
* propagation and evanescence is a surface called a cut-off. It is also
@@ -53,11 +53,11 @@
5353
* behavior, and the behavior of rays in their vicinity is an indication of the
5454
* correctness of the solution.
5555
*
56-
* For plasma, the spatial dependence of the dispersion relation comes through
56+
* For plasmas, the spatial dependence of the dispersion relation comes through
5757
* variation of the plasma equilibrium quantities. These include the vector
5858
* magnetic field, @f$\vec{B}\left(x\right)@f$, the density of each plasma
5959
* particle species, @f$n_{s}\left(x\right)@f$, and the temperature of each
60-
* particle species, @f$T_{s}\left(x\right)@f$, where $s$ indicates a
60+
* particle species, @f$T_{s}\left(x\right)@f$, where @f$s@f$ indicates a
6161
* particular species. For the cases presented here a linear gradient along the
6262
* @f$x@f$ direction is taken for either the particle density or magnetic field
6363
* strength.

‎graph_framework/cpu_context.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ namespace gpu {
116116
//------------------------------------------------------------------------------
117117
/// @brief Construct a cpu context.
118118
///
119-
/// @param[in] index Concurrent index. Not used.
119+
/// @param[in] index Device index. Not used.
120120
//------------------------------------------------------------------------------
121121
cpu_context(const size_t index) {
122122
llvm::InitializeNativeTarget();

‎graph_framework/cuda_context.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ namespace gpu {
134134
//------------------------------------------------------------------------------
135135
/// @brief Cuda context constructor.
136136
///
137-
/// @param[in] index Concurrent index.
137+
/// @param[in] index Device index.
138138
//------------------------------------------------------------------------------
139139
cuda_context(const size_t index) : result_buffer(0), module(0), offset_buffer(0) {
140140
check_error(cuDeviceGet(&device, index), "cuDeviceGet");

‎graph_framework/metal_context.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ namespace gpu {
6565
//------------------------------------------------------------------------------
6666
/// @brief Construct a metal context.
6767
///
68-
/// @param[in] index Concurrent index.
68+
/// @param[in] index Device index.
6969
//------------------------------------------------------------------------------
7070
metal_context(const size_t index) :
7171
device([MTLCopyAllDevices() objectAtIndex:index]),

‎graph_framework/node.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@
311311
/// @code
312312
/// virtual shared_leaf<T, SAFE_MATH> remove_pseudo() {
313313
/// if (this->has_pseudo()) {
314-
/// return sqrt(this->arg->remove_pseudo());
314+
/// return foo(this->arg->remove_pseudo());
315315
/// }
316316
/// return this->shared_from_this();
317317
/// }

0 commit comments

Comments
 (0)