1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-24 Bradley M. Bell
// ----------------------------------------------------------------------------
# include <cstring>
# include <cppad/utility/vector.hpp>
/*
{xrst_begin adolc_sparse_jacobian.cpp}
Adolc Speed: Sparse Jacobian
############################
Specifications
**************
See :ref:`link_sparse_jacobian-name` .
Implementation
**************
{xrst_spell_off}
{xrst_code cpp} */
// suppress conversion warnings before other includes
# include <cppad/wno_conversion.hpp>
//
# include <adolc/adolc.h>
# include <adolc/adolc_sparse.h>
# include <cppad/utility/vector.hpp>
# include <cppad/speed/uniform_01.hpp>
# include <cppad/speed/sparse_jac_fun.hpp>
// list of possible options
# include <map>
extern std::map<std::string, bool> global_option;
namespace {
using CppAD::vector;
typedef vector<size_t> s_vector;
typedef vector<double> d_vector;
typedef vector<adouble> a_vector;
void setup(
// inputs
short tag ,
size_t size ,
size_t m ,
const s_vector& row ,
const s_vector& col ,
const d_vector& x ,
int* options , // const but adolc want non-const arg
// outputs
size_t& n_color ,
int& nnz ,
unsigned int*& rind ,
unsigned int*& cind ,
double*& values )
{ // independent variables
CPPAD_ASSERT_UNKNOWN( size = x.size() );
int keep = 0; // keep forward mode results
trace_on(tag, keep);
size_t n = size;
a_vector a_x(n);
for(size_t j = 0; j < n; ++j)
a_x[j] <<= x[j];
//
// dependent variables
a_vector a_y(m);
//
// AD computation of f(x)
size_t order = 0;
CppAD::sparse_jac_fun<adouble>(m, n, a_x, row, col, order, a_y);
//
// create function object f : x -> y
double yi;
for(size_t i = 0; i < m; i++)
a_y[i] >>= yi;
trace_off();
//
// null pointers for recalculation of sparsity pattern
free(rind);
free(cind);
free(values);
rind = nullptr;
cind = nullptr;
values = nullptr;
//
// Retrieve n_color using undocumented feature of sparsedrivers.cpp
int same_pattern = 0;
n_color = size_t( sparse_jac(tag, int(m), int(n),
same_pattern, x.data(), &nnz, &rind, &cind, &values, options
) );
}
}
bool link_sparse_jacobian(
const std::string& job ,
size_t size ,
size_t repeat ,
size_t m ,
const CppAD::vector<size_t>& row ,
const CppAD::vector<size_t>& col ,
CppAD::vector<double>& x_return ,
CppAD::vector<double>& jacobian ,
size_t& n_color )
{
// --------------------------------------------------------------------
// check global options
// Allow colpack true even though it is not used below because it is
// true durng the adolc correctness tests.
const char* valid[] = { "onetape", "optimize", "colpack"};
size_t n_valid = sizeof(valid) / sizeof(valid[0]);
typedef std::map<std::string, bool>::iterator iterator;
//
for(iterator itr=global_option.begin(); itr!=global_option.end(); ++itr)
{ if( itr->second )
{ bool ok = false;
for(size_t i = 0; i < n_valid; i++)
ok |= itr->first == valid[i];
if( ! ok )
return false;
}
}
// -----------------------------------------------------
static size_t static_size = 0;
static int static_nnz = 0;
unsigned int* static_rind = nullptr;
unsigned int* static_cind = nullptr;
double* static_values = nullptr;
// -----------------------------------------------------
short tag = 0;
//
// options that control sparse_jac
int options[4];
if( global_option["boolsparsity"] )
options[0] = 1; // sparsity by propagation of bit pattern
else
options[0] = 0; // sparsity pattern by index domains
options[1] = 0; // 0 = safe mode, 1 = tight mode
options[2] = 0; // 0 = autodetect, 1 = forward, 2 = reverse
options[3] = 0; // 0 = column compression, 1 = row compression
//
// independent variiables
size_t n = size;
d_vector x(n);
//
// default value for n_color
n_color = 0;
//
bool onetape = global_option["onetape"];
// -----------------------------------------------------
if( job == "setup" )
{ // get a value for x
CppAD::uniform_01( size_t(n), x);
//
// record the tape and run coloring problem
options[2] = -1;
setup(tag, size, m, row, col, x, options,
n_color, static_nnz, static_rind, static_cind, static_values
);
static_size = size;
//
return true;
}
if( job == "teardown" )
{ free(static_rind);
free(static_cind);
free(static_values);
static_rind = nullptr;
static_cind = nullptr;
static_values = nullptr;
return true;
}
// -----------------------------------------------------
CPPAD_ASSERT_UNKNOWN( job == "run" );
//
while (repeat--)
{ // choose a value for x
CppAD::uniform_01( size_t(n), x);
//
if ( ! onetape )
{ // retape and calculate jacobian
options[2] = -1; // stop at sparsity pattern, return n_color
setup(tag, size, m, row, col, x, options,
n_color, static_nnz, static_rind, static_cind, static_values
);
options[2] = 0;
}
else
{ if( size != static_size )
CPPAD_ASSERT_UNKNOWN( size == static_size );
}
// calculate the jacobian at this x
int same_pattern = 1;
sparse_jac(
tag, int(m), int(n), same_pattern, x.data(),
&static_nnz, &static_rind, &static_cind, &static_values,
options
);
}
// --------------------------------------------------------------------
// jacobian
CPPAD_ASSERT_UNKNOWN( size_t(static_nnz) == row.size() );
for(int ell = 0; ell < static_nnz; ell++)
{ CPPAD_ASSERT_UNKNOWN( row[ell] == size_t(static_rind[ell]) );
CPPAD_ASSERT_UNKNOWN( col[ell] == size_t(static_cind[ell]) );
jacobian[ell] = static_values[ell];
}
// x_return
for(size_t j = 0; j < n; j++)
x_return[j] = x[j];
//
return true;
}
/* {xrst_code}
{xrst_spell_on}
{xrst_end adolc_sparse_jacobian.cpp}
*/
|