Note calling the CUDA Driver API with Python ctypes

background

I want to tap the CUDA Driver API from python.

With pybind11 or cfffi, it is troublesome to build native code. I want to do it with pure python (standard python function) (CI build is troublesome with CUDA SDK installation)

Use ctypes.

https://docs.python.org/3/library/ctypes.html

Assume the CUDA Driver API (an API that can be used if the driver is included). It does not handle the CUDA Runtime API (which requires the installation of the CUDA SDK or runtime) (API is a hassle)

It is assumed that the running Kernel is generated in PTX format. By hitting CUDA with python, for example, you can use PTX code as a template, dynamically rewrite it on the python side, and compile (set the optimum parameters according to the target GPU).

Note converting CUDA code to NVPTX in Clang https://qiita.com/syoyo/items/4e60543aded0210fde49

Where is dll/so?

Windows

C:\Windows\System32\nvcuda.dll

Normally you just need nvcuda.dll (CUDA is automatically installed when you install the NVIDIA driver, so if you can not load it, it will be a PC without NV GPU (e.g. Intel built-in/Xe or AMD GPU))

Linux(Ubuntu)

For Ubuntu, it is located around / usr/lib/x86_64-linux-gnu /.

Just touch

from ctypes import *

cu = cdll.LoadLibrary("/usr/lib/x86_64-linux-gnu/libcuda.so")
print(cu)

ret = cu.cuInit(0)
assert ret == 0 # CUDA_SUCCESS

ver = c_int()
ret = cu.cuDriverGetVersion(byref(ver))
assert ret == 0 # CUDA_SUCCESS

print("CUDA version", ver)
CUDA version c_int(11020)

Voila!

It seems that you should use byref for pointers.

After that, you should be OK if you call various APIs!

Other

PTX Compiler API

Notes on the PTX Compiler API https://qiita.com/syoyo/items/cfaf0f7dd20b67cc734e

Since only static lib is provided, you need to create some dll once, but if you are not satisfied with the PTX compile in the driver, you can also do PTX compile on the client side with pure Python!

Runtime API

Sometimes you want to use a library built on top of the runtime API, such as cuSparse.

Most of the dll/so can be redistributed, so

https://docs.nvidia.com/cuda/eula/index.html

If necessary, bundle it with your own app to handle it. (E.g. CUDA SDK installation is troublesome every time in CI environment)

TODO

Recommended Posts

Note calling the CUDA Driver API with Python ctypes
Call the API with python3.
Hit the Etherpad-lite API with Python
A note about hitting the Facebook API with the Python SDK
[Note] Export the html of the site with python.
I moved the automatic summarization API "summpy" with python3.
I tried hitting the API with echonest's python client
Use Trello API with python
Use Twitter API with Python
[Note] Operate MongoDB with Python
Web API with Python + Falcon
The Linux Watchdog driver API
Play RocketChat with API / Python
Try hitting the Twitter API quickly and easily with Python
The first API to make with python Djnago REST framework
Let's touch the API of Netatmo Weather Station with Python. #Python #Netatmo
Extract the xz file with python
Control the motor with a motor driver using python on Raspberry Pi 3!
Getting the arXiv API in Python
Create Awaitable with Python / C API
Get reviews with python googlemap api
Hit the Sesami API in Python
Run Rotrics DexArm with python API
Specifying the date with the Twitter API
[September 2020 version] Explains the procedure to use Gmail API with Python
Discord Bot with recording function starting with Python: (5) Directly operate the Discord API
Quine Post with Qiita API (Python)
Get the weather with Python requests 2
Create REST API that returns the current time with Python3 + Falcon
Find the Levenshtein Distance with python
The story of making a standard driver for db with python.
Install the Python plugin with Netbeans 8.0.2
[Note] Hello world output with python
[python] Read information with Redmine API
Hit the web API in Python
I liked the tweet with python. ..
Master the type with Python [Python 3.9 compatible]
Access the Twitter API in Python
Python Note: Get the current month
How to send a request to the DMM (FANZA) API with python
I tried to get the authentication code of Qiita API with Python.
Hit a method of a class instance with the Python Bottle Web API
Get the number of articles accessed and likes with Qiita API + Python
I tried to get the movie information of TMDb API with Python
Make the Python console covered with UNKO
Collecting information from Twitter with Python (Twitter API)
Access the Docker Remote API with Requests
Try using the Wunderlist API in Python
[Python] Set the graph range with matplotlib
Automatically create Python API documentation with Sphinx
[Note] Get data from PostgreSQL with Python
Try using the Kraken API in Python
Behind the flyer: Using Docker with Python
Simple Slack API client made with Python
Note when creating an environment with python
Check the existence of the file with python
[Python] Get the variable name with str
[Python] Round up with just the operator
Display Python 3 in the browser with MAMP
Note for Pyjulia calling Julia from Python
Tweet using the Twitter API in Python