Multithreaded Programming
notes on the parallel Maple engine
Introduction
The Shared Memory Model
Limitations
Programming
Maple provides a parallel mode for its math engine. The parallel mode can evaluate multiple expressions at the same time. On a computer with multiple CPUs, this allows Maple to solve problems faster.
Note: Maple automatically determines the number of processors it should use. CPUs with Intel's hyperthreaded technology are treated as having one processor per physical core. To adjust the number of processors that Maple uses, see the numcpus kernel option.
When multiple threads are running, some variables are thread global, meaning that they are accessible from any thread. As these values are shared between threads, any changes in thread global variables are visible in all threads. Variables that are not thread global are called thread local, meaning that they are only accessible from the current thread.
Thread Global
In Maple, any variable in global scope is thread global. This means that any global variable can be accessed and modified from any thread. Maple packages, which are assigned to global names, are thread global. In addition, kernel options and interface variables are also thread global.
In a thread global module, that is, a module that is assigned to a global name, module local variables are also thread global. A module is a data structure, and the local nature of some variables controls access to those variables in a Maple language context, but not in a thread access context.
Thread Local
The call stack, including function locals and environment variables, are thread local. Evaluating a procedure in parallel is similar to evaluating a procedure recursively. The recursive call is given its own environment with its own local variables. Modifying those locals does not affect the locals in the calling function. However, if the recursive call does modify global variables, then those changes are visible to the parent. Similarly, when a procedure is evaluated in parallel, it has a local environment that is not visible to other threads; however, changes to global values are visible.
Module locals declared with the thread_local type modifier are also thread local. These variables store a different value for each thread that accesses the module.
Atomic Operations
Atomic operations are operations that complete as a single uninterruptible unit. In Maple, the assignment operation is atomic. Therefore, if one thread assigns 1 to the global variable x and another thread also assigns 2 to the same global variable x, then either the assignment of 1 to x starts and completes before the assignment of 2, or the other way around. Consequently, both assignments cannot occur at the same time and x contains an valid result in both cases. The Maple engine also provides the following atomic operations: communication with the interface (for print, printf, interface, etc.), file operations, and external calling.
Maple Commands
Parallel programming is more complex than single-threaded programming. Code that is written in a single-threaded environment is often not safe to run in parallel. Thus most of the Maple library is not safe for parallel programming. Maple routines that are thread-safe will be documented on their help page. The Maple kernel is thread-safe, so any operation that is implemented in the kernel should be safe to run in parallel. See the thread-safe functions page for a list of the functions verified to be thread-safe.
Performance Issues
See the multithreaded performance limitations help page for more information about specific performance issues with multithreaded parallel programming.
Maple provides two models for multithreaded programming, the Task Programming Model and the Threads package.
Task Programming Model
The Task Programming Model is a high-level programming model, designed to make multithreaded programming easier. In this model, users create Tasks instead of threads. A Task is a function call (a procedure with arguments) that can be executed on a thread. Maple will automatically schedule the Tasks to threads. This allows code written using the Task programming model to scale to computers with a large number of processors.
Threads[Create]
The Threads[Create] function allows the explicit creation of threads. Combined with mutexes and condition variables, this provides a similar programming interface as pthreads. Correct and efficient programming with Threads[Create] can be difficult. Therefore it is strongly suggested that the Task Programming Model be used instead.
External Call and Open Maple
External calling and Open Maple routines can only be called from threads that are created by Maple. Threads created by calling other threading library routines (like pthreads_create or CreateThread) cannot call any of the External calling API functions.
Other features
There are also a some multithreaded related kernel options. The new kernel option multithreaded returns a value of true if the engine is in multithreaded mode. The numcpus kernel option returns the number of CPUs that Maple determines the current computer to have. The numactivethreads kernel option returns the number of currently executing threads.
See Also
CodeTools[ThreadSafetyCheck]
kernelopts
module
procedure
Threads
Download Help Document