pyCUDA with Flask gives pycuda._driver.LogicError: cuModuleLoadDataEx

前端 未结 2 1263
误落风尘
误落风尘 2021-01-17 11:35

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 u

相关标签:
2条回答
  • 2021-01-17 12:13

    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'])

    0 讨论(0)
  • 2021-01-17 12:25

    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"
    
    0 讨论(0)
提交回复
热议问题