Commit ff664908 authored by Mike Kremer's avatar Mike Kremer
Browse files

Fixed memory issues with problem and constraint interface using auto diff.

git-svn-id: http://www.openflipper.org/svnrepo/CoMISo/trunk@186 1355f012-dd97-4b2f-ae87-10fa9f823a57
parent 5cd3a6af
...@@ -17,6 +17,8 @@ ...@@ -17,6 +17,8 @@
#include <CoMISo/Config/CoMISoDefines.hh> #include <CoMISo/Config/CoMISoDefines.hh>
#include "SuperSparseMatrixT.hh" #include "SuperSparseMatrixT.hh"
#include <boost/shared_array.hpp>
#include <adolc/adolc.h> #include <adolc/adolc.h>
#include <adolc/adouble.h> #include <adolc/adouble.h>
#include <adolc/drivers/drivers.h> #include <adolc/drivers/drivers.h>
...@@ -55,44 +57,16 @@ public: ...@@ -55,44 +57,16 @@ public:
n_unknowns_(_n_unknowns), n_unknowns_(_n_unknowns),
type_(_type), type_(_type),
x_d_(new adouble[n_unknowns_]), x_d_(new adouble[n_unknowns_]),
r_ind_(NULL),
c_ind_(NULL),
val_(NULL),
H_(NULL),
grad_(new double[n_unknowns_]), grad_(new double[n_unknowns_]),
function_evaluated_(false), function_evaluated_(false),
use_tape_(true), use_tape_(true),
constant_hessian_evaluated_(false) { constant_hessian_evaluated_(false) {
for(size_t i = 0; i < 11; ++i) tape_stats_[i] = -1; for(size_t i = 0; i < 11; ++i) tape_stats_[i] = -1;
if(sparse_hessian()) {
r_ind_ = new unsigned int[n_unknowns_];
c_ind_ = new unsigned int[n_unknowns_];
val_ = new double[n_unknowns_];
} else {
H_ = new double*[n_unknowns_];
for(int i = 0; i < n_unknowns_; ++i) {
H_[i] = new double[i+1];
}
}
} }
/// Destructor /// Destructor
virtual ~NConstraintInterfaceAD() { virtual ~NConstraintInterfaceAD() {
delete[] grad_;
delete[] x_d_;
if(sparse_hessian()) {
delete[] r_ind_;
delete[] c_ind_;
delete[] val_;
} else {
for(int i = 0; i < n_unknowns_; ++i) {
delete[] H_[i];
}
delete[] H_;
}
} }
/** /**
...@@ -123,7 +97,7 @@ public: ...@@ -123,7 +97,7 @@ public:
// Call virtual function to compute // Call virtual function to compute
// functional value // functional value
y_d = evaluate(x_d_); y_d = evaluate(x_d_.get());
y_d >>= y; y_d >>= y;
...@@ -162,7 +136,7 @@ public: ...@@ -162,7 +136,7 @@ public:
_g.resize(n_unknowns_); _g.resize(n_unknowns_);
_g.setZero(); _g.setZero();
int ec = gradient(1, n_unknowns_, _x, grad_); int ec = gradient(1, n_unknowns_, _x, grad_.get());
#ifndef NDEBUG #ifndef NDEBUG
std::cout << "Info: gradient() returned code " << ec << std::endl; std::cout << "Info: gradient() returned code " << ec << std::endl;
...@@ -192,7 +166,11 @@ public: ...@@ -192,7 +166,11 @@ public:
int nz = 0; int nz = 0;
int opt[2] = {0, 0}; int opt[2] = {0, 0};
int ec = sparse_hess(1, n_unknowns_, 0, _x, &nz, &r_ind_, &c_ind_, &val_, opt); unsigned int* r_ind = NULL;
unsigned int* c_ind = NULL;
double* val = NULL;
int ec = sparse_hess(1, n_unknowns_, 0, _x, &nz, &r_ind, &c_ind, &val, opt);
assert(*nz >= 0); assert(*nz >= 0);
assert(r_ind != NULL); assert(r_ind != NULL);
...@@ -205,7 +183,7 @@ public: ...@@ -205,7 +183,7 @@ public:
for(int i = 0; i < nz; ++i) { for(int i = 0; i < nz; ++i) {
_H(r_ind_[i], c_ind_[i]) = val_[i]; _H(r_ind[i], c_ind[i]) = val[i];
} }
if(constant_hessian()) { if(constant_hessian()) {
...@@ -213,9 +191,20 @@ public: ...@@ -213,9 +191,20 @@ public:
constant_hessian_evaluated_ = true; constant_hessian_evaluated_ = true;
} }
delete[] r_ind;
delete[] c_ind;
delete[] val;
} else { } else {
int ec = hessian(1, n_unknowns_, const_cast<double*>(_x), H_); double** H;
H = new double*[n_unknowns_];
for(int i = 0; i < n_unknowns_; ++i) {
H[i] = new double[i+1];
}
int ec = hessian(1, n_unknowns_, const_cast<double*>(_x), H);
#ifndef NDEBUG #ifndef NDEBUG
std::cout << "Info: hessian() returned code " << ec << std::endl; std::cout << "Info: hessian() returned code " << ec << std::endl;
...@@ -224,10 +213,10 @@ public: ...@@ -224,10 +213,10 @@ public:
for(int i = 0; i < n_unknowns_; ++i) { for(int i = 0; i < n_unknowns_; ++i) {
for(int j = 0; j <= i; ++j) { for(int j = 0; j <= i; ++j) {
_H(i, j) = H_[i][j]; _H(i, j) = H[i][j];
if(i != j) { if(i != j) {
_H(j, i) = H_[i][j]; _H(j, i) = H[i][j];
} }
} }
} }
...@@ -236,6 +225,11 @@ public: ...@@ -236,6 +225,11 @@ public:
constant_hessian_ = _H; constant_hessian_ = _H;
constant_hessian_evaluated_ = true; constant_hessian_evaluated_ = true;
} }
for(int i = 0; i < n_unknowns_; ++i) {
delete[] H[i];
}
delete[] H;
} }
} }
...@@ -280,17 +274,9 @@ private: ...@@ -280,17 +274,9 @@ private:
// Constraint type // Constraint type
ConstraintType type_; ConstraintType type_;
adouble* x_d_; boost::shared_array<adouble> x_d_;
// Sparse hessian data
unsigned int* r_ind_;
unsigned int* c_ind_;
double* val_;
// Non-sparse hessian data
double** H_;
double* grad_; boost::shared_array<double> grad_;
int tape_stats_[11]; int tape_stats_[11];
......
...@@ -15,9 +15,10 @@ ...@@ -15,9 +15,10 @@
//== INCLUDES ================================================================= //== INCLUDES =================================================================
#include <CoMISo/Utils/SmartPointer.hh>
#include <vector> #include <vector>
#include <boost/shared_array.hpp>
#include <adolc/adolc.h> #include <adolc/adolc.h>
#include <adolc/adouble.h> #include <adolc/adouble.h>
#include <adolc/drivers/drivers.h> #include <adolc/drivers/drivers.h>
...@@ -61,43 +62,15 @@ public: ...@@ -61,43 +62,15 @@ public:
NProblemInterfaceAD(int _n_unknowns) : NProblemInterfaceAD(int _n_unknowns) :
n_unknowns_(_n_unknowns), n_unknowns_(_n_unknowns),
x_d_(new adouble[_n_unknowns]), x_d_(new adouble[_n_unknowns]),
r_ind_(NULL),
c_ind_(NULL),
val_(NULL),
H_(NULL),
function_evaluated_(false), function_evaluated_(false),
use_tape_(true), use_tape_(true),
constant_hessian_evaluated_(false) { constant_hessian_evaluated_(false) {
for(size_t i = 0; i < 11; ++i) tape_stats_[i] = -1; for(size_t i = 0; i < 11; ++i) tape_stats_[i] = -1;
if(sparse_hessian()) {
r_ind_ = new unsigned int[n_unknowns_];
c_ind_ = new unsigned int[n_unknowns_];
val_ = new double[n_unknowns_];
} else {
H_ = new double*[n_unknowns_];
for(int i = 0; i < n_unknowns_; ++i) {
H_[i] = new double[i+1];
}
}
} }
/// Destructor /// Destructor
virtual ~NProblemInterfaceAD() { virtual ~NProblemInterfaceAD() {
if(sparse_hessian()) {
delete[] r_ind_;
delete[] c_ind_;
delete[] val_;
} else {
for(int i = 0; i < n_unknowns_; ++i) {
delete[] H_[i];
}
delete[] H_;
}
delete[] x_d_;
} }
// ================================================ // ================================================
...@@ -147,7 +120,7 @@ public: ...@@ -147,7 +120,7 @@ public:
// Call virtual function to compute // Call virtual function to compute
// functional value // functional value
y_d = evaluate(x_d_); y_d = evaluate(x_d_.get());
y_d >>= y; y_d >>= y;
...@@ -207,10 +180,15 @@ public: ...@@ -207,10 +180,15 @@ public:
if(sparse_hessian()) { if(sparse_hessian()) {
// Sparse hessian data
unsigned int* r_ind = NULL;
unsigned int* c_ind = NULL;
double* val = NULL;
int nz = 0; int nz = 0;
int opt[2] = {0, 0}; int opt[2] = {0, 0};
int ec = sparse_hess(1, n_unknowns_, 0, _x, &nz, &r_ind_, &c_ind_, &val_, opt); int ec = sparse_hess(1, n_unknowns_, 0, _x, &nz, &r_ind, &c_ind, &val, opt);
assert(*nz >= 0); assert(*nz >= 0);
assert(r_ind != NULL); assert(r_ind != NULL);
...@@ -223,7 +201,7 @@ public: ...@@ -223,7 +201,7 @@ public:
for(int i = 0; i < nz; ++i) { for(int i = 0; i < nz; ++i) {
_H.coeffRef(r_ind_[i], c_ind_[i]) = val_[i]; _H.coeffRef(r_ind[i], c_ind[i]) = val[i];
} }
if(constant_hessian()) { if(constant_hessian()) {
...@@ -231,9 +209,21 @@ public: ...@@ -231,9 +209,21 @@ public:
constant_hessian_evaluated_ = true; constant_hessian_evaluated_ = true;
} }
delete[] r_ind;
delete[] c_ind;
delete[] val;
} else { } else {
int ec = hessian(1, n_unknowns_, const_cast<double*>(_x), H_); // Non-sparse hessian data
double** H;
H = new double*[n_unknowns_];
for(int i = 0; i < n_unknowns_; ++i) {
H[i] = new double[i+1];
}
int ec = hessian(1, n_unknowns_, const_cast<double*>(_x), H);
#ifndef NDEBUG #ifndef NDEBUG
std::cout << "Info: hessian() returned code " << ec << std::endl; std::cout << "Info: hessian() returned code " << ec << std::endl;
...@@ -242,10 +232,10 @@ public: ...@@ -242,10 +232,10 @@ public:
for(int i = 0; i < n_unknowns_; ++i) { for(int i = 0; i < n_unknowns_; ++i) {
for(int j = 0; j <= i; ++j) { for(int j = 0; j <= i; ++j) {
_H.coeffRef(i, j) = H_[i][j]; _H.coeffRef(i, j) = H[i][j];
if(i != j) { if(i != j) {
_H.coeffRef(j, i) = H_[i][j]; _H.coeffRef(j, i) = H[i][j];
} }
} }
} }
...@@ -254,6 +244,11 @@ public: ...@@ -254,6 +244,11 @@ public:
constant_hessian_ = _H; constant_hessian_ = _H;
constant_hessian_evaluated_ = true; constant_hessian_evaluated_ = true;
} }
for(int i = 0; i < n_unknowns_; ++i) {
delete[] H[i];
}
delete[] H;
} }
} }
...@@ -274,15 +269,7 @@ private: ...@@ -274,15 +269,7 @@ private:
int n_unknowns_; int n_unknowns_;
adouble* x_d_; boost::shared_array<adouble> x_d_;
// Sparse hessian data
unsigned int* r_ind_;
unsigned int* c_ind_;
double* val_;
// Non-sparse hessian data
double** H_;
int tape_stats_[11]; int tape_stats_[11];
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment