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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1634 - (hide annotations)
Sat Jul 12 09:08:33 2008 UTC (11 years, 3 months ago) by phornby
File MIME type: text/plain
File size: 3933 byte(s)
linux_gcc_eg_options.py:
remove the std99 option, it is no longer needed as the code compiles without
C 1999 extension (need for these extensions elinminated in windows port).
Turn on all warnings except unknown pragmas. Should catch a lot of stuff.

SConstruct:
Impassioned plea

system_dep.h:
Add the standard incantation for dealing with const declarations
in C code called from C and C++

blocktimer:
Get the calling interface right for C code called from C and C++
and use __const as defined in system_dep.h
(Should be re-factored into compiler_dep.h file).

MeshAdapterFactory.cpp:
Since we have (effectively) no control over netCDF policy,
cast const char *'s to char *'s


1 ksteube 1314
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 phornby 1628 #include "malloc.h"
15     #include "string.h"
16 ksteube 1314
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 phornby 1634 blocktimer_increment(__const char *name, double start_time)
60 ksteube 1314 {
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 phornby 1634 blocktimer_getOrCreateTimerId(__const char *name)
75 ksteube 1314 {
76     int id=0;
77     #ifdef BLOCKTIMER
78     static int nextId = 0; /* Next timer ID to assign */
79     ENTRY item, *found_item;
80    
81     if (!g_initialized) { return(0); }
82    
83     /* Has a timer with 'name' already been defined? */
84     item.key = name;
85     item.data = (void *) NULL;
86     found_item = hsearch(item, FIND);
87    
88     if (found_item != NULL) { /* Already defined so retrieve it from the hash */
89     /* Return the ID of the entry we found */
90     int *idTmp = found_item->data;
91     id = *idTmp;
92     }
93     else { /* Not already defined so create one */
94     /* malloc new int, can't use stack var or all items share same data */
95     int *idTmp = (int *)malloc(sizeof(int));
96     /* Enter the new name in the hash */
97     if (nextId >= NUM_TIMERS) {
98     fprintf(stderr, "blocktimer: exceeded limit of %d timers, increase NUM_TIMERS\n", NUM_TIMERS);
99     exit(1);
100     }
101     *idTmp = nextId++;
102     item.key = name;
103     item.data = (void *) idTmp;
104     hsearch(item, ENTER);
105     id = *idTmp;
106 ksteube 1347 g_names[id] = (char *)strdup(name);
107 ksteube 1314 }
108    
109     #endif /* BLOCKTIMER */
110     return(id);
111     }
112    
113     void
114     blocktimer_reportSortByName()
115     {
116     #ifdef BLOCKTIMER
117     int i;
118    
119     if (!g_initialized) { return; }
120    
121     if (!g_end_computed) {
122     blocktimer_increment("entire program", g_start_time);
123     g_end_computed = 1;
124     }
125     printf("BlockTimer sorted by name (sorting TBD):\n");
126     for(i=0; i<NUM_TIMERS; i++) {
127     if (g_names[i] != (char *) NULL) {
128     printf(" %7d %15.2lf %s\n", g_count[i], g_times[i], g_names[i]);
129     }
130     }
131     #endif /* BLOCKTIMER */
132     }
133    
134     void
135     blocktimer_reportSortByTime()
136     {
137     #ifdef BLOCKTIMER
138     int i;
139    
140     if (!g_initialized) { return; }
141    
142     if (!g_end_computed) {
143     blocktimer_increment("entire program", g_start_time);
144     g_end_computed = 1;
145     }
146     printf("BlockTimer sorted by time (sorting TBD):\n");
147     for(i=0; i<NUM_TIMERS; i++) {
148     if (g_names[i] != (char *) NULL) {
149     printf(" %7d %15.2lf seconds for %s\n", g_count[i], g_times[i], g_names[i]);
150     }
151     }
152     #endif /* BLOCKTIMER */
153     }
154    
155     /* Copied from Paso_timer() */
156     double
157     blocktimer_time()
158     {
159     double out=0.0;
160     #ifdef PASO_MPI
161     out = MPI_Wtime();
162     #else
163     #ifdef _OPENMP
164     out=omp_get_wtime();
165     #else
166     out=((double) clock())/CLOCKS_PER_SEC;
167     #endif
168     #endif
169     return(out);
170     }
171    

  ViewVC Help
Powered by ViewVC 1.1.26