Skip to content
Snippets Groups Projects
VISCTimer.h 4.77 KiB
//===------------ VISCTimer.h - Header file for "VISC Timer API" ----------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef VISC_TIMER_HEADER
#define VISC_TIMER_HEADER

/************************** Timer Routines ***************************/
extern "C" {

/* A time or duration. */
//#if _POSIX_VERSION >= 200112L
typedef unsigned long long visc_Timestamp; /* time in microseconds */
//#else
//# error "Timestamps not implemented"
//#endif

enum visc_TimerState {
  visc_Timer_STOPPED,
  visc_Timer_RUNNING,
};

struct visc_Timer {
  enum visc_TimerState state;
  visc_Timestamp elapsed;       /* Amount of time elapsed so far */
  visc_Timestamp init;		/* Beginning of the current time interval,
				 * if state is RUNNING.  End of the last
				 * recorded time interfal otherwise.  */
};

/* Reset a timer.
 * Use this to initialize a timer or to clear
 * its elapsed time.  The reset timer is stopped.
 */
void
visc_ResetTimer(struct visc_Timer *timer);

/* Start a timer.  The timer is set to RUNNING mode and
 * time elapsed while the timer is running is added to
 * the timer.
 * The timer should not already be running.
 */
void
visc_StartTimer(struct visc_Timer *timer);

/* Stop a timer.
 * This stops adding elapsed time to the timer.
 * The timer should not already be stopped.
 */
void
visc_StopTimer(struct visc_Timer *timer);

/* Get the elapsed time in seconds. */
double
visc_GetElapsedTime(struct visc_Timer *timer);

/* Execution time is assigned to one of these categories. */
enum visc_TimerID {
  visc_TimerID_NONE = 0,
  visc_TimerID_IO,		/* Time spent in input/output */
  visc_TimerID_KERNEL,		/* Time spent computing on the device,
				 * recorded asynchronously */
  visc_TimerID_COPY,		/* Time spent synchronously moving data
				 * to/from device and allocating/freeing
				 * memory on the device */
  visc_TimerID_DRIVER,		/* Time spent in the host interacting with the
				 * driver, primarily for recording the time
                                 * spent queueing asynchronous operations */
  visc_TimerID_COPY_ASYNC,	/* Time spent in asynchronous transfers */
  visc_TimerID_COMPUTE,		/* Time for all program execution other
				 * than parsing command line arguments,
				 * I/O, kernel, and copy */
  visc_TimerID_OVERLAP,		/* Time double-counted in asynchronous and
				 * host activity: automatically filled in,
				 * not intended for direct usage */
  // GPU FUNCTION
  visc_TimerID_INIT_CTX,
  visc_TimerID_CLEAR_CTX,
  visc_TimerID_COPY_SCALAR,
  visc_TimerID_COPY_PTR,
  visc_TimerID_MEM_FREE,
  visc_TimerID_READ_OUTPUT,
  visc_TimerID_SETUP,
  visc_TimerID_MEM_TRACK,
  visc_TimerID_MEM_UNTRACK,
  visc_TimerID_MISC,
  // LAUNCH FUNCTION
  visc_TimerID_PTHREAD_CREATE,
  visc_TimerID_ARG_PACK,
  visc_TimerID_ARG_UNPACK,
  visc_TimerID_COMPUTATION,
  visc_TimerID_OUTPUT_PACK,
  visc_TimerID_OUTPUT_UNPACK,

  visc_TimerID_LAST		/* Number of timer IDs */
};

/* Dynamic list of asynchronously tracked times between events */
struct visc_async_time_marker_list {
  char *label; // actually just a pointer to a string
  enum visc_TimerID timerID;	/* The ID to which the interval beginning
                                 * with this marker should be attributed */
  void * marker;
  //cudaEvent_t marker; 		/* The driver event for this marker */
  struct visc_async_time_marker_list *next;
};

struct visc_SubTimer {
  char *label;
  struct visc_Timer timer;
  struct visc_SubTimer *next;
};

struct visc_SubTimerList {
  struct visc_SubTimer *current;
  struct visc_SubTimer *subtimer_list;
};

/* A set of timers for recording execution times. */
struct visc_TimerSet {
  enum visc_TimerID current;
  struct visc_async_time_marker_list* async_markers;
  visc_Timestamp async_begin;
  visc_Timestamp wall_begin;
  struct visc_Timer timers[visc_TimerID_LAST];
  struct visc_SubTimerList *sub_timer_list[visc_TimerID_LAST];
};

/* Reset all timers in the set. */
void
visc_InitializeTimerSet(struct visc_TimerSet *timers);

void
visc_AddSubTimer(struct visc_TimerSet *timers, char *label, enum visc_TimerID visc_Category);
/* Select which timer the next interval of time should be accounted
 * to. The selected timer is started and other timers are stopped.
 * Using visc_TimerID_NONE stops all timers. */
inline void
visc_SwitchToTimer(struct visc_TimerSet *timers, enum visc_TimerID timer);

void
visc_SwitchToSubTimer(struct visc_TimerSet *timers, char *label, enum visc_TimerID category);

/* Print timer values to standard output. */
void
visc_PrintTimerSet(struct visc_TimerSet *timers);

/* Release timer resources */
void
visc_DestroyTimerSet(struct visc_TimerSet * timers);

}
#endif //VISC_RT_HEADER