Gitlab will migrate to a new storage backend starting 0300 UTC on 2020-04-04. We do not anticipate a maintenance window for this migration. Performance may be impacted over the weekend. Thanks for your patience.

LALInferenceBench.c 5.28 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
/*
 *  LALInferenceBench.c:  Benchmark LALInference functions
 *
 *  Copyright (C) 2015 John Veitch
 *
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with with program; see the file COPYING. If not, write to the
 *  Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 */

#include <stdio.h>
#include <lal/LALInference.h>
#include <lal/LALInferenceInit.h>
#include <lal/LALInferenceReadData.h>
#include <lal/LALInferenceCalibrationErrors.h>
#include <lal/LALInferenceLikelihood.h>
#include <lal/LALInferenceTemplate.h>
#include <sys/resource.h>

#ifdef __GNUC__
#define UNUSED __attribute__ ((unused))
#else
#define UNUSED
#endif

const char HELPSTR[]=\
"lalinference_bench: Benchmark template and likelihood functions.\n\
 Options:\n\
    --Niter            : Number of calls to time (delfault 1000) \n\
    --bench-template   : Only benchmark template function\n\
    --bench-likelihood : Only benchmark likelihood function\n\
                         (defaults to benchmarking both)\n\
 Example (for 1.0-1.0 binary with seglen 8, srate 4096): \n\
 $ ./lalinference_bench --psdlength 1000 --psdstart 1 --seglen 8 --srate 4096 --trigtime 0 --ifo H1 --H1-channel LALSimAdLIGO --H1-cache LALSimAdLIGO --dataseed 1324 --Niter 10000 --fix-chirpmass 1.218 --fix-q 1.0\n\n\n\
";

void fprintf_bench(FILE *fp, struct rusage start, struct rusage end, UINT4 Niter);
void fprintf_bench(FILE *fp, struct rusage start, struct rusage end, UINT4 Niter)
{
  REAL8 utime = (end.ru_utime.tv_sec - start.ru_utime.tv_sec) + 1e-6 * (end.ru_utime.tv_usec - start.ru_utime.tv_usec);
  REAL8 stime = (end.ru_stime.tv_sec - start.ru_stime.tv_sec) + 1e-6 * (end.ru_stime.tv_usec - start.ru_stime.tv_usec);
  
  fprintf(fp,"USER Total: %lf s\n",utime);
  fprintf(fp,"USER Per iteration: %e s\n",utime / (double) Niter);
  
  fprintf(fp,"SYS Total: %lf s\n",stime);
  fprintf(fp,"SYS Per iteration: %e s\n",stime / (double) Niter);
}

void LALInferenceTemplateNoop(UNUSED LALInferenceModel *model);
void LALInferenceTemplateNoop(UNUSED LALInferenceModel *model)
{
  return;
}

void bench_likelihood(LALInferenceRunState *runState,UINT4 Niter);
void bench_likelihood(LALInferenceRunState *runState,UINT4 Niter)
{
  UINT4 i=0;
  struct rusage r_usage_start,r_usage_end;
  
  /* Clear the template */
  LALInferenceTemplateNullFreqdomain(runState->threads[0]->model);
  
  LALInferenceTemplateFunction old_templt=runState->threads[0]->model->templt;
  runState->threads[0]->model->templt=LALInferenceTemplateNoop;
  
  fprintf(stdout,"Benchmarking likelihood:\n");
  getrusage(RUSAGE_SELF, &r_usage_start);
  for(i=0;i<Niter;i++)
  {
    runState->likelihood(runState->threads[0]->model->params,runState->data, runState->threads[0]->model);
  }
  getrusage(RUSAGE_SELF, &r_usage_end);
  fprintf_bench(stdout, r_usage_start, r_usage_end, Niter);
  runState->threads[0]->model->templt=old_templt;
  
}

void bench_template(LALInferenceRunState *runState, UINT4 Niter);
void bench_template(LALInferenceRunState *runState, UINT4 Niter)
{
  UINT4 i=0;
  struct rusage r_usage_start,r_usage_end;
  fprintf(stdout,"Benchmarking template:\n");
  getrusage(RUSAGE_SELF, &r_usage_start);
  for(i=0;i<Niter;i++)
  {
    runState->threads[0]->model->templt(runState->threads[0]->model);
  }
  getrusage(RUSAGE_SELF, &r_usage_end);
  fprintf_bench(stdout, r_usage_start, r_usage_end, Niter);
}

int main(int argc, char *argv[]){
  ProcessParamsTable *procParams = NULL,*ppt=NULL;
  LALInferenceRunState *runState=NULL;
  UINT4 Niter=1000;
  UINT4 bench_L=1;
  UINT4 bench_T=1;
  
  procParams=LALInferenceParseCommandLine(argc,argv);

  if(LALInferenceGetProcParamVal(procParams,"--help"))
  {
    fprintf(stdout,"%s",HELPSTR);
    bench_T=bench_L=0;
  }
  if((ppt=LALInferenceGetProcParamVal(procParams,"--Niter")))
     Niter=atoi(ppt->value);
  if(LALInferenceGetProcParamVal(procParams,"--bench-template"))
  {
    bench_T=1; bench_L=0;
  }
  if(LALInferenceGetProcParamVal(procParams,"--bench-likelihood"))
  {
    bench_T=0; bench_L=1;
  }

  
  runState = LALInferenceInitRunState(procParams);

  if(runState) {
    LALInferenceInjectInspiralSignal(runState->data,runState->commandLine);

    /* Simulate calibration errors */
    LALInferenceApplyCalibrationErrors(runState->data,runState->commandLine);
  }
  
  /* Set up the template and likelihood functions */
  LALInferenceInitCBCThreads(runState,1);
  LALInferenceInitLikelihood(runState);

146 147 148
  /* Disable waveform caching */
  runState->threads[0]->model->waveformCache=NULL;
  
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
  if(bench_T)
  {
    printf("Template test will run with parameters:\n");
    LALInferencePrintVariables(runState->threads[0]->model->params);
    printf("\n");

    bench_template(runState,Niter);
    printf("\n");
  }
  if(bench_L)
  {
    bench_likelihood(runState,Niter);
    printf("\n");
  }
  
  return(0);
}