int main(int argc, char **argv) { enum YepStatus status; Yep64u startTimeNaive, startTimeYeppp, endTimeNaive, endTimeYeppp, frequency; Yep64f entropyNaive, entropyYeppp; YepSize i; /* Allocate an array of probabilities */ Yep64f *p = (Yep64f*)calloc(ARRAY_SIZE, sizeof(Yep64f)); /* Initialize the Yeppp! library */ status = yepLibrary_Init(); assert(status == YepStatusOk); /* Populate the array of probabilities with random probabilities */ for (i = 0; i < ARRAY_SIZE; i++) { /* 0 < p[i] <= 1.0 */ p[i] = ((double)(rand() + 1)) / ((double)(RAND_MAX) + 1.0); } /* Retrieve the number of timer ticks per second */ status = yepLibrary_GetTimerFrequency(&frequency); assert(status == YepStatusOk); /* Retrieve the number of timer ticks before calling naive entropy computation */ status = yepLibrary_GetTimerTicks(&startTimeNaive); assert(status == YepStatusOk); /* Compute entropy using naive implementation */ entropyNaive = compute_entropy_naive(p, ARRAY_SIZE); /* Retrieve the number of timer ticks after calling naive entropy computation */ status = yepLibrary_GetTimerTicks(&endTimeNaive); assert(status == YepStatusOk); /* Retrieve the number of timer ticks before calling Yeppp!-based entropy computation */ status = yepLibrary_GetTimerTicks(&startTimeYeppp); assert(status == YepStatusOk); /* Compute entropy using Yeppp!-based implementation */ entropyYeppp = compute_entropy_yeppp(p, ARRAY_SIZE); /* Retrieve the number of timer ticks after calling Yeppp!-based entropy computation */ status = yepLibrary_GetTimerTicks(&endTimeYeppp); assert(status == YepStatusOk); /* Report the results */ printf("Naive implementation:\n"); printf("\tEntropy = %lf\n", ((double)entropyNaive)); printf("\tTime = %lf\n", ((double)(endTimeNaive - startTimeNaive)) / ((double)(frequency))); printf("Yeppp! implementation:\n"); printf("\tEntropy = %lf\n", ((double)entropyYeppp)); printf("\tTime = %lf\n", ((double)(endTimeYeppp - startTimeYeppp)) / ((double)(frequency))); /* Deinitialize the Yeppp! library */ status = yepLibrary_Release(); assert(status == YepStatusOk); /* Release the memory for probabilities array */ free(p); return 0; }
// Main searching function (loops inside) void search( Search_settings *sett, Command_line_opts *opts, Search_range *s_range, FFTW_plans *plans, FFTW_arrays *fftw_arr, Aux_arrays *aux, int *FNum ) { // struct stat buffer; struct flock lck; int pm, mm, nn; // hemisphere, sky positions int sgnlc=0; // number of candidates FLOAT_TYPE *sgnlv; // array with candidates data char outname[512]; int fd, status; FILE *state; #ifdef YEPPP status = yepLibrary_Init(); assert(status == YepStatusOk); #endif #ifdef TIMERS struct timespec tstart = get_current_time(CLOCK_REALTIME), tend; #endif // Allocate buffer for triggers sgnlv = (FLOAT_TYPE *)calloc(NPAR*2*sett->nfft, sizeof(FLOAT_TYPE)); state = NULL; if(opts->checkp_flag) state = fopen (opts->qname, "w"); /* Loop over hemispheres */ for (pm=s_range->pst; pm<=s_range->pmr[1]; ++pm) { sprintf (outname, "%s/triggers_%03d_%04d%s_%d.bin", opts->prefix, opts->ident, opts->band, opts->label, pm); /* Two main loops over sky positions */ for (mm=s_range->mst; mm<=s_range->mr[1]; ++mm) { for (nn=s_range->nst; nn<=s_range->nr[1]; ++nn) { if(opts->checkp_flag) { ftruncate(fileno(state), 0); fprintf(state, "%d %d %d %d %d\n", pm, mm, nn, s_range->sst, *FNum); fseek(state, 0, SEEK_SET); } /* Loop over spindowns is inside job_core() */ status = job_core( pm, // hemisphere mm, // grid 'sky position' nn, // other grid 'sky position' sett, // search settings opts, // cmd opts s_range, // range for searching plans, // fftw plans fftw_arr, // arrays for fftw aux, // auxiliary arrays &sgnlc, // current number of candidates sgnlv, // candidate array FNum); // candidate signal number // Get back to regular spin-down range s_range->sst = s_range->spndr[0]; /* Add trigger parameters to a file */ // if enough signals found (no. of signals > half length of buffer) if (sgnlc > sett->nfft) { if((fd = open (outname, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR| S_IWUSR|S_IRGRP|S_IROTH)) < 0) { perror(outname); return; } #ifdef USE_LOCKING lck.l_type = F_WRLCK; lck.l_whence = 0; lck.l_start = 0L; lck.l_len = 0L; if (fcntl (fd, F_SETLKW, &lck) < 0) perror ("fcntl()"); #endif write(fd, (void *)(sgnlv), sgnlc*NPAR*sizeof(FLOAT_TYPE)); if (close(fd) < 0) perror ("close()"); sgnlc=0; } /* if sgnlc > sett-nfft */ } // for nn s_range->nst = s_range->nr[0]; } // for mm s_range->mst = s_range->mr[0]; // Write the leftover from the last iteration of the buffer if((fd = open(outname, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR| S_IWUSR|S_IRGRP|S_IROTH)) < 0) { perror(outname); return; } #ifdef USE_LOCKING lck.l_type = F_WRLCK; lck.l_whence = 0; lck.l_start = 0L; lck.l_len = 0L; if (fcntl (fd, F_SETLKW, &lck) < 0) perror ("fcntl()"); #endif write(fd, (void *)(sgnlv), sgnlc*NPAR*sizeof(FLOAT_TYPE)); if (close(fd) < 0) perror ("close()"); sgnlc=0; } // for pm //#mb state file has to be modified accordingly to the buffer if(opts->checkp_flag) fclose(state); // Free triggers buffer free(sgnlv); #ifdef TIMERS tend = get_current_time(CLOCK_REALTIME); double time_elapsed = get_time_difference(tstart, tend); printf("Time elapsed: %e s\n", time_elapsed); #endif }
//mesh adaptive direct search (MADS) maximum search declaration //double Yep64f* MADS(Search_settings *sett, Aux_arrays *aux, double* in, double *start, double delta, double *pc, int bins){ int i, j, k, l, m, n, o, r, a = 0; double sinalt, cosalt, sindelt, cosdelt; double param[4]; //initial size of mesh double smallest = 100; double **sigaa, **sigbb; // aa[nifo][N] sigaa = matrix(sett->nifo, sett->N); sigbb = matrix(sett->nifo, sett->N); for(r = 0; r < 4;r++){ param[r] = pc[r]/bins; } for(r = 0; r < 4; r++){ if(param[r] < smallest) smallest = param[r]; } #ifdef YEPPP yepLibrary_Init(); Yep64f *p = (Yep64f*)malloc(sizeof(Yep64f)*4); Yep64f *out = (Yep64f*)malloc(sizeof(Yep64f)*11); Yep64f *nSource = (Yep64f*)malloc(sizeof(Yep64f)*3); Yep64f *extr = (Yep64f*)malloc(sizeof(Yep64f)*11); Yep64f *res = (Yep64f*)malloc(sizeof(Yep64f)*11); enum YepStatus status; #endif // puts("MADS"); for(i = 0; i < 11; i++) extr[i] = in[i]; while(smallest >= delta){ //when to end computations k = 0; for(j = 0; j < 8; j++){ for(i = 0; i < 4; i++) p[i] = in[6+i]; if(j < 4){ p[k] = in[6+k] - start[k]*(param[k] - delta); k++; } else { k--; p[k] = in[6+k] + start[k]*(param[k] - delta); } sinalt = sin(p[3]); cosalt = cos(p[3]); sindelt = sin(p[2]); cosdelt = cos(p[2]); nSource[0] = cosalt*cosdelt; nSource[1] = sinalt*cosdelt; nSource[2] = sindelt; for (o = 0; o < sett->nifo; ++o){ modvir(sinalt, cosalt, sindelt, cosdelt, sett->N, &ifo[o], aux, sigaa[o], sigbb[o]); } res = Fstatnet(sett, p, nSource, sigaa, sigbb); //Fstat function for mesh points if (res[5] < extr[5]){ for (l = 0; l < 11; l++){ extr[l] = res[l]; } } } //j if (extr[5] == in[5]){ smallest = smallest - delta; for(r = 0; r < 4; r++){ param[r] = param[r] - delta; } } else{ for(m = 0; m < 4; m++) p[m] = extr[6+m]; for(m = 0; m < 11; m++) in[m] = extr[m]; } } // while for (n= 0; n < 11; n++){ out[n] = extr[n]; } free(p); free(nSource); free(extr); free(res); free_matrix(sigaa, sett->nifo, sett->N); free_matrix(sigbb, sett->nifo, sett->N); return out; }
// Main programm int main (int argc, char *argv[]) { Search_settings sett; Command_line_opts opts; Search_range s_range; Aux_arrays aux_arr; double *F; // F-statistic array int i, j, r, c, a, b, g; int d, o, m, k; int bins = 2, ROW, dim = 4; // neighbourhood of point will be divide into defined number of bins double pc[4]; // % define neighbourhood around each parameter for initial grid double pc2[4]; // % define neighbourhood around each parameter for direct maximum search (MADS & Simplex) double tol = 1e-10; // double delta = 1e-5; // initial step in MADS function // double *results; // Vector with results from Fstatnet function // double *maximum; // True maximum of Fstat // double results_max[11]; double s1, s2, s3, s4; double sgnlo[4]; double **arr; // arr[ROW][COL], arrg[ROW][COL]; double nSource[3]; double sinalt, cosalt, sindelt, cosdelt; double F_min; char path[512]; double x, y; ROW = pow((bins+1),4); #ifdef YEPPP yepLibrary_Init(); Yep64f *results_max = (Yep64f*)malloc(sizeof(Yep64f)*11); Yep64f *results_first = (Yep64f*)malloc(sizeof(Yep64f)*11); Yep64f *results = (Yep64f*)malloc(sizeof(Yep64f)*11); Yep64f *maximum = (Yep64f*)malloc(sizeof(Yep64f)*11); // Yep64f *sgnlo = (Yep64f*)malloc(sizeof(Yep64f)*4); // Yep64f *nSource = (Yep64f*)malloc(sizeof(Yep64f)*3); Yep64f *mean = (Yep64f*)malloc(sizeof(Yep64f)*4); enum YepStatus status; #endif pc[0] = 0.015; pc[1] = 0.015; pc[2] = 0.015; pc[3] = 0.015; for (i = 0; i < 4; i++){ pc2[i] = 2*pc[i]/bins; } // Time tests double tdiff; clock_t tstart, tend; // Command line options handle_opts(&sett, &opts, argc, argv); // Output data handling /* struct stat buffer; if (stat(opts.prefix, &buffer) == -1) { if (errno == ENOENT) { // Output directory apparently does not exist, try to create one if(mkdir(opts.prefix, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1) { perror (opts.prefix); return 1; } } else { // can't access output directory perror (opts.prefix); return 1; } } */ sprintf(path, "%s/candidates.coi", opts.dtaprefix); //Glue function if(strlen(opts.glue)) { glue(&opts); sprintf(opts.dtaprefix, "./data_total"); sprintf(opts.dtaprefix, "%s/followup_total_data", opts.prefix); opts.ident = 000; } FILE *coi; int z; if ((coi = fopen(path, "r")) != NULL) { // while(!feof(coi)) { /* if(!fread(&w, sizeof(unsigned short int), 1, coi)) { break; } fread(&mean, sizeof(float), 5, coi); fread(&fra, sizeof(unsigned short int), w, coi); fread(&ops, sizeof(int), w, coi); if((fread(&mean, sizeof(float), 4, coi)) == 4){ */ while(fscanf(coi, "%le %le %le %le", &mean[0], &mean[1], &mean[2], &mean[3]) == 4){ //Time test // tstart = clock(); arr = matrix(ROW, 4); //Function neighbourhood - generating grid around point arr = neigh(mean, pc, bins); // Output data handling /* struct stat buffer; if (stat(opts.prefix, &buffer) == -1) { if (errno == ENOENT) { // Output directory apparently does not exist, try to create one if(mkdir(opts.prefix, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1) { perror (opts.prefix); return 1; } } else { // can't access output directory perror (opts.prefix); return 1; } } */ // Grid data if(strlen(opts.addsig)) { read_grid(&sett, &opts); } // Search settings search_settings(&sett); // Detector network settings detectors_settings(&sett, &opts); // Array initialization init_arrays(&sett, &opts, &aux_arr, &F); // Amplitude modulation functions for each detector for(i=0; i<sett.nifo; i++) rogcvir(&ifo[i]); // Adding signal from file if(strlen(opts.addsig)) { add_signal(&sett, &opts, &aux_arr, &s_range); } // Setting number of using threads (not required) omp_set_num_threads(1); results_max[5] = 0.; // ifo zostaje shared // ifo....shft i ifo....xdatm{a,b] prerobić na lokalne tablice w fstatnet // w regionie parallel wprowadzić tablice private aa i bb ; alokować i przekazywać je jako argumenty do fstatnet i amoeba // Main loop - over all parameters + parallelisation #pragma omp parallel default(shared) private(d, i, sgnlo, sinalt, cosalt, sindelt, cosdelt, nSource, results, maximum) { double **sigaa, **sigbb; // aa[nifo][N] sigaa = matrix(sett.nifo, sett.N); sigbb = matrix(sett.nifo, sett.N); #pragma omp for for (d = 0; d < ROW; ++d){ for (i = 0; i < 4; i++){ sgnlo[i] = arr[d][i]; // sgnlo[i] = mean[i]; } sinalt = sin(sgnlo[3]); cosalt = cos(sgnlo[3]); sindelt = sin(sgnlo[2]); cosdelt = cos(sgnlo[2]); nSource[0] = cosalt*cosdelt; nSource[1] = sinalt*cosdelt; nSource[2] = sindelt; for (i = 0; i < sett.nifo; ++i){ modvir(sinalt, cosalt, sindelt, cosdelt, sett.N, &ifo[i], &aux_arr, sigaa[i], sigbb[i]); } // F-statistic in given point results = Fstatnet(&sett, sgnlo, nSource, sigaa, sigbb); //printf("Fstatnet: %le %le %le %le %le %le\n", results[6], results[7], results[8], results[9], results[5], results[4]); #pragma omp critical if(results[5] < results_max[5]){ for (i = 0; i < 11; i++){ results_max[i] = results[i]; } } // Maximum search using simplex algorithm if(opts.simplex_flag){ // puts("Simplex"); maximum = amoeba(&sett, &aux_arr, sgnlo, nSource, results, dim, tol, pc2, sigaa, sigbb); printf("Amoeba: %le %le %le %le %le %le\n", maximum[6], maximum[7], maximum[8], maximum[9], maximum[5], maximum[4]); // Maximum value in points searching #pragma omp critical if(maximum[5] < results_max[5]){ for (i = 0; i < 11; i++){ results_max[i] = maximum[i]; } } } //simplex } // d - main outside loop free_matrix(sigaa, sett.nifo, sett.N); free_matrix(sigbb, sett.nifo, sett.N); } //pragma for(g = 0; g < 11; g++) results_first[g] = results_max[g]; // Maximum search using MADS algorithm if(opts.mads_flag) { // puts("MADS"); maximum = MADS(&sett, &aux_arr, results_max, mean, tol, pc2, bins); } //Time test // tend = clock(); // tdiff = (tend - tstart)/(double)CLOCKS_PER_SEC; printf("%le %le %le %le %le %le\n", results_max[6], results_max[7], results_max[8], results_max[9], results_max[5], results_max[4]); } // while fread coi // } } //if coi else { perror (path); return 1; } // Output information /* puts("**********************************************************************"); printf("*** Maximum value of F-statistic for grid is : (-)%.8le ***\n", -results_first[5]); printf("Sgnlo: %.8le %.8le %.8le %.8le\n", results_first[6], results_first[7], results_first[8], results_first[9]); printf("Amplitudes: %.8le %.8le %.8le %.8le\n", results_first[0], results_first[1], results_first[2], results_first[3]); printf("Signal-to-noise ratio: %.8le\n", results_first[4]); printf("Signal-to-noise ratio from estimated amplitudes (for h0 = 1): %.8le\n", results_first[10]); puts("**********************************************************************"); if((opts.mads_flag)||(opts.simplex_flag)){ printf("*** True maximum is : (-)%.8le ***\n", -maximum[5]); printf("Sgnlo for true maximum: %.8le %.8le %.8le %.8le\n", maximum[6], maximum[7], maximum[8], maximum[9]); printf("Amplitudes for true maximum: %.8le %.8le %.8le %.8le\n", maximum[0], maximum[1], maximum[2], maximum[3]); printf("Signal-to-noise ratio for true maximum: %.8le\n", maximum[4]); printf("Signal-to-noise ratio from estimated amplitudes (for h0 = 1) for true maximum: %.8le\n", maximum[10]); puts("**********************************************************************"); }*/ // Cleanup & memory free free(results_max); free(results_first); free(results); free(maximum); free(mean); free_matrix(arr, ROW, 4); cleanup_followup(&sett, &opts, &s_range, &aux_arr, F); return 0; }
// Function computes F-statistics in given point double* Fstatnet(Search_settings *sett, double *sgnlo, double *nSource, double **sigaa, double **sigbb){ int i = 0, n = 0; double aatemp, bbtemp, aa = 0., bb = 0.; complex double exph, xasum, xbsum; double **shft; // old ifo[i].sig.shft[n] complex double **xDatma, **xDatmb; // old ifo[n].sig.xDatma[i], ifo[n].sig.xDatmb[i] shft = matrix(sett->nifo, sett->N); xDatma = matrix_complex(sett->nifo, sett->N); xDatmb = matrix_complex(sett->nifo, sett->N); #ifdef YEPPP int VLEN = sett->N; yepLibrary_Init(); //Yep64f *x = (Yep64f*)calloc(ARRAY_SIZE, sizeof(Yep64f)); Yep64f *_sph = (Yep64f*)malloc(sizeof(Yep64f)*VLEN); Yep64f *_cph = (Yep64f*)malloc(sizeof(Yep64f)*VLEN); Yep64f *phase = (Yep64f*)malloc(sizeof(Yep64f)*VLEN); Yep64f *fstat_out = (Yep64f*)malloc(sizeof(Yep64f)*11); enum YepStatus status; #endif //From jobcore.c, line 237 //Loop for each detector 1 xasum = 0 - I * 0; xbsum = 0 - I * 0; for(n=0; n < sett->nifo; ++n) { // Calculate detector positions with respect to baricenter // Copied from jobcore.c, line 248 //shft & phase loop for(i=0; i < sett->N; ++i) { shft[n][i] = nSource[0]*ifo[n].sig.DetSSB[i*3] + nSource[1]*ifo[n].sig.DetSSB[i*3+1] + nSource[2]*ifo[n].sig.DetSSB[i*3+2]; // Phase modulation function // Copied from jobcore.c, line 265 phase[i] = sgnlo[0]*(double)(i + shft[n][i]) + (sgnlo[1]*i*i) + ((sett->oms + 2.*sgnlo[1]*i)*shft[n][i]); } //shft & phase //Sin & Cos calculations using Yeppp! status = yepMath_Cos_V64f_V64f(phase, _cph, VLEN); assert(status == YepStatusOk); status = yepMath_Sin_V64f_V64f(phase, _sph, VLEN); assert(status == YepStatusOk); // Matched filter // Copied from jobcore.c, line 276 and 337 for (i = 0; i < sett->N; ++i){ exph = _cph[i] - I * _sph[i]; xDatma[n][i] = ifo[n].sig.xDat[i]*sigaa[n][i]*exph; xDatmb[n][i] = ifo[n].sig.xDat[i]*sigbb[n][i]*exph; } } //End of detector loop 1 //Loop for each detector 2 for(n=0; n < sett->nifo; ++n) { aatemp = 0.; bbtemp = 0.; for(i = 0; i < sett->N; i++){ aatemp += sqr(sigaa[n][i]); bbtemp += sqr(sigbb[n][i]); } for(i=0; i < sett->N; ++i) { xDatma[n][i] /= ifo[n].sig.sig2; xDatmb[n][i] /= ifo[n].sig.sig2; xasum += xDatma[n][i]; xbsum += xDatmb[n][i]; } aa += aatemp/ifo[n].sig.sig2; bb += bbtemp/ifo[n].sig.sig2; }// End of detector loop 2 // F - statistic fstat_out[5] = - ((( sqr(creal(xasum)) + sqr(cimag(xasum)))/aa) + ((sqr(creal(xbsum)) + sqr(cimag(xbsum)))/bb)); // Amplitude estimates fstat_out[0] = 2*creal(xasum)/aa; fstat_out[1] = 2*creal(xbsum)/bb; fstat_out[2] = -2*cimag(xasum)/aa; fstat_out[3] = -2*cimag(xbsum)/bb; // Signal-to-noise ratio fstat_out[4] = sqrt(2*(-fstat_out[5]-2)); fstat_out[6] = sgnlo[0]; fstat_out[7] = sgnlo[1]; fstat_out[8] = sgnlo[2]; fstat_out[9] = sgnlo[3]; // Signal-to-noise ratio from estimated amplitudes (for h0 = 1) fstat_out[10] = sqrt(sqr(2*creal(xasum)) + sqr(2*creal(xbsum)) + sqr(2*cimag(xasum)) + sqr(2*cimag(xbsum))); //printf("Fstatnet: %le %le %le %le %le %le\n", fstat_out[6], fstat_out[7], fstat_out[8], fstat_out[9], fstat_out[5], fstat_out[4]); free(_sph); free(_cph); free(phase); free_matrix(shft, sett->nifo, sett->N); free_matrix_complex(xDatma, sett->nifo, sett->N); free_matrix_complex(xDatmb, sett->nifo, sett->N); return fstat_out; }