Skip to content

Instantly share code, notes, and snippets.

@ddemidov
Last active September 2, 2016 11:53
Show Gist options
  • Save ddemidov/a46ff9f58514fbddee59da1d0f3a0bb5 to your computer and use it in GitHub Desktop.
Save ddemidov/a46ff9f58514fbddee59da1d0f3a0bb5 to your computer and use it in GitHub Desktop.
#include <vexcl/vexcl.hpp>
template <int N, int M>
struct mat {
double v[N][M];
};
template <int N>
struct vec {
double v[N];
};
std::string math_h = R"(
template <int N, int M>
struct mat {
double v[N][M];
};
template <int N>
struct vec {
double v[N];
};
template <int N, int M>
vec<N> operator*(mat<N,M> A, vec<M> x) {
vec<N> y;
for(int i = 0; i < N; ++i) {
y.v[i] = 0;
for(int j = 0; j < M; ++j)
y.v[i] += A.v[i][j] * x.v[j];
}
return y;
}
template <int N>
double dot(vec<N> a, vec<N> b) {
double s = 0;
for(int i = 0; i < N; ++i) s += a.v[i] * b.v[i];
return s;
}
)";
namespace vex {
template <int N, int M>
struct is_cl_native< mat<N,M> > : std::true_type {};
template <int N, int M>
struct type_name_impl< mat<N,M> > {
static std::string get() {
std::ostringstream s;
s << "mat<" << N << "," << M << ">";
return s.str();
}
};
template <int N>
struct is_cl_native< vec<N> > : std::true_type {};
template <int N>
struct type_name_impl< vec<N> > {
static std::string get() {
std::ostringstream s;
s << "vec<" << N << ">";
return s.str();
}
};
};
int main() {
vex::Context ctx(vex::Filter::Env);
std::cout << ctx << std::endl;
vex::scoped_program_header ph(ctx, math_h);
vex::scoped_compile_options co(ctx, "-x clc++");
mat<2,2> A = {1, 1, 1, 1};
vec<2> x = {1, 1};
vex::vector< mat<2,2> > vA(ctx, 16);
vex::vector< vec<2> > vx(ctx, 16);
vex::vector< vec<2> > vy(ctx, 16);
vA = A;
vx = x;
vy = vA * vx;
vec<2> y = vy[0];
// [2 2]
std::cout << "[" << y.v[0] << " " << y.v[1] << "]" << std::endl;
vex::vector<double> p(ctx, 16);
p = 1;
p = dot(vx, vy) + dot(p,p);
// 5
std::cout << p[0] << std::endl;
}
cl_cpp: cl_cpp.cpp
g++ -o $@ $^ -Wno-ignored-attributes -I$(VEXCL_ROOT) -lOpenCL -lboost_system -lboost_filesystem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment