Skip to content

Instantly share code, notes, and snippets.

@ddemidov
Created June 13, 2012 18:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ddemidov/2925718 to your computer and use it in GitHub Desktop.
Save ddemidov/2925718 to your computer and use it in GitHub Desktop.
Hello world in VexCL
#include <iostream>
#include <vector>
#include <vexcl/vexcl.hpp>
int main() {
try {
// Init VexCL context: grab one GPU with double precision.
vex::Context ctx(
vex::Filter::Type(CL_DEVICE_TYPE_GPU) &&
vex::Filter::DoublePrecision &&
vex::Filter::Count(1)
);
if (!ctx) throw std::runtime_error("GPUs with double precision not found");
std::cout << ctx << std::endl;
// Prepare input data.
const size_t N = 1 << 20;
std::vector<double> a(N, 1);
std::vector<double> b(N, 2);
std::vector<double> c(N);
// Allocate device vectors and transfer input data to device.
vex::vector<double> A(ctx.queue(), a);
vex::vector<double> B(ctx.queue(), b);
vex::vector<double> C(ctx.queue(), N);
// Launch kernel on compute device.
C = A + B;
// Get result back to host.
copy(C, c);
// Should get '3' here.
std::cout << c[42] << std::endl;
return 0;
} catch (const cl::Error &err) {
std::cerr << "OpenCL error: " << err << std::endl;
} catch (const std::exception &err) {
std::cerr << "Error: " << err.what() << std::endl;
}
return 1;
}
VEXCL_ROOT=/usr/include
hello: hello.cpp
g++ -std=c++0x -o hello hello.cpp -I$(VEXCL_ROOT) -lOpenCL -lboost_system
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment