From bf888045d66c7d99262ab42cfde4502c69e88a5e Mon Sep 17 00:00:00 2001 From: jtclemm Date: Mon, 4 Nov 2024 13:16:07 -0700 Subject: [PATCH] Fixing memory seg fault w/ multi occasional nlist --- src/nstencil.cpp | 68 +++++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/src/nstencil.cpp b/src/nstencil.cpp index 5d2bf5d239..540b17b8b8 100644 --- a/src/nstencil.cpp +++ b/src/nstencil.cpp @@ -223,15 +223,15 @@ void NStencil::create_setup() // smax = max possible size of entire 3d stencil // stencil will be empty if cutneighmax = 0.0 - sx = static_cast(cutneighmax*bininvx); - if (sx*binsizex < cutneighmax) sx++; - sy = static_cast(cutneighmax*bininvy); - if (sy*binsizey < cutneighmax) sy++; - sz = static_cast(cutneighmax*bininvz); - if (sz*binsizez < cutneighmax) sz++; + sx = static_cast(cutneighmax * bininvx); + if (sx * binsizex < cutneighmax) sx++; + sy = static_cast(cutneighmax * bininvy); + if (sy * binsizey < cutneighmax) sy++; + sz = static_cast(cutneighmax * bininvz); + if (sz * binsizez < cutneighmax) sz++; if (dimension == 2) sz = 0; - int smax = (2*sx+1) * (2*sy+1) * (2*sz+1); + int smax = (2 * sx + 1) * (2 * sy + 1) * (2 * sz + 1); // reallocate stencil structs if necessary // for BIN and MULTI_OLD styles @@ -240,10 +240,10 @@ void NStencil::create_setup() if (smax > maxstencil) { maxstencil = smax; memory->destroy(stencil); - memory->create(stencil,maxstencil,"neighstencil:stencil"); + memory->create(stencil, maxstencil, "neighstencil:stencil"); if (xyzflag) { memory->destroy(stencilxyz); - memory->create(stencilxyz,maxstencil,3,"neighstencil:stencilxyz"); + memory->create(stencilxyz, maxstencil, 3, "neighstencil:stencilxyz"); } } @@ -251,9 +251,9 @@ void NStencil::create_setup() int i; int n = atom->ntypes; if (maxstencil_multi_old == 0) { - nstencil_multi_old = new int[n+1]; - stencil_multi_old = new int*[n+1]; - distsq_multi_old = new double*[n+1]; + nstencil_multi_old = new int[n + 1]; + stencil_multi_old = new int*[n + 1]; + distsq_multi_old = new double*[n + 1]; for (i = 1; i <= n; i++) { nstencil_multi_old[i] = 0; stencil_multi_old[i] = nullptr; @@ -265,9 +265,9 @@ void NStencil::create_setup() for (i = 1; i <= n; i++) { memory->destroy(stencil_multi_old[i]); memory->destroy(distsq_multi_old[i]); - memory->create(stencil_multi_old[i],maxstencil_multi_old, + memory->create(stencil_multi_old[i], maxstencil_multi_old, "neighstencil:stencil_multi_old"); - memory->create(distsq_multi_old[i],maxstencil_multi_old, + memory->create(distsq_multi_old[i], maxstencil_multi_old, "neighstencil:distsq_multi_old"); } } @@ -280,7 +280,7 @@ void NStencil::create_setup() if (nb) copy_bin_info_multi(); // Deallocate arrays if previously allocated - if((n > maxcollections) && stencil_multi){ + if ((n > maxcollections) && stencil_multi){ memory->destroy(nstencil_multi); for (i = 0; i < maxcollections; i++) { for (j = 0; j < maxcollections; j++) @@ -339,10 +339,10 @@ void NStencil::create_setup() memory->create(maxstencil_multi, n, n, "neighstencil::maxstencil_multi"); memory->create(nstencil_multi, n, n, "neighstencil::nstencil_multi"); stencil_multi = new int**[n](); - for (i = 0; i < n; ++i) { + for (i = 0; i < n; i++) { stencil_multi[i] = new int*[n](); - for (j = 0; j < n; ++j) { - maxstencil_multi[i][j] = 0; + for (j = 0; j < n; j++) { + maxstencil_multi[i][j] = 0; nstencil_multi[i][j] = 0; stencil_multi[i][j] = nullptr; } @@ -378,23 +378,23 @@ void NStencil::create_setup() stencil_range = sqrt(cutcollectionsq[i][j]); - sx = static_cast (stencil_range*bininvx_multi[bin_collection]); - if (sx*binsizex_multi[bin_collection] < stencil_range) sx++; - sy = static_cast (stencil_range*bininvy_multi[bin_collection]); - if (sy*binsizey_multi[bin_collection] < stencil_range) sy++; - sz = static_cast (stencil_range*bininvz_multi[bin_collection]); - if (sz*binsizez_multi[bin_collection] < stencil_range) sz++; + sx = static_cast (stencil_range * bininvx_multi[bin_collection]); + if (sx * binsizex_multi[bin_collection] < stencil_range) sx++; + sy = static_cast (stencil_range * bininvy_multi[bin_collection]); + if (sy * binsizey_multi[bin_collection] < stencil_range) sy++; + sz = static_cast (stencil_range * bininvz_multi[bin_collection]); + if (sz * binsizez_multi[bin_collection] < stencil_range) sz++; if (dimension == 2) sz = 0; stencil_sx_multi[i][j] = sx; stencil_sy_multi[i][j] = sy; stencil_sz_multi[i][j] = sz; - smax = ((2*sx+1) * (2*sy+1) * (2*sz+1)); + smax = ((2 * sx + 1) * (2 * sy + 1) * (2 * sz + 1)); if (smax > maxstencil_multi[i][j]) { maxstencil_multi[i][j] = smax; - if(stencil_multi[i][j]) + if (stencil_multi[i][j]) memory->destroy(stencil_multi[i][j]); memory->create(stencil_multi[i][j], smax, "neighstencil::stencil_multi"); @@ -457,16 +457,18 @@ double NStencil::memory_usage() { double bytes = 0; if (neighstyle == Neighbor::BIN) { - bytes += memory->usage(stencil,maxstencil); - bytes += memory->usage(stencilxyz,maxstencil,3); + bytes += memory->usage(stencil, maxstencil); + bytes += memory->usage(stencilxyz, maxstencil, 3); } else if (neighstyle == Neighbor::MULTI_OLD) { - bytes += (double)atom->ntypes*maxstencil_multi_old * sizeof(int); - bytes += (double)atom->ntypes*maxstencil_multi_old * sizeof(double); + bytes += (double)atom->ntypes * maxstencil_multi_old * sizeof(int); + bytes += (double)atom->ntypes * maxstencil_multi_old * sizeof(double); } else if (neighstyle == Neighbor::MULTI) { int n = ncollections; - for (int i = 0; i < n; i++) { - for (int j = 0; j < n; j++) { - bytes += (double)maxstencil_multi[i][j] * sizeof(int); + if (maxstencil_multi) { + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + bytes += (double)maxstencil_multi[i][j] * sizeof(int); + } } } }