Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
75fbcae0c7c2d89dc9856f5004ab4557ff199505
[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 <type_traits>
14
15 #include "src/mc/mc_forward.hpp"
16 #include "src/mc/RemotePtr.hpp"
17
18 namespace simgrid {
19 namespace mc {
20
21 /** Process index used when no process is available
22  *
23  *  The expected behaviour is that if a process index is needed it will fail.
24  * */
25 const int ProcessIndexMissing = -1;
26
27 /** Process index used when we don't care about the process index
28  * */
29 const int ProcessIndexDisabled = -2;
30
31 /** Constant used when any process will do.
32  *
33  *  This is is index of the first process.
34  */
35 const int ProcessIndexAny = 0;
36
37 /** Options for read operations
38  *
39  *  This is a set of flags managed with bitwise operators. Only the
40  *  meaningful operations are defined: addition, conversions to/from
41  *  integers are not allowed.
42  */
43 class ReadOptions {
44   std::uint32_t value_;
45   constexpr explicit ReadOptions(std::uint32_t value) : value_(value) {}
46 public:
47   constexpr ReadOptions() : value_(0) {}
48
49   constexpr operator bool() const { return value_ != 0; }
50   constexpr bool operator!() const { return value_ == 0; }
51
52   constexpr ReadOptions operator|(ReadOptions const& that) const
53   {
54     return ReadOptions(value_ | that.value_);
55   }
56   constexpr ReadOptions operator&(ReadOptions const& that) const
57   {
58     return ReadOptions(value_ & that.value_);
59   }
60   constexpr ReadOptions operator^(ReadOptions const& that) const
61   {
62     return ReadOptions(value_ ^ that.value_);
63   }
64   constexpr ReadOptions operator~() const
65   {
66     return ReadOptions(~value_);
67   }
68
69   ReadOptions& operator|=(ReadOptions const& that)
70   {
71     value_ |= that.value_;
72     return *this;
73   }
74   ReadOptions& operator&=(ReadOptions const& that)
75   {
76     value_ &= that.value_;
77     return *this;
78   }
79   ReadOptions& operator^=(ReadOptions const& that)
80   {
81     value_ &= that.value_;
82     return *this;
83   }
84
85   /** Copy the data to the given buffer */
86   static constexpr ReadOptions none() { return ReadOptions(0); }
87
88   /** Allows to return a pointer to another buffer where the data is
89    *  available instead of copying the data into the buffer
90    */
91   static constexpr ReadOptions lazy() { return ReadOptions(1); }
92 };
93
94 /** A value read from another process */
95 template<class T>
96 class Remote {
97 private:
98   char buffer[sizeof(T)];
99 public:
100   void*       data() { return buffer; }
101   const void* data() const { return buffer; }
102   constexpr std::size_t size() const { return sizeof(T); }
103 };
104
105 /** A given state of a given process (abstract base class)
106  *
107  *  Currently, this might either be:
108  *
109  *  * the current state of an existing process;
110  *
111  *  * a snapshot.
112  */
113 class AddressSpace {
114 private:
115   Process* process_;
116 public:
117   AddressSpace(Process* process) : process_(process) {}
118   virtual ~AddressSpace();
119
120   simgrid::mc::Process* process() const { return process_; }
121
122   /** Read data from the address space
123    *
124    *  @param buffer        target buffer for the data
125    *  @param size          number of bytes
126    *  @param address       remote source address of the data
127    *  @param process_index which process (used for SMPI privatization)
128    *  @param options
129    */
130   virtual const void* read_bytes(void* buffer, std::size_t size,
131     RemotePtr<void> address, int process_index = ProcessIndexAny,
132     ReadOptions options = ReadOptions::none()) const = 0;
133
134   /** Read a given data structure from the address space */
135   template<class T> inline
136   void read(T *buffer, RemotePtr<T> ptr, int process_index = ProcessIndexAny)
137   {
138     this->read_bytes(buffer, sizeof(T), ptr, process_index);
139   }
140
141   template<class T> inline
142   void read(Remote<T>& buffer, RemotePtr<T> ptr, int process_index = ProcessIndexAny)
143   {
144     this->read_bytes(buffer.data(), sizeof(T), ptr, process_index);
145   }
146
147   /** Read a given data structure from the address space */
148   template<class T> inline
149   T read(RemotePtr<T> ptr, int process_index = ProcessIndexMissing)
150   {
151     static_assert(std::is_trivial<T>::value, "Cannot read a non-trivial type");
152     T res;
153     return *(T*)this->read_bytes(&res, sizeof(T), ptr, process_index);
154   }
155 };
156
157 }
158 }
159
160 #endif