PETSc |
PETSc and Threads |
. PETSc is not currently thread-safe; because the issues involved with toolkits and thread safety is complex, this short answer is almost meaningless. Thus, this page attempts to explain how threads and thread safety relate to PETSc. Note that we are only discussing here "software threads" as opposed to "hardware threads" Threads are used in 2 main ways
Threads are merely streams of control and do not have any global data associated with them. Any global variables (e.g., common blocks in FORTRAN) are "shared" by all the threads, that is, any thread can access and change that data. In addition, any space allocated (e.g., in C with malloc or C++ with new) that a thread has a reference to can be read/changed by that thread. The only private data a thread has are the local variables in the subroutines that it has called (i.e., the stack for that thread) or local variables that you explicitly indicate are to be not shared in compiler directives. In its simplest form, thread safety means that any memory (global or allocated) that more than one thread has access to, has some mechanism to insure that it remains consistent when the various threads act upon it. This can be managed by simply associating a lock with each "memory" and making sure that each thread locks the memory before accessing it and unlocks when it has completed accessing the memory. In an object oriented library, rather than associating locks with individual data items, one can think about associating locks with objects; so that only a single thread can operate on an object at a time. PETSc is not thread-safe for three reasons:
PETSc can be used in a limited way that is thread safe, so long as only one of the threads calls methods on objects, or calls profiled PETSc routines, PETSc will run fine. For example, one could use loop level parallelism to evaluate a finite difference stencil on a grid, this is supported through the PETSc routines VecCreateShared(), see src/snes/examples/tutorials/ex5s.c. However, this is limited power. Some concerns about a thread model for parallelism. A thread model for parallelism of numerical methods appears to be powerful for problems that can store their data in very simple (well controlled) data structures. For example, if field data is stored in a two dimensional array, then each thread can be assigned a nonoverlapping slice of data to operate on. OpenMP makes managing much of this sort of thing reasonably straightforward. When data must be stored in a more complicated opaque data structure (for example an unstructured grid or sparse matrix), it is more difficult to partition the data among the threads to prevent conflict and still get good performance. More difficult, but certainly not impossible. For these situations, perhaps it is more natural for each thread to maintain its own private data structure that is later merged into a common data structure; but to do this one has to introduce a great deal of private state associated with the thread, i.e., it becomes more like a "light-weight process". In conclusion, at least for the PETSc package, the concept of being thread-safe is not simple. It has major ramifications about its performance and how it would be used; it is not a simple matter of throwing a few locks around and then every thing is honky-dory. If you have any comments/brickbats on this summary, please direct them to petsc-maint@mcs.anl.gov; we are interested in alternative viewpoints. |
|
|