PAPI  5.4.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cpi.c
Go to the documentation of this file.
1 /* From Dave McNamara at PSRV. Thanks! */
2 
3 /* If an event is countable but you've exhausted the counter resources
4 and you try to add an event, it seems subsequent PAPI_start and/or
5 PAPI_stop will causes a Seg. Violation.
6 
7  I got around this by calling PAPI to get the # of countable events,
8 then making sure that I didn't try to add more than these number of
9 events. I still have a problem if someone adds Level 2 cache misses
10 and then adds FLOPS 'cause I didn't count FLOPS as actually requiring
11 2 counters. */
12 
13 #include "papi_test.h"
14 #include <mpi.h>
15 #include <stdio.h>
16 #include <math.h>
17 
18 extern int TESTS_QUIET; /* Declared in test_utils.c */
19 char *netevents[] =
20  { "LO_RX_PACKETS", "LO_TX_PACKETS", "ETH0_RX_PACKETS", "ETH0_TX_PACKETS" };
21 
22 double
23 f( double a )
24 {
25  return ( 4.0 / ( 1.0 + a * a ) );
26 }
27 
28 int
29 main( int argc, char **argv )
30 {
32  int evtcode;
33  int retval, i, ins = 0;
34  long long g1[2], g2[2];
35 
36  int done = 0, n, myid, numprocs;
37  double PI25DT = 3.141592653589793238462643;
38  double mypi, pi, h, sum, x;
39  double startwtime = 0.0, endwtime;
40  int namelen;
41  char processor_name[MPI_MAX_PROCESSOR_NAME];
42 
43  tests_quiet( argc, argv ); /* Set TESTS_QUIET variable */
44 
45  if ( ( retval =
47  test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
48 
49  if ( ( retval = PAPI_create_eventset( &EventSet ) ) != PAPI_OK )
50  test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
51 
52  if ( ( retval = PAPI_create_eventset( &EventSet1 ) ) != PAPI_OK )
53  test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
54 
55  PAPI_event_name_to_code( netevents[2], &evtcode );
56  if ( ( retval = PAPI_query_event( evtcode ) ) != PAPI_OK ) {
57  if ( retval != PAPI_ECNFLCT )
58  test_fail( __FILE__, __LINE__, "PAPI_aquery_event", retval );
59  }
60  if ( ( retval = PAPI_add_event( EventSet, evtcode ) ) != PAPI_OK ) {
61  if ( retval != PAPI_ECNFLCT )
62  test_fail( __FILE__, __LINE__, "PAPI_add_event", retval );
63  }
64 
65  PAPI_event_name_to_code( netevents[3], &evtcode );
66  if ( ( retval = PAPI_query_event( evtcode ) ) != PAPI_OK ) {
67  if ( retval != PAPI_ECNFLCT )
68  test_fail( __FILE__, __LINE__, "PAPI_aquery_event", retval );
69  }
70  if ( ( retval = PAPI_add_event( EventSet, evtcode ) ) != PAPI_OK ) {
71  if ( retval != PAPI_ECNFLCT )
72  test_fail( __FILE__, __LINE__, "PAPI_add_event", retval );
73  }
74 
75  if ( ( retval = PAPI_query_event( PAPI_FP_INS ) ) != PAPI_OK ) {
76  if ( ( retval = PAPI_query_event( PAPI_FP_OPS ) ) == PAPI_OK ) {
77  ins = 2;
78  if ( ( retval =
79  PAPI_add_event( EventSet1, PAPI_FP_OPS ) ) != PAPI_OK ) {
80  if ( retval != PAPI_ECNFLCT )
81  test_fail( __FILE__, __LINE__, "PAPI_add_event", retval );
82  }
83  }
84  } else {
85  ins = 1;
86  if ( ( retval = PAPI_add_event( EventSet1, PAPI_FP_INS ) ) != PAPI_OK ) {
87  if ( retval != PAPI_ECNFLCT )
88  test_fail( __FILE__, __LINE__, "PAPI_add_event", retval );
89  }
90  }
91 
92  if ( ( retval = PAPI_add_event( EventSet1, PAPI_TOT_CYC ) ) != PAPI_OK ) {
93  if ( retval != PAPI_ECNFLCT )
94  test_fail( __FILE__, __LINE__, "PAPI_add_event", retval );
95  }
96 
97  MPI_Init( &argc, &argv );
98 
99  MPI_Comm_size( MPI_COMM_WORLD, &numprocs );
100  MPI_Comm_rank( MPI_COMM_WORLD, &myid );
101  MPI_Get_processor_name( processor_name, &namelen );
102 
103  fprintf( stdout, "Process %d of %d on %s\n",
104  myid, numprocs, processor_name );
105  fflush( stdout );
106 
107  if ( ( retval = PAPI_start( EventSet ) ) != PAPI_OK )
108  test_fail( __FILE__, __LINE__, "PAPI_start", retval );
109 
110  if ( ( retval = PAPI_start( EventSet1 ) ) != PAPI_OK )
111  test_fail( __FILE__, __LINE__, "PAPI_start", retval );
112 
113  n = 0;
114  while ( !done ) {
115  if ( myid == 0 ) {
116  if ( n == 0 )
117  n = 1000000;
118  else
119  n = 0;
120 
121  startwtime = MPI_Wtime( );
122  }
123  MPI_Bcast( &n, 1, MPI_INT, 0, MPI_COMM_WORLD );
124  if ( n == 0 )
125  done = 1;
126  else {
127  h = 1.0 / ( double ) n;
128  sum = 0.0;
129  /* A slightly better approach starts from large i and works back */
130  for ( i = myid + 1; i <= n; i += numprocs ) {
131  x = h * ( ( double ) i - 0.5 );
132  sum += f( x );
133  }
134  mypi = h * sum;
135 
136  MPI_Reduce( &mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD );
137 
138  if ( myid == 0 ) {
139  printf( "pi is approximately %.16f, Error is %.16f\n",
140  pi, fabs( pi - PI25DT ) );
141  endwtime = MPI_Wtime( );
142  printf( "wall clock time = %f\n", endwtime - startwtime );
143  fflush( stdout );
144  }
145  }
146  }
147 
148  if ( ( retval = PAPI_stop( EventSet1, g1 ) ) != PAPI_OK )
149  test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
150 
151  if ( ( retval = PAPI_stop( EventSet, g2 ) ) != PAPI_OK )
152  test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
153 
154  MPI_Finalize( );
155 
156 
157  printf( "ETH0_RX_BYTES: %lld ETH0_TX_BYTES: %lld\n", g2[0], g2[1] );
158  if ( ins == 0 ) {
159  printf( "PAPI_TOT_CYC : %lld\n", g1[0] );
160  } else if ( ins == 1 ) {
161  printf( "PAPI_FP_INS : %lld PAPI_TOT_CYC : %lld\n", g1[0], g1[1] );
162  } else if ( ins == 2 ) {
163  printf( "PAPI_FP_OPS : %lld PAPI_TOT_CYC : %lld\n", g1[0], g1[1] );
164  }
165  test_pass( __FILE__, NULL, 0 );
166  return 0;
167 }
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2258
double f(double a)
Definition: cpi.c:23
int PAPI_add_event(int EventSet, int EventCode)
Definition: papi.c:1622
#define PAPI_FP_OPS
long long myid
Definition: iozone.c:1335
#define PAPI_NULL
Definition: papi.h:290
int EventSet
Definition: data_range.c:25
return PAPI_OK
Definition: linux-nvml.c:458
tests_quiet(argc, argv)
fflush(stdout)
#define printf
Definition: papi_test.h:125
static double
Definition: fileop.c:1281
test_pass(__FILE__, NULL, 0)
int int argc
Definition: iozone.c:1609
static double a[MATRIX_SIZE][MATRIX_SIZE]
Definition: rapl_basic.c:37
int TESTS_QUIET
Definition: test_utils.c:11
char ** argv
Definition: iozone.c:1610
test_fail(__FILE__, __LINE__,"PAPI_library_init", retval)
int PAPI_library_init(int version)
Definition: papi.c:497
int i
Definition: fileop.c:140
char * netevents[]
Definition: cpi.c:19
#define PAPI_TOT_CYC
#define PAPI_ECNFLCT
Definition: papi.h:259
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1424
int PAPI_event_name_to_code(char *in, int *out)
Definition: papi.c:1010
int PAPI_query_event(int EventCode)
Definition: papi.c:698
int x
Definition: fileop.c:78
int PAPI_start(int EventSet)
Definition: papi.c:2053
ssize_t retval
Definition: libasync.c:338
#define PAPI_VER_CURRENT
Definition: papi.h:223
int main(int argc, char **argv)
List all appio events codes and names.
int EventSet1
Definition: zero_fork.c:40
#define PAPI_FP_INS
int n
Definition: mendes-alt.c:164
FILE * pi
Definition: iozone.c:1367