File: rocsparse_clients_envariables.cpp

package info (click to toggle)
rocsparse 6.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 20,408 kB
  • sloc: cpp: 234,069; f90: 9,307; sh: 2,262; python: 1,939; makefile: 1,587; ansic: 440; xml: 26
file content (357 lines) | stat: -rw-r--r-- 12,906 bytes parent folder | download | duplicates (2)
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
/* ************************************************************************
 * Copyright (C) 2023 Advanced Micro Devices, Inc. All rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * ************************************************************************ */

#include "rocsparse_clients_envariables.hpp"
#include "rocsparse-types.h"
#include <iostream>
#include <mutex>
constexpr rocsparse_clients_envariables::var_bool rocsparse_clients_envariables::s_var_bool_all[];
constexpr rocsparse_clients_envariables::var_string
    rocsparse_clients_envariables::s_var_string_all[];

template <std::size_t N, typename T>
static inline constexpr std::size_t countof(T (&)[N])
{
    return N;
}

static constexpr size_t s_var_bool_size = countof(rocsparse_clients_envariables::s_var_bool_all);
static constexpr size_t s_var_string_size
    = countof(rocsparse_clients_envariables::s_var_string_all);

static constexpr const char* s_var_bool_names[s_var_bool_size]
    = {"ROCSPARSE_CLIENTS_VERBOSE", "ROCSPARSE_CLIENTS_TEST_DEBUG_ARGUMENTS"};
static constexpr const char* s_var_string_names[s_var_string_size]
    = {"ROCSPARSE_CLIENTS_MATRICES_DIR", "ROCSPARSE_CLIENTS_TEST_DATA_DIR"};
static constexpr const char* s_var_bool_descriptions[s_var_bool_size]
    = {"0: disabled, 1: enabled", "0: disabled, 1: enabled"};
static constexpr const char* s_var_string_descriptions[s_var_string_size]
    = {"Full path of the matrices directory", "The path where the test data file is located"};

///
/// @brief Grab an environment variable value.
/// @return true if the operation is successful, false otherwise.
///
template <typename T>
static bool rocsparse_getenv(const char* name, bool& defined, T& val);

template <>
bool rocsparse_getenv<bool>(const char* name, bool& defined, bool& val)
{
    val                    = false;
    const char* getenv_str = getenv(name);
    defined                = (getenv_str != nullptr);
    if(defined)
    {
        auto getenv_int = atoi(getenv_str);
        if((getenv_int != 0) && (getenv_int != 1))
        {
            std::cerr << "rocsparse error, invalid environment variable " << name
                      << " must be 0 or 1." << std::endl;
            val = false;
            return false;
        }
        else
        {
            val = (getenv_int == 1);
            return true;
        }
    }
    else
    {
        return true;
    }
}

template <>
bool rocsparse_getenv<std::string>(const char* name, bool& defined, std::string& val)
{
    const char* getenv_str = getenv(name);
    defined                = (getenv_str != nullptr);
    if(defined)
    {
        val = getenv_str;
    }
    return true;
}

struct rocsparse_clients_envariables_impl
{
    static std::mutex s_mutex;

public:
    //
    // \brief Return value of a Boolean variable.
    //
    inline bool get(rocsparse_clients_envariables::var_bool v) const
    {
        return this->m_var_bool[v];
    };

    //
    // \brief Return value of a string variable.
    //
    inline const char* get(rocsparse_clients_envariables::var_string v) const
    {
        return this->m_var_string[v].c_str();
    };

    //
    // \brief Set value of a Boolean variable.
    //
    inline void set(rocsparse_clients_envariables::var_bool v, bool value)
    {

        rocsparse_clients_envariables_impl::s_mutex.lock();
        this->m_var_bool[v] = value;
        rocsparse_clients_envariables_impl::s_mutex.unlock();
    };

    inline void set(rocsparse_clients_envariables::var_string v, const char* value)
    {
        rocsparse_clients_envariables_impl::s_mutex.lock();
        this->m_var_string[v] = value;
        rocsparse_clients_envariables_impl::s_mutex.unlock();
    };

    //
    // \brief Return value of a string variable.
    //
    //
    // \brief Is a Boolean variable defined ?
    //
    inline bool is_defined(rocsparse_clients_envariables::var_bool v) const
    {
        return this->m_var_bool_defined[v];
    };

    //
    // \brief Is a string variable defined ?
    //
    inline bool is_defined(rocsparse_clients_envariables::var_string v) const
    {
        return this->m_var_string_defined[v];
    };

    //
    // Return the unique instance.
    //
    static rocsparse_clients_envariables_impl& Instance();

private:
    ~rocsparse_clients_envariables_impl()                                         = default;
    rocsparse_clients_envariables_impl(const rocsparse_clients_envariables_impl&) = delete;
    rocsparse_clients_envariables_impl& operator=(const rocsparse_clients_envariables_impl&)
        = delete;

    bool m_var_bool[s_var_bool_size]{};
    bool m_var_bool_defined[s_var_bool_size]{};

    std::string m_var_string[s_var_string_size]{};
    bool        m_var_string_defined[s_var_string_size]{};

    rocsparse_clients_envariables_impl()
    {
        for(auto tag : rocsparse_clients_envariables::s_var_bool_all)
        {
            switch(tag)
            {
            case rocsparse_clients_envariables::VERBOSE:
            {
                const bool success = rocsparse_getenv(
                    s_var_bool_names[tag], this->m_var_bool_defined[tag], this->m_var_bool[tag]);
                if(!success)
                {
                    std::cerr << "rocsparse_getenv failed on fetching " << s_var_bool_names[tag]
                              << std::endl;
                    throw(rocsparse_status_invalid_value);
                }
                break;
            }
            case rocsparse_clients_envariables::TEST_DEBUG_ARGUMENTS:
            {
                const bool success = rocsparse_getenv(
                    s_var_bool_names[tag], this->m_var_bool_defined[tag], this->m_var_bool[tag]);
                if(!success)
                {
                    std::cerr << "rocsparse_getenv failed on fetching " << s_var_bool_names[tag]
                              << std::endl;
                    throw(rocsparse_status_invalid_value);
                }
                break;
            }
            }
        }

        for(auto tag : rocsparse_clients_envariables::s_var_string_all)
        {
            switch(tag)
            {
            case rocsparse_clients_envariables::MATRICES_DIR:
            {
                const bool success = rocsparse_getenv(s_var_string_names[tag],
                                                      this->m_var_string_defined[tag],
                                                      this->m_var_string[tag]);
                if(!success)
                {
                    std::cerr << "rocsparse_getenv failed on fetching " << s_var_string_names[tag]
                              << std::endl;
                    throw(rocsparse_status_invalid_value);
                }
                break;
            }
            case rocsparse_clients_envariables::TEST_DATA_DIR:
            {
                const bool success = rocsparse_getenv(s_var_string_names[tag],
                                                      this->m_var_string_defined[tag],
                                                      this->m_var_string[tag]);
                if(!success)
                {
                    std::cerr << "rocsparse_getenv failed on fetching " << s_var_string_names[tag]
                              << std::endl;
                    throw(rocsparse_status_invalid_value);
                }
                break;
            }
            }
        }

        if(this->m_var_bool[rocsparse_clients_envariables::VERBOSE])
        {
            for(auto tag : rocsparse_clients_envariables::s_var_bool_all)
            {
                switch(tag)
                {
                case rocsparse_clients_envariables::VERBOSE:
                {
                    const bool v = this->m_var_bool[tag];
                    std::cout << ""
                              << "env variable " << s_var_bool_names[tag] << " : "
                              << ((this->m_var_bool_defined[tag]) ? ((v) ? "enabled" : "disabled")
                                                                  : "<undefined>")
                              << std::endl;
                    break;
                }
                case rocsparse_clients_envariables::TEST_DEBUG_ARGUMENTS:
                {
                    const bool v = this->m_var_bool[tag];
                    std::cout << ""
                              << "env variable " << s_var_bool_names[tag] << " : "
                              << ((this->m_var_bool_defined[tag]) ? ((v) ? "enabled" : "disabled")
                                                                  : "<undefined>")
                              << std::endl;
                    break;
                }
                }
            }

            for(auto tag : rocsparse_clients_envariables::s_var_string_all)
            {
                switch(tag)
                {
                case rocsparse_clients_envariables::MATRICES_DIR:
                {
                    const std::string v = this->m_var_string[tag];
                    std::cout << ""
                              << "env variable " << s_var_string_names[tag] << " : "
                              << ((this->m_var_string_defined[tag]) ? this->m_var_string[tag]
                                                                    : "<undefined>")
                              << std::endl;
                    break;
                }
                case rocsparse_clients_envariables::TEST_DATA_DIR:
                {
                    const std::string v = this->m_var_string[tag];
                    std::cout << ""
                              << "env variable " << s_var_string_names[tag] << " : "
                              << ((this->m_var_string_defined[tag]) ? this->m_var_string[tag]
                                                                    : "<undefined>")
                              << std::endl;
                    break;
                }
                }
            }
        }
    }
};

std::mutex rocsparse_clients_envariables_impl::s_mutex;

rocsparse_clients_envariables_impl& rocsparse_clients_envariables_impl::Instance()
{
    static rocsparse_clients_envariables_impl instance;
    return instance;
}

bool rocsparse_clients_envariables::is_defined(rocsparse_clients_envariables::var_string v)
{
    return rocsparse_clients_envariables_impl::Instance().is_defined(v);
}

const char* rocsparse_clients_envariables::get(rocsparse_clients_envariables::var_string v)
{
    return rocsparse_clients_envariables_impl::Instance().get(v);
}

void rocsparse_clients_envariables::set(rocsparse_clients_envariables::var_string v,
                                        const char*                               value)
{
    rocsparse_clients_envariables_impl::Instance().set(v, value);
}

const char* rocsparse_clients_envariables::get_name(rocsparse_clients_envariables::var_string v)
{
    return s_var_string_names[v];
}

const char*
    rocsparse_clients_envariables::get_description(rocsparse_clients_envariables::var_string v)
{
    return s_var_string_descriptions[v];
}

bool rocsparse_clients_envariables::is_defined(rocsparse_clients_envariables::var_bool v)
{
    return rocsparse_clients_envariables_impl::Instance().is_defined(v);
}

bool rocsparse_clients_envariables::get(rocsparse_clients_envariables::var_bool v)
{
    return rocsparse_clients_envariables_impl::Instance().get(v);
}

void rocsparse_clients_envariables::set(rocsparse_clients_envariables::var_bool v, bool value)
{
    rocsparse_clients_envariables_impl::Instance().set(v, value);
}

const char* rocsparse_clients_envariables::get_name(rocsparse_clients_envariables::var_bool v)
{
    return s_var_bool_names[v];
}

const char*
    rocsparse_clients_envariables::get_description(rocsparse_clients_envariables::var_bool v)
{
    return s_var_bool_descriptions[v];
}