-
Notifications
You must be signed in to change notification settings - Fork 555
Expand file tree
/
Copy pathlu.cpp
More file actions
140 lines (112 loc) · 4.13 KB
/
Copy pathlu.cpp
File metadata and controls
140 lines (112 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*******************************************************
* Copyright (c) 2022, 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 <err_oneapi.hpp>
#include <lu.hpp>
#if defined(WITH_LINEAR_ALGEBRA)
#include <blas.hpp>
#include <copy.hpp>
#include <kernel/lu_split.hpp>
#include <memory.hpp>
#include <oneapi/mkl/lapack.hpp>
#include <platform.hpp>
namespace arrayfire {
namespace oneapi {
Array<int> convertPivot(sycl::buffer<int64_t> &pivot, int in_sz, int out_sz,
bool convert_pivot) {
std::vector<int> d_po(out_sz);
for (int i = 0; i < out_sz; i++) { d_po[i] = i; }
auto d_pi = pivot.get_host_access();
if (convert_pivot) {
for (int j = 0; j < in_sz; j++) {
// 1 indexed in pivot
std::swap(d_po[j], d_po[d_pi[j] - 1]);
}
Array<int> res = createHostDataArray(dim4(out_sz), &d_po[0]);
return res;
} else {
d_po.resize(in_sz);
for (int j = 0; j < in_sz; j++) { d_po[j] = static_cast<int>(d_pi[j]); }
}
Array<int> res = createHostDataArray(dim4(in_sz), &d_po[0]);
return res;
}
template<typename T>
void lu(Array<T> &lower, Array<T> &upper, Array<int> &pivot,
const Array<T> &in) {
dim4 iDims = in.dims();
int M = iDims[0];
int N = iDims[1];
int MN = std::min(M, N);
Array<T> in_copy = copyArray<T>(in);
pivot = lu_inplace(in_copy);
// SPLIT into lower and upper
dim4 ldims(M, MN);
dim4 udims(MN, N);
lower = createEmptyArray<T>(ldims);
upper = createEmptyArray<T>(udims);
kernel::lu_split<T>(lower, upper, in_copy);
}
template<typename T>
Array<int> lu_inplace(Array<T> &in, const bool convert_pivot) {
dim4 iDims = in.dims();
dim4 iStrides = in.strides();
int64_t M = iDims[0];
int64_t N = iDims[1];
int64_t MN = std::min(M, N);
int64_t LDA = iStrides[1];
std::int64_t scratchpad_size =
::oneapi::mkl::lapack::getrf_scratchpad_size<T>(getQueue(), M, N, LDA);
auto ipiv = memAlloc<int64_t>(MN);
auto scratchpad = memAlloc<compute_t<T>>(scratchpad_size);
sycl::buffer<compute_t<T>> in_buffer =
in.template getBufferWithOffset<compute_t<T>>();
::oneapi::mkl::lapack::getrf(getQueue(), M, N, in_buffer, LDA, *ipiv,
*scratchpad, scratchpad->size());
Array<int> pivot = convertPivot(*ipiv, MN, M, convert_pivot);
return pivot;
}
bool isLAPACKAvailable() { return true; }
#define INSTANTIATE_LU(T) \
template Array<int> lu_inplace<T>(Array<T> & in, \
const bool convert_pivot); \
template void lu<T>(Array<T> & lower, Array<T> & upper, \
Array<int> & pivot, const Array<T> &in);
INSTANTIATE_LU(float)
INSTANTIATE_LU(cfloat)
INSTANTIATE_LU(double)
INSTANTIATE_LU(cdouble)
} // namespace oneapi
} // namespace arrayfire
#else // WITH_LINEAR_ALGEBRA
namespace arrayfire {
namespace oneapi {
template<typename T>
void lu(Array<T> &lower, Array<T> &upper, Array<int> &pivot,
const Array<T> &in) {
AF_ERROR("Linear Algebra is disabled on OneAPI backend",
AF_ERR_NOT_CONFIGURED);
}
template<typename T>
Array<int> lu_inplace(Array<T> &in, const bool convert_pivot) {
AF_ERROR("Linear Algebra is disabled on OneAPI backend",
AF_ERR_NOT_CONFIGURED);
}
bool isLAPACKAvailable() { return false; }
#define INSTANTIATE_LU(T) \
template Array<int> lu_inplace<T>(Array<T> & in, \
const bool convert_pivot); \
template void lu<T>(Array<T> & lower, Array<T> & upper, \
Array<int> & pivot, const Array<T> &in);
INSTANTIATE_LU(float)
INSTANTIATE_LU(cfloat)
INSTANTIATE_LU(double)
INSTANTIATE_LU(cdouble)
} // namespace oneapi
} // namespace arrayfire
#endif // WITH_LINEAR_ALGEBRA