Refactor half neighbor lists code

Signed-off-by: Rafael Ravedutti <rafaelravedutti@gmail.com>
This commit is contained in:
Rafael Ravedutti 2022-03-18 01:28:11 +01:00
parent 5df544637f
commit e7737e9151
5 changed files with 33 additions and 56 deletions

View File

@ -28,8 +28,7 @@
#include <atom.h> #include <atom.h>
#include <stats.h> #include <stats.h>
double computeForceLJFullNeigh(Parameter *param, Atom *atom, Neighbor *neighbor, Stats *stats) double computeForceLJFullNeigh(Parameter *param, Atom *atom, Neighbor *neighbor, Stats *stats) {
{
int Nlocal = atom->Nlocal; int Nlocal = atom->Nlocal;
int* neighs; int* neighs;
MD_FLOAT* fx = atom->fx; MD_FLOAT* fx = atom->fx;
@ -108,8 +107,7 @@ double computeForceLJFullNeigh(Parameter *param, Atom *atom, Neighbor *neighbor,
return E-S; return E-S;
} }
double computeForceLJHalfNeigh(Parameter *param, Atom *atom, Neighbor *neighbor, Stats *stats) double computeForceLJHalfNeigh(Parameter *param, Atom *atom, Neighbor *neighbor, Stats *stats) {
{
int Nlocal = atom->Nlocal; int Nlocal = atom->Nlocal;
int* neighs; int* neighs;
MD_FLOAT* fx = atom->fx; MD_FLOAT* fx = atom->fx;
@ -167,9 +165,13 @@ double computeForceLJHalfNeigh(Parameter *param, Atom *atom, Neighbor *neighbor,
fiy += dely * force; fiy += dely * force;
fiz += delz * force; fiz += delz * force;
fx[j] -= delx * force; // We do not need to update forces for ghost atoms
fy[j] -= dely * force; if(j < Nlocal) {
fz[j] -= delz * force; // TODO: Experiment with AoS layout for forces
fx[j] -= delx * force;
fy[j] -= dely * force;
fz[j] -= delz * force;
}
} }
} }

View File

@ -28,10 +28,10 @@
typedef struct { typedef struct {
int every; int every;
int ncalls; int ncalls;
int* neighbors;
int maxneighs; int maxneighs;
int half_neigh;
int* neighbors;
int* numneigh; int* numneigh;
int halfneigh;
} Neighbor; } Neighbor;
extern void initNeighbor(Neighbor*, Parameter*); extern void initNeighbor(Neighbor*, Parameter*);

View File

@ -181,6 +181,10 @@ int main(int argc, char** argv) {
param.nz = atoi(argv[++i]); param.nz = atoi(argv[++i]);
continue; continue;
} }
if((strcmp(argv[i], "-half") == 0)) {
param.half_neigh = atoi(argv[++i]);
continue;
}
if((strcmp(argv[i], "-r") == 0) || (strcmp(argv[i], "--radius") == 0)) { if((strcmp(argv[i], "-r") == 0) || (strcmp(argv[i], "--radius") == 0)) {
param.cutforce = atof(argv[++i]); param.cutforce = atof(argv[++i]);
continue; continue;

View File

@ -46,14 +46,11 @@ static int nmax;
static int nstencil; // # of bins in stencil static int nstencil; // # of bins in stencil
static int* stencil; // stencil list of bin offsets static int* stencil; // stencil list of bin offsets
static MD_FLOAT binsizex, binsizey, binsizez; static MD_FLOAT binsizex, binsizey, binsizez;
static int halfneigh; //use half neighbour list
static int coord2bin(MD_FLOAT, MD_FLOAT , MD_FLOAT); static int coord2bin(MD_FLOAT, MD_FLOAT , MD_FLOAT);
static MD_FLOAT bindist(int, int, int); static MD_FLOAT bindist(int, int, int);
/* exported subroutines */ /* exported subroutines */
void initNeighbor(Neighbor *neighbor, Parameter *param) void initNeighbor(Neighbor *neighbor, Parameter *param) {
{
MD_FLOAT neighscale = 5.0 / 6.0; MD_FLOAT neighscale = 5.0 / 6.0;
xprd = param->nx * param->lattice; xprd = param->nx * param->lattice;
yprd = param->ny * param->lattice; yprd = param->ny * param->lattice;
@ -70,11 +67,10 @@ void initNeighbor(Neighbor *neighbor, Parameter *param)
neighbor->maxneighs = 100; neighbor->maxneighs = 100;
neighbor->numneigh = NULL; neighbor->numneigh = NULL;
neighbor->neighbors = NULL; neighbor->neighbors = NULL;
neighbor->halfneigh = param->half_neigh; neighbor->half_neigh = param->half_neigh;
} }
void setupNeighbor(Parameter* param) void setupNeighbor(Parameter* param) {
{
MD_FLOAT coord; MD_FLOAT coord;
int mbinxhi, mbinyhi, mbinzhi; int mbinxhi, mbinyhi, mbinzhi;
int nextx, nexty, nextz; int nextx, nexty, nextz;
@ -116,25 +112,19 @@ void setupNeighbor(Parameter* param)
coord = xlo - cutneigh - SMALL * xprd; coord = xlo - cutneigh - SMALL * xprd;
mbinxlo = (int) (coord * bininvx); mbinxlo = (int) (coord * bininvx);
if (coord < 0.0) { if (coord < 0.0) { mbinxlo = mbinxlo - 1; }
mbinxlo = mbinxlo - 1;
}
coord = xhi + cutneigh + SMALL * xprd; coord = xhi + cutneigh + SMALL * xprd;
mbinxhi = (int) (coord * bininvx); mbinxhi = (int) (coord * bininvx);
coord = ylo - cutneigh - SMALL * yprd; coord = ylo - cutneigh - SMALL * yprd;
mbinylo = (int) (coord * bininvy); mbinylo = (int) (coord * bininvy);
if (coord < 0.0) { if (coord < 0.0) { mbinylo = mbinylo - 1; }
mbinylo = mbinylo - 1;
}
coord = yhi + cutneigh + SMALL * yprd; coord = yhi + cutneigh + SMALL * yprd;
mbinyhi = (int) (coord * bininvy); mbinyhi = (int) (coord * bininvy);
coord = zlo - cutneigh - SMALL * zprd; coord = zlo - cutneigh - SMALL * zprd;
mbinzlo = (int) (coord * bininvz); mbinzlo = (int) (coord * bininvz);
if (coord < 0.0) { if (coord < 0.0) { mbinzlo = mbinzlo - 1; }
mbinzlo = mbinzlo - 1;
}
coord = zhi + cutneigh + SMALL * zprd; coord = zhi + cutneigh + SMALL * zprd;
mbinzhi = (int) (coord * bininvz); mbinzhi = (int) (coord * bininvz);
@ -152,20 +142,13 @@ void setupNeighbor(Parameter* param)
nextx = (int) (cutneigh * bininvx); nextx = (int) (cutneigh * bininvx);
if(nextx * binsizex < FACTOR * cutneigh) nextx++; if(nextx * binsizex < FACTOR * cutneigh) nextx++;
nexty = (int) (cutneigh * bininvy); nexty = (int) (cutneigh * bininvy);
if(nexty * binsizey < FACTOR * cutneigh) nexty++; if(nexty * binsizey < FACTOR * cutneigh) nexty++;
nextz = (int) (cutneigh * bininvz); nextz = (int) (cutneigh * bininvz);
if(nextz * binsizez < FACTOR * cutneigh) nextz++; if(nextz * binsizez < FACTOR * cutneigh) nextz++;
if (stencil) { if (stencil) { free(stencil); }
free(stencil); stencil = (int*) malloc((2 * nextz + 1) * (2 * nexty + 1) * (2 * nextx + 1) * sizeof(int));
}
stencil = (int*) malloc(
(2 * nextz + 1) * (2 * nexty + 1) * (2 * nextx + 1) * sizeof(int));
nstencil = 0; nstencil = 0;
int kstart = -nextz; int kstart = -nextz;
@ -173,28 +156,20 @@ void setupNeighbor(Parameter* param)
for(int j = -nexty; j <= nexty; j++) { for(int j = -nexty; j <= nexty; j++) {
for(int i = -nextx; i <= nextx; i++) { for(int i = -nextx; i <= nextx; i++) {
if(bindist(i, j, k) < cutneighsq) { if(bindist(i, j, k) < cutneighsq) {
stencil[nstencil++] = stencil[nstencil++] = k * mbiny * mbinx + j * mbinx + i;
k * mbiny * mbinx + j * mbinx + i;
} }
} }
} }
} }
mbins = mbinx * mbiny * mbinz; mbins = mbinx * mbiny * mbinz;
if (bincount) { free(bincount); }
if (bincount) {
free(bincount);
}
bincount = (int*) malloc(mbins * sizeof(int)); bincount = (int*) malloc(mbins * sizeof(int));
if (bins) { free(bins); }
if (bins) {
free(bins);
}
bins = (int*) malloc(mbins * atoms_per_bin * sizeof(int)); bins = (int*) malloc(mbins * atoms_per_bin * sizeof(int));
} }
void buildNeighbor(Atom *atom, Neighbor *neighbor) void buildNeighbor(Atom *atom, Neighbor *neighbor) {
{
int nall = atom->Nlocal + atom->Nghost; int nall = atom->Nlocal + atom->Nghost;
/* extend atom arrays if necessary */ /* extend atom arrays if necessary */
@ -231,8 +206,7 @@ void buildNeighbor(Atom *atom, Neighbor *neighbor)
for(int m = 0; m < bincount[jbin]; m++) { for(int m = 0; m < bincount[jbin]; m++) {
int j = loc_bin[m]; int j = loc_bin[m];
if((j == i) || (neighbor->half_neigh && (j < i))) {
if ( (j == i) || (neighbor->halfneigh && (j < i))) {
continue; continue;
} }
@ -247,7 +221,7 @@ void buildNeighbor(Atom *atom, Neighbor *neighbor)
#else #else
const MD_FLOAT cutoff = cutneighsq; const MD_FLOAT cutoff = cutneighsq;
#endif #endif
if( rsq <= cutoff ) { if(rsq <= cutoff) {
neighptr[n++] = j; neighptr[n++] = j;
} }
} }
@ -274,8 +248,7 @@ void buildNeighbor(Atom *atom, Neighbor *neighbor)
} }
/* internal subroutines */ /* internal subroutines */
MD_FLOAT bindist(int i, int j, int k) MD_FLOAT bindist(int i, int j, int k) {
{
MD_FLOAT delx, dely, delz; MD_FLOAT delx, dely, delz;
if(i > 0) { if(i > 0) {
@ -305,8 +278,7 @@ MD_FLOAT bindist(int i, int j, int k)
return (delx * delx + dely * dely + delz * delz); return (delx * delx + dely * dely + delz * delz);
} }
int coord2bin(MD_FLOAT xin, MD_FLOAT yin, MD_FLOAT zin) int coord2bin(MD_FLOAT xin, MD_FLOAT yin, MD_FLOAT zin) {
{
int ix, iy, iz; int ix, iy, iz;
if(xin >= xprd) { if(xin >= xprd) {
@ -336,8 +308,7 @@ int coord2bin(MD_FLOAT xin, MD_FLOAT yin, MD_FLOAT zin)
return (iz * mbiny * mbinx + iy * mbinx + ix + 1); return (iz * mbiny * mbinx + iy * mbinx + ix + 1);
} }
void binatoms(Atom *atom) void binatoms(Atom *atom) {
{
int nall = atom->Nlocal + atom->Nghost; int nall = atom->Nlocal + atom->Nghost;
int resize = 1; int resize = 1;

View File

@ -52,7 +52,7 @@ void initParameter(Parameter *param) {
param->reneigh_every = 20; param->reneigh_every = 20;
param->x_out_every = 20; param->x_out_every = 20;
param->v_out_every = 5; param->v_out_every = 5;
param->half_neigh = 1; param->half_neigh = 0;
param->proc_freq = 2.4; param->proc_freq = 2.4;
} }