File: lal_sw.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 (217 lines) | stat: -rw-r--r-- 7,750 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
/***************************************************************************
                                   sw.cpp
                             -------------------
                            W. Michael Brown (ORNL)

  Class for acceleration of the sw pair style.

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

    begin                : Tue March 26, 2013
    email                : brownw@ornl.gov
 ***************************************************************************/

#if defined(USE_OPENCL)
#include "sw_cl.h"
#elif defined(USE_CUDART)
const char *sw=0;
#else
#include "sw_cubin.h"
#endif

#include "lal_sw.h"
#include <cassert>
namespace LAMMPS_AL {
#define SWT SW<numtyp, acctyp>

extern Device<PRECISION,ACC_PRECISION> device;

template <class numtyp, class acctyp>
SWT::SW() : BaseThree<numtyp,acctyp>(), _allocated(false) {
}

template <class numtyp, class acctyp>
SWT::~SW() {
  clear();
}

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

template <class numtyp, class acctyp>
int SWT::init(const int ntypes, const int nlocal, const int nall,
              const int max_nbors, const double cell_size,
              const double gpu_split, FILE *_screen, double **ncutsq,
              double **ncut, double **sigma, double **powerp, double **powerq,
              double **sigma_gamma, double **c1, double **c2, double **c3,
              double **c4, double **c5, double **c6, double ***lambda_epsilon,
              double ***costheta, const int *map, int ***e2param) {
  _lj_types=ntypes;

  int oldparam=-1;
  int onetype=-1;
  int onetype3=0;
  int spq=1;
  int mtypes=0;
  #ifdef USE_OPENCL
  for (int ii=1; ii<ntypes; ii++) {
    int i=map[ii];
    if (i<0) continue;
    for (int jj=1; jj<ntypes; jj++) {
      int j=map[jj];
      if (j<0) continue;
      if (powerp[ii][jj] != 4.0 || powerq[ii][jj] != 0.0)
        spq=0;
      for (int kk=1; kk<ntypes; kk++) {
        int k=map[kk];
        if (k<0) continue;
        int param=e2param[i][j][k];
        if (oldparam!=param) {
          oldparam=param;
          onetype=ntypes*ii+jj;
          onetype3=ntypes*ntypes*ii+ntypes*jj+kk;
          mtypes++;
        }
      }
    }
  }
  if (mtypes>1) onetype=-1;
  #endif

  int success;
  success=this->init_three(nlocal,nall,max_nbors,0,cell_size,gpu_split,
                           _screen,sw,"k_sw","k_sw_three_center",
                           "k_sw_three_end","k_sw_short_nbor",onetype,
                           onetype3,spq);
  if (success!=0)
    return success;

  UCL_H_Vec<numtyp> host_write(ntypes*ntypes*ntypes*4,*(this->ucl_device),
                               UCL_WRITE_ONLY);
  host_write.zero();

  for (int i=1; i<ntypes; i++)
    for (int j=1; j<ntypes; j++) {
      double ccutsq = ncut[i][j]*ncut[i][j];
      if (ccutsq > 0.0 && ncutsq[i][j]>=ccutsq)
        ncutsq[i][j]=ccutsq*0.98;
  }

  // pack coefficients into arrays
  cutsq.alloc(ntypes*ntypes,*(this->ucl_device),UCL_READ_ONLY);
  this->atom->type_pack1(ntypes,ntypes,cutsq,host_write,ncutsq);
  sw_pre.alloc(ntypes*ntypes,*(this->ucl_device),UCL_READ_ONLY);
  this->atom->type_pack4(ntypes,ntypes,sw_pre,host_write,ncut,sigma,
                         powerp,powerq);
  c_14.alloc(ntypes*ntypes,*(this->ucl_device),UCL_READ_ONLY);
  this->atom->type_pack4(ntypes,ntypes,c_14,host_write,c1,c2,c3,c4);
  c_56.alloc(ntypes*ntypes,*(this->ucl_device),UCL_READ_ONLY);
  this->atom->type_pack2(ntypes,ntypes,c_56,host_write,c5,c6);
  cut_sigma_gamma.alloc(ntypes*ntypes,*(this->ucl_device),UCL_READ_ONLY);
  this->atom->type_pack2(ntypes,ntypes,cut_sigma_gamma,host_write,ncut,
                         sigma_gamma);
  sw_pre3.alloc(ntypes*ntypes*ntypes,*(this->ucl_device),UCL_READ_ONLY);
  this->atom->type_pack2(ntypes,sw_pre3,host_write,lambda_epsilon,costheta);

  _allocated=true;
  this->_max_bytes=cutsq.row_bytes()+sw_pre.row_bytes()+c_14.row_bytes()+
    c_56.row_bytes()+cut_sigma_gamma.row_bytes()+sw_pre3.row_bytes();
  return 0;
}

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

  cutsq.clear();
  sw_pre.clear();
  c_14.clear();
  c_56.clear();
  cut_sigma_gamma.clear();
  sw_pre3.clear();
  this->clear_atomic();
}

template <class numtyp, class acctyp>
double SWT::host_memory_usage() const {
  return this->host_memory_usage_atomic()+sizeof(SW<numtyp,acctyp>);
}

#define KTHREADS this->_threads_per_atom
#define JTHREADS this->_threads_per_atom
// ---------------------------------------------------------------------------
// Calculate energies, forces, and torques
// ---------------------------------------------------------------------------
template <class numtyp, class acctyp>
int SWT::loop(const int eflag, const int vflag, const int evatom,
              bool &success) {
  const int nbor_pitch=this->nbor->nbor_pitch();

  // build the short neighbor list
  int ainum=this->_ainum;
  this->time_pair.start();

  int BX=this->block_pair();
  int GX=static_cast<int>(ceil(static_cast<double>(ainum)/BX));
  this->k_short_nbor.set_size(GX,BX);
  this->k_short_nbor.run(&this->atom->x, &cutsq, &_lj_types,
                         &this->nbor->dev_nbor, &this->nbor->dev_packed,
                         &ainum, &nbor_pitch, &this->_threads_per_atom);

  // this->_nbor_data == nbor->dev_packed for gpu_nbor == 0 and tpa > 1
  // this->_nbor_data == nbor->dev_nbor for gpu_nbor == 1 or tpa == 1
  ainum=this->ans->inum();
  BX=this->block_size();
  GX=static_cast<int>(ceil(static_cast<double>(this->ans->inum())/
                           (BX/(KTHREADS*JTHREADS))));
  this->k_3center_sel->set_size(GX,BX);
  this->k_3center_sel->run(&this->atom->x, &cut_sigma_gamma, &sw_pre3,
                           &_lj_types, &this->nbor->dev_nbor,
                           &this->ans->force, &this->ans->engv, &eflag,
                           &vflag, &ainum, &nbor_pitch,
                           &this->_threads_per_atom, &evatom);

  Answer<numtyp,acctyp> *end_ans;
  #ifdef THREE_CONCURRENT
  end_ans=this->ans2;
  #else
  end_ans=this->ans;
  #endif
  if (evatom!=0) {
    this->k_three_end_vatom.set_size(GX,BX);
    this->k_three_end_vatom.run(&this->atom->x, &cut_sigma_gamma,
                                &sw_pre3, &_lj_types, &this->nbor->dev_nbor,
                                &this->nbor->three_ilist, &end_ans->force,
                                &end_ans->engv, &eflag, &vflag, &ainum,
                                &nbor_pitch,&this->_threads_per_atom,
                                &this->_gpu_nbor);
  } else {
    this->k_3end_sel->set_size(GX,BX);
    this->k_3end_sel->run(&this->atom->x, &cut_sigma_gamma, &sw_pre3,
                          &_lj_types, &this->nbor->dev_nbor,
                          &this->nbor->three_ilist, &end_ans->force,
                          &end_ans->engv, &eflag, &vflag, &ainum, &nbor_pitch,
                          &this->_threads_per_atom, &this->_gpu_nbor);
  }

  BX=this->block_pair();
  int GXT=static_cast<int>(ceil(static_cast<double>(this->ans->inum())/
                               (BX/this->_threads_per_atom)));
  this->k_sel->set_size(GXT,BX);
  this->k_sel->run(&this->atom->x, &sw_pre, &c_14, &c_56,
                   &_lj_types, &this->nbor->dev_nbor,
                   &this->ans->force, &this->ans->engv, &eflag, &vflag,
                   &ainum, &nbor_pitch, &this->_threads_per_atom, &GX);

  this->time_pair.stop();
  return GX;
}

template class SW<PRECISION,ACC_PRECISION>;
}