-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup.py
256 lines (230 loc) · 7.75 KB
/
setup.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import distutils.cmd
import distutils.log
import errno
import subprocess
import os
from setuptools import setup
class CudaCommand(distutils.cmd.Command):
description = 'Compile CUDA sources'
user_options = [
('gcc=', None, 'Path to the gcc compiler.'),
('gcc-4-8=', None, 'Path to gcc-4.8 compiler.'),
('nvcc=', None, 'Path to the Nvidia nvcc compiler.'),
('cuda-lib=', None, 'Path to the CUDA libraries.'),
]
def run(self):
src_path = os.path.abspath('./CUDAsrc')
build_path = os.path.abspath('./CUDAbuild')
print('Source Path:\t' + src_path)
print('Build Path:\t' + build_path)
import tensorflow as tf
tf_cflags = tf.sysconfig.get_compile_flags()
tf_lflags = tf.sysconfig.get_link_flags()
link_cuda_lib = '-L' + self.cuda_lib
# Remove old build files
if os.path.isdir(build_path):
print('Removing old build files from %s' % build_path)
for file in os.listdir(build_path):
os.remove(os.path.join(build_path, file))
else:
print('Creating build directory at %s' % build_path)
os.mkdir(build_path)
print('Compiling CUDA code...')
print('LinearSolve -- Multi BiCgStab iLU')
subprocess.check_call(
[
self.nvcc,
'-std=c++11',
'-c',
'-o',
os.path.join(build_path, 'multi_bicgstab_ilu_linear_solve_op.cu.o'),
os.path.join(src_path, 'multi_bicgstab_ilu_linear_solve_op.cu.cc'),
# '-O2',
'-x',
'cu',
'-O3',
'-Xptxas',
'-O3',
'-Xcompiler',
'-fPIC'
]
+ tf_cflags
)
subprocess.check_call(
[
self.gcc_4_8,
'-std=c++11',
'-shared',
'-o',
os.path.join(build_path, 'multi_bicgstab_ilu_linear_solve_op.so'),
os.path.join(src_path, 'multi_bicgstab_ilu_linear_solve_op.cc'),
os.path.join(build_path, 'multi_bicgstab_ilu_linear_solve_op.cu.o'),
'-fPIC'
]
+ tf_cflags
+ tf_lflags
+ ['-L/usr/local/cuda-10.0/lib64/', '-lcudart', '-lcublas', '-lcusolver', '-lcusparse']
)
print('LinearSolve -- BiCgStab iLU')
subprocess.check_call(
[
self.nvcc,
'-std=c++11',
'-c',
'-o',
os.path.join(build_path, 'bicgstab_ilu_linear_solve_op.cu.o'),
os.path.join(src_path, 'bicgstab_ilu_linear_solve_op.cu.cc'),
#'-O2',
'-x',
'cu',
'-O3',
'-Xptxas',
'-O3',
'-Xcompiler',
'-fPIC'
]
+ tf_cflags
)
subprocess.check_call(
[
self.gcc_4_8,
'-std=c++11',
'-shared',
'-o',
os.path.join(build_path, 'bicgstab_ilu_linear_solve_op.so'),
os.path.join(src_path, 'bicgstab_ilu_linear_solve_op.cc'),
os.path.join(build_path, 'bicgstab_ilu_linear_solve_op.cu.o'),
'-fPIC'
]
+ tf_cflags
+ tf_lflags
+ ['-L/usr/local/cuda-10.0/lib64/', '-lcudart', '-lcublas', '-lcusolver', '-lcusparse']
)
# Build the CentralDifferenceMatrix CUDA Kernels
print('CentralDifference -- CSR matrix format')
subprocess.check_call(
[
self.nvcc,
'-std=c++11',
'-c',
'-o',
os.path.join(build_path, 'central_difference_csr_op.cu.o'),
os.path.join(src_path, 'central_difference_csr_op.cu.cc'),
'-x',
'cu',
'-O3',
'-Xptxas',
'-O3',
'-Xcompiler',
'-fPIC',
'-Xcompiler="-pthread"'
]
+ tf_cflags
)
subprocess.check_call(
[
self.gcc,
'-std=c++11',
'-shared',
'-o',
os.path.join(build_path, 'central_difference_csr_op.so'),
os.path.join(src_path, 'central_difference_csr_op.cc'),
os.path.join(build_path, 'central_difference_csr_op.cu.o'),
'-fPIC'
]
+ tf_cflags
+ tf_lflags
+ ['-L/usr/local/cuda/lib64/', '-lcudart', '-lcublas', ]
)
# Build the Laplace Matrix Generation CUDA Kernels
print('PressureSolve -- Laplace matrix op')
subprocess.check_call(
[
self.nvcc,
'-std=c++11',
'-c',
'-o',
os.path.join(build_path, 'laplace_op.cu.o'),
os.path.join(src_path, 'laplace_op.cu.cc'),
'-x',
'cu',
'-O3',
'-Xptxas',
'-O3',
'-Xcompiler',
'-fPIC'
]
+ tf_cflags
)
# Build the Laplace Matrix Generation Custom Op
# This is only needed for the Laplace Matrix Generation Benchmark
subprocess.check_call(
[
self.gcc_4_8,
'-std=c++11',
'-shared',
'-o',
os.path.join(build_path, 'laplace_op.so'),
os.path.join(src_path, 'laplace_op.cc'),
os.path.join(build_path, 'laplace_op.cu.o'),
'-fPIC'
]
+ tf_cflags
+ tf_lflags
+ ['-L/usr/local/cuda/lib64/','-lcudart']
)
# Build the Pressure Solver CUDA Kernels
print('PressureSolve -- CG solver')
subprocess.check_call(
[
self.nvcc,
'-std=c++11',
'-c',
'-lcublas',
'-lcurand',
'-o',
os.path.join(build_path, 'pressure_solve_op.cu.o'),
os.path.join(src_path, 'pressure_solve_op.cu.cc'),
'-x', 'cu',
'-O3',
'-Xptxas',
'-O3',
'-Xcompiler',
'-fPIC'
]
+ tf_cflags
)
# Build the Pressure Solver Custom Op
subprocess.check_call(
[
self.gcc_4_8,
'-std=c++11',
'-shared',
'-o',
os.path.join(build_path, 'pressure_solve_op.so'),
os.path.join(src_path, 'pressure_solve_op.cc'),
os.path.join(build_path, 'pressure_solve_op.cu.o'),
os.path.join(build_path, 'laplace_op.cu.o'),
'-fPIC'
]
+ tf_cflags
+ tf_lflags
+ ['-L/usr/local/cuda/lib64/','-lcudart']
)
print('Done with PISO files, heading into PhiFlow!')
os.system('python PhiFlow/setup.py tf_cuda')
def initialize_options(self):
self.gcc = 'gcc'
self.gcc_4_8 = 'g++-4.8'
self.nvcc = 'nvcc'
self.cuda_lib = '/usr/local/cuda/lib64/'
def finalize_options(self):
assert os.path.isfile(self.gcc) or self.gcc == 'gcc'
assert os.path.isfile(self.nvcc) or self.nvcc == 'nvcc'
with open(os.path.join(os.path.dirname(__file__),'PhiFlow', 'phi', 'VERSION'), 'r') as version_file:
version = version_file.read()
setup(
cmdclass={
'tf_cuda': CudaCommand,
}
)