joaoleal / CppADCodeGen

Source Code Generation for Automatic Differentiation using Operator Overloading
Other
162 stars 36 forks source link

model->Domain() returns size for previous model #84

Open bradbell opened 10 months ago

bradbell commented 10 months ago

Saving the C++ file and bash script in the same directory and running the bash script reproduces the problem. You will need to change the value of prefix in the bash script:

bash script

#! /usr/bin/env bash
set -e -u
#
# prefix
# You will need to change this to the prefix for your copy of cppadcg
prefix="$HOME/repo/cmpad.git/build/prefix"
#
# -----------------------------------------------------------------------------
#
# LD_LIBRARY_PATH
export LD_LIBRARY_PATH="$prefix/lib:$prefix/lib64"
#
if grep 'UNIQUE_PTR_ASSIGN_TO_NULL_BEFORE_REASIGN *1' example.cpp
then
   reasign='true'
elif grep 'UNIQUE_PTR_ASSIGN_TO_NULL_BEFORE_REASIGN *0' example.cpp
then
   reasign='false'
else
   echo 'Cannot find UNIQUE_PTR_ASSIGN_TO_NULL_BEFORE_REASIGN [01]'
   exit 1
fi
#
g++ example.cpp -o example \
   -g \
   -I $prefix/include \
   -L $prefix/lib \
   -L $prefix/lib64 \
   -l cppad_lib
#
if ./example
then
   if [ "$reasign" == 'true' ]
   then
      echo './example OK: as expected'
      exit 1
   else
      echo './example OK: not expected'
      exit 0
   fi
else
   if [ "$reasign" == 'true' ]
   then
      echo './example Error: not expected'
      exit 1
   else
      echo './example Error: as expected'
      exit 0
   fi
fi                  

C++ file

#include <iosfwd>
#include <vector>
#include <cppad/cg.hpp>

//
// NIQUE_PTR_ASSIGN_TO_NULL_BEFORE_REASIGN
// If this is zero (is one) main returns an error flag (returns 0)
# define UNIQUE_PTR_ASSIGN_TO_NULL_BEFORE_REASIGN 0

using namespace CppAD;
using namespace CppAD::cg;

namespace {
   // CGD, ADCG
   typedef CG<double> CGD;
   typedef AD<CGD> ADCG;
   //
   // record_fun
   void record_fun(ADFun<CGD>& fun, size_t n)
   {
      // ax
      std::vector<ADCG> ax(n);
      Independent(ax);
      //
      // ay
      std::vector<ADCG> ay(1);
      ay[0] = ax[0];
      for(size_t j = 1; j < n; ++j)
         ay[0] += ax[j];
      //
      // fun
      fun.Dependent(ax, ay);
   }
}

int main(void) {
   //
   // ok
   bool ok = true;
   //
   // n, fun
   size_t n = 2;
   ADFun<CGD> fun;
   record_fun(fun, n);
   //
   // licbgen_one
   ModelCSourceGen<double> cgen_one(fun, "my_model");
   cgen_one.setCreateJacobian(true);
   ModelLibraryCSourceGen<double> libcgen_one(cgen_one);
   //
   // compiler
   GccCompiler<double> compiler;
   //
   // dynamicLib
   DynamicModelLibraryProcessor<double> p_one(libcgen_one);
   std::unique_ptr<DynamicLib<double>> dynamicLib = 
      p_one.createDynamicLibrary(compiler);
   //
   // model
   std::unique_ptr<GenericModel<double>> model = dynamicLib->model("my_model");
   //
   // ok
   ok &= model->Domain() == n;
   //
   // dynamicLib, model
# if UNIQUE_PTR_ASSIGN_TO_NULL_BEFORE_REASIGN
   dynamicLib = nullptr;
   model      = nullptr;
# endif
   //
   // n, fun
   n = 3;
   record_fun(fun, n);
   //
   // licbgen_two
   ModelCSourceGen<double> cgen_two(fun, "my_model");
   cgen_two.setCreateJacobian(true);
   ModelLibraryCSourceGen<double> libcgen_two(cgen_two);
   //
   // dynamicLib
   DynamicModelLibraryProcessor<double> p_two(libcgen_two);
   dynamicLib = p_two.createDynamicLibrary(compiler);
   //
   // model
   model = dynamicLib->model("my_model");
   //
   // ok
   ok &= model->Domain() == n;
   //
   if( ok )
      return 0;
   return 1;
}