Optimized Belief Propagation (CPU and GPU)
InputSignature.cpp
Go to the documentation of this file.
1 /*
2 Copyright (C) 2024 Scott Grauer-Gray
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
29 #include <charconv>
30 #include "InputSignature.h"
31 
32 //constructor to generate evaluation input signature from string array with
33 //strings corresponding to each part
34 InputSignature::InputSignature(const std::array<std::string_view, 3>& in_sig_strings) {
35  const std::map<std::string_view, unsigned int> datatype_str_to_size{
36  {run_environment::kDataSizeToNameMap.at(sizeof(float)), sizeof(float)},
37  {run_environment::kDataSizeToNameMap.at(sizeof(double)), sizeof(double)},
38  {run_environment::kDataSizeToNameMap.at(sizeof(short)), sizeof(short)}};
39  data_type_size_ = datatype_str_to_size.at(in_sig_strings[run_eval::kRunInputDatatypeIdx]);
40  int num_from_string{};
41  std::from_chars(
42  in_sig_strings[run_eval::kRunInputNumInputIdx].data(),
43  in_sig_strings[run_eval::kRunInputNumInputIdx].data() + in_sig_strings[run_eval::kRunInputNumInputIdx].size(),
44  num_from_string);
45  eval_set_num_ = num_from_string;
46  use_templated_loop_iters_ =
47  (in_sig_strings[run_eval::kRunInputLoopItersTemplatedIdx] ==
48  run_eval::kBoolValFalseTrueDispStr[1]) ? true : false;
49 }
50 
51 //constructor to generate evaluation input signature from parameters
52 //corresponding to each part
54  std::optional<unsigned int> data_type_size,
55  std::optional<unsigned int> eval_set_num,
56  std::optional<bool> use_templated_loop_iters) :
57  data_type_size_(data_type_size),
58  eval_set_num_(eval_set_num),
59  use_templated_loop_iters_(use_templated_loop_iters) {}
60 
61 //less than operator for comparing evaluation input signatures
62 //so they can be ordered
63 //operator needed to support ordering since input signature
64 //is used as std::map key and also for evaluation output order
65 //don't use input signature parts where one of the comparison sides
66 //has "no value" which corresponds to "any" for the input signature
67 bool InputSignature::operator<(const InputSignature& rhs) const {
68  if (((data_type_size_) && (rhs.data_type_size_)) &&
69  ((*data_type_size_ != *rhs.data_type_size_))) {
70  //compare datatype
71  //order is float, double, half
72  //define mapping of datatype string to value for comparison
73  const std::map<unsigned int, unsigned int> datatype_size_to_order_num{
74  {sizeof(float), 0},
75  {sizeof(double), 1},
76  {sizeof(short), 2}};
77  return (datatype_size_to_order_num.at(*data_type_size_) <
78  datatype_size_to_order_num.at(*rhs.data_type_size_));
79  }
80  else if (((eval_set_num_) && (rhs.eval_set_num_)) &&
81  ((*eval_set_num_ != *rhs.eval_set_num_))) {
82  //compare evaluation data number
83  //ordering is as expected by numeric value (such as 0 < 1)
84  return (eval_set_num_ < rhs.eval_set_num_);
85  }
86  else if ((use_templated_loop_iters_ && rhs.use_templated_loop_iters_) &&
87  ((*use_templated_loop_iters_ != *rhs.use_templated_loop_iters_))) {
88  //compare whether or not using templated iter count
89  //order is using templated iter count followed by not using templated iter count
90  if (*use_templated_loop_iters_ == true) { return true; /* a < b is true */ }
91  }
92  return false; /* a <= b is false */
93 }
94 
95 //equality operator for comparing evaluation input signatures
96 //only compare inputs with valid values...no value corresponds to "any value"
97 //for part of input signature so only consider parts where both have valid
98 //values
100  return (std::tie(data_type_size_, eval_set_num_, use_templated_loop_iters_) ==
101  std::tie(rhs.data_type_size_, rhs.eval_set_num_, rhs.use_templated_loop_iters_));
102 }
103 
104 //alternate "equal" operator where an attribute is considered "equal"
105 //in cases where one side is "ANY" for the attribute as indicated
106 //by "no value" for std::optional object
108  return (std::tie(rhs.data_type_size_ ? data_type_size_ : std::optional<unsigned int>(),
109  rhs.eval_set_num_ ? eval_set_num_ : std::optional<unsigned int>(),
110  rhs.use_templated_loop_iters_ ? use_templated_loop_iters_ : std::optional<bool>()) ==
111  std::tie(data_type_size_ ? rhs.data_type_size_ : std::optional<unsigned int>(),
112  eval_set_num_ ? rhs.eval_set_num_ : std::optional<unsigned int>(),
113  use_templated_loop_iters_ ? rhs.use_templated_loop_iters_ : std::optional<bool>()));
114 }
Declares class for defining input signature for evaluation run that consists of evaluation set number...
Class defines input signature for evaluation run that contains evaluation set number,...
bool operator==(const InputSignature &rhs) const
Equality operator for comparing evaluation input signatures.
bool operator<(const InputSignature &rhs) const
Less than operator for comparing evaluation input signatures so they can be ordered Operator needed ...
InputSignature(const std::array< std::string_view, 3 > &in_sig_strings)
Constructor to generate evaluation input signature from string array with strings corresponding to ea...
bool EqualsUsingAny(const InputSignature &rhs) const
Alternate "equal" operator where an attribute is considered "equal" in cases where one side is "ANY" ...
const std::map< std::size_t, std::string_view > kDataSizeToNameMap
Mapping from data size to data type string.
constexpr std::size_t kRunInputNumInputIdx
constexpr std::size_t kRunInputLoopItersTemplatedIdx
constexpr std::size_t kRunInputDatatypeIdx
constexpr std::array< std::string_view, 2 > kBoolValFalseTrueDispStr
Define string for display of "true" and "false" values of bool value.