PAPI 7.1.0.0
Loading...
Searching...
No Matches
Simple2_Lib.c
Go to the documentation of this file.
1#include <stdio.h>
2#include <stdint.h>
3#include <stdlib.h>
4#include <string.h>
5#include <math.h>
6#include "sde_lib.h"
7
8// API functions
9void simple_init(void);
10double simple_compute(double x);
11
12// The following counters are hidden to programs linking with
13// this library, so they can not be accessed directly.
14static double comp_value;
15static long long int total_iter_cnt, low_wtrmrk, high_wtrmrk;
17
18static const char *ev_names[4] = {
19 "COMPUTED_VALUE",
20 "TOTAL_ITERATIONS",
21 "LOW_WATERMARK_REACHED",
22 "HIGH_WATERMARK_REACHED"
23};
24
25long long int counter_accessor_function( void *param );
26
27void simple_init(void){
28
29 // Initialize library specific variables
30 comp_value = 0.0;
32 low_wtrmrk = 0;
33 high_wtrmrk = 0;
34
35 // Initialize PAPI SDEs
36 handle = papi_sde_init("Simple2");
41 papi_sde_add_counter_to_group(handle, ev_names[2], "ANY_WATERMARK_REACHED", PAPI_SDE_SUM);
42 papi_sde_add_counter_to_group(handle, ev_names[3], "ANY_WATERMARK_REACHED", PAPI_SDE_SUM);
43
44 return;
45}
46
47// The following function will _NOT_ be called by other libray functions or normal
48// applications. It is a hook for the utility 'papi_native_avail' to be able to
49// discover the SDEs that are exported by this library.
51 handle = fptr_struct->init("Simple2");
56 fptr_struct->add_counter_to_group(handle, ev_names[2], "ANY_WATERMARK_REACHED", PAPI_SDE_SUM);
57 fptr_struct->add_counter_to_group(handle, ev_names[3], "ANY_WATERMARK_REACHED", PAPI_SDE_SUM);
58
59 fptr_struct->describe_counter(handle, ev_names[0], "Sum of values that are within the watermarks.");
60 fptr_struct->describe_counter(handle, ev_names[1], "Total iterations executed by the library.");
61 fptr_struct->describe_counter(handle, ev_names[2], "Number of times a value was below the low watermark.");
62 fptr_struct->describe_counter(handle, ev_names[3], "Number of times a value was above the high watermark.");
63 fptr_struct->describe_counter(handle, "ANY_WATERMARK_REACHED", "Number of times a value was not between the two watermarks.");
64
65 return handle;
66}
67
68// This function allows the library to perform operations in order to compute the value of an SDE at run-time
69long long counter_accessor_function( void *param ){
70 long long ll;
71 double *dbl_ptr = (double *)param;
72
73 // Scale the variable by a factor of two. Real libraries will do meaningful work here.
74 double value = *dbl_ptr * 2.0;
75
76 // Copy the bits of the result in a long long int.
77 (void)memcpy(&ll, &value, sizeof(double));
78
79 return ll;
80}
81
82// Perform some nonsense computation to emulate a possible library behavior.
83// Notice that no SDE routines need to be called in the critical path of the library.
84double simple_compute(double x){
85 double sum = 0.0;
86 int lcl_iter = 0;
87
88 if( x > 1.0 )
89 x = 1.0/x;
90 if( x < 0.000001 )
91 x += 0.3579;
92
93 while( 1 ){
94 double y,x2,x3,x4;
95 lcl_iter++;
96
97 // Compute a function with range [0:1] so we can iterate
98 // multiple times without diverging or creating FP exceptions.
99 x2 = x*x;
100 x3 = x2*x;
101 x4 = x2*x2;
102 y = 42.53*x4 -67.0*x3 +25.0*x2 +x/2.15;
103 y = y*y;
104 if( y < 0.01 )
105 y = 0.5-y;
106
107 // Now set the next x to be the current y, so we can iterate again.
108 x = y;
109
110 // Add y to sum unconditionally
111 sum += y;
112
113 if( y < 0.1 ){
114 low_wtrmrk++;
115 continue;
116 }
117
118 if( y > 0.9 ){
119 high_wtrmrk++;
120 continue;
121 }
122
123 // Only add y to comp_value if y is between the low and high watermarks.
124 comp_value += y;
125
126 // If some condition is met, terminate the loop
127 if( 0.61 < y && y < 0.69 )
128 break;
129 }
130 total_iter_cnt += lcl_iter;
131
132 return sum;
133}
static const char * ev_names[4]
Definition: Simple2_Lib.c:18
void simple_init(void)
Definition: Simple2_Lib.c:27
papi_handle_t papi_sde_hook_list_events(papi_sde_fptr_struct_t *fptr_struct)
Definition: Simple2_Lib.c:50
double simple_compute(double x)
Definition: Simple2_Lib.c:84
long long int counter_accessor_function(void *param)
Definition: Simple2_Lib.c:69
static long long int total_iter_cnt
Definition: Simple2_Lib.c:15
static long long int high_wtrmrk
Definition: Simple2_Lib.c:15
static papi_handle_t handle
Definition: Simple2_Lib.c:16
static long long int low_wtrmrk
Definition: Simple2_Lib.c:15
static double comp_value
Definition: Simple2_Lib.c:14
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
int papi_sde_add_counter_to_group(papi_handle_t handle, const char *event_name, const char *group_name, uint32_t group_flags)
Definition: sde_lib.c:449
int papi_sde_register_counter_cb(papi_handle_t handle, const char *event_name, int cntr_mode, int cntr_type, papi_sde_fptr_t callback, void *param)
Definition: sde_lib.c:312
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_SUM
Definition: sde_lib.h:33
#define PAPI_SDE_DELTA
Definition: sde_lib.h:25
int(* describe_counter)(papi_handle_t handle, const char *event_name, const char *event_description)
Definition: sde_lib.h:108
papi_handle_t(* init)(const char *lib_name)
Definition: sde_lib.h:103
int(* register_counter)(papi_handle_t handle, const char *event_name, int mode, int type, void *counter)
Definition: sde_lib.h:105
int(* add_counter_to_group)(papi_handle_t handle, const char *event_name, const char *group_name, uint32_t group_flags)
Definition: sde_lib.h:109
int(* register_counter_cb)(papi_handle_t handle, const char *event_name, int mode, int type, papi_sde_fptr_t callback, void *param)
Definition: sde_lib.h:106
volatile double y
volatile double x