Skip to content

Commit 8778a3f

Browse files
committed
anchor for mdbook
1 parent 2bfe966 commit 8778a3f

File tree

5 files changed

+39
-28
lines changed

5 files changed

+39
-28
lines changed

simulators/1_mass_spring/include/simulator.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <cmath>
55
#include "square_mesh.h"
66
#include <iostream>
7+
8+
// ANCHOR: Header
79
template <typename T, int dim>
810
class MassSpringSimulator
911
{
@@ -21,3 +23,4 @@ class MassSpringSimulator
2123
// The private pointer to the implementation class Impl
2224
std::unique_ptr<Impl> pimpl_;
2325
};
26+
// ANCHOR_END: Header

simulators/1_mass_spring/src/InertialEnergy.cu

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ void InertialEnergy<T, dim>::generate_hess()
5959
device_hess_row_indices(i) = i;
6060
device_hess_col_indices(i) = i;
6161
device_hess_values(i) = m;
62-
// std::cout << device_hess_values(i) << ' ' << device_hess_row_indices(i) << ' ' << device_hess_col_indices(i) << std::endl;
63-
// printf("%f %d %d\n", device_hess_values(i), device_hess_row_indices(i), device_hess_col_indices(i));
6462
})
6563
.wait();
6664
}
@@ -116,7 +114,6 @@ const DeviceBuffer<T> &InertialEnergy<T, dim>::grad()
116114
device_grad(i) = m * (device_x(i) - device_x_tilde(i));
117115
})
118116
.wait();
119-
// display_vec(device_grad);
120117
return device_grad;
121118
} // Calculate the gradient of the energy
122119

simulators/1_mass_spring/src/MassSpringEnergy.cu

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ void MassSpringEnergy<T, dim>::update_k(const std::vector<T> &k)
7070
pimpl_->device_k.copy_from(k);
7171
}
7272

73+
// ANCHOR: val
7374
template <typename T, int dim>
7475
T MassSpringEnergy<T, dim>::val()
7576
{
@@ -81,19 +82,21 @@ T MassSpringEnergy<T, dim>::val()
8182
DeviceBuffer<T> device_val(N);
8283
ParallelFor(256).apply(N, [device_val = device_val.viewer(), device_x = device_x.cviewer(), device_e = device_e.cviewer(), device_l2 = device_l2.cviewer(), device_k = device_k.cviewer()] __device__(int i) mutable
8384
{
84-
int idx1= device_e(2 * i); // First node index
85-
int idx2 = device_e(2 * i + 1); // Second node index
86-
T diff = 0;
87-
for (int d = 0; d < dim;d++){
88-
T diffi = device_x(dim * idx1 + d) - device_x(dim * idx2 + d);
89-
diff += diffi * diffi;
90-
}
91-
device_val(i) = 0.5 * device_l2(i) * device_k(i) * (diff / device_l2(i) - 1) * (diff / device_l2(i) - 1); })
85+
int idx1= device_e(2 * i); // First node index
86+
int idx2 = device_e(2 * i + 1); // Second node index
87+
T diff = 0;
88+
for (int d = 0; d < dim;d++){
89+
T diffi = device_x(dim * idx1 + d) - device_x(dim * idx2 + d);
90+
diff += diffi * diffi;
91+
}
92+
device_val(i) = 0.5 * device_l2(i) * device_k(i) * (diff / device_l2(i) - 1) * (diff / device_l2(i) - 1); })
9293
.wait();
9394

9495
return devicesum(device_val);
9596
} // Calculate the energy
97+
// ANCHOR_END: val
9698

99+
// ANCHOR: grad
97100
template <typename T, int dim>
98101
const DeviceBuffer<T> &MassSpringEnergy<T, dim>::grad()
99102
{
@@ -106,25 +109,26 @@ const DeviceBuffer<T> &MassSpringEnergy<T, dim>::grad()
106109
device_grad.fill(0);
107110
ParallelFor(256).apply(N, [device_x = device_x.cviewer(), device_e = device_e.cviewer(), device_l2 = device_l2.cviewer(), device_k = device_k.cviewer(), device_grad = device_grad.viewer()] __device__(int i) mutable
108111
{
109-
int idx1= device_e(2 * i); // First node index
110-
int idx2 = device_e(2 * i + 1); // Second node index
111-
T diff = 0;
112-
T diffi[dim];
113-
for (int d = 0; d < dim;d++){
114-
diffi[d] = device_x(dim * idx1 + d) - device_x(dim * idx2 + d);
115-
diff += diffi[d] * diffi[d];
116-
}
117-
T factor = 2 * device_k(i) * (diff / device_l2(i) -1);
118-
for(int d=0;d<dim;d++){
119-
atomicAdd(&device_grad(dim * idx1 + d), factor * diffi[d]);
120-
atomicAdd(&device_grad(dim * idx2 + d), -factor * diffi[d]);
121-
122-
} })
112+
int idx1= device_e(2 * i); // First node index
113+
int idx2 = device_e(2 * i + 1); // Second node index
114+
T diff = 0;
115+
T diffi[dim];
116+
for (int d = 0; d < dim;d++){
117+
diffi[d] = device_x(dim * idx1 + d) - device_x(dim * idx2 + d);
118+
diff += diffi[d] * diffi[d];
119+
}
120+
T factor = 2 * device_k(i) * (diff / device_l2(i) -1);
121+
for(int d=0;d<dim;d++){
122+
atomicAdd(&device_grad(dim * idx1 + d), factor * diffi[d]);
123+
atomicAdd(&device_grad(dim * idx2 + d), -factor * diffi[d]);
124+
} })
123125
.wait();
124126
// display_vec(device_grad);
125127
return device_grad;
126128
}
129+
// ANCHOR_END: grad
127130

