/[escript]/trunk-mpi-branch/escript/src/blockpapi.c
ViewVC logotype

Contents of /trunk-mpi-branch/escript/src/blockpapi.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1223 - (show annotations)
Fri Aug 3 02:40:39 2007 UTC (11 years, 8 months ago) by gross
File MIME type: text/plain
File size: 4303 byte(s)
first attemt towards an improved MPI version.  

1
2 #if 0
3
4 A simple library to make it easy to use PAPI to time a single block of code.
5
6 You can start and stop the timers as often as you like and the results will
7 be accumulated.
8
9 If you use the event PAPI_FP_OPS or NATV_FP_OPS_RETIRED your MFLOPS will be
10 computed based automatically.
11
12 Usage example:
13
14 #include "blockpapi.h"
15
16 /* Add a PAPI preset event */
17 blockpapi_addEvent(PAPI_FP_OPS, "total floating-point operations");
18
19 /* Add a couple native events */
20 blockpapi_addEvent(NATV_CPU_CYCLES, "total cpu cycles");
21 blockpapi_addEvent(NATV_BACK_END_BUBBLE_ALL, "cycles stalled for any reason");
22
23 blockpapi_start();
24 /* Compute something here */
25 blockpapi_stop();
26
27 blockpapi_writeReport();
28 blockpapi_writeSystemInfo();
29
30 Compile with cc -DBLOCKPAPI file.c blockpapi.c -lpapi
31
32 #endif
33
34
35
36
37 #include "blockpapi.h"
38
39 #ifdef BLOCKPAPI
40 #include "papi.h"
41 #endif
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46
47 #define MAX_COUNTERS 100
48
49 int g_numCounters=0;
50 int g_events[MAX_COUNTERS];
51 long_long_t g_values[MAX_COUNTERS];
52 char *g_descriptions[MAX_COUNTERS];
53 int g_numPasses = 0;
54 double g_totalMicroSecs = 0.0;
55 long_long_t g_numFpOps = -1;
56
57 void blockpapi_addEvent(int event, char *description) {
58 #ifdef BLOCKPAPI
59 if (g_numCounters >= MAX_COUNTERS) {
60 fprintf(stderr, "blockpapi_addEvent: too many counters, cannot add '%s'\n", description);
61 exit(1);
62 }
63 if (g_numCounters > PAPI_num_counters()) {
64 fprintf(stderr, "blockpapi_addEvent: CPU does not support this many counters, cannot add '%s'\n", description);
65 exit(1);
66 }
67 g_values[g_numCounters] = (long_long_t) 0;
68 g_events[g_numCounters] = event;
69 g_descriptions[g_numCounters] = strdup(description);
70 g_numCounters++;
71 #endif
72 }
73
74 void blockpapi_start() {
75 #ifdef BLOCKPAPI
76 int retval;
77 if (g_numCounters == 0) { return; }
78 g_numPasses++;
79 g_totalMicroSecs -= PAPI_get_real_usec();
80 if ((retval = PAPI_start_counters(g_events, g_numCounters)) != PAPI_OK) {
81 fprintf(stderr, "blockpapi_start: PAPI_start_counters failed\n");
82 fprintf(stderr, "PAPI error %d: %s\n",retval,PAPI_strerror(retval));
83 exit(1);
84 }
85 #endif
86 }
87
88 void blockpapi_stop() {
89 #ifdef BLOCKPAPI
90 long_long_t g_values_tmp[MAX_COUNTERS];
91 if (g_numCounters == 0) { return; }
92 g_totalMicroSecs += PAPI_get_real_usec();
93 /* Add the the latest counts to the running total */
94 if ( (PAPI_accum_counters(g_values, g_numCounters)) != PAPI_OK) {
95 fprintf(stderr, "blockpapi_stop: PAPI_accum_counters failed");
96 exit(1);
97 }
98 /* Stop the counters, toss the values since we accumulated them above */
99 if ((PAPI_stop_counters(g_values_tmp, g_numCounters)) != PAPI_OK) {
100 fprintf(stderr, "blockpapi_stop: PAPI_stop_counters failed");
101 exit(1);
102 }
103 #endif
104 }
105
106 void blockpapi_writeReport() {
107 #ifdef BLOCKPAPI
108 int i;
109 if (g_numCounters == 0) { return; }
110 printf("Blockpapi report: this block was executed %5d time(s) in %10.2lf seconds\n", g_numPasses, (double) g_totalMicroSecs/1000000.0);
111 for (i=0; i<g_numCounters; i++) {
112 /* blockpapi_writeReportLine(g_values[i], g_events[i], g_descriptions[i]); */
113 char name[200];
114 PAPI_event_code_to_name(g_events[i], name);
115 printf("%15ld %-25s %s\n", g_values[i], name, g_descriptions[i]);
116 /* If num FP ops available we'll save it to compute FLOPS */
117 if (!strcmp(name, "PAPI_FP_OPS") || !strcmp(name, "FP_OPS_RETIRED")) {
118 g_numFpOps = g_values[i];
119 }
120 }
121 if (g_numFpOps > 0 && g_totalMicroSecs > 0.0) {
122 printf("%15.2f %-25s %s\n", g_numFpOps/g_totalMicroSecs, "MFLOPS", "millions of floating point operations per second");
123 }
124 #endif
125 }
126
127 long_long_t *blockpapi_getValues() {
128 return(g_values);
129 }
130
131 void blockpapi_writeSystemInfo() {
132 #ifdef BLOCKPAPI
133 int num_hwcntrs = 0;
134 const PAPI_hw_info_t *hwinfo = NULL;
135 if ((hwinfo = PAPI_get_hardware_info ()) != NULL) {
136 printf ("This system has %d %s CPUs running at %.0f MHz.\n", hwinfo->totalcpus,
137 hwinfo->model_string, hwinfo->mhz);
138 printf ("They are revision %f ", hwinfo->revision);
139 }
140 if ((num_hwcntrs = PAPI_num_counters ()) < PAPI_OK) {
141 printf ("and have no performance counters.\n");
142 exit (1);
143 }
144 else {
145 printf ("and have %d performance counters.\n", num_hwcntrs);
146 }
147 #endif
148 }
149

  ViewVC Help
Powered by ViewVC 1.1.26