Moving and modifying data in a heterogenous memory system can be annoying. You have to keep track of the source and destination, and often use vendor-specific APIs to perform the modifications. In Umpire, all data modification and movement is wrapped up in a concept we call operations. Full documentation for all of these is available here. The full code listing for each example is include at the bottom of the page.
Let's start by looking at how we copy data around. The :class:`umpire::ResourceManager` provides an interface to copy that handles figuring out where the source and destination pointers were allocated, and selects the correct implementation to copy the data:
.. literalinclude:: ../../../examples/tutorial/tut_copy.cpp :lines: 18
This example allocates the destination data using any valid Allocator.
If you want to move data to a new Allocator and deallocate the old copy, Umpire provides a :func:`umpire::ResourceManager::move` operation.
.. literalinclude:: ../../../examples/tutorial/tut_move.cpp :lines: 17-18
The move operation combines an allocation, a copy, and a deallocate into one function call, allowing you to move data without having to have the destination data allocated. As always, this operation will work with any valid destination Allocator.
Setting a whole block of memory to a value (like 0) is a common operation, that most people know as a memset. Umpire provides a :func:`umpire::ResourceManager::memset` implementation that can be applied to any allocation, regardless of where it came from:
.. literalinclude:: ../../../examples/tutorial/tut_memset.cpp :lines: 36
Reallocating CPU memory is easy, there is a function designed specifically to
do it: realloc
. When the original allocation was made in a different memory
however, you can be out of luck. Umpire provides a
:func:`umpire::ResourceManager::reallocate` operation:
.. literalinclude:: ../../../examples/tutorial/tut_reallocate.cpp :lines: 40
This method returns a pointer to the reallocated data. Like all operations, this can be used regardless of the Allocator used for the source data.
Copy Example Listing
.. literalinclude:: ../../../examples/tutorial/tut_copy.cpp
Move Example Listing
.. literalinclude:: ../../../examples/tutorial/tut_move.cpp
Memset Example Listing
.. literalinclude:: ../../../examples/tutorial/tut_memset.cpp
Reallocate Example Listing
.. literalinclude:: ../../../examples/tutorial/tut_reallocate.cpp