From aae3557936624f40aad3440c0e5f95a6db4a8f7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?El=20Kharroubi=20Micha=C3=ABl?= <michael.el-kharroubi@hesge.ch> Date: Mon, 19 Feb 2024 16:22:03 +0100 Subject: [PATCH] Future exemple works --- Examples/future.cpp | 36 +++++++++++++++++++++++++++--------- Examples/makefile | 7 +++++-- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/Examples/future.cpp b/Examples/future.cpp index f0589c4..a532d55 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 d90b4c6..a6054fd 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 -- GitLab