File: btl_ugni_endpoint.c

package info (click to toggle)
openmpi 5.0.8-4
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 201,684 kB
  • sloc: ansic: 613,078; makefile: 42,353; sh: 11,194; javascript: 9,244; f90: 7,052; java: 6,404; perl: 5,179; python: 1,859; lex: 740; fortran: 61; cpp: 20; tcl: 12
file content (423 lines) | stat: -rw-r--r-- 14,375 bytes parent folder | download | duplicates (4)
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
/*
 * Copyright (c) 2011-2018 Los Alamos National Security, LLC. All rights
 *                         reserved.
 * Copyright (c) 2011-2013 UT-Battelle, LLC. All rights reserved.
 * Copyright (c) 2017-2019 Intel, Inc.  All rights reserved.
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 */

#include "btl_ugni_endpoint.h"
#include "btl_ugni_smsg.h"
#include "opal/mca/pmix/pmix-internal.h"

static void mca_btl_ugni_ep_construct(mca_btl_base_endpoint_t *ep)
{
    memset((char *) ep + sizeof(ep->super), 0, sizeof(*ep) - sizeof(ep->super));
    OBJ_CONSTRUCT(&ep->frag_wait_list, opal_list_t);
    OBJ_CONSTRUCT(&ep->lock, opal_recursive_mutex_t);
}

static void mca_btl_ugni_ep_destruct(mca_btl_base_endpoint_t *ep)
{
    OBJ_DESTRUCT(&ep->frag_wait_list);
    OBJ_DESTRUCT(&ep->lock);
    free(ep->remote_attr);
}

OBJ_CLASS_INSTANCE(mca_btl_ugni_endpoint_t, opal_list_item_t, mca_btl_ugni_ep_construct,
                   mca_btl_ugni_ep_destruct);

static int mca_btl_ugni_endpoint_get_modex(mca_btl_base_endpoint_t *ep)
{
    mca_btl_ugni_modex_t *modex;
    size_t msg_size;
    int rc;

    assert(NULL != ep && NULL != ep->peer_proc);

    /* Receive the modex */
    OPAL_MODEX_RECV(rc, &mca_btl_ugni_component.super.btl_version, &ep->peer_proc->proc_name,
                    (void **) &modex, &msg_size);
    if (OPAL_UNLIKELY(OPAL_SUCCESS != rc)) {
        BTL_ERROR(("error receiving modex"));
        return rc;
    }

    ep->ep_rem_addr = modex->addr;
    ep->ep_rem_id = modex->id;

    BTL_VERBOSE(("received modex for ep %p. addr: %d, id: %d", (void *) ep, ep->ep_rem_addr,
                 ep->ep_rem_id));

    free(modex);

    return OPAL_SUCCESS;
}

int mca_btl_ugni_init_ep(mca_btl_ugni_module_t *ugni_module, mca_btl_ugni_endpoint_t **ep,
                         mca_btl_ugni_module_t *btl, opal_proc_t *peer_proc)
{
    mca_btl_ugni_endpoint_t *endpoint;
    int rc;

    endpoint = OBJ_NEW(mca_btl_ugni_endpoint_t);
    assert(endpoint != NULL);

    endpoint->smsg_progressing = 0;
    endpoint->state = MCA_BTL_UGNI_EP_STATE_INIT;
    endpoint->peer_proc = peer_proc;

    /* get the modex info for this endpoint and setup a ugni endpoint. this call may lead
     * to re-entry through opal_progress(). */
    rc = mca_btl_ugni_endpoint_get_modex(endpoint);
    if (OPAL_SUCCESS != rc) {
        assert(0);
        return rc;
    }

    /* add this endpoint to the pointer array */
    endpoint->index = opal_pointer_array_add(&ugni_module->endpoints, endpoint);

    *ep = endpoint;

    return OPAL_SUCCESS;
}

void mca_btl_ugni_release_ep(mca_btl_ugni_endpoint_t *ep)
{
    mca_btl_ugni_module_t *ugni_module = mca_btl_ugni_ep_btl(ep);
    int rc;

    opal_mutex_lock(&ep->lock);

    rc = mca_btl_ugni_ep_disconnect(ep, false);
    if (OPAL_UNLIKELY(OPAL_SUCCESS != rc)) {
        BTL_VERBOSE(("btl/ugni error disconnecting endpoint"));
    }

    /* TODO -- Clear space at the end of the endpoint array */
    opal_pointer_array_set_item(&ugni_module->endpoints, ep->index, NULL);

    opal_mutex_unlock(&ep->lock);

    OBJ_RELEASE(ep);
}

static inline int mca_btl_ugni_ep_smsg_get_mbox(mca_btl_base_endpoint_t *ep)
{
    mca_btl_ugni_module_t *ugni_module = mca_btl_ugni_ep_btl(ep);
    opal_free_list_item_t *mbox;

    assert(NULL == ep->mailbox);

    mbox = opal_free_list_get(&ugni_module->smsg_mboxes);
    if (OPAL_UNLIKELY(NULL == mbox)) {
        return OPAL_ERR_OUT_OF_RESOURCE;
    }

    ep->mailbox = (mca_btl_ugni_smsg_mbox_t *) mbox;
    ep->mailbox->attr.index = ep->index;

    /* per ugni spec we need to zero mailbox data before connecting */
    memset((char *) ep->mailbox->attr.smsg_attr.msg_buffer
               + ep->mailbox->attr.smsg_attr.mbox_offset,
           0, ep->mailbox->attr.smsg_attr.buff_size);
    return OPAL_SUCCESS;
}

static int mca_btl_ugni_ep_send_disconnect(mca_btl_base_endpoint_t *ep)
{
    int rc;

    do {
        rc = mca_btl_ugni_endpoint_smsg_send_wtag(ep, NULL, 0, NULL, 0, -1,
                                                  MCA_BTL_UGNI_TAG_DISCONNECT);
        if (OPAL_LIKELY(GNI_RC_NOT_DONE != rc)) {
            break;
        }

        /* most likely got here because we are out of credits. check the remote CQ to get credit
         * return */
        (void) mca_btl_ugni_progress_remote_smsg(mca_btl_ugni_ep_btl(ep));
    } while (1);

    return mca_btl_rc_ugni_to_opal(rc);
}

int mca_btl_ugni_ep_disconnect(mca_btl_base_endpoint_t *ep, bool send_disconnect)
{
    mca_btl_ugni_module_t *ugni_module = mca_btl_ugni_ep_btl(ep);
    mca_btl_ugni_device_t *device;
    int rc;

    if (MCA_BTL_UGNI_EP_STATE_INIT == ep->state) {
        /* nothing to do */
        return OPAL_SUCCESS;
    }

    device = ep->smsg_ep_handle.device;

    while (device->dev_smsg_local_cq.active_operations) {
        /* ensure all sends are complete before removing and procs */
        rc = mca_btl_ugni_progress_local_smsg(ugni_module, device);
        if (OPAL_SUCCESS != rc) {
            break;
        }
    }

    if (MCA_BTL_UGNI_EP_STATE_CONNECTED == ep->state && send_disconnect) {
        rc = mca_btl_ugni_ep_send_disconnect(ep);
        if (OPAL_UNLIKELY(OPAL_SUCCESS != rc)) {
            BTL_VERBOSE(("could not send disconnect message to peer"));
        }

        /* wait for the disconnect messagse to go */
        do {
            /* ensure all sends are complete before removing and procs */
            rc = mca_btl_ugni_progress_local_smsg(ugni_module, device);
            if (OPAL_SUCCESS != rc) {
                break;
            }
        } while (device->dev_smsg_local_cq.active_operations);

        (void) opal_atomic_add_fetch_32(&ep->smsg_ep_handle.device->smsg_connections, -1);
    }

    mca_btl_ugni_device_lock(device);

    /* NTH: this call may not need the device lock. seems to work without it but
     * the lock is here to be safe. */
    (void) mca_btl_ugni_ep_handle_cleanup(&ep->smsg_ep_handle);

    mca_btl_ugni_device_unlock(device);

    if (ep->mailbox) {
        opal_free_list_return(&ugni_module->smsg_mboxes, ((opal_free_list_item_t *) ep->mailbox));
        ep->mailbox = NULL;
    }

    ep->state = MCA_BTL_UGNI_EP_STATE_INIT;

    return OPAL_SUCCESS;
}

static inline int mca_btl_ugni_ep_connect_start(mca_btl_base_endpoint_t *ep)
{
    mca_btl_ugni_module_t *ugni_module = mca_btl_ugni_ep_btl(ep);
    mca_btl_ugni_device_t *device = ugni_module->devices;
    int rc;

    /* protect against re-entry from opal_progress */
    if (OPAL_UNLIKELY(MCA_BTL_UGNI_EP_STATE_CONNECTING == ep->state)) {
        return OPAL_ERR_RESOURCE_BUSY;
    }

    ep->state = MCA_BTL_UGNI_EP_STATE_CONNECTING;

    BTL_VERBOSE(("initiating connection to remote peer with address: %u id: %u proc: %p",
                 ep->ep_rem_addr, ep->ep_rem_id, (void *) ep->peer_proc));

    /* bind endpoint to remote address */
    /* we bind two endpoints to separate out local smsg completion and local fma completion */
    mca_btl_ugni_device_lock(device);
    rc = mca_btl_ugni_ep_handle_init(ep, device->dev_smsg_local_cq.gni_handle, device,
                                     &ep->smsg_ep_handle);
    mca_btl_ugni_device_unlock(device);
    if (OPAL_UNLIKELY(OPAL_SUCCESS != rc)) {
        return rc;
    }

    /* build connection data */
    rc = mca_btl_ugni_ep_smsg_get_mbox(ep);
    if (OPAL_UNLIKELY(OPAL_SUCCESS != rc)) {
        return rc;
    }

    ep->remote_attr = calloc(1, sizeof(*ep->remote_attr));
    if (OPAL_UNLIKELY(NULL == ep->remote_attr)) {
        return OPAL_ERR_OUT_OF_RESOURCE;
    }

    BTL_VERBOSE(("btl/ugni connection to remote peer initiated"));

    return OPAL_SUCCESS;
}

