04 CPP Concepts
04 CPP Concepts
#include <sycl/sycl.hpp>
using namespace sycl;
q.submit([&](handler &h) {
auto x = x_buf.template get_access<access::mode::read>(h); // accessor x(x_buf, h, read_only);
auto y = y_buf.template get_access<access::mode::read_write>(h); // accessor y(y_buf, h, read_write);
h.parallel_for(N, [=](id<1> i) {
y[i] += a * x[i];
});
});
q.wait_and_throw();
}
SYCL and Kokkos are modern C++ with classes, templates, lambdas, …
Namespaces
int a = 1, b = 2;
int c = max(a, b);
int y;
int &z = y;
foo2(z);
std::cout << y << std::endl;
auto
auto can be used in variable declaration if the compiler can deduce the
type during compilation
auto a = 5;
sum = add(2); // 3 or 7?
Classes
public:
Particle(T x, T y) {
this->x = x;
this->y = y;
}
void print() {
std::cout << x << " " << y << std::endl;
}
};
Functors
Adder add{5};
int sum = add(2);
std::cout << "The sum is: " << sum << std::endl;
Error Handling
try {
if (y == 0) throw "Division by zero error";
std::cout << "x / y = " << x / y << std::endl;
} catch (const char* msg) {
std::cerr << "Error: " << msg << std::endl;
}
return 0;
}
Summary
SYCL and Kokkos are modern C++ aiming towards generic parallel
programming
Classes, templates, lambdas, …
Reusable, expressive, and efficient code