Last active
April 5, 2017 11:10
-
-
Save kketernality/651ff91416a36d1506fe583a54da4158 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <assert.h> | |
#include <time.h> | |
#include <iostream> | |
#include <algorithm> | |
#include <numeric> | |
#include <thrust/host_vector.h> | |
#include <thrust/device_vector.h> | |
#include <thrust/copy.h> | |
#include <thrust/sequence.h> | |
#include <thrust/functional.h> | |
#include <thrust/transform.h> | |
#include <thrust/sort.h> | |
int main(int argc, char** argv) { | |
assert(argc == 2 or argc == 3); | |
int test_size = atoi(argv[1]); | |
int test_iter = (argc == 2) ? 1 : atoi(argv[2]); | |
thrust::host_vector<int> H(test_size); | |
thrust::device_vector<int> D(test_size); | |
thrust::host_vector<int> E(test_size); | |
std::vector<double> speedups; | |
for (int iter = 0; iter < test_iter; iter++) { | |
thrust::sequence(H.begin(), H.end()); | |
std::random_shuffle(H.begin(), H.end()); | |
E = H; | |
clock_t t0 = clock(); | |
//thrust::sort(H.begin(), H.end()); slower! | |
std::sort(H.begin(), H.end()); | |
clock_t t1 = clock(); | |
clock_t t2 = clock(); | |
D = E; | |
thrust::sort(D.begin(), D.end()); | |
E = D; | |
clock_t t3 = clock(); | |
thrust::transform(H.begin(), H.end(), E.begin(), H.begin(), thrust::minus<int>()); | |
int errors = H.size() - thrust::count(H.begin(), H.end(), 0); | |
double cpu_time = 1000*((double)(t1-t0))/CLOCKS_PER_SEC; | |
double gpu_time = 1000*((double)(t3-t2))/CLOCKS_PER_SEC; | |
double speedup = cpu_time/gpu_time; | |
if (errors != 0) { | |
std::cout << "Test size: " << test_size << ", " | |
<< "Error count: " << errors << std::endl; | |
assert(errors == 0); | |
} | |
std::cout << "Iter: " << iter << ", " | |
<< "CPU: " << cpu_time << " ms, " | |
<< "GPU: " << gpu_time << " ms. " | |
<< "Speedup: " << speedup << std::endl; | |
speedups.push_back(speedup); | |
} | |
double speedup_avg = std::accumulate(speedups.begin(), speedups.end(), 0.0)/speedups.size(); | |
std::cout << "Average speedup: " << speedup_avg << std::endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <assert.h> | |
#include <time.h> | |
#include <iostream> | |
#include <algorithm> | |
#include <numeric> | |
#include <vector> | |
#include <thrust/host_vector.h> | |
#include <thrust/device_vector.h> | |
#include <thrust/copy.h> | |
#include <thrust/sequence.h> | |
#include <thrust/functional.h> | |
#include <thrust/transform.h> | |
#include <thrust/sort.h> | |
int main(int argc, char** argv) { | |
assert(argc == 1 or argc == 2 or argc == 3); | |
int test_size = (argc == 1) ? 4 : atoi(argv[1]); | |
int test_iter = (argc == 2) ? 1 : atoi(argv[2]); | |
std::vector<int> host_vec(test_size); | |
thrust::sequence(host_vec.begin(), host_vec.end()); | |
std::random_shuffle(host_vec.begin(), host_vec.end()); | |
for (int i = 0; i < host_vec.size(); i++) | |
std::cout << host_vec[i] << " "; | |
std::cout << std::endl; | |
thrust::device_vector<int> dev_vec(host_vec.size()); | |
thrust::copy(host_vec.begin(), host_vec.end(), dev_vec.begin()); | |
thrust::sort(dev_vec.begin(), dev_vec.end()); | |
thrust::copy(dev_vec.begin(), dev_vec.end(), host_vec.begin()); | |
for (int i = 0; i < host_vec.size(); i++) | |
std::cout << host_vec[i] << " "; | |
std::cout << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment