PAPI 7.1.0.0
Loading...
Searching...
No Matches
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
4and you try to add an event, it seems subsequent PAPI_start and/or
5PAPI_stop will causes a Seg. Violation.
6
7 I got around this by calling PAPI to get the # of countable events,
8then making sure that I didn't try to add more than these number of
9events. I still have a problem if someone adds Level 2 cache misses
10and then adds FLOPS 'cause I didn't count FLOPS as actually requiring
112 counters. */
12
13#include "papi_test.h"
14#include <mpi.h>
15#include <stdio.h>
16#include <math.h>
17
18extern int TESTS_QUIET; /* Declared in test_utils.c */
19char *netevents[] =
20 { "LO_RX_PACKETS", "LO_TX_PACKETS", "ETH0_RX_PACKETS", "ETH0_TX_PACKETS" };
21
22double
23f( double a )
24{
25 return ( 4.0 / ( 1.0 + a * a ) );
26}
27
28int
29main( 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
50 test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
51
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 =
80 if ( retval != PAPI_ECNFLCT )
81 test_fail( __FILE__, __LINE__, "PAPI_add_event", retval );
82 }
83 }
84 } else {
85 ins = 1;
87 if ( retval != PAPI_ECNFLCT )
88 test_fail( __FILE__, __LINE__, "PAPI_add_event", retval );
89 }
90 }
91
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 i
add PAPI preset or native hardware event to an event set
Create a new empty PAPI EventSet.
Convert a name to a numeric hardware event code.
initialize the PAPI library.
Query if PAPI event exists.
Start counting hardware events in an event set.
Stop counting hardware events in an event set.
int TESTS_QUIET
Definition: test_utils.c:18
char * netevents[]
Definition: cpi.c:19
double f(double a)
Definition: cpi.c:23
static pthread_t myid[NUM_THREADS]
#define PAPI_VER_CURRENT
Definition: f90papi.h:54
#define PAPI_OK
Definition: f90papi.h:73
#define PAPI_NULL
Definition: f90papi.h:78
#define PAPI_TOT_CYC
Definition: f90papi.h:308
#define PAPI_ECNFLCT
Definition: f90papi.h:234
#define PAPI_FP_INS
Definition: f90papi.h:366
#define PAPI_FP_OPS
Definition: f90papi.h:319
static int EventSet
Definition: init_fini.c:8
static double a[MATRIX_SIZE][MATRIX_SIZE]
Definition: libmsr_basic.c:38
FILE * stdout
int tests_quiet(int argc, char **argv)
Definition: test_utils.c:376
void PAPI_NORETURN test_fail(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:491
void PAPI_NORETURN test_pass(const char *filename)
Definition: test_utils.c:432
int main()
Definition: pernode.c:20
volatile double x
int EventSet1
Definition: zero_fork.c:47
int retval
Definition: zero_fork.c:53