YASK
Yet Another Stencil Kit: a software framework for creating HPC stencil code. Copyright 2014-2023 Intel Corporation.
Loading...
Searching...
No Matches
yask_common_api.hpp
Go to the documentation of this file.
1/*****************************************************************************
2
3YASK: Yet Another Stencil Kit
4Copyright (c) 2014-2023, Intel Corporation
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to
8deal in the Software without restriction, including without limitation the
9rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10sell copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13* The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22IN THE SOFTWARE.
23
24*****************************************************************************/
25
27
28// This file uses Doxygen markup for API documentation-generation.
29// See https://www.doxygen.nl/manual/index.html.
32#pragma once
33
34#include <cstdint>
35#include <cinttypes>
36#include <climits>
37#include <type_traits>
38#include <string>
39#include <vector>
40#include <set>
41#include <map>
42#include <iostream>
43#include <ostream>
44#include <sstream>
45#include <memory>
46#include <functional>
47
48// Things SWIG can't handle.
49#ifdef SWIG
50#ifndef YASK_DEPRECATED
51#define YASK_DEPRECATED
52#endif
53#ifndef YASK_NORETURN
54#define YASK_NORETURN
55#endif
56#define YASK_INT64_T long int
57#else
59#ifndef YASK_DEPRECATED
60#define YASK_DEPRECATED [[deprecated]]
61#endif
63#ifndef YASK_NORETURN
64#define YASK_NORETURN [[noreturn]]
65#endif
67#define YASK_INT64_T std::int64_t
68#endif
69
70namespace yask {
71
79
83
85
87
89 typedef std::vector<idx_t> idx_t_vec;
90
92
96 typedef std::initializer_list<idx_t> idx_t_init_list;
97
99 typedef std::vector<std::string> string_vec;
100
101 // Forward declarations of class-pointers.
102
103 class yask_output;
105 typedef std::shared_ptr<yask_output> yask_output_ptr;
106
107 class yask_file_output;
109 typedef std::shared_ptr<yask_file_output> yask_file_output_ptr;
110
111 class yask_string_output;
113 typedef std::shared_ptr<yask_string_output> yask_string_output_ptr;
114
115 class yask_stdout_output;
117 typedef std::shared_ptr<yask_stdout_output> yask_stdout_output_ptr;
118
119 class yask_null_output;
121 typedef std::shared_ptr<yask_null_output> yask_null_output_ptr;
122
124
125 class yask_exception: public std::exception {
126 private:
128 std::string _msg;
129
130 public:
131
134 _msg("YASK error") { };
135
137 yask_exception(const std::string& message) :
138 _msg(message) { };
139
140 virtual ~yask_exception() { };
141
143
145 virtual const char* what() const noexcept;
146
148 virtual void add_message(const std::string& message );
150
152
154 virtual const char* get_message() const;
155 };
156
157 #ifndef SWIG
158
160
164 #define THROW_YASK_EXCEPTION(message) do { \
165 auto msg = std::string("YASK error: ") + message; \
166 yask_exception e(msg); \
167 throw e; \
168 } while(0)
169
171
175 #define FORMAT_AND_THROW_YASK_EXCEPTION(message) do { \
176 std::stringstream err; \
177 err << message; \
178 THROW_YASK_EXCEPTION(err.str()); \
179 } while(0)
180
181 #endif
182
185 public:
186 virtual ~yask_output_factory() {}
187
189
195 new_file_output(const std::string& file_name ) const;
198
200
206
208
214
216
222 };
223
226 public:
227 virtual ~yask_output() {}
228
230
231 virtual std::ostream& get_ostream() =0;
232 };
233
235 class yask_file_output : public virtual yask_output {
236 public:
237 virtual ~yask_file_output() {}
238
240
241 virtual std::string get_filename() const =0;
242
244 virtual void close() =0;
245 };
246
248 class yask_string_output : public virtual yask_output {
249 public:
250 virtual ~yask_string_output() {}
251
253
255 virtual std::string get_string() const =0;
256
258 virtual void discard() =0;
259 };
260
262 class yask_stdout_output : public virtual yask_output {
263 public:
264 virtual ~yask_stdout_output() {}
265 };
266
268
269 class yask_null_output : public virtual yask_output {
270 public:
271 virtual ~yask_null_output() {}
272 };
273
275
281 std::vector<double>
282 get_center_fd_coefficients(int derivative_order,
284 int radius );
286
288
293 std::vector<double>
294 get_forward_fd_coefficients(int derivative_order,
296 int accuracy_order );
298
300
305 std::vector<double>
306 get_backward_fd_coefficients(int derivative_order,
308 int accuracy_order );
310
312
315 std::vector<double>
316 get_arbitrary_fd_coefficients(int derivative_order,
318 double eval_point,
320 const std::vector<double> sample_points );
322
323 #ifndef SWIG
324
326
335
336 public:
337
339
343
344 private:
345 std::string _name;
346 std::string _help;
347 std::string _help_leader;
348 std::string _current_value_str;
349
350 protected:
351
353 virtual void _print_help(std::ostream& os,
354 const std::string& display_name,
355 int width) const;
356
358
362 virtual bool _is_opt(const string_vec& args, int& argi,
363 const std::string& str) const;
364
366
371 virtual double _double_val(const string_vec& args, int& argi);
372
374
379 virtual idx_t _idx_val(const string_vec& args, int& argi);
380
382
387 virtual std::string _string_val(const string_vec& args, int& argi);
388
389 public:
391 option_base(const std::string& name,
392 const std::string& help_msg,
393 const std::string& current_value_prefix = std::string("Current value = "),
394 const std::string& help_line_prefix = std::string(" ")) :
395 _name(name), _help(help_msg),
396 _help_leader(help_line_prefix),
397 _current_value_str(current_value_prefix)
398 { }
399 virtual ~option_base() { }
400
402 virtual const std::string& get_name() const {
403 return _name;
404 }
405
407 virtual const std::string& get_help() const {
408 return _help;
409 }
410
412 virtual void print_help(std::ostream& os,
413 int width) const {
414 _print_help(os, _name, width);
415 }
416
418 virtual std::ostream& print_value(std::ostream& os) const =0;
419
421
425 virtual bool check_arg(const string_vec& args, int& argi) =0;
426 };
427
429 typedef std::shared_ptr<option_base> option_ptr;
430
432 class bool_option : public option_base {
433 bool& _val;
434
435 public:
437 bool_option(const std::string& name,
438 const std::string& help_msg,
439 bool& val) :
440 option_base(name, help_msg), _val(val) { }
441
443 virtual void print_help(std::ostream& os,
444 int width) const override;
445
447 virtual std::ostream& print_value(std::ostream& os) const override {
448 os << (_val ? "true" : "false");
449 return os;
450 }
451
453 virtual bool check_arg(const string_vec& args, int& argi) override;
454 };
455
457 class int_option : public option_base {
458 int& _val;
459
460 public:
462 int_option(const std::string& name,
463 const std::string& help_msg,
464 int& val) :
465 option_base(name, help_msg), _val(val) { }
466
468 virtual void print_help(std::ostream& os,
469 int width) const override;
470
472 virtual std::ostream& print_value(std::ostream& os) const override {
473 os << _val;
474 return os;
475 }
476
478 virtual bool check_arg(const string_vec& args, int& argi) override;
479 };
480
482 class double_option : public option_base {
483 double& _val;
484
485 public:
487 double_option(const std::string& name,
488 const std::string& help_msg,
489 double& val) :
490 option_base(name, help_msg), _val(val) { }
491
493 virtual void print_help(std::ostream& os,
494 int width) const override;
495
497 virtual std::ostream& print_value(std::ostream& os) const override {
498 os << _val;
499 return os;
500 }
501
503 virtual bool check_arg(const string_vec& args, int& argi) override;
504 };
505
507 class idx_option : public option_base {
508 idx_t& _val;
509
510 public:
512 idx_option(const std::string& name,
513 const std::string& help_msg,
514 idx_t& val) :
515 option_base(name, help_msg), _val(val) { }
516
518 virtual void print_help(std::ostream& os,
519 int width) const override;
520
522 virtual std::ostream& print_value(std::ostream& os) const override {
523 os << _val;
524 return os;
525 }
526
528 virtual bool check_arg(const string_vec& args, int& argi) override;
529 };
530
532 class string_option : public option_base {
533 std::string& _val;
534
535 public:
537 string_option(const std::string& name,
538 const std::string& help_msg,
539 std::string& val) :
540 option_base(name, help_msg), _val(val) { }
541
543 virtual void print_help(std::ostream& os,
544 int width) const override;
545
547 virtual std::ostream& print_value(std::ostream& os) const override {
548 os << "'" << _val << "'";
549 return os;
550 }
551
553 virtual bool check_arg(const string_vec& args,
554 int& argi) override;
555 };
556
558
562 std::set<std::string> _allowed_strs; // empty to allow any strings.
563 string_vec& _val;
564
565 public:
567 string_list_option(const std::string& name,
568 const std::string& help_msg,
569 string_vec& val) :
570 option_base(name, help_msg),
571 _val(val) { }
572
574 string_list_option(const std::string& name,
575 const std::string& help_msg,
576 const std::set<std::string>& allowed_strs,
577 string_vec& val) :
578 option_base(name, help_msg),
579 _allowed_strs(allowed_strs),
580 _val(val) { }
581
583 virtual void print_help(std::ostream& os,
584 int width) const override;
585
587 virtual std::ostream& print_value(std::ostream& os) const override {
588 int n = 0;
589 for (auto& v : _val) {
590 if (n)
591 os << ",";
592 os << v;
593 n++;
594 }
595 return os;
596 }
597
599 virtual bool check_arg(const string_vec& args, int& argi) override;
600 };
601
602 private:
603 std::map<std::string, option_ptr> _opts;
604 int _width = 78;
605
606 public:
607
610
613
615 static string_vec set_args(const std::string& arg_string);
616
618 virtual void set_width(int width) {
619 _width = width;
620 }
621
623 virtual int get_width() const {
624 return _width;
625 }
626
628 virtual void add_option(option_ptr opt) {
629 _opts[opt->get_name()] = opt;
630 }
631
633 virtual void print_help(std::ostream& os) const;
634
636 virtual void print_values(std::ostream& os) const;
637
639
645 virtual std::string parse_args(const std::string& pgm_name,
646 const string_vec& args);
647
649 virtual std::string parse_args(const std::string& pgm_name,
650 const std::string& arg_string) {
651 auto args = set_args(arg_string);
652 return parse_args(pgm_name, args);
653 }
654
657 virtual std::string parse_args(int argc, char** argv) {
658 std::string pgm_name = argv[0];
659 string_vec args;
660 for (int i = 1; i < argc; i++)
661 args.push_back(argv[i]);
662 return parse_args(pgm_name, args);
663 }
664 };
665
667
674 extern void
675 yask_print_splash(std::ostream& os, int argc, char** argv,
676 std::string invocation_leader = "invocation: ");
677 #endif
678
681} // namespace yask.
682
A boolean option.
Definition yask_common_api.hpp:432
virtual void print_help(std::ostream &os, int width) const override
Print help message for a boolean option.
bool_option(const std::string &name, const std::string &help_msg, bool &val)
Constructor.
Definition yask_common_api.hpp:437
virtual std::ostream & print_value(std::ostream &os) const override
Print current value of the boolean.
Definition yask_common_api.hpp:447
virtual bool check_arg(const string_vec &args, int &argi) override
Check for a boolean option (set or unset variants).
A double option.
Definition yask_common_api.hpp:482
virtual std::ostream & print_value(std::ostream &os) const override
Print the current value of the double.
Definition yask_common_api.hpp:497
virtual bool check_arg(const string_vec &args, int &argi) override
Check for the option and its double argument.
virtual void print_help(std::ostream &os, int width) const override
Print help message for a double option.
double_option(const std::string &name, const std::string &help_msg, double &val)
Constructor.
Definition yask_common_api.hpp:487
An idx_t option.
Definition yask_common_api.hpp:507
virtual std::ostream & print_value(std::ostream &os) const override
Print the current value of the int_t.
Definition yask_common_api.hpp:522
virtual void print_help(std::ostream &os, int width) const override
Print help message for an int_t option.
virtual bool check_arg(const string_vec &args, int &argi) override
Check for the option and its int_t argument.
idx_option(const std::string &name, const std::string &help_msg, idx_t &val)
Constructor.
Definition yask_common_api.hpp:512
An integer option.
Definition yask_common_api.hpp:457
virtual void print_help(std::ostream &os, int width) const override
Print help message for an int option.
virtual bool check_arg(const string_vec &args, int &argi) override
Check for the option and its integer argument.
virtual std::ostream & print_value(std::ostream &os) const override
Print the current value of the int.
Definition yask_common_api.hpp:472
int_option(const std::string &name, const std::string &help_msg, int &val)
Constructor.
Definition yask_common_api.hpp:462
Base class for a command-line option.
Definition yask_common_api.hpp:342
virtual double _double_val(const string_vec &args, int &argi)
Get one double value from args[argi++].
virtual std::ostream & print_value(std::ostream &os) const =0
Print current value of this option.
virtual const std::string & get_name() const
Get the current option name.
Definition yask_common_api.hpp:402
virtual std::string _string_val(const string_vec &args, int &argi)
Get one string value from args[argi++].
virtual void print_help(std::ostream &os, int width) const
Print help on this option.
Definition yask_common_api.hpp:412
virtual idx_t _idx_val(const string_vec &args, int &argi)
Get one idx_t value from args[argi++].
virtual bool _is_opt(const string_vec &args, int &argi, const std::string &str) const
Check for matching option to str at args[argi].
virtual bool check_arg(const string_vec &args, int &argi)=0
Check for matching option and any needed args at args[argi].
virtual const std::string & get_help() const
Get the unformatted help string.
Definition yask_common_api.hpp:407
option_base(const std::string &name, const std::string &help_msg, const std::string &current_value_prefix=std::string("Current value = "), const std::string &help_line_prefix=std::string(" "))
Constructor.
Definition yask_common_api.hpp:391
virtual void _print_help(std::ostream &os, const std::string &display_name, int width) const
Format and print help for option named display_name to os.
A list-of-strings option.
Definition yask_common_api.hpp:561
virtual bool check_arg(const string_vec &args, int &argi) override
Check for the option and its string-list argument.
virtual std::ostream & print_value(std::ostream &os) const override
Print the current value of the strings.
Definition yask_common_api.hpp:587
string_list_option(const std::string &name, const std::string &help_msg, const std::set< std::string > &allowed_strs, string_vec &val)
Constructor with set of allowed strings.
Definition yask_common_api.hpp:574
virtual void print_help(std::ostream &os, int width) const override
Print help message for a list-of-strings option.
string_list_option(const std::string &name, const std::string &help_msg, string_vec &val)
Constructor allowing any strings.
Definition yask_common_api.hpp:567
A string option.
Definition yask_common_api.hpp:532
string_option(const std::string &name, const std::string &help_msg, std::string &val)
Constructor.
Definition yask_common_api.hpp:537
virtual bool check_arg(const string_vec &args, int &argi) override
Check for the option and its string argument.
virtual std::ostream & print_value(std::ostream &os) const override
Print the current value of the string.
Definition yask_common_api.hpp:547
virtual void print_help(std::ostream &os, int width) const override
Print help message for a string option.
A class to parse command-line arguments.
Definition yask_common_api.hpp:334
virtual int get_width() const
Get help width.
Definition yask_common_api.hpp:623
virtual void set_width(int width)
Set help width.
Definition yask_common_api.hpp:618
virtual ~command_line_parser()
Destructor.
Definition yask_common_api.hpp:612
virtual std::string parse_args(int argc, char **argv)
Same as parse_args(), but pgm_name is populated from argv[0] and rest of argv is parsed.
Definition yask_common_api.hpp:657
virtual void add_option(option_ptr opt)
Add an allowed option to the parser.
Definition yask_common_api.hpp:628
virtual void print_values(std::ostream &os) const
Print current settings of all options to os.
virtual std::string parse_args(const std::string &pgm_name, const string_vec &args)
Parse options from 'args' and set corresponding vars.
static string_vec set_args(const std::string &arg_string)
Convenience funcion to tokenize args from a string.
virtual void print_help(std::ostream &os) const
Print help info on all options to os.
command_line_parser()
Constructor.
Definition yask_common_api.hpp:609
std::shared_ptr< option_base > option_ptr
Pointer to an option handler.
Definition yask_common_api.hpp:429
virtual std::string parse_args(const std::string &pgm_name, const std::string &arg_string)
Same as parse_args(), but splits 'arg_string' into tokens.
Definition yask_common_api.hpp:649
Exception from YASK framework.
Definition yask_common_api.hpp:125
yask_exception()
Construct a YASK exception with default message.
Definition yask_common_api.hpp:133
yask_exception(const std::string &message)
Construct a YASK exception with message.
Definition yask_common_api.hpp:137
virtual const char * what() const noexcept
Get description.
virtual const char * get_message() const
Get description.
virtual void add_message(const std::string &message)
Append message to description of this exception.
File output.
Definition yask_common_api.hpp:235
virtual std::string get_filename() const =0
Get the filename.
virtual void close()=0
Close file.
Null output.
Definition yask_common_api.hpp:269
Factory to create output objects.
Definition yask_common_api.hpp:184
virtual yask_file_output_ptr new_file_output(const std::string &file_name) const
Create a file output object.
virtual yask_null_output_ptr new_null_output() const
Create a null output object.
virtual yask_string_output_ptr new_string_output() const
Create a string output object.
virtual yask_stdout_output_ptr new_stdout_output() const
Create a stdout output object.
Base interface for output.
Definition yask_common_api.hpp:225
virtual std::ostream & get_ostream()=0
Access underlying C++ ostream object.
Stdout output.
Definition yask_common_api.hpp:262
String output.
Definition yask_common_api.hpp:248
virtual void discard()=0
Discard contents of current buffer.
virtual std::string get_string() const =0
Get the output.
YASK_INT64_T idx_t
Type to use for indexing grids.
Definition yask_common_api.hpp:86
std::vector< double > get_backward_fd_coefficients(int derivative_order, int accuracy_order)
Create finite-difference (FD) coefficients for the standard backward form.
std::shared_ptr< yask_string_output > yask_string_output_ptr
Shared pointer to yask_string_output.
Definition yask_common_api.hpp:113
std::vector< double > get_center_fd_coefficients(int derivative_order, int radius)
Create finite-difference (FD) coefficients for the standard center form.
std::shared_ptr< yask_file_output > yask_file_output_ptr
Shared pointer to yask_file_output.
Definition yask_common_api.hpp:109
std::vector< std::string > string_vec
Vector of strings.
Definition yask_common_api.hpp:99
std::shared_ptr< yask_output > yask_output_ptr
Shared pointer to yask_output.
Definition yask_common_api.hpp:105
std::vector< double > get_arbitrary_fd_coefficients(int derivative_order, double eval_point, const std::vector< double > sample_points)
Create finite-difference (FD) coefficients at arbitrary evaluation and sample points.
std::shared_ptr< yask_null_output > yask_null_output_ptr
Shared pointer to yask_null_output.
Definition yask_common_api.hpp:121
std::string yask_get_version_string()
Version information.
std::initializer_list< idx_t > idx_t_init_list
Initializer list of indices.
Definition yask_common_api.hpp:96
std::vector< idx_t > idx_t_vec
Vector of indices.
Definition yask_common_api.hpp:89
std::vector< double > get_forward_fd_coefficients(int derivative_order, int accuracy_order)
Create finite-difference (FD) coefficients for the standard forward form.
std::shared_ptr< yask_stdout_output > yask_stdout_output_ptr
Shared pointer to yask_stdout_output.
Definition yask_common_api.hpp:117
void yask_print_splash(std::ostream &os, int argc, char **argv, std::string invocation_leader="invocation: ")
Print a YASK spash message to os.
#define YASK_INT64_T
Signed 64-bit int.
Definition yask_common_api.hpp:67