-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathbuild_helper.py
174 lines (140 loc) · 5.23 KB
/
build_helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import os
import shutil
import subprocess
import sys
import tempfile
from distutils import ccompiler
def print_warning(*lines):
print('**************************************************')
for line in lines:
print('*** WARNING: %s' % line)
print('**************************************************')
def get_path(key):
return os.environ.get(key, '').split(os.pathsep)
def search_on_path(filenames):
for p in get_path('PATH'):
for filename in filenames:
full = os.path.join(p, filename)
if os.path.exists(full):
return os.path.abspath(full)
return None
minimum_cuda_version = 10010
maxinum_cuda_version = 10030
minimum_cudnn_version = 7000
def get_compiler_setting():
nvcc_path = search_on_path(('nvcc', 'nvcc.exe'))
cuda_path_default = None
if nvcc_path is None:
print_warning('nvcc not in path.', 'Please set path to nvcc.')
else:
cuda_path_default = os.path.normpath(
os.path.join(os.path.dirname(nvcc_path), '..'))
cuda_path = os.environ.get('CUDA_PATH', '') # Nvidia default on Windows
if len(cuda_path) > 0 and cuda_path != cuda_path_default:
print_warning('nvcc path != CUDA_PATH',
'nvcc path: %s' % cuda_path_default,
'CUDA_PATH: %s' % cuda_path)
if not os.path.exists(cuda_path):
cuda_path = cuda_path_default
if not cuda_path and os.path.exists('/usr/local/cuda'):
cuda_path = '/usr/local/cuda'
include_dirs = []
library_dirs = []
define_macros = []
if cuda_path:
include_dirs.append(os.path.join(cuda_path, 'include'))
if sys.platform == 'win32':
library_dirs.append(os.path.join(cuda_path, 'bin'))
library_dirs.append(os.path.join(cuda_path, 'lib', 'x64'))
else:
library_dirs.append(os.path.join(cuda_path, 'lib64'))
library_dirs.append(os.path.join(cuda_path, 'lib'))
if sys.platform == 'darwin':
library_dirs.append('/usr/local/cuda/lib')
return {
'include_dirs': include_dirs,
'library_dirs': library_dirs,
'define_macros': define_macros,
'language': 'c++',
}
def check_cuda_version():
compiler = ccompiler.new_compiler()
settings = get_compiler_setting()
try:
out = build_and_run(compiler,
'''
#include <cuda.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
printf("%d", CUDA_VERSION);
return 0;
}
''',
include_dirs=settings['include_dirs'])
except Exception as e:
print_warning('Cannot check CUDA version', str(e))
return False
cuda_version = int(out)
if cuda_version < minimum_cuda_version:
print_warning('CUDA version is too old: %d' % cuda_version,
'CUDA v10.1 or CUDA v10.2 is required')
return False
if cuda_version > maxinum_cuda_version:
print_warning('CUDA version is too new: %d' % cuda_version,
'CUDA v10.1 or CUDA v10.2 is required')
return True
def check_cudnn_version():
compiler = ccompiler.new_compiler()
settings = get_compiler_setting()
try:
out = build_and_run(compiler,
'''
#include <cudnn.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
printf("%d", CUDNN_VERSION);
return 0;
}
''',
include_dirs=settings['include_dirs'])
except Exception as e:
print_warning('Cannot check cuDNN version\n{0}'.format(e))
return False
cudnn_version = int(out)
if cudnn_version < minimum_cudnn_version:
print_warning('cuDNN version is too old: %d' % cudnn_version,
'cuDNN v7 or newer is required')
return False
return True
def build_and_run(compiler,
source,
libraries=(),
include_dirs=(),
library_dirs=()):
temp_dir = tempfile.mkdtemp()
try:
fname = os.path.join(temp_dir, 'a.cpp')
with open(fname, 'w') as f:
f.write(source)
objects = compiler.compile([fname],
output_dir=temp_dir,
include_dirs=include_dirs)
try:
postargs = ['/MANIFEST'] if sys.platform == 'win32' else []
compiler.link_executable(objects,
os.path.join(temp_dir, 'a'),
libraries=libraries,
library_dirs=library_dirs,
extra_postargs=postargs,
target_lang='c++')
except Exception as e:
msg = 'Cannot build a stub file.\nOriginal error: {0}'.format(e)
raise Exception(msg)
try:
out = subprocess.check_output(os.path.join(temp_dir, 'a'))
return out
except Exception as e:
msg = 'Cannot execute a stub file.\nOriginal error: {0}'.format(e)
raise Exception(msg)
finally:
shutil.rmtree(temp_dir, ignore_errors=True)