PAPI  5.6.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
zero_omp.c
Go to the documentation of this file.
1 /*
2 * File: zero_omp.c
3 * Author: Philip Mucci
4 * mucci@cs.utk.edu
5 * Mods: Nils Smeds
6 * smeds@pdc.kth.se
7 * Anders Nilsson
8 * anni@pdc.kth.se
9 */
10 
11 /* This file performs the following test: start, stop and timer
12 functionality for 2 slave OMP threads
13 
14  - It attempts to use the following two counters. It may use less
15 depending on hardware counter resource limitations. These are counted
16 in the default counting domain and default granularity, depending on
17 the platform. Usually this is the user domain (PAPI_DOM_USER) and
18 thread context (PAPI_GRN_THR).
19 
20  + PAPI_FP_INS
21  + PAPI_TOT_CYC
22 
23 Each thread inside the Thread routine:
24  - Get cyc.
25  - Get us.
26  - Start counters
27  - Do flops
28  - Stop and read counters
29  - Get us.
30  - Get cyc.
31 
32 Master serial thread:
33  - Get us.
34  - Get cyc.
35  - Run parallel for loop
36  - Get us.
37  - Get cyc.
38 */
39 
40 #include <stdio.h>
41 #include <stdlib.h>
42 
43 #include "papi.h"
44 #include "papi_test.h"
45 
46 #include "do_loops.h"
47 
48 #ifdef _OPENMP
49 #include <omp.h>
50 #else
51 #error "This compiler does not understand OPENMP"
52 #endif
53 
54 const PAPI_hw_info_t *hw_info = NULL;
55 
56 void
57 Thread( int n )
58 {
59  int retval, num_tests = 1;
60  int EventSet1 = PAPI_NULL;
61  int PAPI_event, mask1;
62  int num_events1;
63  long long **values;
64  long long elapsed_us, elapsed_cyc;
66 
67  if (!TESTS_QUIET) {
68  printf( "Thread %#x started\n", omp_get_thread_num( ) );
69  }
70 
71  /* add PAPI_TOT_CYC and one of the events in
72  PAPI_FP_INS, PAPI_FP_OPS or PAPI_TOT_INS,
73  depending on the availability of the event
74  on the platform */
75  EventSet1 = add_two_events( &num_events1, &PAPI_event, &mask1 );
76  if (num_events1==0) {
77  if (!TESTS_QUIET) printf("No events added!\n");
78  test_fail(__FILE__,__LINE__,"No events",0);
79  }
80 
81  retval = PAPI_event_code_to_name( PAPI_event, event_name );
82  if ( retval != PAPI_OK )
83  test_fail( __FILE__, __LINE__, "PAPI_event_code_to_name", retval );
84 
85  values = allocate_test_space( num_tests, num_events1 );
86 
87  elapsed_us = PAPI_get_real_usec( );
88 
89  elapsed_cyc = PAPI_get_real_cyc( );
90 
91  retval = PAPI_start( EventSet1 );
92  if ( retval != PAPI_OK )
93  test_fail( __FILE__, __LINE__, "PAPI_start", retval );
94 
95  do_flops( n );
96 
97  retval = PAPI_stop( EventSet1, values[0] );
98  if ( retval != PAPI_OK )
99  test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
100 
101  elapsed_us = PAPI_get_real_usec( ) - elapsed_us;
102 
103  elapsed_cyc = PAPI_get_real_cyc( ) - elapsed_cyc;
104 
105  remove_test_events( &EventSet1, mask1 );
106 
107  if ( !TESTS_QUIET ) {
108  printf( "Thread %#x %-12s : \t%lld\n", omp_get_thread_num( ),
109  event_name, values[0][1] );
110  printf( "Thread %#x PAPI_TOT_CYC: \t%lld\n", omp_get_thread_num( ),
111  values[0][0] );
112  printf( "Thread %#x Real usec : \t%lld\n", omp_get_thread_num( ),
113  elapsed_us );
114  printf( "Thread %#x Real cycles : \t%lld\n", omp_get_thread_num( ),
115  elapsed_cyc );
116  }
117 
118  /* It is illegal for the threads to exit in OpenMP */
119  /* test_pass(__FILE__,0,0); */
120  free_test_space( values, num_tests );
121 
123  if (!TESTS_QUIET) {
124  printf( "Thread %#x finished\n", omp_get_thread_num( ) );
125  }
126 }
127 
128 int
129 main( int argc, char **argv )
130 {
131  int retval;
132  long long elapsed_us, elapsed_cyc;
133  int quiet;
134 
135  /* Set TESTS_QUIET variable */
136  quiet = tests_quiet( argc, argv );
137 
139  if ( retval != PAPI_VER_CURRENT ) {
140  test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
141  }
142 
143  hw_info = PAPI_get_hardware_info( );
144  if ( hw_info == NULL ) {
145  test_fail( __FILE__, __LINE__, "PAPI_get_hardware_info", 2 );
146  }
147 
149  if (!quiet) printf("Can't find PAPI_TOT_INS\n");
150  test_skip(__FILE__,__LINE__,"Event missing",1);
151  }
152 
154  if (!quiet) printf("Can't find PAPI_TOT_CYC\n");
155  test_skip(__FILE__,__LINE__,"Event missing",1);
156  }
157 
158  elapsed_us = PAPI_get_real_usec( );
159 
160  elapsed_cyc = PAPI_get_real_cyc( );
161 
162 
163  retval = PAPI_thread_init( ( unsigned long ( * )( void ) )
164  ( omp_get_thread_num ) );
165  if ( retval != PAPI_OK ) {
166  if ( retval == PAPI_ECMP ) {
167  if (!quiet) printf("Trouble init threads\n");
168  test_skip( __FILE__, __LINE__, "PAPI_thread_init", retval );
169  }
170  else {
171  test_fail( __FILE__, __LINE__, "PAPI_thread_init", retval );
172  }
173  }
174 #pragma omp parallel
175  {
176  Thread( 1000000 * ( omp_get_thread_num( ) + 1 ) );
177  }
178  omp_set_num_threads( 1 );
179  Thread( 1000000 * ( omp_get_thread_num( ) + 1 ) );
180  omp_set_num_threads( omp_get_max_threads( ) );
181 #pragma omp parallel
182  {
183  Thread( 1000000 * ( omp_get_thread_num( ) + 1 ) );
184  }
185 
186  elapsed_cyc = PAPI_get_real_cyc( ) - elapsed_cyc;
187 
188  elapsed_us = PAPI_get_real_usec( ) - elapsed_us;
189 
190  if ( !TESTS_QUIET ) {
191  printf( "Master real usec : \t%lld\n", elapsed_us );
192  printf( "Master real cycles : \t%lld\n", elapsed_cyc );
193  }
194 
195  test_pass( __FILE__ );
196 
197  return 0;
198 }
char event_name[2][PAPI_MAX_STR_LEN]
Definition: data_range.c:29
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2314
int mask1
Definition: zero_fork.c:48
int PAPI_event[2]
Definition: data_range.c:30
void test_pass(const char *filename)
Definition: test_utils.c:432
Hardware info structure.
Definition: papi.h:780
#define PAPI_NULL
Definition: papi.h:292
int num_events1
Definition: zero_fork.c:49
#define PAPI_TOT_INS
return PAPI_OK
Definition: linux-nvml.c:497
int int argc
Definition: iozone.c:1609
Return codes and api definitions.
void test_skip(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:559
char ** argv
Definition: iozone.c:1610
int PAPI_thread_init(unsigned long int(*id_fn)(void))
Definition: papi.c:123
int PAPI_library_init(int version)
Definition: papi.c:500
int quiet
Definition: rapl_overflow.c:18
#define PAPI_TOT_CYC
long long elapsed_cyc
Definition: zero_fork.c:50
int add_two_events(int *num_events, int *papi_event, int *mask)
Definition: test_utils.c:615
void free_test_space(long long **values, int num_tests)
Definition: test_utils.c:70
#define PAPI_ECMP
Definition: papi.h:256
int PAPI_event_code_to_name(int EventCode, char *out)
Definition: papi.c:915
int num_tests
Definition: zero_fork.c:53
void do_flops(int n)
Definition: multiplex.c:23
int PAPI_query_event(int EventCode)
Definition: papi.c:684
long long PAPI_get_real_usec(void)
Definition: papi.c:6264
printf("\tTry: -i 0 -i 1 \n\n")
int PAPI_unregister_thread(void)
Definition: papi.c:244
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
static void * Thread(void *arg)
#define PAPI_MAX_STR_LEN
Definition: papi.h:465
long long PAPI_get_real_cyc(void)
Definition: papi.c:6217
int PAPI_start(int EventSet)
Definition: papi.c:2096
const PAPI_hw_info_t * PAPI_get_hardware_info(void)
Definition: papi.c:6185
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
ssize_t retval
Definition: libasync.c:338
long long elapsed_us
Definition: zero_fork.c:50
long long ** allocate_test_space(int num_tests, int num_events)
Definition: test_utils.c:46
#define PAPI_VER_CURRENT
Definition: papi.h:225
static const PAPI_hw_info_t * hw_info
Definition: byte_profile.c:28
int main(int argc, char **argv)
List all appio events codes and names.
int remove_test_events(int *EventSet, int mask)
Definition: test_utils.c:201
int EventSet1
Definition: zero_fork.c:47