diff --git a/README.md b/README.md index 3077356913a71d63c713997928ddeb83fa34b83f..d4ba98ec03eba9ace6f41dc0000c9e7584d6d09d 100644 --- a/README.md +++ b/README.md @@ -1,92 +1,13 @@ # GrayScottChallenge +## Step1 +What you get as a starting point: +- the naive code from Pierre Aubert +- an operational SyCL square program +- the ChatGPT proposal -## Getting started +Mix them all so to have a working SyCL implementation of the Gray-Scott reaction, with the same parameters and output as the original naive code, but hopefully faster... -To make it easy for you to get started with GitLab, here's a list of recommended next steps. +## Step2 -Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)! - -## Add your files - -- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files -- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command: - -``` -cd existing_repo -git remote add origin https://gitlab.in2p3.fr/chamont/grayscottchallenge.git -git branch -M main -git push -uf origin main -``` - -## Integrate with your tools - -- [ ] [Set up project integrations](https://gitlab.in2p3.fr/chamont/grayscottchallenge/-/settings/integrations) - -## Collaborate with your team - -- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/) -- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html) -- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically) -- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/) -- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html) - -## Test and Deploy - -Use the built-in continuous integration in GitLab. - -- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html) -- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/) -- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html) -- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/) -- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html) - -*** - -# Editing this README - -When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template. - -## Suggestions for a good README -Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information. - -## Name -Choose a self-explaining name for your project. - -## Description -Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors. - -## Badges -On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge. - -## Visuals -Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method. - -## Installation -Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection. - -## Usage -Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README. - -## Support -Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc. - -## Roadmap -If you have ideas for releases in the future, it is a good idea to list them in the README. - -## Contributing -State if you are open to contributions and what your requirements are for accepting them. - -For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self. - -You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser. - -## Authors and acknowledgment -Show your appreciation to those who have contributed to the project. - -## License -For open source projects, say how it is licensed. - -## Project status -If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers. diff --git a/Step1/ChatGpt/chatgpt-grayscott-alpaka.cpp b/Step1/ChatGpt/chatgpt-grayscott-alpaka.cpp new file mode 100644 index 0000000000000000000000000000000000000000..35b432708fc3d99cbf511600e35f0b1c844e40ba --- /dev/null +++ b/Step1/ChatGpt/chatgpt-grayscott-alpaka.cpp @@ -0,0 +1,116 @@ +// ChatGPT commence par refuser : Alpaka est une bibliothèque de programmation parallèle qui permet d'écrire des codes hautement parallèles de manière portable. Malheureusement, Alpaka ne prend pas en charge les équations différentielles spécifiques telles que l'équation de Gray-Scott. Cependant, je peux vous fournir un exemple de code en utilisant une autre bibliothèque, comme OpenMP ou CUDA, pour résoudre l'équation de Gray-Scott de manière parallèle. Faites-moi savoir si vous souhaitez voir un exemple utilisant l'une de ces bibliothèques. +// Apres trois tentatives, et en précisant que je veux une version GPU + +#include <iostream> +#include <alpaka/alpaka.hpp> + +// Définir les constantes de l'équation de Gray-Scott +constexpr float Du = 0.16f; +constexpr float Dv = 0.08f; +constexpr float F = 0.035f; +constexpr float k = 0.06f; + +// Définir les dimensions spatiales et temporelles +constexpr size_t Width = 512; +constexpr size_t Height = 512; +constexpr size_t NumIterations = 1000; + +// Définir la structure de grille +struct Grid { + alpaka::Vec<float, 2> u; + alpaka::Vec<float, 2> v; +}; + +int main() { + using Dim = alpaka::dim::DimInt<2>; + using Extent = alpaka::vec::Vec<size_t, 2>; + + // Créer des objets d'exécution Alpaka + alpaka::DevCuda devCuda; + alpaka::CtxDev<Dim> ctxDev(devCuda); + alpaka::QueueCudaSync<Dim> queue(ctxDev); + + // Allouer la mémoire de la grille sur le dispositif + Grid* gridDev; + alpaka::alloc::alloc<Grid, Dim, Extent>( + &gridDev, ctxDev, Extent{Width, Height}); + + // Initialiser la grille sur le dispositif avec des valeurs par défaut + alpaka::kernel::exec<Dim>( + queue, + alpaka::iter::iter<Dim>(Extent{Width, Height}), + [gridDev] ALPAKA_FN_HOST_ACC(const auto& idxDev) { + size_t i = alpaka::getIdx<Dim, alpaka::dim::Idx<0>>(idxDev); + size_t j = alpaka::getIdx<Dim, alpaka::dim::Idx<1>>(idxDev); + + gridDev[idxDev].u = 1.0f; + gridDev[idxDev].v = 0.0f; + }); + + // Boucle principale pour résoudre l'équation de Gray-Scott + for (size_t iteration = 0; iteration < NumIterations; ++iteration) { + // Paralléliser le calcul de la nouvelle valeur de la grille + alpaka::kernel::exec<Dim>( + queue, + alpaka::iter::iter<Dim>(Extent{Width, Height}), + [gridDev] ALPAKA_FN_HOST_ACC(const auto& idxDev) { + size_t i = alpaka::getIdx<Dim, alpaka::dim::Idx<0>>(idxDev); + size_t j = alpaka::getIdx<Dim, alpaka::dim::Idx<1>>(idxDev); + + float laplacianU = 0.0f; + float laplacianV = 0.0f; + + // Calculer le laplacien pour u et v + if (i > 0 && i < Width - 1 && j > 0 && j < Height - 1) { + laplacianU = gridDev[alpaka::idx::getIdx<Dim>(idxDev[0] - 1, idxDev[1])].u + + gridDev[alpaka::idx::getIdx<Dim>(idxDev[0] + 1, idxDev[1])].u + + gridDev[alpaka::idx::getIdx<Dim>(idxDev[0], idxDev[1] - 1)].u + + gridDev[alpaka::idx::getIdx<Dim>(idxDev[0], idxDev[1] + 1)].u - + 4.0f * gridDev[idxDev].u; + laplacianV = gridDev[alpaka::idx::getIdx<Dim>(idxDev[0] - 1, idxDev[1])].v + + gridDev[alpaka::idx::getIdx<Dim>(idxDev[0] + 1, idxDev[1])].v + + gridDev[alpaka::idx::getIdx<Dim>(idxDev[0], idxDev[1] - 1)].v + + gridDev[alpaka::idx::getIdx<Dim>(idxDev[0], idxDev[1] + 1)].v - + 4.0f * gridDev[idxDev].v; + } + + // Calculer les nouvelles valeurs de u et v + float deltaU = Du * laplacianU - gridDev[idxDev].u * gridDev[idxDev].v * gridDev[idxDev].v + + F * (1.0f - gridDev[idxDev].u); + float deltaV = Dv * laplacianV + gridDev[idxDev].u * gridDev[idxDev].v * gridDev[idxDev].v - + (F + k) * gridDev[idxDev].v; + + // Mettre à jour les valeurs de u et v + gridDev[idxDev].u += deltaU; + gridDev[idxDev].v += deltaV; + }); + } + + // Récupérer la grille résolue depuis le dispositif + Grid* gridHost = new Grid[Width * Height]; + alpaka::alloc::copy<Dim>( + queue, + gridHost, + gridDev, + Extent{Width, Height}); + + // Afficher les valeurs de la grille résolue + for (size_t i = 0; i < Width; ++i) { + for (size_t j = 0; j < Height; ++j) { + std::cout << "u[" << i << "][" << j << "] = " << gridHost[i * Height + j].u << std::endl; + std::cout << "v[" << i << "][" << j << "] = " << gridHost[i * Height + j].v << std::endl; + } + } + + // Libérer la mémoire + delete[] gridHost; + alpaka::alloc::dealloc<Dim>(gridDev, ctxDev); + + return 0; +} + + + + + + diff --git a/Step1/ChatGpt/chatgpt-grayscott-kokkos.cpp b/Step1/ChatGpt/chatgpt-grayscott-kokkos.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8cdfee8f14e7b89f345ece234c48745fe265510f --- /dev/null +++ b/Step1/ChatGpt/chatgpt-grayscott-kokkos.cpp @@ -0,0 +1,84 @@ +#include <Kokkos_Core.hpp> +#include <iostream> + +// Dimension du maillage +constexpr int N = 256; +// Paramètres de l'équation de Gray-Scott +constexpr double Du = 0.00016; +constexpr double Dv = 0.00008; +constexpr double F = 0.035; +constexpr double k = 0.06; +// Pas de temps et nombre d'itérations +constexpr double dt = 1.0; +constexpr int iterations = 5000; + +// Définition du type de données pour le maillage +using ViewType = Kokkos::View<double**>; + +// Fonction de résolution de l'équation de Gray-Scott +void grayScottEquation(ViewType u, ViewType v) +{ + const int size = N - 2; + + Kokkos::parallel_for("grayScottEquation", size * size, KOKKOS_LAMBDA(const int index) { + const int i = (index / size) + 1; + const int j = (index % size) + 1; + + const double laplacianU = u(i - 1, j) + u(i + 1, j) + u(i, j - 1) + u(i, j + 1) - 4 * u(i, j); + const double laplacianV = v(i - 1, j) + v(i + 1, j) + v(i, j - 1) + v(i, j + 1) - 4 * v(i, j); + + const double uvv = u(i, j) * v(i, j) * v(i, j); + + u(i, j) += Du * laplacianU - uvv + F * (1.0 - u(i, j)) * dt; + v(i, j) += Dv * laplacianV + uvv - (F + k) * v(i, j) * dt; + }); +} + +int main() +{ + // Initialisation de Kokkos + Kokkos::initialize(); + + // Allocation du maillage + ViewType u("u", N, N); + ViewType v("v", N, N); + + // Initialisation des valeurs de départ + Kokkos::parallel_for("initialization", N * N, KOKKOS_LAMBDA(const int index) { + const int i = index / N; + const int j = index % N; + + u(i, j) = 1.0; + v(i, j) = 0.0; + + if (i >= N / 2 - 5 && i <= N / 2 + 5 && j >= N / 2 - 5 && j <= N / 2 + 5) { + v(i, j) = 1.0; + } + }); + + // Résolution de l'équation de Gray-Scott + for (int iter = 0; iter < iterations; ++iter) { + grayScottEquation(u, v); + } + + // Affichage des résultats + Kokkos::parallel_for("output", N * N, KOKKOS_LAMBDA(const int index) { + const int i = index / N; + const int j = index % N; + + if (u(i, j) > 0.5) { + std::cout << "X "; + } else { + std::cout << " "; + } + + if (j == N - 1) { + std::cout << std::endl; + } + }); + + // Finalisation de Kokkos + Kokkos::finalize(); + + return 0; +} diff --git a/Step1/ChatGpt/chatgpt-grayscott-sycl-2.cpp b/Step1/ChatGpt/chatgpt-grayscott-sycl-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..05b348db250e49c8abe3c34cf0f481d2f2c3ce35 --- /dev/null +++ b/Step1/ChatGpt/chatgpt-grayscott-sycl-2.cpp @@ -0,0 +1,119 @@ + +// En demandant à ChatGPT de compléter le Laplacien + +#include <CL/sycl.hpp> +#include <iostream> +#include <vector> + +namespace sycl = cl::sycl; + +// Définition des constantes de l'équation de Gray-Scott +constexpr double Du = 0.02; +constexpr double Dv = 0.01; +constexpr double F = 0.04; +constexpr double k = 0.06; +constexpr double dt = 0.1; + +// Fonction pour initialiser les données +void initializeData(std::vector<double>& u, std::vector<double>& v, int size) { + // Initialise u et v avec des valeurs aléatoires + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_real_distribution<> dis(0.0, 1.0); + + for (int i = 0; i < size; ++i) { + u[i] = 1.0 - dis(gen); + v[i] = dis(gen); + } +} + +// Kernel pour mettre à jour les valeurs de u et v +class GrayScottKernel { +public: + GrayScottKernel(const double* u, const double* v, double* result_u, double* result_v, + int size, int width) + : u_(u), v_(v), result_u_(result_u), result_v_(result_v), size_(size), width_(width) {} + + void operator()(sycl::nd_item<1> item) { + size_t index = item.get_global_id(0); + + int row = index / width_; + int col = index % width_; + + double u = u_[index]; + double v = v_[index]; + + double laplacian_u = calculateLaplacian(u_, row, col, size_, width_); + double laplacian_v = calculateLaplacian(v_, row, col, size_, width_); + + // Mise à jour des valeurs de u et v selon l'équation de Gray-Scott + double du_dt = Du * laplacian_u - u * v * v + F * (1.0 - u); + double dv_dt = Dv * laplacian_v + u * v * v - (F + k) * v; + + double new_u = u + dt * du_dt; + double new_v = v + dt * dv_dt; + + result_u_[index] = new_u; + result_v_[index] = new_v; + } + +private: + const double* u_; + const double* v_; + double* result_u_; + double* result_v_; + int size_; + int width_; + + // Fonction pour calculer le laplacien à partir des valeurs de u ou v + double calculateLaplacian(const double* data, int row, int col, int size, int width) { + double center = data[row * width + col]; + double left = col > 0 ? data[row * width + (col - 1)] : center; + double right = col < width - 1 ? data[row * width + (col + 1)] : center; + double top = row > 0 ? data[(row - 1) * width + col] : center; + double bottom = row < size / width - 1 ? data[(row + 1) * width + col] : center; + + return (left + right + top + bottom - 4.0 * center); + } +}; + +int main() { + const int size = 1000; + + std::vector<double> u(size); + std::vector<double> v(size); + + initializeData(u, v, size); + + // Création de l'environnement SYCL + sycl::default_selector selector; + sycl::queue queue(selector); + + // Allocation des buffers SYCL pour les données + sycl::buffer<double> uBuf(u.data(), sycl::range<1>(size)); + sycl::buffer<double> vBuf(v.data(), sycl::range<1>(size)); + sycl::buffer<double> resultUBuf(u.data(), sycl::range<1>(size)); + sycl::buffer<double> resultVBuf(v.data(), sycl::range<1>(size)); + + // Lancement du kernel + queue.submit([&](sycl::handler& cgh) { + auto uAcc = uBuf.get_access<sycl::access::mode::read>(cgh); + auto vAcc = vBuf.get_access<sycl::access::mode::read>(cgh); + auto resultUAcc = resultUBuf.get_access<sycl::access::mode::write>(cgh); + auto resultVAcc = resultVBuf.get_access<sycl::access::mode::write>(cgh); + + cgh.parallel_for<sycl::range<1>>(sycl::range<1>(size), GrayScottKernel(uAcc.get_pointer(), vAcc.get_pointer(), resultUAcc.get_pointer(), resultVAcc.get_pointer())); + }); + + // Copie des résultats de retour du buffer SYCL vers le vecteur + queue.wait(); + resultUBuf.get_access<sycl::access::mode::read>().memcpy(u.data(), size * sizeof(double)); + resultVBuf.get_access<sycl::access::mode::read>().memcpy(v.data(), size * sizeof(double)); + + // Affichage des résultats + for (int i = 0; i < size; ++i) { + std::cout << "u[" << i << "] = " << u[i] << ", v[" << i << "] = " << v[i] << std::endl; + } + + return 0; +} diff --git a/Step1/ChatGpt/chatgpt-grayscott-sycl.cpp b/Step1/ChatGpt/chatgpt-grayscott-sycl.cpp new file mode 100644 index 0000000000000000000000000000000000000000..521d9634e70d13269e6b9afbe83927bcc9ad14bd --- /dev/null +++ b/Step1/ChatGpt/chatgpt-grayscott-sycl.cpp @@ -0,0 +1,98 @@ +#include <CL/sycl.hpp> +#include <iostream> +#include <vector> + +namespace sycl = cl::sycl; + +// Définition des constantes de l'équation de Gray-Scott +constexpr double Du = 0.02; +constexpr double Dv = 0.01; +constexpr double F = 0.04; +constexpr double k = 0.06; +constexpr double dt = 0.1; + +// Fonction pour initialiser les données +void initializeData(std::vector<double>& u, std::vector<double>& v, int size) { + // Initialise u et v avec des valeurs aléatoires + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_real_distribution<> dis(0.0, 1.0); + + for (int i = 0; i < size; ++i) { + u[i] = 1.0 - dis(gen); + v[i] = dis(gen); + } +} + +// Kernel pour mettre à jour les valeurs de u et v +class GrayScottKernel { +public: + GrayScottKernel(const double* u, const double* v, double* result_u, double* result_v) + : u_(u), v_(v), result_u_(result_u), result_v_(result_v) {} + + void operator()(sycl::nd_item<1> item) { + size_t index = item.get_global_id(0); + + double u = u_[index]; + double v = v_[index]; + + double laplacian_u = 0.0; + double laplacian_v = 0.0; + + // Calcul du laplacien + // ... + + // Mise à jour des valeurs de u et v + // ... + + result_u_[index] = u; + result_v_[index] = v; + } + +private: + const double* u_; + const double* v_; + double* result_u_; + double* result_v_; +}; + +int main() { + const int size = 1000; + + std::vector<double> u(size); + std::vector<double> v(size); + + initializeData(u, v, size); + + // Création de l'environnement SYCL + sycl::default_selector selector; + sycl::queue queue(selector); + + // Allocation des buffers SYCL pour les données + sycl::buffer<double> uBuf(u.data(), sycl::range<1>(size)); + sycl::buffer<double> vBuf(v.data(), sycl::range<1>(size)); + sycl::buffer<double> resultUBuf(u.data(), sycl::range<1>(size)); + sycl::buffer<double> resultVBuf(v.data(), sycl::range<1>(size)); + + // Lancement du kernel + queue.submit([&](sycl::handler& cgh) { + auto uAcc = uBuf.get_access<sycl::access::mode::read>(cgh); + auto vAcc = vBuf.get_access<sycl::access::mode::read>(cgh); + auto resultUAcc = resultUBuf.get_access<sycl::access::mode::write>(cgh); + auto resultVAcc = resultVBuf.get_access<sycl::access::mode::write>(cgh); + + cgh.parallel_for<sycl::range<1>>(sycl::range<1>(size), GrayScottKernel(uAcc.get_pointer(), vAcc.get_pointer(), resultUAcc.get_pointer(), resultVAcc.get_pointer())); + }); + + // Copie des résultats de retour du buffer SYCL vers le vecteur + queue.wait(); + resultUBuf.get_access<sycl::access::mode::read>().memcpy(u.data(), size * sizeof(double)); + resultVBuf.get_access<sycl::access::mode::read>().memcpy(v.data(), size * sizeof(double)); + + // Affichage des résultats + for (int i = 0; i < size; ++i) { + std::cout << "u[" << i << "] = " << u[i] << ", v[" << i << "] = " << v[i] << std::endl; + } + + return 0; +} diff --git a/Step1/SyclSquare/sycl-square.cpp b/Step1/SyclSquare/sycl-square.cpp new file mode 100644 index 0000000000000000000000000000000000000000..665ea50b8b5eb40d7884c4e9a59ed7997ebfaa78 --- /dev/null +++ b/Step1/SyclSquare/sycl-square.cpp @@ -0,0 +1,42 @@ +#include <CL/sycl.hpp> +#include <iostream> + +#define SIZE 10 + +using namespace cl::sycl; + +int main() { + float input[SIZE]; + float output[SIZE]; + + // Initialize input array + for (int i = 0; i < SIZE; i++) { + input[i] = i + 1; + } + + // Create SYCL queue + queue q; + + // Submit command group for execution + q.submit([&](handler& cgh) { + // Create accessors for input and output buffers + accessor inputAccessor(input, range<1>(SIZE), cgh); + accessor outputAccessor(output, range<1>(SIZE), cgh); + + // Define the kernel + cgh.parallel_for(range<1>(SIZE), [=](id<1> idx) { + outputAccessor[idx] = inputAccessor[idx] * inputAccessor[idx]; + }); + }); + + // Wait for the command group to finish + q.wait(); + + // Print the result + for (int i = 0; i < SIZE; i++) { + std::cout << output[i] << " "; + } + std::cout << std::endl; + + return 0; +}