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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
/*
* Copyright (C) 2014, The University of Texas at Austin
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - Neither the name of The University of Texas at Austin nor the names
* of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/*
* Copyright (C) 2014-2015, Michael Lehn
*
* ulmBLAS adopted general ideas from BLIS. Using micro kernels from BLIS
* only requires minor modifications,
*
*/
#ifndef ULMBLAS_IMPL_LEVEL2_GEMV_TCC
#define ULMBLAS_IMPL_LEVEL2_GEMV_TCC 1
#include <ulmblas/impl/auxiliary/memorypool.h>
#include <ulmblas/impl/config/blocksize.h>
#include <ulmblas/impl/config/fusefactor.h>
#include <ulmblas/impl/level1/axpy.h>
#include <ulmblas/impl/level1/copy.h>
#include <ulmblas/impl/level1/dot.h>
#include <ulmblas/impl/level1/scal.h>
#include <ulmblas/impl/level1extensions/axpy2v.h>
#include <ulmblas/impl/level1extensions/axpyf.h>
#include <ulmblas/impl/level1extensions/dotxf.h>
#include <ulmblas/impl/level1extensions/gecopy.h>
#include <ulmblas/impl/level2/gemv.h>
#include <iostream>
namespace ulmBLAS {
template <typename IndexType, typename Alpha, typename TA, typename TX,
typename Beta, typename TY>
void
gemv(IndexType m,
IndexType n,
const Alpha &alpha,
bool conjA,
const TA *A,
IndexType incRowA,
IndexType incColA,
const TX *x,
IndexType incX,
const Beta &beta,
TY *y,
IndexType incY)
{
typedef typename std::common_type<Alpha, TA, TX, Beta, TY>::type T;
const IndexType UnitStride(1);
static const bool homogeneousTypes = std::is_same<T,Alpha>::value
&& std::is_same<T,TA>::value
&& std::is_same<T,TX>::value
&& std::is_same<T,TY>::value;
if (m<=0 || n<=0 || (alpha==Alpha(0) && beta==Beta(1))) {
return;
}
scal(m, beta, y, incY);
if (alpha==Alpha(0)) {
return;
}
//
// If all operands have the same element type and matrix A is col major we use
// fused axpy/acxpy operations.
//
if (homogeneousTypes && incRowA==UnitStride) {
if (!conjA) {
const IndexType bf = FuseFactor<T>::axpyf;
const IndexType nb = (n/bf)*bf;
for (IndexType j=0; j<nb; j+=bf) {
axpyf(m, alpha, &x[j*incX], incX,
&A[j*incColA], UnitStride, incColA,
y, incY);
}
for (IndexType j=nb; j<n; ++j) {
axpy(m, alpha*x[j*incX], &A[j*incColA], UnitStride, y, incY);
}
} else {
const IndexType bf = FuseFactor<T>::acxpyf;
const IndexType nb = (n/bf)*bf;
for (IndexType j=0; j<nb; j+=bf) {
acxpyf(m, alpha, &x[j*incX], incX,
&A[j*incColA], UnitStride, incColA,
y, incY);
}
for (IndexType j=nb; j<n; ++j) {
acxpy(m, alpha*x[j*incX], &A[j*incColA], UnitStride, y, incY);
}
}
//
// If all operands have the same element type and matrix A is row major we use
// fused dotu/dotc operations.
//
} else if (homogeneousTypes && incColA==UnitStride) {
if (!conjA) {
const IndexType bf = FuseFactor<T>::dotuxf;
const IndexType mb = (m/bf)*bf;
TY tmp[bf];
for (IndexType i=0; i<mb; i+=bf) {
ref::dotuxf(n, &A[i*incRowA], incRowA, UnitStride,
x, incX,
tmp, UnitStride);
for (IndexType l=0; l<bf; ++l) {
y[(i+l)*incY] += alpha*tmp[l];
}
}
for (IndexType i=mb; i<m; ++i) {
dotu(n, &A[i*incRowA], UnitStride, x, incX, tmp[0]);
y[i*incY] += alpha*tmp[0];
}
} else {
const IndexType bf = FuseFactor<T>::dotcxf;
const IndexType mb = (m/bf)*bf;
TY tmp[bf];
for (IndexType i=0; i<mb; i+=bf) {
ref::dotcxf(n, &A[i*incRowA], incRowA, UnitStride,
x, incX,
tmp, UnitStride);
for (IndexType l=0; l<bf; ++l) {
y[(i+l)*incY] += alpha*tmp[l];
}
}
for (IndexType i=mb; i<m; ++i) {
dotc(n, &A[i*incRowA], UnitStride, x, incX, tmp[0]);
y[i*incY] += alpha*tmp[0];
}
}
} else {
//
// Otherwise we pack operands.
//
static MemoryPool<T> memoryPool;
const bool packA = !((incRowA==UnitStride || incColA==UnitStride)
&& std::is_same<T,TA>::value
&& !conjA);
const bool packX = !(incX==UnitStride && std::is_same<T,TX>::value);
const bool packY = !(incY==UnitStride && std::is_same<T,TY>::value);
const IndexType MC = BlockSize<T>::MC;
const IndexType NC = BlockSize<T>::NC;
const T &alpha_ = alpha;
T *buffer_A = packA ? memoryPool.allocate(MC*NC) : 0;
T *buffer_x = packX ? memoryPool.allocate(NC) : 0;
T *buffer_y = packY ? memoryPool.allocate(MC) : 0;
const T *A_ = packA ? buffer_A : 0;
const T *x_ = packX ? buffer_x : 0;
const IndexType mb = (m+MC-1) / MC;
const IndexType nb = (n+NC-1) / NC;
const IndexType mc_ = m % MC;
const IndexType nc_ = n % NC;
for (IndexType j=0; j<nb; ++j) {
IndexType nc = (j!=nb-1 || nc_==0) ? NC : nc_;
if (packX) {
copy(nc, &x[j*NC*incX], incX, buffer_x, UnitStride);
} else {
x_ = &x[j*NC];
}
for (IndexType i=0; i<mb; ++i) {
IndexType mc = (i!=mb-1 || mc_==0) ? MC : mc_;
IndexType incRow_A, incCol_A;
if (packA) {
incRow_A = UnitStride;
incCol_A = mc;
gecopy(mc, nc, conjA,
&A[i*MC*incRowA+j*NC*incColA], incRowA, incColA,
buffer_A, incRow_A, incCol_A);
} else {
incRow_A = incRowA;
incCol_A = incColA;
A_ = &A[i*MC*incRowA+j*NC*incColA];
}
if (packY) {
gemv(mc, nc, alpha_,
A_, incRow_A, incCol_A,
x_, UnitStride,
T(0),
buffer_y, UnitStride);
axpy(mc, T(1), buffer_y, UnitStride, &y[i*MC*incY], incY);
} else {
gemv(mc, nc, alpha_,
A_, incRow_A, incCol_A,
x_, UnitStride,
T(1),
&y[i*MC*incY], UnitStride);
}
}
}
memoryPool.release(buffer_x);
memoryPool.release(buffer_y);
memoryPool.release(buffer_A);
}
}
template <typename IndexType, typename Alpha, typename TA, typename TX,
typename Beta, typename TY>
void
gemv(IndexType m,
IndexType n,
const Alpha &alpha,
const TA *A,
IndexType incRowA,
IndexType incColA,
const TX *x,
IndexType incX,
const Beta &beta,
TY *y,
IndexType incY)
{
gemv(m, n, alpha, false, A, incRowA, incColA, x, incX, beta, y, incY);
}
} // namespace ulmBLAS
#endif // ULMBLAS_IMPL_LEVEL2_GEMV_TCC
|