This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Parametric analysis: Thread safety

+3 votes

Dear all,

I wrote a module that computes a vector field, given an angle. Now, I need to perform the very same computation on all possible angles, for instance 0 - 2 \pi, and I thought it could be easy using threads:

def chunks(l, n):
    """ Yield successive n-sized chunks from l.
    """
    for i in xrange(0, len(l), n):
        yield l[i:i+n]

AlphaTrial = map(lambda x : math.pi * x / 180, range(0, 359))

# Queue for multithreading results
q = Queue.Queue()

# Number of threads
nthreads = 4

# Thread list
t = []

for i in chunks(AlphaTrial, len(AlphaTrial) /  nthreads):
    #def solver(mesh, u, alphaList, pre_rect, mid_rect, pos_rect, CrackLength, q):
    p = threading.Thread(target = thetasolver.solver, args = (mesh, ulast, i, pre_rect, mid_rect, pos_rect, CrackLength, q))
    #p = threading.Thread()
    t = t + [p]
    p.start()

for i in t:
    i.join()

#   thetasolver.solver(mesh, ulast, AlphaTrial, pre_rect, mid_rect, pos_rect, CrackLength, q)

I was wrong, the serial working function thetasolver.solver() conflicts when using threads, and I have a lot of errors:

    In instant.import_module_directly: Failed to import module 'dolfin_compile_code_5bd690057874abdfd6640fcf7cb4f51c' from '/Users/sensei/.instant/cache/dolfin_compile_code_5bd690057874abdfd6640fcf7cb4f51c';
    ImportError:No module named dolfin_compile_code_5bd690057874abdfd6640fcf7cb4f51c;
    Failed to import module found in cache. Modulename: 'dolfin_compile_code_5bd690057874abdfd6640fcf7cb4f51c';
    Path: '/Users/sensei/.instant/cache/dolfin_compile_code_5bd690057874abdfd6640fcf7cb4f51c';
    ImportError:No module named dolfin_compile_code_5bd690057874abdfd6640fcf7cb4f51c;
    Exception in thread Thread-1:
    Traceback (most recent call last):
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 808, in __bootstrap_inner
        self.run()
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 761, in run
        self.__target(*self.__args, **self.__kwargs)
      File "/Users/sensei/Documents/Projects/fenics/thetasolver.py", line 106, in solver
    ...
    In instant.import_module_directly: Failed to import module 'dolfin_compile_code_a02552e49a92b850b27301fe16336757' from '/Users/sensei/.instant/cache/dolfin_compile_code_a02552e49a92b850b27301fe16336757';
    ...
    In instant.recompile: The module did not compile with command 'make VERBOSE=1', see '/Users/sensei/.instant/error/dolfin_compile_code_40851ad132f51f309639266aedbece88/compile.log'
    In instant.recompile: The module did not compile with command 'make VERBOSE=1', see '/Users/sensei/.instant/error/dolfin_compile_code_a148337abd11a60e53a185a184c56b83/compile.log'
    ...

Since the only change is in parallelization, I believe I cannot simply do that. Note that the only thing that changes is the angle, mesh and other fields are constants. The thetasolver method creates its own local VectorFunctionSpace, DirichletBC and the needed structures to perform an analysis.

How can I make a parametric analysis in parallel, or, with an equivalent speedup with respect to serializing it?

Thanks!

asked Nov 21, 2014 by senseiwa FEniCS User (2,620 points)

So, no luck with parametric analyses?

1 Answer

+1 vote

I'm really interested in this type of analysis, allowing a parallelization of the code.

Please, someone answer asap!

answered Nov 27, 2014 by gifo75 FEniCS Novice (650 points)
...