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*)`:
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_tsz)
voidhpvm_free(void*ptr)
T*hpvm_new(Args...args)
T*hpvm_new_arr(size_tnum_obj)
voidhpvm_delete(T*obj)
voidhpvm_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):
- 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:
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).