The algorithm in standard C++

The formula for the area of a circle is, as you probably remember from middle-school; . In english, it's radius squared times pi. Here it is in code:

auto circle_area_cpu(const Circle& c) {  const auto pi = 3.14f;  return c.r * c.r * pi;}

Using the circle_area_cpu() function, we can calculate the full area using std::transform() and std::reduce() in C++:

auto sum_circle_areas_cpu() { 
  constexpr auto n = 1024; 
  auto circles = make_circles(n); 
  auto areas = std::vector<float>(n);
  std::transform(circles.begin(), circles.end(), areas.begin(),     circle_area_cpu);   auto plus = std::plus<float>{};
  auto area = std::reduce(areas.begin(), ...

Get C++ High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.