Skip to content

Commit dc2254b

Browse files
committed
Squashed commit of the following:
commit bd40883 Author: maki49 <[email protected]> Date: Tue Sep 30 17:49:30 2025 +0800 remove timer and title commit ae7a76d Author: maki49 <[email protected]> Date: Fri Sep 26 21:15:49 2025 +0800 minimize the index access commit cb68103 Author: maki49 <[email protected]> Date: Fri Sep 26 21:03:58 2025 +0800 wait after each term to save memory commit 4793d3c Author: maki49 <[email protected]> Date: Wed Sep 24 13:57:26 2025 +0800 malloc_trim(0) commit 150b929 Author: maki49 <[email protected]> Date: Wed Sep 24 13:43:32 2025 +0800 fix: filter zero-tensors commit 3a773e4 Author: maki49 <[email protected]> Date: Tue Sep 23 21:27:58 2025 +0800 symmetry filter commit 811e6da Author: maki49 <[email protected]> Date: Tue Sep 23 21:27:22 2025 +0800 default cvc (only for test) commit 3f4b6b9 Author: maki49 <[email protected]> Date: Tue Sep 23 19:02:13 2025 +0800 omp parallel method 3 commit d91c80d Author: maki49 <[email protected]> Date: Tue Sep 23 17:31:38 2025 +0800 omp parallel method 2 commit 0a8a529 Author: maki49 <[email protected]> Date: Tue Sep 23 16:08:48 2025 +0800 fix omp bug commit e329128 Author: maki49 <[email protected]> Date: Sat Sep 20 16:58:40 2025 +0800 CVC (D-perspective) algorithm (draft) fix adding tensor maps fix compile problems commit d4f7320 Author: Minye Zhang <[email protected]> Date: Mon May 19 17:25:38 2025 +0200 remove redundant member variables
1 parent 08fdf0b commit dc2254b

File tree

13 files changed

+478
-48
lines changed

13 files changed

+478
-48
lines changed

include/RI/global/Shared_Vector.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ class Shape_Vector
2828
for(auto ptr_in=v_in.begin(); ptr_in<v_in.end(); )
2929
*(ptr_this++) = *(ptr_in++);
3030
}
31+
Shape_Vector(const std::vector<std::size_t>& v_in)
32+
:size_(v_in.size())
33+
{
34+
assert(v_in.size() <= sizeof(v) / sizeof(*v));
35+
for (std::size_t i = 0;i < size_;++i) this->v[i] = v_in[i];
36+
}
37+
Shape_Vector(std::vector<std::size_t>&& v_in)
38+
:size_(v_in.size())
39+
{
40+
assert(v_in.size() <= sizeof(v) / sizeof(*v));
41+
for (std::size_t i = 0;i < size_;++i) this->v[i] = v_in[i];
42+
}
3143

3244
const std::size_t* begin() const noexcept { return this->v; }
3345
const std::size_t* end() const noexcept { return this->v+size_; }

include/RI/global/Tensor.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ class Tensor
4747
Tensor transpose() const;
4848
Tensor dagger() const;
4949

50+
/// permute from the input index order to {0, 1, 2, ..., N}
51+
Tensor permute_from(const std::vector<std::size_t>& order) const;
52+
/// permute from {0, 1, 2, ..., N } to the input new index order
53+
// Tensor permute_to(const std::vector<std::size_t>) const;
54+
5055
// ||d||_p = (|d_1|^p+|d_2|^p+...)^{1/p}
5156
// if(p==std::numeric_limits<double>::max()) ||d||_max = max_i |d_i|
5257
Global_Func::To_Real_t<T> norm(const double p) const;

include/RI/global/Tensor.hpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,4 +324,74 @@ std::array<std::array<std::array<std::array<T,N3>,N2>,N1>,N0> to_array(const Ten
324324
return a;
325325
}
326326

327+
// check whether an array is a permutation of {from, from+1, ..., to}
328+
inline bool is_permutation(const std::vector<size_t>& arr, const std::size_t from, const std::size_t to)
329+
{
330+
std::vector<bool> seen(arr.size(), false);
331+
if (arr.size() != to - from + 1) return false;
332+
for (std::size_t i = 0; i < arr.size(); ++i) {
333+
if (arr[i] < from || arr[i] > to) return false;
334+
const std::size_t idx = arr[i] - from;
335+
if (seen[idx]) return false;
336+
seen[idx] = true;
337+
}
338+
return true;
339+
}
340+
341+
template<typename T>
342+
Tensor<T> Tensor<T>::permute_from(const std::vector<std::size_t>& order) const
343+
{
344+
const std::size_t N = order.size();
345+
assert(this->shape.size() == N);
346+
assert(is_permutation(order, 0, N-1));
347+
Shape_Vector new_shape(this->shape);
348+
for (std::size_t d = 0; d < N; ++d)
349+
new_shape[d] = this->shape[order[d]];
350+
351+
Tensor<T> t(new_shape);
352+
353+
/* N-permutation
354+
for (std::size_t i0 = 0; i0 < new_shape[0], ++i0)
355+
for (std::size_t i1 = 0, i1 < new_shape[1], ++i1)
356+
...
357+
for (std::size_t iN = 0, iN < new_shape[N], ++iN)
358+
t(i0, i1, ..., iN) = this->operator()(order(i0), order(i1), ..., order(N))
359+
*/
360+
std::vector<std::size_t> strides(N);
361+
strides[N - 1] = 1;
362+
for (int d = N - 2; d >= 0; --d)
363+
strides[d] = strides[d + 1] * this->shape[d + 1];
364+
365+
std::vector<std::size_t> new_strides(N);
366+
new_strides[N - 1] = 1;
367+
for (int d = N - 2; d >= 0; --d)
368+
new_strides[d] = new_strides[d + 1] * new_shape[d + 1];
369+
370+
371+
std::vector<std::size_t> new_idx(N, 0);
372+
std::vector<std::size_t> idx(N, 0);
373+
for (std::size_t i = 0; i < t.get_shape_all(); ++i) // new 1D-index
374+
{
375+
// new ND-index
376+
std::size_t tmp = i;
377+
for (std::size_t d = 0; d < N; ++d)
378+
{
379+
new_idx[d] = tmp / new_strides[d];
380+
tmp %= new_strides[d];
381+
}
382+
// old ND-index
383+
for (std::size_t d = 0; d < N; ++d)
384+
idx[order[d]] = new_idx[d];
385+
386+
// old 1D-index
387+
std::size_t i0 = 0;
388+
for (std::size_t d = 0; d < N; ++d)
389+
i0 += idx[d] * strides[d];
390+
391+
(*t.data)[i] = (*this->data)[i0];
392+
}
393+
394+
return t;
395+
}
396+
327397
}

include/RI/global/Tensor_Multiply-32.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,28 @@ namespace Tensor_Multiply
8080
return Txy;
8181
}
8282

83+
// Txy(x0,x1,..., xM) = Tx(x0, x1,x2, ..., xM, y0, y1, ..., yN) * Vy(y0, y1, ..., yN)
84+
template<typename Tdata>
85+
Tensor<Tdata> gemv(const Tensor<Tdata>& Tx, const Tensor<Tdata>& Vy)
86+
{
87+
assert(Tx.shape.size() >= Vy.shape.size());
88+
const std::size_t ny = Vy.get_shape_all();
89+
assert(Tx.get_shape_all() % ny == 0);
90+
const std::size_t dim = Tx.shape.size() - Vy.shape.size();
91+
std::vector<std::size_t> shape_vector;
92+
if (dim == 0)
93+
shape_vector.push_back(1);
94+
else
95+
for (int d = 0; d < dim;++d)
96+
shape_vector.push_back(Tx.shape[d]);
97+
Tensor<Tdata> Txy(shape_vector);
98+
Blas_Interface::gemv(
99+
'N', Txy.get_shape_all(), ny,
100+
Tdata(1.0), Tx.ptr(), ny, Vy.ptr(), 1,
101+
Tdata(0.0), Txy.ptr(), 1);
102+
return Txy;
103+
}
104+
83105
}
84106

85107
}

include/RI/physics/Exx.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,9 @@ class Exx
126126
};
127127
Flag_Save_Result flag_save_result;
128128

129-
MPI_Comm mpi_comm;
130-
std::map<TA,Tatom_pos> atoms_pos;
131-
std::array<Tatom_pos,Ndim> latvec;
132-
std::array<Tcell,Ndim> period;
129+
std::string method = "loop3"; // "loop3", "cvc"
130+
std::map<TA, std::map<TAC, std::map<TA, std::map<TAC, Tensor<Tdata>>>>> cvc_;
131+
std::map<TA, std::map<TAC, std::map<TA, std::map<TAC, Tensor<Tdata>>>>>& get_cvc() const { return this->cvc_; }
133132
};
134133

135134
}

include/RI/physics/Exx.hpp

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,17 @@ namespace RI
1818

1919
template<typename TA, typename Tcell, std::size_t Ndim, typename Tdata>
2020
void Exx<TA,Tcell,Ndim,Tdata>::set_parallel(
21-
const MPI_Comm &mpi_comm_in,
22-
const std::map<TA,Tatom_pos> &atoms_pos_in,
23-
const std::array<Tatom_pos,Ndim> &latvec_in,
24-
const std::array<Tcell,Ndim> &period_in)
21+
const MPI_Comm &mpi_comm,
22+
const std::map<TA,Tatom_pos> &atoms_pos,
23+
const std::array<Tatom_pos,Ndim> &latvec,
24+
const std::array<Tcell,Ndim> &period)
2525
{
26-
this->mpi_comm = mpi_comm_in;
27-
this->atoms_pos = atoms_pos_in;
28-
this->latvec = latvec_in;
29-
this->period = period_in;
30-
3126
this->lri.set_parallel(
32-
this->mpi_comm, this->atoms_pos, this->latvec, this->period,
27+
mpi_comm, atoms_pos, latvec, period,
3328
{Label::ab_ab::a0b0_a1b1, Label::ab_ab::a0b0_a1b2, Label::ab_ab::a0b0_a2b1, Label::ab_ab::a0b0_a2b2});
3429
this->flag_finish.stru = true;
3530
//if()
36-
this->post_2D.set_parallel(this->mpi_comm, this->atoms_pos, this->period);
31+
this->post_2D.set_parallel(mpi_comm, atoms_pos, period);
3732
}
3833

