[arrayfire] 239/284: Add getHostMemorySize and getDeviceMemorySize functions

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 d3d2996374de5cf4b59e60dc024ae40b72139d93
Author: Shehzan Mohammed <shehzan at arrayfire.com>
Date:   Wed Jan 20 17:51:37 2016 -0500

    Add getHostMemorySize and getDeviceMemorySize functions
    
    * Print memory size in CPU and OpenCL info
    * Change opencl::getDevice to accept id
    * Fix multi-line error strings
---
 src/api/c/err_common.cpp        |   8 +--
 src/backend/cpu/platform.cpp    |  24 ++++++++-
 src/backend/cpu/platform.hpp    |   4 ++
 src/backend/cuda/platform.cpp   |  11 ++++
 src/backend/cuda/platform.hpp   |   4 ++
 src/backend/host_memory.cpp     | 113 ++++++++++++++++++++++++++++++++++++++++
 src/backend/host_memory.hpp     |  18 +++++++
 src/backend/opencl/platform.cpp |  22 ++++++--
 src/backend/opencl/platform.hpp |  10 +++-
 9 files changed, 204 insertions(+), 10 deletions(-)

diff --git a/src/api/c/err_common.cpp b/src/api/c/err_common.cpp
index 382dac1..495967a 100644
--- a/src/api/c/err_common.cpp
+++ b/src/api/c/err_common.cpp
@@ -198,13 +198,13 @@ const char *af_err_to_string(const af_err err)
     case AF_ERR_BATCH:              return "Invalid batch configuration";
     case AF_ERR_NOT_SUPPORTED:      return "Function not supported";
     case AF_ERR_NOT_CONFIGURED:     return "Function not configured to build";
-    case AF_ERR_NONFREE:            return "Function unavailable."
+    case AF_ERR_NONFREE:            return "Function unavailable. "
                                            "ArrayFire compiled without Non-Free algorithms support";
     case AF_ERR_NO_DBL:             return "Double precision not supported for this device";
-    case AF_ERR_NO_GFX:             return "Graphics functionality unavailable."
+    case AF_ERR_NO_GFX:             return "Graphics functionality unavailable. "
                                            "ArrayFire compiled without Graphics support";
-    case AF_ERR_LOAD_LIB:           return "Failed to load dynamic library."
-                                           "See http://www.arrayfire.com/docs/unifiedbackend.htm"
+    case AF_ERR_LOAD_LIB:           return "Failed to load dynamic library. "
+                                           "See http://www.arrayfire.com/docs/unifiedbackend.htm "
                                            "for instructions to set up environment for Unified backend";
     case AF_ERR_LOAD_SYM:           return "Failed to load symbol";
     case AF_ERR_ARR_BKND_MISMATCH:  return "There was a mismatch between an array and the current backend";
diff --git a/src/backend/cpu/platform.cpp b/src/backend/cpu/platform.cpp
index 0039b20..49abda3 100644
--- a/src/backend/cpu/platform.cpp
+++ b/src/backend/cpu/platform.cpp
@@ -19,6 +19,7 @@
 #include <defines.hpp>
 #include <version.hpp>
 #include <queue.hpp>
+#include <host_memory.hpp>
 
 #ifdef _WIN32
 #include <limits.h>
@@ -197,6 +198,15 @@ static const std::string get_system(void)
 #endif
 }
 
+// http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring/217605#217605
+// trim from start
+static inline std::string &ltrim(std::string &s)
+{
+    s.erase(s.begin(), std::find_if(s.begin(), s.end(),
+                                    std::not1(std::ptr_fun<int, int>(std::isspace))));
+    return s;
+}
+
 std::string getInfo()
 {
     std::ostringstream info;
@@ -204,7 +214,9 @@ std::string getInfo()
 
     info << "ArrayFire v" << AF_VERSION
          << " (CPU, " << get_system() << ", build " << AF_REVISION << ")" << std::endl;
-    info << string("[0] ") << cinfo.vendor() <<": " << cinfo.model() << " ";
+    std::string model = cinfo.model();
+    info << string("[0] ") << cinfo.vendor() <<": " << ltrim(model)
+         << ", " << (int)(getDeviceMemorySize(getActiveDeviceId()) / 1048576.0) << " MB, ";
     info << "Max threads("<< cinfo.threads()<<") ";
 #ifndef NDEBUG
     info << AF_COMPILER_STR;
@@ -249,6 +261,16 @@ int getActiveDeviceId()
     return 0;
 }
 
+size_t getDeviceMemorySize(int device)
+{
+    return common::getHostMemorySize();
+}
+
+size_t getHostMemorySize()
+{
+    return common::getHostMemorySize();
+}
+
 static const int MAX_QUEUES = 1;
 
 
diff --git a/src/backend/cpu/platform.hpp b/src/backend/cpu/platform.hpp
index 0cd42ae..9118ade 100644
--- a/src/backend/cpu/platform.hpp
+++ b/src/backend/cpu/platform.hpp
@@ -28,6 +28,10 @@ namespace cpu {
 
     int getActiveDeviceId();
 
+    size_t getDeviceMemorySize(int device);
+
+    size_t getHostMemorySize();
+
     void sync(int device);
 
     queue& getQueue(int idx = 0);
diff --git a/src/backend/cuda/platform.cpp b/src/backend/cuda/platform.cpp
index d172903..46b7303 100644
--- a/src/backend/cuda/platform.cpp
+++ b/src/backend/cuda/platform.cpp
@@ -23,6 +23,7 @@
 #include <cstring>
 #include <err_cuda.hpp>
 #include <util.hpp>
+#include <host_memory.hpp>
 
 using namespace std;
 
@@ -304,6 +305,16 @@ cudaStream_t getStream(int device)
     return str;
 }
 
+size_t getDeviceMemorySize(int device)
+{
+    return getDeviceProp(device).totalGlobalMem;
+}
+
+size_t getHostMemorySize()
+{
+    return common::getHostMemorySize();
+}
+
 int setDevice(int device)
 {
     return DeviceManager::getInstance().setActiveDevice(device);
diff --git a/src/backend/cuda/platform.hpp b/src/backend/cuda/platform.hpp
index 20862fb..9302f41 100644
--- a/src/backend/cuda/platform.hpp
+++ b/src/backend/cuda/platform.hpp
@@ -46,6 +46,10 @@ int getDeviceNativeId(int device);
 
 cudaStream_t getStream(int device);
 
+size_t getDeviceMemorySize(int device);
+
+size_t getHostMemorySize();
+
 int setDevice(int device);
 
 void sync(int device);
diff --git a/src/backend/host_memory.cpp b/src/backend/host_memory.cpp
new file mode 100644
index 0000000..9b4f1e5
--- /dev/null
+++ b/src/backend/host_memory.cpp
@@ -0,0 +1,113 @@
+/*
+ * Author:  David Robert Nadeau
+ * Site:    http://NadeauSoftware.com/
+ * License: Creative Commons Attribution 3.0 Unported License
+ *          http://creativecommons.org/licenses/by/3.0/deed.en_US
+ * Source:  http://nadeausoftware.com/sites/NadeauSoftware.com/files/getMemorySize.c
+ */
+
+#include "host_memory.hpp"
+
+#if defined(_WIN32)
+#include <Windows.h>
+
+#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/param.h>
+
+#if defined(BSD)
+#include <sys/sysctl.h>
+#endif
+
+#else
+#define NOMEMORYSIZE
+#endif
+
+namespace common
+{
+
+#ifdef NOMEMORYSIZE
+size_t getHostMemorySize()
+{
+    return 0L; // Can't detect
+}
+
+#else
+
+/**
+ * Returns the size of physical memory (RAM) in bytes.
+ */
+size_t getHostMemorySize()
+{
+#if defined(_WIN32) && (defined(__CYGWIN__) || defined(__CYGWIN32__))
+    /* Cygwin under Windows. ------------------------------------ */
+    /* New 64-bit MEMORYSTATUSEX isn't available.  Use old 32.bit */
+    MEMORYSTATUS status;
+    status.dwLength = sizeof(status);
+    GlobalMemoryStatus( &status );
+    return (size_t)status.dwTotalPhys;
+
+#elif defined(_WIN32)
+    /* Windows. ------------------------------------------------- */
+    /* Use new 64-bit MEMORYSTATUSEX, not old 32-bit MEMORYSTATUS */
+    MEMORYSTATUSEX status;
+    status.dwLength = sizeof(status);
+    GlobalMemoryStatusEx( &status );
+    return (size_t)status.ullTotalPhys;
+
+#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
+    /* UNIX variants. ------------------------------------------- */
+    /* Prefer sysctl() over sysconf() except sysctl() HW_REALMEM and HW_PHYSMEM */
+
+#if defined(CTL_HW) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM64))
+    int mib[2];
+    mib[0] = CTL_HW;
+#if defined(HW_MEMSIZE)
+    mib[1] = HW_MEMSIZE;        /* OSX. --------------------- */
+#elif defined(HW_PHYSMEM64)
+    mib[1] = HW_PHYSMEM64;      /* NetBSD, OpenBSD. --------- */
+#endif
+    int64_t size = 0;       /* 64-bit */
+    size_t len = sizeof( size );
+    if ( sysctl( mib, 2, &size, &len, NULL, 0 ) == 0 )
+        return (size_t)size;
+    return 0L;          /* Failed? */
+
+#elif defined(_SC_AIX_REALMEM)
+    /* AIX. ----------------------------------------------------- */
+    return (size_t)sysconf( _SC_AIX_REALMEM ) * (size_t)1024L;
+
+#elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
+    /* FreeBSD, Linux, OpenBSD, and Solaris. -------------------- */
+    return (size_t)sysconf( _SC_PHYS_PAGES ) *
+        (size_t)sysconf( _SC_PAGESIZE );
+
+#elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGE_SIZE)
+    /* Legacy. -------------------------------------------------- */
+    return (size_t)sysconf( _SC_PHYS_PAGES ) *
+        (size_t)sysconf( _SC_PAGE_SIZE );
+
+#elif defined(CTL_HW) && (defined(HW_PHYSMEM) || defined(HW_REALMEM))
+    /* DragonFly BSD, FreeBSD, NetBSD, OpenBSD, and OSX. -------- */
+    int mib[2];
+    mib[0] = CTL_HW;
+#if defined(HW_REALMEM)
+    mib[1] = HW_REALMEM;        /* FreeBSD. ----------------- */
+#elif defined(HW_PYSMEM)
+    mib[1] = HW_PHYSMEM;        /* Others. ------------------ */
+#endif
+    unsigned int size = 0;      /* 32-bit */
+    size_t len = sizeof( size );
+    if ( sysctl( mib, 2, &size, &len, NULL, 0 ) == 0 )
+        return (size_t)size;
+    return 0L;          /* Failed? */
+#endif /* sysctl and sysconf variants */
+
+#else
+    return 0L;          /* Unknown OS. */
+#endif
+}
+
+#endif // NOMEMORYSIZE
+} // namespace common
diff --git a/src/backend/host_memory.hpp b/src/backend/host_memory.hpp
new file mode 100644
index 0000000..5955cbf
--- /dev/null
+++ b/src/backend/host_memory.hpp
@@ -0,0 +1,18 @@
+/*******************************************************
+ * Copyright (c) 2014, ArrayFire
+ * All rights reserved.
+ *
+ * This file is distributed under 3-clause BSD license.
+ * The complete license agreement can be obtained at:
+ * http://arrayfire.com/licenses/BSD-3-Clause
+ ********************************************************/
+
+#pragma once
+#include <cstddef>
+
+namespace common
+{
+
+size_t getHostMemorySize();
+
+}
diff --git a/src/backend/opencl/platform.cpp b/src/backend/opencl/platform.cpp
index 884dca1..94efd7a 100644
--- a/src/backend/opencl/platform.cpp
+++ b/src/backend/opencl/platform.cpp
@@ -43,6 +43,7 @@
 #include <errorcodes.hpp>
 #include <err_opencl.hpp>
 #include <util.hpp>
+#include <host_memory.hpp>
 
 using std::string;
 using std::vector;
@@ -404,7 +405,9 @@ std::string getInfo()
             std::to_string(nDevices) +
             (show_braces ? string("]") : "-");
 
-        info << id << " " << getPlatformName(*device) << ": " << ltrim(dstr);
+        size_t msize = device->getInfo<CL_DEVICE_GLOBAL_MEM_SIZE>();
+        info << id << " " << getPlatformName(*device) << ": " << ltrim(dstr)
+             << ", " << msize / 1048576 << " MB";
 #ifndef NDEBUG
         info << " -- ";
         string devVersion = device->getInfo<CL_DEVICE_VERSION>();
@@ -481,10 +484,23 @@ CommandQueue& getQueue()
     return *(devMngr.mQueues[devMngr.mActiveQId]);
 }
 
-const cl::Device& getDevice()
+const cl::Device& getDevice(int id)
 {
     DeviceManager& devMngr = DeviceManager::getInstance();
-    return *(devMngr.mDevices[devMngr.mActiveQId]);
+    if(id == -1) id = devMngr.mActiveQId;
+    return *(devMngr.mDevices[id]);
+}
+
+size_t getDeviceMemorySize(int device)
+{
+    const cl::Device& dev = getDevice(device);
+    size_t msize = dev.getInfo<CL_DEVICE_GLOBAL_MEM_SIZE>();
+    return msize;
+}
+
+size_t getHostMemorySize()
+{
+    return common::getHostMemorySize();
 }
 
 cl_device_type getDeviceType()
diff --git a/src/backend/opencl/platform.hpp b/src/backend/opencl/platform.hpp
index d4f9f0e..9b5377d 100644
--- a/src/backend/opencl/platform.hpp
+++ b/src/backend/opencl/platform.hpp
@@ -33,7 +33,9 @@ class DeviceManager
 
     friend cl::CommandQueue& getQueue();
 
-    friend const cl::Device& getDevice();
+    friend const cl::Device& getDevice(int id);
+
+    friend size_t getDeviceMemorySize(int device);
 
     friend bool isGLSharingSupported();
 
@@ -100,7 +102,11 @@ const cl::Context& getContext();
 
 cl::CommandQueue& getQueue();
 
-const cl::Device& getDevice();
+const cl::Device& getDevice(int id = -1);
+
+size_t getDeviceMemorySize(int device);
+
+size_t getHostMemorySize();
 
 cl_device_type getDeviceType();
 

-- 
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