File: a11c_openmp.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 (76 lines) | stat: -rw-r--r-- 1,988 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
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-22 Bradley M. Bell
// ----------------------------------------------------------------------------

/*
{xrst_begin a11c_openmp.cpp}

A Simple OpenMP Example and Test
################################

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

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

{xrst_end a11c_openmp.cpp}
----------------------------------------------------------------------------
*/
// BEGIN C++
# include <omp.h>
# include <limits>
# include <cmath>
# include <cassert>
// for size_t
# include <cstddef>
//
# define NUMBER_THREADS 4

namespace {
   // Beginning of Example A.1.1.1c of OpenMP 2.5 standard document ---------
   void a1(int n, float *a, float *b)
   {  // Original had 'int i;' instead of 'int i = 0;'
      // Silence warning by clang 9.0.0: variable 'i' is uninitialized.
      int i = 0;
   # pragma omp parallel for
      for(i = 1; i < n; i++) /* i is private by default */
      {  assert( omp_get_num_threads() == NUMBER_THREADS );
         b[i] = (a[i] + a[i-1]) / float(2);
      }
   }
   // End of Example A.1.1.1c of OpenMP 2.5 standard document ---------------
}
bool a11c(void)
{  bool ok = true;

   // Test setup
   size_t i, n = 1000;
   float *a = new float[n];
   float *b = new float[n];
   for(i = 0; i < n; i++)
      a[i] = float(i);

   int n_thread = NUMBER_THREADS;   // number of threads in parallel regions
   omp_set_dynamic(0);              // off dynamic thread adjust
   omp_set_num_threads(n_thread);   // set the number of threads

   a1(int(n), a, b);

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

   delete [] a;
   delete [] b;

   return ok;
}
// END C++