source: trunk/bug/alloc_global.sh @ 2898

Last change on this file since 2898 was 2898, checked in by bradbell, 7 years ago

Bring up to date so run with current version.

alloc_global.sh: fix check so does not fail when cannot get enough threads.

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
1#! /bin/bash -e
2# $Id: alloc_global.sh 2898 2013-09-18 13:07:32Z bradbell $
3# -----------------------------------------------------------------------------
4# CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-13 Bradley M. Bell
5#
6# CppAD is distributed under multiple licenses. This distribution is under
7# the terms of the
8#                     Eclipse Public License Version 1.0.
9#
10# A copy of this license is included in the COPYING file of this distribution.
11# Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
12# -----------------------------------------------------------------------------
13#!/bin/bash -e
14#
15if [ ! -e build ]
16then
17        mkdir build
18fi
19cd build
20if [ ! -e ../../cppad/configure.hpp ]
21then
22        cmake ../..
23fi
24#
25echo "$0"
26name=`echo $0 | sed -e 's|.*/||' -e 's|\..*||'`
27echo "create $name.cpp"
28cat << EOF > $name.cpp
29// BEGIN PROGRAM
30# include <cppad/cppad.hpp>
31# include <omp.h>
32# define NUMBER_THREADS  2
33
34namespace {
35        using CppAD::thread_alloc;
36
37        // used to inform CppAD when we are in parallel execution mode
38        bool in_parallel(void)
39        {       return static_cast<bool>( omp_in_parallel() ); }
40
41        // used to inform CppAD of current thread number thread_number()
42        size_t thread_number(void)
43        {       return static_cast<bool>( omp_get_thread_num() ); }
44
45        // structure with information for one thread
46        typedef struct {
47                // function object (worker input)
48                CppAD::vector<double> x;
49        } thread_one_t;
50
51        // vector with information for all threads
52        thread_one_t thread_all_[NUMBER_THREADS];
53
54        // --------------------------------------------------------------------
55        // function that does the work for one thread
56        void worker(void)
57        {
58                size_t thread_num = thread_number();
59                thread_all_[thread_num].x.resize(1);
60                thread_all_[thread_num].x[0]=static_cast<double>(thread_num);
61
62                std::stringstream stream;
63                stream << "thread_num = " << thread_num << std::endl;
64                std::cout << stream.str();
65        }
66}
67
68// Test routine called by the master thread (thread_num = 0).
69bool alloc_global(void)
70{       bool ok = true;
71        using std::cout;
72        using std::endl;
73       
74        size_t num_threads = NUMBER_THREADS;
75        if( omp_get_max_threads() < num_threads )
76        {       cout << "can't set num_threads = " << num_threads << endl;
77                ok = false;
78                return ok;
79        }
80
81        // call setup for using thread_alloc in parallel mode.
82        thread_alloc::parallel_setup(num_threads, in_parallel, thread_number);
83       
84        // Execute the worker function in parallel
85        int thread_num;
86# pragma omp parallel for
87        for(thread_num = 0; thread_num < num_threads; thread_num++)
88                worker();
89// end omp parallel for
90       
91        // now inform CppAD that there is only one thread
92        thread_alloc::parallel_setup(1, CPPAD_NULL, CPPAD_NULL);
93
94        for(thread_num = 0; thread_num < num_threads; thread_num++)
95        {       // check calculations by this thread in parallel model
96                ok &= thread_all_[thread_num].x[0] == static_cast<double>(thread_num);
97
98                // free memory that was allocated by thread thread_num
99                thread_all_[thread_num].x.resize(0);
100        }
101
102        return ok;
103}
104int main(void)
105{       bool ok = alloc_global();
106        std::cout << "OK = " << ok << std::endl;
107        return int(! ok);
108}
109EOF
110echo "g++ -g $name.cpp -I../.. -fopenmp -std=c++11 -o $name"
111g++ -g $name.cpp -I../.. -fopenmp -std=c++11 -o $name
112#
113echo "./$name"
114./$name
115#
116echo "rm $name $name.cpp"
117rm $name $name.cpp
Note: See TracBrowser for help on using the repository browser.