source: trunk/test_more/omp_alloc.cpp @ 3757

Last change on this file since 3757 was 3757, checked in by bradbell, 4 years ago

merge to branch: trunk
from repository: https://github.com/coin-or/CppAD
start hash code: af6d293b6d7c5f0e00b361108403b4200215a7a7
end hash code: aec9ede58726ce1a40825eac63d4d8f7cb40bfba

commit aec9ede58726ce1a40825eac63d4d8f7cb40bfba
Author: Brad Bell <bradbell@…>
Date: Sun Nov 29 21:24:17 2015 -0800

Move omh/libaray to omh/utility becasue it corresponds to cppad/utility.

commit 17b6e02f29dcc2a1ab2d2e6980a979374e73e19e
Author: Brad Bell <bradbell@…>
Date: Sun Nov 29 19:03:22 2015 -0800

Batch edit the entire souce directory using the script

bin/batch_edit.sh

  1. With a few exceptions, move cppad/*.hpp to cppad/utility/*.hpp
  2. Change include gaurd from CPPAD_<NAME>_INCLUDED to CPPAD_<NAME>_HPP
  3. Make the source code control $Id$ commands more uniform.
  4. Remove invisible white space
  5. Create utility.hpp (includes entire utilty directory)

commit 4a8fbe249f97116cac86583a7d9bf596dbb06473
Author: Brad Bell <bradbell@…>
Date: Sun Nov 29 18:12:50 2015 -0800

new version that passes bin/check_all.sh

commit f041069fe3e24d68133f829353f7200cf2c299ed
Author: Brad Bell <bradbell@…>
Date: Sun Nov 29 14:27:08 2015 -0800

pases bin/check_all.sh

commit 32ac61696305b7625938d4c488203cdcc19f36dc
Author: Brad Bell <bradbell@…>
Date: Sun Nov 29 06:13:56 2015 -0800

passed bin/check_all.sh

File size: 5.4 KB
Line 
1// $Id$
2/* --------------------------------------------------------------------------
3CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-15 Bradley M. Bell
4
5CppAD is distributed under multiple licenses. This distribution is under
6the terms of the
7                    Eclipse 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
13/*
14$begin omp_alloc.cpp$$
15$spell
16        openmp
17$$
18
19$section OpenMP Memory Allocator: Example and Test$$
20$mindex allocation multi thread$$
21
22
23$head Deprecated 2011-08-31$$
24This example is only intended to help convert calls to $cref omp_alloc$$
25to calls to $cref thread_alloc$$.
26
27$code
28$verbatim%test_more/omp_alloc.cpp%0%// BEGIN C++%// END C++%1%$$
29$$
30
31$end
32*/
33// BEGIN C++
34# include <cppad/utility/omp_alloc.hpp>
35# include <cppad/utility/memory_leak.hpp>
36# include <vector>
37
38namespace { // Begin empty namespace
39
40bool omp_alloc_bytes(void)
41{       bool ok = true;
42        using CppAD::omp_alloc;
43        size_t thread;
44
45        // check initial memory values
46        ok &= ! CppAD::memory_leak();
47
48        // amount of static memory used by thread zero
49        size_t static_inuse = omp_alloc::inuse(0);
50
51        // determine the currently executing thread
52        // (should be zero because not in parallel mode)
53        thread = omp_alloc::get_thread_num();
54
55        // repeatedly allocate enough memory for at least two size_t values.
56        size_t min_size_t = 2;
57        size_t min_bytes  = min_size_t * sizeof(size_t);
58        size_t n_outter   = 10;
59        size_t n_inner    = 5;
60        size_t cap_bytes(0), i, j, k;
61        for(i = 0; i < n_outter; i++)
62        {       // Do not use CppAD::vector here because its use of omp_alloc
63                // complicates the inuse and avaialble results.
64                std::vector<void*> v_ptr(n_inner);
65                for( j = 0; j < n_inner; j++)
66                {       // allocate enough memory for min_size_t size_t objects
67                        v_ptr[j]    = omp_alloc::get_memory(min_bytes, cap_bytes);
68                        size_t* ptr = reinterpret_cast<size_t*>(v_ptr[j]);
69                        // determine the number of size_t values we have obtained
70                        size_t  cap_size_t = cap_bytes / sizeof(size_t);
71                        ok                &= min_size_t <= cap_size_t;
72                        // use placement new to call the size_t copy constructor
73                        for(k = 0; k < cap_size_t; k++)
74                                new(ptr + k) size_t(i + j + k);
75                        // check that the constructor worked
76                        for(k = 0; k < cap_size_t; k++)
77                                ok &= ptr[k] == (i + j + k);
78                }
79                // check that n_inner * cap_bytes are inuse and none are available
80                ok &= omp_alloc::inuse(thread) == n_inner*cap_bytes + static_inuse;
81                ok &= omp_alloc::available(thread) == 0;
82                // return the memrory to omp_alloc
83                for(j = 0; j < n_inner; j++)
84                        omp_alloc::return_memory(v_ptr[j]);
85                // check that now n_inner * cap_bytes are now available
86                // and none are in use
87                ok &= omp_alloc::inuse(thread) == static_inuse;
88                ok &= omp_alloc::available(thread) == n_inner * cap_bytes;
89        }
90        // return all the available memory to the system
91        omp_alloc::free_available(thread);
92        ok &= ! CppAD::memory_leak();
93
94        return ok;
95}
96
97class my_char {
98public:
99        char ch_ ;
100        my_char(void) : ch_(' ')
101        { }
102        my_char(const my_char& my_ch) : ch_(my_ch.ch_)
103        { }
104};
105
106bool omp_alloc_array(void)
107{       bool ok = true;
108        using CppAD::omp_alloc;
109        size_t i;
110
111        // check initial memory values
112        size_t thread = omp_alloc::get_thread_num();
113        ok &= thread == 0;
114        ok &= ! CppAD::memory_leak();
115        size_t static_inuse = omp_alloc::inuse(0);
116
117        // initial allocation of an array
118        size_t  size_min  = 3;
119        size_t  size_one;
120        my_char *array_one  =
121                omp_alloc::create_array<my_char>(size_min, size_one);
122
123        // check the values and change them to null 'x'
124        for(i = 0; i < size_one; i++)
125        {       ok &= array_one[i].ch_ == ' ';
126                array_one[i].ch_ = 'x';
127        }
128
129        // now create a longer array
130        size_t size_two;
131        my_char *array_two =
132                omp_alloc::create_array<my_char>(2 * size_min, size_two);
133
134        // check the values in array one
135        for(i = 0; i < size_one; i++)
136                ok &= array_one[i].ch_ == 'x';
137
138        // check the values in array two
139        for(i = 0; i < size_two; i++)
140                ok &= array_two[i].ch_ == ' ';
141
142        // check the amount of inuse and available memory
143        // (an extra size_t value is used for each memory block).
144        size_t check = static_inuse + sizeof(my_char)*(size_one + size_two);
145        ok   &= omp_alloc::inuse(thread) - check < sizeof(my_char);
146        ok   &= omp_alloc::available(thread) == 0;
147
148        // delete the arrays
149        omp_alloc::delete_array(array_one);
150        omp_alloc::delete_array(array_two);
151        ok   &= omp_alloc::inuse(thread) == static_inuse;
152        check = sizeof(my_char)*(size_one + size_two);
153        ok   &= omp_alloc::available(thread) - check < sizeof(my_char);
154
155        // free the memory for use by this thread
156        omp_alloc::free_available(thread);
157        ok &= ! CppAD::memory_leak();
158
159        return ok;
160}
161} // End empty namespace
162
163bool omp_alloc(void)
164{       bool ok  = true;
165        using CppAD::omp_alloc;
166
167        // check initial state of allocator
168        ok  &= omp_alloc::get_max_num_threads() == 1;
169
170        // set the maximum number of threads greater than one
171        // so that omp_alloc holds onto memory
172        CppAD::omp_alloc::set_max_num_threads(2);
173        ok  &= omp_alloc::get_max_num_threads() == 2;
174        ok  &= ! CppAD::memory_leak();
175
176        // now use memory allocator in state where it holds onto memory
177        ok   &= omp_alloc_bytes();
178        ok   &= omp_alloc_array();
179
180        // check that the tests have not held onto memory
181        ok  &= ! CppAD::memory_leak();
182
183        // set the maximum number of threads back to one
184        // so that omp_alloc no longer holds onto memory
185        CppAD::omp_alloc::set_max_num_threads(1);
186
187        return ok;
188}
189
190
191// END C++
Note: See TracBrowser for help on using the repository browser.