source: trunk/test_more/omp_alloc.cpp @ 3788

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

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

commit 431e0a227dbfe6172d265d9d79a2b5b258c5bc18
Author: Brad Bell <bradbell@…>
Date: Tue Feb 9 07:41:29 2016 -0700

  1. Change package.sh to automatically update version on (master branch only).
  2. Change version.sh copy to check and change.

commit a14455414810cfe3c3e4bca90090defc2528a353
Author: Brad Bell <bradbell@…>
Date: Tue Feb 9 06:19:54 2016 -0700

Change check_verbatim to check_srcfile
because all but one of the verbatim commands were changed to srcfile commands.


check_include_omh.sh: old check that file names did not change case (for cygwin development).

commit 4ce45b796b57629332ab46d8ae6df94e0a1ed998
Author: Brad Bell <bradbell@…>
Date: Tue Feb 9 06:04:57 2016 -0700

batch_edit.sh to change $verbatim and $codep to $srcfile and $srccode.


det_by_minor.c: remove some invisible white space.

commit 56553b88c9623c30d2222425a9640b95ce4c8281
Author: Brad Bell <bradbell@…>
Date: Mon Feb 8 18:01:49 2016 -0700

check_jenkins.sh: jenkins.sh no longer takes an argument.
jenkins.sh: fix name of script in error message.

commit 3b8a208cfc7e8ef3c928c17eb291aa3b90ff0050
Author: Brad Bell <bradbell@…>
Date: Mon Feb 8 07:57:02 2016 -0700

new_release.sh: track branches in comments, back to master at OK end, first check of response.

commit 442b7cbc45c022776e8257d3c3404dccdd06c420
Author: Brad Bell <bradbell@…>
Date: Mon Feb 8 06:01:11 2016 -0700

  1. Advance to release 20160000.1.
  2. Check using master version of new_release.sh.
  3. Make sure auto-tools version up to date.
  4. Ask user if doing further testing before commiting new release.

commit f7bdd1f48e72feb05d604da63914022809f45c28
Author: Brad Bell <bradbell@…>
Date: Sun Feb 7 07:59:41 2016 -0700

Add shared library version number to cppad_lib; i.e., cppad_lib.yyyy.mmdd.rel

commit a4c716552e3ad05b337aea58b643c9ad1cbcd4ac
Author: Brad Bell <bradbell@…>
Date: Sun Feb 7 05:25:39 2016 -0700

Make cppad_lib libarary conditional on colpack_prefix being specified.

commit 5e8890eb8de8b0cde146a6ed59c391d7c355ff24
Author: Brad Bell <bradbell@…>
Date: Tue Jan 26 10:49:37 2016 -0700

vector.hpp: fix The -> This.

commit e4e5442b069d7b00e197c31616da32eee20460b3
Merge: c4559d5 ed28b89
Author: Brad Bell <bradbell@…>
Date: Tue Jan 26 09:47:58 2016 -0700

Merge pull request #14 from barak/master


fix spelling in description of cppad_profile_flag on cmake command line

commit ed28b899c9fedab52a578aa7dd73818638081fe6
Author: Barak A. Pearlmutter <barak+git@…>
Date: Tue Jan 26 16:24:32 2016 +0000

typo

File size: 5.4 KB
Line 
1// $Id$
2/* --------------------------------------------------------------------------
3CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-16 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$srcfile%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.