PAPI  5.7.0.0
libmsr_basic.c
Go to the documentation of this file.
1 /****************************/
2 /* THIS IS OPEN SOURCE CODE */
3 /****************************/
4 
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 
19 #include "papi.h"
20 #include "papi_test.h"
21 
22 #define MAX_LIBMSR_EVENTS 64
23 
24 typedef union { long long ll; double dbl; } ll_dbl_union_t;
25 
26 #ifdef SLEEP_TEST
27 void run_test( int quiet )
28 {
29  if ( !quiet )
30  printf( "Sleeping 2 second...\n" );
31  sleep( 2 );
32 }
33 
34 #else /* e.g. nothing defined or BUSY_TEST / WRAP_TEST */
35 
36 #define MATRIX_SIZE 1024
37 
38 static double a[MATRIX_SIZE][MATRIX_SIZE];
39 static double b[MATRIX_SIZE][MATRIX_SIZE];
40 static double c[MATRIX_SIZE][MATRIX_SIZE];
41 
42 /* Naive matrix multiply */
43 void run_test( int quiet )
44 {
45  double s;
46  int i,j,k;
47  if ( !quiet )
48  printf( "Doing a naive %dx%d MMM...\n",MATRIX_SIZE,MATRIX_SIZE );
49  for( i=0; i<MATRIX_SIZE; i++ ) {
50  for( j=0; j<MATRIX_SIZE; j++ ) {
51  a[i][j]=( double )i*( double )j;
52  b[i][j]=( double )i/( double )( j+5 );
53  }
54  }
55  for( j=0; j<MATRIX_SIZE; j++ ) {
56  for( i=0; i<MATRIX_SIZE; i++ ) {
57  s=0;
58  for( k=0; k<MATRIX_SIZE; k++ ) {
59  s+=a[i][k]*b[k][j];
60  }
61  c[i][j] = s;
62  }
63  }
64  s=0.0;
65  for( i=0; i<MATRIX_SIZE; i++ ) {
66  for( j=0; j<MATRIX_SIZE; j++ ) {
67  s+=c[i][j];
68  }
69  }
70  if ( !quiet ) printf( "Matrix multiply sum: s=%lf\n",s );
71 }
72 
73 #endif
74 
75 
76 int main ( int argc, char **argv )
77 {
78  int retval,cid,libmsr_cid=-1,numcmp;
79  int EventSet = PAPI_NULL;
80  long long *values;
81  int num_events=0;
82  int code;
83  char event_names[MAX_LIBMSR_EVENTS][PAPI_MAX_STR_LEN];
86  int r,i, do_wrap = 0;
87  const PAPI_component_info_t *cmpinfo = NULL;
88  PAPI_event_info_t evinfo;
89  long long before_time,after_time;
90  double elapsed_time;
91  ll_dbl_union_t tmp_ll_dbl;
92  int repeat;
93 
94  /* Set TESTS_QUIET variable */
95  tests_quiet( argc, argv );
96  if ( argc > 1 && strstr( argv[1], "-w" ) )
97  do_wrap = 1;
98 
99  /* PAPI Initialization */
101  if ( retval != PAPI_VER_CURRENT )
102  test_fail( __FILE__, __LINE__,"PAPI_library_init failed\n",retval );
103 
104  if ( !TESTS_QUIET )
105  printf( "Trying all LIBMSR events\n" );
106 
107  numcmp = PAPI_num_components();
108 
109  for( cid=0; cid<numcmp; cid++ ) {
110 
111  if ( ( cmpinfo = PAPI_get_component_info( cid ) ) == NULL )
112  test_fail( __FILE__, __LINE__,"PAPI_get_component_info failed\n", 0 );
113 
114  if ( strstr( cmpinfo->name,"libmsr" ) ) {
115  libmsr_cid=cid;
116  if ( !TESTS_QUIET ) printf( "Found libmsr component at cid %d\n",libmsr_cid );
117  if ( cmpinfo->disabled ) {
118  if ( !TESTS_QUIET ) printf( "libmsr component disabled: %s\n",cmpinfo->disabled_reason );
119  test_skip( __FILE__,__LINE__, "libmsr component disabled", 0 );
120  }
121  break;
122  }
123  }
124 
125  /* Component not found */
126  if ( cid==numcmp )
127  test_skip( __FILE__,__LINE__,"No libmsr component found\n",0 );
128 
129  /* Create EventSet */
131  if ( retval != PAPI_OK )
132  test_fail( __FILE__, __LINE__, "PAPI_create_eventset()",retval );
133 
134  /* Add all events */
135  num_events = 0;
136  code = PAPI_NATIVE_MASK;
137  r = PAPI_enum_cmp_event( &code, PAPI_ENUM_FIRST, libmsr_cid );
138  while ( r == PAPI_OK ) {
139  retval = PAPI_event_code_to_name( code, event_names[num_events] );
140  if ( retval != PAPI_OK ) test_fail( __FILE__, __LINE__, "PAPI_event_code_to_name", retval );
141  retval = PAPI_get_event_info( code,&evinfo );
142  if ( retval != PAPI_OK ) test_fail( __FILE__, __LINE__,"Error getting event info\n",retval );
143  strncpy( units[num_events], evinfo.units, sizeof( units[0] )-1 );
144  // buffer must be null terminated to safely use strstr operation on it below
145  units[num_events][sizeof( units[0] )-1] = '\0';
146  data_type[num_events] = evinfo.data_type;
147 
148  retval = PAPI_add_event( EventSet, code );
149  if ( retval != PAPI_OK )
150  break; /* We've hit an event limit */
151  num_events++;
152 
153  r = PAPI_enum_cmp_event( &code, PAPI_ENUM_EVENTS, libmsr_cid );
154  }
155 
156  values=calloc( num_events,sizeof( long long ) );
157  if ( values==NULL )
158  test_fail( __FILE__, __LINE__,"No memory - calloc failed",retval );
159 
160  if ( !TESTS_QUIET ) printf( "Starting measurements...\n" );
161 
162  /* Start Counting */
163  before_time=PAPI_get_real_nsec();
165  if ( retval != PAPI_OK ) test_fail( __FILE__, __LINE__, "PAPI_start()",retval );
166 
167  for ( repeat=0; repeat<3; repeat++ ) {
168 
169  /* Run test */
171 
172  /* Stop Counting */
173  after_time=PAPI_get_real_nsec();
175  if ( retval != PAPI_OK ) test_fail( __FILE__, __LINE__, "PAPI_read()",retval );
176 
177  elapsed_time=( ( double )( after_time-before_time ) )/1.0e9;
178 
179  if ( !TESTS_QUIET ) {
180  printf( "Stopping measurements, took %.3fs, gathering results...\n", elapsed_time );
181  for( i=0; i<num_events; i++ ) {
182  tmp_ll_dbl.ll = values[i];
183  if ( data_type[i]==PAPI_DATATYPE_FP64 ) {
184  printf( "%-40s %12.6f %s\n", event_names[i], tmp_ll_dbl.dbl, units[i] );
185  } else if ( data_type[i]==PAPI_DATATYPE_UINT64 ) {
186  printf( "%-40s %12ld %s\n", event_names[i], tmp_ll_dbl.ll, units[i] );
187  }
188  }
189  printf( "\n" );
190  }
191  }
192 
194  if ( retval != PAPI_OK ) test_fail( __FILE__, __LINE__, "PAPI_stop()",retval );
195 
197  if ( retval != PAPI_OK ) test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset()",retval );
198 
200  if ( retval != PAPI_OK ) test_fail( __FILE__, __LINE__,"PAPI_destroy_eventset()",retval );
201 
202  test_pass( __FILE__ );
203 
204  return 0;
205 }
206 
#define PAPI_OK
Definition: fpapi.h:105
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2314
#define PAPI_NATIVE_MASK
char disabled_reason[PAPI_MAX_STR_LEN]
Definition: papi.h:637
const PAPI_component_info_t * PAPI_get_component_info(int cidx)
Definition: papi.c:796
void test_pass(const char *filename)
Definition: test_utils.c:432
int PAPI_add_event(int EventSet, int EventCode)
Definition: papi.c:1663
char units[PAPI_MIN_STR_LEN]
Definition: papi.h:976
int main(int argc, char **argv)
Definition: libmsr_basic.c:76
static int num_events
int PAPI_num_components(void)
Definition: papi.c:4387
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
int EventSet
int retval
Definition: zero_fork.c:53
char units[MAX_EVENTS][BUFSIZ]
Definition: powercap_plot.c:15
Return codes and api definitions.
int PAPI_get_event_info(int EventCode, PAPI_event_info_t *info)
Definition: papi.c:835
void test_skip(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:561
long long ll
Definition: libmsr_basic.c:24
static double c[MATRIX_SIZE][MATRIX_SIZE]
Definition: libmsr_basic.c:40
#define MATRIX_SIZE
Definition: libmsr_basic.c:36
int PAPI_library_init(int version)
Definition: papi.c:500
void run_test(int quiet)
Definition: libmsr_basic.c:43
int quiet
Definition: rapl_overflow.c:18
#define MAX_LIBMSR_EVENTS
Tests basic functionality of libmsr component.
Definition: libmsr_basic.c:22
#define PAPI_MIN_STR_LEN
Definition: fpapi.h:41
#define PAPI_NULL
Definition: fpapi.h:13
double s
Definition: byte_profile.c:36
char name[PAPI_MAX_STR_LEN]
Definition: papi.h:630
int PAPI_enum_cmp_event(int *EventCode, int modifier, int cidx)
Definition: papi.c:1357
int PAPI_cleanup_eventset(int EventSet)
Definition: papi.c:2890
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1464
int PAPI_event_code_to_name(int EventCode, char *out)
Definition: papi.c:915
int TESTS_QUIET
Definition: test_utils.c:18
int tests_quiet(int argc, char **argv)
Definition: test_utils.c:376
void test_fail(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:468
long long PAPI_get_real_nsec(void)
Definition: papi.c:6237
int data_type[MAX_EVENTS]
Definition: powercap_plot.c:16
int PAPI_destroy_eventset(int *EventSet)
Definition: papi.c:2014
int PAPI_read(int EventSet, long long *values)
Definition: papi.c:2559
int PAPI_start(int EventSet)
Definition: papi.c:2096
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
static double b[MATRIX_SIZE][MATRIX_SIZE]
Definition: libmsr_basic.c:39
static double a[MATRIX_SIZE][MATRIX_SIZE]
Definition: libmsr_basic.c:38
int i
Definition: fileop.c:140
#define PAPI_MAX_STR_LEN
Definition: fpapi.h:43