Giter Club home page Giter Club logo

Comments (5)

f0k avatar f0k commented on May 19, 2024

Is there any C API for CUDAMat? I want to create a matrix using CUDAMat in python from an already allocated GPU memory and I can only access the memory by a pointer in c.

CUDAMat is a C shared library accessed from Python via the ctypes module. So the CUDAMatrix is defined in Python and there is no C API to create it. However, if you can get your pointer into Python, you can create a CUDAMatrix accessing this pointer. See this example:
https://github.com/Theano/Theano/blob/7a97cfe/theano/misc/cudamat_utils.py#L67-L86
In that example, it is assumed that the memory belongs to the already existing object x, so the matrix is marked as not owning the data, and the object x is attached as a property to the CUDAMatrix object so Python will not delete x before deleting the CUDAMatrix. If instead, you want the memory to be managed by cudamat (and freed with cublasFree), just set owns_data to True.

Hope this makes things clear -- if you want to make this use case easier, I'd be open for a PR that adds a factory method CUDAMatrix.from_memory or something, taking a device pointer, shape and an optional parent object.

from cudamat.

knsong avatar knsong commented on May 19, 2024

@f0k Thanks for your answer! It's clear.
But the python module I'm trying to extend uses boost.python to wrap C++ code and doesn't expose the GPU memory pointer to python. So I have to start from C++ and it seems that boost.python doesn't have an elegant way to expose a pointer such as float * to python. So I'm wondering whether can I use cudamat to handle this along with boost python?

from cudamat.

knsong avatar knsong commented on May 19, 2024

Or what type of object should I expose in python managing the GPU memory pointer?

from cudamat.

f0k avatar f0k commented on May 19, 2024

So I have to start from C++ and it seems that boost.python doesn't have an elegant way to expose a pointer such as float * to python.

There should be a way. In the worst case, you can expose it as a Python long and then cast it to a pointer within Python using ctypes, as in the example code I linked to.
You can also create the cudamat struct in C/C++, but then you still have to pass it to Python -- this won't make things easier.

from cudamat.

knsong avatar knsong commented on May 19, 2024

@f0k I will try it. Thanks for your thorough explanation.

from cudamat.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.