CUDA operation error: cuGraphicsResourceGetMappedPointer failed: resource not mapped as pointer when pycuda.gl.RegisteredMapping.device_ptr_and_size() #455
Describe the bug
I have an variable that is <class 'pycuda._driver.RegisteredMapping'> yet when I try to call pycuda.gl.RegisteredMapping.device_ptr_and_size() to get its pointer and size so that I can map it to a GPU array (then to a pytorch tensor for down stream tasks), I got the following error:
CUDA operation error: cuGraphicsResourceGetMappedPointer failed: resource not mapped as pointer
To Reproduce
Steps to reproduce the behavior:
go to the notebook code bellow
run the whole notebook with an available spout stream matching the names in the notebook
Expected behavior
logs:
<class 'pycuda._driver.RegisteredMapping'>
CUDA operation error: cuGraphicsResourceGetMappedPointer failed: resource not mapped as pointer
Environment (please complete the following information):
OS: [e.g. Windows 11]
CUDA version: [e.g. 12.5]
CUDA driver version: [e.g. 560.81]
PyCUDA version: [e.g 2024.1.2]
Python version: [e.g. 3.12]
Additional context
I had some trouble installing pycuda with OpenGL, had to change siteconf.py to install the pycuda.gl module
Code:
%%
import torch
import numpy as np
import pycuda.driver as cuda
import pycuda.gl as cuda_gl
import pycuda.gpuarray as gpuarray
from pycuda.gl import graphics_map_flags
import pycuda.autoinit
from OpenGL.GL import *
import glfw
from SpoutGL import SpoutReceiver, SpoutSender
Describe the bug I have an variable that is
<class 'pycuda._driver.RegisteredMapping'>
yet when I try to callpycuda.gl.RegisteredMapping.device_ptr_and_size()
to get its pointer and size so that I can map it to a GPU array (then to a pytorch tensor for down stream tasks), I got the following error:CUDA operation error: cuGraphicsResourceGetMappedPointer failed: resource not mapped as pointer
To Reproduce Steps to reproduce the behavior:
Environment (please complete the following information):
Additional context I had some trouble installing pycuda with OpenGL, had to change siteconf.py to install the pycuda.gl module
Code:
%%
import torch import numpy as np import pycuda.driver as cuda import pycuda.gl as cuda_gl import pycuda.gpuarray as gpuarray from pycuda.gl import graphics_map_flags import pycuda.autoinit from OpenGL.GL import * import glfw from SpoutGL import SpoutReceiver, SpoutSender
%%
def setProjection(width, height): glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(0, width, height, 0, 1, -1) glMatrixMode(GL_MODELVIEW)
def drawSquare(width, height): glEnable(GL_TEXTURE_2D) glBegin(GL_QUADS) glTexCoord2f(0, 0); glVertex2f(0, 0) glTexCoord2f(1, 0); glVertex2f(width, 0) glTexCoord2f(1, 1); glVertex2f(width, height) glTexCoord2f(0, 1); glVertex2f(0, height) glEnd() glDisable(GL_TEXTURE_2D)
def check_gl_error(): err = glGetError() if err != GL_NO_ERROR: print(f"OpenGL error: {err}")
class SimpleModel(torch.nn.Module): def init(self): super(SimpleModel, self).init() self.conv = torch.nn.Conv2d(4, 4, 3, padding=1)
%%
Replace with your parameters
receiverName = 'tdOut' senderName = 'pythonOut' displayWidth, displayHeight = 1280, 720
Initialize GLFW and create a window
if not glfw.init(): raise Exception("GLFW initialization failed")
window = glfw.create_window(displayWidth, displayHeight, "Spout Receiver/Sender", None, None) if not window: glfw.terminate() raise Exception("GLFW window creation failed")
glfw.make_context_current(window)
Set up Spout receiver and sender
spout_receiver = SpoutReceiver() spout_receiver.setReceiverName(receiverName) spout_sender = SpoutSender() spout_sender.setSenderName(senderName)
def create_texture(width, height): tex_id = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, tex_id) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, None) glBindTexture(GL_TEXTURE_2D, 0) check_gl_error() return tex_id
def recreate_textures(new_width, new_height): global width, height, input_tex_id, output_tex_id, input_image, output_image
Create initial textures
input_tex_id = create_texture(displayWidth, displayHeight) output_tex_id = create_texture(displayWidth, displayHeight)
setProjection(displayWidth, displayHeight) glClearColor(0.0, 0.0, 0.0, 1.0)
Create CUDA-OpenGL interop resources
cuda.init() cuda_gl_context = cuda_gl.make_context(pycuda.autoinit.device) cuda_gl_context.push()
input_image = cuda_gl.RegisteredImage(int(input_tex_id), GL_TEXTURE_2D) output_image = cuda_gl.RegisteredImage(int(output_tex_id), GL_TEXTURE_2D)
Define a simple PyTorch model (replace with your actual model)
model = SimpleModel().cuda()
width, height = displayWidth, displayHeight
Main loop
while not glfw.window_should_close(window): glfw.poll_events()
Cleanup
glDeleteTextures([input_tex_id, output_tex_id]) glfw.terminate() spout_sender.releaseSender() cuda_gl_context.pop()