Skip to content

Instantly share code, notes, and snippets.

@pavanky
Last active August 29, 2015 14:18
Show Gist options
  • Save pavanky/6f18bb70dbc805280d0b to your computer and use it in GitHub Desktop.
Save pavanky/6f18bb70dbc805280d0b to your computer and use it in GitHub Desktop.
timing arrayfire
#include <iostream>
#include <limits>
#include <arrayfire.h>
using namespace std;
using namespace af;
int comp(const void * elem1, const void * elem2) {
int f = *((int*)elem1);
int s = *((int*)elem2);
if (f > s) return 1;
if (f < s) return -1;
return 0;
}
void addOnDevice(const af::array lhs,const af::array rhs, int count) {
double totalTime = 0;
af::timer start = af::timer::start();
for (int idx = 0; idx < count; idx++) {
af::array result = lhs + rhs;
result.eval();
}
af::sync();
totalTime = af::timer::stop(start);
printf("function addOnDevice took an average of %g seconds to add %d elements after %d iterations\n", totalTime/count, lhs.elements(), count);
}
void addOnHost(int* lhs, int* rhs, int n, int count) {
int* result = (int *)malloc(sizeof(int) * n);
double totalTime = 0;
af::timer start = af::timer::start();
for (int idx = 0; idx < count; idx++) {
for (int i = 0; i < n; i++) {
result[i] = lhs[i] + rhs[i];
}
}
totalTime += af::timer::stop(start);
free(result);
printf("function addOnHost took an average of %g seconds to add %d elements after %d iterations\n", totalTime/count, n, count);
}
int main()
{
af::deviceset(0);
af::info();
int n = 50e6;
int iterations = 10;
// allocate and populate the host data
int* hdata1 = (int *)malloc(sizeof(int) * n);
for (int i = 0; i < n; i++) {
hdata1[i] = int(rand() % 20);
}
int* hdata2 = (int *)malloc(sizeof(int) * n);
for (int i = 0; i < n; i++) {
hdata2[i] = int(rand() % 20);
}
// copy the same data to the devicw
af::array ddata1(n, 1, hdata1, af::afHost);
af::array ddata2(n, 1, hdata2, af::afHost);
af::sync();
addOnHost(hdata1, hdata2, n, iterations);
addOnDevice(ddata1, ddata2, iterations);
// manage memory
free(hdata1);
free(hdata2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment