File: peruse.c

package info (click to toggle)
openmpi 5.0.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 202,312 kB
  • sloc: ansic: 612,441; makefile: 42,495; sh: 11,230; javascript: 9,244; f90: 7,052; java: 6,404; perl: 5,154; python: 1,856; lex: 740; fortran: 61; cpp: 20; tcl: 12
file content (351 lines) | stat: -rw-r--r-- 10,921 bytes parent folder | download | duplicates (5)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 * Copyright (c) 2004-2006 The University of Tennessee and The University
 *                         of Tennessee Research Foundation.  All rights
 *                         reserved.
 * Copyright (c) 2004-2008 High Performance Computing Center Stuttgart,
 *                         University of Stuttgart.  All rights reserved.
 * Copyright (c) 2018      Cisco Systems, Inc.  All rights reserved
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 */

#include "ompi_config.h"
#  include <string.h>
#include "mpi.h"
#include "ompi/peruse/peruse.h"
#include "ompi/peruse/peruse-internal.h"
#include "ompi/communicator/communicator.h"
#include "ompi/runtime/params.h"

/*
 * Data
 */

typedef struct {
    const char* name;
    const int   id;
} peruse_event_associations_t;

/**
 * The associations between the peruse event name and id. This array
 * should be ended by the tuple {NULL, PERUSE_CUSTOM_EVENT}.
 */
static const peruse_event_associations_t PERUSE_events[] = {
    /* Point-to-point request events */
    { "PERUSE_COMM_REQ_ACTIVATE", PERUSE_COMM_REQ_ACTIVATE },
    { "PERUSE_COMM_REQ_MATCH_UNEX", PERUSE_COMM_REQ_MATCH_UNEX },
    { "PERUSE_COMM_REQ_INSERT_IN_POSTED_Q", PERUSE_COMM_REQ_INSERT_IN_POSTED_Q },
    { "PERUSE_COMM_REQ_REMOVE_FROM_POSTED_Q", PERUSE_COMM_REQ_REMOVE_FROM_POSTED_Q },
    { "PERUSE_COMM_REQ_XFER_BEGIN", PERUSE_COMM_REQ_XFER_BEGIN },
    { "PERUSE_COMM_REQ_XFER_CONTINUE", PERUSE_COMM_REQ_XFER_CONTINUE },
    { "PERUSE_COMM_REQ_XFER_END", PERUSE_COMM_REQ_XFER_END },
    { "PERUSE_COMM_REQ_COMPLETE", PERUSE_COMM_REQ_COMPLETE },
    { "PERUSE_COMM_REQ_NOTIFY", PERUSE_COMM_REQ_NOTIFY },
    { "PERUSE_COMM_MSG_ARRIVED", PERUSE_COMM_MSG_ARRIVED },
    { "PERUSE_COMM_MSG_INSERT_IN_UNEX_Q", PERUSE_COMM_MSG_INSERT_IN_UNEX_Q },
    { "PERUSE_COMM_MSG_REMOVE_FROM_UNEX_Q", PERUSE_COMM_MSG_REMOVE_FROM_UNEX_Q },
    { "PERUSE_COMM_MSG_MATCH_POSTED_REQ", PERUSE_COMM_MSG_MATCH_POSTED_REQ },

    /* Queue events*/
    { "PERUSE_COMM_SEARCH_POSTED_Q_BEGIN", PERUSE_COMM_SEARCH_POSTED_Q_BEGIN },
    { "PERUSE_COMM_SEARCH_POSTED_Q_END", PERUSE_COMM_SEARCH_POSTED_Q_END },
    { "PERUSE_COMM_SEARCH_UNEX_Q_BEGIN", PERUSE_COMM_SEARCH_UNEX_Q_BEGIN },
    { "PERUSE_COMM_SEARCH_UNEX_Q_END", PERUSE_COMM_SEARCH_UNEX_Q_END },
    { "PERUSE_CUSTOM_EVENT", PERUSE_CUSTOM_EVENT }
};

const int PERUSE_num_events = (sizeof(PERUSE_events) / sizeof(peruse_event_associations_t));

/*
 * PERUSE user-callable function
 */
int PERUSE_Init (void)
{
    if (MPI_PARAM_CHECK) {
        int32_t state = ompi_mpi_state;
        if (state < OMPI_MPI_STATE_INIT_COMPLETED ||
            state >= OMPI_MPI_STATE_FINALIZE_STARTED) {
            return PERUSE_ERR_INIT;
        }
    }
    ompi_peruse_init ();
    return PERUSE_SUCCESS;
}


