PAPI  5.7.0.0
papi_sp_ops.c
Go to the documentation of this file.
1 /* This file attempts to test the single-precision floating point */
2 /* performance counter PAPI_SP_OPS */
3 
4 /* by Vince Weaver, <vincent.weaver@maine.edu> */
5 
6 /* Note! There are many many many things that can go wrong */
7 /* when trying to get a sane floating point measurement. */
8 
9 
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <string.h>
14 
15 #include "papi.h"
16 #include "papi_test.h"
17 
18 #include "display_error.h"
19 #include "testcode.h"
20 
21 
22 int main(int argc, char **argv) {
23 
24  int num_runs=100,i;
25  long long high=0,low=0,average=0,expected=1500000;
26  double error,double_result;
27 
28  long long count,total=0;
29  int quiet=0,retval,ins_result;
30  int eventset=PAPI_NULL;
31 
32  quiet=tests_quiet(argc,argv);
33 
34  if (!quiet) {
35  printf("\nTesting the PAPI_SP_OPS event.\n\n");
36  }
37 
38  /* Init the PAPI library */
40  if ( retval != PAPI_VER_CURRENT ) {
41  test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
42  }
43 
44  /* Create the eventset */
45  retval=PAPI_create_eventset(&eventset);
46  if (retval!=PAPI_OK) {
47  test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
48  }
49 
50  /* Add FP_OPS event */
51  retval=PAPI_add_named_event(eventset,"PAPI_SP_OPS");
52  if (retval!=PAPI_OK) {
53  if (!quiet) fprintf(stderr,"PAPI_SP_OPS not available!\n");
54  test_skip( __FILE__, __LINE__, "adding PAPI_SP_OPS", retval );
55  }
56 
57  /**************************************/
58  /* Test a loop with no floating point */
59  /**************************************/
60  total=0;
61  expected=0;
62 
63  if (!quiet) {
64  printf("Testing a loop with %lld floating point (%d times):\n",
65  expected,num_runs);
66  }
67 
68  for(i=0;i<num_runs;i++) {
69  PAPI_reset(eventset);
70  PAPI_start(eventset);
71 
72  ins_result=branches_testcode();
73 
74  retval=PAPI_stop(eventset,&count);
75 
76  if (ins_result==CODE_UNIMPLEMENTED) {
77  fprintf(stderr,"\tCode unimplemented\n");
78  test_skip( __FILE__, __LINE__, "unimplemented", 0);
79  }
80 
81  if (retval!=PAPI_OK) {
82  test_fail( __FILE__, __LINE__,
83  "reading PAPI_TOT_INS", retval );
84  }
85 
86  if (count>high) high=count;
87  if ((low==0) || (count<low)) low=count;
88  total+=count;
89  }
90 
91  average=(total/num_runs);
92 
93  error=display_error(average,high,low,expected,quiet);
94 
95  if (average>10) {
96  if (!quiet) printf("Unexpected FP event value\n");
97  test_fail( __FILE__, __LINE__, "Unexpected FP event", 1 );
98  }
99 
100  if (!quiet) printf("\n");
101 
102  /*******************************************/
103  /* Test a single-precision matrix multiply */
104  /*******************************************/
105  total=0; high=0; low=0;
107 
108  num_runs=3;
109 
110  if (!quiet) {
111  printf("Testing a matrix multiply with %lld single-precision FP operations (%d times)\n",
112  expected,num_runs);
113  }
114 
115  for(i=0;i<num_runs;i++) {
116 
117  PAPI_reset(eventset);
118  PAPI_start(eventset);
119 
120  double_result=flops_float_matrix_matrix_multiply();
121 
122  retval=PAPI_stop(eventset,&count);
123 
124  if (retval!=PAPI_OK) {
125  test_fail( __FILE__, __LINE__,
126  "reading PAPI_TOT_INS", retval );
127  }
128 
129  if (count>high) high=count;
130  if ((low==0) || (count<low)) low=count;
131  total+=count;
132  }
133 
134  if (!quiet) printf("Result %lf\n",double_result);
135 
136  average=(total/num_runs);
137 
138  error=display_error(average,high,low,expected,quiet);
139 
140  if ((error > 1.0) || (error<-1.0)) {
141  if (!quiet) printf("Instruction count off by more than 1%%\n");
142  test_fail( __FILE__, __LINE__, "Error too high", 1 );
143  }
144 
145  if (!quiet) printf("\n");
146 
147 
148  /*******************************************/
149  /* Test a double-precision matrix multiply */
150  /*******************************************/
151  total=0; high=0; low=0;
153 
154  expected=expected*0;
155 
156  num_runs=3;
157 
158  if (!quiet) {
159  printf("Testing a matrix multiply with %lld double-precision FP operations (%d times)\n",
160  expected,num_runs);
161  }
162 
163  for(i=0;i<num_runs;i++) {
164 
165  PAPI_reset(eventset);
166  PAPI_start(eventset);
167 
168  double_result=flops_double_matrix_matrix_multiply();
169 
170  retval=PAPI_stop(eventset,&count);
171 
172  if (retval!=PAPI_OK) {
173  test_fail( __FILE__, __LINE__,
174  "reading PAPI_TOT_INS", retval );
175  }
176 
177  if (count>high) high=count;
178  if ((low==0) || (count<low)) low=count;
179  total+=count;
180  }
181 
182  if (!quiet) printf("Result %lf\n",double_result);
183 
184  average=(total/num_runs);
185 
186  error=display_error(average,high,low,expected,quiet);
187 
188  if ((error > 1.0) || (error<-1.0)) {
189  if (!quiet) printf("Instruction count off by more than 1%%\n");
190  test_fail( __FILE__, __LINE__, "Error too high", 1 );
191  }
192 
193  if (!quiet) printf("\n");
194 
195  test_pass( __FILE__ );
196 
197  PAPI_shutdown();
198 
199  return 0;
200 }
#define PAPI_OK
Definition: fpapi.h:105
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2314
double flops_double_matrix_matrix_multiply(void)
void test_pass(const char *filename)
Definition: test_utils.c:432
static int expected[NUM_THREADS]
int PAPI_reset(int EventSet)
Definition: papi.c:2459
double display_error(long long average, long long high, long long low, long long expected, int quiet)
Definition: display_error.c:7
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
#define CODE_UNIMPLEMENTED
Definition: testcode.h:2
int retval
Definition: zero_fork.c:53
int main(int argc, char **argv)
Definition: papi_sp_ops.c:22
Return codes and api definitions.
void test_skip(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:561
int PAPI_add_named_event(int EventSet, const char *EventName)
Definition: papi.c:1876
int PAPI_library_init(int version)
Definition: papi.c:500
void PAPI_shutdown(void)
Definition: papi.c:4461
int quiet
Definition: rapl_overflow.c:18
int flops_float_init_matrix(void)
#define PAPI_NULL
Definition: fpapi.h:13
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1464
int branches_testcode(void)
static int total
Definition: rapl_overflow.c:9
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
int PAPI_start(int EventSet)
Definition: papi.c:2096
float flops_float_matrix_matrix_multiply(void)
int flops_double_init_matrix(void)
static long count
int i
Definition: fileop.c:140