Skip to content
Snippets Groups Projects
Commit 9e207c10 authored by git_unspecified's avatar git_unspecified
Browse files

Update README

parent e5d8fb8d
No related branches found
No related tags found
No related merge requests found
# hpvm-deepcopy # hpvm-deepcopy
A deepcopy utility allowing automatic deep copy of arbitrary C/C++ objects, with optional custom behavior override. A deepcopy utility allowing automatic snapshot of arbitrary C/C++ objects into a continuous member buffer, with optional custom behavior override.
This utilize contains a compiler frontend and a runtime library, and must be used in combination. This utilize contains a compiler frontend and a runtime library, and must be used in combination.
## Usage ## Usage
While designed in the intention to collaborate with HPVM, this utility have no inherent dependency on the HPVM project, and can be used on its own to enable other usages like sending arbitrary objects over pipes or network.
Various examples of different complexity using this deepcopy utility with HPVM can be found in `src/tests`. A few quirks, like turning off run-time exception handling, is required by HPVM, and user should refer to the HPVM documentation for more details.
### During Program Development ### During Program Development
#### Using Deepcopy
1. Include `src/hpvm_dclib.hpp`, in front of *any* other includes. This is important.
```cpp
#include "hpvm_dclib.hpp"
// Make sure dclib is at front of everything
```
2. For an arbitrary cpp object, one can snapshot it into a contiguous buffer automatically using `hpvm_do_snapshot(T*)`:
```cpp
MatrixXd mat1(2, 5);
auto m1b = hpvm_do_snapshot(&mat1); // Perform snapshot
```
3. The snapshotted object can be freely moved around, serialize, and deserialize, be referring to:
```cpp
m1b.buf // Buffer start pointer
m1b.buf->total_size() // Buffer size
```
4. Wrap any usage of the object between unwind and rewind code (usually on GPU kernel if using HPVM OCL backend):
```cpp
auto& mat1d = *use_on_device<MatrixXd>(m1b.buf);
mat1d(0, 1) = 999; // Or use the object however needed
done_use_on_device(m1b.buf);
```
### During Compilation ### During Compilation
...@@ -25,8 +57,40 @@ This utilize contains a compiler frontend and a runtime library, and must be use ...@@ -25,8 +57,40 @@ This utilize contains a compiler frontend and a runtime library, and must be use
``` ```
3. And deepcopy should now work as expected. `build.sh` provides and example to build a few testcases together with HPVM. 3. And deepcopy should now work as expected. `build.sh` provides and example to build a few testcases together with HPVM.
#### Supporting Deepcopy
1. Any memory allocation intended to be used by objects potentially copied must be allocated and de-allocated using dclib functions:
```cpp
void* hpvm_malloc(size_t sz)
void hpvm_free(void* ptr)
T* hpvm_new(Args... args)
T* hpvm_new_arr(size_t num_obj)
void hpvm_delete(T* obj)
void hpvm_delete_arr(T* obj)
```
And any library need to be patched replacing allocation calls to be used with deepcopy. Memory segments not allocated using our allocator will be ignored.
- To handle `std::` container, we provide a memory allocator, without type restrictions on what they holds (and they can be recursive):
```cpp
DeepCopyAlloc<T>>;
// e.g
std::vector<int, DeepCopyAlloc<int>> my_deepcopyable_int_vector;
```
- We also provide a patched version of `Eigen`, in `src/eigen`.
2. If the user wish to override the deepcopy behavior of a certain type, e.g., only selectively copying certain fields, the user should specialize the following templated function:
```cpp
template <class T>
extern typename std::enable_if<has_custom_behavior<T>::value>::type
hpvm_snapshot_custom(HpvmBuf& buf, T* dst_obj, T* original_obj);
```
for the chosen type `T`.
## Known Limitations
1. Virtual functions on object only works if the address space does not change.
2. Pointers pointing to the middle of a buffer will not be fixed after copying.
## Internals ## Internals
A more in-depth description of how the low level bit works can be found [here](https://docs.google.com/presentation/d/1FW40excTZBG4qcGRu_IQ6BB9to-xhvTjYThR8CJFeNs). A more in-depth description of how the low level bit works can be found [here](https://docs.google.com/presentation/d/1FW40excTZBG4qcGRu_IQ6BB9to-xhvTjYThR8CJFeNs).
[](./doc-img/illu.png) ![image](./doc-img/illu.png)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment