Search code examples
pythoncudapycuda

Where is pycuda.driver.mem_alloc() ? In pycuda/driver.py I can only find mem_alloc_like()


I am new to python and PYCUDA.

In many PYCUDA examples I can find codes like this:

import pycuda.driver as cuda

cuda.mem_alloc(a.nbytes) 

But when I take a look into pycuda/driver.py, I can't find where mem_alloc is defined.

I can only find mem_alloc_like, which calls mem_alloc():

def mem_alloc_like(ary):

    return mem_alloc(ary.nbytes)

which tells me that mem_alloc must be somewhere in the libraries driver.py imports.

However, the following are the imports I find in driver.py, and none of six, numpy, or sys has a function named as "mem_alloc()"

from __future__ import absolute_import
from __future__ import print_function
import six
try:
    from pycuda._driver import *  # noqa
except ImportError as e:
    if "_v2" in str(e):
    from warnings import warn
    warn("Failed to import the CUDA driver interface, with an error "
            "message indicating that the version of your CUDA header "
            "does not match the version of your CUDA driver.")
    raise
import numpy as np
import sys

Solution

  • pyCUDA is wrapper around CUDA driver API. This is how pyCUDA exposes those APIs

    1. write wrapper around driver API in C / C++

    2. Expose those function to python using Boost::python because python's interpreter is implemented in C(Cpython)

    3. Compile those wrapper into a shared library that is _driver.so

    4. import the shared library that is the from pycuda._driver import *.

    But when I take a look into pycuda/driver.py, I can't find where mem_alloc is defined.

    It is defined in pycuda/src/cpp/cuda.hpp