Skip to content
Snippets Groups Projects
Commit aae35579 authored by Michaël El Kharroubi's avatar Michaël El Kharroubi :satellite:
Browse files

Future exemple works

parent 90d962e6
No related branches found
No related tags found
No related merge requests found
#include <array> #include <execution>
#include <iomanip>
#include <iostream> #include <iostream>
#include <ranges> #include <ranges>
#include <vector> #include <vector>
...@@ -7,22 +8,39 @@ ...@@ -7,22 +8,39 @@
#include "includes/cartesian_product.hpp" #include "includes/cartesian_product.hpp"
#include "includes/mdspan.hpp" #include "includes/mdspan.hpp"
constexpr int dim_x = 5; constexpr int dim_x = 8;
constexpr int dim_y = 5; constexpr int dim_y = 8;
int main() { 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 xs = std::views::iota(0, dim_x);
auto ys = std::views::iota(0, dim_y); 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 idxs = std::views::cartesian_product(xs, ys);
auto md = std::experimental::mdspan{data.data(), dim_x, dim_y}; // We perform our operations on GPU
std::for_each(std::execution::par_unseq,
std::for_each(idxs.begin(), idxs.end(), [&md](auto tup) { idxs.begin(),
const auto& [x, y] = tup; idxs.end(),
std::cout << x << "," << y << std::endl; // 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"; std::cout << "\n";
......
.PHONY:clean all .PHONY:clean all
CC=nvc++
FLAGS=--std=c++23 -stdpar=gpu
INCLUDES_WARNINGS:=--diag_suppress useless_using_declaration INCLUDES_WARNINGS:=--diag_suppress useless_using_declaration
all: artefacts/future all: artefacts/future
artefacts/future: artefacts/future.o artefacts/future: artefacts/future.o
nvc++ $^ -o $@ $(CC) $(FLAGS) $^ -o $@
artefacts/%.o: %.cpp artefacts/%.o: %.cpp
nvc++ --std=c++23 $(INCLUDES_WARNINGS) -c $^ -o $@ $(CC) $(FLAGS) $(INCLUDES_WARNINGS) -c $^ -o $@
clean: clean:
rm -vf artefacts/** rm -vf artefacts/**
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment