root/orte/test/system/opal_hotel.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. evict_cbfunc
  2. main

   1 /* -*- C -*-
   2  *
   3  * $HEADER$
   4  *
   5  */
   6 #include <stdio.h>
   7 #include <unistd.h>
   8 
   9 #include "opal/mca/event/event.h"
  10 #include "opal/class/opal_hotel.h"
  11 #include "opal/runtime/opal.h"
  12 #include "opal/runtime/opal_progress.h"
  13 
  14 #define NUM_OCC 200
  15 #define NUM_RMS 128
  16 #define NUM_CYCLES 10
  17 
  18 static int num_evicted = 0;
  19 
  20 typedef struct {
  21     int id;
  22     int room;
  23 } occupant_t;
  24 
  25 occupant_t occupants[NUM_OCC];
  26 occupant_t *checked_out[NUM_OCC];
  27 
  28 static void evict_cbfunc(opal_hotel_t *hotel,
  29                          int room_num,
  30                          void *occupant_arg)
  31 {
  32     int *occupant = (int*) occupant_arg;
  33     fprintf(stderr, "Room %d / occupant %d evicted!\n", *occupant, room_num);
  34     ++num_evicted;
  35 }
  36 
  37 int main(int argc, char* argv[])
  38 {
  39     int rc;
  40     opal_hotel_t hotel;
  41     int i, j, rm;
  42     int num_occupied;
  43 
  44     if (0 > (rc = opal_init(&argc, &argv))) {
  45         fprintf(stderr, "orte_hotel: couldn't init opal - error code %d\n", rc);
  46         return rc;
  47     }
  48 
  49     OBJ_CONSTRUCT(&hotel, opal_hotel_t);
  50     opal_hotel_init(&hotel, NUM_RMS, opal_sync_event_base,
  51                     3000000, OPAL_EV_SYS_HI_PRI, evict_cbfunc);
  52 
  53     /* prep the occupants */
  54     for (i=0; i < NUM_OCC; i++) {
  55         occupants[i].id = i;
  56         occupants[i].room = -1;
  57     }
  58 
  59     /* arbitrarily checkin/checkout some things */
  60     for (i=0; i < NUM_RMS; i++) {
  61         if (OPAL_SUCCESS != opal_hotel_checkin(&hotel,
  62                                                (void*)(&occupants[i]), &rm)) {
  63             fprintf(stderr, "Hotel is fully occupied\n");
  64             continue;
  65         }
  66         occupants[i].room = rm;
  67         fprintf(stderr, "Occupant %d checked into room %d\n",
  68                 occupants[i].id, rm);
  69     }
  70     num_occupied = NUM_RMS;
  71     fprintf(stderr, "---------------------------------------\n");
  72 
  73     /* cycle thru adding and removing some */
  74     for (i=0; i < NUM_CYCLES; i++) {
  75         for (j=0; j < 30; j++) {
  76             fprintf(stderr, "Checking occupant %d out of room %d\n",
  77                     occupants[i + j].id, occupants[i + j].room);
  78             opal_hotel_checkout(&hotel, occupants[i + j].room);
  79             --num_occupied;
  80         }
  81         for (j=0; j < 30; j++) {
  82             if (OPAL_SUCCESS !=
  83                 opal_hotel_checkin(&hotel, (void*) &(occupants[i + j]), &rm)) {
  84                 fprintf(stderr, "Hotel is fully occupied\n");
  85                 continue;
  86             }
  87             occupants[i + j].room = rm;
  88             fprintf(stderr, "Occupant %d checked into room %d\n",
  89                     occupants[i + j].id, rm);
  90             ++num_occupied;
  91         }
  92         fprintf(stderr, "---------------------------------------\n");
  93     }
  94 
  95     /* sit here and see if we get an eviction notice */
  96     fprintf(stderr, "Waiting for %d evictions...\n", num_occupied);
  97     while (num_evicted < num_occupied) {
  98         opal_progress();
  99     }
 100     fprintf(stderr, "All occupants evicted!\n");
 101 
 102     OBJ_DESTRUCT(&hotel);
 103 
 104     opal_finalize();
 105     return 0;
 106 }

/* [<][>][^][v][top][bottom][index][help] */