From 21332566d04f5ca49f21b8a7f07a82ba7250a144 Mon Sep 17 00:00:00 2001 From: macrocosme Date: Mon, 7 Dec 2020 20:08:16 +0100 Subject: [PATCH 1/6] Functional omp version with timing output for test --- include/rficlean.h | 10 +-- src/cleanit.c | 199 +++++++++++++++++++++++--------------------- src/rficlean_data.c | 113 ++++++++++++++++++++----- 3 files changed, 200 insertions(+), 122 deletions(-) diff --git a/include/rficlean.h b/include/rficlean.h index ef5dace..194dcf8 100644 --- a/include/rficlean.h +++ b/include/rficlean.h @@ -8,16 +8,16 @@ long byte_offset; int headerless,obits,iflip,nred, iwhite,nints,nsub,maxhist,numhist,nsubchans,tnorm,fnorm,igetstat,nharm,pcl,nsect,zerodm,bl_start,nblocks; char inpfile[128], outfile[128], gminfofile[128],gmhdrfile[128],psfile[100]; FILE *input, *output, *gminfo, *gmhdr; -double tempra,tempdec, psrf, *last_mspec, *wrms, *wmean, *ai; +double tempra,tempdec, psrf, *last_mspec, **wrms, **wmean, **ai; float forcefthresh,fthresh,rthresh,sthresh,last_tvar,last_fvar,chanfrac,sampfrac,clipthresh; float *fftstat, *chanstat, *predist, *xpredist, *postdist, *xpostdist, *finaldist, *xfinaldist ; double *tfvar, *tfmean,meanvar,rmsvar ; -double *chandata, *mspec, *rspec, *vspec, *wspec, *wt; +double **chandata, *mspec, *rspec, *vspec, *wspec, *wt; long int *coff; -fftw_complex *in; -fftw_complex *out; -fftw_plan fplan, bplan; +fftw_complex **in; +fftw_complex **out; +fftw_plan *fplan, *bplan; int strings_equal (char *string1, char *string2); diff --git a/src/cleanit.c b/src/cleanit.c index c898aa7..4a302f4 100644 --- a/src/cleanit.c +++ b/src/cleanit.c @@ -1,16 +1,17 @@ /* * cleanit --- the actual cleaning/processing of data. - * - * + * + * * Yogesh Maan 2018. - * - * + * + * */ #include #include #include #include "rficlean.h" #include +#include //-------------------------------------------------------------- int all_samef(double *arr, long int np) @@ -133,7 +134,7 @@ void fftclean(fftw_complex *in, fftw_complex *out, long int npts, int ioff) nfft = npts ; nfft2 = nfft/2; - fftw_execute ( fplan ); + fftw_execute ( fplan[omp_get_thread_num()] ); anfft = (double)nfft; for (i=0;iath || fabs(out[i][1])>bth) && psrifs[i]>0 ){ - fftstat[ioff+i] = fftstat[ioff+i] + 1.0; + fftstat[ioff+i] += 1.0; for (j=i,ih=1; ih<=nharm && jath) ai[i] = (ai[i]/fabs(ai[i]))*ath; + if( fabs(ai[omp_get_thread_num()][i])>ath) ai[omp_get_thread_num()][i] = (ai[omp_get_thread_num()][i]/fabs(ai[omp_get_thread_num()][i]))*ath; } - for (i=0;iath) ai[i] = m1; + if( fabs((ai[omp_get_thread_num()][i]-m1))>ath) ai[omp_get_thread_num()][i] = m1; } - for (i=0;iath){ + if( fabs(ai[omp_get_thread_num()][i]-m1)>ath){ wt[i] = -1.0; wt[i+1] = -1.0; } @@ -298,17 +299,17 @@ void tsfind(double *ttdata, long int npts, float thresh, double *wt) double m1,r1,m2; - for (i=0;iath){ + if( fabs((ai[omp_get_thread_num()][i]-m1))>ath){ wt[i] = -1.0; } } @@ -358,31 +359,35 @@ void cleanit(float *data, int nchans, long int nadd) // get some pre-cleaning statistics for (channum=0; channum0){ + if(pcl>0) { kk = (int)(0.9*nchans); for (i=kk;i 0.0) { lg = mspec[i]; lgr = rspec[i]; break; } } - for (i=0;ichanfrac){ + for (ii=0; iichanfrac) { + #pragma omp parallel for private(channum,ii,jj) collapse(2) for (channum=0; channum 0.0) last_mspec[ii] = mspec[ii];} + for (ii=0; ii 0.0) last_mspec[ii] = mspec[ii]; } - + // Try clipping some channels in individual samples + #pragma omp parallel for private(t,c,nxc) schedule(dynamic) for (t=0; t0) isame = isame + 1; + for (c=0; c0) isame = isame + 1; } if(isame >= (int)(0.8*nvar)){ for (c=0; c 2018 * */ @@ -12,9 +12,11 @@ #include #include "rficlean.h" #include +#include +#include -void rficlean_data(FILE *input, FILE *output) -{ +void rficlean_data(FILE *input, FILE *output) +{ char string[80],plotdevice[100]; float *fblock,min,max,hpower,realtime,fsaved[2], *ts0dm, atemp; unsigned short *sblock; @@ -22,6 +24,13 @@ void rficlean_data(FILE *input, FILE *output) int nsaved=0,opened=0, wpout=10; long int ns,nsblk,nout,iter,i,j,k, jt1, jt2, iblock; long int itemp, isum, nsize, istart, ii, jj, kk, n0; + time_t s, start, cpu_time; + clock_t t_global, t_function, t_function_sum; + int i_function; + i_function = 0; + + s = time(NULL); + t_global = clock(); last_tvar = -1.0; last_fvar = -1.0; @@ -41,7 +50,7 @@ void rficlean_data(FILE *input, FILE *output) printf ("\n Preparing all buffers...\n"); /***/ - chandata = (double *) malloc(nsize*sizeof(double)); + // chandata = (double *) malloc(nsize*sizeof(double)); vspec = (double *) malloc(nsize*sizeof(double)); rspec = (double *) malloc(nsize*sizeof(double)); coff = (long int *) malloc((nchans)*sizeof(long int)); @@ -50,17 +59,34 @@ void rficlean_data(FILE *input, FILE *output) wspec = (double *) malloc(nsize*sizeof(double)); /***/ - in = fftw_malloc (sizeof(fftw_complex)*(naddt+5)); - out = fftw_malloc (sizeof(fftw_complex)*(naddt+5)); - fplan = fftw_plan_dft_1d( naddt, in, out,FFTW_FORWARD, FFTW_ESTIMATE ); - bplan = fftw_plan_dft_1d( naddt, in, out,FFTW_BACKWARD, FFTW_ESTIMATE ); - ai = (double *) malloc(sizeof(double)*(nsize)); + // in = fftw_malloc (sizeof(fftw_complex)*(naddt+5)); + // out = fftw_malloc (sizeof(fftw_complex)*(naddt+5)); + // fplan = fftw_plan_dft_1d( naddt, in, out, FFTW_FORWARD, FFTW_ESTIMATE ); + // bplan = fftw_plan_dft_1d( naddt, in, out, FFTW_BACKWARD, FFTW_ESTIMATE ); + // ai = (double *) malloc(sizeof(double)*(nsize)); psrifs = (long int *) malloc(sizeof(long int)*(naddt)); + #ifdef _OPENMP + if (fftw_init_threads()) fftw_plan_with_nthreads(omp_get_max_threads()); + #endif + chandata = (double **) malloc(omp_get_max_threads() * sizeof(double *)); + in = (fftw_complex**) malloc (omp_get_max_threads() * sizeof(fftw_complex*)); + out = (fftw_complex**) malloc (omp_get_max_threads() * sizeof(fftw_complex*)); + fplan = (fftw_plan*) malloc (omp_get_max_threads() * sizeof(fftw_plan)); + bplan = (fftw_plan*) malloc (omp_get_max_threads() * sizeof(fftw_plan)); + ai = (double **) malloc(omp_get_max_threads() * sizeof(double *)); + for (i=0; i Date: Mon, 7 Dec 2020 20:14:25 +0100 Subject: [PATCH 2/6] Add threads and omp --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index b11637e..e619edb 100644 --- a/Makefile +++ b/Makefile @@ -6,8 +6,8 @@ BINDIR=bin MYBIN=/home/maan/pulsar_softwares/bin/ CC=gcc -CFLAGS=-I$(IDIR) -Wno-unused-result -O3 -march=native -LIBS=-lm -lfftw3 -lcpgplot +CFLAGS=-I$(IDIR) -Wno-unused-result -O3 -march=native -fopenmp +LIBS=-lm -lfftw3_threads -lfftw3 -lcpgplot _DEPS = header.h rficlean.h version.h DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS)) From ee6dcda7645df48d8f49fc0575b3580215d53150 Mon Sep 17 00:00:00 2001 From: macrocosme Date: Mon, 11 Jan 2021 15:34:04 +0100 Subject: [PATCH 3/6] Adding ifdefs _OPENMP --- src/cleanit.c | 186 ++++++++++++++++++++++++++------------------ src/rficlean_data.c | 34 ++++---- 2 files changed, 131 insertions(+), 89 deletions(-) diff --git a/src/cleanit.c b/src/cleanit.c index 4a302f4..839efef 100644 --- a/src/cleanit.c +++ b/src/cleanit.c @@ -134,7 +134,15 @@ void fftclean(fftw_complex *in, fftw_complex *out, long int npts, int ioff) nfft = npts ; nfft2 = nfft/2; - fftw_execute ( fplan[omp_get_thread_num()] ); + + #ifdef _OPENMP + int thread_num = omp_get_thread_num(); + #else + int thread_num=0; + #endif + + fftw_execute ( fplan[thread_num] ); + anfft = (double)nfft; for (i=0;iath || fabs(out[i][1])>bth) && psrifs[i]>0 ){ fftstat[ioff+i] += 1.0; for (j=i,ih=1; ih<=nharm && jath) ai[omp_get_thread_num()][i] = (ai[omp_get_thread_num()][i]/fabs(ai[omp_get_thread_num()][i]))*ath; + if( fabs(ai[thread_num][i])>ath) ai[thread_num][i] = (ai[thread_num][i]/fabs(ai[thread_num][i]))*ath; } - for (i=0;iath) ai[omp_get_thread_num()][i] = m1; + if( fabs((ai[thread_num][i]-m1))>ath) ai[thread_num][i] = m1; } - for (i=0;iath){ + if( fabs(ai[thread_num][i]-m1)>ath){ wt[i] = -1.0; wt[i+1] = -1.0; } @@ -298,18 +324,23 @@ void tsfind(double *ttdata, long int npts, float thresh, double *wt) double anpt,ath; double m1,r1,m2; - - for (i=0;iath){ + if( fabs((ai[thread_num][i]-m1))>ath){ wt[i] = -1.0; } } @@ -355,6 +386,7 @@ void cleanit(float *data, int nchans, long int nadd) float thresh; double lg,lgr, an, atemp; + int thread_num=0; // get some pre-cleaning statistics @@ -362,32 +394,35 @@ void cleanit(float *data, int nchans, long int nadd) #pragma omp parallel for private(channum,inc,isame,ii,jj) schedule(dynamic) for (channum=0; channum0) isame = isame + 1; + for (c=0; c0) isame = isame + 1; } if(isame >= (int)(0.8*nvar)){ for (c=0; c Date: Mon, 11 Jan 2021 19:18:08 +0100 Subject: [PATCH 4/6] Adding ifdefs _OPENMP to includes --- src/cleanit.c | 2 ++ src/rficlean_data.c | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/cleanit.c b/src/cleanit.c index 839efef..0d2ae6d 100644 --- a/src/cleanit.c +++ b/src/cleanit.c @@ -11,7 +11,9 @@ #include #include "rficlean.h" #include +#ifdef _OPENMP #include +#endif //-------------------------------------------------------------- int all_samef(double *arr, long int np) diff --git a/src/rficlean_data.c b/src/rficlean_data.c index 4676a5f..f7c5bfd 100644 --- a/src/rficlean_data.c +++ b/src/rficlean_data.c @@ -12,8 +12,10 @@ #include #include "rficlean.h" #include -#include #include +#ifdef _OPENMP +#include +#endif void rficlean_data(FILE *input, FILE *output) { From 48b9f9235a586da029d421b6237fb9d592c49585 Mon Sep 17 00:00:00 2001 From: macrocosme Date: Wed, 17 Feb 2021 10:25:56 +0100 Subject: [PATCH 5/6] Fix double pointer, missing bracket, timing print --- include/rficlean.h | 2 +- src/cleanit.c | 1 + src/rficlean_data.c | 1 - 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/rficlean.h b/include/rficlean.h index e4916fe..d55ae05 100644 --- a/include/rficlean.h +++ b/include/rficlean.h @@ -9,7 +9,7 @@ long byte_offset; int headerless,obits,iflip,nred, iwhite,nints,nsub,maxhist,numhist,nsubchans,tnorm,fnorm,igetstat,nharm,pcl,nsect,zerodm,bl_start,nblocks; char inpfile[128], outfile[128], gminfofile[128],gmhdrfile[128],psfile[100]; FILE *input, *output, *gminfo, *gmhdr; -double tempra,tempdec, psrf, psrfdf, *last_mspec, *wrms, *wmean, *ai; +double tempra,tempdec, psrf, psrfdf, *last_mspec, **wrms, **wmean, **ai; float forcefthresh,fthresh,rthresh,sthresh,last_tvar,last_fvar,chanfrac,sampfrac,clipthresh; float *fftstat, *chanstat, *predist, *xpredist, *postdist, *xpostdist, *finaldist, *xfinaldist ; double *tfvar, *tfmean,meanvar,rmsvar ; diff --git a/src/cleanit.c b/src/cleanit.c index 1260ee1..15cae67 100644 --- a/src/cleanit.c +++ b/src/cleanit.c @@ -525,6 +525,7 @@ void cleanit(float *data, int nchans, long int nadd) if(wt[t] < 0.0){ for (c=0; c Date: Wed, 31 Mar 2021 19:22:36 +0200 Subject: [PATCH 6/6] Add missing thread_num as omp private --- src/cleanit.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cleanit.c b/src/cleanit.c index 15cae67..2318121 100644 --- a/src/cleanit.c +++ b/src/cleanit.c @@ -394,7 +394,7 @@ void cleanit(float *data, int nchans, long int nadd) for (channum=0; channum