Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyCUDA context error when using Flask

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?

like image 700
cloudwayx Avatar asked Apr 01 '18 05:04

cloudwayx


2 Answers

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

like image 183
aforwardz Avatar answered Sep 22 '22 09:09

aforwardz


Starting the Flask application in non-threaded mode worked for me

app.run(host=HOST, port=PORT, debug=False,threaded=False)
like image 27
Mr Vinagi Avatar answered Sep 25 '22 09:09

Mr Vinagi