예제 #1
0
int main(int argc,char **argv)
{char pattern[128],line[128],errbuff[128];
 int error,n,j,k,nloop,acc_loop = 0;
 double before_t,after_t,diff_t,acc_t = 0;
 regex_t *r; regmatch_t *m;
 printf("Copyright 1997 by George J. Carrette.\n");
 printf("Regex test driver. For more info see:\n");
 printf("http://people.delphi.com/gjc/winregex.html\n");
 if ((argc > 1) && (argv[1][0]))
  {if (!(infile = fopen(argv[1],"r")))
    {perror(argv[1]);
     return(1);}}
 r = (regex_t *) malloc(sizeof(regex_t));
 if (prompt("nloop",pattern,sizeof(pattern),"default 1"))
  nloop = atol(pattern);
 if (nloop <= 0) nloop = 1;
 while(prompt("Pattern",pattern,sizeof(pattern),
	          "quit, or try ^([0-9]+)(\\-| |$)(.*)$"))
 {memset(r,0,sizeof(regex_t));
  if ((error = regcomp(r,pattern,REG_EXTENDED)))
   {regerror(error,r,errbuff,sizeof(errbuff));
    printf("regcomp: %s\n",errbuff);}
  else
   {printf("Compiled with %d nsub\n",r->re_nsub);
    n = r->re_nsub + 1;
    m = (regmatch_t *) malloc(sizeof(regmatch_t) * n);
	while(prompt("Data",line,sizeof(line),pattern))
	{before_t = myclock();
	 for(k=0;k<nloop;++k) error = regexec(r,line,n,m,0);
	 after_t = myclock();
	 diff_t = after_t - before_t;
	 acc_loop += nloop;
	 acc_t += diff_t;
	 printf("%d loops, %.3f seconds, %.1f micro-seconds per loop\n",
		    nloop,diff_t,
			diff_t * 1000000 / nloop);
     if (error)
	   {regerror(error,r,errbuff,sizeof(errbuff));
	    printf("regexec: %s\n",errbuff);}
	 else
      for(j=0;j<n;++j)
	   printf("%d[%d,%d] = %.*s\n",
		      j,m[j].rm_so,m[j].rm_eo,
			  (m[j].rm_so >= 0)
			  ? (m[j].rm_eo - m[j].rm_so) : 0,
			  (m[j].rm_so >= 0)
			  ? &line[m[j].rm_so] : "");}
    free(m);
    regfree(r);}}
 free(r);
 if (infile) fclose(infile);
 if (acc_loop)
  printf("%d total loops. %.1f seconds, %.1f micro-seconds per loop\n",
	     acc_loop,acc_t, acc_t * 100000 / acc_loop);
 return(0);}
