Skip to content

Commit

Permalink
BugFix: memory error incollective effects circular buffers (#771)
Browse files Browse the repository at this point in the history
* circular buffer with size one correctly handled

* avoid divide by 0 if no parts in bunch

---------

Co-authored-by: Lee Carver <carver@grappa.esrf.fr>
  • Loading branch information
swhite2401 and Lee Carver committed May 23, 2024
1 parent ef82172 commit 588edbc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
4 changes: 3 additions & 1 deletion atintegrators/BeamLoadingCavityPass.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ struct elem


void write_buffer(double *data, double *buffer, int datasize, int buffersize){
memmove(buffer, buffer + datasize, datasize*buffersize*sizeof(double));
if(buffersize>1){
memmove(buffer, buffer + datasize, datasize*buffersize*sizeof(double));
}
memcpy(buffer + datasize*(buffersize-1), data, datasize*sizeof(double));
}

Expand Down
17 changes: 12 additions & 5 deletions atintegrators/atimplib.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ static double getTableWake(double *waketable,double *waketableT,double distance,
};

static void rotate_table_history(long nturns,long nslice,double *turnhistory,double circumference){
memmove(turnhistory, turnhistory + nslice, 4*nslice*nturns*sizeof(double));
double *z = turnhistory+nslice*nturns*2;
int i;
for(i=0; i<nslice*nturns; i++){
z[i] += -circumference;
if(nturns > 1){
memmove(turnhistory, turnhistory + nslice, 4*nslice*nturns*sizeof(double));
double *z = turnhistory+nslice*nturns*2;
for(i=0; i<nslice*nturns; i++){
z[i] += -circumference;
}
}
double *x0 = turnhistory + (nturns-1)*nslice;
double *y0 = turnhistory + (2*nturns-1)*nslice;
Expand Down Expand Up @@ -171,7 +173,12 @@ static void slice_bunch(double *r_in,int num_particles,int nslice,int nturns,
zpos[i] += bunch_spos[ib]-bunch_spos[nbunch-1];
xpos[i] = (weight[i]>0.0) ? xpos[i]/weight[i] : 0.0;
ypos[i] = (weight[i]>0.0) ? ypos[i]/weight[i] : 0.0;
weight[i] *= bunch_currents[ib]/np_bunch[ib];
if (np_bunch[ib] == 0.0) {
weight[i] = 0.0;
}
else {
weight[i] *= bunch_currents[ib]/np_bunch[ib];
}
}
atFree(np_bunch);
atFree(smin);
Expand Down

0 comments on commit 588edbc

Please sign in to comment.