Adding a simple cuda library to RDKit

There are many interesting things a chemist can do with a GPU. Think FastROCS as an example where GPU has greatly speed-up aligning two conformers.

A chemist, in addition to learning how to code on a GPU a little bit, has to distribute the code. One strategy would be to include the code in another software package that's widely used by the target audience. This can be a very tedious task... and so the task of this post will be to cover this 'boiler plate' part. The code presented here does nothing meaningful, it just shows you how to do the piping.

For the ubuntu users in the house, visit https://developer.nvidia.com/cuda-downloads. In my setup ubuntu 14.04 64bit DEB was the choice, then 'sudo apt-get install nvidia-cuda-toolkit'

Here is a simple 'hello world' program that truly does some work on the GPU.
Let's save that as hello-world.cu file. It can be anywhere – this will be independent of rdkit for now. That's all good. Now let's make that part of rdkit. First step: in rdkit/CMakeList.txt find the CUDA package to be able to compile .cu files Then, create 'rdkit/Code/RDCuda' directory and put the hello-world.cu there. Create also a rdkit/Code/RDCuda/hello-world.h.
You should be able to call 'nvcc hello-world.cu ...' as before on hello-world.cu file, and everything should still work with ./hello-world.o Finally, let's create rdkit/Code/RDCuda/CMakeFile.txt - by putting things in this file we'll let cmake cover this directory. There is one gotcha here: rdkit_cuta_library is a new macro. It is the same as rdkit_library, with 'add_library' cmake command replaced with 'cuda_add_library'. Here is how it should look inside rdkit/Code/cmake/Modules/RDKitUtils.cmake Finally, now we can compile this creature! Go to $RDBASE/build and compile- Hopefully, nothing blew in your face: location of CUDA should be correctly picked up by the cmake command. Make should compile without problems. The .so file should be compiled in $RDBASE/lib/libRDCudaLib.so. Let's play with it a little bit That all looks good: from the output of nm command. '_ZN6RDCuda10helloworldEv' is the helloworld() function, it's defined (it has T rather than U in the second column); from ldd the libcudart points to a correct file on my machine.
Let's make it all accessibly via python. One of the strengths of rdkit is the easily programmable python interface that wraps around fast c++ code.
Create rdkit/Code/RDCuda/Wrap and put the following python wrapper there, called cuda.cpp. Also, create rdkit/Code/RDCuda/Wrap/CMakeList.txt Now, the rdkit/Code/RDCuda/CMakeList.txt has to become aware of the 'Wrap' directory, so append the following line to it In rdkit/rdkit/CUDA/__init__.py should have the following: Finally, go to $RDBASE/build and Just a few checks now The so file 'libRDCudaLib' is pointing in the right direction, let's fire up ipython and perform the final test.
Finally, let's do something more 'real', and pass an RDKit mol into the guts of the cuda file. We're going to pass the molecule and print the number of atoms. rdkit/Code/RDCuda/hellocuda.h And in rdkit/Code/RDCuda/hellocuda.cu we create a function that doesn't use the GPU all, it's merely stored as a .cu file. That's easy, now modify the rdkit/Code/RDCuda/CMakeLists.txt We've got all the business end setup, let's complete by editing the python wrapper in rdkit/Code/RDCuda/Wrap/cuda.cpp And rdkit/Code/RDCuda/Wrap/CMakeLists.txt And again all should be good, the following example should work Next post will cover how to do something chemically useful with this code. For now, checkout my github fork of rdkit for details.

Comments

Popular posts from this blog

React.js – edit and delete comments in CommentBox

Example slurm cluster on your laptop (multiple VMs via vagrant)

Wrapping openbabel in python - using cython