Skip to content

Commit 1a2271f

Browse files
Miro IliasMiro Ilias
authored andcommitted
Added cmake detection and test of the OpenBLAS library
- thanks to https://github.com/BVLC/caffe/blob/master/cmake/Modules/FindOpenBLAS.cmake - added dedicated openblas test, which has the same testing source the same as for the blas test (fc_openblas in test_openblas.py) - taken care of ENABLE_OPENBLAS variable - provide openblas package installation for .travis.yml in 2 extra platforms (thanks to Dominic Jodoin <support@travis-ci.com>) - closes #172 - .travis.yml : openblas and blas can be together, but not with lapack,atlas packages ! and atlas ! - test/test_openblas.py : test both blas and openblas in new test - test/test.py : test all excep openblas - reduced number of installed packages for blas+openblas to get travis-ci test passed - fc_blas unified with fc_openblas, no need to duplicate the code - WARNING: testing on travis-ci is shaky, sometimes is passes
1 parent 808d094 commit 1a2271f

File tree

5 files changed

+227
-4
lines changed

5 files changed

+227
-4
lines changed

.travis.yml

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ matrix:
77
- os: linux
88
python: 2.7
99
compiler: gcc
10-
env: SOURCES=ubuntu-toolchain-r-test
10+
env: SOURCES=ubuntu-toolchain-r-test TEST_SCRIPT=test.py
1111
addons:
1212
apt:
1313
sources:
@@ -32,7 +32,7 @@ matrix:
3232
- os: linux
3333
python: 3.5
3434
compiler: gcc
35-
env: SOURCES=ubuntu-toolchain-r-test
35+
env: SOURCES=ubuntu-toolchain-r-test TEST_SCRIPT=test.py
3636
addons:
3737
apt:
3838
sources:
@@ -53,6 +53,44 @@ matrix:
5353
# boost
5454
- libboost-all-dev
5555

