使用 Flask 时 PyCUDA 上下文错误 [英] PyCUDA context error when using Flask

查看:645
本文介绍了使用 Flask 时 PyCUDA 上下文错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PyCUDA 来实现如图所示的 smooth_local_affine 这里.当我只是在 linux 上运行程序时,它运行良好.但是当我尝试在 Flask 上下文中导入它时:

from smooth_local_affine import smooth_local_affine从烧瓶进口烧瓶app = Flask(_name_)...

出现以下错误:

<块引用>

-------------------------------------------------------------------PyCUDA 错误:模块清理时上下文堆栈不为空.-------------------------------------------------------------------清理上下文堆栈时,上下文仍处于活动状态.在我们执行的这一点上,CUDA 可能已经被取消初始化,所以我们没有办法干净利落地完成.该程序将立即中止.使用 Context.pop() 来避免这个问题.

然后我尝试添加context.pop(),然后又出现错误;

<块引用>

atexit._run_exitfuncs 中的错误:回溯(最近一次调用):
文件"/home/yifang/anaconda3/envs/python3/lib/python3.6/site-packages/pycuda-2017.1-py3.6-linux-x86_64.egg/pycuda/autoinit.py",第 14 行,在 _finish_up 中context.pop() pycuda._driver.LogicError:context::pop 失败:设备上下文无效 - 无法弹出非当前上下文

有谁知道如何在 Flask 环境中运行 PyCuda?或者也许我可以在不使用 PyCuda 的情况下使用此 smooth_local_affine 功能的其他替代方法?

解决方案

我在这里介绍一个解决方案,因为我已经尝试了很多解决方案但仍然无法奏效.幸运的是,我找到了一个正确答案.

一些解决方案,例如

import pycuda.autoinit

cuda.init设备 = cuda.Device(0)ctx = device.make_context()输入、输出、绑定、流=allocate_buffer()ctx.pop()

如果您将脚本作为简单程序运行,这些可能会起作用,但如果您使用 Flask 或其他 Web 服务器运行,则会引发上下文错误.根据我的搜索,原因可能是flask server在请求进来时会产生新的线程.

在这种情况下的真正解决方案非常简单,您只需添加如下代码:

 以 engine.create_execution_context() 作为上下文:ctx = cuda.Context.attach()输入、输出、绑定、流=allocate_buffer()ctx.分离()

这对我有用

I am using the PyCUDA to implement the smooth_local_affine as shown here. It works well when I simply run the program on linux. But when I tried to import it under Flask context:

from smooth_local_affine import smooth_local_affine
from flask import Flask
app = Flask(_name_)
...

The following error occurs:

-------------------------------------------------------------------  
PyCUDA ERROR: The context stack was not empty upon module cleanup.
-------------------------------------------------------------------   
A context was still active when the context stack was being cleaned up.  
At this point in our execution, CUDA may already have been deinitialized, 
so there is no way we can finish cleanly. The program will be aborted now. 
Use Context.pop() to avoid this problem.

Then I tried to add context.pop(),then another error occurs;

Error in atexit._run_exitfuncs: Traceback (most recent call last):
File "/home/yifang/anaconda3/envs/python3/lib/python3.6/site-packages/pycuda-2017.1-py3.6-linux-x86_64.egg/pycuda/autoinit.py", line 14, in _finish_up context.pop() pycuda._driver.LogicError: context::pop failed: invalid device context - cannot pop non-current context

Anyone knows how to run PyCuda in Flask environment? Or maybe any alternative ways that I can use this smooth_local_affine feature without using PyCuda?

解决方案

Let me present one solution here because I have tried a lot of solutions but still not work. Fortunately I found one correct answer.

some solutions like

import pycuda.autoinit

or

cuda.init 
device = cuda.Device(0) 
ctx = device.make_context() 
inputs, outputs, bindings, stream = allocate_buffer() 
ctx.pop()

these may work if you run the script as simple program, but it will raise context error if you run with flask or other web servers. According to my searching, the reason is possibally that flask server would spawn new thread when a request came in.

The real solution in such a circumstance is quite simple and you should just add code like this:

with engine.create_execution_context() as context:
    ctx = cuda.Context.attach()
    inputs, outputs, bindings, stream = allocate_buffer()
    ctx.detach()

This works for me

这篇关于使用 Flask 时 PyCUDA 上下文错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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