pxi-fsu-time-order is as fast as the original

This commit is contained in:
Ryan Tang 2022-02-10 16:34:49 -05:00
parent d34cd5c771
commit c8d6946528

View File

@ -66,43 +66,15 @@ struct subevent
struct subevent *subevents[MAX_ID]; struct subevent *subevents[MAX_ID];
int nevts[MAX_ID], iptr[MAX_ID]; int nevts[MAX_ID], iptr[MAX_ID];
int maxevts[MAX_ID]; int maxevts[MAX_ID];
struct subevent fillevents[MAX_ID];
void saveBuffer( int i, int min_id){
void swap64(long long int * a, long long int *b){ memcpy( fillevents[i].data , subevents[min_id][iptr[min_id]].data, subevents[min_id][iptr[min_id]].length * sizeof(unsigned int));
long long int t = *a; fillevents[i].length = subevents[min_id][iptr[min_id]].length;
*a = *b; fillevents[i].detID = subevents[min_id][iptr[min_id]].detID;
*b = t; fillevents[i].timestamp = subevents[min_id][iptr[min_id]].timestamp;
}
void swapInt(int * a, int *b){
int t = *a;
*a = *b;
*b = t;
}
int partition(long long int arr[], int index [], int low, int high){
long long int pivot = arr[high];
int i = (low -1);
for(int j = low; j <= high -1 ; j++){
if( arr[j] < pivot ){
i++;
swap64(&arr[i], &arr[j]);
swapInt(&index[i], &index[j]);
}
}
swap64(&arr[i+1], &arr[high]);
swapInt(&index[i+1], &index[high]);
return i+1;
}
void quickSort(long long int arr[], int index[], int low, int high){
if (low < high){
int pi = partition(arr, index, low, high);
quickSort(arr, index, low, pi - 1);
quickSort(arr, index, pi + 1, high);
}
} }
@ -139,9 +111,9 @@ int main(int argc, char **argv) {
memset(nevts, 0, sizeof(nevts)); memset(nevts, 0, sizeof(nevts));
memset(iptr, 0, sizeof(iptr)); /// index of time memset(iptr, 0, sizeof(iptr)); /// index of time
long long int timeIndex[MAX_ID]; int saveBufferMem = 0;
int index[MAX_ID]; int nFill = 0;
int fillSize = 0; int FillFlag = false;
int i=0, j=0; int i=0, j=0;
@ -231,12 +203,24 @@ int main(int argc, char **argv) {
subevents[i][j].timestamp = 0; subevents[i][j].timestamp = 0;
subevents[i][j].detID = -1; subevents[i][j].detID = -1;
} }
//=== FSU modification
saveBufferMem += sizeof( unsigned int ) * MAX_SUB_LENGTH;
fillevents[i].data = new unsigned int [MAX_SUB_LENGTH];
fillevents[i].length = 0;
fillevents[i].timestamp = 0;
fillevents[i].detID = -1;
} }
//debug
int count = 0; int count = 0;
int debugCount = 0; int debugCount = 0;
printf("Static Memory = %ld KB (cf. MAX_ID=%d)\n", sizeof(subevents)/1024, MAX_ID); printf("Static Memory = %ld KB (cf. MAX_ID=%d)\n", sizeof(subevents)/1024, MAX_ID);
printf("Save Buffer Memory = %.3f MB \n", saveBufferMem / 1024./1024.);
while (1) { //main while loop while (1) { //main while loop
///////// /////////
@ -411,87 +395,6 @@ int main(int argc, char **argv) {
//######################## FSU //######################## FSU
// find group of event within timewindow, check is contain gamma. if not, throw away all. // find group of event within timewindow, check is contain gamma. if not, throw away all.
if( eventWindow > 0 ){
//quick sort of subevents[i][iptr[i]].timestamp, i = 0 , idmax +1
for( i = 0 ; i < idmax + 1; i++) {
if( nevts[i] > 0 ){
timeIndex[i] = subevents[i][iptr[i]].timestamp;
}else{
timeIndex[i] = MAXLONGLONGINT;
}
index[i] = i;
//printf("%3d , %llu, %d \n", i, timeIndex[i], index[i] );
}
quickSort(timeIndex, index, 0, idmax);
for( i = 0 ; i < idmax + 1; i++) {
//printf("%3d , %llu , %d\n", i, timeIndex[i], index[i]);
}
//reduce the index size
fillSize = 1;
for( i = 1; i < idmax + 1; i++){
if( timeIndex[i] - timeIndex[0] < eventWindow) fillSize ++;
}
// display
if ( count < debugCount) {
//if ( count < debugCount || timeIndex[10] == MAXLONGLONGINT) {
printf("===============================================\n");
for( int i = 0; i < idmax+1; i++) {
printf("%3d, %llu, %d \n", i, timeIndex[i], index[i]);
if( i == fillSize - 1 ) printf("------------------- %d \n", fillSize);
}
}
if( timeIndex[0] == MAXLONGLONGINT ) break;
//CHeck if fill evt.to for the data.
bool fillFlag = false;
for( i = 0 ; i < fillSize; i++ ){
if( count < debugCount ) printf("********************* %llu , detID : %d\n", timeIndex[0], subevents[index[i]][iptr[index[i]]].detID);
if( subevents[index[i]][iptr[index[i]]].detID < 100 ) fillFlag = true;
}
if( count < debugCount ) printf("=============== fillFlag : %d \n", fillFlag);
for( i = 0; i < fillSize; i++){
min_id = index[i];
if( fillFlag ){
fwrite(subevents[min_id][iptr[min_id]].data, sizeof(unsigned int)*subevents[min_id][iptr[min_id]].length, 1, fpw);
if( count < debugCount ) printf("filling subevents[%d][iprt[%d]] \n", min_id, min_id);
evts_tot_write++;
}else{
evts_tot_drop++;
}
//free data memory up until it's needed again
if( count < debugCount ) printf(" Free subevents[%d][iprt[%d]] \n", min_id, min_id);
free(subevents[min_id][iptr[min_id]].data);
subevents[min_id][iptr[min_id]].data = NULL;
totmem -= sizeof(unsigned int)*subevents[min_id][iptr[min_id]].length;
subevents[min_id][iptr[min_id]].length = 0;
subevents[min_id][iptr[min_id]].timestamp = 0;
subevents[min_id][iptr[min_id]].detID = -1;
nevts[min_id]--;
if (++iptr[min_id] >= maxevts[min_id]) iptr[min_id] -= maxevts[min_id];
}
count ++;
}else{
/////////
// write event with minimum time to file // write event with minimum time to file
timemin_old = timemin; timemin_old = timemin;
timemin = -1; timemin = -1;
@ -516,7 +419,48 @@ int main(int argc, char **argv) {
} }
if (subevents[min_id][iptr[min_id]].data == NULL) {printf("Error: data = NULL\n"); return -1;} if (subevents[min_id][iptr[min_id]].data == NULL) {printf("Error: data = NULL\n"); return -1;}
fwrite(subevents[min_id][iptr[min_id]].data, sizeof(unsigned int)*subevents[min_id][iptr[min_id]].length, 1, fpw); ///instead of writing, collect data within eventWindow, and also check is there any gamma data
if( nFill == 0 ){
saveBuffer(nFill, min_id);
if( fillevents[nFill].detID < 100 ) FillFlag = true;
if ( count < debugCount) printf("%3d, %llu, %3d , %s\n", nFill, fillevents[nFill].timestamp, fillevents[nFill].detID, FillFlag ? "fill" : "drop");
nFill ++;
}else{
if( subevents[min_id][iptr[min_id]].timestamp - fillevents[0].timestamp < eventWindow ) {
saveBuffer(nFill, min_id);
if( fillevents[nFill].detID < 100 ) FillFlag = true;
if ( count < debugCount) printf("%3d, %llu, %3d , %s\n", nFill, fillevents[nFill].timestamp, fillevents[nFill].detID, FillFlag ? "fill" : "drop");
nFill ++;
}else{
if( FillFlag ){
if ( count < debugCount) printf("----------------- filled \n");
for( i = 0; i < nFill; i++) fwrite(fillevents[i].data, sizeof(unsigned int)*fillevents[i].length, 1, fpw);
evts_tot_write += nFill;
}else{
if ( count < debugCount) printf("----------------- dropped \n");
evts_tot_drop += nFill;
}
nFill = 0;
FillFlag = false ;
saveBuffer(nFill, min_id);
if( fillevents[nFill].detID < 100 ) FillFlag = true;
if ( count < debugCount) printf("%3d, %llu, %3d , %s\n", nFill, fillevents[nFill].timestamp, fillevents[nFill].detID, FillFlag ? "fill" : "drop");
nFill ++;
}
}
///fwrite(subevents[min_id][iptr[min_id]].data, sizeof(unsigned int)*subevents[min_id][iptr[min_id]].length, 1, fpw);
//free data memory up until it's needed again //free data memory up until it's needed again
free(subevents[min_id][iptr[min_id]].data); free(subevents[min_id][iptr[min_id]].data);
@ -527,13 +471,18 @@ int main(int argc, char **argv) {
nevts[min_id]--; nevts[min_id]--;
if (++iptr[min_id] >= maxevts[min_id]) iptr[min_id] -= maxevts[min_id]; if (++iptr[min_id] >= maxevts[min_id]) iptr[min_id] -= maxevts[min_id];
evts_tot_write++; ///evts_tot_write++;
}else break; }else { // fill the rest of data
/////////
for( i = 0; i < nFill; i++) fwrite(fillevents[i].data, sizeof(unsigned int)*fillevents[i].length, 1, fpw);
evts_tot_drop += nFill;
break;
} }
count ++;
//print statistics //print statistics
if( evts_tot_read % 10000 == 0 ) if( evts_tot_read % 10000 == 0 )
printf("Malloc (%d MB) : evts in (\x1B[34m%lld\x1B[0m) : evts out (\x1B[32m%lld\x1B[0m) : evts drop (\x1B[32m%lld\x1B[0m) : diff (\x1B[31m%lld\x1B[0m)\r", printf("Malloc (%d MB) : evts in (\x1B[34m%lld\x1B[0m) : evts out (\x1B[32m%lld\x1B[0m) : evts drop (\x1B[32m%lld\x1B[0m) : diff (\x1B[31m%lld\x1B[0m)\r",
@ -552,7 +501,8 @@ int main(int argc, char **argv) {
//print statistics last time //print statistics last time
printf("\33[2K"); printf("\33[2K");
printf("Malloc (%d MB) : evts in (\x1B[34m%lld\x1B[0m) : evts out (\x1B[32m%lld\x1B[0m) : diff (\x1B[31m%lld\x1B[0m)\n", (totmem)/1024/1024, evts_tot_read, evts_tot_write, evts_tot_read-evts_tot_write); printf("Malloc (%d MB) : evts in (\x1B[34m%lld\x1B[0m) : evts out (\x1B[32m%lld\x1B[0m) : evts drop (\x1B[32m%lld\x1B[0m) : diff (\x1B[31m%lld\x1B[0m)\n",
(totmem)/1024/1024, evts_tot_read, evts_tot_write, evts_tot_drop, evts_tot_read-evts_tot_write - evts_tot_drop);
if (outoforder > 0) printf("\x1B[31mWarning, there are %d events out of time order\x1B[0m\n", outoforder); if (outoforder > 0) printf("\x1B[31mWarning, there are %d events out of time order\x1B[0m\n", outoforder);
if (totmem != 0) printf("\x1B[31mError: total memory not conserved\x1B[0m\n"); if (totmem != 0) printf("\x1B[31mError: total memory not conserved\x1B[0m\n");