Fastest way to calculate minimum euclidean distance between two matrices containing high dimensional vectors

后端 未结 3 818
孤街浪徒
孤街浪徒 2021-02-04 18:26

I started a similar question on another thread, but then I was focusing on how to use OpenCV. Having failed to achieve what I originally wanted, I will ask here exactly what I w

3条回答
  •  梦谈多话
    2021-02-04 18:48

    As you observed, your code is dominated by the matrix product that represents about 2.8e9 arithmetic operations. Yopu say that Matlab (or rather the highly optimized MKL) computes it in about 0.05s. This represents a rate of 57 GFLOPS showing that it is not only using vectorization but also multi-threading. With Eigen, you can enable multi-threading by compiling with OpenMP enabled (-fopenmp with gcc). On my 5 years old computer (2.66Ghz Core2), using floats and 4 threads, your product takes about 0.053s, and 0.16s without OpenMP, so there must be something wrong with your compilation flags. To summary, to get the best of Eigen:

    • compile in 64bits mode
    • use floats (doubles are twice as slow owing to vectorization)
    • enable OpenMP
    • if your CPU has hyper-threading, then either disable it or define the OMP_NUM_THREADS environment variable to the number of physical cores (this is very important, otherwise the performance will be very bad!)
    • if you have other task running, it might be a good idea to reduce OMP_NUM_THREADS to nb_cores-1
    • use the most recent compiler that you can, GCC, clang and ICC are best, MSVC is usually slower.

提交回复
热议问题