From b063ab42bff9a3382a6c79663d41d13a0c028c50 Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Wed, 27 Apr 2016 11:57:48 +0200 Subject: openblas: fix and cleanup the unit test --- .../repos/builtin/packages/openblas/package.py | 68 +++++++++------------- .../builtin/packages/openblas/test_cblas_dgemm.c | 54 ++++++++++++++--- .../packages/openblas/test_cblas_dgemm.output | 3 + 3 files changed, 74 insertions(+), 51 deletions(-) diff --git a/var/spack/repos/builtin/packages/openblas/package.py b/var/spack/repos/builtin/packages/openblas/package.py index 99649da9ca..72b0b65a2f 100644 --- a/var/spack/repos/builtin/packages/openblas/package.py +++ b/var/spack/repos/builtin/packages/openblas/package.py @@ -88,48 +88,33 @@ class Openblas(Package): self.spec.lapack_shared_lib = self.spec.blas_shared_lib def check_install(self, spec): - "Build and run a small program to test that we have Lapack symbols" + # TODO: Pull this out to the framework function which recieves a pair of xyz.c and xyz.output print "Checking Openblas installation..." - checkdir = "spack-check" - with working_dir(checkdir, create=True): - source = r""" -#include -#include -int main(void) { -int i=0; -double A[6] = {1.0, 2.0, 1.0, -3.0, 4.0, -1.0}; -double B[6] = {1.0, 2.0, 1.0, -3.0, 4.0, -1.0}; -double C[9] = {.5, .5, .5, .5, .5, .5, .5, .5, .5}; -cblas_dgemm(CblasColMajor, CblasNoTrans, CblasTrans, - 3, 3, 2, 1, A, 3, B, 3, 2, C, 3); -for (i = 0; i < 9; i++) - printf("%f\n", C[i]); -return 0; -} -""" - expected = """\ -11.000000 --9.000000 -5.000000 --9.000000 -21.000000 --1.000000 -5.000000 --1.000000 -3.000000 -""" - with open("check.c", 'w') as f: - f.write(source) - cc = which('cc') - # TODO: Automate these path and library settings - cc('-c', "-I%s" % join_path(spec.prefix, "include"), "check.c") - cc('-o', "check", "check.o", - "-L%s" % join_path(spec.prefix, "lib"), "-llapack", "-lblas", "-lpthread") - try: - check = Executable('./check') - output = check(return_output=True) - except: - output = "" + source_file = join_path(os.path.dirname(self.module.__file__), + 'test_cblas_dgemm.c') + output_file = join_path(os.path.dirname(self.module.__file__), + 'test_cblas_dgemm.output') + + with open(output_file, 'r') as f: + expected = f.read() + + cc = which('cc') + cc('-c', "-I%s" % join_path(spec.prefix, "include"), source_file) + link_flags = ["-L%s" % join_path(spec.prefix, "lib"), + "-llapack", + "-lblas", + "-lpthread" + ] + if '+openmp' in spec: + link_flags.extend([self.compiler.openmp_flag]) + cc('-o', "check", "test_cblas_dgemm.o", + *link_flags) + + try: + check = Executable('./check') + output = check(return_output=True) + except: + output = "" success = output == expected if not success: print "Produced output does not match expected output." @@ -142,4 +127,3 @@ return 0; print output print '-'*80 raise RuntimeError("Openblas install check failed") - shutil.rmtree(checkdir) diff --git a/var/spack/repos/builtin/packages/openblas/test_cblas_dgemm.c b/var/spack/repos/builtin/packages/openblas/test_cblas_dgemm.c index 634e99d20b..3813a23b69 100644 --- a/var/spack/repos/builtin/packages/openblas/test_cblas_dgemm.c +++ b/var/spack/repos/builtin/packages/openblas/test_cblas_dgemm.c @@ -1,13 +1,49 @@ #include #include + +double m[] = { + 3, 1, 3, + 1, 5, 9, + 2, 6, 5 +}; + +double x[] = { + -1, 3, -3 +}; + +#ifdef __cplusplus +extern "C" { +#endif + + void dgesv_(int *n, int *nrhs, double *a, int *lda, + int *ipivot, double *b, int *ldb, int *info); + +#ifdef __cplusplus +} +#endif + int main(void) { -int i=0; -double A[6] = {1.0, 2.0, 1.0, -3.0, 4.0, -1.0}; -double B[6] = {1.0, 2.0, 1.0, -3.0, 4.0, -1.0}; -double C[9] = {.5, .5, .5, .5, .5, .5, .5, .5, .5}; -cblas_dgemm(CblasColMajor, CblasNoTrans, CblasTrans, - 3, 3, 2, 1, A, 3, B, 3, 2, C, 3); -for (i = 0; i < 9; i++) - printf("%f\n", C[i]); -return 0; + int i; + // blas: + double A[6] = {1.0, 2.0, 1.0, -3.0, 4.0, -1.0}; + double B[6] = {1.0, 2.0, 1.0, -3.0, 4.0, -1.0}; + double C[9] = {.5, .5, .5, .5, .5, .5, .5, .5, .5}; + cblas_dgemm(CblasColMajor, CblasNoTrans, CblasTrans, + 3, 3, 2, 1, A, 3, B, 3, 2, C, 3); + for (i = 0; i < 9; i++) + printf("%f\n", C[i]); + + // lapack: + int ipiv[3]; + int j; + int info; + int n = 1; + int nrhs = 1; + int lda = 3; + int ldb = 3; + dgesv_(&n,&nrhs, &m[0], &lda, ipiv, &x[0], &ldb, &info); + for (i=0; i<3; ++i) + printf("%5.1f %3d\n", x[i], ipiv[i]); + + return 0; } diff --git a/var/spack/repos/builtin/packages/openblas/test_cblas_dgemm.output b/var/spack/repos/builtin/packages/openblas/test_cblas_dgemm.output index b8316d7477..9c235e314f 100644 --- a/var/spack/repos/builtin/packages/openblas/test_cblas_dgemm.output +++ b/var/spack/repos/builtin/packages/openblas/test_cblas_dgemm.output @@ -7,3 +7,6 @@ 5.000000 -1.000000 3.000000 + -0.3 1 + 3.0 1499101120 + -3.0 32767 -- cgit v1.2.3-60-g2f50