11.2.3 : Le main.cpp
Voici le main.cpp : Commençons par les includes standards :
1 2 3 4 5 6 7 8 |
#include <iostream> #include <ranges> #include <vector> #include <numeric> #include <ranges> #include <execution> #include <algorithm> |
Définissons notre fonction principale :
1 |
int main(){ |
Déclarons quelques tailles de vecteurs :
1 2 3 |
size_t N(2), M(5); size_t nbElement(N*M); |
Puis les vecteurs :
1 2 3 |
std::vector<float> vecInput(nbElement), vecOutput(nbElement); std::iota(vecInput.begin(), vecInput.end(), 0); |
Des vues de vecteurs :
1 2 |
std::span A{vecInput.data(), N * M}; std::span B{vecOutput.data(), M * N}; |
Puis notre produit cartésien :
1 2 3 4 5 6 7 8 9 |
auto v = std::views::cartesian_product(std::views::iota(0lu, N), std::views::iota(0lu, M)); std::for_each(std::execution::par_unseq, std::begin(v), std::end(v), [=] (auto idx) { auto [i, j] = idx; B[i + j * N] = A[i * M + j]; } ); |
Affichons les résultats :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
for (const auto & [n, m] : v){ std::cout << "n = " << n << ", m = " << m << std::endl; } std::cout << "vecInput = "; for(std::vector<float>::iterator it(vecInput.begin()); it != vecInput.end(); ++it){ std::cout << (*it) << ", "; } std::cout << std::endl; std::cout << "vecOutput = "; for(std::vector<float>::iterator it(vecOutput.begin()); it != vecOutput.end(); ++it){ std::cout << (*it) << ", "; } std::cout << std::endl; return 0; } |
Le fichier main.cpp complet :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#include <iostream> #include <ranges> #include <vector> #include <numeric> #include <ranges> #include <execution> #include <algorithm> int main(){ size_t N(2), M(5); size_t nbElement(N*M); std::vector<float> vecInput(nbElement), vecOutput(nbElement); std::iota(vecInput.begin(), vecInput.end(), 0); std::span A{vecInput.data(), N * M}; std::span B{vecOutput.data(), M * N}; auto v = std::views::cartesian_product(std::views::iota(0lu, N), std::views::iota(0lu, M)); std::for_each(std::execution::par_unseq, std::begin(v), std::end(v), [=] (auto idx) { auto [i, j] = idx; B[i + j * N] = A[i * M + j]; } ); for (const auto & [n, m] : v){ std::cout << "n = " << n << ", m = " << m << std::endl; } std::cout << "vecInput = "; for(std::vector<float>::iterator it(vecInput.begin()); it != vecInput.end(); ++it){ std::cout << (*it) << ", "; } std::cout << std::endl; std::cout << "vecOutput = "; for(std::vector<float>::iterator it(vecOutput.begin()); it != vecOutput.end(); ++it){ std::cout << (*it) << ", "; } std::cout << std::endl; return 0; } |
Le fichier main.cpp est disponible ici.