Skip to content

Instantly share code, notes, and snippets.

@bryancatanzaro
Created July 20, 2012 17:35
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 bryancatanzaro/3152103 to your computer and use it in GitHub Desktop.
Save bryancatanzaro/3152103 to your computer and use it in GitHub Desktop.
Using uniform_sequence
#include <iostream>
#include <prelude/sequences/uniform_sequence.h>
#include <prelude/runtime/tags.h>
#include <thrust/host_vector.h>
#include <thrust/copy.h>
#include <thrust/iterator/counting_iterator.h>
typedef copperhead::cpp_tag Tag;
using copperhead::uniform_sequence;
using copperhead::slice;
typedef uniform_sequence<Tag, int, 1> two_d_array;
typedef uniform_sequence<Tag, int, 0> one_d_array;
template<typename Seq>
struct print_array_impl{};
template<typename Tag, typename T, int D>
struct print_array_impl<uniform_sequence<Tag, T, D> >{
static void fun(uniform_sequence<Tag, T, D> a) {
while(!a.empty()) {
print_array(a.next());
std::cout << std::endl;
}
}
};
template<typename Tag, typename T>
struct print_array_impl<uniform_sequence<Tag, T, 0> > {
static void fun(uniform_sequence<Tag, T, 0> a) {
while(!a.empty()) {
std::cout << a.next() << ' ';
}
}
};
template<typename Tag, typename T, int D>
void print_array(uniform_sequence<Tag, T, D> a) {
print_array_impl<uniform_sequence<Tag, T, D> >::fun(a);
}
int main() {
int width = 2;
int height = 3;
thrust::host_vector<int> storage(width * height);
thrust::counting_iterator<int> cnt(1);
thrust::copy(cnt, cnt + width * height, storage.begin());
int* m_d = thrust::raw_pointer_cast(&storage[0]);
one_d_array row_major_sub(2, 1, m_d);
two_d_array row_major(3, 2, row_major_sub);
print_array(row_major); std::cout << std::endl;
one_d_array col_major_sub(2, 2, m_d);
two_d_array col_major(3, 1, col_major_sub);
print_array(col_major); std::cout << std::endl;
two_d_array sliced = slice(row_major, 1, 2);
print_array(sliced); std::cout << std::endl;
one_d_array one_d(width * height, 1, m_d);
one_d_array one_d_sliced = slice(one_d, 2, 3);
print_array(one_d_sliced); std::cout << std::endl;
}
@bryancatanzaro
Copy link
Author

Output:
1 2
3 4
5 6

1 3
2 4
3 5

3 4
5 6

3 4 5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment