|
PAPI
5.0.1.0
|
00001 /* From Dave McNamara at PSRV. Thanks! */ 00002 00003 /* If an event is countable but you've exhausted the counter resources 00004 and you try to add an event, it seems subsequent PAPI_start and/or 00005 PAPI_stop will causes a Seg. Violation. 00006 00007 I got around this by calling PAPI to get the # of countable events, 00008 then making sure that I didn't try to add more than these number of 00009 events. I still have a problem if someone adds Level 2 cache misses 00010 and then adds FLOPS 'cause I didn't count FLOPS as actually requiring 00011 2 counters. */ 00012 00013 #include "papi_test.h" 00014 #include <mpi.h> 00015 #include <stdio.h> 00016 #include <math.h> 00017 00018 extern int TESTS_QUIET; /* Declared in test_utils.c */ 00019 char *netevents[] = 00020 { "LO_RX_PACKETS", "LO_TX_PACKETS", "ETH0_RX_PACKETS", "ETH0_TX_PACKETS" }; 00021 00022 double 00023 f( double a ) 00024 { 00025 return ( 4.0 / ( 1.0 + a * a ) ); 00026 } 00027 00028 int 00029 main( int argc, char **argv ) 00030 { 00031 int EventSet = PAPI_NULL, EventSet1 = PAPI_NULL; 00032 int evtcode; 00033 int retval, i, ins = 0; 00034 long long g1[2], g2[2]; 00035 00036 int done = 0, n, myid, numprocs; 00037 double PI25DT = 3.141592653589793238462643; 00038 double mypi, pi, h, sum, x; 00039 double startwtime = 0.0, endwtime; 00040 int namelen; 00041 char processor_name[MPI_MAX_PROCESSOR_NAME]; 00042 00043 tests_quiet( argc, argv ); /* Set TESTS_QUIET variable */ 00044 00045 if ( ( retval = 00046 PAPI_library_init( PAPI_VER_CURRENT ) ) != PAPI_VER_CURRENT ) 00047 test_fail( __FILE__, __LINE__, "PAPI_library_init", retval ); 00048 00049 if ( ( retval = PAPI_create_eventset( &EventSet ) ) != PAPI_OK ) 00050 test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval ); 00051 00052 if ( ( retval = PAPI_create_eventset( &EventSet1 ) ) != PAPI_OK ) 00053 test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval ); 00054 00055 PAPI_event_name_to_code( netevents[2], &evtcode ); 00056 if ( ( retval = PAPI_query_event( evtcode ) ) != PAPI_OK ) { 00057 if ( retval != PAPI_ECNFLCT ) 00058 test_fail( __FILE__, __LINE__, "PAPI_aquery_event", retval ); 00059 } 00060 if ( ( retval = PAPI_add_event( EventSet, evtcode ) ) != PAPI_OK ) { 00061 if ( retval != PAPI_ECNFLCT ) 00062 test_fail( __FILE__, __LINE__, "PAPI_add_event", retval ); 00063 } 00064 00065 PAPI_event_name_to_code( netevents[3], &evtcode ); 00066 if ( ( retval = PAPI_query_event( evtcode ) ) != PAPI_OK ) { 00067 if ( retval != PAPI_ECNFLCT ) 00068 test_fail( __FILE__, __LINE__, "PAPI_aquery_event", retval ); 00069 } 00070 if ( ( retval = PAPI_add_event( EventSet, evtcode ) ) != PAPI_OK ) { 00071 if ( retval != PAPI_ECNFLCT ) 00072 test_fail( __FILE__, __LINE__, "PAPI_add_event", retval ); 00073 } 00074 00075 if ( ( retval = PAPI_query_event( PAPI_FP_INS ) ) != PAPI_OK ) { 00076 if ( ( retval = PAPI_query_event( PAPI_FP_OPS ) ) == PAPI_OK ) { 00077 ins = 2; 00078 if ( ( retval = 00079 PAPI_add_event( EventSet1, PAPI_FP_OPS ) ) != PAPI_OK ) { 00080 if ( retval != PAPI_ECNFLCT ) 00081 test_fail( __FILE__, __LINE__, "PAPI_add_event", retval ); 00082 } 00083 } 00084 } else { 00085 ins = 1; 00086 if ( ( retval = PAPI_add_event( EventSet1, PAPI_FP_INS ) ) != PAPI_OK ) { 00087 if ( retval != PAPI_ECNFLCT ) 00088 test_fail( __FILE__, __LINE__, "PAPI_add_event", retval ); 00089 } 00090 } 00091 00092 if ( ( retval = PAPI_add_event( EventSet1, PAPI_TOT_CYC ) ) != PAPI_OK ) { 00093 if ( retval != PAPI_ECNFLCT ) 00094 test_fail( __FILE__, __LINE__, "PAPI_add_event", retval ); 00095 } 00096 00097 MPI_Init( &argc, &argv ); 00098 00099 MPI_Comm_size( MPI_COMM_WORLD, &numprocs ); 00100 MPI_Comm_rank( MPI_COMM_WORLD, &myid ); 00101 MPI_Get_processor_name( processor_name, &namelen ); 00102 00103 fprintf( stdout, "Process %d of %d on %s\n", 00104 myid, numprocs, processor_name ); 00105 fflush( stdout ); 00106 00107 if ( ( retval = PAPI_start( EventSet ) ) != PAPI_OK ) 00108 test_fail( __FILE__, __LINE__, "PAPI_start", retval ); 00109 00110 if ( ( retval = PAPI_start( EventSet1 ) ) != PAPI_OK ) 00111 test_fail( __FILE__, __LINE__, "PAPI_start", retval ); 00112 00113 n = 0; 00114 while ( !done ) { 00115 if ( myid == 0 ) { 00116 if ( n == 0 ) 00117 n = 1000000; 00118 else 00119 n = 0; 00120 00121 startwtime = MPI_Wtime( ); 00122 } 00123 MPI_Bcast( &n, 1, MPI_INT, 0, MPI_COMM_WORLD ); 00124 if ( n == 0 ) 00125 done = 1; 00126 else { 00127 h = 1.0 / ( double ) n; 00128 sum = 0.0; 00129 /* A slightly better approach starts from large i and works back */ 00130 for ( i = myid + 1; i <= n; i += numprocs ) { 00131 x = h * ( ( double ) i - 0.5 ); 00132 sum += f( x ); 00133 } 00134 mypi = h * sum; 00135 00136 MPI_Reduce( &mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD ); 00137 00138 if ( myid == 0 ) { 00139 printf( "pi is approximately %.16f, Error is %.16f\n", 00140 pi, fabs( pi - PI25DT ) ); 00141 endwtime = MPI_Wtime( ); 00142 printf( "wall clock time = %f\n", endwtime - startwtime ); 00143 fflush( stdout ); 00144 } 00145 } 00146 } 00147 00148 if ( ( retval = PAPI_stop( EventSet1, g1 ) ) != PAPI_OK ) 00149 test_fail( __FILE__, __LINE__, "PAPI_stop", retval ); 00150 00151 if ( ( retval = PAPI_stop( EventSet, g2 ) ) != PAPI_OK ) 00152 test_fail( __FILE__, __LINE__, "PAPI_stop", retval ); 00153 00154 MPI_Finalize( ); 00155 00156 00157 printf( "ETH0_RX_BYTES: %lld ETH0_TX_BYTES: %lld\n", g2[0], g2[1] ); 00158 if ( ins == 0 ) { 00159 printf( "PAPI_TOT_CYC : %lld\n", g1[0] ); 00160 } else if ( ins == 1 ) { 00161 printf( "PAPI_FP_INS : %lld PAPI_TOT_CYC : %lld\n", g1[0], g1[1] ); 00162 } else if ( ins == 2 ) { 00163 printf( "PAPI_FP_OPS : %lld PAPI_TOT_CYC : %lld\n", g1[0], g1[1] ); 00164 } 00165 test_pass( __FILE__, NULL, 0 ); 00166 return 0; 00167 }