Commit d0a846c2 authored by Ville Pietilä's avatar Ville Pietilä
Browse files

Added more logging. Fix pinned host memory pool to be a real singleton.

parent af2a1bd8
......@@ -8,6 +8,7 @@
#include <map>
#include <queue>
#include <mutex>
#include "unistd.h"
namespace ck {
namespace memory {
......@@ -15,15 +16,27 @@ namespace memory {
class MemPool
{
public:
MemPool() = default;
MemPool() :
enableLogging_(ck::EnvIsEnabled(CK_ENV(CK_LOGGING))),
pid_(getpid())
{
if (enableLogging_)
std::cout << "[ MemPool ] Created memory pool for process " << pid_ << std::endl;
}
~MemPool()
{
if (enableLogging_)
std::cout << "[ MemPool ] Deleting pool for process " << pid_ << "..."<< std::endl;
std::lock_guard<std::mutex> lock(mutex_);
for (auto& [size, q] : memory_pool_)
{
clearMemoryPoolQueue(q);
}
if (enableLogging_)
std::cout << "[ MemPool ] Deleted pool for process " << pid_ << std::endl;
}
void* allocate(std::size_t sizeInBytes)
......@@ -78,10 +91,21 @@ namespace memory {
std::mutex mutex_; // Mutex to protect access to the memory pool.
std::map<size_t, std::queue<void*>> memory_pool_{};
size_t memPoolSizeInBytes_{0};
bool enableLogging_{false};
int pid_{-1};
};
class PinnedHostMemoryAllocatorBase
{
protected:
static MemPool& get_memory_pool() {
static MemPool memory_pool;
return memory_pool;
}
};
template <typename T>
class PinnedHostMemoryAllocator
class PinnedHostMemoryAllocator : public PinnedHostMemoryAllocatorBase
{
public:
using value_type = T;
......@@ -124,11 +148,6 @@ namespace memory {
void destroy(U* p) noexcept {
p->~U();
}
private:
static MemPool& get_memory_pool() {
static MemPool memory_pool;
return memory_pool;
}
};
template <typename T, typename U>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment