[arrayfire] 182/284: Move MappedPtr into the Array class

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Sun Feb 7 18:59:31 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 b36d003e56222a2888184e985442339e8e5af567
Author: Shehzan Mohammed <shehzan at arrayfire.com>
Date:   Fri Jan 8 22:11:41 2016 -0500

    Move MappedPtr into the Array class
---
 src/backend/opencl/Array.hpp            | 43 +++++++++++++++++++++++++++++++++
 src/backend/opencl/cpu/cpu_blas.cpp     | 22 +++++++----------
 src/backend/opencl/cpu/cpu_cholesky.cpp | 14 +++++------
 src/backend/opencl/cpu/cpu_inverse.cpp  | 12 ++++-----
 src/backend/opencl/cpu/cpu_lu.cpp       | 37 +++++++++++++++-------------
 src/backend/opencl/cpu/cpu_qr.cpp       | 27 ++++++++-------------
 src/backend/opencl/cpu/cpu_solve.cpp    | 42 ++++++++++++--------------------
 src/backend/opencl/cpu/cpu_svd.cpp      | 29 +++++++++++++---------
 src/backend/opencl/memory.cpp           | 27 +--------------------
 src/backend/opencl/memory.hpp           |  3 ---
 10 files changed, 128 insertions(+), 128 deletions(-)

diff --git a/src/backend/opencl/Array.hpp b/src/backend/opencl/Array.hpp
index 5f86d6d..abce5b9 100644
--- a/src/backend/opencl/Array.hpp
+++ b/src/backend/opencl/Array.hpp
@@ -21,6 +21,7 @@
 #include <memory.hpp>
 #include <memory>
 #include <err_common.hpp>
+#include <err_opencl.hpp>
 
 namespace opencl
 {
@@ -209,6 +210,48 @@ namespace opencl
 
         JIT::Node_ptr getNode() const;
 
+    private:
+        bool is_const() const
+        {
+            return true;
+        }
+
+        bool is_const()
+        {
+            return false;
+        }
+
+    public:
+        std::shared_ptr<T> getMappedPtr() const
+        {
+            auto func = [=] (void* ptr) {
+                try {
+                    if(ptr != nullptr)
+                        getQueue().enqueueUnmapMemObject(*data, ptr);
+                        ptr = nullptr;
+                } catch(cl::Error err) {
+                    CL_TO_AF_ERROR(err);
+                }
+            };
+
+            T *ptr = nullptr;
+            try {
+                if(ptr == nullptr) {
+                    if(is_const()) {
+                        ptr = (T*)getQueue().enqueueMapBuffer(*const_cast<cl::Buffer*>(get()), true, CL_MAP_READ,
+                                getOffset(), getDataDims().elements() * sizeof(T));
+                    } else {
+                        ptr = (T*)getQueue().enqueueMapBuffer(*(get()), true, CL_MAP_READ|CL_MAP_WRITE,
+                                getOffset(), getDataDims().elements() * sizeof(T));
+                    }
+                }
+            } catch(cl::Error err) {
+                CL_TO_AF_ERROR(err);
+            }
+
+            return std::shared_ptr<T>(ptr, func);
+        }
+
         friend Array<T> createValueArray<T>(const af::dim4 &size, const T& value);
         friend Array<T> createHostDataArray<T>(const af::dim4 &size, const T * const data);
         friend Array<T> createDeviceDataArray<T>(const af::dim4 &size, const void *data);
diff --git a/src/backend/opencl/cpu/cpu_blas.cpp b/src/backend/opencl/cpu/cpu_blas.cpp
index 8c77fff..1ff7e14 100644
--- a/src/backend/opencl/cpu/cpu_blas.cpp
+++ b/src/backend/opencl/cpu/cpu_blas.cpp
@@ -167,9 +167,9 @@ Array<T> matmul(const Array<T> &lhs, const Array<T> &rhs,
     using BT  =       typename blas_base<T>::type;
 
     // get host pointers from mapped memory
-    BT *lPtr = getMappedPtr<BT>(lhs.get());
-    BT *rPtr = getMappedPtr<BT>(rhs.get());
-    BT *oPtr = getMappedPtr<BT>(out.get());
+    std::shared_ptr<BT> lPtr = lhs.getMappedPtr();
+    std::shared_ptr<BT> rPtr = rhs.getMappedPtr();
+    std::shared_ptr<BT> oPtr = out.getMappedPtr();
 
     if(rDims[bColDim] == 1) {
         N = lDims[aColDim];
@@ -177,25 +177,21 @@ Array<T> matmul(const Array<T> &lhs, const Array<T> &rhs,
             CblasColMajor, lOpts,
             lDims[0], lDims[1],
             alpha,
-            lPtr, lStrides[1],
-            rPtr, rStrides[0],
+            lPtr.get(), lStrides[1],
+            rPtr.get(), rStrides[0],
             beta,
-            oPtr, 1);
+            oPtr.get(), 1);
     } else {
         gemm_func<T>()(
             CblasColMajor, lOpts, rOpts,
             M, N, K,
             alpha,
-            lPtr, lStrides[1],
-            rPtr, rStrides[1],
+            lPtr.get(), lStrides[1],
+            rPtr.get(), rStrides[1],
             beta,
-            oPtr, out.dims()[0]);
+            oPtr.get(), out.dims()[0]);
     }
 
-    unmapPtr(lhs.get(), lPtr);
-    unmapPtr(rhs.get(), rPtr);
-    unmapPtr(out.get(), oPtr);
-
     return out;
 }
 
diff --git a/src/backend/opencl/cpu/cpu_cholesky.cpp b/src/backend/opencl/cpu/cpu_cholesky.cpp
index 74bbf59..bd871d7 100644
--- a/src/backend/opencl/cpu/cpu_cholesky.cpp
+++ b/src/backend/opencl/cpu/cpu_cholesky.cpp
@@ -42,10 +42,10 @@ Array<T> cholesky(int *info, const Array<T> &in, const bool is_upper)
     Array<T> out = copyArray<T>(in);
     *info = cholesky_inplace(out, is_upper);
 
-    T* oPtr = getMappedPtr<T>(out.get());
-    if (is_upper) triangle<T, true , false>(oPtr, oPtr, out.dims(), out.strides(), out.strides());
-    else          triangle<T, false, false>(oPtr, oPtr, out.dims(), out.strides(), out.strides());
-    unmapPtr(out.get(), oPtr);
+    std::shared_ptr<T> oPtr = out.getMappedPtr();
+
+    if (is_upper) triangle<T, true , false>(oPtr.get(), oPtr.get(), out.dims(), out.strides(), out.strides());
+    else          triangle<T, false, false>(oPtr.get(), oPtr.get(), out.dims(), out.strides(), out.strides());
 
     return out;
 }
@@ -60,10 +60,10 @@ int cholesky_inplace(Array<T> &in, const bool is_upper)
     if(is_upper)
         uplo = 'U';
 
-    T* inPtr = getMappedPtr<T>(in.get());
+    std::shared_ptr<T> inPtr = in.getMappedPtr();
+
     int info = potrf_func<T>()(AF_LAPACK_COL_MAJOR, uplo,
-                               N, inPtr, in.strides()[1]);
-    unmapPtr(in.get(), inPtr);
+                               N, inPtr.get(), in.strides()[1]);
 
     return info;
 }
diff --git a/src/backend/opencl/cpu/cpu_inverse.cpp b/src/backend/opencl/cpu/cpu_inverse.cpp
index 24b4a67..fee1719 100644
--- a/src/backend/opencl/cpu/cpu_inverse.cpp
+++ b/src/backend/opencl/cpu/cpu_inverse.cpp
@@ -51,15 +51,13 @@ Array<T> inverse(const Array<T> &in)
 
     Array<int> pivot = cpu::lu_inplace<T>(A, false);
 
-    T *aPtr = getMappedPtr<T>(A.get());
-    int *pPtr = getMappedPtr<int>(pivot.get());
 
-    getri_func<T>()(AF_LAPACK_COL_MAJOR, M,
-                    aPtr, A.strides()[1],
-                    pPtr);
+    std::shared_ptr<T>   aPtr = A.getMappedPtr();
+    std::shared_ptr<int> pPtr = pivot.getMappedPtr();
 
-    unmapPtr(A.get(), aPtr);
-    unmapPtr(pivot.get(), pPtr);
+    getri_func<T>()(AF_LAPACK_COL_MAJOR, M,
+                    aPtr.get(), A.strides()[1],
+                    pPtr.get());
 
     return A;
 }
diff --git a/src/backend/opencl/cpu/cpu_lu.cpp b/src/backend/opencl/cpu/cpu_lu.cpp
index 293cb8a..3eb574e 100644
--- a/src/backend/opencl/cpu/cpu_lu.cpp
+++ b/src/backend/opencl/cpu/cpu_lu.cpp
@@ -40,9 +40,13 @@ LU_FUNC(getrf , cdouble, z)
 template<typename T>
 void lu_split(Array<T> &lower, Array<T> &upper, const Array<T> &in)
 {
-    T *l = getMappedPtr<T>(lower.get());
-    T *u = getMappedPtr<T>(upper.get());
-    T *i = getMappedPtr<T>(in.get());
+    std::shared_ptr<T> ls = lower.getMappedPtr();
+    std::shared_ptr<T> us = upper.getMappedPtr();
+    std::shared_ptr<T> is = in.getMappedPtr();
+
+    T *l = ls.get();
+    T *u = us.get();
+    T *i = is.get();
 
     dim4 ldm = lower.dims();
     dim4 udm = upper.dims();
@@ -91,18 +95,17 @@ void lu_split(Array<T> &lower, Array<T> &upper, const Array<T> &in)
             }
         }
     }
-
-    unmapPtr(lower.get(), l);
-    unmapPtr(upper.get(), u);
-    unmapPtr(in.get(), i);
 }
 
 void convertPivot(Array<int> &pivot, int out_sz)
 {
     Array<int> p = range<int>(dim4(out_sz), 0); // Runs opencl
 
-    int *d_pi = getMappedPtr<int>(pivot.get());
-    int *d_po = getMappedPtr<int>(p.get());
+    std::shared_ptr<int> pi = pivot.getMappedPtr();
+    std::shared_ptr<int> po = p.getMappedPtr();
+
+    int *d_pi = pi.get();
+    int *d_po = po.get();
 
     dim_t d0 = pivot.dims()[0];
 
@@ -111,8 +114,8 @@ void convertPivot(Array<int> &pivot, int out_sz)
         std::swap(d_po[j], d_po[d_pi[j] - 1]);
     }
 
-    unmapPtr(pivot.get(), d_pi);
-    unmapPtr(p.get(), d_po);
+    pi.reset();
+    po.reset();
 
     pivot = p;
 }
@@ -145,15 +148,15 @@ Array<int> lu_inplace(Array<T> &in, const bool convert_pivot)
 
     Array<int> pivot = createEmptyArray<int>(af::dim4(min(M, N), 1, 1, 1));
 
-    T *inPtr = getMappedPtr<T>(in.get());
-    int *pivotPtr = getMappedPtr<int>(pivot.get());
+    std::shared_ptr<T>   inPtr = in.getMappedPtr();
+    std::shared_ptr<int> piPtr = pivot.getMappedPtr();
 
     getrf_func<T>()(AF_LAPACK_COL_MAJOR, M, N,
-                    inPtr, in.strides()[1],
-                    pivotPtr);
+                    inPtr.get(), in.strides()[1],
+                    piPtr.get());
 
-    unmapPtr(in.get(), inPtr);
-    unmapPtr(pivot.get(), pivotPtr);
+    inPtr.reset();
+    piPtr.reset();
 
     if(convert_pivot) convertPivot(pivot, M);
 
diff --git a/src/backend/opencl/cpu/cpu_qr.cpp b/src/backend/opencl/cpu/cpu_qr.cpp
index 24a915a..32eca92 100644
--- a/src/backend/opencl/cpu/cpu_qr.cpp
+++ b/src/backend/opencl/cpu/cpu_qr.cpp
@@ -70,20 +70,16 @@ void qr(Array<T> &q, Array<T> &r, Array<T> &t, const Array<T> &in)
     dim4 rdims(M, N);
     r = createEmptyArray<T>(rdims);
 
-    T *qPtr = getMappedPtr<T>(q.get());
-    T *rPtr = getMappedPtr<T>(r.get());
-    T *tPtr = getMappedPtr<T>(t.get());
+    std::shared_ptr<T> qPtr = q.getMappedPtr();
+    std::shared_ptr<T> rPtr = r.getMappedPtr();
+    std::shared_ptr<T> tPtr = t.getMappedPtr();
 
-    triangle<T, true, false>(rPtr, qPtr, rdims, r.strides(), q.strides());
+    triangle<T, true, false>(rPtr.get(), qPtr.get(), rdims, r.strides(), q.strides());
 
     gqr_func<T>()(AF_LAPACK_COL_MAJOR,
                   M, M, min(M, N),
-                  qPtr, q.strides()[1],
-                  tPtr);
-
-    unmapPtr(q.get(), qPtr);
-    unmapPtr(r.get(), rPtr);
-    unmapPtr(t.get(), tPtr);
+                  qPtr.get(), q.strides()[1],
+                  tPtr.get());
 
     q.resetDims(dim4(M, M));
 }
@@ -97,15 +93,12 @@ Array<T> qr_inplace(Array<T> &in)
 
     Array<T> t = createEmptyArray<T>(af::dim4(min(M, N), 1, 1, 1));
 
-    T *iPtr = getMappedPtr<T>(in.get());
-    T *tPtr = getMappedPtr<T>(t.get());
+    std::shared_ptr<T> iPtr = in.getMappedPtr();
+    std::shared_ptr<T> tPtr = t.getMappedPtr();
 
     geqrf_func<T>()(AF_LAPACK_COL_MAJOR, M, N,
-                    iPtr, in.strides()[1],
-                    tPtr);
-
-    unmapPtr(in.get(), iPtr);
-    unmapPtr(t.get(), tPtr);
+                    iPtr.get(), in.strides()[1],
+                    tPtr.get());
 
     return t;
 }
diff --git a/src/backend/opencl/cpu/cpu_solve.cpp b/src/backend/opencl/cpu/cpu_solve.cpp
index 522454a..9e4f093 100644
--- a/src/backend/opencl/cpu/cpu_solve.cpp
+++ b/src/backend/opencl/cpu/cpu_solve.cpp
@@ -85,19 +85,15 @@ Array<T> solveLU(const Array<T> &A, const Array<int> &pivot,
 
     Array<T> B = copyArray<T>(b);
 
-    T *aPtr = getMappedPtr<T>(A.get());
-    T *bPtr = getMappedPtr<T>(B.get());
-    int *pPtr = getMappedPtr<int>(pivot.get());
+    std::shared_ptr<T  > aPtr = A.getMappedPtr();
+    std::shared_ptr<T  > bPtr = B.getMappedPtr();
+    std::shared_ptr<int> pPtr = pivot.getMappedPtr();
 
     getrs_func<T>()(AF_LAPACK_COL_MAJOR, 'N',
                     N, NRHS,
-                    aPtr, A.strides()[1],
-                    pPtr,
-                    bPtr, B.strides()[1]);
-
-    unmapPtr(A.get(), aPtr);
-    unmapPtr(B.get(), bPtr);
-    unmapPtr(pivot.get(), pPtr);
+                    aPtr.get(), A.strides()[1],
+                    pPtr.get(),
+                    bPtr.get(), B.strides()[1]);
 
     return B;
 }
@@ -109,19 +105,16 @@ Array<T> triangleSolve(const Array<T> &A, const Array<T> &b, const af_mat_prop o
     int N = B.dims()[0];
     int NRHS = B.dims()[1];
 
-    T *aPtr = getMappedPtr<T>(A.get());
-    T *bPtr = getMappedPtr<T>(B.get());
+    std::shared_ptr<T> aPtr = A.getMappedPtr();
+    std::shared_ptr<T> bPtr = B.getMappedPtr();
 
     trtrs_func<T>()(AF_LAPACK_COL_MAJOR,
                     options & AF_MAT_UPPER ? 'U' : 'L',
                     'N', // transpose flag
                     options & AF_MAT_DIAG_UNIT ? 'U' : 'N',
                     N, NRHS,
-                    aPtr, A.strides()[1],
-                    bPtr, B.strides()[1]);
-
-    unmapPtr(A.get(), aPtr);
-    unmapPtr(B.get(), bPtr);
+                    aPtr.get(), A.strides()[1],
+                    bPtr.get(), B.strides()[1]);
 
     return B;
 }
@@ -143,29 +136,26 @@ Array<T> solve(const Array<T> &a, const Array<T> &b, const af_mat_prop options)
     Array<T> A = copyArray<T>(a);
     Array<T> B = padArray<T, T>(b, dim4(max(M, N), K), scalar<T>(0));
 
-    T *aPtr = getMappedPtr<T>(A.get());
-    T *bPtr = getMappedPtr<T>(B.get());
+    std::shared_ptr<T> aPtr = A.getMappedPtr();
+    std::shared_ptr<T> bPtr = B.getMappedPtr();
 
     if(M == N) {
         std::vector<int> pivot(N);
         gesv_func<T>()(AF_LAPACK_COL_MAJOR, N, K,
-                       aPtr, A.strides()[1],
+                       aPtr.get(), A.strides()[1],
                        &pivot.front(),
-                       bPtr, B.strides()[1]);
+                       bPtr.get(), B.strides()[1]);
     } else {
         int sM = a.strides()[1];
         int sN = a.strides()[2] / sM;
 
         gels_func<T>()(AF_LAPACK_COL_MAJOR, 'N',
                        M, N, K,
-                       aPtr, A.strides()[1],
-                       bPtr, max(sM, sN));
+                       aPtr.get(), A.strides()[1],
+                       bPtr.get(), max(sM, sN));
         B.resetDims(dim4(N, K));
     }
 
-    unmapPtr(A.get(), aPtr);
-    unmapPtr(B.get(), bPtr);
-
     return B;
 }
 
diff --git a/src/backend/opencl/cpu/cpu_svd.cpp b/src/backend/opencl/cpu/cpu_svd.cpp
index 66e4c0a..c53df8a 100644
--- a/src/backend/opencl/cpu/cpu_svd.cpp
+++ b/src/backend/opencl/cpu/cpu_svd.cpp
@@ -67,23 +67,28 @@ namespace cpu
         int M = iDims[0];
         int N = iDims[1];
 
-        Tr *sPtr = getMappedPtr<Tr>(s.get());
-        T  *uPtr = getMappedPtr<T >(u.get());
-        T  *vPtr = getMappedPtr<T >(vt.get());
-        T  *iPtr = getMappedPtr<T >(in.get());
+        std::shared_ptr<Tr> sPtr = s.getMappedPtr();
+        std::shared_ptr<T > uPtr = u.getMappedPtr();
+        std::shared_ptr<T > vPtr = vt.getMappedPtr();
+        std::shared_ptr<T > iPtr = in.getMappedPtr();
 
 #if defined(USE_MKL) || defined(__APPLE__)
-        svd_func<T, Tr>()(AF_LAPACK_COL_MAJOR, 'A', M, N, iPtr, in.strides()[1],
-                          sPtr, uPtr, u.strides()[1], vPtr, vt.strides()[1]);
+        svd_func<T, Tr>()(AF_LAPACK_COL_MAJOR, 'A',
+                          M, N,
+                          iPtr.get(), in.strides()[1],
+                          sPtr.get(),
+                          uPtr.get(), u.strides()[1],
+                          vPtr.get(), vt.strides()[1]);
 #else
         std::vector<Tr> superb(std::min(M, N));
-        svd_func<T, Tr>()(AF_LAPACK_COL_MAJOR, 'A', 'A', M, N, iPtr, in.strides()[1],
-                          sPtr, uPtr, u.strides()[1], vPtr, vt.strides()[1], &superb[0]);
+        svd_func<T, Tr>()(AF_LAPACK_COL_MAJOR, 'A', 'A',
+                          M, N,
+                          iPtr.get(), in.strides()[1],
+                          sPtr.get(),
+                          uPtr.get(), u.strides()[1],
+                          vPtr.get(), vt.strides()[1],
+                          &superb[0]);
 #endif
-        unmapPtr(s.get() , sPtr);
-        unmapPtr(u.get() , uPtr);
-        unmapPtr(vt.get(), vPtr);
-        unmapPtr(in.get(), iPtr);
     }
 
     template <typename T, typename Tr>
diff --git a/src/backend/opencl/memory.cpp b/src/backend/opencl/memory.cpp
index 924e370..cf3f4cc 100644
--- a/src/backend/opencl/memory.cpp
+++ b/src/backend/opencl/memory.cpp
@@ -14,6 +14,7 @@
 #include <iomanip>
 #include <string>
 #include <types.hpp>
+#include <err_opencl.hpp>
 
 namespace opencl
 {
@@ -281,29 +282,6 @@ namespace opencl
         return bufferPush((cl::Buffer *)ptr);
     }
 
-    template<typename T>
-    T *getMappedPtr(const cl::Buffer *buf)
-    {
-        int n = getActiveDeviceId();
-        mem_iter iter = memory_maps[n].find(const_cast<cl::Buffer*>(buf));
-
-        if (iter == memory_maps[n].end()) {
-            // Buffer not found in memory manager
-            // Very Very Bad
-            return NULL;
-        }
-        size_t alloc_bytes = iter->second.bytes;
-
-        T *ptr = (T*)getQueue().enqueueMapBuffer(
-                     *buf, true, CL_MAP_READ, 0, alloc_bytes);
-        return ptr;
-    }
-
-    void unmapPtr(const cl::Buffer *buf, void *ptr)
-    {
-        getQueue().enqueueUnmapMemObject(*buf, ptr);
-    }
-
     // pinned memory manager
     typedef struct {
         cl::Buffer *buf;
@@ -426,7 +404,6 @@ namespace opencl
     template void memPush(const T* ptr);                        \
     template T* pinnedAlloc(const size_t &elements);            \
     template void pinnedFree(T* ptr);                           \
-    template T* getMappedPtr(const cl::Buffer *buf);            \
 
     INSTANTIATE(float)
     INSTANTIATE(cfloat)
@@ -440,6 +417,4 @@ namespace opencl
     INSTANTIATE(uintl)
     INSTANTIATE(short)
     INSTANTIATE(ushort)
-
-    template void* getMappedPtr(const cl::Buffer *buf);
 }
diff --git a/src/backend/opencl/memory.hpp b/src/backend/opencl/memory.hpp
index f337a7a..96292cd 100644
--- a/src/backend/opencl/memory.hpp
+++ b/src/backend/opencl/memory.hpp
@@ -32,9 +32,6 @@ namespace opencl
     template<typename T> void memPop(const T *ptr);
     template<typename T> void memPush(const T *ptr);
 
-    template<typename T> T *getMappedPtr(const cl::Buffer *buf);
-    void unmapPtr(const cl::Buffer *buf, void *ptr);
-
     template<typename T> T* pinnedAlloc(const size_t &elements);
     template<typename T> void pinnedFree(T* ptr);
 

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