    /***********************************************************************
                   Copyright (c) 2007 SKY Computers, Inc.
    
     Redistribution and use in source and binary forms are permitted
     provided that this notice is preserved and that due credit is given to
     SKY Computers, Inc. The name of SKY Computers, Inc. may not be used to
     endorse or promote products derived from this software without
     specific prior written permission. This software is provided ``as is''
     without express or implied warranty.
    ************************************************************************/

#include "sky_ex_inc.h"

#define COUNT   (10)
#define V_SIZE  (1024)

/* Define some TimeTrac variables. */
TTHandle trace_log;
TTEventHandle tt_hello_bgn, tt_hello_end;
TTEventHandle tt_vran_bgn, tt_vran_end;
TTEventHandle tt_vadd_bgn, tt_vadd_end;
TTEventHandle tt_vmul_bgn, tt_vmul_end;
TTEventHandle tt_vsub_bgn, tt_vsub_end;
TTEventHandle tt_vsin_bgn, tt_vsin_end;

/* ---------------------------------------------------------------------- */ 

/*
 >++
 *
 *   void tt_init (): Set up the TimeTrac file (open the file, define the 
 *       range events) for the main processing thread. Here we define
 *       several range events that we will log.
 *
 *       Returns: Nothing
 >--
 */

int tt_init ()
{
  unsigned group_mask = 0xffffffff;
  unsigned group_id = 0x1;
  int use_thread_safe = 0;
  int num_events = 10000;
  int context_depth = 1;
  int status = 4;
  char filename[256];

  /* Create the trace file name */
  sprintf (filename, "multiple_events");

  /* Create all neccessary handles */
  status = time_trac_open (filename, use_thread_safe, num_events, group_mask,
			   TT_NO_AUTO_SAVE, TT_CREATE_FILE, context_depth, &trace_log);
  VERIFY_TIMETRAC_STATUS (status);
  
  status = time_trac_reg_range_event (trace_log, "a. hello", "Green",
				      group_id, &tt_hello_bgn, &tt_hello_end);
  VERIFY_TIMETRAC_STATUS (status);

  status = time_trac_reg_range_event (trace_log, "b. vinput", "Brown",
				      group_id, &tt_vran_bgn, &tt_vran_end);
  VERIFY_TIMETRAC_STATUS (status);

  status = time_trac_reg_range_event (trace_log, "c. vadd", "Orange",
				      group_id, &tt_vadd_bgn, &tt_vadd_end);
  VERIFY_TIMETRAC_STATUS (status);

  status = time_trac_reg_range_event (trace_log, "d. vmult", "Violet",
				      group_id, &tt_vmul_bgn, &tt_vmul_end);
  VERIFY_TIMETRAC_STATUS (status);
  status = time_trac_reg_range_event (trace_log, "e. vsub", "Blue",
				      group_id, &tt_vsub_bgn, &tt_vsub_end);
  VERIFY_TIMETRAC_STATUS (status);
}

/* ---------------------------------------------------------------------- */ 

/*
 >++
 *
 *   void tt_term (): Save all of the TimeTrac events and then close the 
 *       TimeTrac file.
 *
 *       Returns: Nothing
 >--
 */

int tt_term ()
{
  int status;

  /* Save the events by overwritting any existing file, close the TimeTrac file. */
  status = time_trac_save (trace_log, TT_OVERWRITE_FILE);
  VERIFY_TIMETRAC_STATUS (status);
  status = time_trac_close (trace_log);
  VERIFY_TIMETRAC_STATUS (status);
}

/* ---------------------------------------------------------------------- */ 

/*
 >++
 *
 *   int main (int argc, char **argv, char **arge): This is the main program 
 *       that calls several functions, using TimeTrac to time each one. It 
 *       then repeats 10 times.
 *   
 *       Returns: Exits when done.
 >--
 */

int main (int argc, char **argv, char **arge)
{
  int i = -1;
  float *src1, *src2;
  float *sum1, *mult;
  float *dst0;
    
  /* Set up the TimeTrac parameters. */
  tt_init ();
  
  printf ("Multiple Events -- %d\n", COUNT);
  
  src1 = (float *) malloc (V_SIZE * sizeof (float));
  src2 = (float *) malloc (V_SIZE * sizeof (float));
  sum1 = (float *) malloc (V_SIZE * sizeof (float));
  mult = (float *) malloc (V_SIZE * sizeof (float));
  dst0 = (float *) malloc (V_SIZE * sizeof (float));
  
  /*
   *  Time each of the called functions. The logged time includes
   *  the call overhead.
   */
  for (i = 0; i < COUNT; i++)
  {
      time_trac_record (trace_log, tt_hello_bgn, (float)i);
      printf ("Doing Multiple Events -- %d.\n", i);
      time_trac_record (trace_log, tt_hello_end, (float)i);

      time_trac_record (trace_log, tt_vran_bgn, (float)i);
      vinput (src1, V_SIZE);
      time_trac_record (trace_log, tt_vran_end, (float)i);

      time_trac_record (trace_log, tt_vran_bgn, (float)i);
      vinput (src2, V_SIZE);
      time_trac_record (trace_log, tt_vran_end, (float)i);

      time_trac_record (trace_log, tt_vadd_bgn, (float)i);
      vadd (src1, src2, sum1, V_SIZE);
      time_trac_record (trace_log, tt_vadd_end, (float)i);

      time_trac_record (trace_log, tt_vran_bgn, (float)i);
      vinput (src1, V_SIZE);
      time_trac_record (trace_log, tt_vran_end, (float)i);

      time_trac_record (trace_log, tt_vmul_bgn, (float)i);
      vmul (src1, sum1, dst0, V_SIZE);
      time_trac_record (trace_log, tt_vmul_end, (float)i);

      time_trac_record (trace_log, tt_vran_bgn, (float)i);
      vinput (src2, V_SIZE);
      time_trac_record (trace_log, tt_vran_end, (float)i);

      time_trac_record (trace_log, tt_vsub_bgn, (float)i);
      vsub (dst0, src2, dst0, V_SIZE);
      time_trac_record (trace_log, tt_vsub_end, (float)i);
  }

  /* Save the events and exit */
  tt_term ();
  return (0);
}

/* --------------------------- End of Module ---------------------------- */
 

