问题描述
我想在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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!