24 determineJacobianSparsity();
27 std::map<size_t, std::vector<size_t> > elements;
28 for (
size_t e = 0; e < _jacSparsity.rows.size(); e++) {
29 elements[_jacSparsity.rows[e]].push_back(_jacSparsity.cols[e]);
32 if (!_loopTapes.empty()) {
36 prepareSparseReverseOneWithLoops(elements);
43 startingJob(
"'model (reverse one)'", JobTimer::SOURCE_GENERATION);
45 if (isAtomicsUsed()) {
46 generateSparseReverseOneSourcesWithAtomics(elements);
48 generateSparseReverseOneSourcesNoAtomics(elements);
55 generateGlobalDirectionalFunctionSource(FUNCTION_SPARSE_REVERSE_ONE,
57 FUNCTION_REVERSE_ONE_SPARSITY,
68 size_t m = _fun.Range();
69 size_t n = _fun.Domain();
76 const std::string jobName =
"model (reverse one)";
77 startingJob(
"'" + jobName +
"'", JobTimer::SOURCE_GENERATION);
79 for (
const auto& it : elements) {
81 const std::vector<size_t>& cols = it.second;
84 _cache <<
"model (reverse one, dep " << i <<
")";
85 const std::string subJobName = _cache.str();
87 startingJob(
"'" + subJobName +
"'", JobTimer::GRAPH);
90 handler.setJobTimer(_jobTimer);
95 for (
size_t i = 0; i < n; i++) {
96 indVars[i].setValue(_x[i]);
107 _fun.Forward(0, indVars);
111 CPPADCG_ASSERT_UNKNOWN(dw.size() == n);
115 for (
size_t it2 : cols) {
116 dwCustom.push_back(dw[it2]);
126 _cache << _name <<
"_" << FUNCTION_SPARSE_REVERSE_ONE <<
"_dep" << i;
127 langC.setGenerateFunction(_cache.str());
129 std::ostringstream code;
130 std::unique_ptr<VariableNameGenerator<Base> > nameGen(createVariableNameGenerator(
"dw"));
133 handler.
generateCode(code, langC, dwCustom, nameGenHess, _atomicFunctions, subJobName);
144 size_t m = _fun.Range();
145 size_t n = _fun.Domain();
148 handler.setJobTimer(_jobTimer);
153 for (
size_t i = 0; i < n; i++) {
154 x[i].setValue(_x[i]);
166 CppAD::sparse_jacobian_work work;
167 _fun.SparseJacobianReverse(x, _jacSparsity.sparsity, _jacSparsity.rows, _jacSparsity.cols, jacFlat, work);
172 std::map<size_t, vector<CGBase> > jac;
173 std::vector<std::map<size_t, size_t> > positions(m);
175 for (
const auto& it : elements) {
177 const std::vector<size_t>& row = it.second;
179 jac[i].resize(row.size());
180 std::map<size_t, size_t>& pos = positions[i];
182 for (
size_t e = 0; e < row.size(); e++) {
188 for (
size_t el = 0; el < _jacSparsity.rows.size(); el++) {
189 size_t i = _jacSparsity.rows[el];
190 size_t j = _jacSparsity.cols[el];
191 size_t e = positions[i].at(j);
194 row[e] = jacFlat[el] * py;
200 typename std::map<size_t, vector<CGBase> >::iterator itI;
201 for (itI = jac.begin(); itI != jac.end(); ++itI) {
202 size_t i = itI->first;
206 _cache <<
"model (reverse one, dep " << i <<
")";
207 const std::string subJobName = _cache.str();
214 _cache << _name <<
"_" << FUNCTION_SPARSE_REVERSE_ONE <<
"_dep" << i;
215 langC.setGenerateFunction(_cache.str());
217 std::ostringstream code;
218 std::unique_ptr<VariableNameGenerator<Base> > nameGen(createVariableNameGenerator(
"dw"));
221 handler.
generateCode(code, langC, dwCustom, nameGenHess, _atomicFunctions, subJobName);
static void printFunctionDeclaration(std::ostringstream &out, const std::string &returnType, const std::string &functionName, const std::vector< std::string > &arguments, const std::vector< std::string > &arguments2={})