source: trunk/test_more/alloc_openmp.cpp @ 2354

Last change on this file since 2354 was 2307, checked in by bradbell, 8 years ago

Include automated test for, and fix, bug in thread_alloc.

speed_compare.sh: fix old version of make test command.
alloc_global.sh: original version of test before totally automated.
reachable.sh: add copyright.
thread_alloc.hpp: fix bug, fix documentation of return_memory.

  • Property svn:keywords set to Id
File size: 2.4 KB
Line 
1/* $Id: alloc_openmp.cpp 2307 2012-03-21 19:13:44Z bradbell $ */
2/* --------------------------------------------------------------------------
3CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-12 Bradley M. Bell
4
5CppAD is distributed under multiple licenses. This distribution is under
6the terms of the
7                    Common Public License Version 1.0.
8
9A copy of this license is included in the COPYING file of this distribution.
10Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
11-------------------------------------------------------------------------- */
12// BEGIN PROGRAM
13# include <cppad/cppad.hpp>
14# include <omp.h>
15# define NUMBER_THREADS  2
16
17namespace {
18        using CppAD::thread_alloc;
19
20        // used to inform CppAD when we are in parallel execution mode
21        bool in_parallel(void)
22        {       return static_cast<bool>( omp_in_parallel() ); }
23
24        // used to inform CppAD of current thread number thread_number()
25        size_t thread_number(void)
26        {       return static_cast<bool>( omp_get_thread_num() ); }
27
28        // structure with information for one thread
29        typedef struct {
30                // function object (worker input)
31                CppAD::vector<double> x;
32        } thread_one_t;
33
34        // vector with information for all threads
35        thread_one_t thread_all_[NUMBER_THREADS];
36
37        // --------------------------------------------------------------------
38        // function that does the work for one thread
39        void worker(void)
40        {
41                size_t thread_num = thread_number();
42                thread_all_[thread_num].x.resize(1);
43                thread_all_[thread_num].x[0]=static_cast<double>(thread_num);
44        }
45}
46
47// Test routine called by the master thread (thread_num = 0).
48bool alloc_openmp(void)
49{       bool ok = true;
50       
51        int num_threads = NUMBER_THREADS;
52
53        // call setup for using thread_alloc in parallel mode.
54        thread_alloc::parallel_setup(
55                size_t(num_threads), in_parallel, thread_number
56        );
57       
58        // Execute the worker function in parallel
59        int thread_num;
60# pragma omp parallel for
61        for(thread_num = 0; thread_num < num_threads; thread_num++)
62                worker();
63// end omp parallel for
64       
65        // now inform CppAD that there is only one thread
66        thread_alloc::parallel_setup(1, CPPAD_NULL, CPPAD_NULL);
67
68        for(thread_num = 0; thread_num < num_threads; thread_num++)
69        {       // check calculations by this thread in parallel model
70                ok &= thread_all_[thread_num].x[0] == static_cast<double>(thread_num);
71
72                // test having master thread (thread number zero)
73                // free memory that was allocated by thread number thread_num.
74                thread_all_[thread_num].x.resize(0);
75        }
76
77        return ok;
78}
Note: See TracBrowser for help on using the repository browser.