[arrayfire] 240/284: Updates to Memory Manager and Garbage Collection Heuristics

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Sun Feb 7 18:59:37 UTC 2016


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch debian/experimental
in repository arrayfire.

commit 6c306528854bafd05599ffbb928aaa171c6d0c9c
Author: Shehzan Mohammed <shehzan at arrayfire.com>
Date:   Wed Jan 20 18:30:03 2016 -0500

    Updates to Memory Manager and Garbage Collection Heuristics
    
    * Add getMaxMemorySize virtual function to MemoryManager
        * Used to compute the max_bytes for each memory manager instance
    * max_bytes
        * This is now a part of memory_info
        * Gets it's size from the device memory rather than a generic size
        * For CPU and CUDA Pinned Memory, use getHostDeviceMemory
    * Add getMaxBytes function to fetch the max_bytes of active device
    * MAX_BUFFERS is now 1000
    
    Fix missing include
---
 src/backend/MemoryManager.cpp | 23 ++++++++++++++++++-----
 src/backend/MemoryManager.hpp | 11 +++++++++--
 src/backend/cpu/Array.cpp     |  2 +-
 src/backend/cpu/memory.cpp    | 12 +++++++++++-
 src/backend/cpu/memory.hpp    |  5 +++--
 src/backend/cpu/platform.cpp  |  9 +++++++--
 src/backend/cuda/Array.cpp    |  2 +-
 src/backend/cuda/memory.cpp   | 20 ++++++++++++++++++--
 src/backend/cuda/memory.hpp   |  5 +++--
 src/backend/opencl/Array.cpp  |  2 +-
 src/backend/opencl/memory.cpp | 20 ++++++++++++++++++--
 src/backend/opencl/memory.hpp |  5 +++--
 12 files changed, 93 insertions(+), 23 deletions(-)

diff --git a/src/backend/MemoryManager.cpp b/src/backend/MemoryManager.cpp
index cea4ae6..03cefe7 100644
--- a/src/backend/MemoryManager.cpp
+++ b/src/backend/MemoryManager.cpp
@@ -10,6 +10,7 @@
 #include <iostream>
 #include <iomanip>
 #include <string>
+#include <algorithm>
 #include "MemoryManager.hpp"
 #include "dispatch.hpp"
 #include "err_common.hpp"
@@ -18,10 +19,9 @@
 namespace common
 {
 
-MemoryManager::MemoryManager(int num_devices, unsigned MAX_BUFFERS, unsigned MAX_BYTES, bool debug):
+MemoryManager::MemoryManager(int num_devices, unsigned MAX_BUFFERS, bool debug):
     mem_step_size(1024),
     max_buffers(MAX_BUFFERS),
-    max_bytes(MAX_BYTES),
     memory(num_devices),
     debug_mode(debug)
 {
@@ -32,9 +32,16 @@ MemoryManager::MemoryManager(int num_devices, unsigned MAX_BUFFERS, unsigned MAX
     }
     if (this->debug_mode) mem_step_size = 1;
 
+    static const size_t oneGB = 1 << 30;
     for (int n = 0; n < num_devices; n++) {
-        memory[n].total_bytes = 0;
-        memory[n].lock_bytes = 0;
+        size_t memsize = getMaxMemorySize(n);
+        // Calls garbage collection when:
+        // total_bytes > memsize * 0.75 when memsize <  4GB
+        // total_bytes > memsize - 1 GB when memsize >= 4GB
+        // If memsize returned 0, then use 1GB
+        memory[n].max_bytes    = memsize == 0 ? oneGB : std::max(memsize * 0.75, (double)(memsize - oneGB));
+        memory[n].total_bytes  = 0;
+        memory[n].lock_bytes   = 0;
         memory[n].lock_buffers = 0;
     }
 }
@@ -115,7 +122,7 @@ void *MemoryManager::alloc(const size_t bytes)
             // FIXME: Add better checks for garbage collection
             // Perhaps look at total memory available as a metric
             if (current.map.size() > this->max_buffers ||
-                current.lock_bytes >= this->max_bytes) {
+                current.lock_bytes >= current.max_bytes) {
 
                 this->garbageCollect();
             }
@@ -204,6 +211,12 @@ void MemoryManager::setMemStepSize(size_t new_step_size)
     this->mem_step_size = new_step_size;
 }
 