/* Query all implemented events */
int PERUSE_Query_supported_events (int* num_supported,
                                   char*** event_names,
                                   int** events)
{
    int i;
    *num_supported = PERUSE_num_events;

    *event_names = (char**) malloc (PERUSE_num_events * sizeof (char *));
    *events = (int*) malloc (PERUSE_num_events * sizeof (int));

    for (i = 0; i < PERUSE_num_events; i++) {
        (*event_names)[i] = strdup (PERUSE_events[i].name);
        (*events)[i] = PERUSE_events[i].id;
    }

    return PERUSE_SUCCESS;
}


/* Query supported events */
int PERUSE_Query_event (const char* event_name, int* event)
{
    int i;

    for( i = 0; i < PERUSE_num_events; i++ ) {
        if( !strcmp (event_name, PERUSE_events[i].name) ) {
            *event = PERUSE_events[i].id;
            return PERUSE_SUCCESS;
        }
    }
    return PERUSE_ERR_EVENT;
}


/* Query event name */
int PERUSE_Query_event_name (int event, char** event_name)
{
    if (event < 0 || event > PERUSE_num_events ||
        NULL == PERUSE_events[event].name)
        return PERUSE_EVENT_INVALID;

    *event_name = strdup(PERUSE_events[event].name);
    return PERUSE_SUCCESS;
}


/* Get environment variables that affect MPI library behavior */
int PERUSE_Query_environment (int * env_size, char *** env)
{
    /* XXX tbd */
    return PERUSE_SUCCESS;
}

/* Query the scope of queue metrics - global or per communicator */
int PERUSE_Query_queue_event_scope (int * scope)
{
    *scope = PERUSE_PER_COMM;

    return PERUSE_SUCCESS;
}


/*
 * II. Events, objects initialization and manipulation
 */
/* Initialize event associated with an MPI communicator */
int PERUSE_Event_comm_register (int                       event,
                                MPI_Comm                  comm,
                                peruse_comm_callback_f *  callback_fn,
                                void *                    param,
                                peruse_event_h *          event_h)
{
    ompi_peruse_handle_t * handle;
    if (MPI_PARAM_CHECK) {
        OMPI_ERR_PERUSE_INIT_FINALIZE;

        if( (event < 0) || (event > PERUSE_num_events) ||
            (NULL == PERUSE_events[event].name) )
            return PERUSE_ERR_EVENT;

        if( (MPI_COMM_NULL == comm) || ompi_comm_invalid (comm) )
            return PERUSE_ERR_COMM;

        if (NULL == callback_fn)
            return PERUSE_ERR_GENERIC;

        if (NULL == event_h)
            return PERUSE_ERR_EVENT_HANDLE;
    }

    handle = OBJ_NEW (ompi_peruse_handle_t);

    /*
     * Initialize the newly created handle to the default inactive state.
     */
    handle->active = 0;
    handle->event = event;
    handle->type = PERUSE_TYPE_COMM;
    handle->comm = comm;
    handle->fn = (ompi_peruse_callback_f*) callback_fn;
    handle->param = param;

    /*
     * Update the information on the handle on the communicator
     */
    OPAL_THREAD_LOCK (&comm->c_lock);
    if( NULL == comm->c_peruse_handles ) {
        comm->c_peruse_handles = (ompi_peruse_handle_t**)calloc( PERUSE_num_events, sizeof(ompi_peruse_handle_t*) );
    }
    OPAL_THREAD_UNLOCK (&comm->c_lock);
    comm->c_peruse_handles[event] = handle;

    *event_h = handle;
    return PERUSE_SUCCESS;
}


/* Start collecting data (activate event) */
int PERUSE_Event_activate (peruse_event_h event_h)
{
    ompi_peruse_handle_t* handle = (ompi_peruse_handle_t*)event_h;

    if (MPI_PARAM_CHECK) {
        OMPI_ERR_PERUSE_INIT_FINALIZE;

        if (PERUSE_EVENT_HANDLE_NULL == event_h)
            return PERUSE_ERR_EVENT_HANDLE;
    }

    OPAL_THREAD_LOCK (&handle->lock);
    handle->active = 1;
    OPAL_THREAD_UNLOCK (&handle->lock);
    return PERUSE_SUCCESS;
}


/* Stop collecting data (deactivate event) */
int PERUSE_Event_deactivate (peruse_event_h event_h)
{
    ompi_peruse_handle_t* handle = (ompi_peruse_handle_t*)event_h;

    if (MPI_PARAM_CHECK) {
        OMPI_ERR_PERUSE_INIT_FINALIZE;

        if (PERUSE_EVENT_HANDLE_NULL == event_h)
            return PERUSE_ERR_EVENT_HANDLE;
    }

    OPAL_THREAD_LOCK (&handle->lock);
    handle->active = 0;
    OPAL_THREAD_UNLOCK (&handle->lock);
    return PERUSE_SUCCESS;
}


