@@ -150,133 +150,6 @@ Utilities in |hpx|
150
150
In order to ease the burden of programming in |hpx | we have provided several
151
151
utilities to users. The following section documents those facilies.
152
152
153
- .. _checkpoint :
154
-
155
- Checkpoint
156
- ----------
157
-
158
- A common need of users is to periodically backup an application. This practice
159
- provides resiliency and potential restart points in code. We have developed the
160
- concept of a ``checkpoint `` to support this use case.
161
-
162
- Found in ``hpx/util/checkpoint.hpp ``, ``checkpoint ``\ s are defined as objects
163
- which hold a serialized version of an object or set of objects at a particular
164
- moment in time. This representation can be stored in memory for later use or it
165
- can be written to disk for storage and/or recovery at a later point. In order to
166
- create and fill this object with data we use a function called
167
- ``save_checkpoint ``. In code the function looks like this::
168
-
169
- hpx::future<hpx::util::checkpoint> hpx::util::save_checkpoint(a, b, c, ...);
170
-
171
- ``save_checkpoint `` takes arbitrary data containers such as int, double, float,
172
- vector, and future and serializes them into a newly created ``checkpoint ``
173
- object. This function returns a ``future `` to a ``checkpoint `` containing the
174
- data. Let us look a simple use case below::
175
-
176
- using hpx::util::checkpoint;
177
- using hpx::util::save_checkpoint;
178
-
179
- std::vector<int> vec{1,2,3,4,5};
180
- hpx::future<checkpoint> save_checkpoint(vec);
181
-
182
- Once the future is ready the checkpoint object will contain the ``vector ``
183
- ``vec `` and its five elements.
184
-
185
- It is also possible to modify the launch policy used by ``save_checkpoint ``.
186
- This is accomplished by passing a launch policy as the first argument. It is
187
- important to note that passing ``hpx::launch::sync `` will cause
188
- ``save_checkpoint `` to return a ``checkpoint `` instead of a ``future `` to a
189
- ``checkpoint ``. All other policies passed to ``save_checkpoint `` will return a
190
- ``future `` to a ``checkpoint ``.
191
-
192
- Sometimes ``checkpoint `` s must be declared before they are used.
193
- ``save_checkpoint `` allows users to move pre-created ``checkpoint `` s into the
194
- function as long as they are the first container passing into the function (In
195
- the case where a launch policy is used, the ``checkpoint `` will immediately
196
- follow the launch policy). An example of these features can be found below:
197
-
198
- .. literalinclude :: ../../tests/unit/util/checkpoint.cpp
199
- :language: c++
200
- :lines: 27-38
201
-
202
- Now that we can create ``checkpoint `` s we now must be able to restore the
203
- objects they contain into memory. This is accomplished by the function
204
- ``restore_checkpoint ``. This function takes a ``checkpoint `` and fills its data
205
- into the containers it is provided. It is important to remember that the
206
- containers must be ordered in the same way they were placed into the
207
- ``checkpoint ``. For clarity see the example below:
208
-
209
- .. literalinclude :: ../../tests/unit/util/checkpoint.cpp
210
- :language: c++
211
- :lines: 41-49
212
-
213
- The core utility of ``checkpoint `` is in its ability to make certain data
214
- persistent. Often this means that the data is needed to be stored in an object,
215
- such as a file, for later use. For these cases we have provided two solutions:
216
- stream operator overloads and access iterators.
217
-
218
- We have created the two stream overloads
219
- ``operator<< `` and ``operator>> `` to stream data
220
- out of and into ``checkpoint ``. You can see an
221
- example of the overloads in use below:
222
-
223
- .. literalinclude :: ../../tests/unit/util/checkpoint.cpp
224
- :language: c++
225
- :lines: 176-186
226
-
227
- This is the primary way to move data into and out of a ``checkpoint ``. It is
228
- important to note, however, that users should be cautious when using a stream
229
- operator to load data an another function to remove it (or vice versa). Both
230
- ``operator<< `` and ``operator>> `` rely on a ``.write() `` and a ``.read() ``
231
- function respectively. In order to know how much data to read from the
232
- ``std::istream ``, the ``operator<< `` will write the size of the ``checkpoint ``
233
- before writing the ``checkpoint `` data. Correspondingly, the ``operator>> `` will
234
- read the size of the stored data before reading the data into new instance of
235
- ``checkpoint ``. As long as the user employs the ``operator<< `` and
236
- ``operator>> `` to stream the data this detail can be ignored.
237
-
238
- .. important ::
239
-
240
- Be careful when mixing ``operator<< `` and ``operator>> `` with other
241
- facilities to read and write to a ``checkpoint ``. ``operator<< `` writes and
242
- extra variable and ``operator>> `` reads this variable back separately. Used
243
- together the user will not encounter any issues and can safely ignore this
244
- detail.
245
-
246
- Users may also move the data into and out of a ``checkpoint `` using the exposed
247
- ``.begin() `` and ``.end() `` iterators. An example of this use case is
248
- illustrated below.
249
-
250
- .. literalinclude :: ../../tests/unit/util/checkpoint.cpp
251
- :language: c++
252
- :lines: 129-150
253
-
254
- Checkpointing Components
255
- ------------------------
256
-
257
- ``save_checkpoint `` and ``restore_checkpoint `` are also able to store components
258
- inside ``checkpoint``s. This can be done in one of two ways. First a client of
259
- the component can be passed to ``save_checkpoint ``. When the user wishes to
260
- resurrect the component she can pass a client instance to ``restore_checkpoint ``.
261
-
262
- This technique is demonstrated below:
263
-
264
- .. literalinclude :: ../../tests/unit/util/checkpoint.cpp
265
- :language: c++
266
- :lines: 143-144
267
-
268
- The second way a user can save a component is by passing a ``shared_ptr `` to the
269
- component to ``save_checkpoint ``. This component can be resurrected by creating
270
- a new instance of the component type and passing a ``shared_ptr `` to the new
271
- instance to ``restore_checkpoint ``. An example can be found below:
272
-
273
- This technique is demonstrated below:
274
-
275
- .. literalinclude :: ../../tests/unit/util/checkpoint.cpp
276
- :language: c++
277
- :lines: 113-126
278
-
279
-
280
153
.. _iostreams :
281
154
282
155
The |hpx | I/O-streams component
0 commit comments