source: trunk/bug/alloc_global.sh @ 2745

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

Change scripts to build cppad/configure.hpp (no longer in repository).

alloc_global.sh: Problem is not getting the requested number of 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 2745 2013-01-15 13:37:03Z 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       
72        size_t num_threads = NUMBER_THREADS;
73        if( omp_get_max_threads() < num_threads )
74        {       std::cout << "can't set num_threads = " << num_threads << std::endl;
75                ok = false;
76        }
77
78        // call setup for using thread_alloc in parallel mode.
79        thread_alloc::parallel_setup(num_threads, in_parallel, thread_number);
80       
81        // Execute the worker function in parallel
82        int thread_num;
83# pragma omp parallel for
84        for(thread_num = 0; thread_num < num_threads; thread_num++)
85                worker();
86// end omp parallel for
87       
88        // now inform CppAD that there is only one thread
89        thread_alloc::parallel_setup(1, CPPAD_NULL, CPPAD_NULL);
90
91        for(thread_num = 0; thread_num < num_threads; thread_num++)
92        {       // check calculations by this thread in parallel model
93                ok &= thread_all_[thread_num].x[0] == static_cast<double>(thread_num);
94
95                // free memory that was allocated by thread thread_num
96                thread_all_[thread_num].x.resize(0);
97        }
98
99        return ok;
100}
101int main(void)
102{       bool ok = alloc_global();
103        std::cout << "OK = " << ok << std::endl;
104        return int(! ok);
105}
106EOF
107echo "g++ -g $name.cpp -I../.. -fopenmp -o $name"
108g++ -g $name.cpp -I../.. -fopenmp -o $name
109#
110echo "./$name"
111./$name
112#
113echo "rm $name $name.cpp"
114rm $name $name.cpp
Note: See TracBrowser for help on using the repository browser.