Skip to content

Instantly share code, notes, and snippets.

@sasekazu
Created November 6, 2014 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sasekazu/34b8dcdbdfd4175246b5 to your computer and use it in GitHub Desktop.
Save sasekazu/34b8dcdbdfd4175246b5 to your computer and use it in GitHub Desktop.
Thrust sort time measure
/*
* main.cu
*
* Created on: 2014/11/05
* Author: sase
*/
/*
* tbbTest.cpp
*
* Created on: 2014/11/05
* Author: sase
*/
#include <iostream>
#include <math.h>
#include <sys/time.h>
#include <vector>
#include <algorithm>
#include <numeric>
#include <thrust/device_vector.h>
#include <thrust/sort.h>
using std::cout;
using std::endl;
using std::vector;
double gettimeofday_sec(){
struct timeval t;
gettimeofday(&t, NULL);
return (double)t.tv_sec + (double)t.tv_usec * 1e-6;
}
void SortExample(const int n) {
const int itrmax = 100;
vector<int> a(n), b(n);
vector<double> timeSTL, timeTHRUST;
srand((unsigned) time(NULL));
for(int itr=0; itr<itrmax; ++itr){
// Set random array
for(int i=0; i<n; ++i){
a[i] = rand()%100;
}
std::copy(a.begin(), a.end(), b.begin());
thrust::device_vector<int> d_a(a.data(), a.data()+n);
double tstart = gettimeofday_sec();
// thrust
thrust::sort(d_a.begin(), d_a.end());
double tend = gettimeofday_sec();
// STL
std::sort(b.begin(), b.end());
double tendSTL = gettimeofday_sec();
timeTHRUST.push_back(tend-tstart);
timeSTL.push_back(tendSTL-tend);
}
cout << n << " "
<< std::accumulate(timeTHRUST.begin(), timeTHRUST.end(), 0.0)/(double)itrmax << " "
<< std::accumulate(timeSTL.begin(), timeSTL.end(), 0.0)/(double)itrmax << endl;
}
int main(){
// int size = 100000;
// SortExample(size);
for(int size=10000; size<=50000; size+=10000){
SortExample(size);
}
cout << "warmup done" << endl;
for(int size=10000; size<=500000; size+=10000){
SortExample(size);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment