ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
parallel_device.h
Go to the documentation of this file.
1#ifndef __PARALLEL_DEVICE_H__
2#define __PARALLEL_DEVICE_H__
3#ifdef __MPI
4#include "mpi.h"
5#include <complex>
6#include <type_traits>
8namespace Parallel_Common
9{
10void isend_data(const double* buf, int count, int dest, int tag, MPI_Comm& comm, MPI_Request* request);
11void isend_data(const std::complex<double>* buf, int count, int dest, int tag, MPI_Comm& comm, MPI_Request* request);
12void isend_data(const float* buf, int count, int dest, int tag, MPI_Comm& comm, MPI_Request* request);
13void isend_data(const std::complex<float>* buf, int count, int dest, int tag, MPI_Comm& comm, MPI_Request* request);
14void send_data(const double* buf, int count, int dest, int tag, MPI_Comm& comm);
15void send_data(const std::complex<double>* buf, int count, int dest, int tag, MPI_Comm& comm);
16void send_data(const float* buf, int count, int dest, int tag, MPI_Comm& comm);
17void send_data(const std::complex<float>* buf, int count, int dest, int tag, MPI_Comm& comm);
18void recv_data(double* buf, int count, int source, int tag, MPI_Comm& comm, MPI_Status* status);
19void recv_data(std::complex<double>* buf, int count, int source, int tag, MPI_Comm& comm, MPI_Status* status);
20void recv_data(float* buf, int count, int source, int tag, MPI_Comm& comm, MPI_Status* status);
21void recv_data(std::complex<float>* buf, int count, int source, int tag, MPI_Comm& comm, MPI_Status* status);
22void bcast_data(std::complex<double>* object, const int& n, const MPI_Comm& comm, int root = 0);
23void bcast_data(std::complex<float>* object, const int& n, const MPI_Comm& comm, int root = 0);
24void bcast_data(double* object, const int& n, const MPI_Comm& comm, int root = 0);
25void bcast_data(float* object, const int& n, const MPI_Comm& comm, int root = 0);
26void reduce_data(std::complex<double>* object, const int& n, const MPI_Comm& comm);
27// Sum host data into the same buffer on root. Non-root buffers are unchanged.
28void reduce_data_to_root(std::complex<double>* object, const int n, const MPI_Comm& comm, const int root);
29void reduce_data(std::complex<float>* object, const int& n, const MPI_Comm& comm);
30void reduce_data(double* object, const int& n, const MPI_Comm& comm);
31void reduce_data(float* object, const int& n, const MPI_Comm& comm);
32void gatherv_data(const double* sendbuf, int sendcount, double* recvbuf, const int* recvcounts, const int* displs, MPI_Comm& comm);
33void gatherv_data(const std::complex<double>* sendbuf, int sendcount, std::complex<double>* recvbuf, const int* recvcounts, const int* displs, MPI_Comm& comm);
34void gatherv_data(const float* sendbuf, int sendcount, float* recvbuf, const int* recvcounts, const int* displs, MPI_Comm& comm);
35void gatherv_data(const std::complex<float>* sendbuf, int sendcount, std::complex<float>* recvbuf, const int* recvcounts, const int* displs, MPI_Comm& comm);
36
37#if defined(__NCCL_PARALLEL_DEVICE)
38void nccl_bcast_data(double* object, const int& n, MPI_Comm& comm, int root = 0);
39void nccl_bcast_data(std::complex<double>* object, const int& n, MPI_Comm& comm, int root = 0);
40void nccl_bcast_data(float* object, const int& n, MPI_Comm& comm, int root = 0);
41void nccl_bcast_data(std::complex<float>* object, const int& n, MPI_Comm& comm, int root = 0);
42void nccl_reduce_data(double* object, const int& n, MPI_Comm& comm);
43void nccl_reduce_data(std::complex<double>* object, const int& n, MPI_Comm& comm);
44void nccl_reduce_data(float* object, const int& n, MPI_Comm& comm);
45void nccl_reduce_data(std::complex<float>* object, const int& n, MPI_Comm& comm);
46void nccl_gatherv_data(const double* sendbuf, int sendcount, double* recvbuf, const int* recvcounts, const int* displs, MPI_Comm& comm);
47void nccl_gatherv_data(const std::complex<double>* sendbuf, int sendcount, std::complex<double>* recvbuf, const int* recvcounts, const int* displs, MPI_Comm& comm);
48void nccl_gatherv_data(const float* sendbuf, int sendcount, float* recvbuf, const int* recvcounts, const int* displs, MPI_Comm& comm);
49void nccl_gatherv_data(const std::complex<float>* sendbuf, int sendcount, std::complex<float>* recvbuf, const int* recvcounts, const int* displs, MPI_Comm& comm);
50#endif
51
52#ifndef __CUDA_MPI
53template<typename T, typename Device>
55{
56 bool alloc = false;
57 T* get_buffer(const T* object, const int& n, T* tmp_space = nullptr);
58 T* get(const T* object, const int& n, T* tmp_space = nullptr);
59 void del(T* object);
60 void sync_d2h(T* object_cpu, const T* object, const int& n);
61 void sync_h2d(T* object, const T* object_cpu, const int& n);
62};
63#endif
64
69template <typename T, typename Device>
70void send_dev(const T* object, int count, int dest, int tag, MPI_Comm& comm, T* tmp_space = nullptr)
71{
72#ifdef __CUDA_MPI
73 send_data(object, count, dest, tag, comm);
74#else
76 T* object_cpu = o.get(object, count, tmp_space);
77 send_data(object_cpu, count, dest, tag, comm);
78 o.del(object_cpu);
79#endif
80 return;
81}
82
88template <typename T, typename Device>
89void isend_dev(const T* object, int count, int dest, int tag, MPI_Comm& comm, MPI_Request* request, T* send_space)
90{
91#ifdef __CUDA_MPI
92 isend_data(object, count, dest, tag, comm, request);
93#else
95 T* object_cpu = o.get(object, count, send_space);
96 isend_data(object_cpu, count, dest, tag, comm, request);
97 o.del(object_cpu);
98#endif
99 return;
100}
101
106template <typename T, typename Device>
107void recv_dev(T* object, int count, int source, int tag, MPI_Comm& comm, MPI_Status* status, T* tmp_space = nullptr)
108{
109#ifdef __CUDA_MPI
110 recv_data(object, count, source, tag, comm, status);
111#else
113 T* object_cpu = o.get_buffer(object, count, tmp_space);
114 recv_data(object_cpu, count, source, tag, comm, status);
115 o.sync_h2d(object, object_cpu, count);
116 o.del(object_cpu);
117#endif
118 return;
119}
120
132template <typename T, typename Device>
133void bcast_dev(T* object, const int& n, const MPI_Comm& comm, int root = 0, T* tmp_space = nullptr)
134{
135#if defined(__NCCL_PARALLEL_DEVICE)
136 if (std::is_same<Device, base_device::DEVICE_GPU>::value)
137 {
138 nccl_bcast_data(object, n, const_cast<MPI_Comm&>(comm), root);
139 return;
140 }
141#endif
142#ifdef __CUDA_MPI
143 bcast_data(object, n, comm, root);
144#else
146 int rank = 0;
147 MPI_Comm_rank(comm, &rank);
148 T* object_cpu = rank == root ? o.get(object, n, tmp_space) : o.get_buffer(object, n, tmp_space);
149 bcast_data(object_cpu, n, comm, root);
150 if (rank != root)
151 {
152 o.sync_h2d(object, object_cpu, n);
153 }
154 o.del(object_cpu);
155#endif
156 return;
157}
158
159template <typename T, typename Device>
160void reduce_dev(T* object, const int& n, const MPI_Comm& comm, T* tmp_space = nullptr)
161{
162#if defined(__NCCL_PARALLEL_DEVICE)
163 if (std::is_same<Device, base_device::DEVICE_GPU>::value)
164 {
165 nccl_reduce_data(object, n, const_cast<MPI_Comm&>(comm));
166 return;
167 }
168#endif
169#ifdef __CUDA_MPI
170 reduce_data(object, n, comm);
171#else
173 T* object_cpu = o.get(object, n, tmp_space);
174 reduce_data(object_cpu, n, comm);
175 o.sync_h2d(object, object_cpu, n);
176 o.del(object_cpu);
177#endif
178 return;
179}
180
181template <typename T, typename Device>
182void gatherv_dev(const T* sendbuf,
183 int sendcount,
184 T* recvbuf,
185 const int* recvcounts,
186 const int* displs,
187 MPI_Comm& comm,
188 T* tmp_sspace = nullptr,
189 T* tmp_rspace = nullptr)
190{
191#if defined(__NCCL_PARALLEL_DEVICE)
192 if (std::is_same<Device, base_device::DEVICE_GPU>::value)
193 {
194 nccl_gatherv_data(sendbuf, sendcount, recvbuf, recvcounts, displs, comm);
195 return;
196 }
197#endif
198#ifdef __CUDA_MPI
199 gatherv_data(sendbuf, sendcount, recvbuf, recvcounts, displs, comm);
200#else
202 int size = 0;
203 MPI_Comm_size(comm, &size);
204 int gather_space = displs[size - 1] + recvcounts[size - 1];
205 T* sendbuf_cpu = o1.get(sendbuf, sendcount, tmp_sspace);
206 T* recvbuf_cpu = o2.get_buffer(recvbuf, gather_space, tmp_rspace);
207 gatherv_data(sendbuf_cpu, sendcount, recvbuf_cpu, recvcounts, displs, comm);
208 o2.sync_h2d(recvbuf, recvbuf_cpu, gather_space);
209 o1.del(sendbuf_cpu);
210 o2.del(recvbuf_cpu);
211#endif
212 return;
213}
214
215}
216
217
218#endif
219#endif
#define T
Definition exp.cpp:237
Definition parallel_common.cpp:10
void send_data(const double *buf, int count, int dest, int tag, MPI_Comm &comm)
Definition parallel_device.cpp:273
void reduce_data_to_root(std::complex< double > *object, const int n, const MPI_Comm &comm, const int root)
Definition parallel_device.cpp:325
void bcast_data(std::complex< double > *object, const int &n, const MPI_Comm &comm, int root)
Definition parallel_device.cpp:305
void send_dev(const T *object, int count, int dest, int tag, MPI_Comm &comm, T *tmp_space=nullptr)
send data in Device
Definition parallel_device.h:70
void isend_data(const double *buf, int count, int dest, int tag, MPI_Comm &comm, MPI_Request *request)
Definition parallel_device.cpp:257
void isend_dev(const T *object, int count, int dest, int tag, MPI_Comm &comm, MPI_Request *request, T *send_space)
isend data in Device
Definition parallel_device.h:89
void recv_dev(T *object, int count, int source, int tag, MPI_Comm &comm, MPI_Status *status, T *tmp_space=nullptr)
recv data in Device
Definition parallel_device.h:107
void gatherv_dev(const T *sendbuf, int sendcount, T *recvbuf, const int *recvcounts, const int *displs, MPI_Comm &comm, T *tmp_sspace=nullptr, T *tmp_rspace=nullptr)
Definition parallel_device.h:182
void bcast_dev(T *object, const int &n, const MPI_Comm &comm, int root=0, T *tmp_space=nullptr)
broadcast data in Device
Definition parallel_device.h:133
void gatherv_data(const double *sendbuf, int sendcount, double *recvbuf, const int *recvcounts, const int *displs, MPI_Comm &comm)
Definition parallel_device.cpp:350
void recv_data(double *buf, int count, int source, int tag, MPI_Comm &comm, MPI_Status *status)
Definition parallel_device.cpp:289
void reduce_data(std::complex< double > *object, const int &n, const MPI_Comm &comm)
Definition parallel_device.cpp:321
void reduce_dev(T *object, const int &n, const MPI_Comm &comm, T *tmp_space=nullptr)
Definition parallel_device.h:160
Definition parallel_device.h:55
bool alloc
Definition parallel_device.h:56
T * get_buffer(const T *object, const int &n, T *tmp_space=nullptr)
void sync_h2d(T *object, const T *object_cpu, const int &n)
T * get(const T *object, const int &n, T *tmp_space=nullptr)
void sync_d2h(T *object_cpu, const T *object, const int &n)