This is an old revision of the document!
#include <algorithm>
#include <cstring>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include “SortingBenchmark.h”
#include “hybridsort/inc/hybridsort.h”
#include “cudpp/radixsort/inc/gpuradixsort.h”
#include “gpuqsort/inc/gpuqsort.h”
const std::string SortingBenchmark::algNames[] = { “bitonic”, “radix”, “radix_merge”, “hybrid”, “quick” };
#define ALGORITHMS_COUNT 5
SortingBenchmark::SortingBenchmark(const char* dataBase, bool consoleOutput)
{
mConsoleOutput = consoleOutput;
#ifdef SQLITE3
if(sqlite3_open(dataBase, &mDataBase))
{
fprintf(stderr, “Can't open database: %s\n”, sqlite3_errmsg(mDataBase));
sqlite3_close(mDataBase);
exit(1);
}
char* createTableCmd = “CREATE TABLE results (size int, algorithm varchar, totalTime float, gpuTime float, uid int);”;
sqlite3_exec(mDataBase, createTableCmd, 0, 0, &mSqlError);
#endif
}
void SortingBenchmark::runBenchmark(int size, int numSeq, int repeat, int uid)
{
float* data = new float[size];
float* dataCopy = new float[size];
unsigned int* dataIntCopy = new unsigned int[size];
warmUp();
for (int i = 0; i < numSeq; i++)
{
fillRandomly(data, size);
for (int alg = 0; alg < ALGORITHMS_COUNT; alg++)
{
for (int k = 0; k < repeat; k++)
{
double gpuTime, totalTime;
memcpy(dataCopy, data, sizeof(float) * size);
memcpy(dataIntCopy, data, sizeof(float) * size);
sort(dataCopy, dataIntCopy, size, &gpuTime, &totalTime, alg);
saveResult(gpuTime, totalTime, size, algNames[alg].c_str(), uid);
}
}
}
delete [] data;
delete [] dataCopy;
delete [] dataIntCopy;
}
void SortingBenchmark::warmUp()
{
const int size = 65536;
int ddata[size];
size_t memSize = size * sizeof(int);
cudaMalloc((void**) &ddata, memSize);
cudaFree(ddata);
}
void SortingBenchmark::saveResult(double gpuTime, double totalTime, int size, const char* algorithm, int uid)
{
#ifdef SQLITE3
char command[2048];
sprintf(command,“insert into results values (%d,\”%s\“,%f,%f,%d);\n”,size, algorithm, totalTime, gpuTime, uid);
int rc = sqlite3_exec(mDataBase, command, 0, 0, &mSqlError);
Exit if we failed to insert data.
if (rc!=SQLITE_OK)
{
fprintf(stderr, “SQL error: %s\n”, mSqlError);
sqlite3_free(mSqlError);
sqlite3_close(mDataBase);
exit(1);
}
#endif
if (mConsoleOutput)
printf(“Size: %d Algorithm: %s Total time: %f Gpu time: %f\n”, size, algorithm, totalTime, gpuTime);
}
void SortingBenchmark::sort(float* data, unsigned int* dataAsInt, int size, double* gpuTime, double* totalTime, int algorithm)
{
struct timeval starttime, endtime;
gettimeofday(&starttime,0);
switch (algorithm)
{
case 0: break;
case 1: cudppRadixMergeSortf(data, size, gpuTime); break;
case 2: cudppRadixSortf(data, size, gpuTime); break;
case 3: hybridsort(data, size, gpuTime); break;
case 4: gpuqsort(dataAsInt, size, gpuTime); break;
}
gettimeofday(&endtime,0);
*totalTime = (endtime.tv_sec - starttime.tv_sec)*1000.0 + (endtime.tv_usec - starttime.tv_usec)/1000.0;
}
void SortingBenchmark::fillRandomly(float* data, int size)
{
for (int i = 0; i < size; i++) data[i] = (float) rand();
}''