pyCUDA with Flask gives pycuda._driver.LogicError: cuModuleLoadDataEx

核能气质少年 提交于 2020-01-14 07:23:07

问题


I want to run a pyCUDA code on a flask server. The file runs correctly directly using python3 but fails when the corresponding function is called using flask.

Here is the relevant code:

cudaFlask.py:

import pycuda.autoinit
import pycuda.driver as drv
import numpy

from pycuda.compiler import SourceModule

def cudaTest():
    mod = SourceModule("""
        int x = 4;
    """)

    print ("done")
    return

if __name__ == "__main__":
    cudaTest()

server.py (only the part which calls the function):

@app.route('/bundle', methods=['POST'])
def bundle_edges():
    cudaTest()
    return "success"

On running python cudaFlask.py I get the output done as expected but on starting the server and doing POST request at website/bundle I get the following error on the flask console:

pycuda._driver.LogicError: cuModuleLoadDataEx failed: invalid device context - 

on the line mod = SourceModule...

Where am I going wrong? There is a similar question out there but it has not been answered yet.


回答1:


Solved the issue with lazy loading in flask and making the context manually (i.e. without pycuda.autoinit in PyCUDA.

Refer this for lazy loading in flask.

My views.py file:

import numpy as np
import pycuda.driver as cuda
from pycuda.compiler import SourceModule

def index():
    cuda.init()
    device = cuda.Device(0) # enter your gpu id here
    ctx = device.make_context()

    mod = SourceModule("""
        int x = 4;
    """)

    ctx.pop() # very important

    print ("done")
    return "success"



回答2:


PyCUDA may not be compatible with WSGI web server contexts. You could possibly make it work if you use some kind of message queue like Celery, where the HTTP request places a job on the queue and the worker on the other side of the queue runs the CUDA program.

Edit: A quick and easy way would be to use Python Subprocess check_output function

In the web request:

subprocess.check_output(['python', 'cudaFlask.py'])



来源:https://stackoverflow.com/questions/50601029/pycuda-with-flask-gives-pycuda-driver-logicerror-cumoduleloaddataex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!