File: memheap_base_select.c

package info (click to toggle)
openmpi 5.0.8-3
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 201,692 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 (320 lines) | stat: -rw-r--r-- 9,990 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
/*
 * Copyright (c) 2013-2015 Mellanox Technologies, Inc.
 *                         All rights reserved.
 * Copyright (c) 2015-2019 Research Organization for Information Science
 *                         and Technology (RIST).  All rights reserved.
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 */

#include "oshmem_config.h"

#include "opal/util/argv.h"
#include "opal/util/show_help.h"
#include "oshmem/mca/mca.h"
#include "opal/mca/base/base.h"
#include "opal/mca/base/mca_base_component_repository.h"
#include "oshmem/info/info.h"
#include "oshmem/util/oshmem_util.h"
#include "oshmem/mca/memheap/memheap.h"
#include "oshmem/mca/memheap/base/base.h"
#include "oshmem/include/shmemx.h"
#include "oshmem/mca/sshmem/base/base.h"
#include "ompi/util/timings.h"

#include <sys/mman.h>

mca_memheap_base_config_t mca_memheap_base_config = {
    .device_nic_mem_seg_size = 0
};

mca_memheap_base_module_t mca_memheap = {0};

/**
 * Function for weeding out memheap components that shouldn't be executed.
 * Implementation inspired by btl/base.
 *
 * Call the init function on all available components to find out if
 * they want to run. Select all components that don't fail.  Failing
 * components will be closed and unloaded.  The selected modules will
 * be pointed to by mca_memheap_base_module_t.
 */

static memheap_context_t* _memheap_create(void);

/**
 * Choose to init one component with the highest priority.
 * If the include list if it is not empty choose a component that appear in the list.
 * O/W choose the highest priority component not in the exclude list.
 * Include and exclude lists may be given in the shmem launcher command line.
 */
int mca_memheap_base_select()
{
    int best_priority;
    memheap_context_t *context;
    mca_memheap_base_component_t *best_component = NULL;
    mca_memheap_base_module_t *best_module = NULL;

    OPAL_TIMING_ENV_INIT(timing);

    if( OPAL_SUCCESS != mca_base_select("memheap", oshmem_memheap_base_framework.framework_output,
                                        &oshmem_memheap_base_framework.framework_components,
                                        (mca_base_module_t **) &best_module,
                                        (mca_base_component_t **) &best_component,
                                        &best_priority) ) {
        return OSHMEM_ERROR;
    }

    OPAL_TIMING_ENV_NEXT(timing, "env");

    context = _memheap_create();
    if (NULL == context) {
        return OSHMEM_ERROR;
    }

    OPAL_TIMING_ENV_NEXT(timing, "_memheap_create()");

    if (OSHMEM_SUCCESS != best_component->memheap_init(context)) {
        opal_show_help("help-oshmem-memheap.txt",
                       "find-available:none-found",
                       true,
                       "memheap");
        return OSHMEM_ERROR;
    }

    OPAL_TIMING_ENV_NEXT(timing, "best_component->memheap_init()");

    /* Calculate memheap size in case it was not set during component initialization */
    best_module->memheap_size = context->user_size;
    setenv(SHMEM_HEAP_TYPE,
           best_component->memheap_version.mca_component_name, 1);

    mca_memheap = *best_module;

    MEMHEAP_VERBOSE(10,
                    "SELECTED %s component %s",
                    best_component->memheap_version.mca_type_name, 
                    best_component->memheap_version.mca_component_name);

    OPAL_TIMING_ENV_NEXT(timing, "DONE");
    return OSHMEM_SUCCESS;
}

static size_t _memheap_size(void)
{
    return (size_t) memheap_align(oshmem_shmem_info_env.symmetric_heap_size);
}

static void *memheap_mmap_get(void *hint, size_t size)
{
    void *addr;

    addr = mmap(hint, size,
                PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    if (addr == MAP_FAILED) {
        return NULL;
    }

    return addr;
}

static int memheap_exchange_base_address(size_t size, void **address)
{
    int nprocs = oshmem_num_procs();
    int need_sync = (*address == NULL);
    void *base = NULL;
    void *ptr = NULL;
    int rc, i;
    void **bases;

    bases = calloc(nprocs, sizeof(*bases));
    if (NULL == bases) {
        return OSHMEM_ERROR;
    }

    if (oshmem_my_proc_id() == 0) {
        ptr = memheap_mmap_get(NULL, size);
        base = ptr;
    }

    rc = oshmem_shmem_bcast(&base, sizeof(base), 0);
    if (OSHMEM_SUCCESS != rc) {
        MEMHEAP_ERROR("Failed to exchange allocated vma for base segment "
                      "(error %d)", rc);
        goto out;
    }

    if (oshmem_my_proc_id() != 0) {
        ptr = memheap_mmap_get(base, size);
    }

    MEMHEAP_VERBOSE(100, "#%d: exchange base address: base %p: %s",
                    oshmem_my_proc_id(), base,
                    (base == ptr)? "ok" : "unavailable");

    *address = base;
    if (need_sync) {
        /* They all succeed or fail to allow fallback */
        rc = oshmem_shmem_allgather(&ptr, bases, sizeof(ptr));
        if (OSHMEM_SUCCESS != rc) {
            MEMHEAP_ERROR("Failed to exchange selected vma for base segment "
                          "(error %d)", rc);
            goto out;
        }

        for (i = 0; i < nprocs; i++) {
            if ((NULL == bases[i]) || (bases[i] != base)) {
                *address = NULL;
                break;
            }
        }
    } else if (ptr != base) {
        /* Any failure terminates the rank and others start teardown */
        rc = OSHMEM_ERROR;
    }

out:
    if (((OSHMEM_SUCCESS != rc) || (*address == NULL)) && (ptr != NULL)) {
        (void)munmap(ptr, size);
    }

    free(bases);
    return rc;
}


/*
 * The returned mca_sshmem_base_start_address value is reserved by using
 * mmap() for the expected size.
 */
static int memheap_base_segment_setup(size_t size)
{
    int rc;

    if ((mca_sshmem_base_start_address == (void *)UINTPTR_MAX) ||
        (mca_sshmem_base_start_address == NULL)) {
        if (UINTPTR_MAX == 0xFFFFFFFF) {
            /**
             * if 32 bit we set sshmem_base_start_adress to 0
             * to let OS allocate segment automatically
             */
            mca_sshmem_base_start_address = NULL;
            return OSHMEM_SUCCESS;
        }

        rc = memheap_exchange_base_address(size, &mca_sshmem_base_start_address);
        if (OSHMEM_SUCCESS != rc) {
            MEMHEAP_ERROR("Failed to setup base segment address (error %d)", rc);
            return rc;
        }

        if (NULL != mca_sshmem_base_start_address) {
            goto done; /* Region is reserved */
        }

#if defined(__aarch64__)
        mca_sshmem_base_start_address = (void*)0xAB0000000000;
#else
        mca_sshmem_base_start_address = (void*)0xFF000000;
#endif
    }

    if (mca_sshmem_base_start_address != memheap_mmap_get(
                                      mca_sshmem_base_start_address, size)) {
        MEMHEAP_ERROR("Failed to create segment address %p/%zu",
                      mca_sshmem_base_start_address, size);
        return OSHMEM_ERROR;
    }

done:
    if (oshmem_my_proc_id() == 0) {
        MEMHEAP_VERBOSE(10, "Using symmetric segment address %p/%zu",
                        mca_sshmem_base_start_address, size);
    }

    return OSHMEM_SUCCESS;
}

static memheap_context_t* _memheap_create(void)
{
    int rc = OSHMEM_SUCCESS;
    static memheap_context_t context;
    size_t user_size, size;

    OPAL_TIMING_ENV_INIT(timing);

    user_size = _memheap_size();
    if (user_size < MEMHEAP_BASE_MIN_SIZE) {
        MEMHEAP_ERROR("Requested memheap size is less than minimal meamheap size (%llu < %llu)",
                      (unsigned long long)user_size, MEMHEAP_BASE_MIN_SIZE);
        return NULL ;
    }

    OPAL_TIMING_ENV_NEXT(timing, "_memheap_size()");

    /* Locate and reserve symmetric area */
    rc = memheap_base_segment_setup(user_size + MEMHEAP_BASE_PRIVATE_SIZE);
    if (OSHMEM_SUCCESS != rc) {
        MEMHEAP_ERROR("Failed to negotiate base segment addres");
        return NULL;
    }

    /* Initialize symmetric area */
    rc = mca_memheap_base_alloc_init(&mca_memheap_base_map,
                                     user_size + MEMHEAP_BASE_PRIVATE_SIZE, 0,
                                     "regular_mem");

    OPAL_TIMING_ENV_NEXT(timing, "mca_memheap_base_alloc_init()");

    /* Initialize atomic symmetric area */
    size = mca_memheap_base_config.device_nic_mem_seg_size;
    if ((OSHMEM_SUCCESS == rc) && (size > 0)) {
        rc = mca_memheap_base_alloc_init(&mca_memheap_base_map, size,
                                         SHMEM_HINT_DEVICE_NIC_MEM,
                                         "device_mem");
        if (rc == OSHMEM_ERR_NOT_IMPLEMENTED) {
            /* do not treat NOT_IMPLEMENTED as error */
            rc = OSHMEM_SUCCESS;
        }
    }

    OPAL_TIMING_ENV_NEXT(timing, "mca_memheap_base_alloc_init(DEVICE_MEM)");


    /* Inititialize static/global variables area */
    if (OSHMEM_SUCCESS == rc) {
        rc = mca_memheap_base_static_init(&mca_memheap_base_map);
    }

    OPAL_TIMING_ENV_NEXT(timing, "mca_memheap_base_static_init()");

    /* Memory Registration */
    if (OSHMEM_SUCCESS == rc) {
        rc = mca_memheap_base_reg(&mca_memheap_base_map);
    }

    OPAL_TIMING_ENV_NEXT(timing, "mca_memheap_base_reg()");

    /* Init OOB channel */
    if (OSHMEM_SUCCESS == rc) {
        rc = memheap_oob_init(&mca_memheap_base_map);
    }
    OPAL_TIMING_ENV_NEXT(timing, "memheap_oob_init()");

    if (OSHMEM_SUCCESS == rc) {
        context.user_size = user_size;
        context.private_size = MEMHEAP_BASE_PRIVATE_SIZE;
        context.user_base_addr =
                (void*) ((unsigned char*) mca_memheap_base_map.mem_segs[HEAP_SEG_INDEX].super.va_base
                        + 0);
        context.private_base_addr =
                (void*) ((unsigned char*) mca_memheap_base_map.mem_segs[HEAP_SEG_INDEX].super.va_base
                        + context.user_size);
    }
    OPAL_TIMING_ENV_NEXT(timing, "DONE");

    return ((OSHMEM_SUCCESS == rc) ? &context : NULL );
}