Discussion:
PyCuda in QThread using moveToThread
(too old to reply)
David G Grier
2018-02-16 19:48:06 UTC
Permalink
I am using pycuda to compute holograms for an optical trapping
application that uses PyQt4 for a GUI front end. I would like to move
the pycuda computation into a QThread to keep the GUI responsive.

Is there an up-to-date working example of "the right way" to move a
pycuda computational object into a QThread using moveToThread?
Despite much searching, I have not turned up sample code. The
pycuda FAQ addresses subclassing threads, but not moveToThread
method of QThread.

The following minimal example of a "do nothing" background
object appears to work correctly. It creates an object,
moves it into a thread, creates a pycuda context for the object in
its thread, and then stops the object by quitting the thread.
Before I dig deeper, can anyone confirm that this is the right approach?
Or am I missing something

=== snip for minimal example ===

from PyQt4 import QtCore
import pycuda.driver as cuda

cuda.init()

class ComputeObject(QtCore.QObject):

def __init__(self, deviceID=0):
super(ComputeObject, self).__init__()
self.deviceID = deviceID

@QtCore.pyqtSlot()
def start(self):
print('starting')
self.device = cuda.Device(self.deviceID)
self.context = self.device.make_context()

@QtCore.pyqtSlot()
def stop(self):
print('stopping')
self.context.pop()
self.context = None

if __name__ == '__main__':
from PyQt4.QtGui import QApplication
import sys

app = QApplication(sys.argv)
obj = ComputeObject()

thread = QtCore.QThread()
thread.start()
obj.moveToThread(thread)
thread.started.connect(obj.start)
thread.finished.connect(obj.stop)

thread.quit()
thread.wait()
--
*David G. Grier <http://physics.nyu.edu/grierlab/>*
Department of of Physics <http://physics.nyu.edu/> and Center for Soft
Matter Research <http://csmr.as.nyu.edu/>
New York University <http://nyu.edu/>
726 Broadway, room 873, New York, NY 10003 <https://goo.gl/maps/Tbs7JatcW4y>
Andreas Kloeckner
2018-02-16 20:06:11 UTC
Permalink
Post by David G Grier
I am using pycuda to compute holograms for an optical trapping
application that uses PyQt4 for a GUI front end. I would like to move
the pycuda computation into a QThread to keep the GUI responsive.
Is there an up-to-date working example of "the right way" to move a
pycuda computational object into a QThread using moveToThread?
Despite much searching, I have not turned up sample code. The
pycuda FAQ addresses subclassing threads, but not moveToThread
method of QThread.
The following minimal example of a "do nothing" background
object appears to work correctly. It creates an object,
moves it into a thread, creates a pycuda context for the object in
its thread, and then stops the object by quitting the thread.
Before I dig deeper, can anyone confirm that this is the right approach?
Or am I missing something
As long as you make a new context for each thread (and destroy it at the
end) as you do, you should be OK.

Andreas

_______________________________________________
PyCUDA mailing list

Loading...