PAPI 7.1.0.0
Loading...
Searching...
No Matches
Simple_Lib.c
Go to the documentation of this file.
1#include <stdio.h>
2#include <stdint.h>
3#include <stdlib.h>
4#include <math.h>
5#include "sde_lib.h"
6
7// API functions
8void simple_init(void);
9double simple_compute(double x);
10
11// The following counters are hidden to programs linking with
12// this library, so they can not be accessed directly.
13static double comp_value;
14static long long int total_iter_cnt, low_wtrmrk, high_wtrmrk;
16
17static const char *ev_names[4] = {
18 "COMPUTED_VALUE",
19 "TOTAL_ITERATIONS",
20 "LOW_WATERMARK_REACHED",
21 "HIGH_WATERMARK_REACHED"
22};
23
24
25void simple_init(void){
26
27 // Initialize library specific variables
28 comp_value = 0.0;
30 low_wtrmrk = 0;
31 high_wtrmrk = 0;
32
33 // Initialize PAPI SDEs
34 handle = papi_sde_init("Simple");
39
40 return;
41}
42
43// Perform some nonsense computation to emulate a possible library behavior.
44// Notice that no SDE routines need to be called in the critical path of the library.
45double simple_compute(double x){
46 double sum = 0.0;
47 int lcl_iter = 0;
48
49 if( x > 1.0 )
50 x = 1.0/x;
51 if( x < 0.000001 )
52 x += 0.3579;
53
54 while( 1 ){
55 double y,x2,x3,x4;
56 lcl_iter++;
57
58 // Compute a function with range [0:1] so we can iterate
59 // multiple times without diverging or creating FP exceptions.
60 x2 = x*x;
61 x3 = x2*x;
62 x4 = x2*x2;
63 y = 42.53*x4 -67.0*x3 +25.0*x2 +x/2.15;
64 y = y*y;
65 if( y < 0.01 )
66 y = 0.5-y;
67
68 // Now set the next x to be the current y, so we can iterate again.
69 x = y;
70
71 // Add y to sum unconditionally
72 sum += y;
73
74 if( y < 0.1 ){
75 low_wtrmrk++;
76 continue;
77 }
78
79 if( y > 0.9 ){
81 continue;
82 }
83
84 // Only add y to comp_value if y is between the low and high watermarks.
85 comp_value += y;
86
87 // If some condition is met, terminate the loop
88 if( 0.61 < y && y < 0.69 )
89 break;
90 }
91 total_iter_cnt += lcl_iter;
92
93 return sum;
94}
static const char * ev_names[4]
Definition: Simple_Lib.c:17
void simple_init(void)
Definition: Simple_Lib.c:25
double simple_compute(double x)
Definition: Simple_Lib.c:45
static long long int total_iter_cnt
Definition: Simple_Lib.c:14
static long long int high_wtrmrk
Definition: Simple_Lib.c:14
static papi_handle_t handle
Definition: Simple_Lib.c:15
static long long int low_wtrmrk
Definition: Simple_Lib.c:14
static double comp_value
Definition: Simple_Lib.c:13
papi_handle_t papi_sde_init(const char *name_of_library)
Definition: sde_lib.c:119
int papi_sde_register_counter(papi_handle_t handle, const char *event_name, int cntr_mode, int cntr_type, void *counter)
Definition: sde_lib.c:276
SDE prototypes and macros.
#define PAPI_SDE_double
Definition: sde_lib.h:30
void * papi_handle_t
Definition: sde_lib.h:100
#define PAPI_SDE_long_long
Definition: sde_lib.h:28
#define PAPI_SDE_RO
Definition: sde_lib.h:23
#define PAPI_SDE_INSTANT
Definition: sde_lib.h:26
#define PAPI_SDE_DELTA
Definition: sde_lib.h:25
volatile double y
volatile double x