File: mpool_memkind.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 (171 lines) | stat: -rw-r--r-- 4,619 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
/*
 * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
 *                         University Research and Technology
 *                         Corporation.  All rights reserved.
 * Copyright (c) 2004-2005 The University of Tennessee and The University
 *                         of Tennessee Research Foundation.  All rights
 *                         reserved.
 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
 *                         University of Stuttgart.  All rights reserved.
 * Copyright (c) 2004-2005 The Regents of the University of California.
 *                         All rights reserved.
 * Copyright (c) 2016      Research Organization for Information Science
 *                         and Technology (RIST). All rights reserved.
 * Copyright (c) 2018      Los Alamos National Security, LLC.  All rights reserved.
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 */

/*
 * only do this test if we have built with memkind support
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "opal_config.h"
#ifdef HAVE_MEMKIND_H
#include "opal/constants.h"
#include "opal/align.h"
#include "opal/mca/mpool/mpool.h"
#include "opal/include/opal/frameworks.h"
#include "opal/runtime/opal.h"

#define SIZE (2 * 1024 * 1024)

const char *memory_types[] = {
  "memkind_default",
  "memkind_hbw",
   NULL
};

const char *memory_policy[] = {
   "mempolicy_bind_local",
   "mempolicy_bind_all",
   "mempolicy_perferred_local",
   "mempolicy_interleave_local",
   "mempolicy_interleave_all",
   NULL
};

const char *memory_kind_bits[] = {
   "memkind_mask_page_size_4KB",
   "memkind_mask_page_size_2MB",
   NULL
};

int main (int argc, char* argv[])
{
    int ret = 0;
    void *ptr = NULL;
    char *error = NULL;
    char **mp_ptr = NULL;
    char **mt_ptr = NULL;
    char **mk_ptr = NULL;
    const char mpool_hints[] = "mpool=memkind";
    char hints[1024];

    opal_init_util(&argc, &argv);

    if (opal_frameworks == NULL){
        error = "opal frameworks is NULL";
        goto error;
    }

    if (OPAL_SUCCESS != (ret = mca_base_framework_open(&opal_allocator_base_framework, 0))) {
        error = "mca_allocator_base_open() failed";
        goto error;
    }

    if (OPAL_SUCCESS != (ret = mca_base_framework_open(&opal_mpool_base_framework, 0))) {
        error = "mca_mpool_base_open() failed";
        goto error;
    }

    /*
     * first try basic allocation
     */

    ptr = mca_mpool_base_alloc(SIZE, NULL, mpool_hints);
    if (NULL == ptr) {
        error = "mca_mpool_base_alloc() failed";
        goto error;
    }

    if (OPAL_SUCCESS != mca_mpool_base_free(ptr)) {
        error = "mca_mpool_base_free() failed";
        goto error;
    }

    if (0 != ((uintptr_t)ptr % OPAL_ALIGN_MIN)) {
        error = "improper memory alignment detected";
        goto error;
    }

    /*
     * now try policies
     */

    mp_ptr = (char **)memory_policy;
    while (NULL != *mp_ptr) {

        mt_ptr = (char **)memory_types;
        while (NULL != *mt_ptr) {

            mk_ptr = (char **)memory_kind_bits;
            while (NULL != *mk_ptr) {
                snprintf(hints, sizeof(hints), "%s,policy=%s,type=%s,kind=%s", 
                         mpool_hints, *mp_ptr, *mt_ptr, *mk_ptr);
                ptr = mca_mpool_base_alloc(SIZE, NULL, hints);
                if (NULL == ptr) {
                    error = "mca_mpool_base_alloc() failed";
                    goto error;
                }

                if (OPAL_SUCCESS != mca_mpool_base_free(ptr)) {
                    error = "mca_mpool_base_free() failed";
                    goto error;
                }

                if (0 != ((uintptr_t)ptr % OPAL_ALIGN_MIN)) {
                    error = "improper memory alignment detected";
                    goto error;
                }
                mk_ptr++;
            }
            mt_ptr++;
        }
        mp_ptr++;
    }

    if (OPAL_SUCCESS != (ret = mca_base_framework_close(&opal_mpool_base_framework))) {
        error = "mca_mpool_base_close() failed";
        goto error;
    }

    if (OPAL_SUCCESS != (ret = mca_base_framework_close(&opal_allocator_base_framework))) {
        error = "mca_mpool_base_close() failed";
        goto error;
    }

    opal_finalize();

error:
    if (NULL != error) {
        fprintf(stderr, "mpool/memkind test failed %s\n", error);
        ret = -1;
    } else {
        fprintf(stderr, "mpool/memkind test passed\n");
    }

    return ret;
}
#else
int main (int argc, char* argv[])
{
    return 77;
}
#endif /* HAVE_MEMKIND_H */