Skip to content

Commit 317fb8d

Browse files
authored
Merge pull request lammps#4301 from stanmoore1/reaxff_overflow_errors
Gracefully error out if integer overflow in ReaxFF or QEq
2 parents d48ca25 + 4aca808 commit 317fb8d

File tree

8 files changed

+52
-19
lines changed

8 files changed

+52
-19
lines changed

src/KOKKOS/fix_qeq_reaxff_kokkos.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ void FixQEqReaxFFKokkos<DeviceType>::pre_force(int /*vflag*/)
301301

302302
template<class DeviceType>
303303
KOKKOS_INLINE_FUNCTION
304-
void FixQEqReaxFFKokkos<DeviceType>::num_neigh_item(int ii, int &maxneigh) const
304+
void FixQEqReaxFFKokkos<DeviceType>::num_neigh_item(int ii, bigint &maxneigh) const
305305
{
306306
const int i = d_ilist[ii];
307307
maxneigh += d_numneigh[i];
@@ -316,13 +316,16 @@ void FixQEqReaxFFKokkos<DeviceType>::allocate_matrix()
316316

317317
// determine the total space for the H matrix
318318

319-
m_cap = 0;
319+
bigint m_cap_big = 0;
320320

321321
// limit scope of functor to allow deallocation of views
322322
{
323323
FixQEqReaxFFKokkosNumNeighFunctor<DeviceType> neigh_functor(this);
324-
Kokkos::parallel_reduce(nn,neigh_functor,m_cap);
324+
Kokkos::parallel_reduce(nn,neigh_functor,m_cap_big);
325325
}
326+
if (m_cap_big > MAXSMALLINT)
327+
error->one(FLERR,"Too many neighbors in fix qeq/reaxff");
328+
m_cap = m_cap_big;
326329

327330
// deallocate first to reduce memory overhead
328331

src/KOKKOS/fix_qeq_reaxff_kokkos.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class FixQEqReaxFFKokkos : public FixQEqReaxFF, public KokkosBase {
7070
void pre_force(int) override;
7171

7272
KOKKOS_INLINE_FUNCTION
73-
void num_neigh_item(int, int&) const;
73+
void num_neigh_item(int, bigint&) const;
7474

7575
KOKKOS_INLINE_FUNCTION
7676
void operator()(TagQEqZero, const int&) const;
@@ -290,13 +290,13 @@ class FixQEqReaxFFKokkos : public FixQEqReaxFF, public KokkosBase {
290290
template <class DeviceType>
291291
struct FixQEqReaxFFKokkosNumNeighFunctor {
292292
typedef DeviceType device_type;
293-
typedef int value_type;
293+
typedef bigint value_type;
294294
FixQEqReaxFFKokkos<DeviceType> c;
295295
FixQEqReaxFFKokkosNumNeighFunctor(FixQEqReaxFFKokkos<DeviceType>* c_ptr):c(*c_ptr) {
296296
c.cleanup_copy();
297297
};
298298
KOKKOS_INLINE_FUNCTION
299-
void operator()(const int ii, int &maxneigh) const {
299+
void operator()(const int ii, bigint &maxneigh) const {
300300
c.num_neigh_item(ii, maxneigh);
301301
}
302302
};

src/KOKKOS/kokkos.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,10 +638,10 @@ void KokkosLMP::accelerator(int narg, char **arg)
638638
called by Finish
639639
------------------------------------------------------------------------- */
640640

641-
int KokkosLMP::neigh_count(int m)
641+
bigint KokkosLMP::neigh_count(int m)
642642
{
643643
int inum = 0;
644-
int nneigh = 0;
644+
bigint nneigh = 0;
645645

646646
ArrayTypes<LMPHostType>::t_int_1d h_ilist;
647647
ArrayTypes<LMPHostType>::t_int_1d h_numneigh;

src/KOKKOS/kokkos.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class KokkosLMP : protected Pointers {
6464
static void initialize(const Kokkos::InitializationSettings&, Error *);
6565
static void finalize();
6666
void accelerator(int, char **);
67-
int neigh_count(int);
67+
bigint neigh_count(int);
6868

6969
template<class DeviceType>
7070
int need_dup(int qeq_flag = 0)

src/KOKKOS/pair_reaxff_kokkos.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,10 +1504,18 @@ void PairReaxFFKokkos<DeviceType>::allocate_array()
15041504
if (cut_hbsq > 0.0) {
15051505
MemKK::realloc_kokkos(d_hb_first,"reaxff/kk:hb_first",nmax);
15061506
MemKK::realloc_kokkos(d_hb_num,"reaxff/kk:hb_num",nmax);
1507+
1508+
if (((bigint) nmax*maxhb) > MAXSMALLINT)
1509+
error->one(FLERR,"Too many hydrogen bonds in pair reaxff");
1510+
15071511
MemKK::realloc_kokkos(d_hb_list,"reaxff/kk:hb_list",nmax*maxhb);
15081512
}
15091513
MemKK::realloc_kokkos(d_bo_first,"reaxff/kk:bo_first",nmax);
15101514
MemKK::realloc_kokkos(d_bo_num,"reaxff/kk:bo_num",nmax);
1515+
1516+
if (((bigint) nmax*maxbo) > MAXSMALLINT)
1517+
error->one(FLERR,"Too many bonds in pair reaxff");
1518+
15111519
MemKK::realloc_kokkos(d_bo_list,"reaxff/kk:bo_list",nmax*maxbo);
15121520

15131521
MemKK::realloc_kokkos(d_BO,"reaxff/kk:BO",nmax,maxbo);

src/QEQ/fix_qeq.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,9 @@ void FixQEq::reallocate_storage()
237237

238238
void FixQEq::allocate_matrix()
239239
{
240-
int i,ii,inum,m;
240+
int i,ii,inum;
241241
int *ilist, *numneigh;
242+
bigint m;
242243

243244
int mincap;
244245
double safezone;
@@ -261,7 +262,10 @@ void FixQEq::allocate_matrix()
261262
i = ilist[ii];
262263
m += numneigh[i];
263264
}
264-
m_cap = MAX((int)(m * safezone), mincap * MIN_NBRS);
265+
bigint m_cap_big = (bigint)MAX(m * safezone, mincap * MIN_NBRS);
266+
if (m_cap_big > MAXSMALLINT)
267+
error->one(FLERR,"Too many neighbors in fix qeq");
268+
m_cap = m_cap_big;
265269

266270
H.n = n_cap;
267271
H.m = m_cap;

src/REAXFF/fix_qeq_reaxff.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,8 @@ void FixQEqReaxFF::reallocate_storage()
338338

339339
void FixQEqReaxFF::allocate_matrix()
340340
{
341-
int i,ii,m;
341+
int i,ii;
342+
bigint m;
342343

343344
int mincap;
344345
double safezone;
@@ -360,7 +361,10 @@ void FixQEqReaxFF::allocate_matrix()
360361
i = ilist[ii];
361362
m += numneigh[i];
362363
}
363-
m_cap = MAX((int)(m * safezone), mincap * REAX_MIN_NBRS);
364+
bigint m_cap_big = (bigint)MAX(m * safezone, mincap * REAX_MIN_NBRS);
365+
if (m_cap_big > MAXSMALLINT)
366+
error->one(FLERR,"Too many neighbors in fix qeq/reaxff");
367+
m_cap = m_cap_big;
364368

365369
H.n = n_cap;
366370
H.m = m_cap;

src/REAXFF/reaxff_allocate.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,16 +169,23 @@ namespace ReaxFF {
169169
static int Reallocate_HBonds_List(reax_system *system, reax_list *hbonds)
170170
{
171171
int i, total_hbonds;
172+
LAMMPS_NS::bigint total_hbonds_big;
172173

173174
int mincap = system->mincap;
174175
double saferzone = system->saferzone;
175176

176-
total_hbonds = 0;
177+
total_hbonds_big = 0;
177178
for (i = 0; i < system->n; ++i)
178179
if ((system->my_atoms[i].Hindex) >= 0) {
179-
total_hbonds += system->my_atoms[i].num_hbonds;
180+
total_hbonds_big += system->my_atoms[i].num_hbonds;
180181
}
181-
total_hbonds = (int)(MAX(total_hbonds*saferzone, mincap*system->minhbonds));
182+
total_hbonds_big = (LAMMPS_NS::bigint)(MAX(total_hbonds_big*saferzone, mincap*system->minhbonds));
183+
184+
auto error = system->error_ptr;
185+
if (total_hbonds_big > MAXSMALLINT)
186+
error->one(FLERR,"Too many hydrogen bonds in pair reaxff");
187+
188+
total_hbonds = total_hbonds_big;
182189

183190
Delete_List(hbonds);
184191
Make_List(system->Hcap, total_hbonds, TYP_HBOND, hbonds);
@@ -190,17 +197,24 @@ namespace ReaxFF {
190197
reax_list *bonds, int *total_bonds, int *est_3body)
191198
{
192199
int i;
200+
LAMMPS_NS::bigint total_bonds_big;
193201

194202
int mincap = system->mincap;
195203
double safezone = system->safezone;
196204

197-
*total_bonds = 0;
205+
total_bonds_big = 0;
198206
*est_3body = 0;
199207
for (i = 0; i < system->N; ++i) {
200208
*est_3body += SQR(system->my_atoms[i].num_bonds);
201-
*total_bonds += system->my_atoms[i].num_bonds;
209+
total_bonds_big += system->my_atoms[i].num_bonds;
202210
}
203-
*total_bonds = (int)(MAX(*total_bonds * safezone, mincap*MIN_BONDS));
211+
total_bonds_big = (LAMMPS_NS::bigint)(MAX(total_bonds_big * safezone, mincap*MIN_BONDS));
212+
213+
auto error = system->error_ptr;
214+
if (total_bonds_big > MAXSMALLINT)
215+
error->one(FLERR,"Too many bonds in pair reaxff");
216+
217+
*total_bonds = total_bonds_big;
204218

205219
if (system->omp_active)
206220
for (i = 0; i < bonds->num_intrs; ++i)

0 commit comments

Comments
 (0)