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

replace Boost is NaN test #101

Closed
bob-carpenter opened this issue May 31, 2013 · 4 comments
Closed

replace Boost is NaN test #101

bob-carpenter opened this issue May 31, 2013 · 4 comments
Assignees
Milestone

Comments

@bob-carpenter
Copy link
Contributor

The current use of Boost's isNaN test is taking up to 5% of total run time.

@ghost ghost assigned syclik May 31, 2013
@syclik
Copy link
Member

syclik commented May 31, 2013

Bob, can you attach the code used for timing? Or check it into the
repository?

I'm curious about the overall timing numbers just measured from the outside
the program rather than compiling it with debug flags and instrumentation.

On Fri, May 31, 2013 at 3:31 PM, Bob Carpenter notifications@github.comwrote:

The current use of Boost's isNaN test is taking up to 5% of total run time.


Reply to this email directly or view it on GitHubhttps://github.com//issues/101
.

@bob-carpenter
Copy link
Contributor Author

I didn't use any new code for timing. I used Instruments from Xcode
over the Rats model.

Checking nan was taking up 5% of the total compute time!

  • Bob

On 5/31/13 3:38 PM, Daniel Lee wrote:

Bob, can you attach the code used for timing? Or check it into the
repository?

I'm curious about the overall timing numbers just measured from the outside
the program rather than compiling it with debug flags and instrumentation.

On Fri, May 31, 2013 at 3:31 PM, Bob Carpenter notifications@github.comwrote:

The current use of Boost's isNaN test is taking up to 5% of total run time.


Reply to this email directly or view it on GitHubhttps://github.com//issues/101
.


Reply to this email directly or view it on GitHub #101 (comment).

@bob-carpenter
Copy link
Contributor Author

Tested with following code in g++ and clang++ (-O3 -g for both), and found no difference between Stan's check_not_nan function and just using (x != x) hack to detect NaNs. I didn't profile how much time the NaN tests were taking of the total, but I think we do need the test. (g++ was about 5% faster than clang++ when the branch in the test wasn't predictable.)

The following code produces 6 tests, all of which were run under g++ and clang++. All were same speed other than the unpredictable branch version, which was 10% slower under clang++ and 4% slower under g++.

#include <stan/math/error_handling/check_not_nan.hpp>
#include <iostream>
#include <limits>

inline double fun(double x) {
  if (x != x) return std::numeric_limits<double>::quiet_NaN();
  // stan::math::check_not_nan<double,double>("fun",x,"x");
  return x;
}

int main() {
  double outer_sum = 0;
  for (int m = 0; m < 10000; ++m) {
    double sum = 0;
    for (int n = 0; n < 1000000; ++n)
      sum += fun(std::numeric_limits<double>::quiet_NaN());   
      // sum += fun(0.001);                                    
      // sum += fun(n % 2 == 0 ? 0.01 : std::numeric_limits<double>::quiet_NaN());
    outer_sum += sum;
  }
  std::cout << "outer_sum=" << outer_sum << std::endl;
}

@mbrubake
Copy link
Member

I just compared (x != x) to boost::math::isnan() and found the run times
were basically the same with clang++ 3.0 under O3. I think the original
profiling may have been misleading or skewed. I'd like to propose that we
try to avoid the x!=x approach and instead use check_not_nan or
boost::math::isnan() as appropriate.

On Wed, Jun 19, 2013 at 3:12 PM, Bob Carpenter notifications@github.comwrote:

Tested with following code in g++ and clang++ (-O3 -g for both), and found
no difference between Stan's check_not_nan function and just using (x != x)
hack to detect NaNs. I didn't profile how much time the NaN tests were
taking of the total, but I think we do need the test. (g++ was about 5%
faster than clang++ when the branch in the test wasn't predictable.)

The following code produces 6 tests, all of which were run under g++ and
clang++. All were same speed other than the unpredictable branch version,
which was 10% slower under clang++ and 4% slower under g++.

#include <stan/math/error_handling/check_not_nan.hpp>
#include
#include

inline double fun(double x) {
if (x != x) return std::numeric_limits::quiet_NaN();
// stan::math::check_not_nan<double,double>("fun",x,"x");
return x;
}

int main() {
double outer_sum = 0;
for (int m = 0; m < 10000; ++m) {
double sum = 0;
for (int n = 0; n < 1000000; ++n)
sum += fun(std::numeric_limits::quiet_NaN());
// sum += fun(0.001);
// sum += fun(n % 2 == 0 ? 0.01 : std::numeric_limits::quiet_NaN());
outer_sum += sum;
}
std::cout << "outer_sum=" << outer_sum << std::endl;
}


Reply to this email directly or view it on GitHubhttps://github.com//issues/101#issuecomment-19707058
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants