File: team_thread.hpp

package info (click to toggle)
cppad 2025.00.00.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,552 kB
  • sloc: cpp: 112,594; sh: 5,972; ansic: 179; python: 71; sed: 12; makefile: 10
file content (136 lines) | stat: -rw-r--r-- 3,754 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# ifndef CPPAD_EXAMPLE_MULTI_THREAD_TEAM_THREAD_HPP
# define CPPAD_EXAMPLE_MULTI_THREAD_TEAM_THREAD_HPP
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-24 Bradley M. Bell
// ----------------------------------------------------------------------------
/*
{xrst_begin team_thread.hpp}
{xrst_spell
  bthread
  openmp
  posix
  pthread
  sthread
}
Specifications for A Team of AD Threads
#######################################

Syntax
******
| ``include`` ``"team_thread.hpp"``
| *ok* = ``team_create`` ( *num_threads* )
| *ok* = ``team_work`` ( *worker* )
| *ok* = ``team_destroy`` ()
| *name* = ``team_name`` ()

Prototype
*********
{xrst_literal
   // BEGIN PROTOTYPE
   // END PROTOTYPE
}

Purpose
*******
These routines start, use, and stop a team of threads that can
be used with the CppAD type ``AD<double>`` .
Examples are provided for
OpenMP threads, Posix threads, Boost threads and Standard threads.

Restrictions
************
Calls to the routines
``team_create`` ,
``team_work`` , and
``team_destroy`` , must all be done by the master thread; i.e.,
:ref:`thread_num<ta_thread_num-name>` must be zero.
In addition, they must all be done in sequential execution mode; i.e.,
when the master thread is the only thread that is running
(:ref:`in_parallel<ta_in_parallel-name>` must be false).

team_create
***********
The argument *num_threads*  > 0
specifies the number of threads in this team.
This initializes both ``AD<double>`` and ``team_work``
to be used with *num_threads* .
If *num_threads*  > 1 ,
*num_threads* ``- 1`` new threads are created
and put in a waiting state until ``team_work`` is called.

team_work
*********
This routine may be called one or more times
between the call to ``team_create`` and ``team_destroy`` .
Each call to ``team_work`` runs *num_threads* versions
of *worker* with the corresponding value of
:ref:`thread_num<ta_thread_num-name>`
between zero and *num_threads* ``- 1`` and
different for each thread,

team_destroy
************
This routine terminates all the other threads except for
thread number zero; i.e., it terminates the threads corresponding to

   *thread_num* = 1 , ... , *num_threads* ``-1``

team_name
*********
This routines returns a name that identifies this threading system.
The return value is a statically allocated ``'\0'`` terminated C string.

ok
**
The return value *ok*
is ``false`` if an error is detected during the
corresponding call.
Otherwise it is ``true`` .
{xrst_toc_hidden
   example/multi_thread/openmp/team_openmp.cpp
   example/multi_thread/bthread/team_bthread.cpp
   example/multi_thread/pthread/team_pthread.cpp
   example/multi_thread/sthread/team_sthread.cpp
}

Example Use
***********
Example use of these specifications can be found in the file
:ref:`team_example.cpp-name` .

Example Implementation
**********************
Example implementations of these specifications can be found in the files:

.. csv-table::
   :widths: auto

   team_openmp.cpp,:ref:`team_openmp.cpp-title`
   team_bthread.cpp,:ref:`team_bthread.cpp-title`
   team_pthread.cpp,:ref:`team_pthread.cpp-title`
   team_sthread.cpp,:ref:`team_sthread.cpp-title`

Speed Test of Implementation
****************************
Speed tests of using CppAD with the team implementations above
can be found in:

.. csv-table::
   :widths: auto

   harmonic.cpp,:ref:`harmonic.cpp-title`
   multi_newton.cpp,:ref:`multi_newton.cpp-title`

{xrst_end team_thread.hpp}
*/
# include <cstddef> // for size_t

// BEGIN PROTOTYPE
extern bool team_create(size_t num_threads);
extern bool team_work(void worker(void));
extern bool team_destroy(void);
extern const char* team_name(void);
// END PROTOTYPE

# endif