Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor qr,solve to use padArrayBorders
  • Loading branch information
9prady9 committed Apr 29, 2020
commit c9d11cc6f118ac9ebbadfaeea13d612c96a59b07
16 changes: 11 additions & 5 deletions src/backend/cpu/qr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/

#include <common/err_common.hpp>
#include <qr.hpp>

#if defined(WITH_LINEAR_ALGEBRA)
#include <err_cpu.hpp>
#include <handle.hpp>

#if defined(WITH_LINEAR_ALGEBRA)
#include <copy.hpp>
#include <lapack_helper.hpp>
#include <math.hpp>
#include <platform.hpp>
#include <queue.hpp>
#include <triangle.hpp>
#include <af/dim4.hpp>
#include <cassert>

using af::dim4;

namespace cpu {

Expand Down Expand Up @@ -67,7 +68,12 @@ void qr(Array<T> &q, Array<T> &r, Array<T> &t, const Array<T> &in) {
int M = iDims[0];
int N = iDims[1];

q = padArray<T, T>(in, dim4(M, max(M, N)));
const dim4 NullShape(0, 0, 0, 0);

dim4 endPadding(M - iDims[0], max(M, N) - iDims[1], 0, 0);
q = (endPadding == NullShape
? copyArray(in)
: padArrayBorders(in, NullShape, endPadding, AF_PAD_ZERO));
q.resetDims(iDims);
t = qr_inplace(q);

Expand Down
18 changes: 12 additions & 6 deletions src/backend/cpu/solve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/

#include <common/err_common.hpp>
#include <solve.hpp>

#if defined(WITH_LINEAR_ALGEBRA)
#include <err_cpu.hpp>
#include <handle.hpp>

#if defined(WITH_LINEAR_ALGEBRA)
#include <copy.hpp>
#include <lapack_helper.hpp>
#include <math.hpp>
#include <platform.hpp>
#include <queue.hpp>
#include <af/dim4.hpp>
#include <cassert>

using af::dim4;

namespace cpu {

Expand Down Expand Up @@ -116,12 +116,18 @@ Array<T> solve(const Array<T> &a, const Array<T> &b,
return triangleSolve<T>(a, b, options);
}

const dim4 NullShape(0, 0, 0, 0);

int M = a.dims()[0];
int N = a.dims()[1];
int K = b.dims()[1];

Array<T> A = copyArray<T>(a);
Array<T> B = padArray<T, T>(b, dim4(max(M, N), K));

dim4 endPadding(max(M, N) - b.dims()[0], K - b.dims()[1], 0, 0);
Array<T> B = (endPadding == NullShape
? copyArray(b)
: padArrayBorders(b, NullShape, endPadding, AF_PAD_ZERO));

if (M == N) {
Array<int> pivot = createEmptyArray<int>(dim4(N, 1, 1));
Expand Down
8 changes: 2 additions & 6 deletions src/backend/cuda/qr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,17 @@
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/

#include <common/err_common.hpp>
#include <qr.hpp>

#include <copy.hpp>
#include <cublas_v2.h>
#include <cusolverDn.hpp>
#include <identity.hpp>
#include <kernel/triangle.hpp>
#include <math.hpp>
#include <memory.hpp>
#include <platform.hpp>

#include <common/err_common.hpp>
#include <math.hpp>

#include <kernel/triangle.hpp>

namespace cuda {

// cusolverStatus_t cusolverDn<>geqrf_bufferSize(
Expand Down
9 changes: 6 additions & 3 deletions src/backend/cuda/solve.cu
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
#include <qr.hpp>
#include <transpose.hpp>

#include <cstdio>

namespace cuda {

// cusolverStatus_t cusolverDn<>getrs(
Expand Down Expand Up @@ -214,6 +212,8 @@ Array<T> leastSquares(const Array<T> &a, const Array<T> &b) {
Array<T> B = createEmptyArray<T>(dim4());

if (M < N) {
const dim4 NullShape(0, 0, 0, 0);

// Least squres for this case is solved using the following
// solve(A, B) == matmul(Q, Xpad);
// Where:
Expand All @@ -224,7 +224,10 @@ Array<T> leastSquares(const Array<T> &a, const Array<T> &b) {

// QR is performed on the transpose of A
Array<T> A = transpose<T>(a, true);
B = padArray<T, T>(b, dim4(N, K), scalar<T>(0));
dim4 endPadding(N - b.dims()[0], K - b.dims()[1], 0, 0);
B = (endPadding == NullShape
? copyArray(b)
: padArrayBorders(b, NullShape, endPadding, AF_PAD_ZERO));

int lwork = 0;

Expand Down
8 changes: 6 additions & 2 deletions src/backend/opencl/cpu/cpu_qr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ void qr(Array<T> &q, Array<T> &r, Array<T> &t, const Array<T> &in) {
int M = iDims[0];
int N = iDims[1];

dim4 padDims(M, max(M, N));
q = padArray<T, T>(in, padDims, scalar<T>(0));
const dim4 NullShape(0, 0, 0, 0);

dim4 endPadding(M - iDims[0], max(M, N) - iDims[1], 0, 0);
q = (endPadding == NullShape
? copyArray(in)
: padArrayBorders(in, NullShape, endPadding, AF_PAD_ZERO));
q.resetDims(iDims);
t = qr_inplace(q);

Expand Down
7 changes: 6 additions & 1 deletion src/backend/opencl/cpu/cpu_solve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,17 @@ Array<T> solve(const Array<T> &a, const Array<T> &b,
return triangleSolve<T>(a, b, options);
}

const dim4 NullShape(0, 0, 0, 0);

int M = a.dims()[0];
int N = a.dims()[1];
int K = b.dims()[1];

Array<T> A = copyArray<T>(a);
Array<T> B = padArray<T, T>(b, dim4(max(M, N), K), scalar<T>(0));
dim4 endPadding(max(M, N) - b.dims()[0], K - b.dims()[1], 0, 0);
Array<T> B = (endPadding == NullShape
? copyArray(b)
: padArrayBorders(b, NullShape, endPadding, AF_PAD_ZERO));

mapped_ptr<T> aPtr = A.getMappedPtr();
mapped_ptr<T> bPtr = B.getMappedPtr();
Expand Down
15 changes: 10 additions & 5 deletions src/backend/opencl/qr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/

#include <blas.hpp>
#include <copy.hpp>
#include <err_opencl.hpp>
#include <qr.hpp>

#include <err_opencl.hpp>

#if defined(WITH_LINEAR_ALGEBRA)

#include <blas.hpp>
#include <copy.hpp>
#include <cpu/cpu_qr.hpp>
#include <identity.hpp>
#include <kernel/triangle.hpp>
Expand All @@ -28,13 +29,17 @@ template<typename T>
void qr(Array<T> &q, Array<T> &r, Array<T> &t, const Array<T> &orig) {
if (OpenCLCPUOffload()) { return cpu::qr(q, r, t, orig); }

const dim4 NullShape(0, 0, 0, 0);

dim4 iDims = orig.dims();
int M = iDims[0];
int N = iDims[1];

dim4 pDims(M, std::max(M, N));
dim4 endPadding(M - iDims[0], max(M, N) - iDims[1], 0, 0);
Array<T> in =
padArray<T, T>(orig, pDims, scalar<T>(0)); // copyArray<T>(orig);
(endPadding == NullShape
? copyArray(orig)
: padArrayBorders(orig, NullShape, endPadding, AF_PAD_ZERO));
in.resetDims(iDims);

int MN = std::min(M, N);
Expand Down
18 changes: 9 additions & 9 deletions src/backend/opencl/solve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,24 @@
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/

#include <err_opencl.hpp>
#include <solve.hpp>

#include <err_opencl.hpp>

#if defined(WITH_LINEAR_ALGEBRA)
#include <blas.hpp>
#include <copy.hpp>
#include <err_opencl.hpp>
#include <cpu/cpu_solve.hpp>
#include <lu.hpp>
#include <magma/magma.h>
#include <magma/magma_blas.h>
#include <magma/magma_data.h>
#include <magma/magma_helper.h>
#include <math.hpp>
#include <platform.hpp>
#include <transpose.hpp>
#include <af/opencl.h>

#include <algorithm>
#include <string>

#include <cpu/cpu_solve.hpp>
#include <platform.hpp>

namespace opencl {

template<typename T>
Expand Down Expand Up @@ -107,7 +103,11 @@ Array<T> leastSquares(const Array<T> &a, const Array<T> &b) {
Array<T> A = transpose<T>(a, true);

#if UNMQR
B = padArray<T, T>(b, dim4(N, K), scalar<T>(0));
const dim4 NullShape(0, 0, 0, 0);
dim4 endPadding(N - b.dims()[0], K - b.dims()[1], 0, 0);
B = (endPadding == NullShape
? copyArray(b)
: padArrayBorders(b, NullShape, endPadding, AF_PAD_ZERO));
B.resetDims(dim4(M, K));
#else
B = copyArray<T>(b);
Expand Down