Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement I/O as asynchronous activities
[simgrid.git] / src / mc / AddressSpace.hpp
1 /* Copyright (c) 2008-2018. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_MC_ADDRESS_SPACE_H
7 #define SIMGRID_MC_ADDRESS_SPACE_H
8
9 #include "src/mc/mc_forward.hpp"
10 #include "src/mc/remote/RemotePtr.hpp"
11
12 namespace simgrid {
13 namespace mc {
14
15 /** Process index used when no process is available (SMPI privatization)
16  *
17  *  The expected behavior is that if a process index is needed it will fail.
18  * */
19 const int ProcessIndexMissing = -1;
20
21 /** Process index used when we don't care about the process index (SMPI privatization)
22  * */
23 const int ProcessIndexDisabled = -2;
24
25 /** Constant used when any process will do (SMPI privatization)
26  *
27  *  Note: This is is index of the first process.
28  */
29 const int ProcessIndexAny = 0;
30
31 /** Options for read operations
32  *
33  *  This is a set of flags managed with bitwise operators. Only the
34  *  meaningful operations are defined: addition, conversions to/from
35  *  integers are not allowed.
36  */
37 class ReadOptions {
38   std::uint32_t value_;
39   constexpr explicit ReadOptions(std::uint32_t value) : value_(value) {}
40 public:
41   constexpr ReadOptions() : value_(0) {}
42
43   explicit constexpr operator bool() const { return value_ != 0; }
44   constexpr bool operator!() const { return value_ == 0; }
45
46   constexpr ReadOptions operator|(ReadOptions const& that) const
47   {
48     return ReadOptions(value_ | that.value_);
49   }
50   constexpr ReadOptions operator&(ReadOptions const& that) const
51   {
52     return ReadOptions(value_ & that.value_);
53   }
54   constexpr ReadOptions operator^(ReadOptions const& that) const
55   {
56     return ReadOptions(value_ ^ that.value_);
57   }
58   constexpr ReadOptions operator~() const
59   {
60     return ReadOptions(~value_);
61   }
62
63   ReadOptions& operator|=(ReadOptions const& that)
64   {
65     value_ |= that.value_;
66     return *this;
67   }
68   ReadOptions& operator&=(ReadOptions const& that)
69   {
70     value_ &= that.value_;
71     return *this;
72   }
73   ReadOptions& operator^=(ReadOptions const& that)
74   {
75     value_ &= that.value_;
76     return *this;
77   }
78
79   /** Copy the data to the given buffer */
80   static constexpr ReadOptions none() { return ReadOptions(0); }
81
82   /** Allows to return a pointer to another buffer where the data is
83    *  available instead of copying the data into the buffer
84    */
85   static constexpr ReadOptions lazy() { return ReadOptions(1); }
86 };
87
88 /** A given state of a given process (abstract base class)
89  *
90  *  Currently, this might either be:
91  *
92  *  * the current state of an existing process;
93  *
94  *  * a snapshot.
95  *
96  *  In order to support SMPI privatization, the can read the memory from the
97  *  context of a given SMPI process: if specified, the code reads data from the
98  *  correct SMPI privatization VMA.
99  */
100 class AddressSpace {
101 private:
102   RemoteClient* process_;
103
104 public:
105   explicit AddressSpace(RemoteClient* process) : process_(process) {}
106   virtual ~AddressSpace() = default;
107
108   /** The process of this address space
109    *
110    *  This is where we can get debug informations, memory layout, etc.
111    */
112   simgrid::mc::RemoteClient* process() const { return process_; }
113
114   /** Read data from the address space
115    *
116    *  @param buffer        target buffer for the data
117    *  @param size          number of bytes to read
118    *  @param address       remote source address of the data
119    *  @param process_index which process (used for SMPI privatization)
120    *  @param options
121    */
122   virtual const void* read_bytes(void* buffer, std::size_t size,
123     RemotePtr<void> address, int process_index = ProcessIndexAny,
124     ReadOptions options = ReadOptions::none()) const = 0;
125
126   /** Read a given data structure from the address space */
127   template<class T> inline
128   void read(T *buffer, RemotePtr<T> ptr, int process_index = ProcessIndexAny) const
129   {
130     this->read_bytes(buffer, sizeof(T), ptr, process_index);
131   }
132
133   template<class T> inline
134   void read(Remote<T>& buffer, RemotePtr<T> ptr, int process_index = ProcessIndexAny) const
135   {
136     this->read_bytes(buffer.getBuffer(), sizeof(T), ptr, process_index);
137   }
138
139   /** Read a given data structure from the addres space
140    *
141    *  This version returns by value.
142    */
143   template<class T> inline
144   Remote<T> read(RemotePtr<T> ptr, int process_index = ProcessIndexMissing) const
145   {
146     Remote<T> res;
147     this->read_bytes(&res, sizeof(T), ptr, process_index);
148     return res;
149   }
150
151   /** Read a string of known size */
152   std::string read_string(RemotePtr<char> address, std::size_t len) const
153   {
154     std::string res;
155     res.resize(len);
156     this->read_bytes(&res[0], len, address);
157     return res;
158   }
159
160 };
161
162 }
163 }
164
165 #endif