|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +// ARM replacement for Intel MKL (libMklImports.so). |
| 6 | +// |
| 7 | +// This provides a small, self-contained libMklImports for arm/arm64 that |
| 8 | +// covers exactly the symbols SymSGD needs, with no external BLAS dependency. |
| 9 | +// That is important because the cross-compilation sysroots used in CI do not |
| 10 | +// ship OpenBLAS (or any system BLAS), so linking against one is not an option. |
| 11 | +// |
| 12 | +// SymSGD uses only four CBLAS routines: |
| 13 | +// * cblas_sdot / cblas_saxpy - dense single-precision dot and AXPY, |
| 14 | +// * cblas_sdoti / cblas_saxpyi - their sparse counterparts (MKL extensions). |
| 15 | +// All four are implemented below as plain C loops. With -O3 the compiler |
| 16 | +// autovectorizes the dense paths to NEON, matching hand-written BLAS closely. |
| 17 | +// |
| 18 | +// MKL DFTI (FFT) functions are stubbed — they are referenced by the managed |
| 19 | +// MKL Components initializer but not used by SymSGD. The stubs return error |
| 20 | +// codes so any actual FFT call fails cleanly rather than crashing. |
| 21 | + |
| 22 | +// The native build is compiled with -fvisibility=hidden, so every symbol that |
| 23 | +// must be visible to SymSgdNative (the CBLAS routines) or to the managed |
| 24 | +// P/Invoke layer (DftiErrorMessage) has to be exported explicitly. |
| 25 | +#if defined(_WIN32) |
| 26 | +#define MKLIMPORTS_EXPORT __declspec(dllexport) |
| 27 | +#else |
| 28 | +#define MKLIMPORTS_EXPORT __attribute__((visibility("default"))) |
| 29 | +#endif |
| 30 | + |
| 31 | +// --- Dense BLAS (CBLAS, level 1) --- |
| 32 | + |
| 33 | +MKLIMPORTS_EXPORT float cblas_sdot(const int n, const float *x, const int incx, |
| 34 | + const float *y, const int incy) |
| 35 | +{ |
| 36 | + float result = 0.0f; |
| 37 | + if (incx == 1 && incy == 1) |
| 38 | + { |
| 39 | + for (int i = 0; i < n; i++) |
| 40 | + result += x[i] * y[i]; |
| 41 | + } |
| 42 | + else |
| 43 | + { |
| 44 | + int ix = incx < 0 ? (1 - n) * incx : 0; |
| 45 | + int iy = incy < 0 ? (1 - n) * incy : 0; |
| 46 | + for (int i = 0; i < n; i++, ix += incx, iy += incy) |
| 47 | + result += x[ix] * y[iy]; |
| 48 | + } |
| 49 | + return result; |
| 50 | +} |
| 51 | + |
| 52 | +MKLIMPORTS_EXPORT void cblas_saxpy(const int n, const float a, const float *x, const int incx, |
| 53 | + float *y, const int incy) |
| 54 | +{ |
| 55 | + if (a == 0.0f) |
| 56 | + return; |
| 57 | + if (incx == 1 && incy == 1) |
| 58 | + { |
| 59 | + for (int i = 0; i < n; i++) |
| 60 | + y[i] += a * x[i]; |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + int ix = incx < 0 ? (1 - n) * incx : 0; |
| 65 | + int iy = incy < 0 ? (1 - n) * incy : 0; |
| 66 | + for (int i = 0; i < n; i++, ix += incx, iy += incy) |
| 67 | + y[iy] += a * x[ix]; |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// --- Sparse BLAS (MKL extensions, not in standard BLAS) --- |
| 72 | + |
| 73 | +MKLIMPORTS_EXPORT void cblas_saxpyi(const int nz, const float a, |
| 74 | + const float *x, const int *indx, float *y) |
| 75 | +{ |
| 76 | + for (int i = 0; i < nz; i++) |
| 77 | + y[indx[i]] += a * x[i]; |
| 78 | +} |
| 79 | + |
| 80 | +MKLIMPORTS_EXPORT float cblas_sdoti(const int nz, const float *x, |
| 81 | + const int *indx, const float *y) |
| 82 | +{ |
| 83 | + float result = 0.0f; |
| 84 | + for (int i = 0; i < nz; i++) |
| 85 | + result += x[i] * y[indx[i]]; |
| 86 | + return result; |
| 87 | +} |
| 88 | + |
| 89 | +// --- DFTI (FFT) stubs --- |
| 90 | + |
| 91 | +MKLIMPORTS_EXPORT const char* DftiErrorMessage(long status) |
| 92 | +{ |
| 93 | + return "DFTI not available (arm64 MKL shim build)"; |
| 94 | +} |
| 95 | + |
| 96 | +MKLIMPORTS_EXPORT long DftiCreateDescriptor(void **h, int precision, int domain, int dim, ...) |
| 97 | +{ |
| 98 | + *h = (void*)0; |
| 99 | + return -1; |
| 100 | +} |
| 101 | + |
| 102 | +MKLIMPORTS_EXPORT long DftiSetValue(void *h, int param, ...) |
| 103 | +{ |
| 104 | + return -1; |
| 105 | +} |
| 106 | + |
| 107 | +MKLIMPORTS_EXPORT long DftiCommitDescriptor(void *h) { return -1; } |
| 108 | +MKLIMPORTS_EXPORT long DftiComputeForward(void *h, ...) { return -1; } |
| 109 | +MKLIMPORTS_EXPORT long DftiComputeBackward(void *h, ...) { return -1; } |
| 110 | +MKLIMPORTS_EXPORT long DftiFreeDescriptor(void **h) |
| 111 | +{ |
| 112 | + // Match MKL's contract: clear the caller's handle after freeing so callers |
| 113 | + // that rely on the descriptor being nulled out (e.g. the managed |
| 114 | + // FreeDescriptor(ref IntPtr) P/Invoke) behave correctly. |
| 115 | + if (h != (void*)0) |
| 116 | + *h = (void*)0; |
| 117 | + return 0; |
| 118 | +} |
0 commit comments