File: mem_buffer.h

package info (click to toggle)
mpich 5.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 251,848 kB
  • sloc: ansic: 1,323,147; cpp: 82,869; f90: 72,420; javascript: 40,763; perl: 28,296; sh: 19,399; python: 16,191; xml: 14,418; makefile: 9,474; fortran: 8,046; java: 4,635; pascal: 352; asm: 324; ruby: 176; awk: 27; lisp: 19; php: 8; sed: 4
file content (167 lines) | stat: -rw-r--r-- 5,820 bytes parent folder | download | duplicates (3)
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
/**
 * Copyright (c) NVIDIA CORPORATION & AFFILIATES, 2001-2020. ALL RIGHTS RESERVED.
 *
 * See file LICENSE for terms.
 */

#ifndef GTEST_MEM_BUFFER_H_
#define GTEST_MEM_BUFFER_H_

#include <ucs/memory/memory_type.h>
#include <ucs/sys/math.h>
#include <stdint.h>
#include <limits>
#include <string>
#include <vector>


/**
 * Wrapper and utility functions for memory type buffers, e.g buffers which are
 * not necessarily allocated on host memory, such as cuda, rocm, etc.
 */
class mem_buffer {
public:
    static const size_t size_max = std::numeric_limits<size_t>::max();

    static const std::vector<ucs_memory_type_t>& supported_mem_types();

    static bool is_mem_type_supported(ucs_memory_type_t mem_type);

    /* allocate buffer of a given memory type */
    static void *
    allocate(size_t size, ucs_memory_type_t mem_type, bool async = false);
    /* release buffer of a given memory type */
    static void
    release(void *ptr, ucs_memory_type_t mem_type, bool async = false);

    /* fill pattern in a host-accessible buffer */
    static void pattern_fill(void *buffer, size_t length, uint64_t seed);

    /* check pattern in a host-accessible buffer */
    static void pattern_check(const void *buffer, size_t length, uint64_t seed,
                              const void *orig_ptr = NULL);

    /* check pattern in a host-accessible buffer, take seed from 1st word */
    static void pattern_check(const void *buffer, size_t length,
                              const void *orig_ptr = NULL);

    /* fill pattern in a memtype buffer */
    static void pattern_fill(void *buffer, size_t length, uint64_t seed,
                             ucs_memory_type_t mem_type);

    /* check pattern in a memtype buffer */
    static void pattern_check(const void *buffer, size_t length, uint64_t seed,
                              ucs_memory_type_t mem_type);

    /* set all buffer to a constant value */
    static void
    memset(void *buffer, size_t length, int c, ucs_memory_type_t mem_type);

    /* copy from host memory to memtype buffer */
    static void copy_to(void *dst, const void *src, size_t length,
                        ucs_memory_type_t dst_mem_type);

    /* copy from memtype buffer to host memory */
    static void copy_from(void *dst, const void *src, size_t length,
                          ucs_memory_type_t src_mem_type);

    /* copy between memtype buffers */
    static void copy_between(void *dst, const void *src, size_t length,
                             ucs_memory_type_t dst_mem_type,
                             ucs_memory_type_t src_mem_type);

    /* compare memtype buffer with host memory, return true if equal */
    static bool compare(const void *expected, const void *buffer,
                        size_t length, ucs_memory_type_t mem_type);

    /* compare when both expected data and buffer can be different mem types */
    static bool compare(const void *expected, const void *buffer,
                        size_t length, ucs_memory_type_t mem_type_expected,
                        ucs_memory_type_t mem_type_buffer);

    /* return the string name of a memory type */
    static std::string mem_type_name(ucs_memory_type_t mem_type);

    /* returns whether any other type of memory besides the CPU is supported */
    static bool is_gpu_supported();

    /* set device context if compiled with GPU support */
    static void set_device_context();

    /* returns whether ROCM device supports managed memory */
    static bool is_rocm_managed_supported();

    /* returns whether ROCM device supports hipMallocPitch */
    static bool is_rocm_malloc_pitch_supported();

    /* Get from NVML BAR1 free size */
    static void get_bar1_free_size_nvml();

    /* Return free memory on the BAR1 / GPU. If GPU is not used
     * SIZE_MAX is returned */
    static size_t get_bar1_free_size()
    {
        return m_bar1_free_size;
    }

    /**
     * Check whether asynchronous operations are supported for the memory type
     */
    static bool is_async_supported(ucs_memory_type_t mem_type);

    mem_buffer(size_t size, ucs_memory_type_t mem_type);
    mem_buffer(size_t size, ucs_memory_type_t mem_type, uint64_t seed);
    virtual ~mem_buffer();

    ucs_memory_type_t mem_type() const;

    void *ptr() const;

    size_t size() const;

    void pattern_fill(uint64_t seed, size_t length = size_max);

    void pattern_check(uint64_t seed, size_t length = size_max) const;

    void memset(int c);

private:
    static bool is_cuda_supported();

    static bool is_rocm_supported();

    static inline uint64_t pat(uint64_t prev)
    {
        /* LFSR pattern */
        static const uint64_t polynom = 1337;
        return (prev << 1) | (__builtin_parityl(prev & polynom));
    }

    static inline void pattern_check(uint64_t expected, uint64_t actual,
                                     size_t length, size_t offset,
                                     const void *buffer, const void *orig_ptr)
    {
        const uint64_t mask = UCS_MASK(length * 8 * sizeof(char));

        if (ucs_unlikely(actual != (expected & mask))) {
            pattern_check_failed(expected, actual, length, mask, offset,
                                 orig_ptr);
        }
    }

    static void pattern_check_failed(uint64_t expected, uint64_t actual,
                                     size_t length, uint64_t mask,
                                     size_t offset, const void *orig_ptr);
    static bool check_mem_types(ucs_memory_type_t dst_mem_type,
                                ucs_memory_type_t src_mem_type,
                                const uint64_t mem_types);

    static size_t           m_bar1_free_size;

    const ucs_memory_type_t m_mem_type;
    void * const            m_ptr;
    const size_t            m_size;
};


#endif