Skip to content

Instantly share code, notes, and snippets.

@9prady9
Last active July 7, 2020 03:35
Show Gist options
  • Save 9prady9/9b17da1218574d0a69d3d3eeb0a5488d to your computer and use it in GitHub Desktop.
Save 9prady9/9b17da1218574d0a69d3d3eeb0a5488d to your computer and use it in GitHub Desktop.
bash script and simple c++ program that tests results of bare mkl, Eigen with mkl and ArrayFire
set -x
export ITOOLS_ROOT=/opt/intel
export MKLROOT=${ITOOLS_ROOT}/mkl
export AF_PATH=/home/pradeep/gitroot/ArrayFireWorkspace/worktrees/v3.7/build/pkg
mkl_options="-lmkl_intel_ilp64 -lmkl_intel_thread -lmkl_core -liomp5 -lpthread -lm -ldl"
inc_path_options="-I${MKLROOT}/include -I${AF_PATH}/include -I/usr/include/eigen3"
lib_path_options="-L${MKLROOT}/lib/intel64 -L${ITOOLS_ROOT}/lib/intel64 -L${AF_PATH}/lib"
g++ -DMKL_ILP64 -m64 ${inc_path_options} ${lib_path_options} -Wl,--no-as-needed ${mkl_options} -lafcpu main.cpp -o run_cpu
g++ -DMKL_ILP64 -m64 ${inc_path_options} ${lib_path_options} -Wl,--no-as-needed ${mkl_options} -lafcuda main.cpp -o run_cu
g++ -DMKL_ILP64 -m64 ${inc_path_options} ${lib_path_options} -Wl,--no-as-needed ${mkl_options} -lafopencl main.cpp -o run_ocl
echo "LIBRARY_PATH=${AF_PATH}/lib:${MKLROOT}/lib/intel64:${ITOOLS_ROOT}/lib/intel64"
echo "CPU"
LD_LIBRARY_PATH=${AF_PATH}/lib:${MKLROOT}/lib/intel64:${ITOOLS_ROOT}/lib/intel64 ./run_cpu
echo "CUDA"
LD_LIBRARY_PATH=${AF_PATH}/lib:${MKLROOT}/lib/intel64:${ITOOLS_ROOT}/lib/intel64 ./run_cu
echo "OPENCL"
LD_LIBRARY_PATH=${AF_PATH}/lib:${MKLROOT}/lib/intel64:${ITOOLS_ROOT}/lib/intel64 ./run_ocl
#include <iostream>
#include <random>
#include <cstdio>
#include <vector>
#include <arrayfire.h>
#include <mkl.h>
#define EIGEN_USE_MKL_ALL
#include <Eigen/Core>
void mkltest(const int N, const float *a, const float *b, float *c) {
cblas_sgemm(CblasColMajor, CblasNoTrans, CblasNoTrans,
N, N, N, 1.0f, a, N, b, N, 0.0f, c, N);
}
af::array aftest(const af::array& a, const af::array& b) {
return af::matmul(a, b);
}
int main()
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dis(0.0, 1.0);
const int n = 40;
const int size = n*n;
std::vector<float> values1(size);
std::vector<float> values2(size);
std::vector<float> directout(size);
for (int i = 0; i < size; ++i) {
values1[i] = dis(gen);
values2[i] = dis(gen);
}
Eigen::MatrixXf eig1 = Eigen::Map<Eigen::MatrixXf>(values1.data(), n, n);
Eigen::MatrixXf eig2 = Eigen::Map<Eigen::MatrixXf>(values2.data(), n, n);
af::array arr1( n , n , values1.data() );
af::array arr2( n , n , values2.data() );
mkltest(n, values1.data(), values2.data(), directout.data());
Eigen::MatrixXf eout = Eigen::Map<Eigen::MatrixXf>(directout.data(), n, n);
Eigen::MatrixXf eigm = eig1 * eig2;
af::array aout = aftest(arr1, arr2);
std::cout << "Bare MKL: " << eout.array().sum() << std::endl;
std::cout << "Eigen with MKL: " << eigm.array().sum() << std::endl;
std::cout << "ArrayFire" << af::sum<float>( aout ) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment