Skip to content

Instantly share code, notes, and snippets.

@IgorBaratta
Last active July 30, 2021 11:31
Show Gist options
  • Select an option

  • Save IgorBaratta/497046cbb17a575ef20563dab3bdebdb to your computer and use it in GitHub Desktop.

Select an option

Save IgorBaratta/497046cbb17a575ef20563dab3bdebdb to your computer and use it in GitHub Desktop.
#include <oneapi/dpl/algorithm>
#include <oneapi/dpl/execution>
#include <CL/sycl.hpp>
#include <vector>
int main(int argc, char *argv[])
{
std::size_t n = 100'000'000;
int nrep = 10;
sycl::queue queue{sycl::default_selector()};
auto policy = oneapi::dpl::execution::make_device_policy(queue);
double alpha = 1.0;
{
sycl::usm_allocator<double, sycl::usm::alloc::shared>
alloc(queue);
std::vector<double, decltype(alloc)> x(n, alloc);
std::vector<double, decltype(alloc)> y(n, alloc);
auto policy = oneapi::dpl::execution::make_device_policy(queue);
std::fill(policy, x.begin(), x.end(), 0.555);
std::fill(policy, y.begin(), y.end(), -0.555);
double *_x = x.data();
double *_y = y.data();
auto start = std::chrono::high_resolution_clock::now();
for (int j = 0; j < nrep; j++)
{
std::transform(policy, x.cbegin(), x.cend(), y.cbegin(), y.begin(),
[alpha](const double &vx, const double &vy)
{ return vx * alpha + vy; });
}
auto end = std::chrono::high_resolution_clock::now();
auto t = std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
std::cout << "Elapsed time " << t.count();
}
{
double *x = cl::sycl::malloc_device<double>(n, queue);
double *y = cl::sycl::malloc_device<double>(n, queue);
queue.fill<double>(x, 0.555, n);
queue.fill<double>(y, -0.555, n);
queue.wait();
auto start = std::chrono::high_resolution_clock::now();
for (int j = 0; j < nrep; j++)
{
std::transform(policy, x, x + n, y, y,
[alpha](const double &vx, const double &vy)
{ return vx * alpha + vy; });
}
auto end = std::chrono::high_resolution_clock::now();
auto t = std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
std::cout << "\nElapsed time " << t.count();
cl::sycl::free(x, queue);
cl::sycl::free(y, queue);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment