Merge pull request #845 from davydden/openblas_cleanup_unit_test

openblas: fix a bug and cleanup the unit test
This commit is contained in:
Todd Gamblin 2016-05-10 01:28:49 -07:00
commit 87c772b117
3 changed files with 75 additions and 52 deletions

View file

@ -94,43 +94,28 @@ def setup_dependent_package(self, module, dspec):
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 <cblas.h>
#include <stdio.h>
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)
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')
# 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")
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)
@ -148,4 +133,3 @@ def check_install(self, spec):
print output
print '-'*80
raise RuntimeError("Openblas install check failed")
shutil.rmtree(checkdir)

View file

@ -1,13 +1,49 @@
#include <cblas.h>
#include <stdio.h>
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;
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;
// 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;
}

View file

@ -7,3 +7,6 @@
5.000000
-1.000000
3.000000
-0.3 1
3.0 1499101120
-3.0 32767