File: loop_spawn.c

package info (click to toggle)
prrte 3.0.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 9,128 kB
  • sloc: ansic: 80,431; sh: 4,289; perl: 3,195; makefile: 1,829; lex: 352; python: 239
file content (205 lines) | stat: -rw-r--r-- 6,794 bytes parent folder | download | duplicates (2)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*file .c : spawned  the file Exe*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#include <sys/time.h>
#include <errno.h>
#include <getopt.h>

#include <pmix.h>

#include "test.h"

#define     EXE_TEST             "./loop_child"

static bool verbose = false;

static void regcbfunc(pmix_status_t status, size_t ref, void *cbdata)
{
    mylock_t *lock = (mylock_t *) cbdata;

    DEBUG_WAKEUP_THREAD(lock);
}

static void evhandler(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)
{
    mylock_t *lock = NULL;
    pmix_status_t jobstatus = 0;
    pmix_nspace_t jobid = {0};
    size_t n;
    char *msg = NULL;

    /* 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)) {
                PMIX_LOAD_NSPACE(jobid, info[n].value.data.proc->nspace);
            } else if (0 == strncmp(info[n].key, PMIX_EVENT_RETURN_OBJECT, PMIX_MAX_KEYLEN)) {
                lock = (mylock_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 (verbose) {
            fprintf(stdout, "JOB %s COMPLETED WITH STATUS %d MSG %s\n",
                    jobid, jobstatus,  (NULL == msg) ? "NONE" : msg);
        }
    }
    if (NULL != lock) {
        /* save the status */
        lock->status = jobstatus;
        if (NULL != msg) {
            lock->msg = strdup(msg);
        }
        /* release the lock */
        DEBUG_WAKEUP_THREAD(lock);
    }

    /* we _always_ have to execute the evhandler callback or
     * else the event progress engine will hang */
    if (NULL != cbfunc) {
        cbfunc(PMIX_EVENT_ACTION_COMPLETE, NULL, 0, NULL, NULL, cbdata);
    }
}

int main(int argc, char **argv)
{
    int iter, itermax = 100;
    bool sync = false;
    pmix_status_t rc;
    pmix_proc_t myproc;
    pmix_proc_t procs[2];
    pmix_app_t app;
    pmix_nspace_t nspace;
    pmix_proc_t pname;
    mylock_t lock, rellock;
    pmix_info_t iptr[3];
    static struct option myoptions[] = {
        {"iters", required_argument, NULL, 'i'},
        {"sync", no_argument, NULL, 's'},
        {"verbose", no_argument, NULL, 'v'},
        {"help", no_argument, NULL, 'h'},
        {"report", required_argument, NULL, 'r'},
        {0, 0, 0, 0}
    };
    int option_index;
    int opt;
    int freq = 100;

    while ((opt = getopt_long(argc, argv, "hsvi:r:", myoptions, &option_index)) != -1) {
        switch (opt) {
            case 'i':
                itermax = strtol(optarg, NULL, 10);
                break;
            case 's':
                sync = true;
                break;
            case 'v':
                verbose = true;
                break;
            case 'r':
                freq = strtol(optarg, NULL, 10);
                break;
            case 'h':
                fprintf(stderr,
                        "Usage: %s\n    Options:\n"
                        "        [-i N] [number of iterations]\n"
                        "        [-s] [Sync mode - wait for termination before spawning next child]\n"
                        "        [-v] [Verbose]\n"
                        "        [-r N] [Report progress every N iterations]\n",
                        argv[0]);
                exit(1);
            default:
                fprintf(stderr,
                        "Usage: %s\n    Options:\n"
                        "        [-i N] [number of iterations]\n"
                        "        [-s] [Sync mode - wait for termination before spawning next child]\n"
                        "        [-v] [Verbose]\n"
                        "        [-r N] [Report progress every N iterations]\n",
                        argv[0]);
                exit(1);
        }
    }

    if (verbose) {
        printf("parent*******************************\n");
        printf("parent: Launching Child*\n");
    }


    rc = PMIx_Init(&myproc, NULL, 0);
    if (PMIX_SUCCESS != rc) {
        fprintf(stderr, "Failed to init\n");
        exit(1);
    }

    PMIX_APP_CONSTRUCT(&app);
    app.cmd = strdup(EXE_TEST);
    PMIX_ARGV_APPEND(rc, app.argv, EXE_TEST);
    if (verbose) {
        PMIX_ARGV_APPEND(rc, app.argv, "--verbose");
    }
    app.maxprocs = 1;

    PMIX_XFER_PROCID(&procs[0], &myproc);

    for (iter = 0; iter < itermax; ++iter) {
        rc = PMIx_Spawn(NULL, 0, &app, 1, nspace);
        if (PMIX_SUCCESS != rc) {
            fprintf(stderr, "Failed to spawn iteration %d: %s\n", iter, PMIx_Error_string(rc));
            goto done;
        }
        if (sync) {
            DEBUG_CONSTRUCT_LOCK(&rellock);
            rc = PMIX_EVENT_JOB_END;
            /* give the handler a name */
            PMIX_INFO_LOAD(&iptr[0], PMIX_EVENT_HDLR_NAME, "JOB_TERMINATION_EVENT", PMIX_STRING);
            /* specify we only want to be notified when our
             * child job terminates */
            PMIX_LOAD_PROCID(&pname, nspace, PMIX_RANK_WILDCARD);
            PMIX_INFO_LOAD(&iptr[1], PMIX_EVENT_AFFECTED_PROC, &pname, PMIX_PROC);
            /* request that they return our lock object */
            PMIX_INFO_LOAD(&iptr[2], PMIX_EVENT_RETURN_OBJECT, &rellock, PMIX_POINTER);
            /* do the registration */
            DEBUG_CONSTRUCT_LOCK(&lock);
            PMIx_Register_event_handler(&rc, 1, iptr, 3, evhandler, regcbfunc, &lock);
            DEBUG_WAIT_THREAD(&lock);
            DEBUG_DESTRUCT_LOCK(&lock);
            PMIX_INFO_DESTRUCT(&iptr[0]);
            PMIX_INFO_DESTRUCT(&iptr[1]);
            PMIX_INFO_DESTRUCT(&iptr[2]);
        }

        PMIX_LOAD_PROCID(&procs[1], nspace, 0);
        rc = PMIx_Connect(procs, 2, NULL, 0);
        if (PMIX_SUCCESS != rc) {
            fprintf(stderr, "[%s.%u]: Failed to connect\n", myproc.nspace, myproc.rank);
            exit(1);
        }

        PMIx_Disconnect(procs, 2, NULL, 0);

        if (sync) {
            DEBUG_WAIT_THREAD(&rellock);
            DEBUG_DESTRUCT_LOCK(&rellock);
        }
        if (!verbose && 0 == (iter % freq)) {
            fprintf(stderr, "Completed iteration %d\n", iter);
        }
    }

done:
    PMIx_Finalize(NULL, 0);
    if (verbose) {
        printf("parent: End .\n" );
    }
    return 0;
}