带有Flask的pyCUDA提供pycuda._driver.LogicError:cuModuleLoadDataEx [英] pyCUDA with Flask gives pycuda._driver.LogicError: cuModuleLoadDataEx

查看:368
本文介绍了带有Flask的pyCUDA提供pycuda._driver.LogicError:cuModuleLoadDataEx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在flask服务器上运行pyCUDA代码.该文件可以直接使用python3正确运行,但是使用flask调用相应的功能时会失败.

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.

以下是相关代码:

cudaFlask.py:

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(仅调用函数的部分):

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

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

在运行python cudaFlask.py时,按预期方式获得输出done,但是在启动服务器并在website/bundle处执行POST请求时,在烧瓶控制台上收到以下错误:

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 - 

在线mod = SourceModule...

我要去哪里错了? 那里有一个类似的问题,但尚未得到答案.

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

推荐答案

解决了在flask中延迟加载并手动制作context的问题(即在PyCUDA中没有pycuda.autoinit的情况.)

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

请参考,以便在flask中进行延迟加载.

Refer this for lazy loading in flask.

我的views.py文件:

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"

这篇关于带有Flask的pyCUDA提供pycuda._driver.LogicError:cuModuleLoadDataEx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