Skip to content

Commit

Permalink
Reorganize fallback allocator.
Browse files Browse the repository at this point in the history
  • Loading branch information
Keita Iwabuchi committed Feb 15, 2024
1 parent 881239a commit 6b204d0
Show file tree
Hide file tree
Showing 9 changed files with 349 additions and 304 deletions.
2 changes: 1 addition & 1 deletion example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ add_metall_executable(snapshot snapshot.cpp)

add_metall_executable(csr_graph csr_graph.cpp)

add_metall_executable(fallback_allocator_adaptor fallback_allocator_adaptor.cpp)
add_metall_executable(fallback_allocator fallback_allocator.cpp)

add_metall_executable(datastore_description datastore_description.cpp)

Expand Down
4 changes: 2 additions & 2 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ Therefore, the data structures in the examples below are more like how to use ST

### Fallback Allocator

* [fallback_allocator_adaptor.cpp](fallback_allocator_adaptor.cpp)
* [fallback_allocator.cpp](fallback_allocator.cpp)

* How to use [fallback_allocator_adaptor](../include/metall/utility/fallback_allocator_adaptor.hpp)
* How to use [fallback_allocator](../include/metall/container/fallback_allocator.hpp)


### MPI (experimental implementation)
Expand Down
33 changes: 33 additions & 0 deletions example/fallback_allocator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2020 Lawrence Livermore National Security, LLC and other Metall
// Project Developers. See the top-level COPYRIGHT file for details.
//
// SPDX-License-Identifier: (Apache-2.0 OR MIT)

#include <iostream>

#include <metall/metall.hpp>
#include <metall/container/vector.hpp>

using vector_t =
metall::container::vector<int, metall::manager::fallback_allocator<int>>;

int main() {
// Allocation using Metall
{
metall::manager manager(metall::create_only, "/tmp/dir");
auto pvec = manager.construct<vector_t>("vec")(manager.get_allocator());
pvec->push_back(1);
std::cout << (*pvec)[0] << std::endl;
}

// Allocation not using Metall, i.e., uses a heap allocator (malloc()).
// This code would cause a build error if fallback_allocator were not used.
{
vector_t vec; // As no metall allocator argument is passed, the fallback
// allocator uses malloc() internally.
vec.push_back(2);
std::cout << vec[0] << std::endl;
}

return 0;
}
41 changes: 0 additions & 41 deletions example/fallback_allocator_adaptor.cpp

This file was deleted.

33 changes: 33 additions & 0 deletions include/metall/basic_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <metall/tags.hpp>
#include <metall/stl_allocator.hpp>
#include <metall/container/scoped_allocator.hpp>
#include <metall/container/fallback_allocator.hpp>
#include <metall/kernel/manager_kernel.hpp>
#include <metall/detail/named_proxy.hpp>
#include <metall/kernel/segment_storage.hpp>
Expand Down Expand Up @@ -69,6 +70,38 @@ class basic_manager {
container::scoped_allocator_adaptor<allocator_type<OuterT>,
allocator_type<InnerT>...>;

/// \brief A STL compatible allocator which fallbacks to a heap allocator
/// (e.g., malloc()) if no argument is provided to construct a
/// allocator_type instance.
///
/// \tparam T The type of the object to allocate
///
/// \details
/// This allocator enables the following code.
/// \code
/// {
/// using alloc = fallback_allocator<int>;
/// // Allocate a vector object in a heap.
/// vector<int, alloc> vec;
/// // Allocate a vector object in a Metall space.
/// vector<int, alloc> vec2(manager.get_allocator());
/// }
/// \endcode
/// \attention
/// One of the primary purposes of this allocator is to provide a way to
/// temporarily allocate data structures that use Metall’s STL-allocator in a
/// heap in addition to in Metall memory space. It is advised to use this
/// allocator with caution as this two memory spaces are used transparently by
/// this allocator.
template <typename T>
using fallback_allocator =
container::fallback_allocator_adaptor<allocator_type<T>>;

/// \brief Fallback allocator type wrapped by scoped_allocator_adaptor.
template <typename T>
using scoped_fallback_allocator_type =
container::scoped_allocator_adaptor<fallback_allocator<T>>;

/// \brief Construct proxy
template <typename T>
using construct_proxy =
Expand Down
Loading

0 comments on commit 6b204d0

Please sign in to comment.