Skip to content

Commit d84f358

Browse files
committed
fix more bugs
1 parent 547c65e commit d84f358

11 files changed

Lines changed: 215 additions & 158 deletions

File tree

simulators/1_mass_spring/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
add_executable(1_mass_spring)
2+
3+
target_compile_options(1_mass_spring PRIVATE -g)
24
set_target_properties(1_mass_spring PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
35

46
target_link_libraries(1_mass_spring PRIVATE muda cusolver cublas cusparse )

simulators/1_mass_spring/include/InertialEnergy.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class InertialEnergy
2020
void update_x(const std::vector<T> &x);
2121
void update_x_tilde(const std::vector<T> &x_tilde);
2222
void update_m(T m);
23-
T val(); // Calculate the value of the energy
24-
std::vector<T> &grad(); // Calculate the gradient of the energy
25-
SparseMatrix<T> &hess(); // Calculate the Hessian matrix of the energy
23+
T val(); // Calculate the value of the energy
24+
std::vector<T> &grad(); // Calculate the gradient of the energy
25+
SparseMatrix<T> hess(); // Calculate the Hessian matrix of the energy
2626

2727
private:
2828
// The implementation details of the VecAdder class are placed in the implementation class declared here.

simulators/1_mass_spring/include/MassSpringEnergy.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@ class MassSpringEnergy
1414
MassSpringEnergy(MassSpringEnergy &&rhs);
1515
MassSpringEnergy(const MassSpringEnergy &rhs);
1616
MassSpringEnergy &operator=(MassSpringEnergy &&rhs);
17-
MassSpringEnergy &operator=(const MassSpringEnergy &rhs);
1817

1918
void update_x(const std::vector<T> &x);
2019
void update_e(const std::vector<int> &e);
2120
void update_l2(const std::vector<T> &l2);
2221
void update_k(const std::vector<T> &k);
23-
T val(); // Calculate the value of the energy
24-
std::vector<T> &grad(); // Calculate the gradient of the energy
25-
SparseMatrix<T> &hess(); // Calculate the Hessian matrix of the energy
22+
T val(); // Calculate the value of the energy
23+
std::vector<T> &grad(); // Calculate the gradient of the energy
24+
SparseMatrix<T> hess(); // Calculate the Hessian matrix of the energy
2625

2726
private:
2827
// The implementation details of the VecAdder class are placed in the implementation class declared here.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#pragma once
2+
3+
#include <vector>
4+
5+
template <typename T>
6+
class SparseMatrix
7+
{
8+
public:
9+
SparseMatrix(int size);
10+
SparseMatrix();
11+
~SparseMatrix();
12+
SparseMatrix(SparseMatrix<T> &&rhs);
13+
SparseMatrix<T> &operator=(SparseMatrix<T> &&rhs);
14+
SparseMatrix<T> &operator=(SparseMatrix<T> &rhs);
15+
SparseMatrix<T> &operator*(const T &a);
16+
SparseMatrix(const SparseMatrix<T> &rhs);
17+
void set_value(int row, int col, T val, int loc);
18+
void set_diagonal(T val);
19+
20+
const std::vector<int> &get_row_buffer() const;
21+
const std::vector<int> &get_col_buffer() const;
22+
const std::vector<T> &get_val_buffer() const;
23+
std::vector<int> &set_row_buffer();
24+
std::vector<int> &set_col_buffer();
25+
std::vector<T> &set_val_buffer();
26+
SparseMatrix<T> &combine(const SparseMatrix<T> &other);
27+
28+
int get_size() const;
29+
30+
private:
31+
int size;
32+
std::vector<int> row_idx;
33+
std::vector<int> col_idx;
34+
std::vector<T> val;
35+
};

simulators/1_mass_spring/include/uti.h

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,6 @@
11
#pragma once
22
#include <Eigen/Dense>
3-
4-
template <typename T>
5-
class SparseMatrix
6-
{
7-
public:
8-
SparseMatrix(int size);
9-
SparseMatrix();
10-
~SparseMatrix();
11-
12-
void set_value(int row, int col, T val, int loc);
13-
void set_diagonal(T val);
14-
15-
const std::vector<int> &get_row_buffer() const;
16-
const std::vector<int> &get_col_buffer() const;
17-
const std::vector<T> &get_val_buffer() const;
18-
std::vector<int> &set_row_buffer();
19-
std::vector<int> &set_col_buffer();
20-
std::vector<T> &set_val_buffer();
21-
SparseMatrix<T> &combine(const SparseMatrix<T> &other);
22-
23-
int get_size() const;
24-
25-
SparseMatrix<T> operator*(const T &a);
26-
27-
private:
28-
int size;
29-
std::vector<int> row_idx;
30-
std::vector<int> col_idx;
31-
std::vector<T> val;
32-
};
33-
3+
#include "SparseMatrix.h"
344
template <typename T>
355
std::vector<T> add_vector(const std::vector<T> &a, const std::vector<T> &b, const T &factor1 = 1, const T &factor2 = 1);
366

simulators/1_mass_spring/src/InertialEnergy.cu

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,6 @@ template <typename T, int dim>
2929
InertialEnergy<T, dim>::InertialEnergy(const InertialEnergy<T, dim> &rhs)
3030
: pimpl_{std::make_unique<Impl>(*rhs.pimpl_)} {}
3131

32-
template <typename T, int dim>
33-
InertialEnergy<T, dim> &InertialEnergy<T, dim>::operator=(const InertialEnergy<T, dim> &rhs)
34-
{
35-
*pimpl_ = *rhs.pimpl_;
36-
return *this;
37-
};
38-
3932
template <typename T, int dim>
4033
InertialEnergy<T, dim>::InertialEnergy(int N, T m) : pimpl_{std::make_unique<Impl>()}
4134
{
@@ -104,7 +97,7 @@ std::vector<T> &InertialEnergy<T, dim>::grad()
10497
} // Calculate the gradient of the energy
10598

10699
template <typename T, int dim>
107-
SparseMatrix<T> &InertialEnergy<T, dim>::hess()
100+
SparseMatrix<T> InertialEnergy<T, dim>::hess()
108101
{
109102
return pimpl_->host_hess;
110103
} // Calculate the Hessian matrix of the energy

simulators/1_mass_spring/src/MassSpringEnergy.cu

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,6 @@ template <typename T, int dim>
3232
MassSpringEnergy<T, dim>::MassSpringEnergy(const MassSpringEnergy<T, dim> &rhs)
3333
: pimpl_{std::make_unique<Impl>(*rhs.pimpl_)} {}
3434

35-
template <typename T, int dim>
36-
MassSpringEnergy<T, dim> &MassSpringEnergy<T, dim>::operator=(const MassSpringEnergy<T, dim> &rhs)
37-
{
38-
*pimpl_ = *rhs.pimpl_;
39-
return *this;
40-
}
41-
4235
template <typename T, int dim>
4336
MassSpringEnergy<T, dim>::MassSpringEnergy(const std::vector<T> &x, const std::vector<int> &e, const std::vector<T> &l2, const std::vector<T> &k) : pimpl_{std::make_unique<Impl>()}
4437
{
@@ -120,7 +113,7 @@ std::vector<T> &MassSpringEnergy<T, dim>::grad()
120113
diffi[d] = device_x(dim * idx1 + d) - device_x(dim * idx2 + d);
121114
diff += diffi[d] * diffi[d];
122115
}
123-
T factor=2*device_k(i)*(diff - device_l2(i));
116+
T factor = 2 * device_k(i) * (diff / device_l2(i) -1);
124117
for(int d=0;d<dim;d++){
125118
atomicAdd(&device_grad(dim * idx1 + d), factor * diffi[d]);
126119
atomicAdd(&device_grad(dim * idx2 + d), -factor * diffi[d]);
@@ -132,7 +125,7 @@ std::vector<T> &MassSpringEnergy<T, dim>::grad()
132125
}
133126

134127
template <typename T, int dim>
135-
SparseMatrix<T> &MassSpringEnergy<T, dim>::hess()
128+
SparseMatrix<T> MassSpringEnergy<T, dim>::hess()
136129
{
137130
auto &device_x = pimpl_->device_x;
138131
auto &device_e = pimpl_->device_e;
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#include <muda/muda.h>
2+
#include <muda/container.h>
3+
#include "SparseMatrix.h"
4+
5+
using namespace muda;
6+
template <typename T>
7+
SparseMatrix<T>::SparseMatrix(int size) : size(size)
8+
{
9+
row_idx = std::vector<int>(size);
10+
col_idx = std::vector<int>(size);
11+
val = std::vector<T>(size);
12+
}
13+
template <typename T>
14+
SparseMatrix<T>::SparseMatrix() = default;
15+
16+
template <typename T>
17+
SparseMatrix<T>::~SparseMatrix()
18+
{
19+
val.clear();
20+
row_idx.clear();
21+
col_idx.clear();
22+
}
23+
24+
template <typename T>
25+
SparseMatrix<T>::SparseMatrix(SparseMatrix<T> &&rhs)
26+
{
27+
size = rhs.size;
28+
row_idx = std::move(rhs.row_idx);
29+
col_idx = std::move(rhs.col_idx);
30+
val = std::move(rhs.val);
31+
}
32+
33+
template <typename T>
34+
SparseMatrix<T> &SparseMatrix<T>::operator=(SparseMatrix<T> &&rhs)
35+
{
36+
size = rhs.size;
37+
row_idx = std::move(rhs.row_idx);
38+
col_idx = std::move(rhs.col_idx);
39+
val = std::move(rhs.val);
40+
return *this;
41+
}
42+
template <typename T>
43+
SparseMatrix<T> &SparseMatrix<T>::operator=(SparseMatrix<T> &rhs)
44+
{
45+
size = rhs.size;
46+
row_idx = rhs.row_idx;
47+
col_idx = rhs.col_idx;
48+
val = rhs.val;
49+
return *this;
50+
}
51+
template <typename T>
52+
SparseMatrix<T>::SparseMatrix(const SparseMatrix<T> &rhs)
53+
{
54+
size = rhs.size;
55+
row_idx = rhs.row_idx;
56+
col_idx = rhs.col_idx;
57+
val = rhs.val;
58+
}
59+
60+
template <typename T>
61+
void SparseMatrix<T>::set_value(int row, int col, T value, int loc)
62+
{
63+
assert(loc < size);
64+
row_idx[loc] = row;
65+
col_idx[loc] = col;
66+
val[loc] = value;
67+
}
68+
template <typename T>
69+
void SparseMatrix<T>::set_diagonal(T value)
70+
{
71+
for (int i = 0; i < size; i++)
72+
{
73+
set_value(i, i, value, i);
74+
}
75+
}
76+
template <typename T>
77+
SparseMatrix<T> &SparseMatrix<T>::combine(const SparseMatrix<T> &other)
78+
{
79+
int old_size = size;
80+
size += other.size;
81+
row_idx.resize(size);
82+
col_idx.resize(size);
83+
val.resize(size);
84+
// copy memory
85+
for (int i = 0; i < other.size; i++)
86+
{
87+
set_value(other.row_idx[i], other.col_idx[i], other.val[i], i + old_size);
88+
}
89+
return *this;
90+
}
91+
template <typename T>
92+
SparseMatrix<T> &SparseMatrix<T>::operator*(const T &a)
93+
{
94+
DeviceBuffer<T> val_device(val);
95+
int N = val.size();
96+
DeviceBuffer<T> c_device(N);
97+
ParallelFor(256)
98+
.apply(N,
99+
[c_device = c_device.viewer(), val_device = val_device.cviewer(), a] __device__(int i) mutable
100+
{
101+
c_device(i) = val_device(i) * a;
102+
})
103+
.wait();
104+
c_device.copy_to(val);
105+
return *this;
106+
}
107+
template <typename T>
108+
const std::vector<int> &SparseMatrix<T>::get_row_buffer() const
109+
{
110+
return row_idx;
111+
}
112+
template <typename T>
113+
const std::vector<int> &SparseMatrix<T>::get_col_buffer() const
114+
{
115+
return col_idx;
116+
}
117+
template <typename T>
118+
const std::vector<T> &SparseMatrix<T>::get_val_buffer() const
119+
{
120+
return val;
121+
}
122+
123+
template <typename T>
124+
std::vector<int> &SparseMatrix<T>::set_row_buffer()
125+
{
126+
return row_idx;
127+
}
128+
template <typename T>
129+
std::vector<int> &SparseMatrix<T>::set_col_buffer()
130+
{
131+
return col_idx;
132+
}
133+
template <typename T>
134+
std::vector<T> &SparseMatrix<T>::set_val_buffer()
135+
{
136+
return val;
137+
}
138+
template <typename T>
139+
int SparseMatrix<T>::get_size() const
140+
{
141+
return size;
142+
}
143+
template class SparseMatrix<float>;
144+
template class SparseMatrix<double>;

simulators/1_mass_spring/src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
int main()
44
{
5-
float rho = 1000, k = 1e5, initial_stretch = 1.4, n_seg = 5, h = 0.004, side_len = 1, tol = 1e-2;
5+
float rho = 1000, k = 1e5, initial_stretch = 1.4, n_seg = 4, h = 0.004, side_len = 1, tol = 0.01;
66
// printf("Running mass-spring simulator with parameters: rho = %f, k = %f, initial_stretch = %f, n_seg = %d, h = %f, side_len = %f, tol = %f\n", rho, k, initial_stretch, n_seg, h, side_len, tol);
77
MassSpringSimulator<float, 2> simulator(rho, side_len, initial_stretch, k, h, tol, n_seg);
88
simulator.run();

0 commit comments

Comments
 (0)