-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
127 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#ifndef _MOBULA_LINALG_H_ | ||
#define _MOBULA_LINALG_H_ | ||
|
||
#include "defines.h" | ||
#include "context/context.h" | ||
|
||
namespace mobula { | ||
|
||
// out[i, j] = sum(a[i, :] * b[:, j]) | ||
template <typename T> | ||
MOBULA_KERNEL linalg_gemm_ff_kernel(const int n, const T *a, const T *b, const int U, const int J, T *out) { | ||
parfor(n, [&](int i) { | ||
for (int u = 0; u < U; ++u) { | ||
for (int j = 0; j < J; ++j) { | ||
out[i * J + j] += a[i * U + u] * b[u * J + j]; | ||
} | ||
} | ||
}); | ||
} | ||
|
||
// out[i, j] = sum(a[i, :] * b[j, :]) | ||
template <typename T> | ||
MOBULA_KERNEL linalg_gemm_ft_kernel(const int n, const T *a, const T *b, const int U, const int J, T *out) { | ||
parfor(n, [&](int i) { | ||
for (int j = 0; j < J; ++j) { | ||
for (int u = 0; u < U; ++u) { | ||
out[i * J + j] += a[i * U + u] * b[j * U + u]; | ||
} | ||
} | ||
}); | ||
} | ||
|
||
// out[i, j] = sum(a[:, i] * b[:, j]) | ||
template <typename T> | ||
MOBULA_KERNEL linalg_gemm_tf_kernel(const int n, const T *a, const T *b, const int I, const int J, T *out) { | ||
parfor(n, [&](int u) { | ||
for (int i = 0; i < I; ++i) { | ||
for (int j = 0; j < J; ++j) { | ||
out[i * J + j] += a[u * I + i] * b[u * J + j]; | ||
} | ||
} | ||
}); | ||
} | ||
|
||
// out[i, j] = sum(a[:, i] * b[j, :]) | ||
template <typename T> | ||
MOBULA_KERNEL linalg_gemm_tt_kernel(const int n, const T *a, const T *b, const int I, const int U, const int J, T *out) { | ||
parfor(n, [&](int j) { | ||
for (int u = 0; u < U; ++u) { | ||
for (int i = 0; i < I; ++i) { | ||
out[i * J + j] += a[u * I + i] * b[j * U + u]; | ||
} | ||
} | ||
}); | ||
} | ||
|
||
} | ||
|
||
extern "C" { | ||
using namespace mobula; | ||
|
||
void linalg_gemm_ff(const DType *a, const DType *b, const int I, const int U, const int J, DType *out) { | ||
const int N = I; | ||
KERNEL_RUN(linalg_gemm_ff_kernel<DType>, N)(N, a, b, U, J, out); | ||
} | ||
|
||
void linalg_gemm_ft(const DType *a, const DType *b, const int I, const int U, const int J, DType *out) { | ||
const int N = I; | ||
KERNEL_RUN(linalg_gemm_ft_kernel<DType>, N)(N, a, b, U, J, out); | ||
} | ||
|
||
void linalg_gemm_tf(const DType *a, const DType *b, const int I, const int U, const int J, DType *out) { | ||
const int N = U; | ||
KERNEL_RUN(linalg_gemm_tf_kernel<DType>, N)(N, a, b, I, J, out); | ||
} | ||
|
||
void linalg_gemm_tt(const DType *a, const DType *b, const int I, const int U, const int J, DType *out) { | ||
const int N = J; | ||
KERNEL_RUN(linalg_gemm_tt_kernel<DType>, N)(N, a, b, I, U, J, out); | ||
} | ||
|
||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters