38 #include <visp3/core/vpConfig.h>
40 #ifdef VISP_HAVE_CATCH2
41 #define CATCH_CONFIG_ENABLE_BENCHMARKING
42 #define CATCH_CONFIG_RUNNER
45 #include <visp3/core/vpMatrix.h>
47 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
48 #include <opencv2/core.hpp>
51 #ifdef VISP_HAVE_EIGEN3
52 #include <Eigen/Dense>
55 #ifdef ENABLE_VISP_NAMESPACE
62 bool g_runBenchmark =
false;
65 vpMatrix generateMatrix(
unsigned int sz1,
unsigned int sz2)
69 for (
unsigned int i = 0; i < M.getRows(); i++) {
70 for (
unsigned int j = 0; j < M.getCols(); j++) {
71 M[i][j] = i * M.getCols() + j;
78 vpMatrix generateMatrixTranspose(
unsigned int sz1,
unsigned int sz2)
82 for (
unsigned int j = 0; j < M.getCols(); j++) {
83 for (
unsigned int i = 0; i < M.getRows(); i++) {
84 M[i][j] = j * M.getRows() + i;
97 for (
unsigned int i = 0; i < A.
getRows(); i++) {
99 for (
unsigned int j = 0; j < A.
getCols(); j++) {
113 for (
unsigned int j = 0; j < A.
getCols(); j++) {
114 double *coli = At[j];
115 for (
unsigned int i = 0; i < A.
getRows(); i++) {
129 for (
unsigned int i = 0; i < A.
getRows(); i += tileSize) {
130 for (
unsigned int j = 0; j < A.
getCols(); j++) {
131 for (
unsigned int b = 0; b < tileSize && i + b < A.
getRows(); b++) {
132 At[j][i + b] = A[i + b][j];
146 const int nrows =
static_cast<int>(A.
getRows());
147 const int ncols =
static_cast<int>(A.
getCols());
149 for (
int i = 0; i < nrows;) {
150 for (; i <= nrows - tileSize; i += tileSize) {
152 for (; j <= ncols - tileSize; j += tileSize) {
153 for (
int k = i; k < i + tileSize; k++) {
154 for (
int l = j; l < j + tileSize; l++) {
160 for (
int k = i; k < i + tileSize; k++) {
161 for (
int l = j; l < ncols; l++) {
167 for (; i < nrows; i++) {
168 for (
int j = 0; j < ncols; j++) {
179 TEST_CASE(
"Benchmark vpMatrix transpose",
"[benchmark]")
181 if (g_runBenchmark) {
182 const std::vector<std::pair<int, int> > sizes = {
183 {701, 1503}, {1791, 837}, {1201, 1201}, {1024, 1024}, {2000, 2000}, {10, 6}, {25, 6}, {100, 6}, {200, 6},
184 {500, 6}, {1000, 6}, {1500, 6}, {2000, 6}, {6, 10}, {6, 25}, {6, 100}, {6, 200}, {6, 500},
185 {6, 1000}, {6, 1500}, {6, 2000}, {640, 1000}, {800, 640}, {640, 500}, {500, 640}, {640, 837} };
187 for (
auto sz : sizes) {
188 vpMatrix M = generateMatrix(sz.first, sz.second);
189 vpMatrix Mt_true = generateMatrixTranspose(sz.first, sz.second);
191 std::ostringstream oss;
192 oss << sz.first <<
"x" << sz.second;
194 BENCHMARK(oss.str().c_str())
197 REQUIRE(Mt == Mt_true);
202 oss << sz.first <<
"x" << sz.second;
203 oss <<
" - transposeIterateSrc(M)";
204 BENCHMARK(oss.str().c_str())
206 vpMatrix Mt = transposeIterateSrc(M);
207 REQUIRE(Mt == Mt_true);
212 oss << sz.first <<
"x" << sz.second;
213 oss <<
" - transposeIterateDst(M)";
214 BENCHMARK(oss.str().c_str())
216 vpMatrix Mt = transposeIterateDst(M);
217 REQUIRE(Mt == Mt_true);
222 oss << sz.first <<
"x" << sz.second;
223 oss <<
" - transposeTilingSO(M, tileSize=" << g_tileSize <<
")";
224 BENCHMARK(oss.str().c_str())
226 vpMatrix Mt = transposeTilingSO(M, g_tileSize);
227 REQUIRE(Mt == Mt_true);
232 oss << sz.first <<
"x" << sz.second;
233 oss <<
" - transposeTiling(M, tileSize=" << g_tileSize <<
")";
234 BENCHMARK(oss.str().c_str())
236 vpMatrix Mt = transposeTiling(M, g_tileSize);
237 REQUIRE(Mt == Mt_true);
241 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
242 cv::Mat matM(sz.first, sz.second, CV_64FC1);
244 for (
unsigned int i = 0; i < M.
getRows(); i++) {
245 for (
unsigned int j = 0; j < M.
getCols(); j++) {
246 matM.at<
double>(i, j) = M[i][j];
251 oss << sz.first <<
"x" << sz.second;
253 BENCHMARK(oss.str().c_str())
255 cv::Mat matMt = matM.t();
260 #ifdef VISP_HAVE_EIGEN3
261 Eigen::MatrixXd eigenM(sz.first, sz.second);
263 for (
unsigned int i = 0; i < M.
getRows(); i++) {
264 for (
unsigned int j = 0; j < M.
getCols(); j++) {
265 eigenM(i, j) = M[i][j];
270 oss << sz.first <<
"x" << sz.second;
272 BENCHMARK(oss.str().c_str())
274 Eigen::MatrixXd eigenMt = eigenM.
transpose();
281 vpMatrix M = generateMatrix(11, 17);
282 vpMatrix Mt_true = generateMatrixTranspose(11, 17);
285 REQUIRE(Mt == Mt_true);
289 int main(
int argc,
char *argv[])
291 Catch::Session session;
294 using namespace Catch::clara;
295 auto cli = session.cli()
296 | Opt(g_runBenchmark)
299 | Opt(g_tileSize,
"tileSize")[
"--tileSize"](
"Tile size?");
305 session.applyCommandLine(argc, argv);
307 int numFailed = session.run();
317 int main() {
return EXIT_SUCCESS; }
unsigned int getCols() const
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
unsigned int getRows() const
Implementation of a matrix and operations on matrices.
vpMatrix transpose() const