56+
- os: linux
57+
python: 2.7
58+
compiler: gcc
59+
env: SOURCES=ubuntu-toolchain-r-test TEST_SCRIPT=test_openblas.py
60+
addons:
61+
apt:
62+
sources:
63+
- ubuntu-toolchain-r-test
64+
packages:
65+
# compilers
66+
- g++
67+
- gfortran
68+
- cmake
69+
# math libraries, can not mix with lapack/atlas packages !
70+
- libblas-dev
71+
- libopenblas-dev
72+
# python library, development version
73+
#- libpython2.7
74+
75+
- os: linux
76+
python: 3.5
77+
compiler: gcc
78+
env: SOURCES=ubuntu-toolchain-r-test TEST_SCRIPT=test_openblas.py
79+
addons:
80+
apt:
81+
sources:
82+
- ubuntu-toolchain-r-test
83+
packages:
84+
# compilers
85+
- g++
86+
- gfortran
87+
- cmake
88+
# math libraries, can not mix with lapack/atlas packages !
89+
- libblas-dev
90+
- libopenblas-dev
91+
# python libraries ...
92+
- libpython2.7
93+
5694
- os: osx
5795
osx_image: xcode6.4
5896
compiler: gcc
@@ -79,7 +117,7 @@ script:
79117
- pep8 --ignore E501,E265 autocmake
80118
# unit tests
81119
- py.test -vv autocmake/*
82-
- py.test -vv test/test.py
120+
- py.test -vv test/$TEST_SCRIPT
83121

84122
notifications:
85123
email: false

modules/math/openblas.cmake

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#.rst:
2+
#
3+
# Find and link to OpenBLAS.
4+
#
5+
# Variables defined::
6+
#
7+
# OpenBLAS_FOUND
8+
# OpenBLAS_LIB
9+
# OpenBLAS_INCLUDE_DIR
10+
#
11+
# autocmake.yml configuration::
12+
#
13+
# docopt: "--openblas Find and link to OpenBLAS [default: False]."
14+
# define: "'-DENABLE_OPENBLAS={0}'.format(arguments['--openblas'])"
15+
16+
option(ENABLE_OPENBLAS "Find and link to OpenBLAS" OFF)
17+
18+
if (ENABLE_OPENBLAS)
19+
SET(Open_BLAS_INCLUDE_SEARCH_PATHS
20+
/usr/include
21+
/usr/include/openblas
22+
/usr/include/openblas-base
23+
/usr/local/include
24+
/usr/local/include/openblas
25+
/usr/local/include/openblas-base
26+
/opt/OpenBLAS/include
27+
$ENV{OpenBLAS_HOME}
28+
$ENV{OpenBLAS_HOME}/include
29+
)
30+
31+
SET(Open_BLAS_LIB_SEARCH_PATHS
32+
/lib/
33+
/lib/openblas-base
34+
/lib64/
35+
/usr/lib
36+
/usr/lib/openblas-base
37+
/usr/lib64
38+
/usr/local/lib
39+
/usr/local/lib64
40+
/opt/OpenBLAS/lib
41+
$ENV{OpenBLAS}cd
42+
$ENV{OpenBLAS}/lib
43+
$ENV{OpenBLAS_HOME}
44+
$ENV{OpenBLAS_HOME}/lib
45+
)
46+
47+
FIND_PATH(OpenBLAS_INCLUDE_DIR NAMES cblas.h PATHS ${Open_BLAS_INCLUDE_SEARCH_PATHS})
48+
FIND_LIBRARY(OpenBLAS_LIB NAMES openblas PATHS ${Open_BLAS_LIB_SEARCH_PATHS})
49+
50+
SET(OpenBLAS_FOUND ON)
51+
52+
# Check include files
53+
IF(NOT OpenBLAS_INCLUDE_DIR)
54+
SET(OpenBLAS_FOUND OFF)
55+
MESSAGE(STATUS "Could not find OpenBLAS include. Turning OpenBLAS_FOUND off")
56+
ENDIF()
57+
58+
# Check libraries
59+
IF(NOT OpenBLAS_LIB)
60+
SET(OpenBLAS_FOUND OFF)
61+
MESSAGE(STATUS "Could not find OpenBLAS lib. Turning OpenBLAS_FOUND off")
62+
ENDIF()
63+
64+
IF (OpenBLAS_FOUND)
65+
IF (NOT OpenBLAS_FIND_QUIETLY)
66+
MESSAGE(STATUS "Found OpenBLAS libraries: ${OpenBLAS_LIB}")
67+
MESSAGE(STATUS "Found OpenBLAS include: ${OpenBLAS_INCLUDE_DIR}")
68+
ENDIF (NOT OpenBLAS_FIND_QUIETLY)
69+
ELSE (OpenBLAS_FOUND)
70+
IF (OpenBLAS_FIND_REQUIRED)
71+
MESSAGE(FATAL_ERROR "Could not find OpenBLAS")
72+
ENDIF (OpenBLAS_FIND_REQUIRED)
73+
ENDIF (OpenBLAS_FOUND)
74+
75+
MARK_AS_ADVANCED(
76+
OpenBLAS_INCLUDE_DIR
77+
OpenBLAS_LIB
78+
OpenBLAS
79+
)
80+
endif()

test/fc_blas/cmake/autocmake.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ min_cmake_version: 2.8
33
modules:
44
- fc:
55
- source: ../../../modules/fc.cmake
6-
- math:
6+
- math1:
77
- source: ../../../modules/math/blas.cmake
8+
- math2:
9+
- source: ../../../modules/math/openblas.cmake
810
- default_build_paths:
911
- source: ../../../modules/default_build_paths.cmake
1012
- src:

test/fc_blas/src/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1+
if (ENABLE_BLAS)
12
if(BLAS_FOUND)
23
add_executable(example example.f90)
34
target_link_libraries(example ${BLAS_LIBRARIES})
45
else()
56
message(FATAL_ERROR "BLAS library not found")
67
endif()
8+
endif()
9+
10+
if (ENABLE_OPENBLAS)
11+
if(OpenBLAS_FOUND)
12+
add_executable(example example.f90)
13+
target_link_libraries(example ${OpenBLAS_LIB})
14+
else()
15+
message(FATAL_ERROR "OpenBLAS library not found")
16+
endif()
17+
endif()

test/test_openblas.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import os
2+
import sys
3+
import subprocess
4+
import shlex
5+
import shutil
6+
import sys
7+
import time
8+
import datetime
9+
import pytest
10+
11+
HERE = os.path.abspath(os.path.dirname(__file__))
12+
13+
skip_on_osx = pytest.mark.skipif('sys.platform == "darwin"', reason="not working on osx")
14+
skip_on_linux = pytest.mark.skipif('sys.platform == "linux2"', reason="not working on linux")
15+
skip_always = pytest.mark.skipif('1 == 1', reason="tests are broken")
16+
17+
18+
def exe(command):
19+
"""
20+
Executes command and returns string representations of stdout and stderr captured from the console.
21+
When universal_newlines=True stdout and stderr are opened in text mode.
22+
Otherwise, they are opened in binary mode. In that case captured stdout and stderr
23+
are not strings and Python 3 throws type error when compared against strings later in tests.
24+
Note:
25+
This feature is only available if Python is built with universal newline support (the default).
26+
Also, the newlines attribute of the file objects stdout, stdin and stderr are not updated by the
27+
communicate() method.
28+
See https://docs.python.org/2/library/subprocess.html
29+
"""
30+
stdout, stderr = subprocess.Popen(shlex.split(command),
31+
stdout=subprocess.PIPE,
32+
stderr=subprocess.PIPE,
33+
universal_newlines=True).communicate()
34+
35+
if stderr:
36+
sys.stderr.write(stdout)
37+
sys.stderr.write(stderr)
38+
39+
return stdout, stderr
40+
41+
42+
def configure_build_and_exe(name, setup_command, launcher=None):
43+
44+
stamp = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d-%H-%M-%S')
45+
46+
os.chdir(os.path.join(HERE, name, 'cmake'))
47+
shutil.copy(os.path.join('..', '..', '..', 'update.py'), 'update.py')
48+
49+
if os.path.exists('autocmake'):
50+
shutil.rmtree('autocmake')
51+
shutil.copytree(os.path.join('..', '..', '..', 'autocmake'), 'autocmake')
52+
53+
stdout, stderr = exe('python update.py ..')
54+
os.chdir(os.path.join(HERE, name))
55+
56+
make_command = 'make'
57+
binary = './bin/example'
58+
if sys.platform == 'win32':
59+
setup_command += ' --generator="MinGW Makefiles"'
60+
make_command = 'mingw32-make'
61+
binary = 'bin\\\example.exe'
62+
63+
if launcher:
64+
binary = '%s %s' % (launcher, binary)
65+
66+
setup_command += ' build-%s' % stamp
67+
68+
stdout, stderr = exe(setup_command)
69+
assert stderr == ''
70+
71+
os.chdir(os.path.join(HERE, name, 'build-%s' % stamp))
72+
73+
stdout, stderr = exe(make_command)
74+
# we do not check for empty stderr due to warnings flushed to stderr
75+
76+
stdout, stderr = exe(binary)
77+
assert stderr == ''
78+
79+
assert 'PASSED' in stdout
80+
81+
82+
def test_fc():
83+
configure_build_and_exe('fc', 'python setup --fc=gfortran')
84+
85+
86+
def test_fc_blas():
87+
configure_build_and_exe('fc_blas', 'python setup --fc=gfortran --blas')
88+
89+
90+
@skip_on_osx
91+
def test_fc_openblas():
92+
configure_build_and_exe('fc_blas', 'python setup --fc=gfortran --openblas')

0 commit comments

Comments
 (0)