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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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
/***************************************************************************************************
 * Copyright (c) 2017 - 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 **************************************************************************************************/
/* \file
   \brief 

    "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified, 
     bug-ridden, slow implementation of half of Common Lisp."

      - Greenspun's Tenth Rule of Programming

 
  cutlass::profiler::ProblemSpace defines a set of data structures which represent the Cartesian
  product of sequences defined by integer ranges, lists of scalars, and sets of enumerated types.

  These permit a single invocation of the CUTLASS Profiler to iterate over a large set of problems,
  verify and profile various operations when they are compatible with the command line, and
  construct data tables of results that are convenient inputs to post processing in Excel or Pandas. 

  By executing multiple problems per invocation, startup overheads may be amortized across many
  kernel launches. 
*/

#pragma once

// Standard Library includes
#include <string>
#include <vector>
#include <memory>
#include <unordered_map>
#include <cstdlib>

// CUTLASS Utility includes
#include "cutlass/util/command_line.h"

// CUTLASS Library includes
#include "cutlass/library/library.h"

// Profiler includes
#include "enumerated_types.h"

namespace cutlass {
namespace profiler {

/////////////////////////////////////////////////////////////////////////////////////////////////

/// Defines the argument schema
struct ArgumentDescription {

  /// Type of argument
  ArgumentTypeID type;

  /// Prioritized array of aliases used in command line parsing
  std::vector<std::string> aliases;

  /// Description of argument
  std::string description;

  //
  // Methods
  //

  /// Default ctor
  ArgumentDescription(): 
    type(ArgumentTypeID::kInvalid) { }

  /// Constructor with aliases
  ArgumentDescription(
    ArgumentTypeID type_,
    std::vector<std::string> const &aliases_,
    std::string const &description_
  ):
    type(type_), aliases(aliases_), description(description_) { }
};

/// Vector of arguments
using ArgumentDescriptionVector = std::vector<ArgumentDescription>;

/////////////////////////////////////////////////////////////////////////////////////////////////

/// Base class for kernel arguments
struct KernelArgument {

  //
  // Type definitions
  //

  /// Value base class
  struct Value {

    KernelArgument const *argument;
    bool not_null;

    //
    // Methods
    //

    Value(
      KernelArgument const *argument_ = nullptr, 
      bool not_null_ = true
    ): argument(argument_), not_null(not_null_) { }

    virtual ~Value() { }

    virtual std::ostream &print(std::ostream &out) const =0;
  };

  /// Abstract base class to iterate over values within arguments
  struct ValueIterator {

    /// Indicates type of kernel argument
    KernelArgument const *argument;
    
    /// If the iterator points to an argument that is null, it needs to be distinguished
    /// from end.
    bool null_argument;

    //
    // Methods
    //

    /// Constructs a value iterator - no methods are valid if argument_ == nullptr
    ValueIterator(
      KernelArgument const *argument_ = nullptr, 
      bool null_argument_ = false): 
      argument(argument_), null_argument(null_argument_) {

      if (!argument_->not_null()) {
        null_argument = true;
      }
    }

    virtual ~ValueIterator() { }

    /// Advances to next point in range
    virtual void operator++() = 0;

    /// Compares against another value iterator - must be of the same KernelArgument type
    virtual bool operator==(ValueIterator const &it) const = 0;

    /// Returns a unique_ptr<Value> object pointing to a newly created value object
    virtual std::unique_ptr<Value> at() const = 0;

    /// Gets the type of the iterator
    ArgumentTypeID type() const {
      return argument->description->type;
    }

    /// Helper to compute inequality
    bool operator!=(ValueIterator const &it) const {
      return !(*this == it); 
    }

    std::ostream &print(std::ostream &out) const;
  };

  //
  // Data members
  //

  /// Describes the argument
  ArgumentDescription const *description;

  /// Parent node
  KernelArgument *parent;

  /// Sequence in which the kernel argument is to be iterated over. 
  /// Smaller means faster changing. -1 is don't  care
  int ordinal;

  //
  // Methods
  //

  /// Default ctor
  KernelArgument(
    ArgumentDescription const *description_ = nullptr,
    KernelArgument *parent_ = nullptr,
    int ordinal_ = -1
  ): description(description_), parent(parent_), ordinal(ordinal_) { }

  virtual ~KernelArgument();

  /// Returns true if the kernel argument iself is empty
  virtual bool not_null() const =0;

  /// Returns a string name for debugging
  std::string qualified_name() const {
    if (description) {
      if (description->aliases.empty()) {
        return "<description_not_null_no_aliases>";
      }
      return description->aliases.front();
    }
    return "<description_null>";
  }

  virtual std::unique_ptr<ValueIterator> begin() const =0;
  virtual std::unique_ptr<ValueIterator> end() const =0;
};

using KernelArgumentVector = std::vector<std::unique_ptr<KernelArgument>>;

/////////////////////////////////////////////////////////////////////////////////////////////////

/// Defines a scalar argument type as a string that is lexically cast to the appropriate kernel
/// type.
struct ScalarArgument : public KernelArgument {

  //
  // Type definitions
  //
  
  /// Value type
  struct ScalarValue : public KernelArgument::Value {

    std::string value;

    //
    // Methods
    //

    ScalarValue(
      std::string const &value_ = "",
      ScalarArgument const *argument = nullptr,
      bool not_null_ = true
    );

    virtual std::ostream &print(std::ostream &out) const;
  };

  using ValueCollection = std::vector<std::string>;

  /// Abstract base class to iterate over values within arguments
  struct ScalarValueIterator : public KernelArgument::ValueIterator {

    //
    // Data members
    //

    ValueCollection::const_iterator value_it;

    //
    // Methods
    //

    ScalarValueIterator(ScalarArgument const *argument = nullptr);

    virtual void operator++();
    virtual bool operator==(ValueIterator const &it) const;

    /// Gets the value pointed to
    virtual std::unique_ptr<KernelArgument::Value> at() const;
  };

  //
  // Data members
  //

  /// Set of posible values
  ValueCollection values;

  //
  // Methods
  //

  /// Default ctor
  ScalarArgument(
    ArgumentDescription const *description
  ): 
    KernelArgument(description) { }

  virtual bool not_null() const {
    return !values.empty();
  }

  virtual std::unique_ptr<KernelArgument::ValueIterator> begin() const;
  virtual std::unique_ptr<KernelArgument::ValueIterator> end() const;
};

/////////////////////////////////////////////////////////////////////////////////////////////////

/// Closed range supporting additive increment
struct Range {
  
  //
  // Type definitions
  //

  enum class Mode {
    kSequence,
    kRandom,
    kRandomLog2,
    kInvalid
  };

  struct Iterator {

    int64_t value;
    int64_t increment;
    Range const *range;

    //
    // Methods
    //
    
    Iterator(
      int64_t value_ = 0, 
      int64_t increment_ = 1,
      Range const *range_ = nullptr
    ): 
      value(value_), increment(increment_), range(range_) { }

    Iterator & operator++() {
      value += increment;
      return *this;
    }

    Iterator operator++(int) {
      Iterator self(*this);
      ++(*this);
      return self;
    }

    bool operator==(Iterator const &it) const {
      return value == it.value;
    }

    bool operator!=(Iterator const &it) const {
      return !(*this == it);
    }

    static int64_t round(int64_t value, int64_t divisible) {
      int64_t rem = (value % divisible);

      // Round either up or down
      if (rem > divisible / 2) {
        value += (divisible - rem);
      }
      else {
        value -= rem;
      }

      return value;
    }

    int64_t at() const {
      if (!range) {
        return value;
      }

      switch (range->mode) {
        case Mode::kSequence: return value;

        case Mode::kRandom: {
          double rnd = double(range->minimum) + 
            double(std::rand()) / double(RAND_MAX) * (double(range->maximum) - double(range->minimum));

          int64_t value = int64_t(rnd);

          return round(value, range->divisible);      
        }
        break;

        case Mode::kRandomLog2: {
          double lg2_minimum = std::log(double(range->minimum)) / std::log(2.0);
          double lg2_maximum = std::log(double(range->maximum)) / std::log(2.0);
          double rnd = lg2_minimum + double(std::rand()) / double(RAND_MAX) * (lg2_maximum - lg2_minimum);      

          int64_t value = int64_t(std::pow(2.0, rnd));

          return round(value, range->divisible);
        }
        break;
        default: break;
      }
      return value;
    }

    int64_t operator*() const {
      return at();
    }
  };

  //
  // Data members
  //

  int64_t first;        ///< first element in range
  int64_t last;         ///< last element in range
  int64_t increment;    ///< additive increment between values
  
  Mode mode;            ///< mode selection enables alternative values 
  int64_t minimum;      ///< minimum value to return
  int64_t maximum;      ///< maximum value to return
  int64_t divisible;    ///< rounds value down to an integer multiple of this value 

  //
  // Methods
  //

  /// Default constructor - range acts as a scalar
  Range(int64_t first_ = 0): first(first_), last(first_), increment(1), mode(Mode::kSequence), minimum(0), maximum(0), divisible(1) { }

  /// Range acts as a range
  Range(
    int64_t first_, 
    int64_t last_, 
    int64_t increment_ = 1,
    Mode mode_ = Mode::kSequence,
    int64_t minimum_ = 0,
    int64_t maximum_ = 0,
    int64_t divisible_ = 1
  ): first(first_), last(last_), increment(increment_), mode(mode_), minimum(minimum_), maximum(maximum_), divisible(divisible_) {

    // Helpers to avoid constructing invalid ranges
    if (increment > 0) {
      if (last < first) {
        std::swap(last, first);
      }
    }
    else if (increment < 0) {
      if (first < last) {
        std::swap(last, first);
      }
    }
    else if (last != first) {
      last = first;
      increment = 1;
    }
  }

  /// Helper to construct a sequence range
  static Range Sequence(int64_t first_, int64_t last_, int64_t increment_ = 1) {
    return Range(first_, last_, increment_, Mode::kSequence);
  }

  /// Helper to construct a range that is a random distribution 
  static Range Random(int64_t minimum_, int64_t maximum_, int64_t count_, int64_t divisible_ = 1) {
    return Range(1, count_, 1, Mode::kRandom, minimum_, maximum_, divisible_);
  }

  /// Helper to construct a range that is a random distribution over a log scale
  static Range RandomLog2(int64_t minimum_, int64_t maximum_, int64_t count_, int64_t divisible_ = 1) {
    return Range(1, count_, 1, Mode::kRandomLog2, minimum_, maximum_, divisible_);
  }

  /// Returns an iterator to the first element within the range
  Iterator begin() const {
    return Iterator(first, increment, this);
  }

  /// Returns an iterator to the first element *after* the range
  Iterator end() const {
    return Iterator(first + ((last - first)/increment + 1) * increment, increment, this);
  }
};

/// Integer-valued argument - represented as a list of integer-valued ranges
struct IntegerArgument : public KernelArgument {

  //
  // Type definitions
  //

  /// Value type
  struct IntegerValue : public KernelArgument::Value {

    int64_t value;

    //
    // Methods
    //

    IntegerValue(
      int64_t value_ = 0, 
      IntegerArgument const *argument_ = nullptr, 
      bool not_null_ = true
    );

    /// Pretty printer for debugging
    virtual std::ostream &print(std::ostream &out) const;
  };
  
  /// Collection of ranges represent the IntegerArgument's state
  using RangeCollection = std::vector<Range>;

  /// Abstract base class to iterate over values within arguments
  struct IntegerValueIterator : public KernelArgument::ValueIterator {

    //
    // Data members
    //

    RangeCollection::const_iterator range_it;
    Range::Iterator value_it;

    //
    // Methods
    //

    IntegerValueIterator();
    IntegerValueIterator(IntegerArgument const *argument);

    virtual void operator++();
    virtual bool operator==(ValueIterator const &it) const;

    /// Gets the value pointed to
    virtual std::unique_ptr<KernelArgument::Value> at() const;
  };

  //
  // Data members
  //

  /// Set of posible values
  RangeCollection ranges;

  //
  // Methods
  //

  /// Default ctor
  IntegerArgument(
    ArgumentDescription const *description
  ): 
    KernelArgument(description) { }

  virtual bool not_null() const {
    bool _not_null = !ranges.empty();
    return _not_null;
  }

  virtual std::unique_ptr<KernelArgument::ValueIterator> begin() const;
  virtual std::unique_ptr<KernelArgument::ValueIterator> end() const;
};

/////////////////////////////////////////////////////////////////////////////////////////////////

/// Structure defining the data type of tensors
struct TensorArgument : public KernelArgument {

  //
  // Type definitions
  //

  struct TensorDescription {

    /// Data type of elements
    library::NumericTypeID element;

    /// Layout definition
    library::LayoutTypeID layout;

    /// Computed extent
    std::vector<int> extent;

    /// Enables directly specifying stride value used to size tensor
    std::vector<int> stride;

    //
    // Methods
    //

    TensorDescription(
      library::NumericTypeID element_ = library::NumericTypeID::kUnknown,
      library::LayoutTypeID layout_ = library::LayoutTypeID::kUnknown,
      std::vector<int> extent_ = std::vector<int>(),
      std::vector<int> stride_ = std::vector<int>()
    ): 
      element(element_), layout(layout_), extent(extent_), stride(stride_) {}
  };

  using ValueCollection = std::vector<TensorDescription>;

  /// Value structure
  struct TensorValue : public KernelArgument::Value {

    TensorDescription desc;

    //
    // Methods
    //

    TensorValue(
      TensorDescription const &desc_ = TensorDescription(),
      TensorArgument const *argument_ = nullptr, 
      bool not_null_ = true
    );
    
    /// Pretty printer for debugging
    virtual std::ostream &print(std::ostream &out) const;
  };

  /// Abstract base class to iterate over values within arguments
  struct TensorValueIterator : public KernelArgument::ValueIterator {

    //
    // Data members
    //

    ValueCollection::const_iterator value_it;

    //
    // Methods
    //

    TensorValueIterator(TensorArgument const *argument_);

    virtual void operator++();
    virtual bool operator==(ValueIterator const &it) const;

    /// Gets the value pointed to
    virtual std::unique_ptr<KernelArgument::Value> at() const;
  };

  /// Set of possible values
  ValueCollection values;

  //
  // Methods
  //

  /// Default ctor
  TensorArgument(
    ArgumentDescription const *description
  ): 
    KernelArgument(description) { }

  virtual bool not_null() const {
    return !values.empty();
  }

  virtual std::unique_ptr<KernelArgument::ValueIterator> begin() const;
  virtual std::unique_ptr<KernelArgument::ValueIterator> end() const;
};

/////////////////////////////////////////////////////////////////////////////////////////////////

/// Numeric data type
struct EnumeratedTypeArgument : public KernelArgument {

  //
  // Type definitions
  //

  struct EnumeratedTypeValue : public KernelArgument::Value {

    /// Data type of element
    std::string element;

    //
    // Methods
    //

    EnumeratedTypeValue(
      std::string const &element_ = std::string(),
      EnumeratedTypeArgument const *argument_ = nullptr, 
      bool not_null_ = true
    );
    
    /// Pretty printer for debugging
    virtual std::ostream &print(std::ostream &out) const;
  };

  using ValueCollection = std::vector<std::string>;

  /// Abstract base class to iterate over values within arguments
  struct EnumeratedTypeValueIterator : public KernelArgument::ValueIterator {

    //
    // Data members
    //

    ValueCollection::const_iterator value_it;

    //
    // Methods
    //

    EnumeratedTypeValueIterator(EnumeratedTypeArgument const *argument_ = nullptr);

    virtual void operator++();
    virtual bool operator==(ValueIterator const &it) const;

    /// Gets the value pointed to
    virtual std::unique_ptr<KernelArgument::Value> at() const;
  };

  //
  // Data members
  //

  ValueCollection values;

  //
  // Members
  //

  /// Default ctor
  EnumeratedTypeArgument(ArgumentDescription const *description):
    KernelArgument(description) {}

  virtual bool not_null() const {
    return !values.empty();
  }

  virtual std::unique_ptr<KernelArgument::ValueIterator> begin() const;
  virtual std::unique_ptr<KernelArgument::ValueIterator> end() const;
};

/////////////////////////////////////////////////////////////////////////////////////////////////

/// Object storing the space argument values
class ProblemSpace {
public:

  /// Tuple of arguments
  using Problem = std::vector<std::unique_ptr<KernelArgument::Value>>;

  /// Type used to iterator over things
  using IteratorVector = std::vector<std::unique_ptr<KernelArgument::ValueIterator>>;

  /// Iterates over points in the design space
  class Iterator {
  private:

    /// One iterator per argument
    IteratorVector iterators;

  public:

    //
    // Methods
    //

    explicit Iterator();
    Iterator(ProblemSpace const &problem_space);
    Iterator(Iterator &&it);

    // Rule of three
    Iterator(Iterator const &) = delete;
    Iterator &operator=(Iterator const &it) = delete;
    ~Iterator() = default;

    /// Pre-increment - advances to next point in argument range
    void operator++();

    /// Gets the current argument value
    Problem at() const;

    /// Moves iterator to end
    void move_to_end();

    /// Equality operator
    bool operator==(Iterator const &it) const;

    /// Inequality operator
    bool operator!=(Iterator const &it) const {
      return !(*this == it);
    }

    /// Helper to call at() method
    Problem operator*() const {
      return at();
    }

    /// Helper to print iterator state
    std::ostream & print(std::ostream &out) const;

  private:

    /// Helper for recursively constructing iterators
    void construct_(KernelArgument const *argument);
  };

public:

  //
  // Data members
  //

  KernelArgumentVector arguments;

  /// Map of argument names to their position within the argument vector
  std::unordered_map<std::string, size_t> argument_index_map;

public:
  
  //
  // Methods
  //

  /// Default ctor
  ProblemSpace() {}

  /// Constructs a problem space from a vector of arguments. This vector must outlive
  /// the ProblemSpace object, which stores pointers to objects within the
  /// ArgumentDescriptionVector.
  ProblemSpace(ArgumentDescriptionVector const &schema, CommandLine const &cmdline);

  Iterator begin() const;   // returns an iterator to the first point in the range
  Iterator end() const;     // returns an iterator to the first point after the range

  /// Returns the index of an argument by name
  size_t argument_index(char const *name) const;

  /// Gets all argument names as an ordered vector
  std::vector<std::string> argument_names() const;

  /// Returns the number of dimensions of the problem space
  size_t rank() const { return arguments.size(); }
 
private:

  /// Helper for recursively cloning
  void clone_(
    KernelArgumentVector &kernel_args,
    ArgumentDescription const *arg_desc);