+size_t MemoryManager::getMaxBytes()
+{
+    lock_guard_t lock(this->memory_mutex);
+    return this->getCurrentMemoryInfo().max_bytes;
+}
+
 void MemoryManager::printInfo(const char *msg, const int device)
 {
     lock_guard_t lock(this->memory_mutex);
diff --git a/src/backend/MemoryManager.hpp b/src/backend/MemoryManager.hpp
index 5de8e4d..8bb9941 100644
--- a/src/backend/MemoryManager.hpp
+++ b/src/backend/MemoryManager.hpp
@@ -37,11 +37,11 @@ class MemoryManager
         size_t lock_bytes;
         size_t lock_buffers;
         size_t total_bytes;
+        size_t max_bytes;
     } memory_info;
 
     size_t mem_step_size;
     unsigned max_buffers;
-    unsigned max_bytes;
     std::vector<memory_info> memory;
     bool debug_mode;
 
@@ -55,8 +55,13 @@ class MemoryManager
         return 0;
     }
 
+    virtual size_t getMaxMemorySize(int id)
+    {
+        return 0;
+    }
+
 public:
-    MemoryManager(int num_devices, unsigned MAX_BUFFERS, unsigned MAX_BYTES, bool debug);
+    MemoryManager(int num_devices, unsigned MAX_BUFFERS, bool debug);
 
     void *alloc(const size_t bytes);
 
@@ -75,6 +80,8 @@ public:
 
     size_t getMemStepSize();
 
+    size_t getMaxBytes();
+
     void setMemStepSize(size_t new_step_size);
 
     virtual void *nativeAlloc(const size_t bytes)
diff --git a/src/backend/cpu/Array.cpp b/src/backend/cpu/Array.cpp
index 862c576..891604c 100644
--- a/src/backend/cpu/Array.cpp
+++ b/src/backend/cpu/Array.cpp
@@ -161,7 +161,7 @@ createNodeArray(const dim4 &dims, Node_ptr node)
 
     if (length > MAX_TNJ_LEN ||
         buf_count >= MAX_BUFFERS ||
-        bytes >= MAX_BYTES) {
+        bytes >= getMaxBytes()) {
         out.eval();
     }
 
diff --git a/src/backend/cpu/memory.cpp b/src/backend/cpu/memory.cpp
index c387b68..09a0e83 100644
--- a/src/backend/cpu/memory.cpp
+++ b/src/backend/cpu/memory.cpp
@@ -29,6 +29,7 @@ namespace cpu
 class MemoryManager  : public common::MemoryManager
 {
     int getActiveDeviceId();
+    size_t getMaxMemorySize(int id);
 public:
     MemoryManager();
     void *nativeAlloc(const size_t bytes);
@@ -48,8 +49,13 @@ int MemoryManager::getActiveDeviceId()
     return cpu::getActiveDeviceId();
 }
 
+size_t MemoryManager::getMaxMemorySize(int id)
+{
+    return cpu::getDeviceMemorySize(id);
+}
+
 MemoryManager::MemoryManager() :
-    common::MemoryManager(getDeviceCount(), MAX_BUFFERS, MAX_BYTES, AF_MEM_DEBUG || AF_CPU_MEM_DEBUG)
+    common::MemoryManager(getDeviceCount(), MAX_BUFFERS, AF_MEM_DEBUG || AF_CPU_MEM_DEBUG)
 {}
 
 
@@ -79,6 +85,10 @@ size_t getMemStepSize(void)
     return getMemoryManager().getMemStepSize();
 }
 
+size_t getMaxBytes()
+{
+    return getMemoryManager().getMaxBytes();
+}
 
 void garbageCollect()
 {
diff --git a/src/backend/cpu/memory.hpp b/src/backend/cpu/memory.hpp
index 279b3db..8f61f11 100644
--- a/src/backend/cpu/memory.hpp
+++ b/src/backend/cpu/memory.hpp
@@ -26,8 +26,9 @@ namespace cpu
     template<typename T> T* pinnedAlloc(const size_t &elements);
     template<typename T> void pinnedFree(T* ptr);
 
-    static const unsigned MAX_BUFFERS = 100;
-    static const unsigned MAX_BYTES = 100 * (1 << 20);
+    static const unsigned MAX_BUFFERS = 1000;
+
+    size_t getMaxBytes();
 
     void deviceMemoryInfo(size_t *alloc_bytes, size_t *alloc_buffers,
                           size_t *lock_bytes,  size_t *lock_buffers);
diff --git a/src/backend/cpu/platform.cpp b/src/backend/cpu/platform.cpp
index 49abda3..65a5ab1 100644
--- a/src/backend/cpu/platform.cpp
+++ b/src/backend/cpu/platform.cpp
@@ -20,6 +20,7 @@
 #include <version.hpp>
 #include <queue.hpp>
 #include <host_memory.hpp>
+#include <cctype>
 
 #ifdef _WIN32
 #include <limits.h>
@@ -215,8 +216,12 @@ std::string getInfo()
     info << "ArrayFire v" << AF_VERSION
          << " (CPU, " << get_system() << ", build " << AF_REVISION << ")" << std::endl;
     std::string model = cinfo.model();
-    info << string("[0] ") << cinfo.vendor() <<": " << ltrim(model)
-         << ", " << (int)(getDeviceMemorySize(getActiveDeviceId()) / 1048576.0) << " MB, ";
+    size_t memMB = getDeviceMemorySize(getActiveDeviceId()) / 1048576;
+    info << string("[0] ") << cinfo.vendor() <<": " << ltrim(model);
+
+    if(memMB) info << ", " << memMB << " MB, ";
+    else      info << ", Unknown MB, ";
+
     info << "Max threads("<< cinfo.threads()<<") ";
 #ifndef NDEBUG
     info << AF_COMPILER_STR;
diff --git a/src/backend/cuda/Array.cpp b/src/backend/cuda/Array.cpp
index 39cd06c..1ca6012 100644
--- a/src/backend/cuda/Array.cpp
+++ b/src/backend/cuda/Array.cpp
@@ -150,7 +150,7 @@ namespace cuda
 
         if (length > MAX_JIT_LEN ||
             buf_count >= MAX_BUFFERS ||
-            bytes >= MAX_BYTES) {
+            bytes >= getMaxBytes()) {
             out.eval();
         }
 
diff --git a/src/backend/cuda/memory.cpp b/src/backend/cuda/memory.cpp
index 15786d9..f5dc6ca 100644
--- a/src/backend/cuda/memory.cpp
+++ b/src/backend/cuda/memory.cpp
@@ -37,6 +37,7 @@ namespace cuda
 class MemoryManager  : public common::MemoryManager
 {
     int getActiveDeviceId();
+    size_t getMaxMemorySize(int id);
 public:
     MemoryManager();
     void *nativeAlloc(const size_t bytes);
@@ -66,6 +67,7 @@ public:
 class MemoryManagerPinned  : public common::MemoryManager
 {
     int getActiveDeviceId();
+    size_t getMaxMemorySize(int id);
 public:
     MemoryManagerPinned();
     void *nativeAlloc(const size_t bytes);
@@ -82,8 +84,13 @@ int MemoryManager::getActiveDeviceId()
     return cuda::getActiveDeviceId();
 }
 
+size_t MemoryManager::getMaxMemorySize(int id)
+{
+    return cuda::getDeviceMemorySize(id);
+}
+
 MemoryManager::MemoryManager() :
-    common::MemoryManager(getDeviceCount(), MAX_BUFFERS, MAX_BYTES, AF_MEM_DEBUG || AF_CUDA_MEM_DEBUG)
+    common::MemoryManager(getDeviceCount(), MAX_BUFFERS, AF_MEM_DEBUG || AF_CUDA_MEM_DEBUG)
 {}
 
 void *MemoryManager::nativeAlloc(const size_t bytes)
@@ -112,8 +119,13 @@ int MemoryManagerPinned::getActiveDeviceId()
     return 0; // pinned uses a single vector
 }
 
+size_t MemoryManagerPinned::getMaxMemorySize(int id)
+{
+    return cuda::getHostMemorySize();
+}
+
 MemoryManagerPinned::MemoryManagerPinned() :
-    common::MemoryManager(1, MAX_BUFFERS, MAX_BYTES, AF_MEM_DEBUG || AF_CUDA_MEM_DEBUG)
+    common::MemoryManager(1, MAX_BUFFERS, AF_MEM_DEBUG || AF_CUDA_MEM_DEBUG)
 {}
 
 void *MemoryManagerPinned::nativeAlloc(const size_t bytes)
@@ -147,6 +159,10 @@ size_t getMemStepSize(void)
     return getMemoryManager().getMemStepSize();
 }
 
+size_t getMaxBytes()
+{
+    return getMemoryManager().getMaxBytes();
+}
 
 void garbageCollect()
 {
diff --git a/src/backend/cuda/memory.hpp b/src/backend/cuda/memory.hpp
index 5b362cd..590ba3b 100644
--- a/src/backend/cuda/memory.hpp
+++ b/src/backend/cuda/memory.hpp
@@ -25,8 +25,9 @@ namespace cuda
     template<typename T> T* pinnedAlloc(const size_t &elements);
     template<typename T> void pinnedFree(T* ptr);
 
-    static const unsigned MAX_BUFFERS   = 100;
-    static const unsigned MAX_BYTES     = (1 << 30);
+    static const unsigned MAX_BUFFERS   = 1000;
+
+    size_t getMaxBytes();
 
     void deviceMemoryInfo(size_t *alloc_bytes, size_t *alloc_buffers,
                           size_t *lock_bytes,  size_t *lock_buffers);
diff --git a/src/backend/opencl/Array.cpp b/src/backend/opencl/Array.cpp
index 044a632..7b6a26e 100644
--- a/src/backend/opencl/Array.cpp
+++ b/src/backend/opencl/Array.cpp
@@ -179,7 +179,7 @@ namespace opencl
 
         if (is_max_jit_len(length) ||
             buf_count >= MAX_BUFFERS ||
-            bytes >= MAX_BYTES) {
+            bytes >= getMaxBytes()) {
             out.eval();
         }
 
diff --git a/src/backend/opencl/memory.cpp b/src/backend/opencl/memory.cpp
index 2427581..7054e96 100644
--- a/src/backend/opencl/memory.cpp
+++ b/src/backend/opencl/memory.cpp
@@ -32,6 +32,7 @@ namespace opencl
 class MemoryManager  : public common::MemoryManager
 {
     int getActiveDeviceId();
+    size_t getMaxMemorySize(int id);
 public:
     MemoryManager();
     void *nativeAlloc(const size_t bytes);
@@ -52,6 +53,7 @@ class MemoryManagerPinned  : public common::MemoryManager
         std::map<void *, cl::Buffer>
         > pinned_maps;
     int getActiveDeviceId();
+    size_t getMaxMemorySize(int id);
 
 public:
 
@@ -80,8 +82,13 @@ int MemoryManager::getActiveDeviceId()
     return opencl::getActiveDeviceId();
 }
 
+size_t MemoryManager::getMaxMemorySize(int id)
+{
+    return opencl::getDeviceMemorySize(id);
+}
+
 MemoryManager::MemoryManager() :
-    common::MemoryManager(getDeviceCount(), MAX_BUFFERS, MAX_BYTES, AF_MEM_DEBUG || AF_OPENCL_MEM_DEBUG)
+    common::MemoryManager(getDeviceCount(), MAX_BUFFERS, AF_MEM_DEBUG || AF_OPENCL_MEM_DEBUG)
 {}
 
 void *MemoryManager::nativeAlloc(const size_t bytes)
@@ -113,8 +120,13 @@ int MemoryManagerPinned::getActiveDeviceId()
     return opencl::getActiveDeviceId();
 }
 
+size_t MemoryManagerPinned::getMaxMemorySize(int id)
+{
+    return opencl::getDeviceMemorySize(id);
+}
+
 MemoryManagerPinned::MemoryManagerPinned() :
-    common::MemoryManager(getDeviceCount(), MAX_BUFFERS, MAX_BYTES, AF_MEM_DEBUG || AF_OPENCL_MEM_DEBUG),
+    common::MemoryManager(getDeviceCount(), MAX_BUFFERS, AF_MEM_DEBUG || AF_OPENCL_MEM_DEBUG),
     pinned_maps(getDeviceCount())
 {}
 
@@ -163,6 +175,10 @@ size_t getMemStepSize(void)
     return getMemoryManager().getMemStepSize();
 }
 
+size_t getMaxBytes()
+{
+    return getMemoryManager().getMaxBytes();
+}
 
 void garbageCollect()
 {
diff --git a/src/backend/opencl/memory.hpp b/src/backend/opencl/memory.hpp
index da27e0d..ea40b4b 100644
--- a/src/backend/opencl/memory.hpp
+++ b/src/backend/opencl/memory.hpp
@@ -30,8 +30,9 @@ namespace opencl
     template<typename T> T* pinnedAlloc(const size_t &elements);
     template<typename T> void pinnedFree(T* ptr);
 
-    static const unsigned MAX_BUFFERS   = 100;
-    static const unsigned MAX_BYTES     = (1 << 30);
+    static const unsigned MAX_BUFFERS   = 1000;
+
+    size_t getMaxBytes();
 
     void deviceMemoryInfo(size_t *alloc_bytes, size_t *alloc_buffers,
                           size_t *lock_bytes,  size_t *lock_buffers);

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/arrayfire.git



More information about the debian-science-commits mailing list