2013 Project Week:Threaded SimpleITK Modules

From NAMIC Wiki
Revision as of 22:19, 18 December 2012 by Pieper (talk | contribs) (Created page with '__NOTOC__ <gallery> Image:PW-SLC2013.png|Projects List Image:yourimagehere.png| Image description </gallery> ==Key Investigators== * Brad …')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Home < 2013 Project Week:Threaded SimpleITK Modules

Key Investigators

  • Brad Lowekamp, NLM
  • Steve Pieper, Isomics

Project Description

Objective

  • Support multi-threaded execution of SimpleITK filters.

Approach, Plan

Progress

  • Progress here

Preliminary Work

The example code below calls two different python functions from inside threads. One function calls the 'time.sleep' method, and one uses VTK to do some processing. As you can see from the output printed below, the VTK threads block, and a new thread is not started until the last one completes. On the other hand, the sleep based thread does return right away. The behavior is based on the CPython global interpreter lock.

import vtk
import threading
import Queue
import time, datetime
import os

class ThreadClass(threading.Thread):
  def __init__(self, queue, work):
    threading.Thread.__init__(self)
    self.queue = queue
    self.work = work
          
  def run(self):
    now = datetime.datetime.now()
    self.queue.put( "start %s, time: %s" % (self.getName(), now) )
    self.work()
    now = datetime.datetime.now()
    self.queue.put( "end %s, time: %s" % (self.getName(), now) )

def process():
  e = vtk.vtkImageEllipsoidSource()
  e.SetWholeExtent(0,300,0,300,0,300)
  s = vtk.vtkImageGaussianSmooth()
  s.SetInputConnection(e.GetOutputPort())
  s.Update()

def sleep():
  time.sleep(1)

for work,message in ((process,'vtk'), (sleep,'sleep')):
  print ('threading test with %s:' % message)
  threads = 3
  queue = Queue.Queue()
  for i in range(threads):
    t = ThreadClass(queue,work)
    t.start()
  print('started:')

  for i in range(2*threads):
    print(queue.get())
  print('finished\n')


#8 Slicer-superbuild $ ./VTK-build/bin/vtkpython /Users/pieper/Dropbox/hacks/threadVTK.py
threading test with vtk:
started:
start Thread-1, time: 2012-12-18 17:13:17.047282
end Thread-1, time: 2012-12-18 17:13:18.006989
start Thread-2, time: 2012-12-18 17:13:18.007318
end Thread-2, time: 2012-12-18 17:13:18.953446
start Thread-3, time: 2012-12-18 17:13:18.953762
end Thread-3, time: 2012-12-18 17:13:19.897673
finished

threading test with sleep:
started:
start Thread-4, time: 2012-12-18 17:13:19.898124
start Thread-5, time: 2012-12-18 17:13:19.898346
start Thread-6, time: 2012-12-18 17:13:19.898506
end Thread-4, time: 2012-12-18 17:13:20.899137
end Thread-6, time: 2012-12-18 17:13:20.899378
end Thread-5, time: 2012-12-18 17:13:20.899469
finished