math icon indicating copy to clipboard operation
math copied to clipboard

constant vari* for numeric_limits return values

Open syclik opened this issue 10 years ago • 0 comments

From @bob-carpenter on February 4, 2015 1:49

Right now, when external programs use numeric_limits, new vari instances are created in the memory arena. This is done by some routines in Eigen and also by Boost's internal comparisons. These take time to allocate and also take up space, and furthermore increase derivative propagation time.

We're not sure what will work given our override of operator new for vari and the need to do these allocations statically.

Proposed Solution 1

Use an explicit placement new to allocate a vari outside the memory arena and assign it to a static const in the numeric_limits function definition.

This is a clean, isolated solution with no interactions.

Proposed Solution 2

Allocate designated positions on the bottom of the stack that never get collected.

This solution requires modifying the loop bounds in the derivative propagation algorithms. Memory allocation should remain unmodified because this will be using it.

Current Definitions

Here are the definitions that need to change:

template<> 
struct numeric_limits<stan::agrad::var> {  
  static stan::agrad::var epsilon() { return numeric_limits<double>::epsilon(); }
  static stan::agrad::var round_error() { return numeric_limits<double>::round_error(); }
  static stan::agrad::var infinity() { return numeric_limits<double>::infinity(); }
  static stan::agrad::var quiet_NaN() { return numeric_limits<double>::quiet_NaN(); }
  static stan::agrad::var signaling_NaN() { return numeric_limits<double>::signaling_NaN(); }
  static stan::agrad::var denorm_min() { return numeric_limits<double>::denorm_min(); }

Copied from original issue: stan-dev/stan#1263

syclik avatar Jul 06 '15 21:07 syclik