150
|
1 //===- cblas.cpp - Simple Blas subset implementation ----------------------===//
|
|
2 //
|
|
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
4 // See https://llvm.org/LICENSE.txt for license information.
|
|
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
6 //
|
|
7 //===----------------------------------------------------------------------===//
|
|
8 //
|
|
9 // Simple Blas subset implementation.
|
|
10 //
|
|
11 //===----------------------------------------------------------------------===//
|
|
12
|
|
13 #include "include/cblas.h"
|
|
14 #include <assert.h>
|
|
15
|
|
16 extern "C" float cblas_sdot(const int N, const float *X, const int incX,
|
|
17 const float *Y, const int incY) {
|
|
18 float res = 0.0f;
|
|
19 for (int i = 0; i < N; ++i)
|
|
20 res += X[i * incX] * Y[i * incY];
|
|
21 return res;
|
|
22 }
|
|
23
|
|
24 extern "C" void cblas_sgemm(const enum CBLAS_ORDER Order,
|
|
25 const enum CBLAS_TRANSPOSE TransA,
|
|
26 const enum CBLAS_TRANSPOSE TransB, const int M,
|
|
27 const int N, const int K, const float alpha,
|
|
28 const float *A, const int lda, const float *B,
|
|
29 const int ldb, const float beta, float *C,
|
|
30 const int ldc) {
|
|
31 assert(Order == CBLAS_ORDER::CblasRowMajor);
|
|
32 assert(TransA == CBLAS_TRANSPOSE::CblasNoTrans);
|
|
33 assert(TransB == CBLAS_TRANSPOSE::CblasNoTrans);
|
|
34 for (int m = 0; m < M; ++m) {
|
|
35 auto *pA = A + m * lda;
|
|
36 auto *pC = C + m * ldc;
|
|
37 for (int n = 0; n < N; ++n) {
|
|
38 float c = pC[n];
|
|
39 float res = 0.0f;
|
|
40 for (int k = 0; k < K; ++k) {
|
|
41 auto *pB = B + k * ldb;
|
|
42 res += pA[k] * pB[n];
|
|
43 }
|
|
44 pC[n] = alpha * c + beta * res;
|
|
45 }
|
|
46 }
|
|
47 }
|