-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathlibxc_device.hip
More file actions
344 lines (260 loc) · 12.8 KB
/
Copy pathlibxc_device.hip
File metadata and controls
344 lines (260 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**
* ExchCXX Copyright (c) 2020-2022, The Regents of the University of California,
* through Lawrence Berkeley National Laboratory (subject to receipt of
* any required approvals from the U.S. Dept. of Energy). All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* (1) Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* (2) Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* (3) Neither the name of the University of California, Lawrence Berkeley
* National Laboratory, U.S. Dept. of Energy nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* You are under no obligation whatsoever to provide any bug fixes, patches,
* or upgrades to the features, functionality or performance of the source
* code ("Enhancements") to anyone; however, if you choose to make your
* Enhancements available either publicly, or directly to Lawrence Berkeley
* National Laboratory, without imposing a separate written license agreement
* for such Enhancements, then you hereby grant the following license: a
* non-exclusive, royalty-free perpetual license to install, use, modify,
* prepare derivative works, incorporate into other computer software,
* distribute, and sublicense such enhancements or derivative works thereof,
* in binary and source code form.
*/
#include "libxc_common.hpp"
#include <exchcxx/impl/builtin/util.hpp>
#include <exchcxx/util/unused.hpp>
//#include <functionals.cuh>
void throw_if_fail( hipError_t stat, std::string msg ) {
if( stat != hipSuccess ) throw std::runtime_error( msg );
}
void recv_from_device( void* dest, const void* src, const size_t len ) {
auto stat = hipMemcpy( dest, src, len, hipMemcpyDeviceToHost );
throw_if_fail( stat, "recv failed" );
}
void recv_from_device( void* dest, const void* src, const size_t len,
hipStream_t& stream ) {
auto stat = hipMemcpyAsync( dest, src, len, hipMemcpyDeviceToHost, stream );
throw_if_fail( stat, "recv failed" );
}
void send_to_device( void* dest, const void* src, const size_t len ) {
auto stat = hipMemcpy( dest, src, len, hipMemcpyHostToDevice);
throw_if_fail( stat, "send failed" );
}
void send_to_device( void* dest, const void* src, const size_t len,
hipStream_t& stream ) {
auto stat = hipMemcpyAsync( dest, src, len, hipMemcpyHostToDevice, stream);
throw_if_fail( stat, "send failed" );
}
void stream_sync( hipStream_t& stream ) {
auto stat = hipStreamSynchronize( stream );
throw_if_fail( stat, "sync failed" );
}
namespace ExchCXX {
namespace detail {
// LDA interfaces
LDA_EXC_GENERATOR_DEVICE( LibxcKernelImpl::eval_exc_device_ ) const {
throw_if_uninitialized();
EXCHCXX_BOOL_CHECK("KERNEL IS NOT LDA", is_lda() );
size_t sz_rho = this->rho_buffer_len(N);
size_t sz_exc = this->exc_buffer_len(N);
size_t len_rho = sz_rho*sizeof(double);
size_t len_eps = sz_exc*sizeof(double);
std::vector<double> rho_host( sz_rho ), eps_host( sz_exc );
recv_from_device( rho_host.data(), rho, len_rho, stream );
stream_sync( stream );
xc_lda_exc( &kernel_, N, rho_host.data(), eps_host.data() );
send_to_device( eps, eps_host.data(), len_eps, stream );
stream_sync( stream ); // Lifetime of host vectors
}
LDA_EXC_VXC_GENERATOR_DEVICE( LibxcKernelImpl::eval_exc_vxc_device_ ) const {
throw_if_uninitialized();
EXCHCXX_BOOL_CHECK("KERNEL IS NOT LDA", is_lda() );
size_t sz_rho = this->rho_buffer_len(N);
size_t sz_exc = this->exc_buffer_len(N);
size_t sz_vxc = this->vrho_buffer_len(N);
size_t len_rho = sz_rho*sizeof(double);
size_t len_eps = sz_exc*sizeof(double);
size_t len_vxc = sz_vxc*sizeof(double);
std::vector<double> rho_host( sz_rho ), eps_host( sz_exc ), vxc_host( sz_vxc );
recv_from_device( rho_host.data(), rho, len_rho, stream );
stream_sync( stream );
xc_lda_exc_vxc( &kernel_, N, rho_host.data(), eps_host.data(), vxc_host.data() );
send_to_device( eps, eps_host.data(), len_eps, stream );
send_to_device( vxc, vxc_host.data(), len_vxc, stream );
stream_sync( stream ); // Lifetime of host vectors
}
// TODO: LDA kxc interfaces
// GGA interface
GGA_EXC_GENERATOR_DEVICE( LibxcKernelImpl::eval_exc_device_ ) const {
throw_if_uninitialized();
EXCHCXX_BOOL_CHECK("KERNEL IS NOT GGA", is_gga() );
size_t sz_rho = this->rho_buffer_len(N);
size_t sz_sigma = this->sigma_buffer_len(N);
size_t sz_eps = this->exc_buffer_len(N);
size_t len_rho = sz_rho *sizeof(double);
size_t len_eps = sz_eps *sizeof(double);
size_t len_sigma = sz_sigma *sizeof(double);
std::vector<double> rho_host( sz_rho ), eps_host( sz_eps ), sigma_host( sz_sigma );
recv_from_device( rho_host.data(), rho, len_rho , stream );
recv_from_device( sigma_host.data(), sigma, len_sigma, stream );
stream_sync( stream );
xc_gga_exc( &kernel_, N, rho_host.data(), sigma_host.data(), eps_host.data() );
send_to_device( eps, eps_host.data(), len_eps, stream );
stream_sync( stream ); // Lifetime of host vectors
}
GGA_EXC_VXC_GENERATOR_DEVICE( LibxcKernelImpl::eval_exc_vxc_device_ ) const {
throw_if_uninitialized();
EXCHCXX_BOOL_CHECK("KERNEL IS NOT GGA", is_gga() );
size_t sz_rho = this->rho_buffer_len(N);
size_t sz_sigma = this->sigma_buffer_len(N);
size_t sz_eps = this->exc_buffer_len(N);
size_t sz_vrho = this->vrho_buffer_len(N);
size_t sz_vsigma = this->vsigma_buffer_len(N);
size_t len_rho = sz_rho *sizeof(double);
size_t len_sigma = sz_sigma *sizeof(double);
size_t len_vrho = sz_vrho *sizeof(double);
size_t len_vsigma = sz_vsigma*sizeof(double);
size_t len_eps = sz_eps *sizeof(double);
std::vector<double> rho_host( sz_rho ), eps_host( sz_eps ),
sigma_host( sz_sigma ), vrho_host( sz_vrho ), vsigma_host( sz_vsigma );
recv_from_device( rho_host.data(), rho, len_rho , stream );
recv_from_device( sigma_host.data(), sigma, len_sigma, stream );
stream_sync( stream );
xc_gga_exc_vxc( &kernel_, N, rho_host.data(), sigma_host.data(), eps_host.data(),
vrho_host.data(), vsigma_host.data() );
send_to_device( eps, eps_host.data(), len_eps , stream);
send_to_device( vrho, vrho_host.data(), len_vrho , stream);
send_to_device( vsigma, vsigma_host.data(), len_vsigma, stream);
stream_sync( stream ); // Lifetime of host vectors
}
// TODO: GGA kxc interfaces
// mGGA interface
MGGA_EXC_GENERATOR_DEVICE( LibxcKernelImpl::eval_exc_device_ ) const {
throw_if_uninitialized();
EXCHCXX_BOOL_CHECK("KERNEL IS NOT MGGA", is_mgga() );
size_t sz_rho = this->rho_buffer_len(N) ;
size_t sz_sigma = this->sigma_buffer_len(N) ;
size_t sz_lapl = this->lapl_buffer_len(N) ;
size_t sz_tau = this->tau_buffer_len(N) ;
size_t sz_eps = this->exc_buffer_len(N) ;
size_t len_rho = sz_rho *sizeof(double);
size_t len_sigma = sz_sigma*sizeof(double);
size_t len_lapl = sz_lapl *sizeof(double);
size_t len_tau = sz_tau *sizeof(double);
size_t len_eps = sz_eps *sizeof(double);
std::vector<double> rho_host( sz_rho ), eps_host( sz_eps ),
sigma_host( sz_sigma ), lapl_host( sz_lapl ),
tau_host( sz_tau );
recv_from_device( rho_host.data(), rho, len_rho , stream );
recv_from_device( sigma_host.data(), sigma, len_sigma, stream );
recv_from_device( lapl_host.data(), lapl, len_lapl , stream );
recv_from_device( tau_host.data(), tau, len_tau , stream );
stream_sync( stream );
xc_mgga_exc( &kernel_, N, rho_host.data(), sigma_host.data(), lapl_host.data(),
tau_host.data(), eps_host.data() );
send_to_device( eps, eps_host.data(), len_eps, stream );
stream_sync( stream ); // Lifetime of host vectors
}
MGGA_EXC_VXC_GENERATOR_DEVICE( LibxcKernelImpl::eval_exc_vxc_device_ ) const {
throw_if_uninitialized();
EXCHCXX_BOOL_CHECK("KERNEL IS NOT MGGA", is_mgga() );
size_t sz_rho = this->rho_buffer_len(N) ;
size_t sz_sigma = this->sigma_buffer_len(N) ;
size_t sz_lapl = this->lapl_buffer_len(N) ;
size_t sz_tau = this->tau_buffer_len(N) ;
size_t sz_eps = this->exc_buffer_len(N) ;
size_t sz_vrho = this->vrho_buffer_len(N) ;
size_t sz_vsigma = this->vsigma_buffer_len(N);
size_t sz_vlapl = this->vlapl_buffer_len(N) ;
size_t sz_vtau = this->vtau_buffer_len(N) ;
size_t len_rho = sz_rho *sizeof(double);
size_t len_sigma = sz_sigma *sizeof(double);
size_t len_lapl = sz_lapl *sizeof(double);
size_t len_tau = sz_tau *sizeof(double);
size_t len_eps = sz_eps *sizeof(double);
size_t len_vrho = sz_vrho *sizeof(double);
size_t len_vsigma = sz_vsigma*sizeof(double);
size_t len_vlapl = sz_vlapl *sizeof(double);
size_t len_vtau = sz_vtau *sizeof(double);
std::vector<double> rho_host( sz_rho ), eps_host( sz_eps ), sigma_host( sz_sigma ),
lapl_host( sz_lapl ), tau_host( sz_tau );
std::vector<double> vrho_host( sz_vrho ), vsigma_host( sz_vsigma ),
vlapl_host( sz_vlapl ), vtau_host( sz_vtau );
recv_from_device( rho_host.data(), rho, len_rho , stream );
recv_from_device( sigma_host.data(), sigma, len_sigma, stream );
recv_from_device( lapl_host.data(), lapl, len_lapl , stream );
recv_from_device( tau_host.data(), tau, len_tau , stream );
stream_sync( stream );
xc_mgga_exc_vxc( &kernel_, N, rho_host.data(), sigma_host.data(),
lapl_host.data(), tau_host.data(), eps_host.data(), vrho_host.data(),
vsigma_host.data(), vlapl_host.data(), vtau_host.data() );
send_to_device( eps, eps_host.data(), len_eps , stream );
send_to_device( vrho, vrho_host.data(), len_vrho , stream );
send_to_device( vsigma, vsigma_host.data(), len_vsigma, stream );
send_to_device( vlapl, vlapl_host.data(), len_vlapl , stream );
send_to_device( vtau, vtau_host.data(), len_vtau , stream );
stream_sync( stream ); // Lifetime of host vectors
}
UNUSED_DEVICE_INC_INTERFACE_GENERATOR( LDA, EXC,
LibxcKernelImpl::eval_exc_inc_device_, const )
UNUSED_DEVICE_INC_INTERFACE_GENERATOR( LDA, EXC_VXC,
LibxcKernelImpl::eval_exc_vxc_inc_device_, const )
UNUSED_DEVICE_INC_INTERFACE_GENERATOR( GGA, EXC,
LibxcKernelImpl::eval_exc_inc_device_, const )
UNUSED_DEVICE_INC_INTERFACE_GENERATOR( GGA, EXC_VXC,
LibxcKernelImpl::eval_exc_vxc_inc_device_, const )
UNUSED_DEVICE_INC_INTERFACE_GENERATOR( MGGA, EXC,
LibxcKernelImpl::eval_exc_inc_device_, const )
UNUSED_DEVICE_INC_INTERFACE_GENERATOR( MGGA, EXC_VXC,
LibxcKernelImpl::eval_exc_vxc_inc_device_, const )
// ExchCXX's HIP backend does not implement Fxc/vxc_fxc device evaluation
// for Libxc-backed kernels (unlike CUDA and SYCL); disable rather than
// leave the LibxcKernelImpl vtable unresolved at link time.
UNUSED_DEVICE_INTERFACE_GENERATOR( LDA, FXC,
LibxcKernelImpl::eval_fxc_device_, const )
UNUSED_DEVICE_INTERFACE_GENERATOR( LDA, VXC_FXC,
LibxcKernelImpl::eval_vxc_fxc_device_, const )
UNUSED_DEVICE_INTERFACE_GENERATOR( GGA, FXC,
LibxcKernelImpl::eval_fxc_device_, const )
UNUSED_DEVICE_INTERFACE_GENERATOR( GGA, VXC_FXC,
LibxcKernelImpl::eval_vxc_fxc_device_, const )
UNUSED_DEVICE_INTERFACE_GENERATOR( MGGA, FXC,
LibxcKernelImpl::eval_fxc_device_, const )
UNUSED_DEVICE_INTERFACE_GENERATOR( MGGA, VXC_FXC,
LibxcKernelImpl::eval_vxc_fxc_device_, const )
UNUSED_DEVICE_INC_INTERFACE_GENERATOR( LDA, FXC,
LibxcKernelImpl::eval_fxc_inc_device_, const )
UNUSED_DEVICE_INC_INTERFACE_GENERATOR( GGA, FXC,
LibxcKernelImpl::eval_fxc_inc_device_, const )
UNUSED_DEVICE_INC_INTERFACE_GENERATOR( MGGA, FXC,
LibxcKernelImpl::eval_fxc_inc_device_, const )
UNUSED_DEVICE_INC_INTERFACE_GENERATOR( LDA, VXC_FXC,
LibxcKernelImpl::eval_vxc_fxc_inc_device_, const )
UNUSED_DEVICE_INC_INTERFACE_GENERATOR( GGA, VXC_FXC,
LibxcKernelImpl::eval_vxc_fxc_inc_device_, const )
UNUSED_DEVICE_INC_INTERFACE_GENERATOR( MGGA, VXC_FXC,
LibxcKernelImpl::eval_vxc_fxc_inc_device_, const )
}
}