[arrayfire] 114/248: Added CPU fallback for CUDA Inverse when CUDA older than 7

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Tue Nov 17 15:54:11 UTC 2015


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

ghisvail-guest pushed a commit to branch dfsg-clean
in repository arrayfire.

commit adbd62b3810cb28482e485b6688ef2a3f4f35f08
Author: Shehzan Mohammed <shehzan at arrayfire.com>
Date:   Thu Oct 8 19:12:26 2015 -0400

    Added CPU fallback for CUDA Inverse when CUDA older than 7
---
 src/backend/cuda/cpu_lapack/cpu_inverse.cpp | 92 +++++++++++++++++++++++++++++
 src/backend/cuda/cpu_lapack/cpu_inverse.hpp | 19 ++++++
 src/backend/cuda/inverse.cu                 | 22 +++++++
 3 files changed, 133 insertions(+)

diff --git a/src/backend/cuda/cpu_lapack/cpu_inverse.cpp b/src/backend/cuda/cpu_lapack/cpu_inverse.cpp
new file mode 100644
index 0000000..a0ddf39
--- /dev/null
+++ b/src/backend/cuda/cpu_lapack/cpu_inverse.cpp
@@ -0,0 +1,92 @@
+/*******************************************************
+ * 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
+ ********************************************************/
+
+#if defined(WITH_CPU_LINEAR_ALGEBRA)
+
+#include <inverse.hpp>
+#include <err_common.hpp>
+
+#include <af/dim4.hpp>
+#include <handle.hpp>
+#include <identity.hpp>
+#include <copy.hpp>
+#include <iostream>
+#include <cassert>
+
+#include "lapack_helper.hpp"
+#include <cpu_lapack/cpu_lu.hpp>
+#include <cpu_lapack/cpu_solve.hpp>
+
+namespace cuda
+{
+namespace cpu
+{
+
+template<typename T>
+using getri_func_def = int (*)(ORDER_TYPE, int,
+                               T *, int,
+                               const int *);
+
+#define INV_FUNC_DEF( FUNC )                                        \
+template<typename T> FUNC##_func_def<T> FUNC##_func();
+
+#define INV_FUNC( FUNC, TYPE, PREFIX )                              \
+template<> FUNC##_func_def<TYPE>     FUNC##_func<TYPE>()            \
+{ return & LAPACK_NAME(PREFIX##FUNC); }
+
+INV_FUNC_DEF( getri )
+INV_FUNC(getri , float  , s)
+INV_FUNC(getri , double , d)
+INV_FUNC(getri , cfloat , c)
+INV_FUNC(getri , cdouble, z)
+
+template<typename T>
+Array<T> inverse(const Array<T> &in)
+{
+    int M = in.dims()[0];
+    int N = in.dims()[1];
+
+    if (M != N) {
+        Array<T> I = identity<T>(in.dims());
+        return cpu::solve(in, I);
+    }
+
+    Array<T> A = copyArray<T>(in);
+
+    Array<int> pivot = lu_inplace<T>(A, false);
+
+    T *aPtr = pinnedAlloc<T>(A.elements());
+    int *pPtr = pinnedAlloc<int>(pivot.elements());
+    copyData(aPtr, A);
+    copyData(pPtr, pivot);
+
+    getri_func<T>()(AF_LAPACK_COL_MAJOR, M,
+                    aPtr, A.strides()[1],
+                    pPtr);
+
+    writeHostDataArray<T>(A, aPtr, A.elements() * sizeof(T));
+
+    pinnedFree(aPtr);
+    pinnedFree(pPtr);
+
+    return A;
+}
+
+#define INSTANTIATE(T)                                                                   \
+    template Array<T> inverse<T> (const Array<T> &in);
+
+INSTANTIATE(float)
+INSTANTIATE(cfloat)
+INSTANTIATE(double)
+INSTANTIATE(cdouble)
+
+}
+}
+
+#endif
diff --git a/src/backend/cuda/cpu_lapack/cpu_inverse.hpp b/src/backend/cuda/cpu_lapack/cpu_inverse.hpp
new file mode 100644
index 0000000..f45fdee
--- /dev/null
+++ b/src/backend/cuda/cpu_lapack/cpu_inverse.hpp
@@ -0,0 +1,19 @@
+/*******************************************************
+ * 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
+ ********************************************************/
+
+#include <Array.hpp>
+
+namespace cuda
+{
+namespace cpu
+{
+    template<typename T>
+    Array<T> inverse(const Array<T> &in);
+}
+}
diff --git a/src/backend/cuda/inverse.cu b/src/backend/cuda/inverse.cu
index 96295f3..7b2ae3b 100644
--- a/src/backend/cuda/inverse.cu
+++ b/src/backend/cuda/inverse.cu
@@ -36,6 +36,28 @@ INSTANTIATE(cdouble)
 
 }
 
+#elif defined(WITH_CPU_LINEAR_ALGEBRA)
+#include <cpu_lapack/cpu_inverse.hpp>
+
+namespace cuda
+{
+
+template<typename T>
+Array<T> inverse(const Array<T> &in)
+{
+    return cpu::inverse(in);
+}
+
+#define INSTANTIATE(T)                                                                   \
+    template Array<T> inverse<T> (const Array<T> &in);
+
+INSTANTIATE(float)
+INSTANTIATE(cfloat)
+INSTANTIATE(double)
+INSTANTIATE(cdouble)
+
+}
+
 #else
 namespace cuda
 {

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