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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1662 - (show annotations)
Tue Jul 22 01:49:21 2008 UTC (11 years, 3 months ago) by ksteube
File MIME type: text/plain
File size: 4078 byte(s)
Ran a simple Escript program on Mac OS Darwin. Still much to do.

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

  ViewVC Help
Powered by ViewVC 1.1.26