static inline int mca_btl_ugni_ep_connect_finish(mca_btl_base_endpoint_t *ep)
{
    mca_btl_ugni_module_t *ugni_module = mca_btl_ugni_ep_btl(ep);
    gni_return_t grc;
    int rc;

    BTL_VERBOSE(
        ("finishing connection. remote attributes: msg_type = %d, msg_buffer = %p, buff_size = %d, "
         "mem_hndl = {qword1 = %" PRIu64 ", qword2 = %" PRIu64 "}, mbox = %d, mbox_maxcredit = %d, "
         "msg_maxsize = %d",
         ep->remote_attr->smsg_attr.msg_type, ep->remote_attr->smsg_attr.msg_buffer,
         ep->remote_attr->smsg_attr.buff_size, ep->remote_attr->smsg_attr.mem_hndl.qword1,
         ep->remote_attr->smsg_attr.mem_hndl.qword2, ep->remote_attr->smsg_attr.mbox_offset,
         ep->remote_attr->smsg_attr.mbox_maxcredit, ep->remote_attr->smsg_attr.msg_maxsize));

    BTL_VERBOSE(
        ("finishing connection. local attributes: msg_type = %d, msg_buffer = %p, buff_size = %d, "
         "mem_hndl = {qword1 = %" PRIu64 ", qword2 = %" PRIu64 "}, mbox = %d, mbox_maxcredit = %d, "
         "msg_maxsize = %d",
         ep->mailbox->attr.smsg_attr.msg_type, ep->mailbox->attr.smsg_attr.msg_buffer,
         ep->mailbox->attr.smsg_attr.buff_size, ep->mailbox->attr.smsg_attr.mem_hndl.qword1,
         ep->mailbox->attr.smsg_attr.mem_hndl.qword2, ep->mailbox->attr.smsg_attr.mbox_offset,
         ep->mailbox->attr.smsg_attr.mbox_maxcredit, ep->mailbox->attr.smsg_attr.msg_maxsize));

    grc = GNI_SmsgInit(ep->smsg_ep_handle.gni_handle, &ep->mailbox->attr.smsg_attr,
                       &ep->remote_attr->smsg_attr);
    if (OPAL_UNLIKELY(GNI_RC_SUCCESS != grc)) {
        BTL_ERROR(("error initializing SMSG protocol. rc = %d", grc));

        return mca_btl_rc_ugni_to_opal(grc);
    }

    /* set the local event data to the local index and the remote event data to my
     * index on the remote peer. This makes lookup of endpoints on completion take
     * a single lookup in the endpoints array. we will not be able to change the
     * remote peer's index in the endpoint's array after this point. */
    GNI_EpSetEventData(ep->smsg_ep_handle.gni_handle, ep->index, ep->remote_attr->index);

    ep->rmt_irq_mem_hndl = ep->remote_attr->rmt_irq_mem_hndl;
    ep->state = MCA_BTL_UGNI_EP_STATE_CONNECTED;
    (void) opal_atomic_add_fetch_32(&ep->smsg_ep_handle.device->smsg_connections, 1);

    /* send all pending messages */
    BTL_VERBOSE(("endpoint connected. posting %u sends",
                 (unsigned int) opal_list_get_size(&ep->frag_wait_list)));

    rc = mca_btl_ugni_progress_send_wait_list(ep);
    if (OPAL_UNLIKELY(OPAL_SUCCESS != rc)) {
        OPAL_THREAD_LOCK(&ugni_module->ep_wait_list_lock);
        if (false == ep->wait_listed) {
            opal_list_append(&ugni_module->ep_wait_list, &ep->super);
            ep->wait_listed = true;
        }
        OPAL_THREAD_UNLOCK(&ugni_module->ep_wait_list_lock);
    }

    free(ep->remote_attr);
    ep->remote_attr = NULL;

    return OPAL_SUCCESS;
}

static int mca_btl_ugni_directed_ep_post(mca_btl_base_endpoint_t *ep)
{
    mca_btl_ugni_module_t *ugni_module = mca_btl_ugni_ep_btl(ep);
    gni_return_t rc;

    BTL_VERBOSE(
        ("posting directed datagram to remote id: %d for endpoint %p", ep->ep_rem_id, (void *) ep));
    /* the irq cq is associated with only the first device */
    ep->mailbox->attr.rmt_irq_mem_hndl = ugni_module->devices->smsg_irq_mhndl;

    rc = GNI_EpPostDataWId(ep->smsg_ep_handle.gni_handle, &ep->mailbox->attr,
                           sizeof(ep->mailbox->attr), ep->remote_attr, sizeof(*ep->remote_attr),
                           MCA_BTL_UGNI_CONNECT_DIRECTED_ID | ep->index);
    if (OPAL_LIKELY(GNI_RC_SUCCESS == rc)) {
        (void) opal_atomic_add_fetch_32(&ugni_module->active_datagrams, 1);
    }

    return mca_btl_rc_ugni_to_opal(rc);
}

int mca_btl_ugni_wildcard_ep_post(mca_btl_ugni_module_t *ugni_module)
{
    gni_return_t rc;

    BTL_VERBOSE(("posting wildcard datagram"));

    memset(&ugni_module->wc_local_attr, 0, sizeof(ugni_module->wc_local_attr));
    memset(&ugni_module->wc_remote_attr, 0, sizeof(ugni_module->wc_remote_attr));
    rc = GNI_EpPostDataWId(ugni_module->wildcard_ep, &ugni_module->wc_local_attr,
                           sizeof(ugni_module->wc_local_attr), &ugni_module->wc_remote_attr,
                           sizeof(ugni_module->wc_remote_attr), MCA_BTL_UGNI_CONNECT_WILDCARD_ID);

    return mca_btl_rc_ugni_to_opal(rc);
}

int mca_btl_ugni_ep_connect_progress(mca_btl_base_endpoint_t *ep)
{
    int rc;

    BTL_VERBOSE(("progressing connection for endpoint %p with state %d", (void *) ep, ep->state));

    if (MCA_BTL_UGNI_EP_STATE_CONNECTED == ep->state) {
        return OPAL_SUCCESS;
    }

    if (MCA_BTL_UGNI_EP_STATE_INIT == ep->state) {
        rc = mca_btl_ugni_ep_connect_start(ep);
        if (OPAL_SUCCESS != rc) {
            return rc;
        }
    }

    BTL_VERBOSE(("ep->remote_attr->smsg_attr = {.msg_type = %d, .msg_buffer = %p}",
                 ep->remote_attr->smsg_attr.msg_type,
                 (void *) ep->remote_attr->smsg_attr.msg_buffer));

    if (GNI_SMSG_TYPE_INVALID == ep->remote_attr->smsg_attr.msg_type) {
        /* use datagram to exchange connection information with the remote peer */
        if (!ep->dg_posted) {
            rc = mca_btl_ugni_directed_ep_post(ep);
            if (OPAL_SUCCESS == rc) {
                ep->dg_posted = true;
                rc = OPAL_ERR_RESOURCE_BUSY;
            }

            return rc;
        }

        return OPAL_SUCCESS;
    }

    return mca_btl_ugni_ep_connect_finish(ep);
}

int mca_btl_ugni_ep_handle_init(mca_btl_ugni_endpoint_t *ep, gni_cq_handle_t cq,
                                mca_btl_ugni_device_t *device,
                                mca_btl_ugni_endpoint_handle_t *ep_handle)
{
    gni_return_t grc;

    ep_handle->device = device;

    /* create a uGNI endpoint handle and bind it to the remote peer */
    grc = GNI_EpCreate(device->dev_handle, cq, &ep_handle->gni_handle);
    if (OPAL_LIKELY(GNI_RC_SUCCESS == grc)) {
        grc = GNI_EpBind(ep_handle->gni_handle, ep->ep_rem_addr, ep->ep_rem_id);
    }

    return mca_btl_rc_ugni_to_opal(grc);
}

int mca_btl_ugni_ep_handle_cleanup(mca_btl_ugni_endpoint_handle_t *ep_handle)
{
    int rc;

    if (0 == ep_handle->gni_handle) {
        return OPAL_SUCCESS;
    }

    /* TODO: need to fix, may be outstanding tx's, etc. */
    rc = GNI_EpUnbind(ep_handle->gni_handle);
    if (OPAL_UNLIKELY(GNI_RC_SUCCESS != rc)) {
        /* should warn */
    } else {
        (void) GNI_EpDestroy(ep_handle->gni_handle);
    }

    ep_handle->gni_handle = 0;

    return OPAL_SUCCESS;
}