/* Free event handle */
int PERUSE_Event_release (peruse_event_h * event_h)
{
    if (MPI_PARAM_CHECK) {
        OMPI_ERR_PERUSE_INIT_FINALIZE;

        if (PERUSE_EVENT_HANDLE_NULL == event_h)
            return PERUSE_ERR_EVENT_HANDLE;
    }
    /*
     * XXX
     */
    *event_h = PERUSE_EVENT_HANDLE_NULL;
    return PERUSE_SUCCESS;
}

#define PERUSE_MPI_PARAM_CHECK(obj_upper,obj_lower )                  \
    if (MPI_PARAM_CHECK) {                                            \
        OMPI_ERR_PERUSE_INIT_FINALIZE;                                \
                                                                      \
        if (PERUSE_EVENT_HANDLE_NULL == event_h ||                    \
            ((ompi_peruse_handle_t*)event_h)->active ||               \
            ((ompi_peruse_handle_t*)event_h)->type !=                 \
                PERUSE_TYPE_ ## obj_upper)                            \
            return PERUSE_ERR_EVENT_HANDLE;                           \
                                                                      \
        if (NULL == callback_fn)                                      \
            return PERUSE_ERR_PARAMETER;                              \
        /*                                                            \
         * XXX whether the underlying MPI-object has been freed!??    \
        if (ompi_ ## obj_lower ## _invalid (                          \
              ((ompi_peruse_handle_t*)event_h)->obj_lower))           \
            return PERUSE_ERR_MPI_OBJECT;                             \
         */                                                           \
    }                                                                 \

/* Set a new comm callback */
int PERUSE_Event_comm_callback_set (peruse_event_h           event_h,
                                    peruse_comm_callback_f*  callback_fn,
                                    void*                    param)
{
    ompi_peruse_handle_t* handle = (ompi_peruse_handle_t*)event_h;

    PERUSE_MPI_PARAM_CHECK (COMM, comm);

    OPAL_THREAD_LOCK (&handle->lock);
    handle->fn = (ompi_peruse_callback_f*) callback_fn;
    handle->param = param;
    OPAL_THREAD_UNLOCK (&handle->lock);

    return PERUSE_SUCCESS;
}

/* Get the current comm callback */
int PERUSE_Event_comm_callback_get (peruse_event_h           event_h,
                                    peruse_comm_callback_f** callback_fn,
                                    void**                   param)
{
    ompi_peruse_handle_t* handle = (ompi_peruse_handle_t*)event_h;

    PERUSE_MPI_PARAM_CHECK (COMM, comm);

    OPAL_THREAD_LOCK (&handle->lock);
    *callback_fn = (peruse_comm_callback_f*) handle->fn;
    *param = handle->param;
    OPAL_THREAD_UNLOCK (&handle->lock);

    return PERUSE_SUCCESS;
}

/* Obtain event descriptor from an event handle (reverse lookup) */
int PERUSE_Event_get (peruse_event_h event_h, int* event)
{
    if (MPI_PARAM_CHECK) {
        OMPI_ERR_PERUSE_INIT_FINALIZE;

        if (NULL == event_h)
            return PERUSE_ERR_EVENT_HANDLE;

        if (NULL == event)
            return PERUSE_ERR_PARAMETER;
    }

    *event = ((ompi_peruse_handle_t*)event_h)->event;
    return PERUSE_SUCCESS;
}


/* Obtain MPI object associated with event handle */
int PERUSE_Event_object_get (peruse_event_h event_h, void** mpi_object)
{
    ompi_peruse_handle_t* p = (ompi_peruse_handle_t*)event_h;

    if (MPI_PARAM_CHECK) {
        OMPI_ERR_PERUSE_INIT_FINALIZE;

        if (NULL == event_h)
            return PERUSE_ERR_EVENT_HANDLE;

        if (NULL == mpi_object)
            return PERUSE_ERR_PARAMETER;
    }

    switch (p->type) {
        case PERUSE_TYPE_COMM:
            *mpi_object = p->comm;
            return PERUSE_SUCCESS;
    }
    return PERUSE_ERR_GENERIC;
}


/* Propagation mode */
int PERUSE_Event_propagate (peruse_event_h event_h, int mode)
{
    return PERUSE_SUCCESS;
}