简体   繁体   中英

How to create a Python C extension without using distutils?

I'm trying to create a Python extension that is written in CUDA C. I don't think distutils supports using the nvcc compiler, so I'd like to learn how a Python C extension can be created without using distutils, and adapt that method to build an extension written in CUDA C.

I'm using Windows and when using distutils I see that building an extension produces .exp, .lib, .obj, and .pyd files. I'm guessing the .exp, .lib, and .obj files are somehow combined into the .pyd file, but don't know how.

I was able to create a Cython extension that used CUDA by following the example found here:

https://github.com/saullocastro/programming/tree/master/cuda/cython/naive_add

Essentially you compile the CUDA code in the command line using nvcc to produce a .lib file. Then just use that .lib file when making the Cython extension as if it were compiled from C. This way distutils can still be used to build the extension, but after the CUDA code has been manually compiled by nvcc.

One small change I had to make was to add the numpy include directory in the command to build the extension. This was the command I used:

python setup.py build_ext -i -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include" -I"C:\Anaconda\Lib\site-packages\numpy\core\include" -lcudart -L"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\lib\x64" --force clean

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM