File: lal_gayberne.cpp

package info (click to toggle)
lammps 20220106.git7586adbb6a%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 348,064 kB
  • sloc: cpp: 831,421; python: 24,896; xml: 14,949; f90: 10,845; ansic: 7,967; sh: 4,226; perl: 4,064; fortran: 2,424; makefile: 1,501; objc: 238; lisp: 163; csh: 16; awk: 14; tcl: 6
file content (308 lines) | stat: -rw-r--r-- 12,073 bytes parent folder | download
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
/***************************************************************************
                                gayberne.cpp
                             -------------------
                            W. Michael Brown (ORNL)

  Host code for Gay-Berne potential acceleration

 __________________________________________________________________________
    This file is part of the LAMMPS Accelerator Library (LAMMPS_AL)
 __________________________________________________________________________

    begin                :
    email                : brownw@ornl.gov
 ***************************************************************************/

#if defined(USE_OPENCL)
#include "gayberne_cl.h"
#include "gayberne_lj_cl.h"
#elif defined(USE_CUDART)
const char *gayberne=0;
const char *gayberne_lj=0;
#else
#include "gayberne_cubin.h"
#include "gayberne_lj_cubin.h"
#endif

#include "lal_gayberne.h"
#include <cassert>
namespace LAMMPS_AL {

#define GayBerneT GayBerne<numtyp, acctyp>
extern Device<PRECISION,ACC_PRECISION> device;

template <class numtyp, class acctyp>
GayBerneT::GayBerne() : BaseEllipsoid<numtyp,acctyp>(),
                                  _allocated(false) {
}

template <class numtyp, class acctyp>
GayBerneT::~GayBerne() {
  clear();
}

template <class numtyp, class acctyp>
int GayBerneT::bytes_per_atom(const int max_nbors) const {
  return this->bytes_per_atom(max_nbors);
}

template <class numtyp, class acctyp>
int GayBerneT::init(const int ntypes, const double gamma,
                         const double upsilon, const double mu,
                         double **host_shape, double **host_well,
                         double **host_cutsq, double **host_sigma,
                         double **host_epsilon, double *host_lshape,
                         int **h_form, double **host_lj1, double **host_lj2,
                         double **host_lj3, double **host_lj4,
                         double **host_offset, const double *host_special_lj,
                         const int nlocal, const int nall, const int max_nbors,
                         const int maxspecial, const double cell_size,
                         const double gpu_split, FILE *_screen) {
  int success;
  success=this->init_base(nlocal,nall,max_nbors,maxspecial,cell_size,gpu_split,
                          _screen,ntypes,h_form,gayberne,gayberne_lj,
                          "k_gayberne");
  if (success!=0)
    return success;

  // If atom type constants fit in shared memory use fast kernel
  int lj_types=ntypes;
  _shared_types=false;
  int max_shared_types=this->device->max_shared_types();
  if (lj_types<=max_shared_types && this->block_size()>=max_shared_types) {
    lj_types=max_shared_types;
    _shared_types=true;
  }
  _lj_types=lj_types;

  // Allocate a host write buffer for copying type data
  UCL_H_Vec<numtyp> host_write(lj_types*lj_types*32,*(this->ucl_device),
                               UCL_WRITE_ONLY);

  for (int i=0; i<lj_types*lj_types; i++)
    host_write[i]=0.0;

  sigma_epsilon.alloc(lj_types*lj_types,*(this->ucl_device),UCL_READ_ONLY);
  this->atom->type_pack2(ntypes,lj_types,sigma_epsilon,host_write,
                         host_sigma,host_epsilon);

  this->cut_form.alloc(lj_types*lj_types,*(this->ucl_device),UCL_READ_ONLY);
  this->atom->type_pack2(ntypes,lj_types,this->cut_form,host_write,
                         host_cutsq,h_form);

  lj1.alloc(lj_types*lj_types,*(this->ucl_device),UCL_READ_ONLY);
  this->atom->type_pack4(ntypes,lj_types,lj1,host_write,host_lj1,host_lj2,
                         host_cutsq,h_form);

  lj3.alloc(lj_types*lj_types,*(this->ucl_device),UCL_READ_ONLY);
  this->atom->type_pack4(ntypes,lj_types,lj3,host_write,host_lj3,host_lj4,
                         host_offset);

  dev_error.alloc(1,*(this->ucl_device),UCL_WRITE_ONLY);
  dev_error.zero();

  // Allocate, cast and asynchronous memcpy of constant data
  // Copy data for bonded interactions
  gamma_upsilon_mu.alloc(7,*(this->ucl_device),UCL_READ_ONLY);
  host_write[0]=static_cast<numtyp>(gamma);
  host_write[1]=static_cast<numtyp>(upsilon);
  host_write[2]=static_cast<numtyp>(mu);
  host_write[3]=static_cast<numtyp>(host_special_lj[0]);
  host_write[4]=static_cast<numtyp>(host_special_lj[1]);
  host_write[5]=static_cast<numtyp>(host_special_lj[2]);
  host_write[6]=static_cast<numtyp>(host_special_lj[3]);
  ucl_copy(gamma_upsilon_mu,host_write,7,false);

  lshape.alloc(ntypes,*(this->ucl_device),UCL_READ_ONLY);
  UCL_H_Vec<double> d_view;
  d_view.view(host_lshape,lshape.numel(),*(this->ucl_device));
  ucl_copy(lshape,d_view,false);

  // Copy shape, well, sigma, epsilon, and cutsq onto GPU
  // - cast if necessary
  shape.alloc(ntypes,*(this->ucl_device),UCL_READ_ONLY);
  for (int i=0; i<ntypes; i++) {
    host_write[i*4]=host_shape[i][0];
    host_write[i*4+1]=host_shape[i][1];
    host_write[i*4+2]=host_shape[i][2];
  }
  UCL_H_Vec<numtyp4> view4;
  view4.view(host_write,shape.numel());
  ucl_copy(shape,view4,false);

  well.alloc(ntypes,*(this->ucl_device),UCL_READ_ONLY);
  for (int i=0; i<ntypes; i++) {
    host_write[i*4]=host_well[i][0];
    host_write[i*4+1]=host_well[i][1];
    host_write[i*4+2]=host_well[i][2];
  }
  view4.view(host_write,well.numel());
  ucl_copy(well,view4,false);

  _allocated=true;
  this->_max_bytes=sigma_epsilon.row_bytes()+this->cut_form.row_bytes()+
                   lj1.row_bytes()+lj3.row_bytes()+gamma_upsilon_mu.row_bytes()+
                   lshape.row_bytes()+shape.row_bytes()+well.row_bytes();

  return 0;
}

template <class numtyp, class acctyp>
void GayBerneT::clear() {
  if (!_allocated)
    return;

  UCL_H_Vec<int> err_flag(1,*(this->ucl_device));
  ucl_copy(err_flag,dev_error,false);
  if (err_flag[0] == 2)
    std::cerr << "BAD MATRIX INVERSION IN FORCE COMPUTATION.\n";
  err_flag.clear();

  _allocated=false;

  dev_error.clear();
  lj1.clear();
  lj3.clear();
  sigma_epsilon.clear();
  this->cut_form.clear();

  shape.clear();
  well.clear();
  lshape.clear();
  gamma_upsilon_mu.clear();

  this->clear_base();
}

template <class numtyp, class acctyp>
double GayBerneT::host_memory_usage() const {
  return this->host_memory_usage_base()+sizeof(GayBerneT)+
         4*sizeof(numtyp);
}

// ---------------------------------------------------------------------------
// Calculate energies, forces, and torques
// ---------------------------------------------------------------------------
template <class numtyp, class acctyp>
int GayBerneT::loop(const int eflag, const int vflag) {
  const int BX=this->block_size();
  int GX=0, NGX;
  int stride=this->nbor->nbor_pitch();
  int ainum=this->ans->inum();

  if (this->_multiple_forms) {
    this->time_nbor1.start();
    if (this->_last_ellipse>0) {
      // ------------ ELLIPSE_ELLIPSE and ELLIPSE_SPHERE ---------------
      GX=static_cast<int>(ceil(static_cast<double>(this->_last_ellipse)/
                               (BX/this->_threads_per_atom)));
      NGX=static_cast<int>(ceil(static_cast<double>(this->_last_ellipse)/BX));
      this->pack_nbors(NGX,BX, 0, this->_last_ellipse,ELLIPSE_SPHERE,
                                         ELLIPSE_ELLIPSE,_shared_types,_lj_types);
      this->time_nbor1.stop();

      this->time_ellipsoid.start();
      this->k_elps_sel->set_size(GX,BX);
      this->k_elps_sel->run(&this->atom->x, &this->atom->quat,
                            &this->shape, &this->well, &this->gamma_upsilon_mu,
                            &this->sigma_epsilon, &this->_lj_types,
                            &this->lshape, &this->nbor->dev_nbor, &stride,
                            &this->ans->force, &ainum, &this->ans->engv,
                            &this->dev_error, &eflag, &vflag,
                            &this->_last_ellipse, &this->_threads_per_atom);
      this->time_ellipsoid.stop();

      if (this->_last_ellipse==this->ans->inum()) {
        this->time_nbor2.start();
        this->time_nbor2.stop();
        this->time_ellipsoid2.start();
        this->time_ellipsoid2.stop();
        this->time_lj.start();
        this->time_lj.stop();
        return ainum;
      }

      // ------------ SPHERE_ELLIPSE ---------------

      this->time_nbor2.start();
      GX=static_cast<int>(ceil(static_cast<double>(this->ans->inum()-
                               this->_last_ellipse)/
                               (BX/this->_threads_per_atom)));
      NGX=static_cast<int>(ceil(static_cast<double>(this->ans->inum()-
                                this->_last_ellipse)/BX));
      this->pack_nbors(NGX,BX,this->_last_ellipse,this->ans->inum(),
                                         SPHERE_ELLIPSE,SPHERE_ELLIPSE,_shared_types,_lj_types);
      this->time_nbor2.stop();

      this->time_ellipsoid2.start();
      this->k_sphere_elps_sel->set_size(GX,BX);
      this->k_sphere_elps_sel->run(&this->atom->x, &this->atom->quat,
                                   &this->shape,  &this->well,
                                   &this->gamma_upsilon_mu,
                                   &this->sigma_epsilon, &this->_lj_types,
                                   &this->lshape,  &this->nbor->dev_nbor,
                                   &stride, &this->ans->force,
                                   &this->ans->engv, &this->dev_error,
                                   &eflag, &vflag, &this->_last_ellipse,
                                   &ainum, &this->_threads_per_atom);
      this->time_ellipsoid2.stop();
   } else {
      GX=static_cast<int>(ceil(static_cast<double>(this->ans->inum()-
                               this->_last_ellipse)/
                               (BX/this->_threads_per_atom)));
      this->ans->force.zero();
      this->ans->engv.zero();
      this->time_nbor1.stop();
      this->time_ellipsoid.start();
      this->time_ellipsoid.stop();
      this->time_nbor2.start();
      this->time_nbor2.stop();
      this->time_ellipsoid2.start();
      this->time_ellipsoid2.stop();
    }

    // ------------         LJ      ---------------
    this->time_lj.start();
    if (this->_last_ellipse<this->ans->inum()) {
      if (this->_shared_types) {
        this->k_lj_sel->set_size(GX,BX);
        this->k_lj_sel->run(&this->atom->x, &this->lj1, &this->lj3,
                            &this->gamma_upsilon_mu, &stride,
                            &this->nbor->dev_packed, &this->ans->force,
                            &this->ans->engv, &this->dev_error, &eflag,
                            &vflag, &this->_last_ellipse, &ainum,
                            &this->_threads_per_atom);
      } else {
        this->k_lj.set_size(GX,BX);
        this->k_lj.run(&this->atom->x, &this->lj1, &this->lj3,
                       &this->_lj_types, &this->gamma_upsilon_mu, &stride,
                       &this->nbor->dev_packed, &this->ans->force,
                       &this->ans->engv, &this->dev_error, &eflag,
                       &vflag, &this->_last_ellipse, &ainum,
                       &this->_threads_per_atom);
      }
    }
    this->time_lj.stop();
  } else {
    GX=static_cast<int>(ceil(static_cast<double>(this->ans->inum())/
                             (BX/this->_threads_per_atom)));
    NGX=static_cast<int>(ceil(static_cast<double>(this->ans->inum())/BX));
    this->time_nbor1.start();
    this->pack_nbors(NGX, BX, 0, this->ans->inum(),SPHERE_SPHERE,
                                 ELLIPSE_ELLIPSE,_shared_types,_lj_types);
    this->time_nbor1.stop();
    this->time_ellipsoid.start();
    this->k_elps_sel->set_size(GX,BX);
    this->k_elps_sel->run(&this->atom->x,  &this->atom->quat,
                          &this->shape, &this->well, &this->gamma_upsilon_mu,
                          &this->sigma_epsilon, &this->_lj_types, &this->lshape,
                          &this->nbor->dev_nbor, &stride, &this->ans->force,
                          &ainum,  &this->ans->engv, &this->dev_error,
                          &eflag, &vflag, &ainum, &this->_threads_per_atom);
    this->time_ellipsoid.stop();
  }
  return ainum;
}

template class GayBerne<PRECISION,ACC_PRECISION>;
}