forked from tum-pbs/PhiFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
168 lines (152 loc) · 6.22 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
import distutils.cmd
import distutils.log
import subprocess
import os
from os.path import join, isfile, abspath, isdir, dirname
from setuptools import setup
def check_tf_cuda_compatibility():
import tensorflow
build = tensorflow.sysconfig.get_build_info() # is_rocm_build, cuda_compute_capabilities
tf_gcc = build['cpu_compiler']
is_cuda_build = build['is_cuda_build']
print(f"TensorFlow compiler: {tf_gcc}.")
if not is_cuda_build:
raise AssertionError("Your TensorFlow build does not support CUDA.")
else:
cuda_version = build['cuda_version']
cudnn_version = build['cudnn_version']
print(f"TensorFlow was compiled against CUDA {cuda_version} and cuDNN {cudnn_version}.")
return tf_gcc
def compile_cuda(file_names, nvcc, source_dir, target_dir, logfile):
import tensorflow
tf_cflags = tensorflow.sysconfig.get_compile_flags()
command = [
nvcc,
join(source_dir, f'{file_names}.cu.cc'),
'-o', join(target_dir, f'{file_names}.cu.o'),
'-std=c++11',
'-c',
'-D GOOGLE_CUDA=1',
'-x', 'cu',
'-Xcompiler',
'-fPIC',
'--expt-relaxed-constexpr',
'-DNDEBUG',
'-O3'
] + tf_cflags
print(f"nvcc {file_names}")
logfile.writelines(["\n", " ".join(command), "\n"])
subprocess.check_call(command, stdout=logfile, stderr=logfile)
def compile_gcc(file_names, gcc, source_dir, target_dir, cuda_lib, logfile):
import tensorflow
tf_cflags = tensorflow.sysconfig.get_compile_flags()
tf_lflags = tensorflow.sysconfig.get_link_flags()
link_cuda_lib = '-L' + cuda_lib
command = [
gcc,
join(source_dir, f'{file_names}.cc'),
join(target_dir, f'{file_names}.cu.o'),
'-o', join(target_dir, f'{file_names}.so'),
'-std=c++11',
'-shared',
'-fPIC',
'-lcudart',
'-O3',
link_cuda_lib
] + tf_cflags + tf_lflags
print(f"gcc {file_names}")
logfile.writelines(["\n", " ".join(command), "\n"])
subprocess.check_call(command, stdout=logfile, stderr=logfile)
class CudaCommand(distutils.cmd.Command):
description = 'Compile CUDA sources'
user_options = [
('gcc=', None, 'Path to the gcc compiler.'),
('nvcc=', None, 'Path to the Nvidia nvcc compiler.'),
('cuda-lib=', None, 'Path to the CUDA libraries.'),
]
def initialize_options(self):
tf_gcc = check_tf_cuda_compatibility()
self.gcc = tf_gcc if isfile(tf_gcc) else 'gcc'
self.nvcc = '/usr/local/cuda/bin/nvcc' if isfile('/usr/local/cuda/bin/nvcc') else 'nvcc'
self.cuda_lib = '/usr/local/cuda/lib64/'
def finalize_options(self) -> None:
pass
def run(self):
src_path = abspath('./phi/tf/cuda/src')
build_path = abspath('./phi/tf/cuda/build')
logfile_path = abspath('./phi/tf/cuda/log.txt')
print("Source Path:\t" + src_path)
print("Build Path:\t" + build_path)
print("GCC:\t\t" + self.gcc)
print("NVCC:\t\t" + self.nvcc)
print("CUDA lib:\t" + self.cuda_lib)
print("----------------------------")
# Remove old build files
if isdir(build_path):
print('Removing old build files from %s' % build_path)
for file in os.listdir(build_path):
os.remove(join(build_path, file))
else:
print('Creating build directory at %s' % build_path)
os.mkdir(build_path)
print('Compiling CUDA code...')
with open(logfile_path, "w") as logfile:
try:
compile_cuda('resample', self.nvcc, src_path, build_path, logfile=logfile)
compile_gcc('resample', self.gcc, src_path, build_path, self.cuda_lib, logfile=logfile)
compile_cuda('resample_gradient', self.nvcc, src_path, build_path, logfile=logfile)
compile_gcc('resample_gradient', self.gcc, src_path, build_path, self.cuda_lib, logfile=logfile)
# compile_cuda('bicgstab_ilu_linear_solve_op', self.nvcc, src_path, build_path, logfile=logfile)
# compile_gcc('bicgstab_ilu_linear_solve_op', self.gcc, src_path, build_path, self.cuda_lib, logfile=logfile)
except BaseException as err:
print(f"Compilation failed. See {logfile_path} for details.")
raise err
print(f"Compilation complete. See {logfile_path} for details.")
try:
with open(join(dirname(__file__), 'docs/Package_Info.md'), 'r') as readme:
long_description = readme.read()
except FileNotFoundError:
long_description = ""
pass
with open(join(dirname(__file__), 'phi', 'VERSION'), 'r') as version_file:
version = version_file.read()
setup(
name='phiflow',
version=version,
download_url='https://github.com/tum-pbs/PhiFlow/archive/%s.tar.gz' % version,
packages=['phi',
'phi.app',
'phi.app._dash',
'phi.field',
'phi.geom',
'phi.math',
'phi.math.backend',
'phi.physics',
'phi.struct',
'phi.tf',
'phi.torch',
'webglviewer'],
cmdclass={
'tf_cuda': CudaCommand,
},
description='Research-oriented differentiable fluid simulation framework',
long_description=long_description,
long_description_content_type='text/markdown',
keywords=['Differentiable', 'Simulation', 'Fluid', 'Machine Learning', 'Deep Learning'],
license='MIT',
author='Philipp Holl',
author_email='philipp.holl@tum.de',
url='https://github.com/tum-pbs/PhiFlow',
include_package_data=True,
install_requires=['scipy', 'dash', 'plotly', 'imageio', 'matplotlib'],
classifiers=[
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'Topic :: Software Development :: Build Tools',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
],
)