test_blockmem_gridmap_performance.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, the neonavigation authors
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <cstddef>
31 
32 #include <gtest/gtest.h>
33 
35 
36 TEST(BlockmemGridmap, SpacialAccessPerformance)
37 {
38  constexpr int size[3] =
39  {
40  0x420, 0x420, 0x28
41  };
42  constexpr int pad[3] =
43  {
44  0x200, 0x200, 0x10
45  };
46  constexpr int range = 0x10;
47 
50 
51  using Vec = CyclicVecInt<3, 2>;
52  using ThreeDimArrayFloat = std::array<std::array<std::array<float, size[0]>, size[1]>, size[2]>;
53 
54  std::shared_ptr<ThreeDimArrayFloat> array(new ThreeDimArrayFloat);
55  std::shared_ptr<ThreeDimArrayFloat> array_ret(new ThreeDimArrayFloat);
56 
57  gm.reset(Vec(size));
58  gm_ret.reset(Vec(size));
59 
60  Vec i;
61  // Generate dataset.
62  for (i[0] = 0; i[0] < size[0]; ++i[0])
63  {
64  for (i[1] = 0; i[1] < size[1]; ++i[1])
65  {
66  for (i[2] = 0; i[2] < size[2]; ++i[2])
67  {
68  gm[i] = i[2] * 0x100 + i[1] * 0x10 + i[0];
69  (*array)[i[2]][i[1]][i[0]] = gm[i];
70  }
71  }
72  }
73  // Check dataset.
74  for (i[0] = pad[0]; i[0] < size[0] - pad[0]; ++i[0])
75  {
76  for (i[1] = pad[1]; i[1] < size[1] - pad[1]; ++i[1])
77  {
78  for (i[2] = pad[2]; i[2] < size[2] - pad[2]; ++i[2])
79  {
80  ASSERT_EQ(gm[i], (*array)[i[2]][i[1]][i[0]]);
81  }
82  }
83  }
84 
85  // Performance test for BlockMemGridmap.
86  const auto ts0 = boost::chrono::high_resolution_clock::now();
87  for (i[0] = pad[0]; i[0] < size[0] - pad[0]; ++i[0])
88  {
89  for (i[1] = pad[1]; i[1] < size[1] - pad[1]; ++i[1])
90  {
91  for (i[2] = pad[2]; i[2] < size[2] - pad[2]; ++i[2])
92  {
93  Vec j;
94  gm_ret[i] = 0;
95 
96  for (j[0] = -range; j[0] <= range; ++j[0])
97  {
98  for (j[1] = -range; j[1] <= range; ++j[1])
99  {
100  for (j[2] = -range; j[2] <= range; ++j[2])
101  {
102  const Vec ij = i + j;
103  gm_ret[i] += gm[ij];
104  }
105  }
106  }
107  }
108  }
109  std::cerr << ".";
110  }
111  std::cerr << std::endl;
112  const auto te0 = boost::chrono::high_resolution_clock::now();
113  std::cout << "BlockMemGridmap<3, 2>: " << boost::chrono::duration<float>(te0 - ts0).count() << std::endl;
114 
115  // Performance test for 3D Array.
116  const auto ts1 = boost::chrono::high_resolution_clock::now();
117  for (i[0] = pad[0]; i[0] < size[0] - pad[0]; ++i[0])
118  {
119  for (i[1] = pad[1]; i[1] < size[1] - pad[1]; ++i[1])
120  {
121  for (i[2] = pad[2]; i[2] < size[2] - pad[2]; ++i[2])
122  {
123  Vec j;
124  (*array_ret)[i[2]][i[1]][i[0]] = 0;
125 
126  for (j[0] = -range; j[0] <= range; ++j[0])
127  {
128  for (j[1] = -range; j[1] <= range; ++j[1])
129  {
130  for (j[2] = -range; j[2] <= range; ++j[2])
131  {
132  const Vec ij = i + j;
133  (*array_ret)[i[2]][i[1]][i[0]] += (*array)[ij[2]][ij[1]][ij[0]];
134  }
135  }
136  }
137  }
138  }
139  std::cerr << ".";
140  }
141  std::cerr << std::endl;
142  const auto te1 = boost::chrono::high_resolution_clock::now();
143  std::cout << "Array[][][]: " << boost::chrono::duration<float>(te1 - ts1).count() << std::endl;
144 
145  // Check result.
146  for (i[0] = 0x200; i[0] < size[0] - 0x200; ++i[0])
147  {
148  for (i[1] = 0x200; i[1] < size[1] - 0x200; ++i[1])
149  {
150  for (i[2] = 0x10; i[2] < size[2] - 0x10; ++i[2])
151  {
152  ASSERT_EQ(gm_ret[i], (*array_ret)[i[2]][i[1]][i[0]]);
153  }
154  }
155  }
156 
157  // Compare performance.
158  ASSERT_LT(te0 - ts0, te1 - ts1);
159 }
160 
161 int main(int argc, char** argv)
162 {
163  testing::InitGoogleTest(&argc, argv);
164 
165  return RUN_ALL_TESTS();
166 }
void reset(const CyclicVecInt< DIM, NONCYCLIC > &size)
int main(int argc, char **argv)
TEST(BlockmemGridmap, SpacialAccessPerformance)


planner_cspace
Author(s): Atsushi Watanabe
autogenerated on Tue Jul 9 2019 05:00:13