1. 概述
通过pybind11,生成autodock-gpu的python binding,方便python直接调用。
https://github.com/ccsb-scripps/AutoDock-GPU
2. autodock-gpu的编译命令
- 编译命令,生成可执行文件
GPU_INCLUDE_PATH=/usr/local/cuda-11.2/include GPU_LIBRARY_PATH=/usr/local/cuda-11.2/lib64 DEVICE=CUDA make DEVICE=CUDA NUMWI=256 OVERLAP=ON TARGETS="86"
会自动生成
host/src/performdocking.cpp
和host/inc/performdocking.h
两个文件,后面的编译需要这两个文件
因为修改了main代码,这两个文件没办法再次生成,所以一并提交到repo中,可能在其他GPU环境中导致so文件不可用,请留意
- 不试用makefile,单独编译生成autodock_gpu_256wi可执行文件
g++ ./host/src/calcenergy.cpp ./host/src/getparameters.cpp ./host/src/main.cpp ./host/src/miscellaneous.cpp ./host/src/performdocking.cpp ./host/src/processgrid.cpp ./host/src/processligand.cpp ./host/src/processresult.cpp ./host/src/setup.cpp \
-std=c++17 -I./common -I./host/inc -I/usr/local/cuda-11.2/include -I./cuda \
-L/usr/local/cuda-11.2/lib64 -Wl,-rpath=/usr/local/cuda-11.2/lib64: \
-DVERSION=\"v1.5.3-73-gf5cf6ffdd0c5b3f113d5cc424fabee51df04da7e\" kernels.o -lcurand -lcudart \
-o./bin/autodock_gpu_256wi \
-DGPU_DEVICE -DN256WI -DUSE_PIPELINE -fopenmp -O3
3. pybind11
- 增加main函数的wrap函数,难点是对
chat* argv[]
的处理,参考
#include <pybind11/pybind11.h>
#include <stdio.h>
#if PY_VERSION_HEX < 0x03000000
#define MyPyText_AsString PyString_AsString
#else
#define MyPyText_AsString PyUnicode_AsUTF8
#endif
namespace py = pybind11;
int run(py::list inlist) {
int argc = (int)inlist.size();
char** argv = (char**)malloc(argc * sizeof(char*));
for (int i = 0; i < argc; ++i)
argv[i] = (char*)MyPyText_AsString(inlist[i].ptr());
for (int i = 0; i < argc; ++i)
fprintf(stderr, "%s\n", argv[i]);
free(argv);
return 0;
}
PYBIND11_MODULE(example, m) {
m.def("run", &run, "runs the example");
}
还有一些其他的修改点,难度不大,这里不详细赘述
- 生成pybind11的so文件
g++ -O3 -Wall -shared -fPIC $(python3 -m pybind11 --includes) -fno-inline -Wl,-Bsymbolic-functions \
./host/src/calcenergy.cpp ./host/src/getparameters.cpp ./host/src/main.cpp ./host/src/miscellaneous.cpp ./host/src/performdocking.cpp ./host/src/processgrid.cpp ./host/src/processligand.cpp ./host/src/processresult.cpp ./host/src/setup.cpp \
-std=c++17 -I./common -I./host/inc -I/usr/local/cuda-11.2/include -I./cuda \
-L/usr/local/cuda-11.2/lib64 -Wl,-rpath=/usr/local/cuda-11.2/lib64: \
-DVERSION=\"v1.5.3-73-gf5cf6ffdd0c5b3f113d5cc424fabee51df04da7e\" kernels.o -lcurand -lcudart \
-o./bin/autodock$(python3-config --extension-suffix) \
-DGPU_DEVICE -DN256WI -DUSE_PIPELINE -fopenmp
- 测试
在上一步生成的autodock.cpython-310-x86_64-linux-gnu.so
同一目录,创建python文件如下,然后执行
import autodock
argv = "--lfile /home/shuzhang/ai/code/moldock/autodock/output/tmpnnuuab_g.pdbqt --ffile /data/autodock/grid/0cb544cb1474ff6d917fe409598886cb/protein.maps.fld --devnum 2 --ngen 1 --nrun 2 --stopstd 1.999"
mols = autodock.run(argv.split())
to_float = lambda x: float(x.strip())
for i, mol in enumerate(mols):
print(f"----{i}")
for atom in mol.split(";"):
if not atom:
continue
x,y,z = atom.split(",")
print(f"{to_float(x):.5f}, {to_float(y):.5f}, {to_float(z):.5f}")
4. 总结
pybind11功能强大,且简单易用,更多文档参考pybind11官方文档。
网友评论