root/opal/mca/pmix/pmix4x/pmix/test/simple/simpjctrl.c

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

DEFINITIONS

This source file includes following definitions.
  1. notification_fn
  2. evhandler_reg_callbk
  3. infocbfunc
  4. main

   1 /*
   2  * Copyright (c) 2004-2010 The Trustees of Indiana University and Indiana
   3  *                         University Research and Technology
   4  *                         Corporation.  All rights reserved.
   5  * Copyright (c) 2004-2011 The University of Tennessee and The University
   6  *                         of Tennessee Research Foundation.  All rights
   7  *                         reserved.
   8  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
   9  *                         University of Stuttgart.  All rights reserved.
  10  * Copyright (c) 2004-2005 The Regents of the University of California.
  11  *                         All rights reserved.
  12  * Copyright (c) 2006-2013 Los Alamos National Security, LLC.
  13  *                         All rights reserved.
  14  * Copyright (c) 2009-2012 Cisco Systems, Inc.  All rights reserved.
  15  * Copyright (c) 2011      Oak Ridge National Labs.  All rights reserved.
  16  * Copyright (c) 2013-2018 Intel, Inc.  All rights reserved.
  17  * Copyright (c) 2015      Mellanox Technologies, Inc.  All rights reserved.
  18  * $COPYRIGHT$
  19  *
  20  * Additional copyrights may follow
  21  *
  22  * $HEADER$
  23  *
  24  */
  25 
  26 #define _GNU_SOURCE
  27 #include <stdbool.h>
  28 #include <stdio.h>
  29 #include <stdlib.h>
  30 #include <unistd.h>
  31 #include <time.h>
  32 #include <signal.h>
  33 
  34 #include <pmix.h>
  35 #include "simptest.h"
  36 
  37 static pmix_proc_t myproc;
  38 
  39 /* this is the event notification function we pass down below
  40  * when registering for general events - i.e.,, the default
  41  * handler. We don't technically need to register one, but it
  42  * is usually good practice to catch any events that occur */
  43 static void notification_fn(size_t evhdlr_registration_id,
  44                             pmix_status_t status,
  45                             const pmix_proc_t *source,
  46                             pmix_info_t info[], size_t ninfo,
  47                             pmix_info_t results[], size_t nresults,
  48                             pmix_event_notification_cbfunc_fn_t cbfunc,
  49                             void *cbdata)
  50 {
  51     if (NULL != cbfunc) {
  52         cbfunc(PMIX_EVENT_ACTION_COMPLETE, NULL, 0, NULL, NULL, cbdata);
  53     }
  54 }
  55 
  56 /* event handler registration is done asynchronously because it
  57  * may involve the PMIx server registering with the host RM for
  58  * external events. So we provide a callback function that returns
  59  * the status of the request (success or an error), plus a numerical index
  60  * to the registered event. The index is used later on to deregister
  61  * an event handler - if we don't explicitly deregister it, then the
  62  * PMIx server will do so when it see us exit */
  63 static void evhandler_reg_callbk(pmix_status_t status,
  64                                  size_t evhandler_ref,
  65                                  void *cbdata)
  66 {
  67     mylock_t *lk = (mylock_t*)cbdata;
  68 
  69     if (PMIX_SUCCESS != status) {
  70         fprintf(stderr, "Client %s:%d EVENT HANDLER REGISTRATION FAILED WITH STATUS %d, ref=%lu\n",
  71                    myproc.nspace, myproc.rank, status, (unsigned long)evhandler_ref);
  72     }
  73     lk->status = status;
  74     DEBUG_WAKEUP_THREAD(lk);
  75 }
  76 
  77 static void infocbfunc(pmix_status_t status,
  78                        pmix_info_t *info, size_t ninfo,
  79                        void *cbdata,
  80                        pmix_release_cbfunc_t release_fn,
  81                        void *release_cbdata)
  82 {
  83     mylock_t *lk = (mylock_t*)cbdata;
  84 
  85     fprintf(stderr, "Callback recvd with status %d\n", status);
  86 
  87     /* release the caller */
  88     if (NULL != release_fn) {
  89         release_fn(release_cbdata);
  90     }
  91 
  92     lk->status = status;
  93     DEBUG_WAKEUP_THREAD(lk);
  94 }
  95 
  96 int main(int argc, char **argv)
  97 {
  98     int rc;
  99     pmix_value_t value;
 100     pmix_value_t *val = &value;
 101     pmix_proc_t proc;
 102     uint32_t nprocs, n;
 103     pmix_info_t *info, *iptr;
 104     bool flag;
 105     mylock_t mylock;
 106     pmix_data_array_t *dptr;
 107 
 108     /* init us - note that the call to "init" includes the return of
 109      * any job-related info provided by the RM. */
 110     if (PMIX_SUCCESS != (rc = PMIx_Init(&myproc, NULL, 0))) {
 111         fprintf(stderr, "Client ns %s rank %d: PMIx_Init failed: %d\n", myproc.nspace, myproc.rank, rc);
 112         exit(0);
 113     }
 114     fprintf(stderr, "Client ns %s rank %d: Running\n", myproc.nspace, myproc.rank);
 115 
 116 
 117     /* register our default event handler - again, this isn't strictly
 118      * required, but is generally good practice */
 119     DEBUG_CONSTRUCT_LOCK(&mylock);
 120     PMIx_Register_event_handler(NULL, 0, NULL, 0,
 121                                 notification_fn, evhandler_reg_callbk, (void*)&mylock);
 122     DEBUG_WAIT_THREAD(&mylock);
 123     if (0 != mylock.status) {
 124         fprintf(stderr, "[%s:%d] Default handler registration failed\n", myproc.nspace, myproc.rank);
 125         exit(mylock.status);
 126     }
 127     DEBUG_DESTRUCT_LOCK(&mylock);
 128 
 129     /* job-related info is found in our nspace, assigned to the
 130      * wildcard rank as it doesn't relate to a specific rank. Setup
 131      * a name to retrieve such values */
 132     PMIX_PROC_CONSTRUCT(&proc);
 133     (void)strncpy(proc.nspace, myproc.nspace, PMIX_MAX_NSLEN);
 134     proc.rank = PMIX_RANK_WILDCARD;
 135 
 136     /* get our universe size */
 137     if (PMIX_SUCCESS != (rc = PMIx_Get(&proc, PMIX_UNIV_SIZE, NULL, 0, &val))) {
 138         fprintf(stderr, "Client ns %s rank %d: PMIx_Get universe size failed: %d\n", myproc.nspace, myproc.rank, rc);
 139         goto done;
 140     }
 141     nprocs = val->data.uint32;
 142     PMIX_VALUE_RELEASE(val);
 143     fprintf(stderr, "Client %s:%d universe size %d\n", myproc.nspace, myproc.rank, nprocs);
 144 
 145     /* inform the RM that we are preemptible, and that our checkpoint methods are
 146      * "signal" on SIGUSR2 and event on PMIX_JCTRL_CHECKPOINT */
 147     PMIX_INFO_CREATE(info, 2);
 148     flag = true;
 149     PMIX_INFO_LOAD(&info[0], PMIX_JOB_CTRL_PREEMPTIBLE, (void*)&flag, PMIX_BOOL);
 150     /* can't use "load" to load a pmix_data_array_t */
 151     (void)strncpy(info[1].key, PMIX_JOB_CTRL_CHECKPOINT_METHOD, PMIX_MAX_KEYLEN);
 152     info[1].value.type = PMIX_DATA_ARRAY;
 153     dptr = (pmix_data_array_t*)malloc(sizeof(pmix_data_array_t));
 154     info[1].value.data.darray = dptr;
 155     dptr->type = PMIX_INFO;
 156     dptr->size = 2;
 157     PMIX_INFO_CREATE(dptr->array, dptr->size);
 158     rc = SIGUSR2;
 159     iptr = (pmix_info_t*)dptr->array;
 160     PMIX_INFO_LOAD(&iptr[0], PMIX_JOB_CTRL_CHECKPOINT_SIGNAL, &rc, PMIX_INT);
 161     rc = PMIX_JCTRL_CHECKPOINT;
 162     PMIX_INFO_LOAD(&iptr[1], PMIX_JOB_CTRL_CHECKPOINT_EVENT, &rc, PMIX_STATUS);
 163 
 164     /* since this is informational and not a requested operation, the target parameter
 165      * doesn't mean anything and can be ignored */
 166     DEBUG_CONSTRUCT_LOCK(&mylock);
 167     if (PMIX_SUCCESS != (rc = PMIx_Job_control_nb(NULL, 0, info, 2, infocbfunc, (void*)&mylock))) {
 168         fprintf(stderr, "Client ns %s rank %d: PMIx_Job_control_nb failed: %d\n", myproc.nspace, myproc.rank, rc);
 169         goto done;
 170     }
 171     DEBUG_WAIT_THREAD(&mylock);
 172     PMIX_INFO_FREE(info, 2);
 173     if (0 != mylock.status) {
 174         fprintf(stderr, "Client ns %s rank %d: PMIx_Job_control_nb failed: %d\n", myproc.nspace, myproc.rank, mylock.status);
 175         exit(mylock.status);
 176     }
 177     DEBUG_DESTRUCT_LOCK(&mylock);
 178 
 179     /* now request that this process be monitored using heartbeats */
 180     PMIX_INFO_CREATE(iptr, 1);
 181     PMIX_INFO_LOAD(&iptr[0], PMIX_MONITOR_HEARTBEAT, NULL, PMIX_POINTER);
 182 
 183     PMIX_INFO_CREATE(info, 3);
 184     PMIX_INFO_LOAD(&info[0], PMIX_MONITOR_ID, "MONITOR1", PMIX_STRING);
 185     n = 5;  // require a heartbeat every 5 seconds
 186     PMIX_INFO_LOAD(&info[1], PMIX_MONITOR_HEARTBEAT_TIME, &n, PMIX_UINT32);
 187     n = 2;  // two heartbeats can be missed before declaring us "stalled"
 188     PMIX_INFO_LOAD(&info[2], PMIX_MONITOR_HEARTBEAT_DROPS, &n, PMIX_UINT32);
 189 
 190     /* make the request */
 191     DEBUG_CONSTRUCT_LOCK(&mylock);
 192     if (PMIX_SUCCESS != (rc = PMIx_Process_monitor_nb(iptr, PMIX_MONITOR_HEARTBEAT_ALERT,
 193                                                       info, 3, infocbfunc, (void*)&mylock))) {
 194         fprintf(stderr, "Client ns %s rank %d: PMIx_Process_monitor_nb failed: %d\n", myproc.nspace, myproc.rank, rc);
 195         goto done;
 196     }
 197     DEBUG_WAIT_THREAD(&mylock);
 198     PMIX_INFO_FREE(iptr, 1);
 199     PMIX_INFO_FREE(info, 3);
 200     if (0 != mylock.status) {
 201         fprintf(stderr, "Client ns %s rank %d: PMIx_Process_monitor_nb failed: %d\n", myproc.nspace, myproc.rank, mylock.status);
 202         exit(mylock.status);
 203     }
 204     DEBUG_DESTRUCT_LOCK(&mylock);
 205 
 206     /* send a heartbeat */
 207     PMIx_Heartbeat();
 208 
 209     /* call fence to synchronize with our peers - no need to
 210      * collect any info as we didn't "put" anything */
 211     PMIX_INFO_CREATE(info, 1);
 212     flag = false;
 213     PMIX_INFO_LOAD(info, PMIX_COLLECT_DATA, &flag, PMIX_BOOL);
 214     if (PMIX_SUCCESS != (rc = PMIx_Fence(&proc, 1, info, 1))) {
 215         fprintf(stderr, "Client ns %s rank %d: PMIx_Fence failed: %d\n", myproc.nspace, myproc.rank, rc);
 216         goto done;
 217     }
 218     PMIX_INFO_FREE(info, 1);
 219 
 220 
 221   done:
 222     /* finalize us */
 223     fprintf(stderr, "Client ns %s rank %d: Finalizing\n", myproc.nspace, myproc.rank);
 224     if (PMIX_SUCCESS != (rc = PMIx_Finalize(NULL, 0))) {
 225         fprintf(stderr, "Client ns %s rank %d:PMIx_Finalize failed: %d\n", myproc.nspace, myproc.rank, rc);
 226     } else {
 227         fprintf(stderr, "Client ns %s rank %d:PMIx_Finalize successfully completed\n", myproc.nspace, myproc.rank);
 228     }
 229     fflush(stderr);
 230     return(0);
 231 }

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