Allocators¶
The fundamental concept for accessing memory through Umpire is the
umpire::Allocator
. An umpire::Allocator
is a C++ object that
can be used to allocate and deallocate memory, as well as query a pointer to
get some extra information about it.
All umpire::Allocator
s are created and managed by Umpire’s
umpire::ResourceManager
. To get an Allocator, you need to ask for one:
auto& rm = umpire::ResourceManager::getInstance();
umpire::Allocator allocator = rm.getAllocator("HOST");
Once you have an umpire::Allocator
you can use it to allocate and deallocate memory:
double* data = static_cast<double*>(
allocator.allocate(SIZE*sizeof(double)));
std::cout << "Allocated " << (SIZE*sizeof(double)) << " bytes using the "
<< allocator.getName() << " allocator...";
allocator.deallocate(data);
In the next section, we will see how to allocate memory using different resources.
//////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2016-19, Lawrence Livermore National Security, LLC and Umpire
// project contributors. See the COPYRIGHT file for details.
//
// SPDX-License-Identifier: (MIT)
//////////////////////////////////////////////////////////////////////////////
#include "umpire/Allocator.hpp"
#include "umpire/ResourceManager.hpp"
int main(int, char**) {
constexpr std::size_t SIZE = 1024;
auto& rm = umpire::ResourceManager::getInstance();
umpire::Allocator allocator = rm.getAllocator("HOST");
double* data = static_cast<double*>(
allocator.allocate(SIZE*sizeof(double)));
std::cout << "Allocated " << (SIZE*sizeof(double)) << " bytes using the "
<< allocator.getName() << " allocator...";
allocator.deallocate(data);
std::cout << " deallocated." << std::endl;
return 0;
}