OSDN Git Service

2008-08-05 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / libgomp / libgomp.texi
1 \input texinfo @c -*-texinfo-*-
2
3 @c %**start of header
4 @setfilename libgomp.info
5 @settitle GNU libgomp
6 @c %**end of header
7
8
9 @copying
10 Copyright @copyright{} 2006, 2007, 2008 Free Software Foundation, Inc.
11
12 Permission is granted to copy, distribute and/or modify this document
13 under the terms of the GNU Free Documentation License, Version 1.2 or
14 any later version published by the Free Software Foundation; with the
15 Invariant Sections being ``Funding Free Software'', the Front-Cover
16 texts being (a) (see below), and with the Back-Cover Texts being (b)
17 (see below).  A copy of the license is included in the section entitled
18 ``GNU Free Documentation License''.
19
20 (a) The FSF's Front-Cover Text is:
21
22      A GNU Manual
23
24 (b) The FSF's Back-Cover Text is:
25
26      You have freedom to copy and modify this GNU Manual, like GNU
27      software.  Copies published by the Free Software Foundation raise
28      funds for GNU development.
29 @end copying
30
31 @ifinfo
32 @dircategory GNU Libraries
33 @direntry
34 * libgomp: (libgomp).                    GNU OpenMP runtime library
35 @end direntry
36
37 This manual documents the GNU implementation of the OpenMP API for 
38 multi-platform shared-memory parallel programming in C/C++ and Fortran.
39
40 Published by the Free Software Foundation
41 51 Franklin Street, Fifth Floor
42 Boston, MA 02110-1301 USA
43
44 @insertcopying
45 @end ifinfo
46
47
48 @setchapternewpage odd
49
50 @titlepage
51 @title The GNU OpenMP Implementation
52 @page
53 @vskip 0pt plus 1filll
54 @comment For the @value{version-GCC} Version*
55 @sp 1
56 Published by the Free Software Foundation @*
57 51 Franklin Street, Fifth Floor@*
58 Boston, MA 02110-1301, USA@*
59 @sp 1
60 @insertcopying
61 @end titlepage
62
63 @summarycontents
64 @contents
65 @page
66
67
68 @node Top
69 @top Introduction
70 @cindex Introduction
71
72 This manual documents the usage of libgomp, the GNU implementation of the 
73 @uref{http://www.openmp.org, OpenMP} Application Programming Interface (API)
74 for multi-platform shared-memory parallel programming in C/C++ and Fortran.
75
76
77
78 @comment
79 @comment  When you add a new menu item, please keep the right hand
80 @comment  aligned to the same column.  Do not use tabs.  This provides
81 @comment  better formatting.
82 @comment
83 @menu
84 * Enabling OpenMP::            How to enable OpenMP for your applications.
85 * Runtime Library Routines::   The OpenMP runtime application programming 
86                                interface.
87 * Environment Variables::      Influencing runtime behavior with environment 
88                                variables.
89 * The libgomp ABI::            Notes on the external ABI presented by libgomp.
90 * Reporting Bugs::             How to report bugs in GNU OpenMP.
91 * Copying::                    GNU general public license says
92                                how you can copy and share libgomp.
93 * GNU Free Documentation License::
94                                How you can copy and share this manual.
95 * Funding::                    How to help assure continued work for free 
96                                software.
97 * Index::                      Index of this documentation.
98 @end menu
99
100
101 @c ---------------------------------------------------------------------
102 @c Enabling OpenMP
103 @c ---------------------------------------------------------------------
104
105 @node Enabling OpenMP
106 @chapter Enabling OpenMP
107
108 To activate the OpenMP extensions for C/C++ and Fortran, the compile-time 
109 flag @command{-fopenmp} must be specified. This enables the OpenMP directive
110 @code{#pragma omp} in C/C++ and @code{!$omp} directives in free form, 
111 @code{c$omp}, @code{*$omp} and @code{!$omp} directives in fixed form, 
112 @code{!$} conditional compilation sentinels in free form and @code{c$},
113 @code{*$} and @code{!$} sentinels in fixed form, for Fortran. The flag also
114 arranges for automatic linking of the OpenMP runtime library 
115 (@ref{Runtime Library Routines}).
116
117 A complete description of all OpenMP directives accepted may be found in 
118 the @uref{http://www.openmp.org, OpenMP Application Program Interface} manual,
119 version 2.5.
120
121
122 @c ---------------------------------------------------------------------
123 @c Runtime Library Routines
124 @c ---------------------------------------------------------------------
125
126 @node Runtime Library Routines
127 @chapter Runtime Library Routines
128
129 The runtime routines described here are defined by section 3 of the OpenMP 
130 specifications in version 2.5.
131
132 Control threads, processors and the parallel environment.
133
134 @menu
135 * omp_get_dynamic::          Dynamic teams setting
136 * omp_get_max_threads::      Maximum number of threads
137 * omp_get_nested::           Nested parallel regions
138 * omp_get_num_procs::        Number of processors online
139 * omp_get_num_threads::      Size of the active team
140 * omp_get_thread_num::       Current thread ID
141 * omp_in_parallel::          Whether a parallel region is active
142 * omp_set_dynamic::          Enable/disable dynamic teams
143 * omp_set_nested::           Enable/disable nested parallel regions
144 * omp_set_num_threads::      Set upper team size limit
145 @end menu
146
147 Initialize, set, test, unset and destroy simple and nested locks.
148
149 @menu
150 * omp_init_lock::            Initialize simple lock
151 * omp_set_lock::             Wait for and set simple lock
152 * omp_test_lock::            Test and set simple lock if available
153 * omp_unset_lock::           Unset simple lock
154 * omp_destroy_lock::         Destroy simple lock
155 * omp_init_nest_lock::       Initialize nested lock
156 * omp_set_nest_lock::        Wait for and set simple lock
157 * omp_test_nest_lock::       Test and set nested lock if available
158 * omp_unset_nest_lock::      Unset nested lock
159 * omp_destroy_nest_lock::    Destroy nested lock
160 @end menu
161
162 Portable, thread-based, wall clock timer.
163
164 @menu
165 * omp_get_wtick::            Get timer precision.
166 * omp_get_wtime::            Elapsed wall clock time.
167 @end menu
168
169 @node omp_get_dynamic
170 @section @code{omp_get_dynamic} -- Dynamic teams setting
171 @table @asis
172 @item @emph{Description}:
173 This function returns @code{true} if enabled, @code{false} otherwise. 
174 Here, @code{true} and @code{false} represent their language-specific 
175 counterparts.
176
177 The dynamic team setting may be initialized at startup by the 
178 @code{OMP_DYNAMIC} environment variable or at runtime using 
179 @code{omp_set_dynamic}. If undefined, dynamic adjustment is 
180 disabled by default.
181
182 @item @emph{C/C++}:
183 @multitable @columnfractions .20 .80
184 @item @emph{Prototype}: @tab @code{int omp_get_dynamic();}
185 @end multitable
186
187 @item @emph{Fortran}:
188 @multitable @columnfractions .20 .80
189 @item @emph{Interface}: @tab @code{logical function omp_get_dynamic()}
190 @end multitable
191
192 @item @emph{See also}:
193 @ref{omp_set_dynamic}, @ref{OMP_DYNAMIC}
194
195 @item @emph{Reference}:
196 @uref{http://www.openmp.org/, OpenMP specifications v2.5}, section 3.2.8.
197 @end table
198
199
200
201 @node omp_get_max_threads
202 @section @code{omp_get_max_threads} -- Maximum number of threads
203 @table @asis
204 @item @emph{Description}:
205 Return the maximum number of threads used for parallel regions that do
206 not use the clause @code{num_threads}.
207
208 @item @emph{C/C++}:
209 @multitable @columnfractions .20 .80
210 @item @emph{Prototype}: @tab @code{int omp_get_max_threads();}
211 @end multitable
212
213 @item @emph{Fortran}:
214 @multitable @columnfractions .20 .80
215 @item @emph{Interface}: @tab @code{integer function omp_get_max_threads()}
216 @end multitable
217
218 @item @emph{See also}:
219 @ref{omp_set_num_threads}, @ref{omp_set_dynamic}
220
221 @item @emph{Reference}:
222 @uref{http://www.openmp.org/, OpenMP specifications v2.5}, section 3.2.3.
223 @end table
224
225
226
227 @node omp_get_nested
228 @section @code{omp_get_nested} -- Nested parallel regions
229 @table @asis
230 @item @emph{Description}:
231 This function returns @code{true} if nested parallel regions are
232 enabled, @code{false} otherwise. Here, @code{true} and @code{false} 
233 represent their language-specific counterparts.
234
235 Nested parallel regions may be initialized at startup by the 
236 @code{OMP_NESTED} environment variable or at runtime using 
237 @code{omp_set_nested}. If undefined, nested parallel regions are
238 disabled by default.
239
240 @item @emph{C/C++}:
241 @multitable @columnfractions .20 .80
242 @item @emph{Prototype}: @tab @code{int omp_get_nested();}
243 @end multitable
244
245 @item @emph{Fortran}:
246 @multitable @columnfractions .20 .80
247 @item @emph{Interface}: @tab @code{integer function omp_get_nested()}
248 @end multitable
249
250 @item @emph{See also}:
251 @ref{omp_set_nested}, @ref{OMP_NESTED}
252
253 @item @emph{Reference}:
254 @uref{http://www.openmp.org/, OpenMP specifications v2.5}, section 3.2.10.
255 @end table
256
257
258
259 @node omp_get_num_procs
260 @section @code{omp_get_num_procs} -- Number of processors online
261 @table @asis
262 @item @emph{Description}:
263 Returns the number of processors online.
264
265 @item @emph{C/C++}:
266 @multitable @columnfractions .20 .80
267 @item @emph{Prototype}: @tab @code{int omp_get_num_procs();}
268 @end multitable
269
270 @item @emph{Fortran}:
271 @multitable @columnfractions .20 .80
272 @item @emph{Interface}: @tab @code{integer function omp_get_num_procs()}
273 @end multitable
274
275 @item @emph{Reference}:
276 @uref{http://www.openmp.org/, OpenMP specifications v2.5}, section 3.2.5.
277 @end table
278
279
280
281 @node omp_get_num_threads
282 @section @code{omp_get_num_threads} -- Size of the active team
283 @table @asis
284 @item @emph{Description}:
285 The number of threads in the current team. In a sequential section of 
286 the program @code{omp_get_num_threads} returns 1.
287
288 The default team size may be initialized at startup by the 
289 @code{OMP_NUM_THREADS} environment variable. At runtime, the size 
290 of the current team may be set either by the @code{NUM_THREADS}
291 clause or by @code{omp_set_num_threads}. If none of the above were 
292 used to define a specific value and @code{OMP_DYNAMIC} is disabled,
293 one thread per CPU online is used.
294
295 @item @emph{C/C++}:
296 @multitable @columnfractions .20 .80
297 @item @emph{Prototype}: @tab @code{int omp_get_num_threads();}
298 @end multitable
299
300 @item @emph{Fortran}:
301 @multitable @columnfractions .20 .80
302 @item @emph{Interface}: @tab @code{integer function omp_get_num_threads()}
303 @end multitable
304
305 @item @emph{See also}:
306 @ref{omp_get_max_threads}, @ref{omp_set_num_threads}, @ref{OMP_NUM_THREADS}
307
308 @item @emph{Reference}:
309 @uref{http://www.openmp.org/, OpenMP specifications v2.5}, section 3.2.2.
310 @end table
311
312
313
314 @node omp_get_thread_num 
315 @section @code{omp_get_thread_num} -- Current thread ID
316 @table @asis
317 @item @emph{Description}:
318 Unique thread identification number. In a sequential parts of the program, 
319 @code{omp_get_thread_num} always returns 0. In parallel regions the return
320 value varies from 0 to @code{omp_get_max_threads}-1 inclusive. The return 
321 value of the master thread of a team is always 0.
322
323 @item @emph{C/C++}:
324 @multitable @columnfractions .20 .80
325 @item @emph{Prototype}: @tab @code{int omp_get_thread_num();}
326 @end multitable
327
328 @item @emph{Fortran}:
329 @multitable @columnfractions .20 .80
330 @item @emph{Interface}: @tab @code{integer function omp_get_thread_num()}
331 @end multitable
332
333 @item @emph{See also}:
334 @ref{omp_get_max_threads}
335
336 @item @emph{Reference}:
337 @uref{http://www.openmp.org/, OpenMP specifications v2.5}, section 3.2.4.
338 @end table
339
340
341
342 @node omp_in_parallel
343 @section @code{omp_in_parallel} -- Whether a parallel region is active
344 @table @asis
345 @item @emph{Description}:
346 This function returns @code{true} if currently running in parallel, 
347 @code{false} otherwise. Here, @code{true} and @code{false} represent 
348 their language-specific counterparts.
349
350 @item @emph{C/C++}:
351 @multitable @columnfractions .20 .80
352 @item @emph{Prototype}: @tab @code{int omp_in_parallel();}
353 @end multitable
354
355 @item @emph{Fortran}:
356 @multitable @columnfractions .20 .80
357 @item @emph{Interface}: @tab @code{logical function omp_in_parallel()}
358 @end multitable
359
360 @item @emph{Reference}:
361 @uref{http://www.openmp.org/, OpenMP specifications v2.5}, section 3.2.6.
362 @end table
363
364
365 @node omp_set_dynamic
366 @section @code{omp_set_dynamic} -- Enable/disable dynamic teams
367 @table @asis
368 @item @emph{Description}:
369 Enable or disable the dynamic adjustment of the number of threads 
370 within a team. The function takes the language-specific equivalent
371 of @code{true} and @code{false}, where @code{true} enables dynamic 
372 adjustment of team sizes and @code{false} disables it.
373
374 @item @emph{C/C++}:
375 @multitable @columnfractions .20 .80
376 @item @emph{Prototype}: @tab @code{void omp_set_dynamic(int);}
377 @end multitable
378
379 @item @emph{Fortran}:
380 @multitable @columnfractions .20 .80
381 @item @emph{Interface}: @tab @code{subroutine omp_set_dynamic(set)}
382 @item                   @tab @code{integer, intent(in) :: set}
383 @end multitable
384
385 @item @emph{See also}:
386 @ref{OMP_DYNAMIC}, @ref{omp_get_dynamic}
387
388 @item @emph{Reference}:
389 @uref{http://www.openmp.org/, OpenMP specifications v2.5}, section 3.2.7.
390 @end table
391
392
393
394 @node omp_set_nested
395 @section @code{omp_set_nested} -- Enable/disable nested parallel regions
396 @table @asis
397 @item @emph{Description}:
398 Enable or disable nested parallel regions, i.e., whether team members
399 are allowed to create new teams. The function takes the language-specific 
400 equivalent of @code{true} and @code{false}, where @code{true} enables 
401 dynamic adjustment of team sizes and @code{false} disables it.
402
403 @item @emph{C/C++}:
404 @multitable @columnfractions .20 .80
405 @item @emph{Prototype}: @tab @code{void omp_set_dynamic(int);}
406 @end multitable
407
408 @item @emph{Fortran}:
409 @multitable @columnfractions .20 .80
410 @item @emph{Interface}: @tab @code{subroutine omp_set_dynamic(set)}
411 @item                   @tab @code{integer, intent(in) :: set}
412 @end multitable
413
414 @item @emph{See also}:
415 @ref{OMP_NESTED}, @ref{omp_get_nested}
416
417 @item @emph{Reference}:
418 @uref{http://www.openmp.org/, OpenMP specifications v2.5}, section 3.2.9.
419 @end table
420
421
422
423 @node omp_set_num_threads
424 @section @code{omp_set_num_threads} -- Set upper team size limit
425 @table @asis
426 @item @emph{Description}:
427 Specifies the number of threads used by default in subsequent parallel 
428 sections, if those do not specify a @code{num_threads} clause. The 
429 argument of @code{omp_set_num_threads} shall be a positive integer. 
430
431 @item @emph{C/C++}:
432 @multitable @columnfractions .20 .80
433 @item @emph{Prototype}: @tab @code{void omp_set_num_threads(int);}
434 @end multitable
435
436 @item @emph{Fortran}:
437 @multitable @columnfractions .20 .80
438 @item @emph{Interface}: @tab @code{subroutine omp_set_num_threads(set)}
439 @item                   @tab @code{integer, intent(in) :: set}
440 @end multitable
441
442 @item @emph{See also}:
443 @ref{OMP_NUM_THREADS}, @ref{omp_get_num_threads}, @ref{omp_get_max_threads}
444
445 @item @emph{Reference}:
446 @uref{http://www.openmp.org/, OpenMP specifications v2.5}, section 3.2.1.
447 @end table
448
449
450
451 @node omp_init_lock
452 @section @code{omp_init_lock} -- Initialize simple lock
453 @table @asis
454 @item @emph{Description}:
455 Initialize a simple lock. After initialization, the lock is in 
456 an unlocked state.
457
458 @item @emph{C/C++}:
459 @multitable @columnfractions .20 .80
460 @item @emph{Prototype}: @tab @code{void omp_init_lock(omp_lock_t *lock);}
461 @end multitable
462
463 @item @emph{Fortran}:
464 @multitable @columnfractions .20 .80
465 @item @emph{Interface}: @tab @code{subroutine omp_init_lock(lock)}
466 @item                   @tab @code{integer(omp_lock_kind), intent(out) :: lock}
467 @end multitable
468
469 @item @emph{See also}:
470 @ref{omp_destroy_lock}
471
472 @item @emph{Reference}: 
473 @uref{http://www.openmp.org/, OpenMP specifications v2.5}, section 3.3.1.
474 @end table
475
476
477
478 @node omp_set_lock
479 @section @code{omp_set_lock} -- Wait for and set simple lock
480 @table @asis
481 @item @emph{Description}:
482 Before setting a simple lock, the lock variable must be initialized by 
483 @code{omp_init_lock}. The calling thread is blocked until the lock 
484 is available. If the lock is already held by the current thread, 
485 a deadlock occurs.
486
487 @item @emph{C/C++}:
488 @multitable @columnfractions .20 .80
489 @item @emph{Prototype}: @tab @code{void omp_set_lock(omp_lock_t *lock);}
490 @end multitable
491
492 @item @emph{Fortran}:
493 @multitable @columnfractions .20 .80
494 @item @emph{Interface}: @tab @code{subroutine omp_set_lock(lock)}
495 @item                   @tab @code{integer(omp_lock_kind), intent(out) :: lock}
496 @end multitable
497
498 @item @emph{See also}:
499 @ref{omp_init_lock}, @ref{omp_test_lock}, @ref{omp_unset_lock}
500
501 @item @emph{Reference}: 
502 @uref{http://www.openmp.org/, OpenMP specifications v2.5}, section 3.3.3.
503 @end table
504
505
506
507 @node omp_test_lock
508 @section @code{omp_test_lock} -- Test and set simple lock if available
509 @table @asis
510 @item @emph{Description}:
511 Before setting a simple lock, the lock variable must be initialized by 
512 @code{omp_init_lock}. Contrary to @code{omp_set_lock}, @code{omp_test_lock} 
513 does not block if the lock is not available. This function returns 
514 @code{true} upon success, @code{false} otherwise. Here, @code{true} and 
515 @code{false} represent their language-specific counterparts.
516
517 @item @emph{C/C++}:
518 @multitable @columnfractions .20 .80
519 @item @emph{Prototype}: @tab @code{int omp_test_lock(omp_lock_t *lock);}
520 @end multitable
521
522 @item @emph{Fortran}:
523 @multitable @columnfractions .20 .80
524 @item @emph{Interface}: @tab @code{subroutine omp_test_lock(lock)}
525 @item                   @tab @code{logical(omp_logical_kind) :: omp_test_lock}
526 @item                   @tab @code{integer(omp_lock_kind), intent(out) :: lock}
527 @end multitable
528
529 @item @emph{See also}:
530 @ref{omp_init_lock}, @ref{omp_set_lock}, @ref{omp_set_lock}
531
532 @item @emph{Reference}: 
533 @uref{http://www.openmp.org/, OpenMP specifications v2.5}, section 3.3.5.
534 @end table
535
536
537
538 @node omp_unset_lock
539 @section @code{omp_unset_lock} -- Unset simple lock
540 @table @asis
541 @item @emph{Description}:
542 A simple lock about to be unset must have been locked by @code{omp_set_lock}
543 or @code{omp_test_lock} before. In addition, the lock must be held by the 
544 thread calling @code{omp_unset_lock}. Then, the lock becomes unlocked. If one 
545 ore more threads attempted to set the lock before, one of them is chosen to, 
546 again, set the lock for itself.
547
548 @item @emph{C/C++}:
549 @multitable @columnfractions .20 .80
550 @item @emph{Prototype}: @tab @code{void omp_unset_lock(omp_lock_t *lock);}
551 @end multitable
552
553 @item @emph{Fortran}:
554 @multitable @columnfractions .20 .80
555 @item @emph{Interface}: @tab @code{subroutine omp_unset_lock(lock)}
556 @item                   @tab @code{integer(omp_lock_kind), intent(out) :: lock}
557 @end multitable
558
559 @item @emph{See also}:
560 @ref{omp_set_lock}, @ref{omp_test_lock}
561
562 @item @emph{Reference}: 
563 @uref{http://www.openmp.org/, OpenMP specifications v2.5}, section 3.3.4.
564 @end table
565
566
567
568 @node omp_destroy_lock
569 @section @code{omp_destroy_lock} -- Destroy simple lock
570 @table @asis
571 @item @emph{Description}:
572 Destroy a simple lock. In order to be destroyed, a simple lock must be 
573 in the unlocked state. 
574
575 @item @emph{C/C++}:
576 @multitable @columnfractions .20 .80
577 @item @emph{Prototype}: @tab @code{void omp_destroy_lock(omp_lock_t *);}
578 @end multitable
579
580 @item @emph{Fortran}:
581 @multitable @columnfractions .20 .80
582 @item @emph{Interface}: @tab @code{subroutine omp_destroy_lock(lock)}
583 @item                   @tab @code{integer(omp_lock_kind), intent(inout) :: lock}
584 @end multitable
585
586 @item @emph{See also}:
587 @ref{omp_init_lock}
588
589 @item @emph{Reference}: 
590 @uref{http://www.openmp.org/, OpenMP specifications v2.5}, section 3.3.2.
591 @end table
592
593
594
595 @node omp_init_nest_lock
596 @section @code{omp_init_nest_lock} -- Initialize nested lock
597 @table @asis
598 @item @emph{Description}:
599 Initialize a nested lock. After initialization, the lock is in 
600 an unlocked state and the nesting count is set to zero.
601
602 @item @emph{C/C++}:
603 @multitable @columnfractions .20 .80
604 @item @emph{Prototype}: @tab @code{void omp_init_nest_lock(omp_nest_lock_t *lock);}
605 @end multitable
606
607 @item @emph{Fortran}:
608 @multitable @columnfractions .20 .80
609 @item @emph{Interface}: @tab @code{subroutine omp_init_nest_lock(lock)}
610 @item                   @tab @code{integer(omp_nest_lock_kind), intent(out) :: lock}
611 @end multitable
612
613 @item @emph{See also}:
614 @ref{omp_destroy_nest_lock}
615
616 @item @emph{Reference}:
617 @uref{http://www.openmp.org/, OpenMP specifications v2.5}, section 3.3.1.
618 @end table
619
620
621 @node omp_set_nest_lock
622 @section @code{omp_set_nest_lock} -- Wait for and set simple lock
623 @table @asis
624 @item @emph{Description}:
625 Before setting a nested lock, the lock variable must be initialized by 
626 @code{omp_init_nest_lock}. The calling thread is blocked until the lock 
627 is available. If the lock is already held by the current thread, the 
628 nesting count for the lock in incremented.
629
630 @item @emph{C/C++}:
631 @multitable @columnfractions .20 .80
632 @item @emph{Prototype}: @tab @code{void omp_set_nest_lock(omp_nest_lock_t *lock);}
633 @end multitable
634
635 @item @emph{Fortran}:
636 @multitable @columnfractions .20 .80
637 @item @emph{Interface}: @tab @code{subroutine omp_set_nest_lock(lock)}
638 @item                   @tab @code{integer(omp_nest_lock_kind), intent(out) :: lock}
639 @end multitable
640
641 @item @emph{See also}:
642 @ref{omp_init_nest_lock}, @ref{omp_unset_nest_lock}
643
644 @item @emph{Reference}: 
645 @uref{http://www.openmp.org/, OpenMP specifications v2.5}, section 3.3.3.
646 @end table
647
648
649
650 @node omp_test_nest_lock
651 @section @code{omp_test_nest_lock} -- Test and set nested lock if available
652 @table @asis
653 @item @emph{Description}:
654 Before setting a nested lock, the lock variable must be initialized by 
655 @code{omp_init_nest_lock}. Contrary to @code{omp_set_nest_lock}, 
656 @code{omp_test_nest_lock} does not block if the lock is not available. 
657 If the lock is already held by the current thread, the new nesting count 
658 is returned. Otherwise, the return value equals zero.
659
660 @item @emph{C/C++}:
661 @multitable @columnfractions .20 .80
662 @item @emph{Prototype}: @tab @code{int omp_test_nest_lock(omp_nest_lock_t *lock);}
663 @end multitable
664
665 @item @emph{Fortran}:
666 @multitable @columnfractions .20 .80
667 @item @emph{Interface}: @tab @code{integer function omp_test_nest_lock(lock)}
668 @item                   @tab @code{integer(omp_integer_kind) :: omp_test_nest_lock}
669 @item                   @tab @code{integer(omp_nest_lock_kind), intent(inout) :: lock}
670 @end multitable
671
672
673 @item @emph{See also}:
674 @ref{omp_init_lock}, @ref{omp_set_lock}, @ref{omp_set_lock}
675
676 @item @emph{Reference}: 
677 @uref{http://www.openmp.org/, OpenMP specifications v2.5}, section 3.3.5.
678 @end table
679
680
681
682 @node omp_unset_nest_lock
683 @section @code{omp_unset_nest_lock} -- Unset nested lock
684 @table @asis
685 @item @emph{Description}:
686 A nested lock about to be unset must have been locked by @code{omp_set_nested_lock}
687 or @code{omp_test_nested_lock} before. In addition, the lock must be held by the 
688 thread calling @code{omp_unset_nested_lock}. If the nesting count drops to zero, the 
689 lock becomes unlocked. If one ore more threads attempted to set the lock before, 
690 one of them is chosen to, again, set the lock for itself.
691
692 @item @emph{C/C++}:
693 @multitable @columnfractions .20 .80
694 @item @emph{Prototype}: @tab @code{void omp_unset_nest_lock(omp_nest_lock_t *lock);}
695 @end multitable
696
697 @item @emph{Fortran}:
698 @multitable @columnfractions .20 .80
699 @item @emph{Interface}: @tab @code{subroutine omp_unset_nest_lock(lock)}
700 @item                   @tab @code{integer(omp_nest_lock_kind), intent(out) :: lock}
701 @end multitable
702
703 @item @emph{See also}:
704 @ref{omp_set_nest_lock}
705
706 @item @emph{Reference}: 
707 @uref{http://www.openmp.org/, OpenMP specifications v2.5}, section 3.3.4.
708 @end table
709
710
711
712 @node omp_destroy_nest_lock
713 @section @code{omp_destroy_nest_lock} -- Destroy nested lock
714 @table @asis
715 @item @emph{Description}:
716 Destroy a nested lock. In order to be destroyed, a nested lock must be 
717 in the unlocked state and its nesting count must equal zero.
718
719 @item @emph{C/C++}:
720 @multitable @columnfractions .20 .80
721 @item @emph{Prototype}: @tab @code{void omp_destroy_nest_lock(omp_nest_lock_t *);}
722 @end multitable
723
724 @item @emph{Fortran}:
725 @multitable @columnfractions .20 .80
726 @item @emph{Interface}: @tab @code{subroutine omp_destroy_nest_lock(lock)}
727 @item                   @tab @code{integer(omp_nest_lock_kind), intent(inout) :: lock}
728 @end multitable
729
730 @item @emph{See also}:
731 @ref{omp_init_lock}
732
733 @item @emph{Reference}: 
734 @uref{http://www.openmp.org/, OpenMP specifications v2.5}, section 3.3.2.
735 @end table
736
737
738
739 @node omp_get_wtick
740 @section @code{omp_get_wtick} -- Get timer precision
741 @table @asis
742 @item @emph{Description}:
743 Gets the timer precision, i.e., the number of seconds between two 
744 successive clock ticks.
745
746 @item @emph{C/C++}:
747 @multitable @columnfractions .20 .80
748 @item @emph{Prototype}: @tab @code{double omp_get_wtick();}
749 @end multitable
750
751 @item @emph{Fortran}:
752 @multitable @columnfractions .20 .80
753 @item @emph{Interface}: @tab @code{double precision function omp_get_wtick()}
754 @end multitable
755
756 @item @emph{See also}:
757 @ref{omp_get_wtime}
758
759 @item @emph{Reference}: 
760 @uref{http://www.openmp.org/, OpenMP specifications v2.5}, section 3.4.2.
761 @end table
762
763
764
765 @node omp_get_wtime
766 @section @code{omp_get_wtime} -- Elapsed wall clock time
767 @table @asis
768 @item @emph{Description}:
769 Elapsed wall clock time in seconds. The time is measured per thread, no 
770 guarantee can bee made that two distinct threads measure the same time.
771 Time is measured from some "time in the past". On POSIX compliant systems 
772 the seconds since the Epoch (00:00:00 UTC, January 1, 1970) are returned.
773
774 @item @emph{C/C++}:
775 @multitable @columnfractions .20 .80
776 @item @emph{Prototype}: @tab @code{double omp_get_wtime();}
777 @end multitable
778
779 @item @emph{Fortran}:
780 @multitable @columnfractions .20 .80
781 @item @emph{Interface}: @tab @code{double precision function omp_get_wtime()}
782 @end multitable
783
784 @item @emph{See also}:
785 @ref{omp_get_wtick}
786
787 @item @emph{Reference}: 
788 @uref{http://www.openmp.org/, OpenMP specifications v2.5}, section 3.4.1.
789 @end table
790
791
792
793 @c ---------------------------------------------------------------------
794 @c Environment Variables
795 @c ---------------------------------------------------------------------
796
797 @node Environment Variables
798 @chapter Environment Variables
799
800 The variables @env{OMP_DYNAMIC}, @env{OMP_NESTED}, @env{OMP_NUM_THREADS} and 
801 @env{OMP_SCHEDULE} are defined by section 4 of the OpenMP specifications in 
802 version 2.5, while @env{GOMP_CPU_AFFINITY} and @env{GOMP_STACKSIZE} are GNU 
803 extensions.
804
805 @menu
806 * OMP_DYNAMIC::        Dynamic adjustment of threads
807 * OMP_NESTED::         Nested parallel regions
808 * OMP_NUM_THREADS::    Specifies the number of threads to use
809 * OMP_SCHEDULE::       How threads are scheduled
810 * GOMP_CPU_AFFINITY::  Bind threads to specific CPUs
811 * GOMP_STACKSIZE::     Set default thread stack size
812 @end menu
813
814
815 @node OMP_DYNAMIC
816 @section @env{OMP_DYNAMIC} -- Dynamic adjustment of threads
817 @cindex Environment Variable
818 @cindex Implementation specific setting
819 @table @asis
820 @item @emph{Description}:
821 Enable or disable the dynamic adjustment of the number of threads 
822 within a team. The value of this environment variable shall be 
823 @code{TRUE} or @code{FALSE}. If undefined, dynamic adjustment is
824 disabled by default.
825
826 @item @emph{See also}:
827 @ref{omp_set_dynamic}
828
829 @item @emph{Reference}: 
830 @uref{http://www.openmp.org/, OpenMP specifications v2.5}, section 4.3
831 @end table
832
833
834
835 @node OMP_NESTED
836 @section @env{OMP_NESTED} -- Nested parallel regions
837 @cindex Environment Variable
838 @cindex Implementation specific setting
839 @table @asis
840 @item @emph{Description}:
841 Enable or disable nested parallel regions, i.e., whether team members
842 are allowed to create new teams. The value of this environment variable 
843 shall be @code{TRUE} or @code{FALSE}. If undefined, nested parallel 
844 regions are disabled by default.
845
846 @item @emph{See also}:
847 @ref{omp_set_nested}
848
849 @item @emph{Reference}: 
850 @uref{http://www.openmp.org/, OpenMP specifications v2.5}, section 4.4
851 @end table
852
853
854
855 @node OMP_NUM_THREADS
856 @section @env{OMP_NUM_THREADS} -- Specifies the number of threads to use
857 @cindex Environment Variable
858 @cindex Implementation specific setting
859 @table @asis
860 @item @emph{Description}:
861 Specifies the default number of threads to use in parallel regions. The 
862 value of this variable shall be positive integer. If undefined one thread 
863 per CPU online is used.
864
865 @item @emph{See also}:
866 @ref{omp_set_num_threads}
867
868 @item @emph{Reference}: 
869 @uref{http://www.openmp.org/, OpenMP specifications v2.5}, section 4.2
870 @end table
871
872
873
874 @node OMP_SCHEDULE
875 @section @env{OMP_SCHEDULE} -- How threads are scheduled
876 @cindex Environment Variable
877 @cindex Implementation specific setting
878 @table @asis
879 @item @emph{Description}:
880 Allows to specify @code{schedule type} and @code{chunk size}. 
881 The value of the variable shall have the form: @code{type[,chunk]} where
882 @code{type} is one of @code{static}, @code{dynamic} or @code{guided}. 
883 The optional @code{chunk size} shall be a positive integer. If undefined,
884 dynamic scheduling and a chunk size of 1 is used.
885
886 @item @emph{Reference}: 
887 @uref{http://www.openmp.org/, OpenMP specifications v2.5}, sections 2.5.1 and 4.1
888 @end table
889
890
891
892 @node GOMP_CPU_AFFINITY
893 @section @env{GOMP_CPU_AFFINITY} -- Bind threads to specific CPUs
894 @cindex Environment Variable
895 @table @asis
896 @item @emph{Description}:
897 Binds threads to specific CPUs. The variable should contain a space- or
898 comma-separated list of CPUs. This list may contain different kind of 
899 entries: either single CPU numbers in any order, a range of CPUs (M-N) 
900 or a range with some stride (M-N:S). CPU numbers are zero based. For example,
901 @code{GOMP_CPU_AFFINITY="0 3 1-2 4-15:2"} will bind the initial thread
902 to CPU 0, the second to CPU 3, the third to CPU 1, the fourth to 
903 CPU 2, the fifth to CPU 4, the sixth through tenth to CPUs 6, 8, 10, 12,
904 and 14 respectively and then start assigning back from the beginning of
905 the list. @code{GOMP_CPU_AFFINITY=0} binds all threads to CPU 0.
906
907 There is no GNU OpenMP library routine to determine whether a CPU affinity 
908 specification is in effect. As a workaround, language-specific library 
909 functions, e.g., @code{getenv} in C or @code{GET_ENVIRONMENT_VARIABLE} in 
910 Fortran, may be used to query the setting of the @code{GOMP_CPU_AFFINITY} 
911 environment variable. A defined CPU affinity on startup cannot be changed 
912 or disabled during the runtime of the application.
913
914 If this environment variable is omitted, the host system will handle the 
915 assignment of threads to CPUs. 
916 @end table
917
918
919
920 @node GOMP_STACKSIZE
921 @section @env{GOMP_STACKSIZE} -- Set default thread stack size
922 @cindex Environment Variable
923 @cindex Implementation specific setting
924 @table @asis
925 @item @emph{Description}:
926 Set the default thread stack size in kilobytes. This is in opposition 
927 to @code{pthread_attr_setstacksize} which gets the number of bytes as an 
928 argument. If the stacksize can not be set due to system constraints, an 
929 error is reported and the initial stacksize is left unchanged. If undefined,
930 the stack size is system dependent.
931
932 @item @emph{Reference}: 
933 @uref{http://gcc.gnu.org/ml/gcc-patches/2006-06/msg00493.html, 
934 GCC Patches Mailinglist}, 
935 @uref{http://gcc.gnu.org/ml/gcc-patches/2006-06/msg00496.html,
936 GCC Patches Mailinglist}
937 @end table
938
939
940
941 @c ---------------------------------------------------------------------
942 @c The libgomp ABI
943 @c ---------------------------------------------------------------------
944
945 @node The libgomp ABI
946 @chapter The libgomp ABI
947
948 The following sections present notes on the external ABI as 
949 presented by libgomp. Only maintainers should need them.
950
951 @menu
952 * Implementing MASTER construct::
953 * Implementing CRITICAL construct::
954 * Implementing ATOMIC construct::
955 * Implementing FLUSH construct::
956 * Implementing BARRIER construct::
957 * Implementing THREADPRIVATE construct::
958 * Implementing PRIVATE clause::
959 * Implementing FIRSTPRIVATE LASTPRIVATE COPYIN and COPYPRIVATE clauses::
960 * Implementing REDUCTION clause::
961 * Implementing PARALLEL construct::
962 * Implementing FOR construct::
963 * Implementing ORDERED construct::
964 * Implementing SECTIONS construct::
965 * Implementing SINGLE construct::
966 @end menu
967
968
969 @node Implementing MASTER construct
970 @section Implementing MASTER construct
971
972 @smallexample
973 if (omp_get_thread_num () == 0)
974   block
975 @end smallexample
976
977 Alternately, we generate two copies of the parallel subfunction
978 and only include this in the version run by the master thread.
979 Surely that's not worthwhile though...
980
981
982
983 @node Implementing CRITICAL construct
984 @section Implementing CRITICAL construct
985
986 Without a specified name,
987
988 @smallexample
989   void GOMP_critical_start (void);
990   void GOMP_critical_end (void);
991 @end smallexample
992
993 so that we don't get COPY relocations from libgomp to the main
994 application.
995
996 With a specified name, use omp_set_lock and omp_unset_lock with
997 name being transformed into a variable declared like
998
999 @smallexample
1000   omp_lock_t gomp_critical_user_<name> __attribute__((common))
1001 @end smallexample
1002
1003 Ideally the ABI would specify that all zero is a valid unlocked
1004 state, and so we wouldn't actually need to initialize this at
1005 startup.
1006
1007
1008
1009 @node Implementing ATOMIC construct
1010 @section Implementing ATOMIC construct
1011
1012 The target should implement the @code{__sync} builtins.
1013
1014 Failing that we could add
1015
1016 @smallexample
1017   void GOMP_atomic_enter (void)
1018   void GOMP_atomic_exit (void)
1019 @end smallexample
1020
1021 which reuses the regular lock code, but with yet another lock
1022 object private to the library.
1023
1024
1025
1026 @node Implementing FLUSH construct
1027 @section Implementing FLUSH construct
1028
1029 Expands to the @code{__sync_synchronize} builtin.
1030
1031
1032
1033 @node Implementing BARRIER construct
1034 @section Implementing BARRIER construct
1035
1036 @smallexample
1037   void GOMP_barrier (void)
1038 @end smallexample
1039
1040
1041 @node Implementing THREADPRIVATE construct
1042 @section Implementing THREADPRIVATE construct
1043
1044 In _most_ cases we can map this directly to @code{__thread}.  Except
1045 that OMP allows constructors for C++ objects.  We can either
1046 refuse to support this (how often is it used?) or we can 
1047 implement something akin to .ctors.
1048
1049 Even more ideally, this ctor feature is handled by extensions
1050 to the main pthreads library.  Failing that, we can have a set
1051 of entry points to register ctor functions to be called.
1052
1053
1054
1055 @node Implementing PRIVATE clause
1056 @section Implementing PRIVATE clause
1057
1058 In association with a PARALLEL, or within the lexical extent
1059 of a PARALLEL block, the variable becomes a local variable in
1060 the parallel subfunction.
1061
1062 In association with FOR or SECTIONS blocks, create a new
1063 automatic variable within the current function.  This preserves
1064 the semantic of new variable creation.
1065
1066
1067
1068 @node Implementing FIRSTPRIVATE LASTPRIVATE COPYIN and COPYPRIVATE clauses
1069 @section Implementing FIRSTPRIVATE LASTPRIVATE COPYIN and COPYPRIVATE clauses
1070
1071 Seems simple enough for PARALLEL blocks.  Create a private 
1072 struct for communicating between parent and subfunction.
1073 In the parent, copy in values for scalar and "small" structs;
1074 copy in addresses for others TREE_ADDRESSABLE types.  In the 
1075 subfunction, copy the value into the local variable.
1076
1077 Not clear at all what to do with bare FOR or SECTION blocks.
1078 The only thing I can figure is that we do something like
1079
1080 @smallexample
1081 #pragma omp for firstprivate(x) lastprivate(y)
1082 for (int i = 0; i < n; ++i)
1083   body;
1084 @end smallexample
1085
1086 which becomes
1087
1088 @smallexample
1089 @{
1090   int x = x, y;
1091
1092   // for stuff
1093
1094   if (i == n)
1095     y = y;
1096 @}
1097 @end smallexample
1098
1099 where the "x=x" and "y=y" assignments actually have different
1100 uids for the two variables, i.e. not something you could write
1101 directly in C.  Presumably this only makes sense if the "outer"
1102 x and y are global variables.
1103
1104 COPYPRIVATE would work the same way, except the structure 
1105 broadcast would have to happen via SINGLE machinery instead.
1106
1107
1108
1109 @node Implementing REDUCTION clause
1110 @section Implementing REDUCTION clause
1111
1112 The private struct mentioned in the previous section should have 
1113 a pointer to an array of the type of the variable, indexed by the 
1114 thread's @var{team_id}.  The thread stores its final value into the 
1115 array, and after the barrier the master thread iterates over the
1116 array to collect the values.
1117
1118
1119 @node Implementing PARALLEL construct
1120 @section Implementing PARALLEL construct
1121
1122 @smallexample
1123   #pragma omp parallel
1124   @{
1125     body;
1126   @}
1127 @end smallexample
1128
1129 becomes
1130
1131 @smallexample
1132   void subfunction (void *data)
1133   @{
1134     use data;
1135     body;
1136   @}
1137
1138   setup data;
1139   GOMP_parallel_start (subfunction, &data, num_threads);
1140   subfunction (&data);
1141   GOMP_parallel_end ();
1142 @end smallexample
1143
1144 @smallexample
1145   void GOMP_parallel_start (void (*fn)(void *), void *data, unsigned num_threads)
1146 @end smallexample
1147
1148 The @var{FN} argument is the subfunction to be run in parallel.
1149
1150 The @var{DATA} argument is a pointer to a structure used to 
1151 communicate data in and out of the subfunction, as discussed
1152 above with respect to FIRSTPRIVATE et al.
1153
1154 The @var{NUM_THREADS} argument is 1 if an IF clause is present
1155 and false, or the value of the NUM_THREADS clause, if
1156 present, or 0.
1157
1158 The function needs to create the appropriate number of
1159 threads and/or launch them from the dock.  It needs to
1160 create the team structure and assign team ids.
1161
1162 @smallexample
1163   void GOMP_parallel_end (void)
1164 @end smallexample
1165
1166 Tears down the team and returns us to the previous @code{omp_in_parallel()} state.
1167
1168
1169
1170 @node Implementing FOR construct
1171 @section Implementing FOR construct
1172
1173 @smallexample
1174   #pragma omp parallel for
1175   for (i = lb; i <= ub; i++)
1176     body;
1177 @end smallexample
1178
1179 becomes
1180
1181 @smallexample
1182   void subfunction (void *data)
1183   @{
1184     long _s0, _e0;
1185     while (GOMP_loop_static_next (&_s0, &_e0))
1186     @{
1187       long _e1 = _e0, i;
1188       for (i = _s0; i < _e1; i++)
1189         body;
1190     @}
1191     GOMP_loop_end_nowait ();
1192   @}
1193
1194   GOMP_parallel_loop_static (subfunction, NULL, 0, lb, ub+1, 1, 0);
1195   subfunction (NULL);
1196   GOMP_parallel_end ();
1197 @end smallexample
1198
1199 @smallexample
1200   #pragma omp for schedule(runtime)
1201   for (i = 0; i < n; i++)
1202     body;
1203 @end smallexample
1204
1205 becomes
1206
1207 @smallexample
1208   @{
1209     long i, _s0, _e0;
1210     if (GOMP_loop_runtime_start (0, n, 1, &_s0, &_e0))
1211       do @{
1212         long _e1 = _e0;
1213         for (i = _s0, i < _e0; i++)
1214           body;
1215       @} while (GOMP_loop_runtime_next (&_s0, _&e0));
1216     GOMP_loop_end ();
1217   @}
1218 @end smallexample
1219
1220 Note that while it looks like there is trickyness to propagating
1221 a non-constant STEP, there isn't really.  We're explicitly allowed
1222 to evaluate it as many times as we want, and any variables involved
1223 should automatically be handled as PRIVATE or SHARED like any other
1224 variables.  So the expression should remain evaluable in the 
1225 subfunction.  We can also pull it into a local variable if we like,
1226 but since its supposed to remain unchanged, we can also not if we like.
1227
1228 If we have SCHEDULE(STATIC), and no ORDERED, then we ought to be
1229 able to get away with no work-sharing context at all, since we can
1230 simply perform the arithmetic directly in each thread to divide up
1231 the iterations.  Which would mean that we wouldn't need to call any
1232 of these routines.
1233
1234 There are separate routines for handling loops with an ORDERED
1235 clause.  Bookkeeping for that is non-trivial...
1236
1237
1238
1239 @node Implementing ORDERED construct
1240 @section Implementing ORDERED construct
1241
1242 @smallexample
1243   void GOMP_ordered_start (void)
1244   void GOMP_ordered_end (void)
1245 @end smallexample
1246
1247
1248
1249 @node Implementing SECTIONS construct
1250 @section Implementing SECTIONS construct
1251
1252 A block as 
1253
1254 @smallexample
1255   #pragma omp sections
1256   @{
1257     #pragma omp section
1258     stmt1;
1259     #pragma omp section
1260     stmt2;
1261     #pragma omp section
1262     stmt3;
1263   @}
1264 @end smallexample
1265
1266 becomes
1267
1268 @smallexample
1269   for (i = GOMP_sections_start (3); i != 0; i = GOMP_sections_next ())
1270     switch (i)
1271       @{
1272       case 1:
1273         stmt1;
1274         break;
1275       case 2:
1276         stmt2;
1277         break;
1278       case 3:
1279         stmt3;
1280         break;
1281       @}
1282   GOMP_barrier ();
1283 @end smallexample
1284
1285
1286 @node Implementing SINGLE construct
1287 @section Implementing SINGLE construct
1288
1289 A block like 
1290
1291 @smallexample
1292   #pragma omp single
1293   @{
1294     body;
1295   @}
1296 @end smallexample
1297
1298 becomes
1299
1300 @smallexample
1301   if (GOMP_single_start ())
1302     body;
1303   GOMP_barrier ();
1304 @end smallexample
1305
1306 while 
1307
1308 @smallexample
1309   #pragma omp single copyprivate(x)
1310     body;
1311 @end smallexample
1312
1313 becomes
1314
1315 @smallexample
1316   datap = GOMP_single_copy_start ();
1317   if (datap == NULL)
1318     @{
1319       body;
1320       data.x = x;
1321       GOMP_single_copy_end (&data);
1322     @}
1323   else
1324     x = datap->x;
1325   GOMP_barrier ();
1326 @end smallexample
1327
1328
1329
1330 @c ---------------------------------------------------------------------
1331 @c 
1332 @c ---------------------------------------------------------------------
1333
1334 @node Reporting Bugs
1335 @chapter Reporting Bugs
1336
1337 Bugs in the GNU OpenMP implementation should be reported via 
1338 @uref{http://gcc.gnu.org/bugzilla/, bugzilla}. In all cases, please add 
1339 "openmp" to the keywords field in the bug report.
1340
1341
1342
1343 @c ---------------------------------------------------------------------
1344 @c GNU General Public License
1345 @c ---------------------------------------------------------------------
1346
1347 @include gpl.texi
1348
1349
1350
1351 @c ---------------------------------------------------------------------
1352 @c GNU Free Documentation License
1353 @c ---------------------------------------------------------------------
1354
1355 @include fdl.texi
1356
1357
1358
1359 @c ---------------------------------------------------------------------
1360 @c Funding Free Software
1361 @c ---------------------------------------------------------------------
1362
1363 @include funding.texi
1364
1365 @c ---------------------------------------------------------------------
1366 @c Index
1367 @c ---------------------------------------------------------------------
1368
1369 @node Index
1370 @unnumbered Index
1371
1372 @printindex cp
1373
1374 @bye