Blame src/examples/locks_pthreads.c

Packit Service a1973e
/****************************************************************************
Packit Service a1973e
 * This program shows how to use PAPI_register_thread, PAPI_lock,           *
Packit Service a1973e
 * PAPI_unlock, PAPI_set_thr_specific, PAPI_get_thr_specific.               *
Packit Service a1973e
 * Warning: Don't use PAPI_lock and PAPI_unlock on platforms on which the   *
Packit Service a1973e
 * locking mechanisms are not implemented.                                  *
Packit Service a1973e
 ****************************************************************************/
Packit Service a1973e
Packit Service a1973e
#include <stdio.h>
Packit Service a1973e
#include <stdlib.h>
Packit Service a1973e
#include <pthread.h>
Packit Service a1973e
#include "papi.h" /* This needs to be included every time you use PAPI */
Packit Service a1973e
Packit Service a1973e
#define ERROR_RETURN(retval) { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__);  exit(retval); }
Packit Service a1973e
Packit Service a1973e
#define LOOPS  		100000
Packit Service a1973e
#define SLEEP_VALUE	20000
Packit Service a1973e
Packit Service a1973e
int count;
Packit Service a1973e
int rank;
Packit Service a1973e
Packit Service a1973e
void *Master(void *arg)
Packit Service a1973e
{
Packit Service a1973e
   int i, retval, tmp;
Packit Service a1973e
   int *pointer, * pointer2;
Packit Service a1973e
Packit Service a1973e
   tmp = 20;
Packit Service a1973e
   pointer = &tm;;
Packit Service a1973e
Packit Service a1973e
   /* register the thread */
Packit Service a1973e
   if ( (retval=PAPI_register_thread())!= PAPI_OK )
Packit Service a1973e
      ERROR_RETURN(retval);
Packit Service a1973e
   
Packit Service a1973e
   /* save the pointer for late use */
Packit Service a1973e
   if ( (retval=PAPI_set_thr_specific(1,pointer))!= PAPI_OK )
Packit Service a1973e
      ERROR_RETURN(retval);
Packit Service a1973e
   /* change the value of tmp */
Packit Service a1973e
   tmp = 15;
Packit Service a1973e
Packit Service a1973e
   usleep(SLEEP_VALUE);
Packit Service a1973e
   PAPI_lock(PAPI_USR1_LOCK);
Packit Service a1973e
   /* Make sure Slaves are not sleeping */
Packit Service a1973e
   for (i = 0; i < LOOPS; i++) {
Packit Service a1973e
      count = 2 * count - i;
Packit Service a1973e
   }
Packit Service a1973e
   PAPI_unlock(PAPI_USR1_LOCK);
Packit Service a1973e
Packit Service a1973e
   /* retrieve the pointer saved by PAPI_set_thr_specific */
Packit Service a1973e
   if ( (retval=PAPI_get_thr_specific(1, (void *)&pointer2)) != PAPI_OK )
Packit Service a1973e
      ERROR_RETURN(retval);
Packit Service a1973e
Packit Service a1973e
   /* the output value should be 15 */
Packit Service a1973e
   printf("Thread specific data is %d \n", *pointer2);
Packit Service a1973e
   
Packit Service a1973e
   pthread_exit(NULL);
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
void *Slave(void *arg)
Packit Service a1973e
{
Packit Service a1973e
   int i;
Packit Service a1973e
Packit Service a1973e
   PAPI_lock(PAPI_USR2_LOCK);
Packit Service a1973e
   PAPI_lock(PAPI_USR1_LOCK);
Packit Service a1973e
   for (i = 0; i < LOOPS; i++) {
Packit Service a1973e
      count += i;
Packit Service a1973e
   }
Packit Service a1973e
   PAPI_unlock(PAPI_USR1_LOCK);
Packit Service a1973e
   PAPI_unlock(PAPI_USR2_LOCK);
Packit Service a1973e
   pthread_exit(NULL);
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
Packit Service a1973e
Packit Service a1973e
int main(int argc, char **argv)
Packit Service a1973e
{
Packit Service a1973e
   pthread_t master;
Packit Service a1973e
   pthread_t slave1;
Packit Service a1973e
   int result_m, result_s, rc, i;
Packit Service a1973e
   int retval;
Packit Service a1973e
Packit Service a1973e
   /* Setup a random number so compilers can't optimize it out */
Packit Service a1973e
   count = rand();
Packit Service a1973e
   result_m = count;
Packit Service a1973e
   rank = 0;
Packit Service a1973e
Packit Service a1973e
   for (i = 0; i < LOOPS; i++) {
Packit Service a1973e
      result_m = 2 * result_m - i;
Packit Service a1973e
   }
Packit Service a1973e
   result_s = result_m;
Packit Service a1973e
Packit Service a1973e
   for (i = 0; i < LOOPS; i++) {
Packit Service a1973e
      result_s += i;
Packit Service a1973e
   }
Packit Service a1973e
Packit Service a1973e
   if ((retval = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT)
Packit Service a1973e
   {
Packit Service a1973e
      printf("Library initialization error! \n");
Packit Service a1973e
      exit(-1);
Packit Service a1973e
   }
Packit Service a1973e
Packit Service a1973e
   if ((retval = PAPI_thread_init(&pthread_self)) != PAPI_OK)
Packit Service a1973e
	 ERROR_RETURN(retval);
Packit Service a1973e
Packit Service a1973e
   if ((retval = PAPI_set_debug(PAPI_VERB_ECONT)) != PAPI_OK)
Packit Service a1973e
      ERROR_RETURN(retval);
Packit Service a1973e
Packit Service a1973e
   PAPI_lock(PAPI_USR2_LOCK);
Packit Service a1973e
   rc = pthread_create(&master, NULL, Master, NULL);
Packit Service a1973e
   if (rc) {
Packit Service a1973e
      retval = PAPI_ESYS;
Packit Service a1973e
      ERROR_RETURN(retval);
Packit Service a1973e
   }
Packit Service a1973e
   rc = pthread_create(&slave1, NULL, Slave, NULL);
Packit Service a1973e
   if (rc) {
Packit Service a1973e
      retval = PAPI_ESYS;
Packit Service a1973e
      ERROR_RETURN(retval);
Packit Service a1973e
   }
Packit Service a1973e
   pthread_join(master, NULL);
Packit Service a1973e
   printf("Master: Expected: %d  Recieved: %d\n", result_m, count);
Packit Service a1973e
   if (result_m != count)
Packit Service a1973e
      ERROR_RETURN(1);
Packit Service a1973e
   PAPI_unlock(PAPI_USR2_LOCK);
Packit Service a1973e
Packit Service a1973e
   pthread_join(slave1, NULL);
Packit Service a1973e
   printf("Slave: Expected: %d  Recieved: %d\n", result_s, count);
Packit Service a1973e
Packit Service a1973e
   if (result_s != count)
Packit Service a1973e
      ERROR_RETURN(1);
Packit Service a1973e
Packit Service a1973e
   exit(0);
Packit Service a1973e
}