| 1 |
// Basic BLAS/LAPACK example adapted from a test in Spack for OpenBLAS |
| 2 |
|
| 3 |
#include "cblas_lapack.h" |
| 4 |
|
| 5 |
int main(void) { |
| 6 |
// CBLAS: |
| 7 |
blas_int n_elem = 9; |
| 8 |
blas_int incx = 1; |
| 9 |
double A[6] = {1.0, 2.0, 1.0, -3.0, 4.0, -1.0}; |
| 10 |
double B[6] = {1.0, 2.0, 1.0, -3.0, 4.0, -1.0}; |
| 11 |
double C[9] = {.5, .5, .5, .5, .5, .5, .5, .5, .5}; |
| 12 |
double norm; |
| 13 |
|
| 14 |
CBLAS_FUNC(cblas_dgemm) |
| 15 |
(CblasColMajor, CblasNoTrans, CblasTrans, 3, 3, 2, 1, A, 3, B, 3, 2, C, 3); |
| 16 |
norm = CBLAS_FUNC(cblas_dnrm2)(n_elem, C, incx) - 28.017851; |
| 17 |
|
| 18 |
if (fabs(norm) < 1e-5) { |
| 19 |
printf("OK: CBLAS result using dgemm and dnrm2 as expected\n"); |
| 20 |
} else { |
| 21 |
fprintf(stderr, "CBLAS result using dgemm and dnrm2 incorrect: %f\n", norm); |
| 22 |
exit(EXIT_FAILURE); |
| 23 |
} |
| 24 |
|
| 25 |
// LAPACK: |
| 26 |
double m[] = {3, 1, 3, 1, 5, 9, 2, 6, 5}; |
| 27 |
double x[] = {-1, 3, -3}; |
| 28 |
lapack_int ipiv[3]; |
| 29 |
lapack_int info; |
| 30 |
lapack_int n = 1; |
| 31 |
lapack_int nrhs = 1; |
| 32 |
lapack_int lda = 3; |
| 33 |
lapack_int ldb = 3; |
| 34 |
|
| 35 |
LAPACK_FUNC(dgesv)(&n, &nrhs, &m[0], &lda, ipiv, &x[0], &ldb, &info); |
| 36 |
n_elem = 3; |
| 37 |
norm = CBLAS_FUNC(cblas_dnrm2)(n_elem, x, incx) - 4.255715; |
| 38 |
|
| 39 |
if (fabs(norm) < 1e-5) { |
| 40 |
printf("OK: LAPACK result using dgesv as expected\n"); |
| 41 |
} else { |
| 42 |
fprintf(stderr, "LAPACK result using dgesv incorrect: %f\n", norm); |
| 43 |
exit(EXIT_FAILURE); |
| 44 |
} |
| 45 |
|
| 46 |
return 0; |
| 47 |
} |