1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2016-2019 Intel, Inc. All rights reserved.
* Copyright (c) 2021-2024 Nanook Consulting All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include "examples.h"
#include <pmix_tool.h>
static pmix_proc_t myproc;
static void notification_fn(size_t evhdlr_registration_id, pmix_status_t status,
const pmix_proc_t *source, pmix_info_t info[], size_t ninfo,
pmix_info_t results[], size_t nresults,
pmix_event_notification_cbfunc_fn_t cbfunc, void *cbdata)
{
myrel_t *lock = NULL;
size_t n;
pmix_status_t jobstatus = 0;
pmix_proc_t affected;
char *msg = NULL;
EXAMPLES_HIDE_UNUSED_PARAMS(evhdlr_registration_id, status, source, results, nresults);
memset(&affected, 0, sizeof(pmix_proc_t));
/* we should always have info returned to us - if not, there is
* nothing we can do */
if (NULL != info) {
for (n = 0; n < ninfo; n++) {
if (0 == strncmp(info[n].key, PMIX_JOB_TERM_STATUS, PMIX_MAX_KEYLEN)) {
jobstatus = info[n].value.data.status;
} else if (0 == strncmp(info[n].key, PMIX_EVENT_AFFECTED_PROC, PMIX_MAX_KEYLEN)) {
memcpy(&affected, info[n].value.data.proc, sizeof(pmix_proc_t));
} else if (0 == strncmp(info[n].key, PMIX_EVENT_RETURN_OBJECT, PMIX_MAX_KEYLEN)) {
lock = (myrel_t *) info[n].value.data.ptr;
} else if (0 == strncmp(info[n].key, PMIX_EVENT_TEXT_MESSAGE, PMIX_MAX_KEYLEN)) {
msg = info[n].value.data.string;
}
}
}
if (NULL == lock) {
fprintf(stderr, "LOCK WAS NOT RETURNED IN EVENT NOTIFICATION\n");
goto done;
}
/* save the status */
lock->lock.status = jobstatus;
if (NULL != msg) {
lock->nspace = strdup(msg);
}
/* release the lock */
DEBUG_WAKEUP_THREAD(&lock->lock);
done:
/* we _always_ have to execute the evhandler callback or
* else the event progress engine will hang */
if (NULL != cbfunc) {
cbfunc(PMIX_SUCCESS, NULL, 0, NULL, NULL, cbdata);
}
}
/* event handler registration is done asynchronously because it
* may involve the PMIx server registering with the host RM for
* external events. So we provide a callback function that returns
* the status of the request (success or an error), plus a numerical index
* to the registered event. The index is used later on to deregister
* an event handler - if we don't explicitly deregister it, then the
* PMIx server will do so when it see us exit */
static void evhandler_reg_callbk(pmix_status_t status, size_t evhandler_ref, void *cbdata)
{
mylock_t *lock = (mylock_t *) cbdata;
if (PMIX_SUCCESS != status) {
fprintf(stderr, "Client %s:%d EVENT HANDLER REGISTRATION FAILED WITH STATUS %d, ref=%lu\n",
myproc.nspace, myproc.rank, status, (unsigned long) evhandler_ref);
}
lock->status = status;
lock->evhandler_ref = evhandler_ref;
DEBUG_WAKEUP_THREAD(lock);
}
int main(int argc, char **argv)
{
pmix_status_t rc;
pmix_info_t info[3];
pmix_app_t *app;
size_t napps;
myrel_t myrel;
mylock_t mylock;
pmix_status_t code[6] = {PMIX_ERR_PROC_ABORTING,
PMIX_ERR_PROC_ABORTED,
PMIX_ERR_PROC_REQUESTED_ABORT,
PMIX_ERR_JOB_TERMINATED,
PMIX_ERR_UNREACH,
PMIX_ERR_LOST_CONNECTION_TO_SERVER};
pmix_nspace_t appspace;
EXAMPLES_HIDE_UNUSED_PARAMS(argc, argv);
/* we need to attach to a "system" PMIx server so we
* can ask it to spawn applications for us. There can
* only be one such connection on a node, so we will
* instruct the tool library to only look for it */
PMIX_INFO_LOAD(&info[0], PMIX_TOOL_DO_NOT_CONNECT, NULL, PMIX_BOOL);
PMIX_INFO_LOAD(&info[1], PMIX_LAUNCHER, NULL, PMIX_BOOL);
PMIX_INFO_LOAD(&info[2], PMIX_IOF_LOCAL_OUTPUT, NULL, PMIX_BOOL);
/* initialize the library and make the connection */
if (PMIX_SUCCESS != (rc = PMIx_tool_init(&myproc, info, 3))) {
fprintf(stderr, "PMIx_tool_init failed: %d\n", rc);
exit(rc);
}
DEBUG_CONSTRUCT_MYREL(&myrel);
/* register an event handler so we can be notified when
* our spawned job completes, or if it fails (even at launch) */
DEBUG_CONSTRUCT_LOCK(&mylock);
PMIX_INFO_LOAD(&info[0], PMIX_EVENT_RETURN_OBJECT, &myrel, PMIX_POINTER);
PMIx_Register_event_handler(code, 6, info, 1, notification_fn, evhandler_reg_callbk,
(void *) &mylock);
DEBUG_WAIT_THREAD(&mylock);
rc = mylock.status;
DEBUG_DESTRUCT_LOCK(&mylock);
if (PMIX_SUCCESS != rc) {
fprintf(stderr, "[%s:%d] Default handler registration failed\n", myproc.nspace,
myproc.rank);
goto done;
}
/* parse the cmd line and create our array of app structs
* describing the application we want launched */
napps = 1;
PMIX_APP_CREATE(app, napps);
/* setup the executable */
app[0].cmd = strdup("hello");
app[0].argv = (char **) malloc(2 * sizeof(char *));
app[0].argv[0] = strdup("hello");
app[0].argv[1] = NULL;
app[0].maxprocs = 1;
/* can also provide environmental params in the app.env field */
/* spawn the application */
PMIx_Spawn(NULL, 0, app, napps, appspace);
/* cleanup */
PMIX_APP_FREE(app, napps);
DEBUG_WAIT_THREAD(&myrel.lock);
DEBUG_DESTRUCT_MYREL(&myrel);
done:
PMIx_tool_finalize();
return (0);
}
|