Skip to content

Instantly share code, notes, and snippets.

@schiller-manuel
Created December 24, 2015 00:14
Show Gist options
  • Select an option

  • Save schiller-manuel/de9b3610af4391aadc26 to your computer and use it in GitHub Desktop.

Select an option

Save schiller-manuel/de9b3610af4391aadc26 to your computer and use it in GitHub Desktop.
#include <thrust/zip_function.h>
#include <thrust/tuple.h>
#include <thrust/iterator/zip_iterator.h>
#include <thrust/iterator/transform_iterator.h>
#include <thrust/functional.h>
#include <thrust/copy.h>
#include <iostream>
#include <iterator>
namespace demo
{
struct sum
{
template <typename T1, typename T2, typename T3>
auto operator()(T1 x, T2 y, T3 z) -> decltype(x+y+z)
{
return x+y+z;
}
};
int sum3ints(int x, int y, int z)
{
return x+y+z;
}
} // end demo
int main()
{
auto tuple = thrust::make_tuple(1, 2, 3);
{
auto zip_fun = thrust::make_zip_function(demo::sum3ints);
std::cout << "result = " << zip_fun(tuple) << std::endl;
}
{
auto zip_fun = thrust::make_zip_function(demo::sum());
std::cout << "result = " << zip_fun(tuple) << std::endl;
}
{
using namespace thrust::placeholders;
auto zip_fun = thrust::make_zip_function(_1 + _2 + _3);
std::cout << "result = " << zip_fun(tuple) << std::endl;
const std::size_t N = 4;
int values_1[] = {1,1,1,1};
int values_2[] = {2,2,2,2};
int values_3[] = {3,3,3,3};
auto zip_it = thrust::make_zip_iterator(
thrust::make_tuple(
std::begin(values_1),
std::begin(values_2),
std::begin(values_3)
)
);
auto transform_it = thrust::make_transform_iterator(zip_it, zip_fun);
thrust::copy(transform_it, transform_it+N, std::ostream_iterator<int>(std::cout, "\t"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment