ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
pw_gatherscatter.h
Go to the documentation of this file.
1#include "pw_basis.h"
3#include "source_base/timer.h"
4#include <algorithm>
5#include <typeinfo>
6
7namespace ModulePW
8{
9namespace detail
10{
11template <typename T>
12inline void copy_complex_buffer(const std::complex<T>* in, std::complex<T>* out, const int count)
13{
14 if (count <= 0)
15 {
16 return;
17 }
18
19 std::copy_n(in, count, out);
20}
21
22// Top-level transform copies own the OpenMP parallel region; gather/scatter
23// loops call the non-parallel helper inside their existing parallel regions.
24template <typename T>
25inline void copy_complex_buffer_parallel(const std::complex<T>* in, std::complex<T>* out, const int count)
26{
27 constexpr int chunk_size = 1024;
28 if (count <= chunk_size)
29 {
30 copy_complex_buffer(in, out, count);
31 return;
32 }
33
34#ifdef _OPENMP
35#pragma omp parallel for schedule(static)
36 for (int offset = 0; offset < count; offset += chunk_size)
37 {
38 const int chunk_count = std::min(chunk_size, count - offset);
39 std::copy_n(in + offset, chunk_count, out + offset);
40 }
41#else
42 copy_complex_buffer(in, out, count);
43#endif
44}
45} // namespace detail
46
54template <typename T>
55void PW_Basis::gatherp_scatters(std::complex<T>* in, std::complex<T>* out) const
56{
57
58 if(this->poolnproc == 1) //In this case nst=nstot, nz = nplane,
59 {
60 const int nst_ = this->nst;
61 const int nz_ = this->nz;
62 const int* istot2ixy_ = this->istot2ixy;
63 ModuleBase::timer::start(this->classname, "gatherp_copy_serial");
64#ifdef _OPENMP
65#pragma omp parallel for
66#endif
67 for(int is = 0 ; is < nst_ ; ++is)
68 {
69 int ixy = istot2ixy_[is];
70 std::complex<T>* outp = &out[is*nz_];
71 const std::complex<T>* inp = &in[ixy*nz_];
72 detail::copy_complex_buffer(inp, outp, nz_);
73 }
74 ModuleBase::timer::end(this->classname, "gatherp_copy_serial");
75 return;
76 }
77
78
79#ifdef __MPI
80 //change (nplane fftnxy) to (nplane,nstot)
81 // Hence, we can send them at one time.
82 const int nstot_gps = this->nstot;
83 const int nplane_gps = this->nplane;
84 const int* istot2ixy_gps = this->istot2ixy;
85 ModuleBase::timer::start(this->classname, "gatherp_copy_pack");
86#ifdef _OPENMP
87 #pragma omp parallel for
88#endif
89 for (int istot = 0; istot < nstot_gps; ++istot)
90 {
91 int ixy = istot2ixy_gps[istot];
92 std::complex<T>* outp = &out[istot * nplane_gps];
93 const std::complex<T>* inp = &in[ixy * nplane_gps];
94 detail::copy_complex_buffer(inp, outp, nplane_gps);
95 }
96 ModuleBase::timer::end(this->classname, "gatherp_copy_pack");
97
98 //exchange data
99 //(nplane,nstot) to (numz[ip],ns, poolnproc)
100 if(typeid(T) == typeid(double))
101 {
102 MPI_Alltoallv(out, numr, startr, MPI_DOUBLE_COMPLEX, in, numg, startg, MPI_DOUBLE_COMPLEX, this->pool_world);
103 }
104 else if(typeid(T) == typeid(float))
105 {
106 MPI_Alltoallv(out, numr, startr, MPI_COMPLEX, in, numg, startg, MPI_COMPLEX, this->pool_world);
107 }
108 else
109 {
110 ModuleBase::WARNING_QUIT("PW_Basis::gatherp_scatters", "Unsupported data type for MPI_Alltoallv");
111 }
112
113 // change (nz,ns) to (numz[ip],ns, poolnproc)
114 const int poolnproc_gps = this->poolnproc;
115 const int nst_gps = this->nst;
116 const int nz_gps = this->nz;
117 const int* numz_gps = this->numz;
118 const int* startg_gps = this->startg;
119 const int* startz_gps = this->startz;
120 ModuleBase::timer::start(this->classname, "gatherp_copy_unpack");
121#ifdef _OPENMP
122 #pragma omp parallel for collapse(2)
123#endif
124 for (int ip = 0; ip < poolnproc_gps ;++ip)
125 {
126 for (int is = 0; is < nst_gps; ++is)
127 {
128 int nzip = numz_gps[ip];
129 std::complex<T> *outp0 = &out[startz_gps[ip]];
130 std::complex<T> *inp0 = &in[startg_gps[ip]];
131 std::complex<T>* outp = &outp0[is * nz_gps];
132 const std::complex<T>* inp = &inp0[is * nzip ];
133 detail::copy_complex_buffer(inp, outp, nzip);
134 }
135 }
136 ModuleBase::timer::end(this->classname, "gatherp_copy_unpack");
137#endif
138 return;
139}
140
148template <typename T>
149void PW_Basis::gathers_scatterp(std::complex<T>* in, std::complex<T>* out) const
150{
151 if(this->poolnproc == 1) //In this case nrxx=fftnx*fftny*nz, nst = nstot,
152 {
153 const int nrxx_ = this->nrxx;
154 const int nst_ = this->nst;
155 const int nz_ = this->nz;
156 const int* istot2ixy_ = this->istot2ixy;
157 ModuleBase::timer::start(this->classname, "gathers_zero_serial");
158#ifdef _OPENMP
159#pragma omp parallel for schedule(static)
160#endif
161 for(int i = 0; i < nrxx_; ++i)
162 {
163 out[i] = std::complex<T>(0, 0);
164 }
165 ModuleBase::timer::end(this->classname, "gathers_zero_serial");
166
167 ModuleBase::timer::start(this->classname, "gathers_copy_serial");
168#ifdef _OPENMP
169#pragma omp parallel for
170#endif
171 for(int is = 0 ; is < nst_ ; ++is)
172 {
173 int ixy = istot2ixy_[is];
174 std::complex<T>* outp = &out[ixy*nz_];
175 const std::complex<T>* inp = &in[is*nz_];
176 detail::copy_complex_buffer(inp, outp, nz_);
177 }
178 ModuleBase::timer::end(this->classname, "gathers_copy_serial");
179 return;
180 }
181
182
183#ifdef __MPI
184 // change (nz,ns) to (numz[ip],ns, poolnproc)
185 // Hence, we can send them at one time.
186 const int poolnproc_ = this->poolnproc;
187 const int nst_ = this->nst;
188 const int nz_ = this->nz;
189 const int* numz_ = this->numz;
190 const int* startg_ = this->startg;
191 const int* startz_ = this->startz;
192 ModuleBase::timer::start(this->classname, "gathers_copy_pack");
193#ifdef _OPENMP
194 #pragma omp parallel for collapse(2)
195#endif
196 for (int ip = 0; ip < poolnproc_ ;++ip)
197 {
198 for (int is = 0; is < nst_; ++is)
199 {
200 int nzip = numz_[ip];
201 std::complex<T> *outp0 = &out[startg_[ip]];
202 std::complex<T> *inp0 = &in[startz_[ip]];
203 std::complex<T>* outp = &outp0[is * nzip];
204 const std::complex<T>* inp = &inp0[is * nz_ ];
205 detail::copy_complex_buffer(inp, outp, nzip);
206 }
207 }
208 ModuleBase::timer::end(this->classname, "gathers_copy_pack");
209
210 //exchange data
211 //(numz[ip],ns, poolnproc) to (nplane,nstot)
212 if(typeid(T) == typeid(double))
213 {
214 MPI_Alltoallv(out, numg, startg, MPI_DOUBLE_COMPLEX, in, numr, startr, MPI_DOUBLE_COMPLEX, this->pool_world);
215 }
216 else if(typeid(T) == typeid(float))
217 {
218 MPI_Alltoallv(out, numg, startg, MPI_COMPLEX, in, numr, startr, MPI_COMPLEX, this->pool_world);
219 }
220 else
221 {
222 ModuleBase::WARNING_QUIT("PW_Basis::gathers_scatterp", "Unsupported data type for MPI_Alltoallv");
223 }
224
225 const int nrxx_gsp = this->nrxx;
226 ModuleBase::timer::start(this->classname, "gathers_zero_mpi");
227#ifdef _OPENMP
228 #pragma omp parallel for schedule(static)
229#endif
230 for(int i = 0; i < nrxx_gsp; ++i)
231 {
232 out[i] = std::complex<T>(0, 0);
233 }
234 ModuleBase::timer::end(this->classname, "gathers_zero_mpi");
235 //change (nplane,nstot) to (nplane fftnxy)
236 const int nstot = this->nstot;
237 const int nplane = this->nplane;
238 const int* istot2ixy = this->istot2ixy;
239 ModuleBase::timer::start(this->classname, "gathers_copy_unpack");
240#ifdef _OPENMP
241#pragma omp parallel for
242#endif
243 for (int istot = 0;istot < nstot; ++istot)
244 {
245 int ixy = istot2ixy[istot];
246 //int ixy = (ixy / fftny)*ny + ixy % fftny;
247 std::complex<T>* outp = &out[ixy * nplane];
248 const std::complex<T>* inp = &in[istot * nplane];
250 }
251 ModuleBase::timer::end(this->classname, "gathers_copy_unpack");
252#endif
253 return;
254}
255
256
257
258}
static void start(void)
Start total time calculation.
Definition timer.cpp:44
static void end(const std::string &class_name_in, const std::string &name_in)
Definition timer.cpp:109
int nrxx
Definition pw_basis.h:127
int nst
Definition pw_basis.h:118
int nstot
Definition pw_basis.h:121
int * numz
Definition pw_basis.h:129
int poolnproc
Definition pw_basis.h:189
void gatherp_scatters(std::complex< T > *in, std::complex< T > *out) const
gather planes and scatter sticks
Definition pw_gatherscatter.h:55
int * startr
Definition pw_basis.h:133
int * startg
Definition pw_basis.h:132
int * numr
Definition pw_basis.h:131
int * istot2ixy
Definition pw_basis.h:115
std::string classname
Definition pw_basis.h:66
int nplane
Definition pw_basis.h:135
MPI_Comm pool_world
Definition pw_basis.h:110
void gathers_scatterp(std::complex< T > *in, std::complex< T > *out) const
gather sticks and scatter planes
Definition pw_gatherscatter.h:149
int * numg
Definition pw_basis.h:130
int * startz
Definition pw_basis.h:128
int nz
Definition pw_basis.h:246
#define T
Definition exp.cpp:237
void WARNING_QUIT(const std::string &, const std::string &)
Combine the functions of WARNING and QUIT.
Definition test_delley.cpp:15
Definition pw_gatherscatter.h:10
void copy_complex_buffer_parallel(const std::complex< T > *in, std::complex< T > *out, const int count)
Definition pw_gatherscatter.h:25
void copy_complex_buffer(const std::complex< T > *in, std::complex< T > *out, const int count)
Definition pw_gatherscatter.h:12
Definition pw_op.cpp:3