PAPI  5.3.2.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
matrix-hl.c
Go to the documentation of this file.
1 /****************************************************************************
2  *C
3  *C matrix-hl.f
4  *C An example of matrix-matrix multiplication and using PAPI high level
5  *C to look at the performance. written by Kevin London
6  *C March 2000
7  *C Added to c tests to check stop
8  *C****************************************************************************
9  */
10 #include "papi_test.h"
11 #include <stdlib.h>
12 
13 int
14 main( int argc, char **argv )
15 {
16 
17 #define NROWS1 175
18 #define NCOLS1 225
19 #define NROWS2 NCOLS1
20 #define NCOLS2 150
21  double p[NROWS1][NCOLS1], q[NROWS2][NCOLS2], r[NROWS1][NCOLS2];
22  int i, j, k, num_events, retval;
23  /* PAPI standardized event to be monitored */
24  int event[2];
25  /* PAPI values of the counters */
26  long long values[2], tmp;
27  extern int TESTS_QUIET;
28 
29  tests_quiet( argc, argv );
30 
31  /* Setup default values */
32  num_events = 0;
33 
34  /* See how many hardware events at one time are supported
35  * This also initializes the PAPI library */
36  num_events = PAPI_num_counters( );
37  if ( num_events < 2 ) {
38  printf( "This example program requries the architecture to "
39  "support 2 simultaneous hardware events...shutting down.\n" );
40  test_skip( __FILE__, __LINE__, "PAPI_num_counters", 1 );
41  }
42 
43  if ( !TESTS_QUIET )
44  printf( "Number of hardware counters supported: %d\n", num_events );
45 
47  event[0] = PAPI_FP_OPS;
48  else if ( PAPI_query_event( PAPI_FP_INS ) == PAPI_OK )
49  event[0] = PAPI_FP_INS;
50  else
51  event[0] = PAPI_TOT_INS;
52 
53  /* Time used */
54  event[1] = PAPI_TOT_CYC;
55 
56  /* matrix 1: read in the matrix values */
57  for ( i = 0; i < NROWS1; i++ )
58  for ( j = 0; j < NCOLS1; j++ )
59  p[i][j] = i * j * 1.0;
60 
61  for ( i = 0; i < NROWS2; i++ )
62  for ( j = 0; j < NCOLS2; j++ )
63  q[i][j] = i * j * 1.0;
64 
65  for ( i = 0; i < NROWS1; i++ )
66  for ( j = 0; j < NCOLS2; j++ )
67  r[i][j] = i * j * 1.0;
68 
69  /* Set up the counters */
70  num_events = 2;
71  retval = PAPI_start_counters( event, num_events );
72  if ( retval != PAPI_OK )
73  test_fail( __FILE__, __LINE__, "PAPI_start_counters", retval );
74 
75  /* Clear the counter values */
76  retval = PAPI_read_counters( values, num_events );
77  if ( retval != PAPI_OK )
78  test_fail( __FILE__, __LINE__, "PAPI_read_counters", retval );
79 
80  /* Compute the matrix-matrix multiplication */
81  for ( i = 0; i < NROWS1; i++ )
82  for ( j = 0; j < NCOLS2; j++ )
83  for ( k = 0; k < NCOLS1; k++ )
84  r[i][j] = r[i][j] + p[i][k] * q[k][j];
85 
86  /* Stop the counters and put the results in the array values */
87  retval = PAPI_stop_counters( values, num_events );
88  if ( retval != PAPI_OK )
89  test_fail( __FILE__, __LINE__, "PAPI_stop_counters", retval );
90 
91  /* Make sure the compiler does not optimize away the multiplication
92  * with dummy(r);
93  */
94  dummy( r );
95 
96  if ( !TESTS_QUIET ) {
97  if ( event[0] == PAPI_TOT_INS ) {
98  printf( TAB1, "TOT Instructions:", values[0] );
99  } else {
100  printf( TAB1, "FP Instructions:", values[0] );
101  }
102  printf( TAB1, "Cycles:", values[1] );
103  }
104 
105  /*
106  * Intel Core overreports flops by 50% when using -O
107  * Use -O2 or -O3 to produce the expected # of flops
108  */
109 
110  if ( event[0] == PAPI_FP_INS ) {
111  /* Compare measured FLOPS to expected value */
112  tmp =
113  2 * ( long long ) ( NROWS1 ) * ( long long ) ( NCOLS2 ) *
114  ( long long ) ( NCOLS1 );
115  if ( abs( ( int ) values[0] - ( int ) tmp ) > ( double ) tmp * 0.05 ) {
116  /* Maybe we are counting FMAs? */
117  tmp = tmp / 2;
118  if ( abs( ( int ) values[0] - ( int ) tmp ) >
119  ( double ) tmp * 0.05 ) {
120  printf( "\n" TAB1, "Expected operation count: ", 2 * tmp );
121  printf( TAB1, "Or possibly (using FMA): ", tmp );
122  printf( TAB1, "Instead I got: ", values[0] );
123  test_fail( __FILE__, __LINE__,
124  "Unexpected FLOP count (check vector operations)",
125  1 );
126  }
127  }
128  }
129  test_pass( __FILE__, 0, 0 );
130  return ( PAPI_EMISC );
131 }
#define PAPI_EMISC
Definition: fpapi.h:119
#define NROWS1
void test_skip(char *file, int line, char *call, int retval)
Definition: test_utils.c:614
#define PAPI_TOT_INS
Definition: fpapi.h:185
#define TAB1
Definition: papi_test.h:112
int num_events
device[deviceId] domain[domainId] event
Definition: linux-cuda.c:306
return PAPI_OK
Definition: linux-nvml.c:458
#define PAPI_FP_OPS
Definition: fpapi.h:237
tests_quiet(argc, argv)
#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
int dummy
Definition: iozone.c:19741
int TESTS_QUIET
Definition: test_utils.c:11
char ** argv
Definition: iozone.c:1610
test_fail(__FILE__, __LINE__,"PAPI_library_init", retval)
#define NROWS2
int i
Definition: fileop.c:140
#define PAPI_TOT_CYC
Definition: fpapi.h:194
int PAPI_stop_counters(long long *values, int array_len)
Definition: papi_hl.c:803
int k
Definition: iozone.c:19136
#define NCOLS2
long long
Definition: iozone.c:19827
#define NCOLS1
#define PAPI_FP_INS
Definition: fpapi.h:187
int PAPI_query_event(int EventCode)
Definition: papi.c:696
int PAPI_read_counters(long long *values, int array_len)
Definition: papi_hl.c:706
int
Definition: iozone.c:18528
int PAPI_num_counters(void)
Definition: papi_hl.c:537
int PAPI_start_counters(int *events, int array_len)
Definition: papi_hl.c:593
long j
Definition: iozone.c:19135
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
ssize_t retval
Definition: libasync.c:338
long long tmp
Definition: iozone.c:12031
int main(int argc, char **argv)
List all appio events codes and names.