1 #ifndef CPPAD_CG_MODEL_C_SOURCE_GEN_REV1_INCLUDED 2 #define CPPAD_CG_MODEL_C_SOURCE_GEN_REV1_INCLUDED 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);
227 size_t m = _fun.Range();
228 size_t n = _fun.Domain();
231 _cache << _name <<
"_" << FUNCTION_REVERSE_ONE;
232 std::string model_function(_cache.str());
236 std::string argsDcl = langC.generateDefaultFunctionArgumentsDcl();
237 std::string args = langC.generateDefaultFunctionArguments();
239 _cache <<
"#include <stdlib.h>\n" 242 "int " << _name <<
"_" << FUNCTION_SPARSE_REVERSE_ONE <<
"(unsigned long pos, " << argsDcl <<
");\n" 243 "void " << _name <<
"_" << FUNCTION_REVERSE_ONE_SPARSITY <<
"(unsigned long pos, unsigned long const** elements, unsigned long* nnz);\n" 246 _baseTypeName +
" const ty[]",
247 _baseTypeName +
" px[]",
248 _baseTypeName +
" const py[]",
249 langC.generateArgumentAtomicDcl()});
251 " unsigned long ei, ePos, i, j, nnz, nnzMax;\n" 252 " unsigned long const* pos;\n" 253 " unsigned long* pyPos;\n" 254 " unsigned long* pyPosTmp;\n" 255 " unsigned long nnzPy;\n" 256 " " << _baseTypeName <<
" const * in[2];\n" 257 " " << _baseTypeName <<
"* out[1];\n" 258 " " << _baseTypeName <<
"* compressed;\n" 264 " for (i = 0; i < " << m <<
"; i++) {\n" 265 " if (py[i] != 0.0) {\n" 266 " " << _name <<
"_" << FUNCTION_REVERSE_ONE_SPARSITY <<
"(i, &pos, &nnz);\n" 267 " if (nnz > nnzMax)\n" 269 " else if (nnz == 0)\n" 272 " pyPosTmp = (unsigned long*) realloc(pyPos, nnzPy * sizeof(unsigned long));\n" 273 " if (pyPosTmp != NULL) {\n" 274 " pyPos = pyPosTmp;\n" 277 " return -1; // failure to allocate memory\n" 279 " pyPos[nnzPy - 1] = i;\n" 282 " for (j = 0; j < " << n <<
"; j++) {\n" 286 " if (nnzPy == 0) {\n" 288 " return 0; //nothing to do\n" 291 " compressed = (" << _baseTypeName <<
"*) malloc(nnzMax * sizeof(" << _baseTypeName <<
"));\n" 293 " for (ei = 0; ei < nnzPy; ei++) {\n" 295 " " << _name <<
"_" << FUNCTION_REVERSE_ONE_SPARSITY <<
"(i, &pos, &nnz);\n" 299 " out[0] = compressed;\n";
300 if (!_loopTapes.empty()) {
301 _cache <<
" for(ePos = 0; ePos < nnz; ePos++)\n" 302 " compressed[ePos] = 0;\n" 305 _cache <<
" ret = " << _name <<
"_" << FUNCTION_SPARSE_REVERSE_ONE <<
"(i, " << args <<
");\n" 308 " free(compressed);\n" 313 " for (ePos = 0; ePos < nnz; ePos++) {\n" 314 " px[pos[ePos]] += compressed[ePos];\n" 318 " free(compressed);\n" 322 _sources[model_function +
".c"] = _cache.str();
void setValue(const Base &val)
void setMaxOperationsPerAssignment(size_t maxOperationsPerAssignment)
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={})
void makeVariables(VectorCG &variables)
virtual void generateSparseReverseOneSources()
virtual void setMaxAssignmentsPerFunction(size_t maxAssignmentsPerFunction, std::map< std::string, std::string > *sources)
void makeVariable(AD< CGB > &variable)
virtual void setParameterPrecision(size_t p)
virtual void generateSparseReverseOneSourcesWithAtomics(const std::map< size_t, std::vector< size_t > > &elements)
virtual void generateCode(std::ostream &out, Language< Base > &lang, CppAD::vector< CGB > &dependent, VariableNameGenerator< Base > &nameGen, const std::string &jobName="source")
virtual void generateSparseReverseOneSourcesNoAtomics(const std::map< size_t, std::vector< size_t > > &elements)