• Work
    December 6, 2005

Using multiple threads

Guest Author

The question is: 'How can I take a single threaded application and make it multi-threaded?'

There are a number of ways, and it's probably best to start with the simplest and work towards the complex.

  • The simplest way is to recompile with the -xautopar flag which will ask the compiler to find opportunities for extracting parallelism from the source code. There are some problems that the compiler has to face:
    • Parallelisation changes the order of calculations, so by default the compiler will not parallelise places where this happens. The flag to give it permission is
    • Potential aliasing between pointers restricts the places where compiler can assume that two memory operations are independent. The flags that tell the compiler what degree of aliasing to assume are
      -xrestrict (see
      restricted pointers
      ) and
      -xalias_level=[level] (don't use them without knowing that they are safe for the code)
  • The next approach is to use OpenMP to tell the compiler how to parallelise the code. OpenMP uses
    that are inserted into the source code that guide the parallelisation of the code. The pragma's are only recognised with the compiler flag
    -xopenmp, without the flag the source code remains the same as the original single threaded code. This gives two big advantages; first that it is easy to revert back to the single threaded code; second that the program can be 'incrementally' parallelised (ie parallelised one loop at a time).
  • An alternative approach is to use
    MPI (Message Passing Interface). MPI is useful when spreading work over a cluster of systems. In order to use it, the code needs to be rewritten in such a way as to split the work into discrete chunks that do not depend on each other. Each chunk of work is then run on a different system, and the systems exchange information when they need to sync up.
  • Another approach which also requires some changes to the source is to use pthreads. This requires the developer to to explicitly manage the threading - giving them the most control.

Other resources:

  • Parallelising C codes
  • Parallelising C++ codes


Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.