예제 #2
0
static gboolean
resize_window (GstPipeline * pipeline)
{
  width = (sin (myclock () / 300.0) * 200) + 640;
  height = (sin (myclock () / 300.0) * 200) + 480;

  XResizeWindow (disp, win, width, height);

  XSync (disp, FALSE);

  return TRUE;
}
예제 #3
0
int CDXL_hal::dxl_hal_timeout(void)
{
	long time;

	time = myclock() - glStartTime;

	if(time > gfRcvWaitTime)
		return 1;
	else if(time < 0)
		glStartTime = myclock();

	return 0;
}
예제 #4
0
int main(int argc, char *args[]) {
	screen_t screen;
 

        struct timeb tb;
        struct tm *t;
	
      

	
	init();
	while (1) {

  			ftime(&tb);
			t = localtime(&tb.time);
			clear_clock(screen);
	                	
			doppelpunkt(2,screen);
			myclock(t->tm_hour,t->tm_min, screen);
			
			//putpixel(10,20,1,screen);
			draw_screen(screen);
			
			sleep(10);
	}
	clr();
	return 0;
}
예제 #5
0
int main(int argc, char *argv[])
{
	double start, end, timechk;

	if(argc != 2)
	{
		fprintf(stderr, "usage: %s Count\nn", argv[0]);
		return __LINE__;
	}

	start = myclock();
	decode_data_to_json(argv[1]);
	end = myclock();
	timechk = end - start;
	printf("time: %f\n", timechk);


	return 0;
}
예제 #6
0
static gboolean
move_window (GstPipeline * pipeline)
{
  x += 5;
  y = disp_height - height + (sin (myclock () / 300.0) * height);
  if (x > disp_width)
    x = 0;

  XMoveWindow (disp, win, x, y);

  XSync (disp, FALSE);

  return TRUE;
}
예제 #7
0
파일: tspeed.c 프로젝트: Winter3un/ctf_task
int
main(int argc, char **argv)
{
  int problem;

  iters = init_iters;

  while (1)
    {
      numtps = 1;  /* set pre-run breakpoint here */

      /* Keep trying the speed test, with more iterations, until
	 we get to a reasonable number.  */
      while (problem = trace_speed_test())
	{
	  /* If iteration isn't working, give up.  */
	  if (iters > max_iters)
	    {
	      printf ("Gone over %d iterations, giving up\n", max_iters);
	      break;
	    }
	  if (problem < 0)
	    {
	      printf ("Negative times, giving up\n", max_iters);
	      break;
	    }

	  iters *= 2;
	  printf ("Doubled iterations to %d\n", iters);
	}

      printf ("Tracepoint time is %d ns\n", nspertp);

      /* This is for the benefit of interactive testing and attaching,
	 keeps the program from pegging the machine.  */
      sleep (1);  /* set post-run breakpoint here */

      /* Issue a little bit of output periodically, so we can see if
	 program is alive or hung.  */
      printf ("%s keeping busy, clock=%llu\n", argv[0], myclock ());
    }
  return 0;
}
예제 #8
0
파일: learn.c 프로젝트: kriepy/Afstuderen
void
dm_learn (document *data, double *lambda, double **alpha,
	  int nmixtures, int nlex, int emmax, int remmax, double epsilon)
{
	document *dp;
	double *d0, *f, **p;
	double *s, **mu, **eps, *beta;
	double aimv, z, t1, t2;
	double ppl, sppl, pplp = 0, spplp = 0;
	int i, j, m, n, t, v;
	int start, elapsed, step, steps = 0;

	/* initialize seed */
	srand(time(NULL));

	/* initialize lambda */
	for (i = 0; i < nmixtures; i++)
		lambda[i] = 1.0 / (double)nmixtures;

	/* count data length, allocate p */
	for (dp = data, n = 0; (dp->len) != -1; dp++, n++)
		;
	if ((p = dmatrix(n, nmixtures)) == NULL) {
		fprintf(stderr, "dm_learn:: can't allocate p.\n");
		return;
	}
	/* allocate d0, and cache */
	if ((d0 = (double *)calloc(n, sizeof(double))) == NULL) {
		fprintf(stderr, "dm_learn:: can't allocate d0.\n");
		return;
	}
	for (dp = data, i = 0; (dp->len) != -1; dp++, i++)
	{
		for (j = 0, z = 0; j < dp->len; j++)
			z += dp->cnt[j];
		d0[i] = z;
	}

	/* allocate eps */
	if ((eps = dmatrix(nmixtures,nlex)) == NULL) {
		fprintf(stderr, "dm_learn:: can't allocate eps.\n");
		return;
	}
	/* allocate beta, and initialize */
	if ((beta = (double *)calloc(nlex, sizeof(double))) == NULL) {
		fprintf(stderr, "dm_learn:: can't allocate beta.\n");
		return;
	}
	for (v = 0; v < nlex; v++)
		beta[v] = INITIAL_PCOUNT;

	/* allocate s, mu */
	if ((s = (double *)calloc(nmixtures, sizeof(double))) == NULL) {
		fprintf(stderr, "dm_learn:: can't allocate s.\n");
		return;
	}
	if ((mu = dmatrix(nmixtures, nlex)) == NULL) {
		fprintf(stderr, "dm_learn:: can't allocate mu.\n");
		return;
	}
	/* initialize s, mu */
	for (m = 0; m < nmixtures; m++)
		s[m] = INITIAL_PCOUNT * nlex;
	if ((f = (double *)calloc(nlex, sizeof(double))) == NULL) {
		fprintf(stderr, "dm_learn:: can't allocate f.\n");
		return;
	}
	for (dp = data, z = 0; (dp->len) != -1; dp++)
	{
		for (j = 0; j < dp->len; j++)
		{
			f[dp->id[j]] += dp->cnt[j];
			z += dp->cnt[j];
		}
	}
	for (i = 0; i < nlex; i++)
		f[i] /= z;
	for (m = 0; m < nmixtures; m++)
		dirrand(mu[m], f, nlex, INITIAL_PCOUNT * nlex * 100);
	for (m = 0; m < nmixtures; m++) {
		for (v = 0; v < nlex; v++)
			mu[m][v] += INITIAL_PCOUNT;
		for (v = 0; v < nlex; v++)
			mu[m][v] /= (1 + INITIAL_PCOUNT * nlex);
	}
	
	printf("number of documents   = %d\n", n);
	printf("number of words       = %d\n", nlex);
	printf("number of mixtures    = %d\n", nmixtures);
	printf("convergence criterion = %.6g %%\n", epsilon * 100);

	/*
	 *  learn main
	 *
	 */
	start = myclock();
	for (t = 0; t < emmax; t++)
	{
		/*
		 *  E step
		 *
		 */
		for (step = 1; step <= remmax; step++)
		{
			/* inner REM E step */
			printf("iteration %d/%d [REM %d+%d]..\t",
			       t + 1, emmax, step, steps);
			fflush(stdout);
			for (dp = data, i = 0; (dp->len) != -1; dp++, i++)
			{
				for (m = 0; m < nmixtures; m++)
				{
					for (j = 0, z = 0; j < dp->len; j++) {
						if (dp->cnt[j] == 1) {
							z += log(s[m]*mu[m][dp->id[j]]);
						} else {
							z += lgamma(s[m]*mu[m][dp->id[j]] + dp->cnt[j])
								- lgamma(s[m]*mu[m][dp->id[j]]);
						}
					}
					p[i][m] = log(lambda[m])
						  + lgamma(s[m]) - lgamma(s[m] + d0[i])
						  + z;
				}
				/* normalize, and exp */
				for (m = 0, z = 0; m < nmixtures; m++)
					z = logsumexp(z, p[i][m], (m == 0));
				for (m = 0; m < nmixtures; m++)
					p[i][m] = exp(p[i][m] - z);
			}
			/* inner REM M step */
			for (m = 0; m < nmixtures; m++)
			{
				for (v = 0; v < nlex; v++)
					eps[m][v] = beta[v];
				t1 = t2 = 0;
				for (dp = data, i = 0; (dp->len) != -1; dp++, i++)
				{
					for (j = 0, z = 0; j < dp->len; j++)
					{
						v = dp->id[j];
						if (dp->cnt[j] == 1) {
							aimv = 1;
						} else {
							aimv = s[m]*mu[m][v] *
								(psi(s[m]*mu[m][v] + dp->cnt[j])
								 - psi(s[m]*mu[m][v]));
						}
						eps[m][v] += p[i][m] * aimv;
						z += aimv;
					}
					t1 += p[i][m] * z;
					t2 += p[i][m] * (psi(s[m] + d0[i]) - psi(s[m]));
				}
				/* update s */
				s[m] = t1 / t2;
				/* update mu */
				for (v = 0, z = 0; v < nlex; v++)
					z += eps[m][v];
				for (v = 0; v < nlex; v++)
					mu[m][v] = eps[m][v] / z;
			}
			ppl = dm_ppl(data, lambda, s, mu, d0, nmixtures, nlex);
			printf("PPL = %.6g\r", ppl); fflush(stdout);
			if (fabs(pplp - ppl) / pplp < 1.0e-3)
				break;	/* inner loop converged */
			else
				pplp = ppl;
		}
		steps += step;
		
		/*
		 *  M step
		 *
		 */

		/* MLE lambda */
		for (m = 0; m < nmixtures; m++)
			lambda[m] = 0;
		for (dp = data, i = 0; (dp->len) != -1; dp++, i++)
		{
			for (m = 0; m < nmixtures; m++)
				lambda[m] += p[i][m];
		}
		/* normalize */
		for (m = 0, z = 0; m < nmixtures; m++)
			z += lambda[m];
		for (m = 0; m < nmixtures; m++)
			lambda[m] /= z;
		
		/* compute alpha */
		for (m = 0; m < nmixtures; m++)
			for (v = 0; v < nlex; v++)
				alpha[m][v] = s[m] * mu[m][v];

		/* MLE beta */
		newton_beta (beta, eps, nmixtures, nlex, 0);
		
		/* converged? */
		sppl = dm_ppl(data, lambda, s, mu, d0, nmixtures, nlex);
		elapsed = myclock() - start;
		if ((t > 1) && (spplp - sppl) / spplp < epsilon) {
			printf("\nconverged. [%s]\n", rtime(elapsed));
			free_dmatrix(mu, nmixtures);
			free_dmatrix(eps, nmixtures);
			free_dmatrix(p, n);
			free(beta);
			free(d0);
			free(s);
			free(f);
			return;
		}
		spplp = sppl;
		/*
		 *  ETA
		 *
		 */
		printf("iteration %2d/%d [REM %d+%d]..  \t",
		       t + 1, emmax, step, steps);
		printf("PPL = %.6g\t", sppl);
		printf("ETA:%s (%d sec/step)\r",
		       rtime(elapsed * ((double) emmax / (t + 1) - 1)),
		       (int)((double) elapsed / (t + 1) + 0.5));
		
	}
	printf("\nmaximum iteration reached. exiting..\n");
	
	free_dmatrix(mu, nmixtures);
	free_dmatrix(eps, nmixtures);
	free_dmatrix(p, n);
	free(beta);
	free(d0);
	free(s);
	free(f);
	return;

}
예제 #9
0
int main( int argc, char *argv[] )
{
     IDirectFB              *dfb;
     IDirectFBDisplayLayer  *layer;

     IDirectFBSurface       *bgsurface;
     IDirectFBImageProvider *provider;

     IDirectFBWindow        *window1;
     IDirectFBWindow        *window2;
     IDirectFBSurface       *window_surface1;
     IDirectFBSurface       *window_surface2;

     IDirectFBEventBuffer   *buffer;

     DFBDisplayLayerConfig  layer_config;

#if ((DIRECTFB_MAJOR_VERSION == 0) && (DIRECTFB_MINOR_VERSION == 9) && (DIRECTFB_MICRO_VERSION < 23))
     DFBCardCapabilities    caps;
#else
     DFBGraphicsDeviceDescription caps;
#endif
     IDirectFBWindow*       upper;
     DFBWindowID            id1;

     IDirectFBFont          *font;
     int fontheight;
     int err;
     int quit = 0;


     DFBCHECK(DirectFBInit( &argc, &argv ));
     DFBCHECK(DirectFBCreate( &dfb ));

#if ((DIRECTFB_MAJOR_VERSION == 0) && (DIRECTFB_MINOR_VERSION == 9) && (DIRECTFB_MICRO_VERSION < 23))
     dfb->GetCardCapabilities( dfb, &caps );
#else
     dfb->GetDeviceDescription( dfb, &caps );
#endif

     dfb->GetDisplayLayer( dfb, DLID_PRIMARY, &layer );

     if (!((caps.blitting_flags & DSBLIT_BLEND_ALPHACHANNEL) &&
           (caps.blitting_flags & DSBLIT_BLEND_COLORALPHA  )))
     {
          layer_config.flags = DLCONF_BUFFERMODE;
          layer_config.buffermode = DLBM_BACKSYSTEM;

          layer->SetConfiguration( layer, &layer_config );
     }

     layer->GetConfiguration( layer, &layer_config );
     layer->EnableCursor ( layer, 1 );

     {
          DFBFontDescription desc;

          desc.flags = DFDESC_HEIGHT;
          desc.height = layer_config.width/50;

          DFBCHECK(dfb->CreateFont( dfb, PACKAGE_DATA_DIR"/grunge.ttf", &desc, &font ));
          font->GetHeight( font, &fontheight );
     }

     {
          DFBSurfaceDescription desc;

          DFBCHECK(dfb->CreateImageProvider( dfb,
                                             PACKAGE_DATA_DIR"/bg.png",
                                             &provider ));

          desc.flags = DSDESC_WIDTH | DSDESC_HEIGHT;
          desc.width = layer_config.width;
          desc.height = layer_config.height;

          DFBCHECK(dfb->CreateSurface( dfb, &desc, &bgsurface ) );


          provider->RenderTo( provider, bgsurface, NULL );
          provider->Release( provider );

	  DFBCHECK(bgsurface->SetFont( bgsurface, font ));

          bgsurface->SetColor( bgsurface, 0xCF, 0xCF, 0xFF, 0xFF );
          bgsurface->DrawString( bgsurface,
                                 "Move the mouse over a window to activate it.",
                                 -1, 10, 0, DSTF_LEFT | DSTF_TOP );

          bgsurface->SetColor( bgsurface, 0xFF, 0xCF, 0xFF, 0xFF );
          bgsurface->DrawString( bgsurface,
                    "You can drag them around, too, if you want.",
                                 -1, 10 , 40, DSTF_LEFT | DSTF_TOP );

          bgsurface->SetColor( bgsurface, 0xCF, 0xCF, 0xFF, 0xFF );
	  bgsurface->DrawString( bgsurface,
                    "The one with funky stuff happening and things flying around is an evas.",
                                 -1, 10, 80, DSTF_LEFT | DSTF_TOP );




          layer->SetBackgroundImage( layer, bgsurface );
          layer->SetBackgroundMode( layer, DLBM_IMAGE );
     }
     {
	  DFBWindowDescription desc;
	  desc.flags = ( DWDESC_POSX | DWDESC_POSY |
                         DWDESC_WIDTH | DWDESC_HEIGHT | DWDESC_CAPS );

	  desc.posx = 20;
          desc.posy = 120;
          desc.width = 200;
          desc.height = 200;
	  desc.caps = DWCAPS_ALPHACHANNEL;

          DFBCHECK( layer->CreateWindow( layer, &desc, &window2 ) );
          window2->GetSurface( window2, &window_surface2 );

          window2->SetOpacity( window2, 0xFF );

          window2->CreateEventBuffer( window2, &buffer );

	  {
	     window_surface2->SetColor( window_surface2,
		   0x00, 0x30, 0x10, 0xc0 );
	     window_surface2->DrawRectangle( window_surface2, 0, 0,
		   desc.width, desc.height );
	     window_surface2->SetColor( window_surface2,
		   0x80, 0xa0, 0x00, 0x90 );
	     window_surface2->FillRectangle( window_surface2, 1, 1,
		   desc.width-2, desc.height-2 );


	     DFBCHECK(window_surface2->SetFont(window_surface2, font ));
	     window_surface2->SetColor( window_surface2, 0xCF, 0xFF, 0xCF, 0xFF );

	     window_surface2->DrawString( window_surface2,
		   "Pants!",
		   -1,10, fontheight + 5, DSTF_LEFT | DSTF_TOP );

	  }

          window_surface2->Flip( window_surface2, NULL, 0 );
     }

     {
          DFBWindowDescription desc;

          desc.flags = ( DWDESC_POSX | DWDESC_POSY |
                         DWDESC_WIDTH | DWDESC_HEIGHT | DWDESC_CAPS );
          desc.posx = 200;
          desc.posy = 200;
          desc.width = 240;
          desc.height = 320;
          desc.caps = DWCAPS_ALPHACHANNEL;

          DFBCHECK(layer->CreateWindow( layer, &desc, &window1 ) );
          window1->GetSurface( window1, &window_surface1 );

          window_surface1->SetColor( window_surface1, 0xFF, 0x20, 0x20, 0x90 );
          window_surface1->DrawRectangle( window_surface1, 0, 0,
                                          desc.width, desc.height );

          window_surface1->Flip( window_surface1, NULL, 0 );

          window1->SetOpacity( window1, 0xFF );

          window1->GetID( window1, &id1 );

          window1->AttachEventBuffer( window1, buffer );
     }

     window1->RequestFocus( window1 );
     window1->RaiseToTop( window1 );
     upper = window1;
     {
	evas_init();
	evas = evas_new();
	evas_output_method_set(evas, evas_render_method_lookup("directfb"));
	evas_output_size_set(evas, 240, 320);
	evas_output_viewport_set(evas, 0, 0, 240, 320);
	{
	   Evas_Engine_Info_DirectFB *einfo;

	   einfo = (Evas_Engine_Info_DirectFB *) evas_engine_info_get(evas);

	   einfo->info.dfb = dfb;
	   einfo->info.surface = window_surface1;
	   einfo->info.flags = DSDRAW_BLEND;
	   evas_engine_info_set(evas, (Evas_Engine_Info *) einfo);
	}
	setup();
	evas_render(evas);
	start_time = get_time();
     }

     while (!quit) {
          static IDirectFBWindow* active = NULL;
          static int grabbed = 0;
          static int startx = 0;
          static int starty = 0;
          static int endx = 0;
          static int endy = 0;
          DFBWindowEvent evt;

          buffer->WaitForEventWithTimeout( buffer, 0, 10 );

          while (buffer->GetEvent( buffer, DFB_EVENT(&evt) ) == DFB_OK) {
               IDirectFBWindow* window;

               if (evt.window_id == id1)
                    window = window1;
               else
                    window = window2;

               if (active) {
                    switch (evt.type) {

                    case DWET_BUTTONDOWN:
                         if (!grabbed && evt.button == DIBI_LEFT) {
                              grabbed = 1;
                              layer->GetCursorPosition( layer,
                                                        &startx, &starty );
                              window->GrabPointer( window );
                         }
                         break;

                    case DWET_BUTTONUP:
                         switch (evt.button) {
                              case DIBI_LEFT:
                                   if (grabbed) {
                                        window->UngrabPointer( window );
                                        grabbed = 0;
                                   }
                                   break;
                              case DIBI_MIDDLE:
                                   upper->LowerToBottom( upper );
                                   upper = (upper == window1) ? window2 : window1;
                                   break;
                              case DIBI_RIGHT:
                                   quit = DIKS_DOWN;
                                   break;
                              default:
                                   break;
                         }
                         break;

                    case DWET_KEYDOWN:
                         if (grabbed)
                              break;
                         switch (evt.key_id) {
                              case DIKI_RIGHT:
                                   active->Move (active, 1, 0);
                                   break;
                              case DIKI_LEFT:
                                   active->Move (active, -1, 0);
                                   break;
                              case DIKI_UP:
                                   active->Move (active, 0, -1);
                                   break;
                              case DIKI_DOWN:
                                   active->Move (active, 0, 1);
                                   break;
                              default:
                                   break;
                         }
                         break;

                    case DWET_LOSTFOCUS:
                         if (!grabbed)
                              active = NULL;
                         break;

                    default:
                         break;

                    }
               }
               else if (evt.type == DWET_GOTFOCUS)
                    active = window;

               switch (evt.type) {

               case DWET_MOTION:
                    endx = evt.cx;
                    endy = evt.cy;
                    break;

               case DWET_KEYDOWN:
                    switch (evt.key_symbol) {
                    case DIKS_ESCAPE:
                    case DIKS_SMALL_Q:
                    case DIKS_CAPITAL_Q:
                    case DIKS_BACK:
                    case DIKS_STOP:
                         quit = 1;
                         break;
                    default:
                         break;
                    }
                    break;

               default:
                    break;
               }
          }

          if (active) {
               if (grabbed) {
                    active->Move( active, endx - startx, endy - starty);
                    startx = endx;
                    starty = endy;
               }
               active->SetOpacity( active,
                                   (sin( myclock()/300.0 ) * 85) + 170 );
          }
	  loop();
	  {
	     Eina_List *updates;

	     updates = evas_render_updates(evas);
	     /* efficient update.. only flip the rectangle regions that changed! */
	     if (updates)
	       {
		  DFBRegion region;
		  Eina_List *l;

		  for (l = updates; l; l = l->next)
		    {
		       Evas_Rectangle *rect;

		       rect = l->data;
		       region.x1 = rect->x;
		       region.y1 = rect->y;
		       region.x2 = rect->x + rect->w - 1;
		       region.y2 = rect->y + rect->h - 1;
		       window_surface1->Flip(window_surface1, &region,
					     DSFLIP_BLIT);
		    }
		  evas_render_updates_free(updates);
	       }
	  }
     }

     buffer->Release( buffer );
     window_surface2->Release( window_surface2 );
     window_surface1->Release( window_surface1 );
     window2->Release( window2 );
     window1->Release( window1 );
     layer->Release( layer );
     bgsurface->Release( bgsurface );
     dfb->Release( dfb );

   evas_shutdown();
   return 0;
}
예제 #10
0
파일: tspeed.c 프로젝트: Winter3un/ctf_task
int
trace_speed_test (void)
{
  int i;

  /* Overall loop run time deltas under 1 ms are likely noise and
     should be ignored.  */
  mindelta = 1000;

  // The bodies of the two loops following must be identical.

  now2 = myclock ();
  globfoo2 = 1;
  for (i = 0; i < iters; ++i)
    {
      globfoo2 *= 45;
      globfoo2 += globfoo + globfoo3; 
      globfoo2 *= globfoo + globfoo3; 
      globfoo2 -= globarr[4] + globfoo3; 
      globfoo2 *= globfoo + globfoo3; 
      globfoo2 += globfoo + globfoo3; 
    }
  now3 = myclock ();
  total1 = now3 - now2;

  now4 = myclock ();
  globfoo2 = 1;
  for (i = 0; i < iters; ++i)
    {
      globfoo2 *= 45;
      globfoo2 += globfoo + globfoo3;  /* set tracepoint here */
      globfoo2 *= globfoo + globfoo3; 
      globfoo2 -= globarr[4] + globfoo3; 
      globfoo2 *= globfoo + globfoo3; 
      globfoo2 += globfoo + globfoo3; 
    }
  now5 = myclock ();
  total2 = now5 - now4;

  /* Report on the test results.  */

  nspertp = 0;

  idelta = total2 - total1;

  printf ("Loops took %d usec and %d usec, delta is %d usec, %d iterations\n",
	  total1, total2, idelta, iters);

  /* If the second loop seems to run faster, things are weird so give up.  */
  if (idelta < 0)
    return -1;

  if (idelta > mindelta
      /* Total test time should be between 15 and 30 seconds.  */
      && (total1 + total2) > (15 * 1000000)
      && (total1 + total2) < (30 * 1000000))
    {
      nsdelta = (((unsigned long long) idelta) * 1000) / iters;
      printf ("Second loop took %d ns longer per iter than first\n", nsdelta);
      nspertp = nsdelta / numtps;
      printf ("%d ns per tracepoint\n", nspertp);
      printf ("Base iteration time %d ns\n",
	      ((int) (((unsigned long long) total1) * 1000) / iters));
      printf ("Total test time %d secs\n", ((int) ((now5 - now2) / 1000000)));

      /* Speed test ran with no problem.  */
      return 0;
    }

  /* The test run was too brief, or otherwise not useful.  */
  return 1;
}
예제 #11
0
파일: sgenmain.cpp 프로젝트: b45ch1/adol-c
/*                                                             MAIN PROGRAM */
int main() {
    int i, j, k;
    int tag = 1;                  /* tape tag */
    int taskCount = 0;

    int pFW, pRV, pTR, degree, keep;   /* forward/reverse parameters */
    int evalCount;                     /* # of evaluations */


    /****************************************************************************/
    /*                                        READ CONTROL PARAMETERS FROM FILE */
    int controlParameters[cpCount];
    FILE* controlFile;

    /*------------------------------------------------------------------------*/
    /*                                                      open file to read */
    if ((controlFile = fopen(controlFileName,"r")) == NULL) {
        fprintf(stdout,"ERROR: Could not open control file %s\n",
                controlFileName);
        exit(-1);
    }

    /*------------------------------------------------------------------------*/
    /*                                                        read all values */
    for (i=0; i<cpCount; i++)
        fscanf(controlFile,"%d%*[^\n]",&controlParameters[i]);

    indepDim  = controlParameters[cpDimension];
    pFW       = controlParameters[cpVecCountFW];
    pRV       = controlParameters[cpVecCountRV];
    pTR       = controlParameters[cpVecCountTR];
    degree    = controlParameters[cpDegree];
    evalCount = controlParameters[cpAverageCount];

    /*------------------------------------------------------------------------*/
    /*                                                     close control file */
    fclose(controlFile);


    /****************************************************************************/
    /*                                               VARIABLES & INITIALIZATION */

    /*------------------------------------------------------------------------*/
    /* Initialize all problem parameters (including  dimension) */
    initProblemParameters();

    /*------------------------------------------------------------------------*/
    /* Initialize the independent variables */
    double* indeps = new double[indepDim];
    initIndependents(indeps);

    /*------------------------------------------------------------------------*/
    /* Check main parameters */
    if (evalCount <= 0) {
        fprintf(stdout,"    # of evaluations to average over = ? ");
        fscanf(stdin,"%d",&evalCount);
        fprintf(stdout,"\n");
    }

    if ((degree <= 1) &&
            (controlParameters[cpHosFW] || controlParameters[cpHovFW] ||
             controlParameters[cpHosRV] || controlParameters[cpHovRV] ||
             controlParameters[cpTensor])) {
        fprintf(stdout,"    degree = ? ");
        fscanf(stdin,"%d",&degree);
        fprintf(stdout,"\n");
    }
    keep = degree + 1;

    if ((pFW < 1) &&
            (controlParameters[cpFovFW] || controlParameters[cpHovFW])) {
        fprintf(stdout,"    # of vectors in vector forward mode = ? ");
        fscanf(stdin,"%d",&pFW);
        fprintf(stdout,"\n");
    }

    if ((pRV < 1) &&
            (controlParameters[cpFovRV] || controlParameters[cpHovRV])) {
        fprintf(stdout,"    # of vectors in vector reverse mode = ? ");
        fscanf(stdin,"%d",&pRV);
        fprintf(stdout,"\n");
    }

    if ((pTR < 1) &&
            (controlParameters[cpTensor])) {
        fprintf(stdout,"    # of vectors in tensor mode = ? ");
        fscanf(stdin,"%d",&pTR);
        fprintf(stdout,"\n");
    }

    /*------------------------------------------------------------------------*/
    /* Necessary variable */
    double depOrig=0.0, depTape;    /* function value */
    double ***XPPP, **XPP;
    double ***YPPP, **YPP, *YP;
    double ***ZPPP, **ZPP, *ZP;
    double          *UP, u;
    double                 *VP;
    double                 *WP;
    double          *JP;
    short           **nzPP;
    int retVal=0;                 /* return value */
    double t00, t01, t02, t03;  /* time values */
    double          **TPP;
    double          **SPP;
    double          **HPP;
    int dim;


    /****************************************************************************/
    /*                                                          NORMALIZE TIMER */



    /****************************************************************************/
    /*                                          0. ORIGINAL FUNCTION EVALUATION */
    /*                                             ---> always                  */
    fprintf(stdout,"\nTASK %d: Original function evaluation\n",
            taskCount++);

    t00 = myclock();
    for (i=0; i<evalCount; i++)
        depOrig = originalScalarFunction(indeps);
    t01 = myclock();

    double timeUnit;
    if (t01-t00) {
        timeUnit = 1.0/(t01-t00);
        fprintf(stdout,"          ");
        fprintf(stdout,TIMEFORMAT,1.0,
                (t01-t00)/evalCount);
    } else {
        fprintf(stdout,"    !!! zero timing !!!\n");
        fprintf(stdout,"    set time unit to 1.0\n");
        timeUnit = 1;
    }


    /****************************************************************************/
    /*                                                   1. TAPING THE FUNCTION */
    /*                                                      ---> always         */
    fprintf(stdout,"--------------------------------------------------------");
    fprintf(stdout,"\nTASK %d: Taping the function\n",
            taskCount++);

    t00 = myclock();
    /* NOTE: taping will be performed ONCE only */
    depTape = tapingScalarFunction(tag,indeps);
    t01 = myclock();

    size_t tape_stats[STAT_SIZE];
    tapestats(tag,tape_stats);

    fprintf(stdout,"\n    independents            %zu\n",tape_stats[NUM_INDEPENDENTS]);
    fprintf(stdout,"    dependents              %zu\n",tape_stats[NUM_DEPENDENTS]);
    fprintf(stdout,"    operations              %zu\n",tape_stats[NUM_OPERATIONS]);
    fprintf(stdout,"    operations buffer size  %zu\n",tape_stats[OP_BUFFER_SIZE]);
    fprintf(stdout,"    locations buffer size   %zu\n",tape_stats[LOC_BUFFER_SIZE]);
    fprintf(stdout,"    constants buffer size   %zu\n",tape_stats[VAL_BUFFER_SIZE]);
    fprintf(stdout,"    maxlive                 %zu\n",tape_stats[NUM_MAX_LIVES]);
    fprintf(stdout,"    valstack size           %zu\n\n",tape_stats[TAY_STACK_SIZE]);

    fprintf(stdout,"           ");
    fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit*evalCount,
            (t01-t00));

    /****************************************************************************/
    /*                                                           2. ZOS_FORWARD */
    if (controlParameters[cpZosFW]) {
        fprintf(stdout,"--------------------------------------------------------");
        fprintf(stdout,"\nTASK %d: forward(tag, m=1, n=%d, keep, X[n], Y[m])\n",
                taskCount++,indepDim);
        fprintf(stdout,"         ---> zos_forward\n");

        /*----------------------------------------------------------------------*/
        /* NO KEEP */
        t00 = myclock();
        for (i=0; i<evalCount; i++)
            retVal = forward(tag,1,indepDim,0,indeps,&depTape);
        t01 = myclock();

        fprintf(stdout,"    NO KEEP");
        fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit,
                (t01-t00)/evalCount);

        /*----------------------------------------------------------------------*/
        /* KEEP */
        t02 = myclock();
        for (i=0; i<evalCount; i++)
            retVal = forward(tag,1,indepDim,1,indeps,&depTape);
        t03 = myclock();

        fprintf(stdout,"    KEEP   ");
        fprintf(stdout,TIMEFORMAT,(t03-t02)*timeUnit,
                (t03-t02)/evalCount);

        /*----------------------------------------------------------------------*/
        /* Debug infos */
        if (controlParameters[cpZosFW] > 1) {
            fprintf(stdout,"\n    Return value: %d\n",retVal);
            fprintf(stdout,"    Should be the same values:\n");
            fprintf(stdout,"    (original) %12.8E =? %12.8E (forward from tape)\n",
                    depOrig,depTape);
        }
    }


    /****************************************************************************/
    /*                                                           3. FOS_FORWARD */
    if (controlParameters[cpFosFW]) {
        fprintf(stdout,"--------------------------------------------------------");
        fprintf(stdout,"\nTASK %d: forward(tag, m=1, n=%d, d=1, keep, X[n][d+1], Y[d+1])\n",
                taskCount++,indepDim);
        fprintf(stdout,"         ---> fos_forward\n");

        /*----------------------------------------------------------------------*/
        /* Allocation & initialisation of tensors */
        XPP = new double*[indepDim];
        for (i=0; i<indepDim; i++) {
            XPP[i] = new double[2];
            XPP[i][0] = indeps[i];
            XPP[i][1] = (double)rand();
        }
        YP = new double[2];

        /*----------------------------------------------------------------------*/
        /* NO KEEP */
        t00 = myclock();
        for (i=0; i<evalCount; i++)
            retVal = forward(tag,1,indepDim,1,0,XPP,YP);
        t01 = myclock();

        fprintf(stdout,"    NO KEEP");
        fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit,
                (t01-t00)/evalCount);

        /*----------------------------------------------------------------------*/
        /* KEEP */
        t02 = myclock();
        for (i=0; i<evalCount; i++)
            retVal = forward(tag,1,indepDim,1,2,XPP,YP);
        t03 = myclock();

        fprintf(stdout,"    KEEP   ");
        fprintf(stdout,TIMEFORMAT,(t03-t02)*timeUnit,
                (t03-t02)/evalCount);

        /*----------------------------------------------------------------------*/
        /* Debug infos */
        if (controlParameters[cpFosFW] > 1) {
            fprintf(stdout,"\n    Return value: %d\n",retVal);
            fprintf(stdout,"    Should be the same values:\n");
            fprintf(stdout,"    (original) %12.8E =? %12.8E (forward from tape)\n",
                    depOrig,YP[0]);
        }

        /*----------------------------------------------------------------------*/
        /* Free tensors */
        for (i=0; i<indepDim; i++)
            delete[] XPP[i];
        delete[] XPP;
        delete[] YP;
    }


    /****************************************************************************/
    /*                                                           4. HOS_FORWARD */
    if (controlParameters[cpHosFW]) {
        fprintf(stdout,"--------------------------------------------------------");
        fprintf(stdout,"\nTASK %d: forward(tag, m=1, n=%d, d=%d, keep, X[n][d+1], Y[d+1])\n",
                taskCount++,indepDim,degree);
        fprintf(stdout,"         ---> hos_forward\n");

        /*----------------------------------------------------------------------*/
        /* Allocation & initialisation of tensors */
        XPP = new double*[indepDim];
        for (i=0; i<indepDim; i++) {
            XPP[i] = new double[1+degree];
            XPP[i][0] = indeps[i];
            for (j=1; j<=degree; j++)
                XPP[i][j] = (double)rand();
        }
        YP = new double[1+degree];

        /*----------------------------------------------------------------------*/
        /* NO KEEP */
        t00 = myclock();
        for (i=0; i<evalCount; i++)
            retVal = forward(tag,1,indepDim,degree,0,XPP,YP);
        t01 = myclock();

        fprintf(stdout,"    NO KEEP");
        fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit,
                (t01-t00)/evalCount);

        /*----------------------------------------------------------------------*/
        /* KEEP */
        t02 = myclock();
        for (i=0; i<evalCount; i++)
            retVal = forward(tag,1,indepDim,degree,keep,XPP,YP);
        t03 = myclock();

        fprintf(stdout,"    KEEP   ");
        fprintf(stdout,TIMEFORMAT,(t03-t02)*timeUnit,
                (t03-t02)/evalCount);

        /*----------------------------------------------------------------------*/
        /* Debug infos */
        if (controlParameters[cpHosFW] > 1) {
            fprintf(stdout,"\n    Return value: %d\n",retVal);
            fprintf(stdout,"    Should be the same values:\n");
            fprintf(stdout,"    (original) %12.8E =? %12.8E (forward from tape)\n",
                    depOrig,YP[0]);
        }

        /*----------------------------------------------------------------------*/
        /* Free tensors */
        for (i=0; i<indepDim; i++)
            delete[] XPP[i];
        delete[] XPP;
        delete[] YP;
    }


    /****************************************************************************/
    /*                                                           5. FOV_FORWARD */
    if (controlParameters[cpFovFW]) {
        fprintf(stdout,"--------------------------------------------------------");
        fprintf(stdout,"\nTASK %d: forward(tag, m=1, n=%d, p=%d, x[n], X[n][p], y[m], Y[m][p])\n",
                taskCount++,indepDim,pFW);
        fprintf(stdout,"         ---> fov_forward\n");

        /*----------------------------------------------------------------------*/
        /* Allocation & initialisation of tensors */
        XPP = new double*[indepDim];
        for (i=0; i<indepDim; i++) {
            XPP[i] = new double[pFW];
            for (j=0; j<pFW; j++)
                XPP[i][j] = (double)rand();
        }
        YP  = new double[1];
        YPP = new double*[1];
        YPP[0] = new double[pFW];

        /*----------------------------------------------------------------------*/
        /* always NO KEEP */
        t00 = myclock();
        for (i=0; i<evalCount; i++)
            retVal = forward(tag,1,indepDim,pFW,indeps,XPP,YP,YPP);
        t01 = myclock();

        fprintf(stdout,"  (NO KEEP)");
        fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit,
                (t01-t00)/evalCount);

        /*----------------------------------------------------------------------*/
        /* Debug infos */
        if (controlParameters[cpFovFW] > 1) {
            fprintf(stdout,"\n    Return value: %d\n",retVal);
        }

        /*----------------------------------------------------------------------*/
        /* Free tensors */
        for (i=0; i<indepDim; i++)
            delete[] XPP[i];
        delete[] XPP;
        delete[] YP;
        delete[] YPP[0];
        delete[] YPP;
    }


    /****************************************************************************/
    /*                                                           6. HOV_FORWARD */
    if (controlParameters[cpHovFW]) {
        fprintf(stdout,"--------------------------------------------------------");
        fprintf(stdout,"\nTASK %d: forward(tag, m=1, n=%d, d=%d, p=%d, x[n], X[n][p][d], y[m], Y[m][p][d])\n",
                taskCount++,indepDim,degree,pFW);
        fprintf(stdout,"         ---> hov_forward\n");

        /*----------------------------------------------------------------------*/
        /* Allocation & initialisation of tensors */
        XPPP = new double**[indepDim];
        for (i=0; i<indepDim; i++) {
            XPPP[i] = new double*[pFW];
            for (j=0; j<pFW; j++) {
                XPPP[i][j] = new double[degree];
                for (k=0; k<degree; k++)
                    XPPP[i][j][k] = (double)rand();
            }
        }
        YP  = new double[1];
        YPPP = new double**[1];
        YPPP[0] = new double*[pFW];
        for (j=0; j<pFW; j++)
            YPPP[0][j] = new double[degree];

        /*----------------------------------------------------------------------*/
        /* always NO KEEP */
        t00 = myclock();
        for (i=0; i<evalCount; i++)
            retVal = forward(tag,1,indepDim,degree,pFW,indeps,XPPP,YP,YPPP);
        t01 = myclock();

        fprintf(stdout,"  (NO KEEP)");
        fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit,
                (t01-t00)/evalCount);

        /*----------------------------------------------------------------------*/
        /* Debug infos */
        if (controlParameters[cpHovFW] > 1) {
            fprintf(stdout,"\n    Return value: %d\n",retVal);
        }

        /*----------------------------------------------------------------------*/
        /* Free tensors */
        for (i=0; i<indepDim; i++) {
            for (j=0; j<pFW; j++)
                delete[] XPPP[i][j];
            delete[] XPPP[i];
        }
        delete[] XPPP;
        delete[] YP;
        for (j=0; j<pFW; j++)
            delete[] YPPP[0][j];
        delete[] YPPP[0];
        delete[] YPPP;
    }


    /****************************************************************************/
    /*                                                           7. FOS_REVERSE */
    if (controlParameters[cpFosRV]) {
        fprintf(stdout,"--------------------------------------------------------");
        fprintf(stdout,"\nTASK %d: reverse(tag, m=1, n=%d, d=0, u, Z[n])\n",
                taskCount++,indepDim);
        fprintf(stdout,"         ---> fos_reverse\n");

        /*----------------------------------------------------------------------*/
        /* Allocation & initialisation of tensors */
        ZP = new double[indepDim];
        u  = (double)rand();

        /*----------------------------------------------------------------------*/
        /* Forward with keep*/
        forward(tag,1,indepDim,1,indeps,&depTape);

        /*----------------------------------------------------------------------*/
        /* Reverse */
        t00 = myclock();
        for (i=0; i<evalCount; i++)
            retVal = reverse(tag,1,indepDim,0,u,ZP);
        t01 = myclock();

        fprintf(stdout,"           ");
        fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit,
                (t01-t00)/evalCount);

        /*----------------------------------------------------------------------*/
        /* Debug infos */
        if (controlParameters[cpFosRV] > 1) {
            fprintf(stdout,"\n    Return value: %d\n",retVal);
        }

        /*----------------------------------------------------------------------*/
        /* Free tensors */
        delete[] ZP;
    }


    /****************************************************************************/
    /*                                                           8. HOS_REVERSE */
    if (controlParameters[cpHosRV]) {
        fprintf(stdout,"--------------------------------------------------------");
        fprintf(stdout,"\nTASK %d: reverse(tag, m=1, n=%d, d=%d, u, Z[n][d+1])\n",
                taskCount++,indepDim,degree);
        fprintf(stdout,"         ---> hos_reverse\n");

        /*----------------------------------------------------------------------*/
        /* Allocation & initialisation of tensors */
        ZPP = new double*[indepDim];
        for (i=0; i<indepDim; i++)
            ZPP[i] = new double[degree+1];
        u  = (double)rand();
        XPP = new double*[indepDim];
        for (i=0; i<indepDim; i++) {
            XPP[i] = new double[1+degree];
            XPP[i][0] = indeps[i];
            for (j=1; j<=degree; j++)
                XPP[i][j] = (double)rand();
        }
        YP = new double[1+degree];

        /*----------------------------------------------------------------------*/
        /* Forward with keep*/
        forward(tag,1,indepDim,degree,keep,XPP,YP);

        /*----------------------------------------------------------------------*/
        /* Reverse */
        t00 = myclock();
        for (i=0; i<evalCount; i++)
            retVal = reverse(tag,1,indepDim,degree,u,ZPP);
        t01 = myclock();

        fprintf(stdout,"           ");
        fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit,
                (t01-t00)/evalCount);

        /*----------------------------------------------------------------------*/
        /* Debug infos */
        if (controlParameters[cpHosRV] > 1) {
            fprintf(stdout,"\n    Return value: %d\n",retVal);
        }

        /*----------------------------------------------------------------------*/
        /* Free tensors */
        for (i=0; i<indepDim; i++)
            delete[] ZPP[i];
        delete[] ZPP;
        for (i=0; i<indepDim; i++)
            delete[] XPP[i];
        delete[] XPP;
        delete[] YP;
    }


    /****************************************************************************/
    /*                                                           9. FOV_REVERSE */
    if (controlParameters[cpFovRV]) {
        fprintf(stdout,"--------------------------------------------------------");
        fprintf(stdout,"\nTASK %d: reverse(tag, m=1, n=%d, d=0, p=%d, U[p], Z[p][n])\n",
                taskCount++,indepDim,pRV);
        fprintf(stdout,"         ---> fov_reverse\n");

        /*----------------------------------------------------------------------*/
        /* Allocation & initialisation of tensors */
        ZPP = new double*[pRV];
        for (i=0; i<pRV; i++)
            ZPP[i] = new double[indepDim];
        UP = new double[pRV];
        for (i=0; i<pRV; i++)
            UP[i] = (double)rand();

        /*----------------------------------------------------------------------*/
        /* Forward with keep*/
        forward(tag,1,indepDim,1,indeps,&depTape);

        /*----------------------------------------------------------------------*/
        /* Reverse */
        t00 = myclock();
        for (i=0; i<evalCount; i++)
            retVal = reverse(tag,1,indepDim,0,pRV,UP,ZPP);
        t01 = myclock();

        fprintf(stdout,"           ");
        fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit,
                (t01-t00)/evalCount);

        /*----------------------------------------------------------------------*/
        /* Debug infos */
        if (controlParameters[cpFovRV] > 1) {
            fprintf(stdout,"\n    Return value: %d\n",retVal);
        }

        /*----------------------------------------------------------------------*/
        /* Free tensors */
        for (i=0; i<pRV; i++)
            delete[] ZPP[i];
        delete[] ZPP;
        delete[] UP;
    }


    /****************************************************************************/
    /*                                                          10. HOV_REVERSE */
    if (controlParameters[cpHovRV]) {
        fprintf(stdout,"--------------------------------------------------------");
        fprintf(stdout,"\nTASK %d: reverse(tag, m=1, n=%d, d=%d, p=%d, U[p], Z[p][n][d+1], nz[p][n])\n",
                taskCount++,indepDim,degree,pRV);
        fprintf(stdout,"         ---> hov_reverse\n");

        /*----------------------------------------------------------------------*/
        /* Allocation & initialisation of tensors */
        ZPPP = new double**[pRV];
        for (i=0; i<pRV; i++) {
            ZPPP[i] = new double*[indepDim];
            for (j=0; j<indepDim; j++)
                ZPPP[i][j] = new double[degree+1];
        }
        UP = new double[pRV];
        for (i=0; i<pRV; i++)
            UP[i] = (double)rand();
        XPP = new double*[indepDim];
        for (i=0; i<indepDim; i++) {
            XPP[i] = new double[1+degree];
            XPP[i][0] = indeps[i];
            for (j=1; j<=degree; j++)
                XPP[i][j] = (double)rand();
        }
        YP = new double[1+degree];
        nzPP = new short*[pRV];
        for (i=0; i<pRV; i++)
            nzPP[i] = new short[indepDim];

        /*----------------------------------------------------------------------*/
        /* Forward with keep*/
        forward(tag,1,indepDim,degree,keep,XPP,YP);

        /*----------------------------------------------------------------------*/
        /* Reverse  without nonzero pattern*/
        t00 = myclock();
        for (i=0; i<evalCount; i++)
            retVal = reverse(tag,1,indepDim,degree,pRV,UP,ZPPP);
        t01 = myclock();

        fprintf(stdout,"           ");
        fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit,
                (t01-t00)/evalCount);

        /*----------------------------------------------------------------------*/
        /* Reverse  with nonzero pattern*/
        t00 = myclock();
        for (i=0; i<evalCount; i++)
            retVal = reverse(tag,1,indepDim,degree,pRV,UP,ZPPP,nzPP);
        t01 = myclock();

        fprintf(stdout,"       (NZ)");
        fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit,
                (t01-t00)/evalCount);

        /*----------------------------------------------------------------------*/
        /* Debug infos */
        if (controlParameters[cpHovRV] > 1) {
            fprintf(stdout,"\n    Return value: %d\n",retVal);
        }

        /*----------------------------------------------------------------------*/
        /* Free tensors */
        for (i=0; i<pRV; i++) {
            for (j=0; j<indepDim; j++)
                delete[] ZPPP[i][j];
            delete[] ZPPP[i];
            delete[] nzPP[i];
        }
        delete[] ZPPP;
        delete[] nzPP;
        delete[] UP;
        for (i=0; i<indepDim; i++)
            delete[] XPP[i];
        delete[] XPP;
        delete[] YP;
    }


    /****************************************************************************/
    /*                                                             11. FUNCTION */
    if (controlParameters[cpFunction]) {
        fprintf(stdout,"--------------------------------------------------------");
        fprintf(stdout,"\nTASK %d: function(tag, m=1, n=%d, X[n], Y[m])\n",
                taskCount++,indepDim);

        /*----------------------------------------------------------------------*/
        /* Function evaluation */
        t00 = myclock();
        for (i=0; i<evalCount; i++)
            retVal = function(tag,1,indepDim,indeps,&depTape);
        t01 = myclock();

        fprintf(stdout,"           ");
        fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit,
                (t01-t00)/evalCount);

        /*----------------------------------------------------------------------*/
        /* Debug infos */
        if (controlParameters[cpFunction] > 1) {
            fprintf(stdout,"\n    Return value: %d\n",retVal);
            fprintf(stdout,"    Should be the same values:\n");
            fprintf(stdout,"    (original) %12.8E =? %12.8E (forward from tape)\n",
                    depOrig,depTape);
        }
    }


    /****************************************************************************/
    /*                                                             12. JACOBIAN */
    if (controlParameters[cpJacobian]) {
        fprintf(stdout,"--------------------------------------------------------");
        fprintf(stdout,"\nTASK %d: gradient(tag, n=%d, X[n], G[n])\n",
                taskCount++,indepDim);

        /*----------------------------------------------------------------------*/
        /* Allocation & initialisation of tensors */
        JP = new double[indepDim];

        /*----------------------------------------------------------------------*/
        /* Gradient evaluation */
        t00 = myclock();
        for (i=0; i<evalCount; i++)
            retVal = gradient(tag,indepDim,indeps,JP);
        t01 = myclock();

        fprintf(stdout,"           ");
        fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit,
                (t01-t00)/evalCount);

        /*----------------------------------------------------------------------*/
        /* Debug infos */
        if (controlParameters[cpJacobian] > 1) {
            fprintf(stdout,"\n    Return value: %d\n",retVal);
        }

        /*----------------------------------------------------------------------*/
        /* Free tensors */
        delete[] JP;
    }


    /****************************************************************************/
    /*                                                               13. VECJAC */
    if (controlParameters[cpVecJac]) {
        fprintf(stdout,"--------------------------------------------------------");
        fprintf(stdout,"\nTASK %d: vec_jac(tag, m=1, n=%d, repeat, X[n], U[m], V[n])\n",
                taskCount++,indepDim);

        /*----------------------------------------------------------------------*/
        /* Allocation & initialisation of tensors */
        UP = new double[1];
        UP[0] = (double)rand();
        VP = new double[indepDim];

        /*----------------------------------------------------------------------*/
        /* Evaluation without repeat */
        t00 = myclock();
        for (i=0; i<evalCount; i++)
            retVal = vec_jac(tag,1,indepDim,0,indeps,UP,VP);
        t01 = myclock();

        fprintf(stdout,"(no repeat)");
        fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit,
                (t01-t00)/evalCount);

        /*----------------------------------------------------------------------*/
        /* Evaluation with repeat */
        t00 = myclock();
        for (i=0; i<evalCount; i++)
            retVal = vec_jac(tag,1,indepDim,1,indeps,UP,VP);
        t01 = myclock();

        fprintf(stdout,"   (repeat)");
        fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit,
                (t01-t00)/evalCount);

        /*----------------------------------------------------------------------*/
        /* Debug infos */
        if (controlParameters[cpVecJac] > 1) {
            fprintf(stdout,"\n    Return value: %d\n",retVal);
        }

        /*----------------------------------------------------------------------*/
        /* Free tensors */
        delete[] UP;
        delete[] VP;
    }


    /****************************************************************************/
    /*                                                               14. JACVEC */
    if (controlParameters[cpJacVec]) {
        fprintf(stdout,"--------------------------------------------------------");
        fprintf(stdout,"\nTASK %d: jac_vec(tag, m=1, n=%d, X[n], V[n], U[m])\n",
                taskCount++,indepDim);

        /*----------------------------------------------------------------------*/
        /* Allocation & initialisation of tensors */
        UP = new double[1];
        VP = new double[indepDim];
        for (i=0; i<indepDim; i++)
            VP[i] = (double)rand();

        /*----------------------------------------------------------------------*/
        /* Evaluation */
        t00 = myclock();
        for (i=0; i<evalCount; i++)
            retVal = jac_vec(tag,1,indepDim,indeps,VP,UP);
        t01 = myclock();

        fprintf(stdout,"           ");
        fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit,
                (t01-t00)/evalCount);

        /*----------------------------------------------------------------------*/
        /* Debug infos */
        if (controlParameters[cpJacVec] > 1) {
            fprintf(stdout,"\n    Return value: %d\n",retVal);
        }

        /*----------------------------------------------------------------------*/
        /* Free tensors */
        delete[] UP;
        delete[] VP;
    }


    /****************************************************************************/
    /*                                                              15. HESSIAN */
    if (controlParameters[cpHessian]) {
        fprintf(stdout,"--------------------------------------------------------");
        fprintf(stdout,"\nTASK %d: hessian(tag, n=%d, X[n], lower triangle of H[n][n])\n",
                taskCount++,indepDim);

        /*----------------------------------------------------------------------*/
        /* Allocation & initialisation of tensors */
        HPP = new double*[indepDim];
        for (i=0; i<indepDim; i++)
            HPP[i] = new double[indepDim];

        /*----------------------------------------------------------------------*/
        /* Evaluation */
        t00 = myclock();
        for (i=0; i<evalCount; i++)
            retVal = hessian(tag,indepDim,indeps,HPP);
        t01 = myclock();

        fprintf(stdout,"           ");
        fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit,
                (t01-t00)/evalCount);

        /*----------------------------------------------------------------------*/
        /* Debug infos */
        if (controlParameters[cpHessian] > 1) {
            fprintf(stdout,"\n    Return value: %d\n",retVal);
        }

        /*----------------------------------------------------------------------*/
        /* Free tensors */
        for (i=0; i<indepDim; i++)
            delete[] HPP[i];
        delete[] HPP;
    }


    /****************************************************************************/
    /*                                                              16. HESSVEC */
    if (controlParameters[cpHessVec]) {
        fprintf(stdout,"--------------------------------------------------------");
        fprintf(stdout,"\nTASK %d: hess_vec(tag, n=%d, X[n], V[n], W[n])\n",
                taskCount++,indepDim);

        /*----------------------------------------------------------------------*/
        /* Allocation & initialisation of tensors */
        VP = new double[indepDim];
        for (i=0; i<indepDim; i++)
            VP[i] = (double)rand();
        WP = new double[indepDim];

        /*----------------------------------------------------------------------*/
        /* Evaluation */
        t00 = myclock();
        for (i=0; i<evalCount; i++)
            retVal = hess_vec(tag,indepDim,indeps,VP,WP);
        t01 = myclock();

        fprintf(stdout,"           ");
        fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit,
                (t01-t00)/evalCount);

        /*----------------------------------------------------------------------*/
        /* Debug infos */
        if (controlParameters[cpHessVec] > 1) {
            fprintf(stdout,"\n    Return value: %d\n",retVal);
        }

        /*----------------------------------------------------------------------*/
        /* Free tensors */
        delete[] VP;
        delete[] WP;
    }


    /****************************************************************************/
    /*                                                           17. LAGHESSVEC */
    if (controlParameters[cpLagHessVec]) {
        fprintf(stdout,"--------------------------------------------------------");
        fprintf(stdout,"\nTASK %d: lagra_hess_vec(tag, m=1, n=%d, X[n], U[m], V[n], W[n])\n",
                taskCount++,indepDim);

        /*----------------------------------------------------------------------*/
        /* Allocation & initialisation of tensors */
        UP = new double[1];
        UP[0] = (double)rand();
        VP = new double[indepDim];
        for (i=0; i<indepDim; i++)
            VP[i] = (double)rand();
        WP = new double[indepDim];

        /*----------------------------------------------------------------------*/
        /* Evaluation */
        t00 = myclock();
        for (i=0; i<evalCount; i++)
            retVal = lagra_hess_vec(tag,1,indepDim,indeps,UP,VP,WP);
        t01 = myclock();

        fprintf(stdout,"           ");
        fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit,
                (t01-t00)/evalCount);

        /*----------------------------------------------------------------------*/
        /* Debug infos */
        if (controlParameters[cpLagHessVec] > 1) {
            fprintf(stdout,"\n    Return value: %d\n",retVal);
        }

        /*----------------------------------------------------------------------*/
        /* Free tensors */
        delete[] VP;
        delete[] WP;
        delete[] UP;
    }


    /****************************************************************************/
    /*                                                               18. TENSOR */
    if (controlParameters[cpTensor]) {
        fprintf(stdout,"--------------------------------------------------------");
        fprintf(stdout,"\nTASK %d: tensor_eval(tag, m =1, n=%d, d=%d, p=%d, X[n], tensor[m][dim], S[n][p])\n",
                taskCount++,indepDim,degree, pTR);
        fprintf(stdout,"\n         dim = ((p+d) over d)\n");

        /*----------------------------------------------------------------------*/
        /* Allocation & initialisation of tensors */
        dim = binomi(pTR+degree,degree);
        TPP = new double*[1];
        TPP[0] = new double[dim];
        SPP = new double*[indepDim];
        for (i=0; i<indepDim; i++) {
            SPP[i] = new double[pTR];
            for (j=0; j<pTR; j++)
                SPP[i][j]=(i==j)?1.0:0.0;
        }

        /*----------------------------------------------------------------------*/
        /* tensor evaluation */
        t00 = myclock();
        for (i=0; i<evalCount; i++)
            tensor_eval(tag,1,indepDim,degree,pTR,indeps,TPP,SPP);
        t01 = myclock();

        fprintf(stdout,"           ");
        fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit,
                (t01-t00)/evalCount);

        /*----------------------------------------------------------------------*/
        /* Debug infos */
    if (controlParameters[cpTensor] > 1) {}

        /*----------------------------------------------------------------------*/
        /* Free tensors */
        delete[] TPP[0];
        delete[] TPP;
        for (i=0; i<indepDim; i++)
            delete[] SPP[i];
        delete[] SPP;
    }


    /****************************************************************************/
    /*                                                       19. INVERSE TENSOR */
    if (controlParameters[cpInvTensor] && (1==indepDim)) {
        fprintf(stdout,"--------------------------------------------------------");
        fprintf(stdout,"\nTASK %d: inverse_tensor_eval(tag, m=n=1, d=%d, p=%d, X[n], tensor[m][dim], S[n][p])\n",
                taskCount++,degree, pTR);
        fprintf(stdout,"\n         dim = ((p+d) over d)\n");

        /*----------------------------------------------------------------------*/
        /* Allocation & initialisation of tensors */
        dim = binomi(pTR+degree,degree);
        TPP = new double*[1];
        TPP[0] = new double[dim];
        SPP = new double*[1];
        SPP[0] = new double[pTR];
        for (j=0; j<pTR; j++)
            SPP[0][j]=(0==j)?1.0:0.0;

        /*----------------------------------------------------------------------*/
        /* tensor evaluation */
        t00 = myclock();
        for (i=0; i<evalCount; i++)
            inverse_tensor_eval(tag,1,degree,pTR,indeps,TPP,SPP);
        t01 = myclock();

        fprintf(stdout,"           ");
        fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit,
                (t01-t00)/evalCount);

        /*----------------------------------------------------------------------*/
        /* Debug infos */
    if (controlParameters[cpInvTensor] > 1) {}

        /*----------------------------------------------------------------------*/
        /* Free tensors */
        delete[] TPP[0];
        delete[] TPP;
        delete[] SPP[0];
        delete[] SPP;
    }

    return 1;
}
예제 #12
0
/*                                                             MAIN PROGRAM */
int main() {
    int n,i,it;
    size_t tape_stats[STAT_SIZE];

    /*--------------------------------------------------------------------------*/
    /* Input */
    fprintf(stdout,"SPEELPENNINGS PRODUCT Type 1 (ADOL-C Example)\n\n");
    fprintf(stdout,"number of independent variables = ?  \n");
    scanf("%d",&n);
    int itu;
    fprintf(stdout,"number of evaluations = ?  \n");
    scanf("%d",&itu);

    /*--------------------------------------------------------------------------*/
    double yp=0.0;                 /* 0. time  (undifferentiated double code) */
    double *xp = new double[n];
    /* Init */
    for (i=0;i<n;i++)
        xp[i] = (i+1.0)/(2.0+i);

    double t00 = myclock(1);
    for (it=0; it<itu; it++) {
        yp = 1.0;
        for (i=0; i<n; i++)
            yp *= xp[i];
    }
    double t01 = myclock();

    /*--------------------------------------------------------------------------*/
    double yout=0;                             /* 1. time (tracing ! no keep) */

    double t10 = myclock();
    trace_on(TAG);
    adouble* x;
    x = new adouble[n];
    adouble y;
    y = 1;
    for (i=0; i<n; i++) {
        x[i] <<= xp[i];
        y *= x[i];
    }
    y >>= yout;
    delete [] x;
    trace_off();
    double t11 = myclock();

    fprintf(stdout,"%E =? %E  function values should be the same \n",yout,yp);

    /*--------------------------------------------------------------------------*/
    tapestats(TAG,tape_stats);

    fprintf(stdout,"\n    independents            %zu\n",tape_stats[NUM_INDEPENDENTS]);
    fprintf(stdout,"    dependents              %zu\n",tape_stats[NUM_DEPENDENTS]);
    fprintf(stdout,"    operations              %zu\n",tape_stats[NUM_OPERATIONS]);
    fprintf(stdout,"    operations buffer size  %zu\n",tape_stats[OP_BUFFER_SIZE]);
    fprintf(stdout,"    locations buffer size   %zu\n",tape_stats[LOC_BUFFER_SIZE]);
    fprintf(stdout,"    constants buffer size   %zu\n",tape_stats[VAL_BUFFER_SIZE]);
    fprintf(stdout,"    maxlive                 %zu\n",tape_stats[NUM_MAX_LIVES]);
    fprintf(stdout,"    valstack size           %zu\n\n",tape_stats[TAY_STACK_SIZE]);

    /*--------------------------------------------------------------------------*/
    double **r = new double*[1];
    r[0] = new double[1];
    r[0][0] = yp;
    double err;
    double *z = new double[n];
    double *g = new double[n];
    double* h = new double[n];
    double *ind = new double[n];

    /*--------------------------------------------------------------------------*/
    double t60 = myclock();                      /* 6. time (forward no keep) */
    for (it=0; it<itu; it++)
        forward(TAG,1,n,0,xp,*r);
    double t61 = myclock();

    /*--------------------------------------------------------------------------*/
    double t20 = myclock();                         /* 2. time (forward+keep) */
    for (it=0; it<itu; it++)
        forward(TAG,1,n,1,xp,*r);
    double t21 = myclock();

    /*--------------------------------------------------------------------------*/
    double t30 = myclock();                              /* 3. time (reverse) */
    for (it=0; it<itu; it++)
        reverse(TAG,1,n,0,1.0,g);
    double t31 = myclock();

    err=0;
    for (i=0; i<n; i++) // Compare with deleted product
    { err = maxabs(err,xp[i]*g[i]/r[0][0] - 1.0);
        ind[i] = xp[i];
    }

    fprintf(stdout,"%E = maximum relative errors in gradient (fw+rv)\n",err);

    /*--------------------------------------------------------------------------*/
    double t40 = myclock();                             /* 4. time (gradient) */
    for (it=0; it<itu; it++)
        gradient(TAG,n,ind,z);  //last argument lagrange is ommitted
    double t41 = myclock();

    err = 0;
    for (i=0; i<n; i++)  // Compare with previous numerical result
        err =  maxabs(err,g[i]/z[i] - 1.0);

    fprintf(stdout,"%E = gradient error should be exactly zero \n",err);

    /*--------------------------------------------------------------------------*/
    double *tan = new double[n];            /* 5. time (first row of Hessian) */
    for (i=1; i<n; i++)
        tan[i] = 0.0 ;
    tan[0]=1.0;

    double t50 = myclock();
    for (it=0; it<itu; it++)
        hess_vec(TAG,n,ind,tan,h);  // Computes Hessian times direction tan.
    double t51 = myclock();

    err = abs(h[0]);
    for (i=1; i<n; i++) //Compare with doubly deleted product
        err = maxabs(err,xp[0]*h[i]/g[i]-1.0);

    fprintf(stdout,"%E = maximum relative error in Hessian column \n",err);

    /*--------------------------------------------------------------------------*/
    double h1n = h[n-1];                                /* Check for symmetry */
    tan[0]=0;
    tan[n-1]=1;
    hess_vec(TAG,n,ind,tan,h);   // Computes Hessian times direction tan.

    fprintf(stdout,
            "%E = %E (1,n) and (n,1) entry should be the same\n",h1n,h[0]);

    /*--------------------------------------------------------------------------*/
    /* output of results */
    if (t01-t00) {
        double rtu = 1.0/(t01-t00);
        fprintf(stdout,"\n\n times for ");
        fprintf(stdout,"\n unitime          : \t%E  seconds",(t01-t00)/itu);
        fprintf(stdout,"\n tracing          : \t%E",(t11-t10)*rtu*itu);
        fprintf(stdout,"   units \t%E seconds",(t11-t10));
        fprintf(stdout,
                "\n----------------------------------------------------------");
        fprintf(stdout,"\n forward (no keep): \t%E",(t61-t60)*rtu);
        fprintf(stdout,"   units \t%E seconds",(t61-t60)/itu);
        fprintf(stdout,"\n forward + keep   : \t%E",(t21-t20)*rtu);
        fprintf(stdout,"   units \t%E seconds",(t21-t20)/itu);
        fprintf(stdout,"\n reverse          : \t%E",(t31-t30)*rtu);
        fprintf(stdout,"   units \t%E seconds",(t31-t30)/itu);
        fprintf(stdout,
                "\n----------------------------------------------------------");
        fprintf(stdout,"\n gradient         : \t%E",(t41-t40)*rtu);
        fprintf(stdout,"   units \t%E seconds",(t41-t40)/itu);
        fprintf(stdout,"\n hess*vec         : \t%E",(t51-t50)*rtu);
        fprintf(stdout,"   units \t%E seconds\n",(t51-t50)/itu);
    } else
        fprintf(stdout,"\n-> zero timing due to small problem dimension \n");

    return 1;
}
예제 #13
0
int main(int argc, char ** argv)
{
  udpt = udp_throw_init(0);

  if (!udpt) {
    fprintf(stderr,"ERROR: unable to generate data\n");
    return -1;
  }
    
  read_cmd_options(argc,argv);

  // initialize RNG and seeds
  // use different keyoffsets in parallel, so key spaces are disjoint

  if (!kseed) kseed = (unsigned int) time(NULL);
  kseed += whichgen;
  vseed += whichgen;
  srand(kseed);
  keyoffset += whichgen*keyoffset;

  // create power-law distribution

  fprintf(stdout,"initializing power-law distribution ...\n");
  power_law_distribution_t * power = 
    power_law_initialize(concentration,maxkeys,RAND_MAX);
  
  // packet buffer length of 64 bytes per datum is ample

  int buflen = 64*perpacket;
  char *buf = (char *) malloc(buflen*sizeof(char));
  memset(buf,0,buflen);

  // generate the datums in packets

  fprintf(stdout,"starting generator ...\n");
  fflush(stdout);

  // plot
  //int *ycount = (int *) malloc(maxkeys*sizeof(int));
  //for (int i = 0; i < maxkeys; i++) ycount[i] = 0;

  double timestart = myclock();

  for (uint64_t i = 0; i < npacket; i++) {

    // packet header

    int offset = snprintf(buf,buflen,"packet %" PRIu64 "\n",i*numgen+whichgen);

    // generate one packet with perpacket datums

    uint64_t key;
    for (int j = 0; j < perpacket; j++) {
      uint64_t rn = rand_r(&kseed);
      while ((key = power_law_simulate(rn,power)) >= maxkeys)
        rn = rand_r(&kseed);

      // plot
      //ycount[key]++;

      key += keyoffset;
      uint32_t value = 0;
      uint32_t bias = 0;
      if ((evahash((uint8_t*) &key,sizeof(uint64_t),mask) & 0xFF) == 0x11) {
        bias = 1;
        value = ((rand_r(&vseed) & 0xF) == 0);
      } else value = rand_r(&vseed) & 0x1;

      offset += snprintf(buf+offset,buflen-offset,
                         "%" PRIu64 ",%u,%u\n",key,value,bias);
    }

    // write packet to UDP port(s)

    udp_throw_data(udpt,buf,offset);

    // sleep if rate is throttled 

    if (rate) {
      double n = 1.0*(i+1)*perpacket;
      double elapsed = myclock() - timestart;
      double actual_rate = n/elapsed;
      if (actual_rate > rate) {
        double delay = n/rate - elapsed;
        usleep(1.0e6*delay);
      }
    }
  }

  double timestop = myclock();

  // plot
  //FILE *fp = fopen("tmp.plot","w");
  //for (int i = 0; i < maxkeys; i++) 
  //  fprintf(fp,"%d %d\n",i,ycount[i]);
  //fclose(fp);

  uint64_t ndatum = npacket * perpacket;
  fprintf(stdout,"packets emitted = %" PRIu64 "\n",npacket);
  fprintf(stdout,"datums emitted = %" PRIu64 "\n",ndatum);
  fprintf(stdout,"elapsed time (secs) = %g\n",timestop-timestart);
  fprintf(stdout,"generation rate (datums/sec) = %g\n",
          ndatum/(timestop-timestart));
  fclose(stdout);

  power_law_destroy(power);

  // loop on sending one-integer STOP packets, STOPRATE per sec
  // allows receivers to shut down cleanly, even if behind

  int offset = snprintf(buf,buflen,"%d\n",whichgen);
  while (1) {
    udp_throw_data(udpt,buf,offset);
    usleep(1000000/STOPRATE);
  }

  udp_throw_destroy(udpt);

  return 0;
}
예제 #14
0
int main(int argc, char **argv)
{
  udpt = udp_throw_init(0);

  if (!udpt) {
    fprintf(stderr,"ERROR: unable to generate data\n");
    return -1;
  }

  read_cmd_options(argc,argv);
  
  // initialize RNG and seeds
  // using different kseed in parallel will create disjoint key spaces

  if (!kseed) kseed = (unsigned int) time(NULL);
  kseed += whichgen;
  vseed += whichgen;
  srand(kseed);

  // create active set

  fprintf(stdout,"building active set ...\n");
  active_ring_t *aring = build_rings(16384,NACTIVE,65536,kseed,vseed,mask);

  // packet buffer length of 64 bytes per datum is ample

  int buflen = 64*perpacket;
  char *buf = (char *) malloc(buflen*sizeof(char));
  memset(buf,0,buflen);

  // generate the datums in packets

  fprintf(stdout,"starting generator ...\n");
  fflush(stdout);

  double timestart = myclock();

  for (uint64_t i = 0; i < npacket; i++) {

    // packet header

    int offset = snprintf(buf,buflen,"packet %" PRIu64 "\n",i*numgen+whichgen);

    // create packet containing perpacket datums

    offset = print_packet(aring,perpacket,buf,buflen,offset);

    // write the packet to UDP port(s)

    udp_throw_data(udpt,buf,offset);

    // sleep if rate is throttled 

    if (rate) {
      double n = 1.0*(i+1)*perpacket;
      double elapsed = myclock() - timestart;
      double actual_rate = n/elapsed;
      if (actual_rate > rate) {
        double delay = n/rate - elapsed;
        usleep(1.0e6*delay);
      }
    }
  }
  
  double timestop = myclock();

  print_stats(aring);
  uint64_t ndatum = npacket * perpacket;
  fprintf(stdout,"packets emitted = %" PRIu64 "\n",npacket);
  fprintf(stdout,"datums emitted = %" PRIu64 "\n",ndatum);
  fprintf(stdout,"elapsed time (secs) = %g\n",timestop-timestart);
  fprintf(stdout,"generation rate (datums/sec) = %g\n",
          ndatum/(timestop-timestart));
  fclose(stdout);

  // loop on sending one-integer STOP packets, STOPRATE per sec
  // allows receivers to shut down cleanly, even if behind

  int offset = snprintf(buf,buflen,"%d\n",whichgen);
  while (1) {
    udp_throw_data(udpt,buf,offset);
    usleep(1000000/STOPRATE);
  }

  udp_throw_destroy(udpt);

  return 0;
}
예제 #15
0
void CDXL_hal::dxl_hal_set_timeout( int NumRcvByte )
{
	glStartTime = myclock();
	gfRcvWaitTime = (float)(gfByteTransTime*(float)NumRcvByte + 5.0f);
}