use quick sort in pxi-fsu-time-sort. 4 times slower

This commit is contained in:
Ryan Tang 2022-02-10 13:45:05 -05:00
parent 5d5047b972
commit d34cd5c771
2 changed files with 372 additions and 208 deletions

View File

@ -44,15 +44,22 @@
#define M2_SUB_EVENTS 5000
#define M3_SUB_EVENTS 20000
#define M4_SUB_EVENTS 50000
#define M5_SUB_EVENTS 100000
#define MAX_SUB_EVENTS 200000
#define MAX_MALLOC 4000*1024*1024L //2GB
#define MAXLONGLONGINT 9223372036854775807
//TODO change to load file
#include "../mapping.h"
struct subevent
{
long long int timestamp;
int length; //unit = words with 4 bytes per word
int detID;
unsigned int *data;
};
@ -61,6 +68,44 @@ int nevts[MAX_ID], iptr[MAX_ID];
int maxevts[MAX_ID];
void swap64(long long int * a, long long int *b){
long long int t = *a;
*a = *b;
*b = t;
}
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);
}
}
int main(int argc, char **argv) {
FILE *fpr, *fpw;
@ -73,6 +118,7 @@ int main(int argc, char **argv) {
int pause=0;
long long int nwords=0, evts_tot_read=0, evts_tot_write=0;
long long int evts_tot_drop = 0;
long long int time=0,time_old=0;
int length=0;
@ -91,11 +137,13 @@ int main(int argc, char **argv) {
int min_id = -1;
memset(nevts, 0, sizeof(nevts));
memset(iptr, 0, sizeof(iptr));
memset(iptr, 0, sizeof(iptr)); /// index of time
long long int timeIndex[MAX_ID];
int index[MAX_ID];
int fillSize = 0;
int i=0, j=0;
div_t e_div;
//open input event file
if ((fpr = fopen(argv[1], "r")) == NULL) {
@ -114,6 +162,14 @@ int main(int argc, char **argv) {
return 1;
}
printf("open : \033[1;31m%s\033[m\n", argv[1]);
printf("save : \033[1;34m%s\033[m\n", filenameto);
int eventWindow = 0;
if( argc >= 3 ) eventWindow = atoi(argv[2]);
printf(" event build time window : %d ticks = %d ns \n", eventWindow, 10* eventWindow);
//check for lockfile, active PID, and event file for auto "online" mode detection
FILE *FPLOCK;
@ -144,13 +200,9 @@ int main(int argc, char **argv) {
sscanf(line,"%s\n", onlinefile);
fclose(FPPATH);
if (filename == NULL) {
if (strcmp(onlinefile,argv[1]) == 0) {
online = 1;
}
if (strcmp(onlinefile,argv[1]) == 0) online = 1;
}else {
if (strcmp(onlinefile,filename+1) == 0) {
online = 1;
}
if (strcmp(onlinefile,filename+1) == 0) online = 1;
}
}
}
@ -164,9 +216,6 @@ int main(int argc, char **argv) {
fprsize = fprsize_orig = ftell(fpr);
fseek(fpr, fprpos, SEEK_SET);
//Get memory for default number of subevents per channel id
for (i=0; i < MAX_ID; i++){
subevents[i] = (struct subevent *) malloc(sizeof(struct subevent)*DEF_SUB_EVENTS);
@ -180,9 +229,13 @@ int main(int argc, char **argv) {
subevents[i][j].data = NULL;
subevents[i][j].length = 0;
subevents[i][j].timestamp = 0;
subevents[i][j].detID = -1;
}
}
int count = 0;
int debugCount = 0;
printf("Static Memory = %ld KB (cf. MAX_ID=%d)\n", sizeof(subevents)/1024, MAX_ID);
while (1) { //main while loop
@ -193,8 +246,7 @@ int main(int argc, char **argv) {
// read 4-byte header
if (pause == 1) {
pause = 0;
}
else {
}else {
//////////////
//auto online
@ -223,8 +275,7 @@ int main(int argc, char **argv) {
fclose(FPPATH);
if (filename == NULL) {
if (strcmp(onlinefile,argv[1]) == 0) online = 1;
}
else {
}else {
if (strcmp(onlinefile,filename+1) == 0) online = 1;
}
}
@ -271,6 +322,7 @@ int main(int argc, char **argv) {
subevents[id][j].data = NULL;
subevents[id][j].length = 0;
subevents[id][j].timestamp = 0;
subevents[id][j].detID = -1;
}
// if circular buffer is wrapped around (i.e., final is to left of intial, move data to right of initial)
@ -286,6 +338,7 @@ int main(int argc, char **argv) {
}
subevents[id][evts_old+j].length = subevents[id][j].length;
subevents[id][evts_old+j].timestamp = subevents[id][j].timestamp;
subevents[id][evts_old+j].detID = subevents[id][j].detID;
for (i=0; i<subevents[id][evts_old+j].length; i++) {
subevents[id][evts_old+j].data[i]=subevents[id][j].data[i];
}
@ -295,6 +348,7 @@ int main(int argc, char **argv) {
totmem -= sizeof(unsigned int)*subevents[id][j].length;
subevents[id][j].length = 0;
subevents[id][j].timestamp = 0;
subevents[id][j].detID = -1;
}
}
@ -309,12 +363,11 @@ int main(int argc, char **argv) {
//fill buffers until full (online mode will stop filling buffers after 2.5 sec lag betweeen output/input)
if ( nevts[id] < maxevts[id] && ( (time - time_old)/10000000 < 25 || online == 0 ) ) {
j = nevts[id] + iptr[id];
if (j >= maxevts[id]) j -= maxevts[id];
subevents[id][j].timestamp = time;
subevents[id][j].detID = mapping[id];
if (subevents[id][j].data == NULL) {
subevents[id][j].data = (unsigned int *) malloc(sizeof(unsigned int)*length);
@ -323,8 +376,7 @@ int main(int argc, char **argv) {
return -1;
}
totmem += sizeof(unsigned int)*length;
}
else if (length != subevents[id][j].length) { //not needed anymore since always free data after use now. Keep for future ...
}else if (length != subevents[id][j].length) { //not needed anymore since always free data after use now. Keep for future ...
subevents[id][j].data = (unsigned int *) realloc(subevents[id][j].data, sizeof(unsigned int)*length);
if (subevents[id][j].data == NULL) {
printf("realloc failed\n");
@ -347,16 +399,98 @@ int main(int argc, char **argv) {
nevts[id]++;
evts_tot_read++;
}
else {
}else {
pause = 1;
break;
}
} // end while for fill buffers
} // end while for fill buffers maxevts[id]
/////////
//######################## FSU
// 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
timemin_old = timemin;
@ -367,8 +501,7 @@ int main(int argc, char **argv) {
timemin = subevents[i][iptr[i]].timestamp;
time_old = timemin;
min_id = i;
}
else if (subevents[i][iptr[i]].timestamp < timemin) {
}else if (subevents[i][iptr[i]].timestamp < timemin) {
timemin = subevents[i][iptr[i]].timestamp;
time_old = timemin;
min_id = i;
@ -376,13 +509,13 @@ int main(int argc, char **argv) {
}
}
if (timemin > -1) {
if (timemin < timemin_old) {
printf("\nWarning!!! timemin = %lld and timemin_old = %lld and min_id = %d\n", timemin, timemin_old, min_id);
outoforder++;
}
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);
//free data memory up until it's needed again
@ -395,16 +528,16 @@ int main(int argc, char **argv) {
nevts[min_id]--;
if (++iptr[min_id] >= maxevts[min_id]) iptr[min_id] -= maxevts[min_id];
evts_tot_write++;
}
else break;
}else break;
/////////
}
//print statistics
//e_div=div(evts_tot_read,10000);
//if ( e_div.rem == 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) : diff (\x1B[31m%lld\x1B[0m)\r", (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)\r",
(totmem)/1024/1024, evts_tot_read, evts_tot_write, evts_tot_drop, evts_tot_read-evts_tot_write - evts_tot_drop);
} //end main while

View File

@ -102,13 +102,14 @@ int main(int argc, char **argv) {
int countGP = 0; //gamma-particle coincident
double totalDataSize = 0;
int outFileCount = 0;
for( int i = 0; i < nFile; i++){
evt->OpenFile(inFileName[i]);
if( evt->IsOpen() == false ) continue;
printf("==================================================== %d / %d\n", i, nFile);
printf("==================================================== %d / %d\n", i+1, nFile);
printf("\033[1;31m%s \033[m\n", inFileName[i].Data());
int pos = inFileName[i].Last('/');
@ -141,13 +142,13 @@ int main(int argc, char **argv) {
if (tdif > timeWindow) {
//Gate
if( multiCry > 0 && multiGagg > 0 ) {
//if( multiCry > 2 && multiGagg ==0 ) {
//
outRootFile->cd();
tree->Fill();
countGP++;
}
//
// countGP++;
//}
evID ++;
@ -200,7 +201,37 @@ int main(int argc, char **argv) {
printf(" ----------- root file size : %.3f GB\n", rootFileSize);
printf(" ---------- total read size : %.3f GB\n", totalDataSize);
printf(" ----------- reduction rate : %.3f %%\n", rootFileSize*100./totalDataSize);
if( rootFileSize > 3.0 ) break;
if( rootFileSize > 3.0 ) {
break;
}
///try to open a new root file when file size > 2 GB
/*if( rootFileSize > 2.0 ) {
outRootFile->Close();
delete outRootFile;
delete tree;
outFileCount += 1;
if( outFileCount > 5 ) break;
TString outFileName2 = outFileName;
outFileName2.Insert(outFileName.Sizeof() - 6, Form("_%03d",outFileCount));
outRootFile = new TFile( outFileName2, "recreate");
tree = new TTree("tree", "tree");
tree->Branch("evID", &evID, "event_ID/l");
tree->Branch("multi", &multi, "multi/I");
tree->Branch("detID", id, "detID[multi]/I");
tree->Branch("e", e, "e[multi]/D");
tree->Branch("e_t", e_t, "e_timestamp[multi]/l");
tree->Branch("qdc", qdc, "qdc[multi][8]/I");
tree->Branch("multiCry", &multiCry, "multiplicity_crystal/I");
tree->Branch("multiGagg", &multiGagg, "multiplicity_GAGG/I");
tree->Branch("runID", &runID, "runID/I");
}*/
}