Galois
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PagePool.h
Go to the documentation of this file.
1 /*
2  * This file belongs to the Galois project, a C++ library for exploiting
3  * parallelism. The code is being released under the terms of the 3-Clause BSD
4  * License (a copy is located in LICENSE.txt at the top-level directory).
5  *
6  * Copyright (C) 2018, The University of Texas at Austin. All rights reserved.
7  * UNIVERSITY EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES CONCERNING THIS
8  * SOFTWARE AND DOCUMENTATION, INCLUDING ANY WARRANTIES OF MERCHANTABILITY,
9  * FITNESS FOR ANY PARTICULAR PURPOSE, NON-INFRINGEMENT AND WARRANTIES OF
10  * PERFORMANCE, AND ANY WARRANTY THAT MIGHT OTHERWISE ARISE FROM COURSE OF
11  * DEALING OR USAGE OF TRADE. NO WARRANTY IS EITHER EXPRESS OR IMPLIED WITH
12  * RESPECT TO THE USE OF THE SOFTWARE OR DOCUMENTATION. Under no circumstances
13  * shall University be liable for incidental, special, indirect, direct or
14  * consequential damages or loss of profits, interruption of business, or
15  * related expenses which may arise from use of Software or Documentation,
16  * including but not limited to those resulting from defects in Software and/or
17  * Documentation, or loss or inaccuracy of data of any kind.
18  */
19 
20 #ifndef GALOIS_RUNTIME_PAGEPOOL_H
21 #define GALOIS_RUNTIME_PAGEPOOL_H
22 
23 #include <cstddef>
24 #include <deque>
25 #include <mutex>
26 #include <numeric>
27 #include <unordered_map>
28 #include <vector>
29 
30 #include "galois/config.h"
31 #include "galois/gIO.h"
37 
38 namespace galois {
39 namespace runtime {
40 
42 
43 void* pagePoolAlloc();
44 void pagePoolFree(void*);
45 void pagePoolPreAlloc(unsigned);
46 
47 // Size of returned pages
48 size_t pagePoolSize();
49 
54 int numPagePoolAllocForThread(unsigned tid);
55 
56 namespace internal {
57 
58 struct FreeNode {
59  FreeNode* next;
60 };
61 
64 
65 // Tracks pages allocated
66 template <typename _UNUSED = void>
67 class PageAllocState {
68  std::deque<std::atomic<int>> counts;
69  std::vector<HeadPtrStorage> pool;
70  std::unordered_map<void*, int> ownerMap;
72 
73  void* allocFromOS() {
74  void* ptr = galois::substrate::allocPages(1, true);
75  assert(ptr);
77  counts[tid] += 1;
78  std::lock_guard<galois::substrate::SimpleLock> lg(mapLock);
79  ownerMap[ptr] = tid;
80  return ptr;
81  }
82 
83 public:
84  PageAllocState() {
86  counts.resize(num);
87  pool.resize(num);
88  }
89 
90  int count(int tid) const { return counts[tid]; }
91 
92  int countAll() const {
93  return std::accumulate(counts.begin(), counts.end(), 0);
94  }
95 
96  void* pageAlloc() {
98  HeadPtr& hp = pool[tid].data;
99  if (hp.getValue()) {
100  hp.lock();
101  FreeNode* h = hp.getValue();
102  if (h) {
103  hp.unlock_and_set(h->next);
104  return h;
105  }
106  hp.unlock();
107  }
108  return allocFromOS();
109  }
110 
111  void pageFree(void* ptr) {
112  assert(ptr);
113  mapLock.lock();
114  assert(ownerMap.count(ptr));
115  int i = ownerMap[ptr];
116  mapLock.unlock();
117  HeadPtr& hp = pool[i].data;
118  hp.lock();
119  FreeNode* nh = reinterpret_cast<FreeNode*>(ptr);
120  nh->next = hp.getValue();
121  hp.unlock_and_set(nh);
122  }
123 
124  void pagePreAlloc() { pageFree(allocFromOS()); }
125 };
126 
128 void setPagePoolState(PageAllocState<>* pa);
129 
130 } // end namespace internal
131 
132 } // end namespace runtime
133 } // end namespace galois
134 
135 #endif
PtrLock is a spinlock and a pointer.
Definition: PtrLock.h:42
ThreadPool & getThreadPool(void)
return a reference to system thread pool
Definition: ThreadPool.cpp:259
Definition: CacheLineStorage.h:32
unsigned getMaxThreads() const
return the number of threads supported by the thread pool on the current machine
Definition: ThreadPool.h:178
void pagePoolPreAlloc(unsigned)
Definition: PagePool.cpp:43
size_t pagePoolSize()
Definition: PagePool.cpp:50
static unsigned getTID()
Definition: ThreadPool.h:204
void pagePoolFree(void *)
Definition: PagePool.cpp:48
void pagePreAlloc(int numpages)
Preallocate numpages large pages for each thread.
void * allocPages(unsigned num, bool preFault)
Definition: PageAlloc.cpp:71
int numPagePoolAllocForThread(unsigned tid)
Returns total large pages allocated for thread by Galois memory management subsystem.
Definition: PagePool.cpp:37
void * pagePoolAlloc()
Low level page pool (individual pages, use largeMalloc for large blocks)
Definition: PagePool.cpp:41
SimpleLock is a spinlock.
Definition: SimpleLock.h:36
T accumulate(InputIterator first, InputIterator last, const T &identity, const BinaryOperation &binary_op)
Definition: ParallelSTL.h:268
int numPagePoolAllocTotal()
Returns total large pages allocated by Galois memory management subsystem.
Definition: PagePool.cpp:35