Tape* TapeManager::findTape( const QString & id ) { /* 2002-01-26 LEW */ // printf("Contents of _tapeIDs: %d entries\n", _tapes.count()); // for ( QStringList::Iterator it = _tapeIDs.begin(); it != _tapeIDs.end(); ++it ) { // printf("%s\n", (*it).latin1()); // } // printf("\n"); // QDictIterator<Tape> it( _tapes ); // printf("Contents of _tapes: %d entries\n", _tapes.count()); // for( ; it.current(); ++it ) // printf("index ?: %s (%d)\n", it.current()->getName().latin1(), // it.current()->getCTime()); // printf("\n"); /* 2002-01-26 LEW */ Tape* tape = _tapes[ id ]; if ( !tape ) { tape = new Tape( id.ascii() ); _tapes.insert( tape->getID(), tape ); } return tape; }
double measure(Tape& tape) { uint64_t result(0); timer t; t.start(); std::accumulate(tape.begin(), tape.end(), result); return t.stop(); }
Tape* TapeManager::findTape( const char* id ) { Tape* tape = _tapes[ id ]; if ( !tape ) { tape = new Tape( id ); _tapes.insert( tape->getID(), tape ); } return tape; }
void run() { Tape tape; for (int pc = 0; pc < code.length(); pc++) { switch (code[pc]) { case '+': tape.inc(); break; case '-': tape.dec(); break; case '>': tape.advance(); break; case '<': tape.devance(); break; case '[': if (tape.get() == 0) pc = bracket_map[pc]; break; case ']': if (tape.get() != 0) pc = bracket_map[pc]; break; case '.': printf("%c", tape.get()); fflush(stdout); break; } } }
inline void print_tape() { if (tape_changed) { for (int i=0; i<tape.size(); i++) cout << tape[i]; cout << endl; } }
inline void move_head(char move) { if (move == 'l') pos -= 1; else if (move == 'r') pos += 1; else throw string("unknown state"); if (pos < 0) { tape.insert(tape.begin(), init); pos = 0; } if (pos >= tape.size()) { tape.push_back(init); } moves++; }
namespace graph { thread_local bool _backprop_enabled = true; thread_local Tape tape; void emplace_back(std::function<void()>&& f) { tape.backprop.emplace_back(f); } void backward() { tape.backward(); } void clear() { tape.backprop.clear(); } bool backprop_enabled() { return _backprop_enabled; } void _set_backprop_enabled(bool value) { _backprop_enabled = value; } size_t size() { return tape.backprop.size(); } /* Tape */ void Tape::backward () { for (auto it = backprop.rbegin(); it != backprop.rend(); ++it) (*it)(); backprop.clear(); } /* NoBackprop */ NoBackprop::NoBackprop() : NoBackprop(true) { } NoBackprop::NoBackprop(bool condition) : old_value(_backprop_enabled), enabled(condition) { if(enabled) _backprop_enabled = false; } NoBackprop::~NoBackprop() { if (enabled) _backprop_enabled = old_value; } }
int main() { const int success(0); Tape tape; tape.Initialize("AABB"); try{ tape.Update('B','L'); } catch(Crash &error) { cout << error.what() << ".\n"; } catch (bad_alloc& error) { cout << "allocation error!\n"; } catch(...) { cout << "Something went wrong!\n"; } tape.View(); return success; }
void Run(Tape &tape, bool print_each_step = false) { if (print_each_step) tape.PrintLine(); m_state = 0; while (!m_state_final[m_state]) { Rule23::iterator i = m_rules.find(m_state); if (i == m_rules.end()) { std::cerr << 'S' << m_state << " undefined.\n"; break; } char symbol_read = tape.Read(); Rule13 &rule13 = i->second; Rule13::iterator j = rule13.find(symbol_read); if (j == rule13.end()) { std::cerr << "No rule for S" << m_state << " and '" << symbol_read << "'.\n"; break; } Rule03 &rule03 = j->second; m_state = std::get<0>(rule03); char symbol_write = std::get<1>(rule03); tape.Write(symbol_write); char direction = std::get<2>(rule03); if (direction == 'L') tape.MoveLeft(); else if (direction == 'R') tape.MoveRight(); if (print_each_step) tape.PrintLine(); } }
/** * @brief Get a reference to the actual gradient value of this instance. * @return Reference to the gradient value. */ inline Real& gradient() { return globalTape.gradient(gradientData); }
inline void calcGradient(Data& data, const Real& jacobi) const { globalTape.pushJacobi(data, jacobi, primalValue, gradientData); }
/** * @brief Call the tape to destroy the gradient data. */ inline ~ActiveReal() { globalTape.destroyGradientData(primalValue, gradientData); }
/** * @brief Copy Constructor. The logic is handled by the tape. * * The tape is required to set the primal value of v to the primal value * of this type. * * @param[in] v The value to copy. */ inline ActiveReal(const ActiveReal<Real, Tape>& v) { globalTape.initGradientData(this->primalValue, gradientData); globalTape.store(primalValue, gradientData, v); }
inline ActiveReal(const Expression<Real, R>& rhs) { globalTape.initGradientData(this->primalValue, gradientData); globalTape.store(primalValue, gradientData, rhs.cast()); }
/** * @brief Sets the primal value of this ActiveReal and sets the gradient after it was initialized. * * @param[in] value The primal value for this type. * @param[in] gradient The gradient value for this type. */ inline ActiveReal(const Real& value, const Real& gradient) : primalValue(value) { globalTape.initGradientData(this->primalValue, gradientData); globalTape.setGradient(gradientData, gradient); }
inline ActiveReal<Real, Tape>& operator=(const Expression<Real, R>& rhs){ globalTape.store(primalValue, gradientData, rhs.cast()); return *this; }
/** * @brief Get the value of the gradient of this instance. * @return The gradient value. */ inline Real getGradient() const { return globalTape.getGradient(gradientData); }
void backward() { tape.backward(); }
/** * @brief Set the value of the gradient of this instance. * @param gradient The new gradient value. */ inline void setGradient(const Real& gradient) { globalTape.setGradient(gradientData, gradient); }
/** * @brief Constructs the equivalent of zero and initializes the gradient data. */ inline ActiveReal() : primalValue() { globalTape.initGradientData(primalValue, gradientData); }
/** * @brief Assignment for an ActiveReal on the rhs. E.g a = x; * * The logic is handled by the tape. The tape is required to set the primal value of * the rhs to the primal value of this instance. * * @param[in] rhs The other value on the rhs. * @return Reference to this. */ inline ActiveReal<Real, Tape>& operator=(const ActiveReal<Real, Tape>& rhs) { globalTape.store(primalValue, gradientData, rhs); return *this; }
/** * @brief Sets the primal value of the origin and initializes the gradient data. * * Initializes the value of the start of the ActiveType chain with the value. * * @param[in] value The primal value of the active type. */ inline ActiveReal(const PassiveReal& value) : primalValue(value) { globalTape.initGradientData(this->primalValue, gradientData); }