File: param.h

package info (click to toggle)
nvidia-cuda-samples 11.8~dfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: bookworm
  • size: 236,184 kB
  • sloc: cpp: 101,805; makefile: 51,859; xml: 15,488; ansic: 7,791; python: 74; sh: 67
file content (236 lines) | stat: -rw-r--r-- 6,066 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
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
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * 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.
 *  * Neither the name of NVIDIA CORPORATION 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 ``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 OWNER 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.
 */

/*
 Simple parameter system
 sgreen@nvidia.com 4/2001
*/

#ifndef PARAM_H
#define PARAM_H

#include <iomanip>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>

// base class for named parameter
class ParamBase {
 public:
  ParamBase(const char *name) : m_name(name) {}
  virtual ~ParamBase() {}

  std::string &GetName() { return m_name; }

  virtual float GetFloatValue() = 0;
  virtual int GetIntValue() = 0;
  virtual std::string GetValueString() = 0;

  virtual void Reset() = 0;
  virtual void Increment() = 0;
  virtual void Decrement() = 0;

  virtual float GetPercentage() = 0;
  virtual void SetPercentage(float p) = 0;

  virtual void Write(std::ostream &stream) = 0;
  virtual void Read(std::istream &stream) = 0;

  virtual bool IsList() = 0;

 protected:
  std::string m_name;
};

// derived class for single-valued parameter
template <class T>
class Param : public ParamBase {
 public:
  Param(const char *name, T value = 0, T min = 0, T max = 10000, T step = 1,
        T *ptr = 0)
      : ParamBase(name),
        m_default(value),
        m_min(min),
        m_max(max),
        m_step(step),
        m_precision(3) {
    if (ptr) {
      m_ptr = ptr;
    } else {
      m_ptr = &m_value;
    }

    *m_ptr = value;
  }
  ~Param() {}

  T GetValue() const { return *m_ptr; }
  T SetValue(const T value) { *m_ptr = value; }

  float GetFloatValue() { return (float)*m_ptr; }
  int GetIntValue() { return (int)*m_ptr; }

  std::string GetValueString() {
    std::ostringstream ost;
    ost << std::setprecision(m_precision) << std::fixed;
    ost << *m_ptr;
    return ost.str();
  }

  void SetPrecision(int x) { m_precision = x; }

  float GetPercentage() { return (*m_ptr - m_min) / (float)(m_max - m_min); }

  void SetPercentage(float p) { *m_ptr = (T)(m_min + p * (m_max - m_min)); }

  void Reset() { *m_ptr = m_default; }

  void Increment() {
    *m_ptr += m_step;

    if (*m_ptr > m_max) {
      *m_ptr = m_max;
    }
  }

  void Decrement() {
    *m_ptr -= m_step;

    if (*m_ptr < m_min) {
      *m_ptr = m_min;
    }
  }

  void Write(std::ostream &stream) {
    stream << m_name << " " << *m_ptr << '\n';
  }
  void Read(std::istream &stream) { stream >> m_name >> *m_ptr; }

  bool IsList() { return false; }

 private:
  T m_value;
  T *m_ptr;  // pointer to value declared elsewhere
  T m_default, m_min, m_max, m_step;
  int m_precision;  // number of digits after decimal point in string output
};

const Param<int> dummy("error");

// list of parameters
class ParamList : public ParamBase {
 public:
  ParamList(const char *name = "") : ParamBase(name) { active = true; }
  ~ParamList() {}

  float GetFloatValue() { return 0.0f; }
  int GetIntValue() { return 0; }

  void AddParam(ParamBase *param) {
    m_params.push_back(param);
    m_map[param->GetName()] = param;
    m_current = m_params.begin();
  }

  // look-up parameter based on name
  ParamBase *GetParam(char *name) {
    ParamBase *p = m_map[name];

    if (p) {
      return p;
    } else {
      return (ParamBase *)&dummy;
    }
  }

  ParamBase *GetParam(int i) { return m_params[i]; }

  ParamBase *GetCurrent() { return *m_current; }

  int GetSize() { return (int)m_params.size(); }

  std::string GetValueString() { return m_name; }

  // functions to traverse list
  void Reset() { m_current = m_params.begin(); }

  void Increment() {
    m_current++;

    if (m_current == m_params.end()) {
      m_current = m_params.begin();
    }
  }

  void Decrement() {
    if (m_current == m_params.begin()) {
      m_current = m_params.end() - 1;
    } else {
      m_current--;
    }
  }

  float GetPercentage() { return 0.0f; }
  void SetPercentage(float /*p*/) {}

  void Write(std::ostream &stream) {
    stream << m_name << '\n';

    for (std::vector<ParamBase *>::const_iterator p = m_params.begin();
         p != m_params.end(); ++p) {
      (*p)->Write(stream);
    }
  }

  void Read(std::istream &stream) {
    stream >> m_name;

    for (std::vector<ParamBase *>::const_iterator p = m_params.begin();
         p != m_params.end(); ++p) {
      (*p)->Read(stream);
    }
  }

  bool IsList() { return true; }

  void ResetAll() {
    for (std::vector<ParamBase *>::const_iterator p = m_params.begin();
         p != m_params.end(); ++p) {
      (*p)->Reset();
    }
  }

 protected:
  bool active;
  std::vector<ParamBase *> m_params;
  std::map<std::string, ParamBase *> m_map;
  std::vector<ParamBase *>::const_iterator m_current;
};

#endif