3934
template<typename TA, typename Tcell, std::size_t Ndim, typename Tdata>
@@ -43,7 +38,7 @@ void Exx<TA,Tcell,Ndim,Tdata>::set_symmetry(
4338
{
4439
if(flag_symmetry)
4540
this->lri.filter_atom = std::make_shared<Filter_Atom_Symmetry<TA,TC,Tdata>>(
46-
this->period, irreducible_sector);
41+
this->lri.period, irreducible_sector);
4742
else
4843
this->lri.filter_atom = std::make_shared<Filter_Atom<TA,TAC>>();
4944
}
@@ -271,12 +266,21 @@ void Exx<TA,Tcell,Ndim,Tdata>::cal_Hs(
271266

272267
if(!this->flag_finish.Ds_delta)
273268
this->Hs.clear();
274-
this->lri.cal_loop3(
275-
{Label::ab_ab::a0b0_a1b1,
276-
Label::ab_ab::a0b0_a1b2,
277-
Label::ab_ab::a0b0_a2b1,
278-
Label::ab_ab::a0b0_a2b2},
279-
this->Hs);
269+
if (this->method == "loop3")
270+
this->lri.cal_loop3(
271+
{ Label::ab_ab::a0b0_a1b1,
272+
Label::ab_ab::a0b0_a1b2,
273+
Label::ab_ab::a0b0_a2b1,
274+
Label::ab_ab::a0b0_a2b2 },
275+
this->Hs);
276+
else if (this->method == "cvc")
277+
{
278+
if (this->cvc_.empty())
279+
this->cvc_ = this->lri.cal_cvc();
280+
this->Hs = this->lri.constract_cvc_ds(this->cvc_);
281+
}
282+
else
283+
assert(false && "Unknown method in Exx::cal_Hs");
280284

281285
//if()
282286
this->energy = this->post_2D.cal_energy(

include/RI/physics/GW.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,6 @@ class GW
7676
bool Gs=false;
7777
};
7878
Flag_Finish flag_finish;
79-
80-
MPI_Comm mpi_comm;
81-
std::map<TA,Tatom_pos> atoms_pos;
82-
std::array<Tatom_pos,Ndim> latvec;
83-
std::array<Tcell,Ndim> period;
8479
};
8580

8681
}

include/RI/physics/GW.hpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,13 @@ namespace RI
1313

1414
template<typename TA, typename Tcell, std::size_t Ndim, typename Tdata>
1515
void GW<TA,Tcell,Ndim,Tdata>::set_parallel(
16-
const MPI_Comm &mpi_comm_in,
17-
const std::map<TA,Tatom_pos> &atoms_pos_in,
18-
const std::array<Tatom_pos,Ndim> &latvec_in,
19-
const std::array<Tcell,Ndim> &period_in)
16+
const MPI_Comm &mpi_comm,
17+
const std::map<TA,Tatom_pos> &atoms_pos,
18+
const std::array<Tatom_pos,Ndim> &latvec,
19+
const std::array<Tcell,Ndim> &period)
2020
{
21-
this->mpi_comm = mpi_comm_in;
22-
this->atoms_pos = atoms_pos_in;
23-
this->latvec = latvec_in;
24-
this->period = period_in;
25-
2621
this->lri.set_parallel(
27-
this->mpi_comm, this->atoms_pos, this->latvec, this->period,
22+
mpi_comm, atoms_pos, latvec, period,
2823
{Label::ab_ab::a0b0_a1b1, Label::ab_ab::a0b0_a1b2, Label::ab_ab::a0b0_a2b1, Label::ab_ab::a0b0_a2b2});
2924
this->flag_finish.stru = true;
3025
//if()
@@ -38,7 +33,7 @@ void GW<TA,Tcell,Ndim,Tdata>::set_symmetry(
3833
{
3934
if(flag_symmetry)
4035
this->lri.filter_atom = std::make_shared<Filter_Atom_Symmetry<TA,TC,Tdata>>(
41-
this->period, irreducible_sector);
36+
this->lri.period, irreducible_sector);
4237
else
4338
this->lri.filter_atom = std::make_shared<Filter_Atom<TA,TAC>>();
4439
}

include/RI/physics/RPA.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void RPA<TA,Tcell,Ndim,Tdata>::set_symmetry(
3434
{
3535
if(flag_symmetry)
3636
this->lri.filter_atom = std::make_shared<Filter_Atom_Symmetry<TA,TC,Tdata>>(
37-
this->period, irreducible_sector);
37+
this->lri.period, irreducible_sector);
3838
else
3939
this->lri.filter_atom = std::make_shared<Filter_Atom<TA,TAC>>();
4040
}

0 commit comments

Comments
 (0)