Metall  v0.28
A persistent memory allocator for data-centric analytics
csr.hpp
Go to the documentation of this file.
1 // Copyright 2019 Lawrence Livermore National Security, LLC and other Metall
2 // Project Developers. See the top-level COPYRIGHT file for details.
3 //
4 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
5 
6 #ifndef METALL_EXAMPLE_GRAPH_DATA_STRUCTURE_CSR_HPP
7 #define METALL_EXAMPLE_GRAPH_DATA_STRUCTURE_CSR_HPP
8 
9 #include <memory>
10 #include <metall/metall.hpp>
11 
15 template <typename index_t = uint64_t, typename vid_t = uint64_t,
16  typename allocator_t = std::allocator<char>>
17 class csr {
18  private:
19  // Here, we declare allocator and pointer types we need.
20  // As this data structure can be stored in persistent memory,
21  // we have to use the pointer type the given allocator has
22 
23  // Allocator and pointer types for the indices array
24  using index_allocator_t = typename std::allocator_traits<
25  allocator_t>::template rebind_alloc<index_t>;
26  using index_pointer_t =
27  typename std::allocator_traits<index_allocator_t>::pointer;
28 
29  // Allocator and pointer types for the edges array
30  using edge_allocator_t =
31  typename std::allocator_traits<allocator_t>::template rebind_alloc<vid_t>;
32  using edge_pointer_t =
33  typename std::allocator_traits<edge_allocator_t>::pointer;
34 
35  public:
36  csr(const std::size_t num_vertices, const std::size_t num_edges,
37  allocator_t allocator = allocator_t())
38  : m_num_vertices(num_vertices),
39  m_num_edges(num_edges),
40  m_indices(nullptr),
41  m_edges(nullptr),
42  m_allocator(allocator) {
43  // -- Allocate arrays with dedicated allocators -- //
44  auto index_allocator = index_allocator_t(m_allocator);
45  m_indices = index_allocator.allocate(m_num_vertices + 1);
46  auto edge_allocator = edge_allocator_t(m_allocator);
47  m_edges = edge_allocator.allocate(num_edges);
48  }
49 
50  ~csr() {
51  // -- Deallocate arrays with dedicated allocators -- //
52  auto index_allocator = index_allocator_t(m_allocator);
53  index_allocator.deallocate(m_indices, m_num_vertices + 1);
54  auto edge_allocator = edge_allocator_t(m_allocator);
55  edge_allocator.deallocate(m_edges, m_num_edges);
56  }
57 
59  index_t *indices() {
60  return metall::to_raw_pointer(m_indices);
61  // return std::pointer_traits<char_pointer_t>::to_address(m_indices); //
62  // From C++20
63  }
64 
66  vid_t *edges() {
67  return metall::to_raw_pointer(m_edges);
68  // return std::pointer_traits<char_pointer_t>::to_address(m_edges); // From
69  // C++20
70  }
71 
72  private:
73  const std::size_t m_num_vertices;
74  const std::size_t m_num_edges;
75  index_pointer_t m_indices;
76  edge_pointer_t m_edges;
77  allocator_t m_allocator;
78 };
79 
80 #endif // METALL_EXAMPLE_GRAPH_DATA_STRUCTURE_CSR_HPP
Simple CSR graph data structure that can take a custom C++ allocator and be stored in persistent memo...
Definition: csr.hpp:17
~csr()
Definition: csr.hpp:50
index_t * indices()
Returns a raw pointer to the indices array.
Definition: csr.hpp:59
vid_t * edges()
Returns a raw pointer to the edges array.
Definition: csr.hpp:66
csr(const std::size_t num_vertices, const std::size_t num_edges, allocator_t allocator=allocator_t())
Definition: csr.hpp:36