OpenASIP  2.0
MemorySystem.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2012 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished 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 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file MemorySystem.cc
26  *
27  * Definition of MemorySystem class.
28  *
29  * @author Jussi Nykänen 2004 (nykanen-no.spam-cs.tut.fi)
30  * @author Pekka Jääskeläinen 2005,2009-2012
31  * @note rating: red
32  */
33 
34 #include <string>
35 #include <algorithm>
36 
37 #include "MemorySystem.hh"
38 #include "Machine.hh"
39 #include "AddressSpace.hh"
40 #include "Memory.hh"
41 #include "MapTools.hh"
42 #include "Application.hh"
43 #include "SequenceTools.hh"
44 #include "Conversion.hh"
45 
46 using std::string;
47 using namespace TTAMachine;
48 
49 /**
50  * Constructor.
51  *
52  * @param machine Machine in which MemorySystem belongs to.
53  */
55  machine_(&machine) {
56 }
57 
58 /**
59  * Deletes all shared memory instances.
60  *
61  * Must be called only once for all MemorySystems sharing the memories.
62  */
63 void
65  sharedMemories_.clear();
66 }
67 
68 /**
69  * Destructor.
70  *
71  * Deletes all the Memory instances.
72  */
74  localMemories_.clear();
76 }
77 
78 /**
79  * Adds AddressSpace and corresponding memory to data structure.
80  *
81  * @param as AddressSpace to be added.
82  * @param mem Memory to be added. Becomes property of the MemorySystem,
83  * that is, MemorySystems is responsible for deallocating it.
84  * @param shared If the given Memory instace is shared by multiple
85  * MemorySystems (cores).
86  * @exception IllegalRegistration If the AddressSpace does not belong to the
87  * target machine.
88  */
89 void
91  const AddressSpace& as, MemoryPtr mem, bool shared) {
93  if (!nav.hasItem(as.name())) {
94  string msg = "Address space doesn't belong to the target machine";
95  throw IllegalRegistration(__FILE__, __LINE__, __func__, msg);
96  }
97 
98  memories_[&as] = mem;
99 
100  if (shared) {
101  sharedMemories_.push_back(mem);
102  } else {
103  localMemories_.push_back(mem);
104  }
105  memoryList_.push_back(mem);
106 }
107 
108 bool
109 MemorySystem::hasMemory(const TCEString& aSpaceName) const {
110  MemoryMap::const_iterator iter = memories_.begin();
111  while (iter != memories_.end()) {
112  const AddressSpace& space = *((*iter).first);
113  if (space.name() == aSpaceName) {
114  return true;
115  }
116  ++iter;
117  }
118  return false;
119 }
120 
121 /**
122  * In case two TTA simulation models share memories in an heterogeneous
123  * multicore simulation, this method should be called after initializing
124  * the simulation frontends between all such pairs to fix the memory model
125  * references to point to the same memory model.
126  *
127  * The matching is done by the address space name. The shared address
128  * space must have the 'shared' attribute set.
129  *
130  * @fixme An untested method.
131  */
132 void
134  for (std::size_t i = 0; i < other.memoryCount(); ++i) {
135  const AddressSpace& as = other.addressSpace(i);
136  if (!as.isShared() || !hasMemory(as.name())) continue;
137 
139  AddressSpace* thisAS = nav.item(as.name());
140  /// remove the replaced memory as it should not be controlled
141  /// by this MemorySystem anymore
142  memoryList_.erase(
143  std::find(
144  memoryList_.begin(), memoryList_.end(),
145  memories_[thisAS]));
146 
147  sharedMemories_.erase(
148  std::find(
149  sharedMemories_.begin(), sharedMemories_.end(),
150  memories_[thisAS]));
151 
152  replacedSharedMemories_.push_back(memories_[thisAS]);
153 
154  memories_[thisAS] = other.memory(i);
155 
156  }
157 }
158 
159 /**
160  * Returns Memory instance bound to the given AddressSpace.
161  *
162  * The returned memory instance is writable.
163  *
164  * @param as AddressSpace in which memory is asked.
165  * @return Memory bound to AddressSpace.
166  * @exception InstanceNotFound If no memory is bound to the given
167  * AddressSpace.
168  */
171  return memory(as.name());
172 }
173 
174 /**
175  * Returns Memory instance bound to an address space with the given name.
176  *
177  * The returned memory instance is writable.
178  *
179  * @param addressSpaceName The name of the address space.
180  * @return Memory bound to AddressSpace.
181  * @exception InstanceNotFound If no memory is found with the name.
182  */
184 MemorySystem::memory(const std::string& addressSpaceName) {
185  MemoryMap::const_iterator iter = memories_.begin();
186  while (iter != memories_.end()) {
187  const AddressSpace& space = *((*iter).first);
188  MemoryPtr mem = ((*iter).second);
189  if (space.name() == addressSpaceName) {
190  return mem;
191  }
192  ++iter;
193  }
194  string msg = "No memory model found for address space " + addressSpaceName;
195  throw InstanceNotFound(__FILE__, __LINE__, __func__, msg);
196 }
197 
198 /**
199  * Returns Memory instance bound to the given AddressSpace.
200  *
201  * The returned memory instance is read-only.
202  *
203  * @param as AddressSpace in which memory is asked.
204  * @return Memory bound to AddressSpace.
205  * @exception InstanceNotFound If no memory is bound to the given
206  * AddressSpace.
207  * @todo These methods need to be changed to compare with the AS attributes
208  * not with AS pointer!
209  */
212  MemoryMap::const_iterator iter = memories_.find(&as);
213  if (iter == memories_.end()) {
214  string msg = "No memory found for address space " + as.name();
215  throw InstanceNotFound(__FILE__, __LINE__, __func__, msg);
216  }
217  return ((*iter).second);
218 }
219 
220 /**
221  * Returns the count of Memory instances in this memory system.
222  *
223  * @return The count of Memory instances.
224  */
225 unsigned int
227  return memories_.size();
228 }
229 
230 /**
231  * Returns the ith Memory instance in the memory system.
232  *
233  * @return The Memory instance.
234  * @exception OutOfRange if i is out of bounds.
235  */
237 MemorySystem::memory(unsigned int i) {
238  if (i > memories_.size() - 1) {
239  const string msg =
240  "Index out of bounds. Count of memories is " +
242  throw OutOfRange(__FILE__, __LINE__, __func__, msg);
243  }
244 
245  unsigned int foundCount = 0;
246  MemoryMap::const_iterator iter = memories_.begin();
247  while (iter != memories_.end() && foundCount < i) {
248  ++iter;
249  ++foundCount;
250  }
251  return ((*iter).second);
252 }
253 
254 /**
255  * Returns the address space of the ith Memory instance in the memory system.
256  *
257  * @return Address Space of the memory instance.
258  * @exception OutOfRange if i is out of bounds.
259  */
260 const AddressSpace&
261 MemorySystem::addressSpace(unsigned int i) {
262  if (i > memories_.size() - 1) {
263  const string msg =
264  "Index out of bounds. Count of memories is " +
266  throw OutOfRange(__FILE__, __LINE__, __func__, msg);
267  }
268 
269  unsigned int foundCount = 0;
270  MemoryMap::const_iterator iter = memories_.begin();
271  while (iter != memories_.end() && foundCount < i) {
272  ++iter;
273  ++foundCount;
274  }
275  return *((*iter).first);
276 }
277 
278 /**
279  * Returns the address space with the given name.
280  *
281  * @param name The name of the address space.
282  * @return AddressSpace.
283  * @exception InstanceNotFound if the address space is not found.
284  */
285 const AddressSpace&
286 MemorySystem::addressSpace(const std::string& name) {
287  MemoryMap::const_iterator iter = memories_.begin();
288  while (iter != memories_.end()) {
289  if ((*iter).first->name() == name)
290  return *((*iter).first);
291  ++iter;
292  }
293 
294  throw InstanceNotFound(
295  __FILE__, __LINE__, __func__,
296  "Address space with the given name not found.");
297 }
MemorySystem::sharedMemories_
MemoryContainer sharedMemories_
All memories shared between multiple cores.
Definition: MemorySystem.hh:104
MemorySystem::addAddressSpace
void addAddressSpace(const TTAMachine::AddressSpace &as, MemoryPtr mem, bool shared=true)
Definition: MemorySystem.cc:90
TTAMachine::Component::name
virtual TCEString name() const
Definition: MachinePart.cc:125
machine
TTAMachine::Machine * machine
the architecture definition of the estimated processor
Definition: EstimatorCmdLineUI.cc:59
TTAMachine::AddressSpace
Definition: AddressSpace.hh:51
Memory.hh
OutOfRange
Definition: Exception.hh:320
MapTools.hh
SequenceTools.hh
MemorySystem::machine_
const TTAMachine::Machine * machine_
Machine in which MemorySystem belongs to.
Definition: MemorySystem.hh:98
AddressSpace.hh
MemorySystem::MemorySystem
MemorySystem(const TTAMachine::Machine &machine)
Definition: MemorySystem.cc:54
MemorySystem::~MemorySystem
virtual ~MemorySystem()
Definition: MemorySystem.cc:73
MemorySystem.hh
MemorySystem::memoryConst
const MemoryPtr memoryConst(const TTAMachine::AddressSpace &as) const
Definition: MemorySystem.cc:211
Conversion::toString
static std::string toString(const T &source)
MemorySystem::MemoryPtr
boost::shared_ptr< Memory > MemoryPtr
Definition: MemorySystem.hh:57
TTAMachine::AddressSpace::isShared
virtual bool isShared() const
Definition: AddressSpace.hh:72
MemorySystem::memoryCount
unsigned int memoryCount() const
Definition: MemorySystem.cc:226
MemorySystem::deleteSharedMemories
void deleteSharedMemories()
Definition: MemorySystem.cc:64
MemorySystem::localMemories_
MemoryContainer localMemories_
All private/local memories used by a single core only.
Definition: MemorySystem.hh:106
Conversion.hh
MemorySystem::shareMemoriesWith
void shareMemoriesWith(MemorySystem &other)
Definition: MemorySystem.cc:133
TTAMachine::Machine::Navigator::hasItem
bool hasItem(const std::string &name) const
Application.hh
__func__
#define __func__
Definition: Application.hh:67
Machine.hh
TTAMachine::Machine::addressSpaceNavigator
virtual AddressSpaceNavigator addressSpaceNavigator() const
Definition: Machine.cc:392
MemorySystem
Definition: MemorySystem.hh:55
MemorySystem::hasMemory
bool hasMemory(const TCEString &aSpaceName) const
Definition: MemorySystem.cc:109
IllegalRegistration
Definition: Exception.hh:532
MemorySystem::replacedSharedMemories_
MemoryContainer replacedSharedMemories_
Shared memories which have been replaced with a shared memory from another core. Just for garbage rem...
Definition: MemorySystem.hh:109
MemorySystem::memoryList_
MemoryContainer memoryList_
List of all the memories for faster traversal.
Definition: MemorySystem.hh:102
TCEString
Definition: TCEString.hh:53
MemorySystem::memory
MemoryPtr memory(const TTAMachine::AddressSpace &as)
Definition: MemorySystem.cc:170
TTAMachine::Machine::Navigator::item
ComponentType * item(int index) const
MemorySystem::memories_
MemoryMap memories_
Contains all memories indexed by AddressSpaces.
Definition: MemorySystem.hh:100
TTAMachine
Definition: Assembler.hh:48
MemorySystem::addressSpace
const TTAMachine::AddressSpace & addressSpace(unsigned int i)
Definition: MemorySystem.cc:261
TTAMachine::Machine::Navigator
Definition: Machine.hh:186
InstanceNotFound
Definition: Exception.hh:304
TTAMachine::Machine
Definition: Machine.hh:73