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 @@ ...@@ -3,90 +3,102 @@
#include <iomanip> #include <iomanip>
#include <TNL/Containers/Array.h> #include <TNL/Containers/Array.h>
#include "../../src/util/timer.h"
//---------------------------
#include "../../src/bitonicSort/bitonicSort.h" #include "../../src/bitonicSort/bitonicSort.h"
#include "../../src/util/timer.h" #define SORTERFUNCTION bitonicSort
//---------------------------
using namespace TNL; using namespace TNL;
using namespace TNL::Containers; using namespace TNL::Containers;
using namespace std; 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); Array<int, Devices::Cuda> arr(vec.size());
for(int pow = 10; pow <= 20; pow++) 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; return accumulate(resAcc.begin(), resAcc.end(), 0.0) / resAcc.size();
vector<double> resAcc; }
//sorted sequence double sorted(int size)
{ {
arr = vec; vector<int> vec(size);
auto view = arr.getView(); iota(vec.begin(), vec.end(), 0);
return measure(vec);
}
{ double random(int size)
TIMER t([&](double res){resAcc.push_back(res);}); {
bitonicSort(view); srand(size);
}
}
//almost sorted sequence vector<int> vec(size);
{ iota(vec.begin(), vec.end(), 0);
for(int i = 0; i < 3; i++) random_shuffle(vec.begin(), vec.end());
{
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);
}
}
//decreasing sequence return measure(vec);
{ }
for(size_t i = 0; i < size; i++)
vec[i] = -i;
arr = vec;
auto view = arr.getView();
{ double almostSorted(int size)
TIMER t([&](double res){resAcc.push_back(res);}); {
bitonicSort(view); vector<int> vec(size);
} iota(vec.begin(), vec.end(), 0);
} for(int i = 0; i < 3; i++) //swaps 3 times in array
{
//random sequence int s = rand() % (size - 3);
{ std::swap(vec[s], vec[s + 1]);
random_shuffle(vec.begin(), vec.end()); }
arr = vec; return measure(vec);
auto view = arr.getView(); }
{ double decreasing(int size)
TIMER t([&](double res){resAcc.push_back(res);}); {
bitonicSort(view); 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 << "2^" << pow << delim;
cout << fixed; cout << fixed << setprecision(3);
cout << setprecision(3); cout << random(size) << delim;
cout << (accumulate(resAcc.begin(), resAcc.end(), 0.0) / resAcc.size()) << " ms" << endl; cout << sorted(size) << delim;
cout << almostSorted(size) << delim;
cout << decreasing(size) << delim;
cout << endl;
} }
return 0; 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