PAPI  5.0.1.0
locks_pthreads.c
Go to the documentation of this file.
00001 /* This file checks to make sure the locking mechanisms work correctly on the platform.
00002  * Platforms where the locking mechanisms are not implemented or are incorrectly implemented
00003  * will fail.  -KSL
00004  */
00005 
00006 #include <pthread.h>
00007 #include "papi_test.h"
00008 
00009 volatile long long count = 0;
00010 volatile long long tmpcount = 0;
00011 volatile int num_iters = 0;
00012 
00013 void
00014 lockloop( int iters, volatile long long *mycount )
00015 {
00016     int i;
00017     for ( i = 0; i < iters; i++ ) {
00018         PAPI_lock( PAPI_USR1_LOCK );
00019         *mycount = *mycount + 1;
00020         PAPI_unlock( PAPI_USR1_LOCK );
00021     }
00022 }
00023 
00024 void *
00025 Slave( void *arg )
00026 {
00027     long long duration;
00028 
00029     ( void ) arg;
00030 
00031     sleep( 1 );
00032     duration = PAPI_get_real_usec(  );
00033     lockloop( 10000, &tmpcount );
00034     duration = PAPI_get_real_usec(  ) - duration;
00035 
00036     /* First one here set's the number */
00037     PAPI_lock( PAPI_USR2_LOCK );
00038     if ( num_iters == 0 ) {
00039         printf( "10000 iterations took %lld us.\n", duration );
00040         num_iters = ( int ) ( 10 * ( TIME_LIMIT_IN_US / duration ) );
00041         printf( "Running %d iterations\n", num_iters );
00042     }
00043     PAPI_unlock( PAPI_USR2_LOCK );
00044 
00045     lockloop( num_iters, &count );
00046     pthread_exit( NULL );
00047 }
00048 
00049 
00050 int
00051 main( int argc, char **argv )
00052 {
00053     pthread_t slaves[MAX_THREADS];
00054     int rc, i, nthr;
00055     int retval;
00056     const PAPI_hw_info_t *hwinfo = NULL;
00057 
00058     /* Set TESTS_QUIET variable */
00059     tests_quiet( argc, argv );  
00060 
00061     if ( ( retval =
00062            PAPI_library_init( PAPI_VER_CURRENT ) ) != PAPI_VER_CURRENT )
00063         test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
00064 
00065     if ( ( hwinfo = PAPI_get_hardware_info(  ) ) == NULL )
00066         test_fail( __FILE__, __LINE__, "PAPI_get_hardware_info", 2 );
00067 
00068     retval =
00069         PAPI_thread_init( ( unsigned long ( * )( void ) ) ( pthread_self ) );
00070     if ( retval != PAPI_OK ) {
00071         if ( retval == PAPI_ECMP )
00072             test_skip( __FILE__, __LINE__, "PAPI_thread_init", retval );
00073         else
00074             test_fail( __FILE__, __LINE__, "PAPI_thread_init", retval );
00075     }
00076 
00077     if ( hwinfo->ncpu > MAX_THREADS )
00078         nthr = MAX_THREADS;
00079     else
00080         nthr = hwinfo->ncpu;
00081 
00082     printf( "Creating %d threads\n", nthr );
00083 
00084     for ( i = 0; i < nthr; i++ ) {
00085         rc = pthread_create( &slaves[i], NULL, Slave, NULL );
00086         if ( rc ) {
00087             retval = PAPI_ESYS;
00088             test_fail( __FILE__, __LINE__, "pthread_create", retval );
00089         }
00090     }
00091 
00092     for ( i = 0; i < nthr; i++ ) {
00093         pthread_join( slaves[i], NULL );
00094     }
00095 
00096     printf( "Expected: %lld Received: %lld\n", ( long long ) nthr * num_iters,
00097             count );
00098     if ( nthr * num_iters != count )
00099         test_fail( __FILE__, __LINE__, "Thread Locks", 1 );
00100 
00101     test_pass( __FILE__, NULL, 0 );
00102     exit( 1 );
00103 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines