36 #include <visp3/core/vpConfig.h> 38 #ifdef VISP_HAVE_CATCH2 39 #define CATCH_CONFIG_ENABLE_BENCHMARKING 40 #define CATCH_CONFIG_RUNNER 43 #include <visp3/core/vpMatrix.h> 45 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000) 46 #include <opencv2/core.hpp> 49 #ifdef VISP_HAVE_EIGEN3 50 #include <Eigen/Dense> 55 bool g_runBenchmark =
false;
58 vpMatrix generateMatrix(
unsigned int sz1,
unsigned int sz2)
62 for (
unsigned int i = 0; i < M.getRows(); i++) {
63 for (
unsigned int j = 0; j < M.getCols(); j++) {
64 M[i][j] = i * M.getCols() + j;
71 vpMatrix generateMatrixTranspose(
unsigned int sz1,
unsigned int sz2)
75 for (
unsigned int j = 0; j < M.getCols(); j++) {
76 for (
unsigned int i = 0; i < M.getRows(); i++) {
77 M[i][j] = j * M.getRows() + i;
90 for (
unsigned int i = 0; i < A.
getRows(); i++) {
92 for (
unsigned int j = 0; j < A.
getCols(); j++) {
106 for (
unsigned int j = 0; j < A.
getCols(); j++) {
107 double *coli = At[j];
108 for (
unsigned int i = 0; i < A.
getRows(); i++) {
122 for (
unsigned int i = 0; i < A.
getRows(); i += tileSize) {
123 for (
unsigned int j = 0; j < A.
getCols(); j++) {
124 for (
unsigned int b = 0; b < tileSize && i + b < A.
getRows(); b++) {
125 At[j][i + b] = A[i + b][j];
139 const int nrows =
static_cast<int>(A.
getRows());
140 const int ncols =
static_cast<int>(A.
getCols());
142 for (
int i = 0; i < nrows;) {
143 for (; i <= nrows - tileSize; i += tileSize) {
145 for (; j <= ncols - tileSize; j += tileSize) {
146 for (
int k = i; k < i + tileSize; k++) {
147 for (
int l = j; l < j + tileSize; l++) {
153 for (
int k = i; k < i + tileSize; k++) {
154 for (
int l = j; l < ncols; l++) {
160 for (; i < nrows; i++) {
161 for (
int j = 0; j < ncols; j++) {
172 TEST_CASE(
"Benchmark vpMatrix transpose",
"[benchmark]") {
173 if (g_runBenchmark) {
174 const std::vector<std::pair<int, int>> sizes = { {701, 1503}, {1791, 837}, {1201, 1201}, {1024, 1024}, {2000, 2000},
175 {10, 6}, {25, 6}, {100, 6}, {200, 6}, {500, 6}, {1000, 6}, {1500, 6}, {2000, 6},
176 {6, 10}, {6, 25}, {6, 100}, {6, 200}, {6, 500}, {6, 1000}, {6, 1500}, {6, 2000},
177 {640, 1000}, {800, 640}, {640, 500}, {500, 640}, {640, 837}
180 for (
auto sz : sizes) {
181 vpMatrix M = generateMatrix(sz.first, sz.second);
182 vpMatrix Mt_true = generateMatrixTranspose(sz.first, sz.second);
184 std::ostringstream oss;
185 oss << sz.first <<
"x" << sz.second;
187 BENCHMARK(oss.str().c_str()) {
189 REQUIRE(Mt == Mt_true);
194 oss << sz.first <<
"x" << sz.second;
195 oss <<
" - transposeIterateSrc(M)";
196 BENCHMARK(oss.str().c_str()) {
197 vpMatrix Mt = transposeIterateSrc(M);
198 REQUIRE(Mt == Mt_true);
203 oss << sz.first <<
"x" << sz.second;
204 oss <<
" - transposeIterateDst(M)";
205 BENCHMARK(oss.str().c_str()) {
206 vpMatrix Mt = transposeIterateDst(M);
207 REQUIRE(Mt == Mt_true);
212 oss << sz.first <<
"x" << sz.second;
213 oss <<
" - transposeTilingSO(M, tileSize=" << g_tileSize <<
")";
214 BENCHMARK(oss.str().c_str()) {
215 vpMatrix Mt = transposeTilingSO(M, g_tileSize);
216 REQUIRE(Mt == Mt_true);
221 oss << sz.first <<
"x" << sz.second;
222 oss <<
" - transposeTiling(M, tileSize=" << g_tileSize <<
")";
223 BENCHMARK(oss.str().c_str()) {
224 vpMatrix Mt = transposeTiling(M, g_tileSize);
225 REQUIRE(Mt == Mt_true);
229 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000) 230 cv::Mat matM(sz.first, sz.second, CV_64FC1);
232 for (
unsigned int i = 0; i < M.
getRows(); i++) {
233 for (
unsigned int j = 0; j < M.
getCols(); j++) {
234 matM.at<
double>(i, j) = M[i][j];
239 oss << sz.first <<
"x" << sz.second;
241 BENCHMARK(oss.str().c_str()) {
242 cv::Mat matMt = matM.
t();
247 #ifdef VISP_HAVE_EIGEN3 248 Eigen::MatrixXd eigenM(sz.first, sz.second);
250 for (
unsigned int i = 0; i < M.
getRows(); i++) {
251 for (
unsigned int j = 0; j < M.
getCols(); j++) {
252 eigenM(i, j) = M[i][j];
257 oss << sz.first <<
"x" << sz.second;
259 BENCHMARK(oss.str().c_str()) {
260 Eigen::MatrixXd eigenMt = eigenM.
transpose();
266 vpMatrix M = generateMatrix(11, 17);
267 vpMatrix Mt_true = generateMatrixTranspose(11, 17);
270 REQUIRE(Mt == Mt_true);
274 int main(
int argc,
char *argv[])
276 Catch::Session session;
279 using namespace Catch::clara;
280 auto cli = session.cli()
281 | Opt(g_runBenchmark)
284 | Opt(g_tileSize,
"tileSize")
292 session.applyCommandLine(argc, argv);
294 int numFailed = session.run();
Implementation of a matrix and operations on matrices.
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
unsigned int getCols() const
vpMatrix transpose() const
unsigned int getRows() const