/[escript]/trunk/esysUtils/src/blocktimer.c
ViewVC logotype

Contents of /trunk/esysUtils/src/blocktimer.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3981 - (show annotations)
Fri Sep 21 02:47:54 2012 UTC (6 years, 9 months ago) by jfenwick
File MIME type: text/plain
File size: 4520 byte(s)
First pass of updating copyright notices
1
2 /*****************************************************************************
3 *
4 * Copyright (c) 2003-2012 by University of Queensland
5 * http://www.uq.edu.au
6 *
7 * Primary Business: Queensland, Australia
8 * Licensed under the Open Software License version 3.0
9 * http://www.opensource.org/licenses/osl-3.0.php
10 *
11 * Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 * Development since 2012 by School of Earth Sciences
13 *
14 *****************************************************************************/
15
16
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include <time.h>
21 #include <search.h>
22 #ifdef _OPENMP
23 #include <omp.h>
24 #endif
25
26 #include "blocktimer.h"
27 #include "stdlib.h"
28 #include "string.h"
29
30 #ifdef ESYS_MPI
31 #include "mpi.h"
32 #endif
33
34 #ifdef BLOCKTIMER
35 static char *g_names[NUM_TIMERS]; /* Names of the timers */
36 static int g_count[NUM_TIMERS]; /* How many times was the timer incremented? */
37 static double g_times[NUM_TIMERS]; /* The total time spent in the block */
38 static double g_start_time; /* Start time for the entire program */
39 static int g_initialized = 0; /* Has the blocktimer been initialized? */
40 static int g_end_computed = 0; /* Has the end time been set? */
41 #endif /* BLOCKTIMER */
42
43 void
44 blocktimer_initialize()
45 {
46 #ifdef BLOCKTIMER
47 int i;
48
49 for (i=0; i<NUM_TIMERS; i++) {
50 g_names[i] = (char *)NULL;
51 g_times[i] = 0.0;
52 g_count[i] = 0;
53 }
54
55 if (hcreate(NUM_TIMERS) == 0) {
56 perror("hcreate");
57 fprintf(stderr, "blocktimer_initialize: Could not initialize hash table\n");
58 exit(1);
59 }
60
61 g_initialized = 1;
62
63 g_start_time = blocktimer_time();
64
65 /* Initialize timer for "entire program" to zero so it appears first in the report */
66 blocktimer_increment("entire program", g_start_time);
67 g_count[0] = 0; /* Reset counter for "entire program" to zero */
68 #endif /* BLOCKTIMER */
69 }
70
71 void
72 blocktimer_increment(__const char *name, double start_time)
73 {
74 #ifdef BLOCKTIMER
75 int id;
76
77 if (!g_initialized) { return; }
78
79 id = blocktimer_getOrCreateTimerId(name);
80
81 g_times[id] += blocktimer_time() - start_time;
82 g_count[id] += 1;
83 #endif /* BLOCKTIMER */
84 }
85
86 int
87 blocktimer_getOrCreateTimerId(__const char *name)
88 {
89 int id=0;
90 #ifdef BLOCKTIMER
91 char *tmp_str;
92 static int nextId = 0; /* Next timer ID to assign */
93 ENTRY item, *found_item;
94
95 if (!g_initialized) { return(0); }
96
97 /* Has a timer with 'name' already been defined? */
98 item.key = (char *)name;
99 item.data = (void *) NULL;
100 found_item = hsearch(item, FIND);
101
102 if (found_item != NULL) { /* Already defined so retrieve it from the hash */
103 /* Return the ID of the entry we found */
104 int *idTmp = found_item->data;
105 id = *idTmp;
106 }
107 else { /* Not already defined so create one */
108 /* malloc new int, can't use stack var or all items share same data */
109 int *idTmp = (int *)malloc(sizeof(int));
110 /* Enter the new name in the hash */
111 if (nextId >= NUM_TIMERS) {
112 fprintf(stderr, "blocktimer: exceeded limit of %d timers, increase NUM_TIMERS\n", NUM_TIMERS);
113 exit(1);
114 }
115 *idTmp = nextId++;
116 item.key = (char *)name;
117 item.data = (void *) idTmp;
118 hsearch(item, ENTER);
119 id = *idTmp;
120 /* Make a copy of the name and save with other names */
121 tmp_str = malloc(strlen(name)+1);
122 strcpy(tmp_str, name);
123 g_names[id] = tmp_str;
124 }
125
126 #endif /* BLOCKTIMER */
127 return(id);
128 }
129
130 void
131 blocktimer_reportSortByName()
132 {
133 #ifdef BLOCKTIMER
134 int i;
135
136 if (!g_initialized) { return; }
137
138 if (!g_end_computed) {
139 blocktimer_increment("entire program", g_start_time);
140 g_end_computed = 1;
141 }
142 printf("BlockTimer sorted by name (sorting TBD):\n");
143 for(i=0; i<NUM_TIMERS; i++) {
144 if (g_names[i] != (char *) NULL) {
145 printf(" %7d %15.2f %s\n", g_count[i], g_times[i], g_names[i]);
146 }
147 }
148 #endif /* BLOCKTIMER */
149 }
150
151 void
152 blocktimer_reportSortByTime()
153 {
154 #ifdef BLOCKTIMER
155 int i;
156
157 if (!g_initialized) { return; }
158
159 if (!g_end_computed) {
160 blocktimer_increment("entire program", g_start_time);
161 g_end_computed = 1;
162 }
163 printf("BlockTimer sorted by time (sorting TBD):\n");
164 for(i=0; i<NUM_TIMERS; i++) {
165 if (g_names[i] != (char *) NULL) {
166 printf(" %7d %15.2f seconds for %s\n", g_count[i], g_times[i], g_names[i]);
167 }
168 }
169 #endif /* BLOCKTIMER */
170 }
171
172 /* Copied from Paso_timer() */
173 double
174 blocktimer_time()
175 {
176 double out=0.0;
177 #ifdef ESYS_MPI
178 out = MPI_Wtime();
179 #else
180 #ifdef _OPENMP
181 out=omp_get_wtime();
182 #else
183 out=((double) clock())/CLOCKS_PER_SEC;
184 #endif
185 #endif
186 return(out);
187 }
188

  ViewVC Help
Powered by ViewVC 1.1.26