  /// Parses command line argument
  void parse_(
    KernelArgument *arg,
    CommandLine const &cmdline);
};

/////////////////////////////////////////////////////////////////////////////////////////////////

/// Lexically casts an argument to an int if it is defined. Returns true if not null.
bool arg_as_int(int &int_value, KernelArgument::Value const *value_ptr);

/// Lexically casts an argument to an int64 if it is defined. Returns true if not null.
bool arg_as_int(int64_t &int_value, KernelArgument::Value const *value_ptr);

/// Lexically casts an argument to an int64 if it is defined. Returns true if not null.
bool arg_as_int(
  int &int_value,
  char const *name,
  ProblemSpace const &problem_space, 
  ProblemSpace::Problem const &problem);

/// Lexically casts an argument to an int64 if it is defined. Returns true if not null.
bool arg_as_int(
  int64_t &int_value,
  char const *name,
  ProblemSpace const &problem_space, 
  ProblemSpace::Problem const &problem);

/// Lexically casts an argument to an int64 if it is defined. Returns true if not null.
bool arg_as_NumericTypeID(library::NumericTypeID &numeric_type, KernelArgument::Value const *value_ptr);

/// Lexically casts an argument to an int64 if it is defined. Returns true if not null.
bool arg_as_NumericTypeID(
  library::NumericTypeID &numeric_type,
  char const *name,
  ProblemSpace const &problem_space, 
  ProblemSpace::Problem const &problem);

/// Lexically casts an argument to an int64 if it is defined. Returns true if not null.
bool arg_as_LayoutTypeID(library::LayoutTypeID &layout_type, KernelArgument::Value const *value_ptr);

/// Lexically casts an argument to an int64 if it is defined. Returns true if not null.
bool arg_as_LayoutTypeID(
  library::LayoutTypeID &layout_type,
  char const *name,
  ProblemSpace const &problem_space, 
  ProblemSpace::Problem const &problem);


/// Lexically casts an argument to an int64 if it is defined. Returns true if not null.
bool arg_as_OpcodeClassID(library::OpcodeClassID &opcode_class, KernelArgument::Value const *value_ptr);

/// Lexically casts an argument to an int64 if it is defined. Returns true if not null.
bool arg_as_OpcodeClassID(
  library::OpcodeClassID &opcode_class,
  char const *name,
  ProblemSpace const &problem_space, 
  ProblemSpace::Problem const &problem);


/// Lexically casts an argument to an int64 if it is defined. Returns true if not null.
bool arg_as_SplitKModeID(library::SplitKMode &split_k_mode, KernelArgument::Value const *value_ptr);

/// Lexically casts an argument to an int64 if it is defined. Returns true if not null.
bool arg_as_SplitKModeID(
  library::SplitKMode &split_k_mode,
  char const *name,
  ProblemSpace const &problem_space, 
  ProblemSpace::Problem const &problem);

/// Lexically casts an argument to an int64 if it is defined. Returns true if not null.
bool arg_as_ConvModeID(library::ConvModeID &conv_mode, KernelArgument::Value const *value_ptr);

/// Lexically casts an argument to an int64 if it is defined. Returns true if not null.
bool arg_as_ConvModeID(
  library::ConvModeID &conv_mode,
  char const *name,
  ProblemSpace const &problem_space, 
  ProblemSpace::Problem const &problem);

/// Lexically casts an argument to an int64 if it is defined. Returns true if not null.
bool arg_as_IteratorAlgorithmID(library::IteratorAlgorithmID &iterator_algorithm, KernelArgument::Value const *value_ptr);

/// Lexically casts an argument to an int64 if it is defined. Returns true if not null.
bool arg_as_IteratorAlgorithmID(
  library::IteratorAlgorithmID &iterator_algorithm,
  char const *name,
  ProblemSpace const &problem_space, 
  ProblemSpace::Problem const &problem);


/// Lexically casts an argument to an int64 if it is defined. Returns true if not null.
bool arg_as_ProviderID(library::Provider &provider, KernelArgument::Value const *value_ptr);

/// Lexically casts an argument to an int64 if it is defined. Returns true if not null.
bool arg_as_ProviderID(
  library::Provider &provider,
  char const *name,
  ProblemSpace const &problem_space, 
  ProblemSpace::Problem const &problem);

/// Lexically casts an argument to a given type stored in a byte array. Returns true if not null.
bool arg_as_scalar(
  std::vector<uint8_t> &bytes,
  library::NumericTypeID numeric_type, 
  KernelArgument::Value const *value_ptr);

/// Lexically casts an argument to a given type stored in a byte array. Returns true if not null.
bool arg_as_scalar(
  std::vector<uint8_t> &bytes,
  library::NumericTypeID numeric_type, 
  char const *name, 
  ProblemSpace const &problem_space, 
  ProblemSpace::Problem const &problem);

/// Returns true if a tensor description satisfies a `tensor` value
bool tensor_description_satisfies(
  library::TensorDescription const &tensor_desc,
  TensorArgument::TensorValue const *value_ptr);

/// Returns true if a tensor description satisfies a `tensor` value
bool tensor_description_satisfies(
  library::TensorDescription const &tensor_desc,
  char const *name, 
  ProblemSpace const &problem_space, 
  ProblemSpace::Problem const &problem);


/// Returns true if a conv kind satisfies the value
bool conv_kind_satisfies(
  library::ConvKind const &conv_kind,
  EnumeratedTypeArgument::EnumeratedTypeValue const *value_ptr);

/// Returns true if a conv kind satisfies the value
bool conv_kind_satisfies(
  library::ConvKind const &conv_kind,
  char const *name, 
  ProblemSpace const &problem_space, 
  ProblemSpace::Problem const &problem);

/// Returns true if a iterator algorithm satisfies the value
bool iterator_algorithm_satisfies(
  library::IteratorAlgorithmID const &iterator_algorithm,
  EnumeratedTypeArgument::EnumeratedTypeValue const *value_ptr);

/// Returns true if a iterator algorithm satisfies the value
bool iterator_algorithm_satisfies(
  library::IteratorAlgorithmID const &iterator_algorithm,
  char const *name, 
  ProblemSpace const &problem_space, 
  ProblemSpace::Problem const &problem);

/////////////////////////////////////////////////////////////////////////////////////////////////

} // namespace profiler
} // namespace cutlass

////////////////////////////////////////////////////////////////////////////////////////////////