PAPI  5.0.1.0
zero_omp.c
Go to the documentation of this file.
00001 /* 
00002 * File:    zero_omp.c
00003 * Author:  Philip Mucci
00004 *          mucci@cs.utk.edu
00005 * Mods:    Nils Smeds
00006 *          smeds@pdc.kth.se
00007 *          Anders Nilsson
00008 *          anni@pdc.kth.se
00009 */
00010 
00011 /* This file performs the following test: start, stop and timer
00012 functionality for 2 slave OMP threads
00013 
00014    - It attempts to use the following two counters. It may use less
00015 depending on hardware counter resource limitations. These are counted
00016 in the default counting domain and default granularity, depending on
00017 the platform. Usually this is the user domain (PAPI_DOM_USER) and
00018 thread context (PAPI_GRN_THR).
00019 
00020      + PAPI_FP_INS
00021      + PAPI_TOT_CYC
00022 
00023 Each thread inside the Thread routine:
00024    - Get cyc.
00025    - Get us.
00026    - Start counters
00027    - Do flops
00028    - Stop and read counters
00029    - Get us.
00030    - Get cyc.
00031 
00032 Master serial thread:
00033    - Get us.
00034    - Get cyc.
00035    - Run parallel for loop
00036    - Get us.
00037    - Get cyc.
00038 */
00039 
00040 #include "papi_test.h"
00041 
00042 #ifdef _OPENMP
00043 #include <omp.h>
00044 #else
00045 #error "This compiler does not understand OPENMP"
00046 #endif
00047 
00048 const PAPI_hw_info_t *hw_info = NULL;
00049 
00050 void
00051 Thread( int n )
00052 {
00053     int retval, num_tests = 1;
00054     int EventSet1 = PAPI_NULL;
00055     int PAPI_event, mask1;
00056     int num_events1;
00057     long long **values;
00058     long long elapsed_us, elapsed_cyc;
00059     char event_name[PAPI_MAX_STR_LEN];
00060 
00061     printf( "Thread 0x%x started\n", omp_get_thread_num(  ) );
00062     num_events1 = 2;
00063 
00064     /* add PAPI_TOT_CYC and one of the events in 
00065        PAPI_FP_INS, PAPI_FP_OPS or PAPI_TOT_INS, 
00066        depending on the availability of the event 
00067        on the platform */
00068     EventSet1 = add_two_events( &num_events1, &PAPI_event, &mask1 );
00069 
00070     retval = PAPI_event_code_to_name( PAPI_event, event_name );
00071     if ( retval != PAPI_OK )
00072         test_fail( __FILE__, __LINE__, "PAPI_event_code_to_name", retval );
00073 
00074     values = allocate_test_space( num_tests, num_events1 );
00075 
00076     elapsed_us = PAPI_get_real_usec(  );
00077 
00078     elapsed_cyc = PAPI_get_real_cyc(  );
00079 
00080     retval = PAPI_start( EventSet1 );
00081     if ( retval != PAPI_OK )
00082         test_fail( __FILE__, __LINE__, "PAPI_start", retval );
00083 
00084     do_flops( n );
00085 
00086     retval = PAPI_stop( EventSet1, values[0] );
00087     if ( retval != PAPI_OK )
00088         test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
00089 
00090     elapsed_us = PAPI_get_real_usec(  ) - elapsed_us;
00091 
00092     elapsed_cyc = PAPI_get_real_cyc(  ) - elapsed_cyc;
00093 
00094     remove_test_events( &EventSet1, mask1 );
00095 
00096     if ( !TESTS_QUIET ) {
00097         printf( "Thread 0x%x %-12s : \t%lld\n", omp_get_thread_num(  ),
00098                 event_name, values[0][1] );
00099         printf( "Thread 0x%x PAPI_TOT_CYC: \t%lld\n", omp_get_thread_num(  ),
00100                 values[0][0] );
00101         printf( "Thread 0x%x Real usec   : \t%lld\n", omp_get_thread_num(  ),
00102                 elapsed_us );
00103         printf( "Thread 0x%x Real cycles : \t%lld\n", omp_get_thread_num(  ),
00104                 elapsed_cyc );
00105     }
00106 
00107     /* It is illegal for the threads to exit in OpenMP */
00108     /* test_pass(__FILE__,0,0); */
00109     free_test_space( values, num_tests );
00110 
00111     PAPI_unregister_thread(  );
00112     printf( "Thread 0x%x finished\n", omp_get_thread_num(  ) );
00113 }
00114 
00115 int
00116 main( int argc, char **argv )
00117 {
00118     int maxthr, retval;
00119     long long elapsed_us, elapsed_cyc;
00120 
00121     tests_quiet( argc, argv );  /* Set TESTS_QUIET variable */
00122 
00123     retval = PAPI_library_init( PAPI_VER_CURRENT );
00124     if ( retval != PAPI_VER_CURRENT )
00125         test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
00126 
00127     hw_info = PAPI_get_hardware_info(  );
00128     if ( hw_info == NULL )
00129         test_fail( __FILE__, __LINE__, "PAPI_get_hardware_info", 2 );
00130 
00131     elapsed_us = PAPI_get_real_usec(  );
00132 
00133     elapsed_cyc = PAPI_get_real_cyc(  );
00134 
00135 
00136     retval =
00137         PAPI_thread_init( ( unsigned
00138                             long ( * )( void ) ) ( omp_get_thread_num ) );
00139     if ( retval != PAPI_OK ) {
00140         if ( retval == PAPI_ECMP )
00141             test_skip( __FILE__, __LINE__, "PAPI_thread_init", retval );
00142         else
00143             test_fail( __FILE__, __LINE__, "PAPI_thread_init", retval );
00144     }
00145 #pragma omp parallel private(maxthr)
00146     {
00147         maxthr = omp_get_num_threads(  );
00148         Thread( 1000000 * ( omp_get_thread_num(  ) + 1 ) );
00149     }
00150     omp_set_num_threads( 1 );
00151     Thread( 1000000 * ( omp_get_thread_num(  ) + 1 ) );
00152     omp_set_num_threads( omp_get_max_threads(  ) );
00153 #pragma omp parallel private(maxthr)
00154     {
00155         maxthr = omp_get_num_threads(  );
00156         Thread( 1000000 * ( omp_get_thread_num(  ) + 1 ) );
00157     }
00158 
00159     elapsed_cyc = PAPI_get_real_cyc(  ) - elapsed_cyc;
00160 
00161     elapsed_us = PAPI_get_real_usec(  ) - elapsed_us;
00162 
00163     if ( !TESTS_QUIET ) {
00164         printf( "Master real usec   : \t%lld\n", elapsed_us );
00165         printf( "Master real cycles : \t%lld\n", elapsed_cyc );
00166     }
00167 
00168     test_pass( __FILE__, NULL, 0 );
00169     exit( 0 );
00170 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines