Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Generalize AddressSpace::read<T>() to return a Remote<T>
[simgrid.git] / src / mc / AddressSpace.hpp
1 /* Copyright (c) 2008-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef SIMGRID_MC_ADDRESS_SPACE_H
8 #define SIMGRID_MC_ADDRESS_SPACE_H
9
10 #include <cassert>
11 #include <cstddef>
12 #include <cstdint>
13 #include <cstring>
14 #include <type_traits>
15
16 #include "src/mc/mc_forward.hpp"
17 #include "src/mc/RemotePtr.hpp"
18
19 namespace simgrid {
20 namespace mc {
21
22 /** Process index used when no process is available
23  *
24  *  The expected behaviour is that if a process index is needed it will fail.
25  * */
26 const int ProcessIndexMissing = -1;
27
28 /** Process index used when we don't care about the process index
29  * */
30 const int ProcessIndexDisabled = -2;
31
32 /** Constant used when any process will do.
33  *
34  *  This is is index of the first process.
35  */
36 const int ProcessIndexAny = 0;
37
38 /** Options for read operations
39  *
40  *  This is a set of flags managed with bitwise operators. Only the
41  *  meaningful operations are defined: addition, conversions to/from
42  *  integers are not allowed.
43  */
44 class ReadOptions {
45   std::uint32_t value_;
46   constexpr explicit ReadOptions(std::uint32_t value) : value_(value) {}
47 public:
48   constexpr ReadOptions() : value_(0) {}
49
50   constexpr operator bool() const { return value_ != 0; }
51   constexpr bool operator!() const { return value_ == 0; }
52
53   constexpr ReadOptions operator|(ReadOptions const& that) const
54   {
55     return ReadOptions(value_ | that.value_);
56   }
57   constexpr ReadOptions operator&(ReadOptions const& that) const
58   {
59     return ReadOptions(value_ & that.value_);
60   }
61   constexpr ReadOptions operator^(ReadOptions const& that) const
62   {
63     return ReadOptions(value_ ^ that.value_);
64   }
65   constexpr ReadOptions operator~() const
66   {
67     return ReadOptions(~value_);
68   }
69
70   ReadOptions& operator|=(ReadOptions const& that)
71   {
72     value_ |= that.value_;
73     return *this;
74   }
75   ReadOptions& operator&=(ReadOptions const& that)
76   {
77     value_ &= that.value_;
78     return *this;
79   }
80   ReadOptions& operator^=(ReadOptions const& that)
81   {
82     value_ &= that.value_;
83     return *this;
84   }
85
86   /** Copy the data to the given buffer */
87   static constexpr ReadOptions none() { return ReadOptions(0); }
88
89   /** Allows to return a pointer to another buffer where the data is
90    *  available instead of copying the data into the buffer
91    */
92   static constexpr ReadOptions lazy() { return ReadOptions(1); }
93 };
94
95 /** A value from another process
96  *
97  *  This represents a value from another process:
98  *
99  *  * constructor/destructor are disabled;
100  *
101  *  * raw memory copy (std::memcpy) is used to copy Remote<T>;
102  *
103  *  * raw memory comparison is used to compare them;
104  *
105  *  * when T is a trivial type, Remote is convertible to a T.
106  *
107  *  We currently only handle the case where the type has the same layout
108  *  in the current process and in the target process: we don't handle
109  *  cross-architecture (such as 32-bit/64-bit access).
110  */
111 template<class T>
112 class Remote {
113 private:
114   // If we use a union, it won't work with abstract types:
115   char buffer[sizeof(T)];
116 public:
117   // HACK, some code currently cast this to T* which is **not** legal.
118   void*       data() { return buffer; }
119   const void* data() const { return buffer; }
120   constexpr std::size_t size() const { return sizeof(T); }
121   operator T() const {
122     static_assert(std::is_trivial<T>::value, "Cannot convert non trivial type");
123     T res;
124     std::memcpy(&res, buffer, sizeof(T));
125     return res;
126   }
127   Remote() {}
128   Remote(T const& x)
129   {
130     std::memcpy(&x, buffer, sizeof(T));
131   }
132   Remote& operator=(T const& x)
133   {
134     std::memcpy(&x, buffer, sizeof(T));
135   }
136 };
137
138 /** A given state of a given process (abstract base class)
139  *
140  *  Currently, this might either be:
141  *
142  *  * the current state of an existing process;
143  *
144  *  * a snapshot.
145  */
146 class AddressSpace {
147 private:
148   Process* process_;
149 public:
150   AddressSpace(Process* process) : process_(process) {}
151   virtual ~AddressSpace();
152
153   simgrid::mc::Process* process() const { return process_; }
154
155   /** Read data from the address space
156    *
157    *  @param buffer        target buffer for the data
158    *  @param size          number of bytes
159    *  @param address       remote source address of the data
160    *  @param process_index which process (used for SMPI privatization)
161    *  @param options
162    */
163   virtual const void* read_bytes(void* buffer, std::size_t size,
164     RemotePtr<void> address, int process_index = ProcessIndexAny,
165     ReadOptions options = ReadOptions::none()) const = 0;
166
167   /** Read a given data structure from the address space */
168   template<class T> inline
169   void read(T *buffer, RemotePtr<T> ptr, int process_index = ProcessIndexAny)
170   {
171     this->read_bytes(buffer, sizeof(T), ptr, process_index);
172   }
173
174   template<class T> inline
175   void read(Remote<T>& buffer, RemotePtr<T> ptr, int process_index = ProcessIndexAny)
176   {
177     this->read_bytes(buffer.data(), sizeof(T), ptr, process_index);
178   }
179
180   /** Read a given data structure from the address space */
181   template<class T> inline
182   Remote<T> read(RemotePtr<T> ptr, int process_index = ProcessIndexMissing)
183   {
184     Remote<T> res;
185     this->read_bytes(&res, sizeof(T), ptr, process_index);
186     return res;
187   }
188 };
189
190 }
191 }
192
193 #endif