Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move sematics for adouble #94

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions ADOL-C/include/adolc/adouble.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ class ADOLC_DLL_EXPORT badouble {
void declareDependent();
badouble &operator=(double);
badouble &operator=(const badouble &);
badouble &operator=(const adub &);
double getValue() const;
inline double value() const { return getValue(); }
explicit operator double();
Expand Down Expand Up @@ -275,6 +274,7 @@ class ADOLC_DLL_EXPORT adouble : public badouble {
public:
adouble(const adub &);
adouble(const adouble &);
adouble(adouble &&) noexcept;
adouble(void);
adouble(double);
/* adub prevents postfix operators to occur on the left
Expand All @@ -293,7 +293,8 @@ class ADOLC_DLL_EXPORT adouble : public badouble {
adouble &operator=(double);
adouble &operator=(const badouble &);
adouble &operator=(const adouble &);
adouble &operator=(const adub &);
adouble &operator=(adouble &&);
adouble &operator=(adub &);
adouble &operator=(const pdouble &);

inline locint loc(void) const;
Expand Down
113 changes: 43 additions & 70 deletions ADOL-C/src/adouble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,18 @@ adouble::adouble(const adouble &a) {
#endif
}

/*--------------------------------------------------------------------------*/
adouble::adouble(adouble &&a) noexcept {
// Take data from 'a'
location = a.loc();

// If 'a' wasn't initialized, this one isn't initialized either.
isInit = a.isInit;

// Make 'a' not own the data anymore
a.isInit = false;
}

/*--------------------------------------------------------------------------*/
adouble::adouble(const adub &a) {
location = next_loc();
Expand Down Expand Up @@ -550,85 +562,46 @@ adouble &adouble::operator=(const adouble &x) {
(*this).badouble::operator=(x);
return (*this);
}

/*--------------------------------------------------------------------------*/
/* Assign an adouble an adub */
/* olvo 980517 new version griewank */
badouble &badouble::operator=(const adub &a) {
locint a_loc = a.loc();
ADOLC_OPENMP_THREAD_NUMBER;
ADOLC_OPENMP_GET_THREAD_NUMBER;
int upd = 0;
/* 981020 olvo skip upd_resloc(..) if no tracing performed */
if (ADOLC_CURRENT_TAPE_INFOS.traceFlag)
#if defined(ADOLC_TRACK_ACTIVITY)
upd = upd_resloc_check(a_loc, loc());
#else
upd = upd_resloc(a_loc, loc());
#endif
if (upd) { /* olvo 980708 new n2l & 980921 changed interface */
#if defined(ADOLC_TRACK_ACTIVITY)
/* r-value assignment */
adouble &adouble::operator=(adouble &&a) {
// Destruct the old content of this adouble
if (isInit) {
free_loc(location);
location = a_loc;
const_cast<adub &>(a).isInit = false;
#else
revreal tempVal = ADOLC_GLOBAL_TAPE_VARS.store[a_loc];
if (ADOLC_CURRENT_TAPE_INFOS.keepTaylors)
ADOLC_OVERWRITE_SCAYLOR(ADOLC_GLOBAL_TAPE_VARS.store[loc()],
&ADOLC_GLOBAL_TAPE_VARS.store[a_loc]);
ADOLC_GLOBAL_TAPE_VARS.store[loc()] = tempVal;
#endif
} else {
if (ADOLC_CURRENT_TAPE_INFOS
.traceFlag) { // old: write_assign_a(loc(),a_loc);
#if defined(ADOLC_TRACK_ACTIVITY)
if (ADOLC_GLOBAL_TAPE_VARS.actStore[a_loc]) {
#endif
put_op(assign_a);
ADOLC_PUT_LOCINT(a_loc); // = arg
ADOLC_PUT_LOCINT(loc()); // = res
}

++ADOLC_CURRENT_TAPE_INFOS.numTays_Tape;
if (ADOLC_CURRENT_TAPE_INFOS.keepTaylors)
ADOLC_WRITE_SCAYLOR(ADOLC_GLOBAL_TAPE_VARS.store[loc()]);
#if defined(ADOLC_TRACK_ACTIVITY)
} else {
if (ADOLC_GLOBAL_TAPE_VARS.actStore[location]) {
double coval = ADOLC_GLOBAL_TAPE_VARS.store[a_loc];
if (coval == 0) {
put_op(assign_d_zero);
ADOLC_PUT_LOCINT(location); // = res
} else if (coval == 1.0) {
put_op(assign_d_one);
ADOLC_PUT_LOCINT(location); // = res
} else {
put_op(assign_d);
ADOLC_PUT_LOCINT(location); // = res
ADOLC_PUT_VAL(coval); // = coval
}
// Set to new content
location = a.loc();

++ADOLC_CURRENT_TAPE_INFOS.numTays_Tape;
if (ADOLC_CURRENT_TAPE_INFOS.keepTaylors)
ADOLC_WRITE_SCAYLOR(ADOLC_GLOBAL_TAPE_VARS.store[loc()]);
}
}
#endif
}
ADOLC_GLOBAL_TAPE_VARS.store[loc()] = ADOLC_GLOBAL_TAPE_VARS.store[a_loc];
#if defined(ADOLC_TRACK_ACTIVITY)
ADOLC_GLOBAL_TAPE_VARS.actStore[loc()] =
ADOLC_GLOBAL_TAPE_VARS.actStore[a_loc];
#endif
}
// If 'a' wasn't initialized, this one isn't initialized either.
isInit = a.isInit;

return *this;
// Make sure that data is not touched when 'a' gets destructed
a.isInit = false;

return (*this);
}

/*--------------------------------------------------------------------------*/
/* Assign an adouble an adub */
/* Move an adouble an adub */
/* This treats the input adub as an r-value: It becomes unusable at the end of this method. */
/* olvo 980517 new version griewank */
adouble &adouble::operator=(const adub &a) {
this->loc(); // call for late init
(*this).badouble::operator=(a);
adouble &adouble::operator=(adub &a) {
// Destruct the old content of this adouble
if (isInit) {
free_loc(location);
}

// Set to new content
location = a.loc();

// If 'a' wasn't initialized, this one isn't initialized either.
isInit = a.isInit;

// Make the data is not touched when 'a' gets destructed
a.isInit = false;

return (*this);
}

Expand Down
Loading