File: rocsparse_parse_data.cpp

package info (click to toggle)
rocsparse 6.4.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,428 kB
  • sloc: cpp: 234,069; f90: 9,307; sh: 2,262; python: 1,939; makefile: 1,585; ansic: 440; xml: 26
file content (421 lines) | stat: -rw-r--r-- 16,512 bytes parent folder | download
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
/*! \file */
/* ************************************************************************
 * Copyright (C) 2019-2024 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_parse_data.hpp"
#include "rocsparse_clients_matrices_dir.hpp"
#include "rocsparse_data.hpp"
#include "rocsparse_reproducibility.hpp"
#include "utility.hpp"

#include <fcntl.h>
#include <sys/types.h>

#ifdef WIN32

#ifdef __cpp_lib_filesystem
#include <filesystem>
namespace fs = std::filesystem;
#else
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#endif

#else
#include <sys/wait.h>
#include <unistd.h>
#endif

#include "rocsparse_clients_envariables.hpp"

// Parse YAML data
static std::string rocsparse_parse_yaml(const std::string& yaml, const char* include_path)
{
#ifdef WIN32
    // Generate "/tmp/rocsparse-XXXXXX" like file name
    const std::string alphanum     = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv";
    int               stringlength = alphanum.length() - 1;
    std::string       uniquestr    = "rocsparse-";

    for(int n = 0; n <= 5; ++n)
    {
        uniquestr += alphanum.at(rand() % stringlength);
    }

    fs::path tmpname = fs::temp_directory_path() / uniquestr;

    auto        exepath       = rocsparse_exepath();
    const char* matrices_path = rocsparse_clients_matrices_dir_get(false);
    auto        cmd           = exepath + "rocsparse_gentest.py ";
    if(include_path != nullptr)
    {
        cmd += " -I ";
        cmd += include_path;
    }
    else
    {
        cmd += " -I ./ ";
    }

    if(matrices_path && matrices_path[0] != '\0')
    {
        cmd += " -m ";
        cmd += matrices_path;
    }
    cmd += " --template " + exepath + "rocsparse_template.yaml -o " + tmpname.string() + " " + yaml;

    std::cerr << cmd << std::endl;

    int status = std::system(cmd.c_str());
    if(status != 0)
    {
        perror("system cmd failed");
        exit(EXIT_FAILURE);
    }

    return tmpname.string(); // results to be read and removed later
#else

    char tmp[] = "/tmp/rocsparse-XXXXXX";
    int  fd    = mkostemp(tmp, O_CLOEXEC);
    if(fd == -1)
    {
        perror("Cannot open temporary file");
        exit(EXIT_FAILURE);
    }
    auto        exepath       = rocsparse_exepath();
    const char* matrices_path = rocsparse_clients_matrices_dir_get(false);
    auto        cmd           = exepath + "rocsparse_gentest.py ";
    if(include_path != nullptr)
    {
        cmd += " -I ";
        cmd += include_path;
    }
    else
    {
        cmd += " -I ./ ";
    }

    if(matrices_path && matrices_path[0] != '\0')
    {
        cmd += " -m ";
        cmd += matrices_path;
    }

    cmd += " --template " + exepath + "rocsparse_template.yaml -o " + tmp + " " + yaml;

    std::cerr << cmd << std::endl;

    int status = system(cmd.c_str());
    if(status == -1 || !WIFEXITED(status) || WEXITSTATUS(status))
        exit(EXIT_FAILURE);
    return tmp;

#endif
}

// Parse --data and --yaml command-line arguments, -- matrices-dir and optionally memstat-report

bool rocsparse_parse_data(int& argc, char** argv, const std::string& default_file)
{
    std::string filename;
    char**      argv_p = argv + 1;
    bool        help = false, yaml = false;
#ifdef ROCSPARSE_WITH_MEMSTAT
    const char* memory_report_filename = nullptr;
#endif

    // Scan, process and remove any --yaml or --data options
    std::string command = argv[0];
    for(int i = 1; i < argc; ++i)
    {
        command += std::string(" ") + argv[i];
    }
    const char* include_path = nullptr;
    rocsparse_reproducibility_t::instance().config().set_command(command);
    for(int i = 1; argv[i]; ++i)
    {
        if(!strcmp(argv[i], "-I"))
        {
            include_path = argv[++i];
        }
        else if(!strcmp(argv[i], "--data") || (yaml |= !strcmp(argv[i], "--yaml")))
        {
            if(filename != "")
            {
                std::cerr << "Only one of the --yaml and --data options may be specified"
                          << std::endl;
                exit(EXIT_FAILURE);
            }

            if(!argv[i + 1] || !argv[i + 1][0])
            {
                std::cerr << "The " << argv[i] << " option requires an argument" << std::endl;
                exit(EXIT_FAILURE);
            }
            filename = argv[++i];
        }
#ifdef ROCSPARSE_WITH_MEMSTAT
        else if(!strcmp(argv[i], "--memstat-report"))
        {
            if(!argv[i + 1] || !argv[i + 1][0])
            {
                std::cerr << "The " << argv[i] << " option requires an argument" << std::endl;
                exit(EXIT_FAILURE);
            }
            memory_report_filename = argv[++i];
        }
#endif
        else if(!strcmp(argv[i], "--matrices-dir"))
        {
            if(!argv[i + 1] || !argv[i + 1][0])
            {
                std::cerr << "The " << argv[i] << " option requires an argument" << std::endl;
                exit(EXIT_FAILURE);
            }
            rocsparse_clients_matrices_dir_set(argv[++i]);
        }
        else if(!strcmp(argv[i], "--r-o"))
        {
            if(!argv[i + 1] || !argv[i + 1][0])
            {
                std::cerr << "The " << argv[i] << " option requires an argument" << std::endl;
                exit(EXIT_FAILURE);
            }
            rocsparse_reproducibility_t::instance().config().set_filename(argv[++i]);
        }
        else if(!strcmp(argv[i], "--r"))
        {
            rocsparse_reproducibility_t::instance().enable();
        }
        else if(!strcmp(argv[i], "--r-niter"))
        {
            if(rocsparse_reproducibility_t::instance().is_enabled() == false)
            {
                std::cerr
                    << "--r-niter cannot be used if the reproducibility is not enabled with '--r'"
                    << std::endl;
                exit(EXIT_FAILURE);
            }
            if(!argv[i + 1] || !argv[i + 1][0])
            {
                std::cerr << "The " << argv[i] << " option requires an argument" << std::endl;
                exit(EXIT_FAILURE);
            }
            for(int j = 0; argv[i + 1][j] != '\0'; ++j)
            {
                if(argv[i + 1][j] < '0' || argv[i + 1][j] > '9')
                {
                    std::cerr << "The " << argv[i] << " option requires an integer as an argument"
                              << std::endl;
                    exit(EXIT_FAILURE);
                }
            }
            int32_t num_iterations = atoi(argv[++i]);
            if(num_iterations < 2)
            {
                std::cerr << "The " << argv[i - 1]
                          << " option requires a positive value greater or equal to 2."
                          << std::endl;
                exit(EXIT_FAILURE);
            }
            rocsparse_reproducibility_t::instance().set_num_iterations(num_iterations);
        }
        else if(!strcmp(argv[i], "--r-level"))
        {
            if(rocsparse_reproducibility_t::instance().is_enabled() == false)
            {
                std::cerr
                    << "--r-level cannot be used if the reproducibility is not enabled with '--r'"
                    << std::endl;
                exit(EXIT_FAILURE);
            }
            if(!argv[i + 1] || !argv[i + 1][0])
            {
                std::cerr << "The " << argv[i] << " option requires an argument" << std::endl;
                exit(EXIT_FAILURE);
            }
            int32_t r_level = atoi(argv[++i]);
            if(r_level < 0)
            {
                std::cerr << "The " << argv[i - 1] << " option requires a positive value"
                          << std::endl;
                exit(EXIT_FAILURE);
            }
            rocsparse_reproducibility_t::instance().config().set_info_level(r_level);
        }
        else if(!strcmp(argv[i], "--rocsparse-clients-enable-test-debug-arguments"))
        {
            rocsparse_clients_envariables::set(rocsparse_clients_envariables::TEST_DEBUG_ARGUMENTS,
                                               true);
        }
        else if(!strcmp(argv[i], "--rocsparse-clients-disable-test-debug-arguments"))
        {
            rocsparse_clients_envariables::set(rocsparse_clients_envariables::TEST_DEBUG_ARGUMENTS,
                                               false);
        }
        else if(!strcmp(argv[i], "--rocsparse-disable-debug"))
        {
            rocsparse_disable_debug();
        }
        else if(!strcmp(argv[i], "--rocsparse-enable-debug"))
        {
            rocsparse_enable_debug();
        }
        else if(!strcmp(argv[i], "--rocsparse-enable-debug-verbose"))
        {
            rocsparse_enable_debug_verbose();
        }
        else if(!strcmp(argv[i], "--rocsparse-disable-debug-verbose"))
        {
            rocsparse_disable_debug_verbose();
        }
        else if(!strcmp(argv[i], "--rocsparse-enable-debug-arguments"))
        {
            rocsparse_enable_debug_arguments();
        }
        else if(!strcmp(argv[i], "--rocsparse-disable-debug-arguments"))
        {
            rocsparse_disable_debug_arguments();
        }
        else if(!strcmp(argv[i], "--rocsparse-enable-debug-arguments-verbose"))
        {
            rocsparse_enable_debug_arguments_verbose();
        }
        else if(!strcmp(argv[i], "--rocsparse-disable-debug-arguments-verbose"))
        {
            rocsparse_disable_debug_arguments_verbose();
        }
        else
        {
            *argv_p++ = argv[i];
            if(!help && (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")))
            {
                help = true;
                std::cout << "\n"
                          << argv[0]
                          << " [ --data <path> | --yaml <path> ] [--matrices-dir <path>] [-I "
                             "<path>] <options> ...\n"
                          << std::endl;

                std::cout << "" << std::endl;
                std::cout << "Rocsparse reproducibility options:" << std::endl;
                std::cout << "--r          enable rocsparse reproducibility testing" << std::endl;
                std::cout << "--r-niter    set the number of "
                             "reproducibility iterations."
                          << std::endl;
                std::cout << "--r-o        file results for reproducibility." << std::endl;
                std::cout << "--r-level    level of information shrinking in the reproducibility "
                             "json file."
                          << std::endl;
                std::cout << "" << std::endl;
                std::cout << "Rocsparse clients debug options:" << std::endl;
                std::cout << "--rocsparse-clients-enable-test-debug-arguments   enable rocsparse "
                             "clients test debug arguments, it discards any environment variable "
                             "definition of ROCSPARSE_CLIENTS_TEST_DEBUG_ARGUMENTS."
                          << std::endl;
                std::cout << "--rocsparse-clients-disable-test-debug-arguments  disable rocsparse "
                             "clients test debug arguments, it discards any environment variable "
                             "definition of ROCSPARSE_CLIENTS_TEST_DEBUG_ARGUMENTS."
                          << std::endl;
                std::cout << "" << std::endl;
                std::cout << "Rocsparse debug options:" << std::endl;
                std::cout << "--rocsparse-enable-debug                     enable rocsparse debug, "
                             "it discards any environment variable definition of ROCSPARSE_DEBUG."
                          << std::endl;
                std::cout
                    << "--rocsparse-disable-debug                    disable rocsparse debug, it "
                       "discards any environment variable definition of ROCSPARSE_DEBUG."
                    << std::endl;
                std::cout << "--rocsparse-enable-debug-verbose             enable rocsparse debug "
                             "verbose, it discards any environment variable definition of "
                             "ROCSPARSE_DEBUG_VERBOSE."
                          << std::endl;
                std::cout << "--rocsparse-disable-debug-verbose            disable rocsparse debug "
                             "verbose, it discards any environment variable definition of "
                             "ROCSPARSE_DEBUG_VERBOSE"
                          << std::endl;
                std::cout << "--rocsparse-enable-debug-arguments           enable rocsparse debug "
                             "arguments, it discards any environment variable definition of "
                             "ROCSPARSE_DEBUG_ARGUMENTS."
                          << std::endl;
                std::cout << "--rocsparse-disable-debug-arguments          disable rocsparse debug "
                             "arguments, it discards any environment variable definition of "
                             "ROCSPARSE_DEBUG_ARGUMENTS."
                          << std::endl;
                std::cout << "--rocsparse-enable-debug-arguments-verbose   enable rocsparse debug "
                             "arguments verbose, it discards any environment variable definition "
                             "of ROCSPARSE_DEBUG_ARGUMENTS_VERBOSE"
                          << std::endl;
                std::cout << "--rocsparse-disable-debug-arguments-verbose  disable rocsparse debug "
                             "arguments verbose, it discards any environment variable definition "
                             "of ROCSPARSE_DEBUG_ARGUMENTS_VERBOSE"
                          << std::endl;
                std::cout << "" << std::endl;
                std::cout << "" << std::endl;
                std::cout << "Specific environment variables:" << std::endl;
                for(const auto v : rocsparse_clients_envariables::s_var_bool_all)
                {
                    std::cout << rocsparse_clients_envariables::get_name(v) << " "
                              << rocsparse_clients_envariables::get_description(v) << std::endl;
                }
                for(const auto v : rocsparse_clients_envariables::s_var_string_all)
                {
                    std::cout << rocsparse_clients_envariables::get_name(v) << " "
                              << rocsparse_clients_envariables::get_description(v) << std::endl;
                }
                std::cout << "" << std::endl;
            }
        }
    }

#ifdef ROCSPARSE_WITH_MEMSTAT
    rocsparse_status status = rocsparse_memstat_report(
        memory_report_filename ? memory_report_filename : "rocsparse_test_memstat.json");
    if(status != rocsparse_status_success)
    {
        std::cerr << "rocsparse_memstat_report failed " << std::endl;
        exit(EXIT_FAILURE);
    }
#endif

    // argc and argv contain remaining options and non-option arguments
    *argv_p = nullptr;
    argc    = argv_p - argv;

    if(filename == "-")
        filename = "/dev/stdin";
    else if(filename == "")
        filename = default_file;

    if(yaml)
        filename = rocsparse_parse_yaml(filename, include_path);

    if(filename != "")
    {
        RocSPARSE_TestData::set_filename(filename, yaml);
        return true;
    }

    return false;
}