131+
// ANCHOR: hess
128132
template <typename T, int dim>
129133
const DeviceTripletMatrix<T, 1> &MassSpringEnergy<T, dim>::hess()
130134
{
@@ -170,8 +174,8 @@ const DeviceTripletMatrix<T, 1> &MassSpringEnergy<T, dim>::hess()
170174
} })
171175
.wait();
172176
return device_hess;
173-
174177
} // Calculate the Hessian of the energy
178+
// ANCHOR_END: hess
175179

176180
template class MassSpringEnergy<float, 2>;
177181
template class MassSpringEnergy<float, 3>;

simulators/1_mass_spring/src/simulator.cu

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ void MassSpringSimulator<T, dim>::run()
101101
window.close();
102102
}
103103

104+
// ANCHOR: step_forward
104105
template <typename T, int dim>
105106
void MassSpringSimulator<T, dim>::Impl::step_forward()
106107
{
@@ -111,7 +112,6 @@ void MassSpringSimulator<T, dim>::Impl::step_forward()
111112
T E_last = IP_val();
112113
DeviceBuffer<T> p = search_direction();
113114
T residual = max_vector(p) / h;
114-
// std::cout << "Initial residual " << residual << "\n";
115115
while (residual > tol)
116116
{
117117
std::cout << "Iteration " << iter << " residual " << residual << "E_last" << E_last << "\n";
@@ -132,7 +132,9 @@ void MassSpringSimulator<T, dim>::Impl::step_forward()
132132
}
133133
update_v(add_vector<T>(x, x_n, 1 / h, -1 / h));
134134
}
135-
template <typename T, int dim>
135+
// ANCHOR_END: step_forward
136+
137+
.template <typename T, int dim>
136138
T MassSpringSimulator<T, dim>::Impl::screen_projection_x(T point)
137139
{
138140
return offset + scale * point;

simulators/1_mass_spring/src/uti.cu

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using namespace muda;
44

5+
// ANCHOR: add_vector
56
template <typename T>
67
DeviceBuffer<T> add_vector(const DeviceBuffer<T> &a, const DeviceBuffer<T> &b, const T &factor1, const T &factor2)
78
{
@@ -17,6 +18,8 @@ DeviceBuffer<T> add_vector(const DeviceBuffer<T> &a, const DeviceBuffer<T> &b, c
1718
.wait();
1819
return c_device;
1920
}
21+
// ANCHOR: add_vector
22+
2023
template DeviceBuffer<float> add_vector<float>(const DeviceBuffer<float> &a, const DeviceBuffer<float> &b, const float &factor1, const float &factor2);
2124
template DeviceBuffer<double> add_vector<double>(const DeviceBuffer<double> &a, const DeviceBuffer<double> &b, const double &factor1, const double &factor2);
2225

@@ -97,6 +100,7 @@ T max_vector(const DeviceBuffer<T> &a)
97100
template float max_vector<float>(const DeviceBuffer<float> &a);
98101
template double max_vector<double>(const DeviceBuffer<double> &a);
99102

103+
// ANCHOR: search_dir
100104
template <typename T>
101105
void search_dir(const DeviceBuffer<T> &grad, const DeviceTripletMatrix<T, 1> &hess, DeviceBuffer<T> &dir)
102106
{
@@ -116,6 +120,7 @@ void search_dir(const DeviceBuffer<T> &grad, const DeviceTripletMatrix<T, 1> &he
116120
ctx.sync();
117121
dir.view().copy_from(dir_device.buffer_view());
118122
}
123+
// ANCHOR_END: search_dir
119124
template void search_dir<float>(const DeviceBuffer<float> &grad, const DeviceTripletMatrix<float, 1> &hess, DeviceBuffer<float> &dir);
120125
template void search_dir<double>(const DeviceBuffer<double> &grad, const DeviceTripletMatrix<double, 1> &hess, DeviceBuffer<double> &dir);
121126

0 commit comments

Comments
 (0)