SST/macro
global_builtin_array.h
Go to the documentation of this file.
1 /*
2  * This file is part of SST/macroscale:
3  * The macroscale architecture simulator from the SST suite.
4  * Copyright (c) 2009 Sandia Corporation.
5  * This software is distributed under the BSD License.
6  * Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
7  * the U.S. Government retains certain rights in this software.
8  * For more information, see the LICENSE file in the top
9  * SST/macroscale directory.
10  */
11 
12 #ifndef SSTMAC_SOFTWARE_PROCESS_GLOBAL_BUILTIN_ARRAY_H_INCLUDED
13 #define SSTMAC_SOFTWARE_PROCESS_GLOBAL_BUILTIN_ARRAY_H_INCLUDED
14 
16 #include <cstring>
17 
18 namespace sstmac {
19 namespace sw {
20 
21 template<typename T, int N>
23 {
24 
25  private:
26  typedef spkt_unordered_map<long, T*> val_map;
27  val_map vals_;
28  T* init_;
29 
30  public:
31  typedef typename val_map::const_iterator const_iterator;
32  typedef T static_arr[N];
33 
34  explicit
36  init_(nullptr) {
37  }
38 
39  explicit
41  init_ = new T[N];
42  ::memcpy(init_, init, N * sizeof(T));
43  }
44 
47  "copy constructor should never be called for primitive global");
48  }
49 
50  virtual
52  for (auto& pair : vals_){
53  T* t = pair.second;
54  delete t;
55  }
56  if (init_) delete[] init_;
57  }
58 
59  void
60  print_all() const {
61  typename val_map::const_iterator it = vals_.begin();
62  for (; it != vals_.end(); ++it) {
63  std::cout << it->second << std::endl;
64  }
65  }
66 
67  virtual T*
68  get_val() const {
70  val_map& vals = const_cast<val_map&> (vals_);
71  if (ptxt != process_context::none) {
72  typename val_map::iterator it = vals.find(ptxt);
73  if (it == vals.end()) {
74  T* ret = new T[N];
75  vals[ptxt] = ret;
76  if (init_) {
77  ::memcpy(ret, init_, N * sizeof(T));
78  }
79  return ret;
80  }
81  else {
82  T* r = it->second;
83  return r;
84  }
85  }
87  "getting static array value with no process context");
88  }
89 
90  virtual std::string
91  to_string() const {
92  std::stringstream ss;
93  ss << get_val();
94  return ss.str();
95  }
96 
97  template<typename U>
98  T*
99  operator =(const U& b) {
100  T* myval = get_val();
101  myval = (T*) b;
102  return myval;
103  }
104 
105  template<typename U>
106  bool
107  operator ==(const U& b) const {
108  T myval = get_val();
109  T otherval = (T) b;
110  return myval == otherval;
111  }
112 
113  template<typename U>
114  bool
115  operator !=(const U& b) const {
116  T myval = get_val();
117  T otherval = (T) b;
118  return myval != otherval;
119  }
120 
121  template <typename U>
122  operator U() {
123  U ret = (U) get_val();
124  return ret;
125  }
126 
127  T*
128  operator ++(int dummy) {
129  T* myval = get_val();
130  return myval++;
131  }
132 
133  T*
134  operator --(int dummy) {
135  T* myval = get_val();
136  return myval--;
137  }
138 
139  T*
141  T* myval = get_val();
142  return ++myval;
143  }
144 
145  T*
147  T* myval = get_val();
148  return --myval;
149  }
150 
151  operator void*() const {
152  return (void*) get_val();
153  }
154 
155  operator double*() const {
156  return (double*) get_val();
157  }
158 
159  operator int*() const {
160  return (int*) get_val();
161  }
162 
163  operator bool*() const {
164  return (bool*) get_val();
165  }
166 
167  operator size_t*() const {
168  return (size_t*) get_val();
169  }
170 
171  operator long*() const {
172  return (long*) get_val();
173  }
174 
175  operator long long*() const {
176  return (long long*) get_val();
177  }
178 
179  template<typename U>
180  T*
181  operator +(const U& b) const {
182  return get_val() + b;
183  }
184 
185  template<typename U>
186  T*
187  operator -(const U& b) const {
188  return get_val() - b;
189  }
190 
191 
192 
193  T&
195  return *get_val();
196  }
197 
198  T&
199  operator[](int idx) {
200  T* tarr = get_val();
201  return tarr[idx];
202  }
203 
204  T&
205  operator[](long idx) {
206  T* tarr = get_val();
207  return tarr[idx];
208  }
209 
210  T**
211  operator&() const {
212  return &get_val();
213  }
214 
215  template<typename U>
216  bool
217  operator >(const U& b) {
218  T* myval = get_val();
219  T* otherval = (T*) b;
220  return myval > otherval;
221  }
222 
223  template<typename U>
224  bool
225  operator <(const U& b) {
226  T* myval = get_val();
227  T* otherval = (T) b;
228  return myval < otherval;
229  }
230 
231  const_iterator
232  begin() const {
233  return vals_[0].begin();
234  }
235  const_iterator
236  end() const {
237  return vals_[0].end();
238  }
239 
240 };
241 
242 
243 }
244 }
245 #endif
246 
process_context current_context() const
void * memcpy(const VariablePtr< T > &dst, const VariablePtr< T > &src, size_t size)
Definition: variable.h:381
An error indicating some format was not correct.
Definition: errors.h:131
parallel_runtime * init()
virtual std::string to_string() const
SUMI = Simulator unified messagine interface It is also the name for a solid ink in Japanese - i...
spkt_unordered_map< long, T * > val_map
sstmac_global_builtin_arr(const sstmac_global_builtin_arr< T, N > &other)
#define spkt_throw_printf(exc, template_str,...)
Definition: errors.h:37