Skip to content
Snippets Groups Projects
Commit 71e2e550 authored by Xuan Thang Nguyen's avatar Xuan Thang Nguyen
Browse files

benchmark change

parent 1b59a284
No related branches found
No related tags found
No related merge requests found
......@@ -3,90 +3,102 @@
#include <iomanip>
#include <TNL/Containers/Array.h>
#include "../../src/util/timer.h"
//---------------------------
#include "../../src/bitonicSort/bitonicSort.h"
#include "../../src/util/timer.h"
#define SORTERFUNCTION bitonicSort
//---------------------------
using namespace TNL;
using namespace TNL::Containers;
using namespace std;
typedef Devices::Cuda Device;
const int lowPow = 15, highLow = 22;
const int tries = 50;
int main()
double measure(const vector<int>&vec)
{
srand(2021);
for(int pow = 10; pow <= 20; pow++)
Array<int, Devices::Cuda> arr(vec.size());
vector<double> resAcc;
for(int i = 0; i < tries; i++)
{
int size =(1<< pow);
arr = vec;
auto view = arr.getView();
vector<int> vec(size);
iota(vec.begin(), vec.end(), 0);
{
TIMER t([&](double res){resAcc.push_back(res);});
SORTERFUNCTION(view);
}
}
Array<int, Device> arr;
vector<double> resAcc;
return accumulate(resAcc.begin(), resAcc.end(), 0.0) / resAcc.size();
}
//sorted sequence
{
arr = vec;
auto view = arr.getView();
double sorted(int size)
{
vector<int> vec(size);
iota(vec.begin(), vec.end(), 0);
return measure(vec);
}
{
TIMER t([&](double res){resAcc.push_back(res);});
bitonicSort(view);
}
}
double random(int size)
{
srand(size);
//almost sorted sequence
{
for(int i = 0; i < 3; i++)
{
int s = rand() % (size - 3);
std::swap(vec[s], vec[s + 1]);
}
arr = vec;
auto view = arr.getView();
{
TIMER t([&](double res){resAcc.push_back(res);});
bitonicSort(view);
}
}
vector<int> vec(size);
iota(vec.begin(), vec.end(), 0);
random_shuffle(vec.begin(), vec.end());
//decreasing sequence
{
for(size_t i = 0; i < size; i++)
vec[i] = -i;
arr = vec;
auto view = arr.getView();
return measure(vec);
}
{
TIMER t([&](double res){resAcc.push_back(res);});
bitonicSort(view);
}
}
//random sequence
{
random_shuffle(vec.begin(), vec.end());
double almostSorted(int size)
{
vector<int> vec(size);
iota(vec.begin(), vec.end(), 0);
for(int i = 0; i < 3; i++) //swaps 3 times in array
{
int s = rand() % (size - 3);
std::swap(vec[s], vec[s + 1]);
}
arr = vec;
auto view = arr.getView();
return measure(vec);
}
{
TIMER t([&](double res){resAcc.push_back(res);});
bitonicSort(view);
}
}
double decreasing(int size)
{
vector<int> vec(size);
for(size_t i = 0; i < size; i++)
vec[i] = -i;
return measure(vec);
}
int main()
{
string delim = "\t";
cout << "size" << delim;
cout << "random" << delim;
cout << "sorted" << delim;
cout << "almost" << delim;
cout << "decreasing" << delim;
cout << endl;
for(int pow = lowPow; pow <= highLow; pow++)
{
int size =(1<< pow);
vector<int> vec(size);
cout << "2^" << pow << " = ";
cout << fixed;
cout << setprecision(3);
cout << (accumulate(resAcc.begin(), resAcc.end(), 0.0) / resAcc.size()) << " ms" << endl;
cout << "2^" << pow << delim;
cout << fixed << setprecision(3);
cout << random(size) << delim;
cout << sorted(size) << delim;
cout << almostSorted(size) << delim;
cout << decreasing(size) << delim;
cout << endl;
}
return 0;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment