Skip to content

Instantly share code, notes, and snippets.

@anirul
Last active August 29, 2015 14:20
Show Gist options
  • Save anirul/bd86b531cc227c4118f5 to your computer and use it in GitHub Desktop.
Save anirul/bd86b531cc227c4118f5 to your computer and use it in GitHub Desktop.
OpenCV mat mult
#include <iostream>
#include <vector>
#include <iomanip>
#include <chrono>
#include "opencv2/highgui/highgui.hpp"
#ifdef USE_CUBLAS
#include "opencv2/gpu/gpu.hpp"
#endif
#ifdef USE_CLBLAS
#include "opencv2/ocl/ocl.hpp"
#endif
#include "opencv2/video/video.hpp"
using namespace cv;
#ifdef USE_CUBLAS
using namespace cv::gpu;
#endif
#ifdef USE_CLBLAS
using namespace cv::ocl;
#endif
const unsigned int NB_POINTS = 2048;
int main(int ac, char** av) {
try {
for (int i = 0; i < 10; ++i) {
Mat mat1(NB_POINTS, 128, CV_32FC1);
Mat mat2(128, NB_POINTS, CV_32FC1);
Mat mat3;
Mat result;
auto start = std::chrono::system_clock::now();
gemm(mat1, mat2, 1.0, mat3, 1.0, result);
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> total = end - start;
std::cout << "time (CPU) : " << total.count() << std::endl;
}
#ifdef USE_CUBLAS
for (int i = 0; i < 10; ++i) {
GpuMat mat1(NB_POINTS, 128, CV_32FC1);
GpuMat mat2(128, NB_POINTS, CV_32FC1);
GpuMat mat3;
GpuMat result;
auto start = std::chrono::system_clock::now();
cv::gpu::gemm(mat1, mat2, 1.0, mat3, 1.0, result);
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> total = end - start;
std::cout << "time (CUDA) : " << total.count() << std::endl;
}
#endif
#ifdef USE_CLBLAS
for (int i = 0; i < 10; ++i) {
oclMat mat1(NB_POINTS, 128, CV_32FC1);
oclMat mat2(128, NB_POINTS, CV_32FC1);
oclMat mat3;
oclMat result;
auto start = std::chrono::system_clock::now();
cv::ocl::gemm(mat1, mat2, 1.0, mat3, 1.0, result);
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> total = end - start;
std::cout << "time (OpenCL) : " << total.count() << std::endl;
}
#endif
} catch (Exception& ex) {
std::cout << "exception(CV) : " << ex.what() << std::endl;
}
return 0;
}
@anirul
Copy link
Author

anirul commented May 6, 2015

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D CUDA_GENERATION=Kepler -DWITH_CUBLAS=ON -DWITH_OPENCLAMDBLAS=ON ..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment