Commit 1fdd8c9a authored by Lucas Serrano's avatar Lucas Serrano
Browse files

Benchmark tools

parent 28925231
#!/bin/bash
export REPEAT=20
export PROG="a.out"
export FILE="gemm"
export TIME=/usr/bin/time
rm -f ${FILE}_simple.res
rm -f ${FILE}_eigen.res
rm -f ${FILE}_custom.res
for size in $(seq 3 8); do
echo "Size=$size"
echo -e "\t Simple"
g++ gemm_simple.cpp -DSIZE=$size -O3 -march=native
for i in $(seq 1 ${REPEAT}); do
${TIME} -f "%U, " ./a.out 2> >(tr -d '\n' | tee -a ${FILE}_simple.res) && echo
done
echo >> ${FILE}_simple.res
echo -e "\t Eigen"
g++ gemm_eigen.cpp -DSIZE=$size -O3 -march=native
for i in $(seq 1 ${REPEAT}); do
${TIME} -f "%U, " ./a.out 2> >(tr -d '\n' | tee -a ${FILE}_eigen.res) && echo
done
echo >> ${FILE}_eigen.res
echo -e "\t Custom"
g++ gemm_custom.cpp -I ${BOOST_SIMD_ROOT} -DSIZE=$size -O3 -march=native
for i in $(seq 1 ${REPEAT}); do
${TIME} -f "%U, " ./a.out 2> >(tr -d '\n' | tee -a ${FILE}_custom.res) && echo
done
echo >> ${FILE}_custom.res
done
#include<iostream>
#include "gemm_header.h"
#include "../fast_matrix.hpp"
int main(int argc, char **argv) {
alignas(32) float a[SIZE*SIZE];
alignas(32) float b[SIZE*SIZE];
for (int i=0; i<SIZE; i++) {
for (int j=0; j<SIZE; j++) {
a[i*SIZE+j] = i+j;
}
}
for (int i=0; i<SIZE; i++) {
for (int j=0; j<SIZE; j++) {
float val;
if (i == 0 && j == 1) val = -1;
else if (i == 1 && j == 0) val = 1;
else if (i > 1 && i == j && i % 2) val = -1;
else if (i > 1 && i == j && !(i % 2)) val = 1;
else val = 0;
b[i*SIZE+j] = val;
}
}
using M = BaseMatrix<float, SIZE, VECSIZE>;
M pa(a), pb(b), pc;
for (unsigned int i=0; i < REPEAT; i++) {
matrix_mul_m_m<M>(pa, pb, pc);
matrix_mul_m_m<M>(pb, pc, pa);
}
std::cout << pa << std::endl;
return 0;
}
#include<eigen3/Eigen/Dense>
#include<iostream>
#include "gemm_header.h"
int main(int argc, char **argv) {
Eigen::Matrix<float, SIZE, SIZE> pa, pb, pc;
for (int i=0; i<SIZE; i++) {
for (int j=0; j<SIZE; j++) {
pa(i,j) = i+j;
}
}
for (int i=0; i<SIZE; i++) {
for (int j=0; j<SIZE; j++) {
float val;
if (i == 0 && j == 1) val = -1;
else if (i == 1 && j == 0) val = 1;
else if (i > 1 && i == j && i % 2) val = -1;
else if (i > 1 && i == j && !(i % 2)) val = 1;
else val = 0;
pb(i,j) = val;
}
}
for (unsigned int i=0; i < REPEAT; i++) {
pc = pa * pb;
pa = pb * pc;
}
std::cout << pa << std::endl;
return 0;
}
#define REPEAT 200000000
#ifndef SIZE
#define SIZE 4
#endif
#ifndef VECSIZE
#define VECSIZE 8
#endif
#include <iostream>
#include "gemm_header.h"
template <typename T>
void print_matrix(T* matrix, int n, int m) {
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
std::cout << matrix[i*m+j] << (j == m-1 ? "" : ", ");
}
std::cout << std::endl;
}
}
template <typename T, int N>
void gemm(T *a, T *b, T*c) {
for (int i=0; i < N; i++) {
for (int j=0; j < N; j++) {
T res = 0;
for (int k=0; k < N; k++) res += a[i*N+k] * b[k*N+j];
c[i*N+j] = res;
}
}
}
int main(int argc, char **argv) {
alignas(32) float a[SIZE*SIZE];
alignas(32) float b[SIZE*SIZE];
alignas(32) float c[SIZE*SIZE];
for (int i=0; i<SIZE; i++) {
for (int j=0; j<SIZE; j++) {
a[i*SIZE+j] = i+j;
}
}
for (int i=0; i<SIZE; i++) {
for (int j=0; j<SIZE; j++) {
float val;
if (i == 0 && j == 1) val = -1;
else if (i == 1 && j == 0) val = 1;
else if (i > 1 && i == j && i % 2) val = -1;
else if (i > 1 && i == j && !(i % 2)) val = 1;
else val = 0;
b[i*SIZE+j] = val;
}
}
for (unsigned int i=0; i<REPEAT; i++) { // Computation Loop
gemm<float, SIZE>(a, b, c);
gemm<float, SIZE>(b, c, a);
}
print_matrix<float>(a, SIZE, SIZE);
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment