-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.py
168 lines (142 loc) · 5.71 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
# adapted from http://wiki.cython.org/PackageHierarchy
from __future__ import print_function
import sys, os, shutil, site
import multiprocessing
import subprocess as sb
import tempfile as tmp
import mpi4py
from distutils.core import setup
from distutils.extension import Extension
from distutils import log
from Cython.Distutils import build_ext
from Cython.Build import cythonize
# Use parallel compilation on this number of cores.
nthreads = int(os.getenv('COMPILE_NTHREADS', multiprocessing.cpu_count() ))
isRoot = os.geteuid() == 0 # Do we have root privileges?
class inTempFolder:
"""Context manager for working in temporary folder.
Creates a temporary folder enters in it and removes it when done.
"""
def __enter__(self):
self.originalDir = os.getcwd()
self.tmpdir = tmp.mkdtemp()
os.chdir(self.tmpdir)
return self.tmpdir, self.originalDir
def __exit__(self, type, value, traceback):
shutil.rmtree(self.tmpdir)
os.chdir(self.originalDir)
def installJDFTx(isRoot=False, enableGPU=False):
"""Search and return the path to libjdfx.so and compile jdftx if not found.
Check user folder for jdftx/libjdftx.so. Unless jdftx library is found
compile the source code, copy the library to the user directory and
return the path to the library. If GPU is enabled, look for libjdftx_gpu.so
too.
"""
# is there a valid installation in user folders:
if os.path.exists(os.path.join(site.USER_BASE, "jdftx/libjdftx.so")):
if (not enableGPU) or \
os.path.exists(os.path.join(site.USER_BASE, "jdftx/libjdftx_gpu.so")):
return os.path.join(site.USER_BASE, "jdftx")
with inTempFolder() as (jdftxDir, pythonJDFTxDir):
log.info("JDFTx Compilation:")
log.info("Running cmake...")
jdftxCodeDir = os.path.join(pythonJDFTxDir, "jdftx")
if enableGPU:
sb.check_call(["cmake", "-D", "EnableCUDA=yes",
jdftxCodeDir])
else:
sb.check_call(["cmake", "-D", jdftxCodeDir])
log.info("Running make. This takes a few minutes.")
sb.check_call(["make", "-j%d" % nthreads])
if isRoot:
jdftxLibDir = "/usr/local/jdftx"
else:
jdftxLibDir = os.path.join(site.USER_BASE, "jdftx")
if not os.path.exists(jdftxLibDir):
os.mkdir(jdftxLibDir)
shutil.move("libjdftx.so", jdftxLibDir)
if enableGPU:
shutil.move("libjdftx_gpu.so", jdftxLibDir)
if not os.path.exists(os.path.join(site.USER_BASE, "jdftx/pseudopotentials")):
shutil.move("pseudopotentials", jdftxLibDir)
try:
os.symlink("/usr/local/jdftx/libjdftx.so",
"/usr/lib/libjdftx.so")
if enableGPU:
os.symlink("/usr/local/jdftx/libjdftx_gpu.so",
"/usr/lib/libjdftx_gpu.so")
return ""
except OSError:
return jdftxLibDir
def make_extension(ext_name, ext_libraries=(), is_directory=False, enableGPU=0):
try:
sb.check_call(["ld", "-ljdftx"], stderr=open("/dev/null"))
if enableGPU:
sb.check_call(["ld", "-ljdftx_gpu"])
jdftxLibDir = ""
except sb.CalledProcessError:
jdftxLibDir = installJDFTx(isRoot, enableGPU)
jdftxIncDirs = ["jdftx", ".", mpi4py.get_include()]
if enableGPU: # adds only the default directory for now
jdftxIncDirs.append("/usr/local/cuda/include")
ext_path = ext_name
if is_directory:
ext_path += ".__init__"
return Extension(
ext_name,
[ext_path.replace(".", os.path.sep) + ".pyx"],
include_dirs=jdftxIncDirs,
language="c++",
libraries=ext_libraries,
library_dirs=[jdftxLibDir],
runtime_library_dirs=[jdftxLibDir],
extra_compile_args=['-std=c++0x', '-O3', '-DMPI_ENABLED'] +
['-DGPU_ENABLED'] * enableGPU,
#depends=["jdftx/libjdftx.so"],
)
def writeTargetToSourceCode(target, baseFName="JDFTxCalcBase.pyx"):
targetFName = "JDFTxCalc"+target+".pyx"
with open(baseFName) as baseFile, open(targetFName, 'w') as t:
t.write("# Generated by the setup script from " + baseFName + "\n")
# Do not copy the first comment block that is specific to JDFTxCalcBase.pyx.
while True:
if baseFile.readline()[0] != '#': break
t.write(baseFile.read().format(TARGET = target))
def main():
enableGPU = (len(sys.argv) >= 2 and "--GPU" in sys.argv)
if enableGPU: sys.argv.pop(sys.argv.index("--GPU"))
extensions = []
if enableGPU:
writeTargetToSourceCode(target = 'GPU')
extensions.append(
make_extension("JDFTxCalcGPU", ["jdftx_gpu"], enableGPU=True,
),
)
writeTargetToSourceCode(target = 'CPU')
extensions.append(
make_extension("JDFTxCalcCPU", ["jdftx"]),
)
for e in extensions:
e.cython_directives = {"boundscheck": False,
"wraparound": False,
"infer_types": True}
mpiCompilers = mpi4py.get_config()
os.environ['CC'] = mpiCompilers['mpicc']
os.environ['CXX'] = mpiCompilers['mpicxx']
pyVersion = sys.version_info[0]
extensions = cythonize(extensions, nthreads=nthreads,
compiler_directives = {'language_level': pyVersion})
setup(**{
"name": "pythonJDFTx",
# "packages": [
# "core",
# "electronic",
# "includes",
# "fluid",
# ],
"py_modules":["ElectronicMinimize"],
"ext_modules": extensions,
"cmdclass": {'build_ext': build_ext},
})
if __name__ == "__main__":
main()