File: a11c_pthread.cpp

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 (120 lines) | stat: -rw-r--r-- 3,077 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
// 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 a11c_pthread.cpp}
{xrst_spell
  pthreads
}

A Simple Parallel Pthread Example and Test
##########################################

Purpose
*******
This example just demonstrates pthreads and does not use CppAD at all.

Source Code
***********
{xrst_literal
   // BEGIN C++
   // END C++
}

{xrst_end a11c_pthread.cpp}
----------------------------------------------------------------------------
*/
// BEGIN C++
# include <pthread.h>
# include <limits>
# include <cmath>
# include <cassert>
// for size_t
# include <cstddef>

# define NUMBER_THREADS 4

# ifdef NDEBUG
# define CHECK_ZERO(expression) expression
# else
# define CHECK_ZERO(expression) assert( expression == 0 );
# endif
namespace {
   // Beginning of Example A.1.1.1c of OpenMP 2.5 standard document ---------
   void a1(int n, float *a, float *b)
   {  int i;
      for(i = 1; i < n; i++)
         b[i] = (a[i] + a[i-1]) / 2.0f;
      return;
   }
   // End of Example A.1.1.1c of OpenMP 2.5 standard document ---------------
   struct start_arg { int  n; float* a; float* b; };
   void* start_routine(void* arg_vptr)
   {  start_arg* arg = static_cast<start_arg*>( arg_vptr );
      a1(arg->n, arg->a, arg->b);

      void* no_status = nullptr;
      pthread_exit(no_status);

      return no_status;
   }
}

bool a11c(void)
{  bool ok = true;

   // Test setup
   int i, j, n_total = 10;
   float *a = new float[size_t(n_total)];
   float *b = new float[size_t(n_total)];
   for(i = 0; i < n_total; i++)
      a[i] = float(i);

   // number of threads
   int n_thread = NUMBER_THREADS;
   // the threads
   pthread_t thread[NUMBER_THREADS];
   // arguments to start_routine
   struct start_arg arg[NUMBER_THREADS];
   // attr
   pthread_attr_t attr;
   CHECK_ZERO( pthread_attr_init( &attr ) );
   CHECK_ZERO( pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE) );
   //
   // Break the work up into sub work for each thread
   int n = n_total / n_thread;
   arg[0].n = n;
   arg[0].a = a;
   arg[0].b = b;
   for(j = 1; j < n_thread; j++)
   {  arg[j].n = n + 1;
      arg[j].a = arg[j-1].a + n - 1;
      arg[j].b = arg[j-1].b + n - 1;
      if( j == (n_thread - 1) )
         arg[j].n = n_total - j * n + 1;
   }
   for(j = 0; j < n_thread; j++)
   {  // inform each thread of which block it is working on
      void* arg_vptr = static_cast<void*>( &arg[j] );
      CHECK_ZERO( pthread_create(
         &thread[j], &attr, start_routine, arg_vptr
      ) );
   }
   for(j = 0; j < n_thread; j++)
   {  void* no_status = nullptr;
      CHECK_ZERO( pthread_join(thread[j], &no_status) );
   }

   // check the result
   float eps = 100.0f * std::numeric_limits<float>::epsilon();
   for(i = 1; i < n ; i++)
      ok &= std::fabs( (2. * b[i] - a[i] - a[i-1]) / b[i] ) <= eps;

   delete [] a;
   delete [] b;

   return ok;
}
// END C++