Skip to content

Commit 07afd82

Browse files
committed
1. optimize
1 parent 189f0d1 commit 07afd82

File tree

6 files changed

+31
-53
lines changed

6 files changed

+31
-53
lines changed

include/RI/global/Global_Func-1.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace Global_Func
2323
// Peize Lin add 2022.05.26
2424
template<typename Tkey, typename Tdata,
2525
typename std::enable_if<std::is_arithmetic<Tdata>::value,bool>::type=0>
26-
Tdata find(
26+
inline Tdata find(
2727
const std::map<Tkey, Tdata> &m,
2828
const Tkey &key)
2929
{
@@ -34,7 +34,7 @@ namespace Global_Func
3434
return ptr->second;
3535
}
3636
template<typename Tkey, typename Tdata>
37-
Tensor<Tdata> find(
37+
inline Tensor<Tdata> find(
3838
const std::map<Tkey, Tensor<Tdata>> &m,
3939
const Tkey &key)
4040
{
@@ -45,7 +45,7 @@ namespace Global_Func
4545
return ptr->second;
4646
}
4747
template<typename Tkey, typename Tdata, std::size_t Ndim>
48-
std::array<Tdata,Ndim> find(
48+
inline std::array<Tdata,Ndim> find(
4949
const std::map<Tkey, std::array<Tdata,Ndim>> &m,
5050
const Tkey &key)
5151
{
@@ -56,7 +56,7 @@ namespace Global_Func
5656
return ptr->second;
5757
}
5858
template<typename Tkey, typename Tvalue, typename... Tkeys>
59-
auto find(
59+
inline auto find(
6060
const std::map<Tkey, Tvalue> &m,
6161
const Tkey &key,
6262
const Tkeys&... keys)
@@ -72,7 +72,7 @@ namespace Global_Func
7272
// in_set(3, {2,3,5,7})
7373
// Peize Lin add 2022.05.26
7474
template<typename T>
75-
bool in_set(const T &item, const std::set<T> &s)
75+
inline bool in_set(const T &item, const std::set<T> &s)
7676
{
7777
return s.find(item) != s.end();
7878
}
@@ -98,12 +98,12 @@ namespace Global_Func
9898
}
9999

100100
template<typename T>
101-
std::set<T> to_set(const std::vector<T> &v)
101+
inline std::set<T> to_set(const std::vector<T> &v)
102102
{
103103
return std::set<T>(v.begin(), v.end());
104104
}
105105
template<typename T, std::size_t N>
106-
std::vector<T> to_vector(const std::array<T,N> &v)
106+
inline std::vector<T> to_vector(const std::array<T,N> &v)
107107
{
108108
return std::vector<T>(v.begin(), v.end());
109109
}

include/RI/global/Tensor.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@ class Tensor
2222
{
2323
public:
2424
std::vector<std::size_t> shape;
25-
std::shared_ptr<std::valarray<T>> data;
26-
27-
Tensor(){};
28-
explicit Tensor (const std::vector<std::size_t> &shape_in);
29-
explicit Tensor (const std::vector<std::size_t> &shape_in, std::shared_ptr<std::valarray<T>> data_in);
30-
//Tensor (const Tensor<T> &t_in);
31-
//Tensor (Tensor<T> &&t_in);
32-
//Tensor<T> &operator=(const Tensor<T> &t_in);
33-
//Tensor<T> &operator=(Tensor<T> &&t_in);
25+
std::shared_ptr<std::valarray<T>> data=nullptr;
26+
27+
explicit inline Tensor (const std::vector<std::size_t> &shape_in);
28+
explicit inline Tensor (const std::vector<std::size_t> &shape_in, std::shared_ptr<std::valarray<T>> data_in);
29+
30+
Tensor()=default;
31+
Tensor(const Tensor<T> &t_in)=default;
32+
Tensor(Tensor<T> &&t_in)=default;
33+
Tensor<T> &operator=(const Tensor<T> &t_in)=default;
34+
Tensor<T> &operator=(Tensor<T> &&t_in)=default;
3435

3536
inline std::size_t get_shape_all() const;
3637
inline Tensor reshape (const std::vector<std::size_t> &shape_in) const;

include/RI/global/Tensor.hpp

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ template<typename T>
2323
Tensor<T>::Tensor (const std::vector<std::size_t> &shape_in)
2424
{
2525
this->shape = shape_in;
26-
const std::size_t shape_all = get_shape_all();
27-
this->data = std::make_shared<std::valarray<T>>(0,shape_all);
26+
if(!this->shape.empty())
27+
{
28+
const std::size_t shape_all = get_shape_all();
29+
if(shape_all)
30+
this->data = std::make_shared<std::valarray<T>>(0,shape_all);
31+
}
2832
}
2933

3034
template<typename T>
@@ -35,38 +39,6 @@ Tensor<T>::Tensor (const std::vector<std::size_t> &shape_in, std::shared_ptr<std
3539
this->data = data_in;
3640
}
3741

38-
/*
39-
template<typename T>
40-
Tensor<T>::Tensor (const Tensor<T> &t_in)
41-
{
42-
this->shape = t_in.shape;
43-
this->data = t_in.data;
44-
}
45-
46-
template<typename T>
47-
Tensor<T>::Tensor (Tensor<T> &&t_in)
48-
{
49-
this->shape = std::move(t_in.shape);
50-
this->data = std::move(t_in.data);
51-
}
52-
53-
template<typename T>
54-
Tensor<T> &Tensor<T>::operator=(const Tensor<T> &t_in)
55-
{
56-
this->shape = t_in.shape;
57-
this->data = t_in.data;
58-
return *this;
59-
}
60-
61-
template<typename T>
62-
Tensor<T> &Tensor<T>::operator=(Tensor<T> &&t_in)
63-
{
64-
this->shape = std::move(t_in.shape);
65-
this->data = std::move(t_in.data);
66-
return *this;
67-
}
68-
*/
69-
7042
template<typename T>
7143
std::size_t Tensor<T>::get_shape_all() const
7244
{

include/RI/ri/CS_Matrix_Tools.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ namespace CS_Matrix_Tools
5252
auto three_0 = [&D](const std::function<Tlim(Tensor<Tdata>)> &func) -> Tlim
5353
{
5454
std::valarray<Tlim> uplimits(D.shape[0]);
55+
Tensor<Tdata> D_sub({D.shape[1], D.shape[2]});
5556
for(std::size_t i0=0; i0<D.shape[0]; ++i0)
5657
{
57-
Tensor<Tdata> D_sub({D.shape[1], D.shape[2]});
5858
memcpy(
5959
D_sub.ptr(),
6060
D.ptr()+i0*D.shape[1]*D.shape[2],
@@ -67,9 +67,9 @@ namespace CS_Matrix_Tools
6767
auto three_1 = [&D](const std::function<Tlim(Tensor<Tdata>)> &func) -> Tlim
6868
{
6969
std::valarray<Tlim> uplimits(D.shape[1]);
70+
Tensor<Tdata> D_sub({D.shape[0], D.shape[2]});
7071
for(std::size_t i1=0; i1<D.shape[1]; ++i1)
7172
{
72-
Tensor<Tdata> D_sub({D.shape[0], D.shape[2]});
7373
for(std::size_t i0=0; i0<D.shape[0]; ++i0)
7474
memcpy(
7575
D_sub.ptr()+i0*D.shape[2],

unittests/global/Tensor-test.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ std::ostream &operator<<(std::ostream &os, const RI::Tensor<T> &t)
1212
{
1313
switch(t.shape.size())
1414
{
15+
case 0:
16+
{
17+
os<<std::endl;
18+
return os;
19+
}
1520
case 1:
1621
{
1722
for(std::size_t i0=0; i0<t.shape[0]; ++i0)

unittests/ri/LRI-test.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ namespace LRI_Test
9191
lri.parallel = std::make_shared<Parallel_LRI_test<int,int,Ndim,Tdata>>();
9292
lri.set_parallel(MPI_COMM_WORLD, {}, {}, {1});
9393

94-
lri.csm.set_threshold(0);
94+
lri.csm.set_threshold(1E-10);
9595

9696
for(const RI::Label::ab &label : RI::Label::array_ab)
9797
lri.set_tensors_map2(Ds_ab[label], label, 0);

0 commit comments

Comments
 (0)