<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://grasswiki.osgeo.org/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=%E2%9A%A0%EF%B8%8FOleTange</id>
	<title>GRASS-Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://grasswiki.osgeo.org/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=%E2%9A%A0%EF%B8%8FOleTange"/>
	<link rel="alternate" type="text/html" href="https://grasswiki.osgeo.org/wiki/Special:Contributions/%E2%9A%A0%EF%B8%8FOleTange"/>
	<updated>2026-07-10T05:25:56Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://grasswiki.osgeo.org/w/index.php?title=OpenMP&amp;diff=11068</id>
		<title>OpenMP</title>
		<link rel="alternate" type="text/html" href="https://grasswiki.osgeo.org/w/index.php?title=OpenMP&amp;diff=11068"/>
		<updated>2010-06-14T10:19:55Z</updated>

		<summary type="html">&lt;p&gt;⚠️OleTange: GNU Parallel&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Multithreaded jobs in GRASS ==&lt;br /&gt;
&lt;br /&gt;
* see also the [[Parallel GRASS jobs]] wiki page.&lt;br /&gt;
&lt;br /&gt;
[http://openmp.org/wp/ OpenMP] is an implementation of multithreading, a method of parallelization whereby the master &amp;quot;thread&amp;quot; (a series of instructions executed consecutively) &amp;quot;forks&amp;quot; a specified number of slave &amp;quot;threads&amp;quot; and a task is divided among them (from [http://en.wikipedia.org/wiki/OpenMP wikipedia]). The job is distributed over the available processor cores (2-core, 4-core, ...).&lt;br /&gt;
&lt;br /&gt;
The (yet) only parallelized library in GRASS &amp;gt;=6.3 is GRASS Partial Differential Equations Library (GPDE). The library design is thread safe and supports threaded parallelism with OpenMP. The code is not yet widely used in GRASS. See the [http://download.osgeo.org/grass/grass6_progman/gpdelib.html gpde programmer's manual] for details.&lt;br /&gt;
&lt;br /&gt;
How to activate it with GCC &amp;gt;= 4.2 (compiler flag '-fopenmp' as well as library '-lgomp' are needed):&lt;br /&gt;
&lt;br /&gt;
 # GPDE with openMP support:&lt;br /&gt;
 &lt;br /&gt;
 cd lib/gpde/&lt;br /&gt;
 vim Makefile&lt;br /&gt;
 # uncomment the EXTRA_CFLAGS row and switch the two existing EXTRA_LIBS rows&lt;br /&gt;
&lt;br /&gt;
Integrated system-wide support for OpenMP via a --with-openmp ./configure flag has been requested in {{trac|657}}.&lt;br /&gt;
&lt;br /&gt;
=== OpenMP support in GRASS 7 ===&lt;br /&gt;
&lt;br /&gt;
In GRASS version 7 the gpde and the gmath libraries are providing functions which are partly parallelized with OpenMP. All blas level 2 and 3 functions as well as many linear equation solver in the gmath library, are parallelized using OpenMP pragmas. Several numerical modules, which are using those functions, can now benefit from multi core systems (i.e.: r.gwflow, r3.gwflow, r.solute.transport).&lt;br /&gt;
&lt;br /&gt;
OpenMP flags are compiler dependent, thus OpenMP support should be set using C- and linker-flags before calling configure. &lt;br /&gt;
I.e: for gcc &amp;gt; 4.2: &lt;br /&gt;
&lt;br /&gt;
 CFLAGS=&amp;quot;-O3 -Wall -Werror-implicit-function-declaration -fno-common -fopenmp&amp;quot;&lt;br /&gt;
 LDFLAGS=&amp;quot;-lgomp&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This should enable OpenMP support in the libraries and ALL depending modules. &lt;br /&gt;
&lt;br /&gt;
You can test the OpenMP support when compiling the gpde and gmath tests by hand (switch into the test directories in the lib dirs and type make). The test library modules &amp;quot;test.gmath.lib&amp;quot; and &amp;quot;test.gpde.lib&amp;quot; should be available in the path after starting grass. &lt;br /&gt;
&lt;br /&gt;
The gmath lib test module &amp;quot;test.gmath.lib&amp;quot; provides additionally benchmarks for blas level 2 and 3 functions and for many solver. &lt;br /&gt;
gmath/test&amp;gt; test.gmath.lib help&lt;br /&gt;
&lt;br /&gt;
 Description:&lt;br /&gt;
  Performs benchmarks, unit and integration tests for the gmath library&lt;br /&gt;
 &lt;br /&gt;
 Usage:&lt;br /&gt;
  test.gmath.lib [-uia] [unit=string] [integration=string] [rows=value]&lt;br /&gt;
    [solverbench=string] [blasbench=string] [--verbose] [--quiet]&lt;br /&gt;
 &lt;br /&gt;
 Flags:&lt;br /&gt;
   -u   Run all unit tests&lt;br /&gt;
   -i   Run all integration tests&lt;br /&gt;
   -a   Run all unit and integration tests&lt;br /&gt;
  --v   Verbose module output&lt;br /&gt;
  --q   Quiet module output&lt;br /&gt;
 &lt;br /&gt;
 Parameters:&lt;br /&gt;
          unit   Choose the unit tests to run&lt;br /&gt;
                 options: blas1,blas2,blas3,solver,ccmath,matconv&lt;br /&gt;
   integration   Choose the integration tests to run&lt;br /&gt;
                 options:&lt;br /&gt;
          rows   The size of the matrices and vectors for benchmarking&lt;br /&gt;
                 default: 1000&lt;br /&gt;
   solverbench   Choose solver benchmark&lt;br /&gt;
                 options: krylov,direct&lt;br /&gt;
     blasbench   Choose blas benchmark&lt;br /&gt;
                 options: blas2,blas3&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
I.e testing the speedup of the blas level 2 and 3 functions of the latest svn trunk of grass7, compiled with the flags mentioned above on a 8 core intel xeon system: &lt;br /&gt;
&lt;br /&gt;
 gmath/test&amp;gt; setenv OMP_NUM_THREADS 1&lt;br /&gt;
 gmath/test&amp;gt; test.gmath.lib blasbench=blas2 rows=5000&lt;br /&gt;
 &lt;br /&gt;
 ++ Running blas level 2 benchmark ++&lt;br /&gt;
 Computation time G_math_Ax_sparse: 0.244123&lt;br /&gt;
 Computation time G_math_Ax_sband: 0.280636&lt;br /&gt;
 Computation time G_math_d_Ax: 0.134494&lt;br /&gt;
 Computation time G_math_d_Ax_by: 0.18556&lt;br /&gt;
 Computation time G_math_d_x_dyad: 0.268684&lt;br /&gt;
 &lt;br /&gt;
 -- gmath lib tests finished successfully --&lt;br /&gt;
 &lt;br /&gt;
 gmath/test&amp;gt; setenv OMP_NUM_THREADS 4&lt;br /&gt;
 gmath/test&amp;gt; test.gmath.lib blasbench=blas2 rows=5000 &lt;br /&gt;
 &lt;br /&gt;
 ++ Running blas level 2 benchmark ++&lt;br /&gt;
 Computation time G_math_Ax_sparse: 0.072549&lt;br /&gt;
 Computation time G_math_Ax_sband: 0.192712&lt;br /&gt;
 Computation time G_math_d_Ax: 0.036652&lt;br /&gt;
 Computation time G_math_d_Ax_by: 0.047904&lt;br /&gt;
 Computation time G_math_d_x_dyad: 0.080534&lt;br /&gt;
 &lt;br /&gt;
 -- gmath lib tests finished successfully --&lt;br /&gt;
 &lt;br /&gt;
 gmath/test&amp;gt; setenv OMP_NUM_THREADS 1&lt;br /&gt;
 gmath/test&amp;gt; test.gmath.lib blasbench=blas3 rows=1000&lt;br /&gt;
 &lt;br /&gt;
 ++ Running blas level 3 benchmark ++&lt;br /&gt;
 Computation time G_math_d_aA_B: 0.013263&lt;br /&gt;
 Computation time G_math_d_AB: 18.729&lt;br /&gt;
 &lt;br /&gt;
 -- gmath lib tests finished successfully --&lt;br /&gt;
 &lt;br /&gt;
 gmath/test&amp;gt; setenv OMP_NUM_THREADS 4&lt;br /&gt;
 gmath/test&amp;gt; test.gmath.lib blasbench=blas3 rows=1000&lt;br /&gt;
 &lt;br /&gt;
 ++ Running blas level 3 benchmark ++&lt;br /&gt;
 Computation time G_math_d_aA_B: 0.006946&lt;br /&gt;
 Computation time G_math_d_AB: 4.80446&lt;br /&gt;
 &lt;br /&gt;
 -- gmath lib tests finished successfully --&lt;br /&gt;
&lt;br /&gt;
== General code structure ==&lt;br /&gt;
&lt;br /&gt;
Example cited from &amp;quot;openMP tutorial&amp;quot; (see below):&lt;br /&gt;
&amp;lt;source lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
    #include &amp;lt;omp.h&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    int main ()  {&lt;br /&gt;
        int var1, var2, var3;&lt;br /&gt;
    &lt;br /&gt;
        Some serial code &lt;br /&gt;
        ...&lt;br /&gt;
   &lt;br /&gt;
        /* Beginning of parallel section. Fork a team of threads. */&lt;br /&gt;
        /* Specify variable scoping */&lt;br /&gt;
   &lt;br /&gt;
       #pragma omp parallel private(var1, var2) shared(var3)&lt;br /&gt;
       {&lt;br /&gt;
   &lt;br /&gt;
        /* Parallel section executed by all threads */&lt;br /&gt;
        ...&lt;br /&gt;
   &lt;br /&gt;
        /* All threads join master thread and disband */&lt;br /&gt;
       }  /* end pragma */&lt;br /&gt;
   &lt;br /&gt;
       /* Resume serial code */&lt;br /&gt;
       ...&lt;br /&gt;
   &lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And in the Makefile, add something like this:&lt;br /&gt;
&lt;br /&gt;
   #openMP support&lt;br /&gt;
   EXTRA_CFLAGS=-fopenmp&lt;br /&gt;
   EXTRA_LIBS=$(GISLIB) -lgomp $(MATHLIB)&lt;br /&gt;
&lt;br /&gt;
* Examples:&lt;br /&gt;
: https://computing.llnl.gov/tutorials/openMP/exercise.html&lt;br /&gt;
&lt;br /&gt;
== Run time ==&lt;br /&gt;
&lt;br /&gt;
The default is to create as many threads as the system has processors. If you don't want that, you can control the number with the OMP_NUM_THREADS environment variable. For example to request 3 threads from a Bourne shell:&lt;br /&gt;
&lt;br /&gt;
 OMP_NUM_THREADS=3&lt;br /&gt;
 export OMP_NUM_THREADS&lt;br /&gt;
 &lt;br /&gt;
 g.module ...&lt;br /&gt;
&lt;br /&gt;
== Candidates ==&lt;br /&gt;
&lt;br /&gt;
It is important to understand which modules are '''processor bound''', and concentrate on them. i.e. do not needlessly complicate the code of non-long running processor bound or I/O-bound modules. Almost all of the GIS libraries are not thread-safe. Regardless, these are typically I/O bound not processor bound, so not critical to parallelize. It is expected that most of the CPU-bound loops which will benefit from parallelization will be found in the modules.&lt;br /&gt;
&lt;br /&gt;
A good place to start is by running a [[Bugs#Using_a_profiling_tool|profiling tool]] to find the worst offending functions and deal with them first. Blindly parallelizing every loop you can find has the potential to slow things down due to the overheads needed to create and destroy threads.&lt;br /&gt;
&lt;br /&gt;
* v.lidar: parallelize tcholDec() in {{src|vector/lidar/lidarlib/TcholBand.c}}&lt;br /&gt;
: This would speed up the CPU-bound {{cmd|v.surf.bspline}} and {{cmd|v.lidar.edgedetection}} considerably.&lt;br /&gt;
&lt;br /&gt;
* (suggestion) RST library and {{cmd|v.surf.rst}}&lt;br /&gt;
: Please contact and coordinate with Helena Mitasova before starting work on this.&lt;br /&gt;
&lt;br /&gt;
* (suggestion) {{cmd|r.watershed}}&lt;br /&gt;
: Please contact and coordinate with Markus Metz before starting work on this.&lt;br /&gt;
&lt;br /&gt;
* (suggestion) {{AddonCmd|r.viewshed}}  (C++)&lt;br /&gt;
: Please contact and coordinate with Laura Toma before starting work on this.&lt;br /&gt;
: Should fix bug described in {{trac|390}} first and once that is done move module into the main repo.&lt;br /&gt;
&lt;br /&gt;
* (suggestion) {{cmd|r.sun}}&lt;br /&gt;
: Please contact and coordinate with Markus Neteler / Jaro Hofierka before starting work on this.&lt;br /&gt;
&lt;br /&gt;
== Complete ==&lt;br /&gt;
&lt;br /&gt;
* The GPDE library ({{src|lib/gpde/}}) has OpenMP support (disabled by default)&lt;br /&gt;
&lt;br /&gt;
* The gmath library ({{src|lib/gmath/}}) has OpenMP support for grass blas level 1, 2 and 3 algorithms as well as several iterative and direct linear equation solver (disabled by default)&lt;br /&gt;
&lt;br /&gt;
* GRASS 7 has a ./configure switch for `&amp;lt;tt&amp;gt;--with-pthread&amp;lt;/tt&amp;gt;`&lt;br /&gt;
&lt;br /&gt;
* Experimental {{wikipedia|Pthread}} support for [http://thread.gmane.org/gmane.comp.gis.grass.devel/30313 r.mapcalc is now in GRASS 7svn]&lt;br /&gt;
&lt;br /&gt;
* Yann has added OpenMP support to {{cmd|i.atcorr}}. (not in SVN)&lt;br /&gt;
&lt;br /&gt;
== Alternatives ==&lt;br /&gt;
&lt;br /&gt;
* {{wikipedia|pthreads}}&lt;br /&gt;
&lt;br /&gt;
* GNU Parallel http://www.gnu.org/software/parallel/ makes it easy to write parallel shell scripts.  WARNING: not all GRASS modules and scripts are safe to have other things happening in the same mapset while they are running. Try at your own risk after performing a suitable safety audit. e.g. Make sure g.region is not run, externally changing the region settings.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
 ### r.sun mode 1 loop ###&lt;br /&gt;
 SUNRISE=7.67&lt;br /&gt;
 SUNSET=16.33&lt;br /&gt;
 STEP=0.01&lt;br /&gt;
 # | wc -l   867&lt;br /&gt;
 &lt;br /&gt;
 DAY=355&lt;br /&gt;
&lt;br /&gt;
 seq $SUNRISE $STEP $SUNSET | parallel -j+0 r.sun -s elevin=gauss day=$DAY time={} beam_rad=rad1_test.${DAY}_{}_beam --quiet&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
GNU Parallel can also distribute work to other computers, see the video on how http://www.youtube.com/watch?v=LlXDtd_pRaY&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Poor-man's multithreading using Bourne shell script &amp;amp; backgrounding. WARNING: not all GRASS modules and scripts are safe to have other things happening in the same mapset while they are running. Try at your own risk after performing a suitable safety audit. e.g. Make sure g.region is not run, externally changing the region settings.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
 ### r.sun mode 1 loop ###&lt;br /&gt;
 SUNRISE=7.67&lt;br /&gt;
 SUNSET=16.33&lt;br /&gt;
 STEP=0.01&lt;br /&gt;
 # | wc -l   867&lt;br /&gt;
 CORES=4&lt;br /&gt;
 &lt;br /&gt;
 DAY=355&lt;br /&gt;
 for TIME in `seq $SUNRISE $STEP $SUNSET` ; do&lt;br /&gt;
    echo &amp;quot;time=$TIME&amp;quot;&lt;br /&gt;
    CMD=&amp;quot;r.sun -s elevin=gauss day=$DAY time=$TIME \&lt;br /&gt;
          beam_rad=rad1_test.${DAY}_${TIME}_beam --quiet&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
    # poor man's multi-threading for a multi-core CPU&lt;br /&gt;
    MODULUS=`echo &amp;quot;$TIME $STEP $CORES&amp;quot; | awk '{print $1 % ($2 * $3)}'`&lt;br /&gt;
    if [ &amp;quot;$MODULUS&amp;quot; = &amp;quot;$STEP&amp;quot; ] ; then&lt;br /&gt;
       # stall to let the background jobs finish&lt;br /&gt;
       $CMD&lt;br /&gt;
       sleep 2&lt;br /&gt;
       #while [ `pgrep -c r.sun` -ne 0 ] ; do&lt;br /&gt;
       #   sleep 5&lt;br /&gt;
       #done&lt;br /&gt;
    else&lt;br /&gt;
      $CMD &amp;amp;&lt;br /&gt;
    fi&lt;br /&gt;
 done&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Another example using r.sun Mode 2 can be found on the [[r.sun]] wiki page.&lt;br /&gt;
&lt;br /&gt;
* [[GPU]]&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
* [https://docs.loni.org/wiki/Introduction_to_OpenMP Introduction to OpenMP]&lt;br /&gt;
* [https://computing.llnl.gov/tutorials/openMP OpenMP tutorial]&lt;br /&gt;
* [http://software.intel.com/en-us/articles/threading-models-for-high-performance-computing-pthreads-or-openmp/ Threading Models for High-Performance Computing: Pthreads or OpenMP?]&lt;br /&gt;
&lt;br /&gt;
* GRASS mailing list discussions:&lt;br /&gt;
** http://thread.gmane.org/gmane.comp.gis.grass.devel/16410/&lt;br /&gt;
** http://lists.osgeo.org/pipermail/grass-dev/2009-April/043375.html&lt;br /&gt;
&lt;br /&gt;
* [http://www.khronos.org/opencl/ OpenCL] ({{wikipedia|OpenCL}} Wikipedia entry)&lt;br /&gt;
&lt;br /&gt;
* idea: You ''might'' be able to run the mpd daemon and then launch jobs via &amp;lt;tt&amp;gt;[http://www.mcs.anl.gov/mpi/index.htm mpirun -np 4 &amp;lt;command&amp;gt;]&amp;lt;/tt&amp;gt; in order to make your quad-core into a mini self-contained Beowulf cluster.&lt;br /&gt;
&lt;br /&gt;
[[Category: Development]]&lt;br /&gt;
[[Category: Parallelization]]&lt;/div&gt;</summary>
		<author><name>⚠️OleTange</name></author>
	</entry>
</feed>