PAPI 7.1.0.0
Loading...
Searching...
No Matches
ctests/locks_pthreads.c
Go to the documentation of this file.
1/* This file checks to make sure the locking mechanisms work correctly */
2/* on the platform. */
3/* Platforms where the locking mechanisms are not implemented or are */
4/* incorrectly implemented will fail. -KSL */
5
6#define MAX_THREADS 256
7#define APPR_TOTAL_ITER 1000000
8
9#include <stdio.h>
10#include <stdlib.h>
11#include <unistd.h>
12#include <pthread.h>
13#include <math.h>
14
15#include "papi.h"
16#include "papi_test.h"
17
18volatile long long count = 0;
19volatile long long tmpcount = 0;
20volatile long long thread_iter = 0;
21
22static int quiet=0;
23
24void
25lockloop( int iters, volatile long long *mycount )
26{
27 int i;
28 for ( i = 0; i < iters; i++ ) {
30 *mycount = *mycount + 1;
32 }
33}
34
35void *
36Slave( void *arg )
37{
38 long long duration;
39
40 duration = PAPI_get_real_usec( );
42 duration = PAPI_get_real_usec( ) - duration;
43
44 if (!quiet) {
45 printf("%f lock/unlocks per us\n",
46 (float)thread_iter/(float)duration);
47 }
48 pthread_exit( arg );
49}
50
51
52int
53main( int argc, char **argv )
54{
55 pthread_t slaves[MAX_THREADS];
56 int rc, i, nthr;
57 int retval;
58 const PAPI_hw_info_t *hwinfo = NULL;
59
60 /* Set TESTS_QUIET variable */
61 quiet = tests_quiet( argc, argv );
62
64 if (retval != PAPI_VER_CURRENT ) {
65 test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
66 }
67
68 hwinfo = PAPI_get_hardware_info( );
69 if (hwinfo == NULL ) {
70 test_fail( __FILE__, __LINE__, "PAPI_get_hardware_info", 2 );
71 }
72
73 retval = PAPI_thread_init((unsigned long (*)(void)) ( pthread_self ) );
74 if ( retval != PAPI_OK ) {
75 if ( retval == PAPI_ECMP ) {
76 test_skip( __FILE__, __LINE__,
77 "PAPI_thread_init", retval );
78 }
79 else {
80 test_fail( __FILE__, __LINE__,
81 "PAPI_thread_init", retval );
82 }
83 }
84
85 if ( hwinfo->ncpu > MAX_THREADS ) {
86 nthr = MAX_THREADS;
87 }
88 else {
89 nthr = hwinfo->ncpu;
90 }
91
92 /* Scale the per thread work to keep the serial runtime about the same. */
93 thread_iter = APPR_TOTAL_ITER/sqrt(nthr);
94
95 if (!quiet) {
96 printf( "Creating %d threads, %lld lock/unlock\n",
97 nthr , thread_iter);
98 }
99
100 for ( i = 0; i < nthr; i++ ) {
101 rc = pthread_create( &slaves[i], NULL, Slave, NULL );
102 if ( rc ) {
104 test_fail( __FILE__, __LINE__,
105 "pthread_create", retval );
106 }
107 }
108
109 for ( i = 0; i < nthr; i++ ) {
110 pthread_join( slaves[i], NULL );
111 }
112
113 if (!quiet) {
114 printf( "Expected: %lld Received: %lld\n",
115 ( long long ) nthr * thread_iter,
116 count );
117 }
118
119 if ( nthr * thread_iter != count ) {
120 test_fail( __FILE__, __LINE__, "Thread Locks", 1 );
121 }
122
123 test_pass( __FILE__ );
124
125 return 0;
126
127}
int i
get information about the system hardware
get real time counter value in microseconds
initialize the PAPI library.
Lock one of two mutex variables defined in papi.h.
Initialize thread support in the PAPI library.
Unlock one of the mutex variables defined in papi.h.
void lockloop(int iters, volatile long long *mycount)
static int quiet
volatile long long tmpcount
#define MAX_THREADS
volatile long long count
void * Slave(void *arg)
volatile long long thread_iter
#define APPR_TOTAL_ITER
#define PAPI_VER_CURRENT
Definition: f90papi.h:54
#define PAPI_OK
Definition: f90papi.h:73
#define PAPI_ESYS
Definition: f90papi.h:136
#define PAPI_ECMP
Definition: f90papi.h:214
#define PAPI_USR1_LOCK
Definition: f90papi.h:197
Return codes and api definitions.
unsigned long int pthread_t
int tests_quiet(int argc, char **argv)
Definition: test_utils.c:376
void PAPI_NORETURN test_fail(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:491
void PAPI_NORETURN test_pass(const char *filename)
Definition: test_utils.c:432
void PAPI_NORETURN test_skip(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:584
int main()
Definition: pernode.c:20
rc
Definition: pscanf.h:23
Hardware info structure.
Definition: papi.h:774
int ncpu
Definition: papi.h:775
int retval
Definition: zero_fork.c:53