-
Notifications
You must be signed in to change notification settings - Fork 549
Expand file tree
/
Copy pathblas.cpp
More file actions
95 lines (81 loc) · 3.37 KB
/
blas.cpp
File metadata and controls
95 lines (81 loc) · 3.37 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
/*******************************************************
* 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 <af/array.h>
#include <af/blas.h>
#include "error.hpp"
namespace af {
array matmul(const array &lhs, const array &rhs, const matProp optLhs,
const matProp optRhs) {
af_array out = 0;
AF_THROW(af_matmul(&out, lhs.get(), rhs.get(), optLhs, optRhs));
return array(out);
}
array matmulNT(const array &lhs, const array &rhs) {
af_array out = 0;
AF_THROW(af_matmul(&out, lhs.get(), rhs.get(), AF_MAT_NONE, AF_MAT_TRANS));
return array(out);
}
array matmulTN(const array &lhs, const array &rhs) {
af_array out = 0;
AF_THROW(af_matmul(&out, lhs.get(), rhs.get(), AF_MAT_TRANS, AF_MAT_NONE));
return array(out);
}
array matmulTT(const array &lhs, const array &rhs) {
af_array out = 0;
AF_THROW(af_matmul(&out, lhs.get(), rhs.get(), AF_MAT_TRANS, AF_MAT_TRANS));
return array(out);
}
array matmul(const array &a, const array &b, const array &c) {
dim_t tmp1 = a.dims(0) * b.dims(1);
dim_t tmp2 = b.dims(0) * c.dims(1);
if (tmp1 < tmp2) {
return matmul(matmul(a, b), c);
} else {
return matmul(a, matmul(b, c));
}
}
array matmul(const array &a, const array &b, const array &c, const array &d) {
dim_t tmp1 = a.dims(0) * c.dims(1);
dim_t tmp2 = b.dims(0) * d.dims(1);
if (tmp1 < tmp2) {
return matmul(matmul(a, b, c), d);
} else {
return matmul(a, matmul(b, c, d));
}
}
array dot(const array &lhs, const array &rhs, const matProp optLhs,
const matProp optRhs) {
af_array out = 0;
AF_THROW(af_dot(&out, lhs.get(), rhs.get(), optLhs, optRhs));
return array(out);
}
#define INSTANTIATE_REAL(TYPE) \
template<> \
AFAPI TYPE dot(const array &lhs, const array &rhs, const matProp optLhs, \
const matProp optRhs) { \
double rval = 0, ival = 0; \
AF_THROW( \
af_dot_all(&rval, &ival, lhs.get(), rhs.get(), optLhs, optRhs)); \
return (TYPE)(rval); \
}
#define INSTANTIATE_CPLX(TYPE, REAL) \
template<> \
AFAPI TYPE dot(const array &lhs, const array &rhs, const matProp optLhs, \
const matProp optRhs) { \
double rval = 0, ival = 0; \
AF_THROW( \
af_dot_all(&rval, &ival, lhs.get(), rhs.get(), optLhs, optRhs)); \
TYPE out((REAL)rval, (REAL)ival); \
return out; \
}
INSTANTIATE_REAL(float)
INSTANTIATE_REAL(double)
INSTANTIATE_CPLX(cfloat, float)
INSTANTIATE_CPLX(cdouble, double)
} // namespace af