38 #include <visp3/core/vpConfig.h>
40 #if defined(VISP_HAVE_CATCH2)
42 #include <catch_amalgamated.hpp>
44 #include <visp3/core/vpMatrix.h>
46 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
47 #include <opencv2/core.hpp>
50 #ifdef VISP_HAVE_EIGEN3
51 #include <Eigen/Dense>
54 #ifdef ENABLE_VISP_NAMESPACE
61 bool g_runBenchmark =
false;
64 vpMatrix generateMatrix(
unsigned int sz1,
unsigned int sz2)
68 for (
unsigned int i = 0; i < M.getRows(); i++) {
69 for (
unsigned int j = 0; j < M.getCols(); j++) {
70 M[i][j] = i * M.getCols() + j;
77 vpMatrix generateMatrixTranspose(
unsigned int sz1,
unsigned int sz2)
81 for (
unsigned int j = 0; j < M.getCols(); j++) {
82 for (
unsigned int i = 0; i < M.getRows(); i++) {
83 M[i][j] = j * M.getRows() + i;
96 for (
unsigned int i = 0; i < A.
getRows(); i++) {
98 for (
unsigned int j = 0; j < A.
getCols(); j++) {
112 for (
unsigned int j = 0; j < A.
getCols(); j++) {
113 double *coli = At[j];
114 for (
unsigned int i = 0; i < A.
getRows(); i++) {
128 for (
unsigned int i = 0; i < A.
getRows(); i += tileSize) {
129 for (
unsigned int j = 0; j < A.
getCols(); j++) {
130 for (
unsigned int b = 0; b < tileSize && i + b < A.
getRows(); b++) {
131 At[j][i + b] = A[i + b][j];
145 const int nrows =
static_cast<int>(A.
getRows());
146 const int ncols =
static_cast<int>(A.
getCols());
148 for (
int i = 0; i < nrows;) {
149 for (; i <= nrows - tileSize; i += tileSize) {
151 for (; j <= ncols - tileSize; j += tileSize) {
152 for (
int k = i; k < i + tileSize; k++) {
153 for (
int l = j; l < j + tileSize; l++) {
159 for (
int k = i; k < i + tileSize; k++) {
160 for (
int l = j; l < ncols; l++) {
166 for (; i < nrows; i++) {
167 for (
int j = 0; j < ncols; j++) {
178 TEST_CASE(
"Benchmark vpMatrix transpose",
"[benchmark]")
180 if (g_runBenchmark) {
181 const std::vector<std::pair<int, int> > sizes = {
182 {701, 1503}, {1791, 837}, {1201, 1201}, {1024, 1024}, {2000, 2000}, {10, 6}, {25, 6}, {100, 6}, {200, 6},
183 {500, 6}, {1000, 6}, {1500, 6}, {2000, 6}, {6, 10}, {6, 25}, {6, 100}, {6, 200}, {6, 500},
184 {6, 1000}, {6, 1500}, {6, 2000}, {640, 1000}, {800, 640}, {640, 500}, {500, 640}, {640, 837} };
186 for (
auto sz : sizes) {
187 vpMatrix M = generateMatrix(sz.first, sz.second);
188 vpMatrix Mt_true = generateMatrixTranspose(sz.first, sz.second);
190 std::ostringstream oss;
191 oss << sz.first <<
"x" << sz.second;
193 BENCHMARK(oss.str().c_str())
196 REQUIRE(Mt == Mt_true);
201 oss << sz.first <<
"x" << sz.second;
202 oss <<
" - transposeIterateSrc(M)";
203 BENCHMARK(oss.str().c_str())
205 vpMatrix Mt = transposeIterateSrc(M);
206 REQUIRE(Mt == Mt_true);
211 oss << sz.first <<
"x" << sz.second;
212 oss <<
" - transposeIterateDst(M)";
213 BENCHMARK(oss.str().c_str())
215 vpMatrix Mt = transposeIterateDst(M);
216 REQUIRE(Mt == Mt_true);
221 oss << sz.first <<
"x" << sz.second;
222 oss <<
" - transposeTilingSO(M, tileSize=" << g_tileSize <<
")";
223 BENCHMARK(oss.str().c_str())
225 vpMatrix Mt = transposeTilingSO(M, g_tileSize);
226 REQUIRE(Mt == Mt_true);
231 oss << sz.first <<
"x" << sz.second;
232 oss <<
" - transposeTiling(M, tileSize=" << g_tileSize <<
")";
233 BENCHMARK(oss.str().c_str())
235 vpMatrix Mt = transposeTiling(M, g_tileSize);
236 REQUIRE(Mt == Mt_true);
240 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
241 cv::Mat matM(sz.first, sz.second, CV_64FC1);
243 for (
unsigned int i = 0; i < M.
getRows(); i++) {
244 for (
unsigned int j = 0; j < M.
getCols(); j++) {
245 matM.at<
double>(i, j) = M[i][j];
250 oss << sz.first <<
"x" << sz.second;
252 BENCHMARK(oss.str().c_str())
254 cv::Mat matMt = matM.t();
259 #ifdef VISP_HAVE_EIGEN3
260 Eigen::MatrixXd eigenM(sz.first, sz.second);
262 for (
unsigned int i = 0; i < M.
getRows(); i++) {
263 for (
unsigned int j = 0; j < M.
getCols(); j++) {
264 eigenM(i, j) = M[i][j];
269 oss << sz.first <<
"x" << sz.second;
271 BENCHMARK(oss.str().c_str())
273 Eigen::MatrixXd eigenMt = eigenM.
transpose();
280 vpMatrix M = generateMatrix(11, 17);
281 vpMatrix Mt_true = generateMatrixTranspose(11, 17);
284 REQUIRE(Mt == Mt_true);
288 int main(
int argc,
char *argv[])
290 Catch::Session session;
291 auto cli = session.cli()
292 | Catch::Clara::Opt(g_runBenchmark)[
"--benchmark"](
"run benchmark?")
293 | Catch::Clara::Opt(g_tileSize,
"tileSize")[
"--tileSize"](
"Tile size?");
296 session.applyCommandLine(argc, argv);
298 int numFailed = session.run();
305 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