forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
autodiffxd tests: Establish heap use baselines (RobotLocomotion#14018)
* autodiffxd tests: Establish heap use baselines Relevant to: RobotLocomotion#13985 Extend and use LimitMalloc to measure and enforce heap use of AutoDiffXd math operators. These limits will help clarify the effect of subsequent changes on the implementation of AutoDiffXd. Summary of changes * LimitMalloc: * Add params() accessor and .min_num_allocations parameter * Adjust error messages for clarity * Extend unit tests to cover new features * Disarm automatically under leak sanitizer builds * Introduce autodiffxd_heap_test * Add cases for global function overrides in autodiffxd.h
- Loading branch information
1 parent
71dbe45
commit 84aba35
Showing
5 changed files
with
266 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#include <gtest/gtest.h> | ||
|
||
#include "drake/common/autodiff.h" | ||
#include "drake/common/eigen_types.h" | ||
#include "drake/common/test_utilities/limit_malloc.h" | ||
|
||
using Eigen::VectorXd; | ||
|
||
namespace drake { | ||
namespace test { | ||
namespace { | ||
|
||
// Provide some autodiff variables that don't expire at scope end for test | ||
// cases. | ||
class AutoDiffXdHeapTest : public ::testing::Test { | ||
protected: | ||
AutoDiffXd x_{0.4, Eigen::VectorXd::Ones(3)}; | ||
AutoDiffXd y_{0.3, Eigen::VectorXd::Ones(3)}; | ||
}; | ||
|
||
// @note The test cases use a sum in function arguments to induce a temporary | ||
// that can potentially be consumed by pass-by-value optimizations. As of this | ||
// writing, none of the tested functions uses the optimization, so current heap | ||
// counts are a baseline for the existing implementations. | ||
|
||
// @note The tests cases use `volatile` variables to prevent optimizers from | ||
// removing the tested function calls entirely. As of this writing, there is no | ||
// evidence that the technique is strictly necessary. However, future | ||
// implementations may be vulnerable to dead-code elimination. | ||
|
||
TEST_F(AutoDiffXdHeapTest, Abs) { | ||
LimitMalloc guard({.max_num_allocations = 2, .min_num_allocations = 2}); | ||
volatile auto v = abs(x_ + y_); | ||
} | ||
|
||
TEST_F(AutoDiffXdHeapTest, Abs2) { | ||
LimitMalloc guard({.max_num_allocations = 2, .min_num_allocations = 2}); | ||
volatile auto v = abs2(x_ + y_); | ||
} | ||
|
||
TEST_F(AutoDiffXdHeapTest, Acos) { | ||
LimitMalloc guard({.max_num_allocations = 2, .min_num_allocations = 2}); | ||
volatile auto v = acos(x_ + y_); | ||
} | ||
|
||
TEST_F(AutoDiffXdHeapTest, Asin) { | ||
LimitMalloc guard({.max_num_allocations = 2, .min_num_allocations = 2}); | ||
volatile auto v = asin(x_ + y_); | ||
} | ||
|
||
TEST_F(AutoDiffXdHeapTest, Atan) { | ||
LimitMalloc guard({.max_num_allocations = 2, .min_num_allocations = 2}); | ||
volatile auto v = atan(x_ + y_); | ||
} | ||
|
||
TEST_F(AutoDiffXdHeapTest, Atan2) { | ||
LimitMalloc guard({.max_num_allocations = 8, .min_num_allocations = 8}); | ||
volatile auto v = atan2(x_ + y_, y_); | ||
volatile auto w = atan2(x_, x_ + y_); | ||
} | ||
|
||
TEST_F(AutoDiffXdHeapTest, Cos) { | ||
LimitMalloc guard({.max_num_allocations = 2, .min_num_allocations = 2}); | ||
volatile auto v = cos(x_ + y_); | ||
} | ||
|
||
TEST_F(AutoDiffXdHeapTest, Cosh) { | ||
LimitMalloc guard({.max_num_allocations = 2, .min_num_allocations = 2}); | ||
volatile auto v = cosh(x_ + y_); | ||
} | ||
|
||
TEST_F(AutoDiffXdHeapTest, Exp) { | ||
LimitMalloc guard({.max_num_allocations = 2, .min_num_allocations = 2}); | ||
volatile auto v = exp(x_ + y_); | ||
} | ||
|
||
TEST_F(AutoDiffXdHeapTest, Log) { | ||
LimitMalloc guard({.max_num_allocations = 2, .min_num_allocations = 2}); | ||
volatile auto v = log(x_ + y_); | ||
} | ||
|
||
TEST_F(AutoDiffXdHeapTest, Min) { | ||
LimitMalloc guard({.max_num_allocations = 4, .min_num_allocations = 4}); | ||
volatile auto v = min(x_ + y_, y_); | ||
volatile auto w = min(x_, x_ + y_); | ||
} | ||
|
||
TEST_F(AutoDiffXdHeapTest, Max) { | ||
LimitMalloc guard({.max_num_allocations = 4, .min_num_allocations = 4}); | ||
volatile auto v = max(x_ + y_, y_); | ||
volatile auto w = max(x_, x_ + y_); | ||
} | ||
|
||
TEST_F(AutoDiffXdHeapTest, Pow) { | ||
LimitMalloc guard({.max_num_allocations = 2, .min_num_allocations = 2}); | ||
volatile auto v = pow(x_ + y_, 2.0); | ||
} | ||
|
||
TEST_F(AutoDiffXdHeapTest, Sin) { | ||
LimitMalloc guard({.max_num_allocations = 2, .min_num_allocations = 2}); | ||
volatile auto v = sin(x_ + y_); | ||
} | ||
|
||
TEST_F(AutoDiffXdHeapTest, Sinh) { | ||
LimitMalloc guard({.max_num_allocations = 2, .min_num_allocations = 2}); | ||
volatile auto v = sinh(x_ + y_); | ||
} | ||
|
||
TEST_F(AutoDiffXdHeapTest, Sqrt) { | ||
LimitMalloc guard({.max_num_allocations = 2, .min_num_allocations = 2}); | ||
volatile auto v = sqrt(x_ + y_); | ||
} | ||
|
||
TEST_F(AutoDiffXdHeapTest, Tan) { | ||
LimitMalloc guard({.max_num_allocations = 2, .min_num_allocations = 2}); | ||
volatile auto v = tan(x_ + y_); | ||
} | ||
|
||
TEST_F(AutoDiffXdHeapTest, Tanh) { | ||
LimitMalloc guard({.max_num_allocations = 2, .min_num_allocations = 2}); | ||
volatile auto v = tanh(x_ + y_); | ||
} | ||
|
||
} // namespace | ||
} // namespace test | ||
} // namespace drake |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.