diff --git a/Examples/future.cpp b/Examples/future.cpp index f0589c418ff63535cb3470a04e59d7c0e0b6608c..a532d552ea9639daa8a92d5d5b632682122234cb 100644 --- a/Examples/future.cpp +++ b/Examples/future.cpp @@ -1,4 +1,5 @@ -#include <array> +#include <execution> +#include <iomanip> #include <iostream> #include <ranges> #include <vector> @@ -7,22 +8,39 @@ #include "includes/cartesian_product.hpp" #include "includes/mdspan.hpp" -constexpr int dim_x = 5; -constexpr int dim_y = 5; +constexpr int dim_x = 8; +constexpr int dim_y = 8; int main() { - std::array<float, dim_x * dim_y> data{0}; + std::vector<int> data{dim_x * dim_y}; + auto md = std::mdspan{data.data(), dim_x, dim_y}; + + // We create a view for x & y indexes auto xs = std::views::iota(0, dim_x); auto ys = std::views::iota(0, dim_y); + // We build the indexes using the cartesian product auto idxs = std::views::cartesian_product(xs, ys); - auto md = std::experimental::mdspan{data.data(), dim_x, dim_y}; - - std::for_each(idxs.begin(), idxs.end(), [&md](auto tup) { - const auto& [x, y] = tup; - std::cout << x << "," << y << std::endl; + // We perform our operations on GPU + std::for_each(std::execution::par_unseq, + idxs.begin(), + idxs.end(), + // Here we need to do a copy capture so that compiler knows that + // we will use the underlying memory on the GPU. + [md](auto tup) { + const auto& [x, y] = tup; + md(x, y) = x * y; + }); + + // We print the result, since it's performed on CPU, + // we can simply capture by reference. + std::for_each(xs.begin(), xs.end(), [&md, &ys](auto i) { + std::for_each(ys.begin(), ys.end(), [&md, i](auto j) { + std::cout << std::setw(3) << md(i, j); + }); + std::cout << "\n"; }); std::cout << "\n"; diff --git a/Examples/makefile b/Examples/makefile index d90b4c6b3ad46de4e77d8dfd96d70dae41e4fe42..a6054fd98f6ba1b7a7e855c1fe895c5e4cce0c05 100644 --- a/Examples/makefile +++ b/Examples/makefile @@ -1,14 +1,17 @@ .PHONY:clean all +CC=nvc++ +FLAGS=--std=c++23 -stdpar=gpu INCLUDES_WARNINGS:=--diag_suppress useless_using_declaration + all: artefacts/future artefacts/future: artefacts/future.o - nvc++ $^ -o $@ + $(CC) $(FLAGS) $^ -o $@ artefacts/%.o: %.cpp - nvc++ --std=c++23 $(INCLUDES_WARNINGS) -c $^ -o $@ + $(CC) $(FLAGS) $(INCLUDES_WARNINGS) -c $^ -o $@ clean: rm -vf artefacts/** \ No newline at end of file