1 /* --------------------------------------------------------------------------
2 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-15 Bradley M. Bell
3 
4 CppAD is distributed under the terms of the
5              Eclipse Public License Version 2.0.
6 
7 This Source Code may also be made available under the following
8 Secondary License when the conditions for such availability set forth
9 in the Eclipse Public License, Version 2.0 are satisfied:
10       GNU General Public License, Version 2.0 or later.
11 ---------------------------------------------------------------------------- */
12 /*
13 $begin adolc_alloc_mat$$
14 $spell
15     adolc
16     alloc
17 $$
18 
19 $section Adolc Test Utility: Allocate and Free Memory For a Matrix$$
20 
21 $head Syntax$$
22 $codei%mat% = adolc_alloc_mat(%m%, %n%)
23 %$$
24 $codei%adolc_free_mat(%mat%)
25 %$$
26 
27 $head Purpose$$
28 Use the $cref thread_alloc$$ memory allocator to allocate and free
29 memory that can be used as a matrix with the Adolc package.
30 
31 $head m$$
32 Is the number of rows in the matrix.
33 
34 $head n$$
35 Is the number of columns in the matrix.
36 
37 $head mat$$
38 Is the matrix.
39 To be specific,
40 between a call to $code adolc_alloc_mat$$,
41 and the corresponding call to $code adolc_free_mat$$,
42 for $icode%i% = 0 , %...% , %m%-1%$$
43 and $icode%j% = 0 , %...% , %n%-1%$$,
44 $icode%mat%[%i%][%j%]%$$ is the element in row $icode i$$
45 and column $icode j$$.
46 
47 $end
48 */
49 # include <cppad/utility/thread_alloc.hpp>
50 
adolc_alloc_mat(size_t m,size_t n)51 double** adolc_alloc_mat(size_t m, size_t n)
52 {   using CppAD::thread_alloc;
53     size_t size_min = m * n, size_out;
54     double*  vec = thread_alloc::create_array<double>(size_min, size_out);
55     double** mat = thread_alloc::create_array<double*>(size_min, size_out);
56 
57     for(size_t i = 0; i < m; i++)
58         mat[i] = vec + i * n;
59 
60     return mat;
61 }
adolc_free_mat(double ** mat)62 void adolc_free_mat(double** mat)
63 {   using CppAD::thread_alloc;
64     thread_alloc::delete_array(mat[0]);
65     thread_alloc::delete_array(mat);
66     return;
67 }
68