Skip to content

Commit

Permalink
Apply clang-format
Browse files Browse the repository at this point in the history
Apply clang-format 17.0.6 with --style=Google
  • Loading branch information
rafbiels committed Jan 8, 2024
1 parent 929f91f commit 8601834
Show file tree
Hide file tree
Showing 16 changed files with 332 additions and 290 deletions.
18 changes: 18 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
BasedOnStyle: Google
IncludeCategories:
- Regex: '^<sycl/.*>$'
Priority: 3
SortPriority: 0
CaseSensitive: false
- Regex: '^<.*\.h>'
Priority: 1
SortPriority: 0
CaseSensitive: false
- Regex: '^<.*'
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: '.*'
Priority: 4
SortPriority: 0
CaseSensitive: false
12 changes: 7 additions & 5 deletions src/MPI_for_CUDA_backend/scatter_reduce_gather.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Compile with `mpicxx -fsycl -fsycl-targets=nvptx64-nvidia-cuda -Xsycl-target-backend --cuda-gpu-arch=sm_xx scatter_reduce_gather.cpp -o res`
// Compile with `mpicxx -fsycl -fsycl-targets=nvptx64-nvidia-cuda
// -Xsycl-target-backend --cuda-gpu-arch=sm_xx scatter_reduce_gather.cpp -o res`
// Where sm_xx is the Compute Capability (CC). If the `-Xsycl-target-backend
// --cuda-gpu-arch=` flags are not explicitly provided the lowest supported CC
// will be used: sm_50.
Expand All @@ -14,10 +15,10 @@

#include <assert.h>
#include <mpi.h>

#include <sycl/sycl.hpp>

int main(int argc, char *argv[]) {

/* -------------------------------------------------------------------------------------------
MPI Initialization.
--------------------------------------------------------------------------------------------*/
Expand All @@ -32,9 +33,10 @@ int main(int argc, char *argv[]) {

if (size != 2) {
if (rank == 0) {
printf("This program requires exactly 2 MPI ranks, but you are "
"attempting to use %d! Exiting...\n",
size);
printf(
"This program requires exactly 2 MPI ranks, "
"but you are attempting to use %d! Exiting...\n",
size);
}
MPI_Finalize();
exit(0);
Expand Down
43 changes: 22 additions & 21 deletions src/MPI_for_CUDA_backend/send_recv_buff.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Compile with `mpicxx -fsycl -fsycl-targets=nvptx64-nvidia-cuda -Xsycl-target-backend --cuda-gpu-arch=sm_xx send_recv_buff.cpp -o res`
// Where sm_xx is the Compute Capability (CC). If the `-Xsycl-target-backend
// Compile with `mpicxx -fsycl -fsycl-targets=nvptx64-nvidia-cuda
// -Xsycl-target-backend --cuda-gpu-arch=sm_xx send_recv_buff.cpp -o res`
// where sm_xx is the Compute Capability (CC). If the `-Xsycl-target-backend
// --cuda-gpu-arch=` flags are not explicitly provided the lowest supported CC
// will be used: sm_50.

Expand All @@ -8,13 +9,13 @@

#include <assert.h>
#include <mpi.h>

#include <sycl/sycl.hpp>

int main(int argc, char *argv[]) {

/* -------------------------------------------------------------------------------------------
MPI Initialization.
--------------------------------------------------------------------------------------------*/
/* ---------------------------------------------------------------------------
MPI Initialization.
----------------------------------------------------------------------------*/

MPI_Init(&argc, &argv);

Expand All @@ -26,17 +27,18 @@ int main(int argc, char *argv[]) {

if (size != 2) {
if (rank == 0) {
printf("This program requires exactly 2 MPI ranks, but you are "
"attempting to use %d! Exiting...\n",
size);
printf(
"This program requires exactly 2 MPI ranks, "
"but you are attempting to use %d! Exiting...\n",
size);
}
MPI_Finalize();
exit(0);
}

/* -------------------------------------------------------------------------------------------
SYCL Initialization, which internally sets the CUDA device.
--------------------------------------------------------------------------------------------*/
/* ---------------------------------------------------------------------------
SYCL Initialization, which internally sets the CUDA device.
----------------------------------------------------------------------------*/

sycl::queue q{};

Expand All @@ -46,16 +48,16 @@ int main(int argc, char *argv[]) {
std::vector<int> data(nelem, -1);

{
/* -------------------------------------------------------------------------------------------
Create a SYCL buffer in each rank. The sycl::buffer created in each rank will
manage the copy of data to and from the device as required.
--------------------------------------------------------------------------------------------*/
/* -------------------------------------------------------------------------
Create a SYCL buffer in each rank. The sycl::buffer created in each rank
will manage the copy of data to and from the device as required.
--------------------------------------------------------------------------*/

sycl::buffer<int> buff(&data[0], sycl::range{nelem});

/* -------------------------------------------------------------------------------------------
Perform the send/receive.
--------------------------------------------------------------------------------------------*/
/* -------------------------------------------------------------------------
Perform the send/receive.
--------------------------------------------------------------------------*/

if (rank == 0) {
// Operate on the Rank 0 data.
Expand Down Expand Up @@ -107,8 +109,7 @@ int main(int argc, char *argv[]) {
// Check the values. Since this is outside the scope where the buffer was
// created, the data array is automatically updated on the host.
if (rank == 1) {
for (int i = 0; i < nelem; ++i)
assert(data[i] == -2);
for (int i = 0; i < nelem; ++i) assert(data[i] == -2);
}

MPI_Finalize();
Expand Down
17 changes: 9 additions & 8 deletions src/MPI_for_CUDA_backend/send_recv_usm.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Compile with `mpicxx -fsycl -fsycl-targets=nvptx64-nvidia-cuda -Xsycl-target-backend --cuda-gpu-arch=sm_xx send_recv_usm.cpp -o res`
// Where sm_xx is the Compute Capability (CC). If the `-Xsycl-target-backend
// Compile with `mpicxx -fsycl -fsycl-targets=nvptx64-nvidia-cuda
// -Xsycl-target-backend --cuda-gpu-arch=sm_xx send_recv_usm.cpp -o res` Where
// sm_xx is the Compute Capability (CC). If the `-Xsycl-target-backend
// --cuda-gpu-arch=` flags are not explicitly provided the lowest supported CC
// will be used: sm_50.

Expand All @@ -8,10 +9,10 @@

#include <assert.h>
#include <mpi.h>

#include <sycl/sycl.hpp>

int main(int argc, char *argv[]) {

/* -------------------------------------------------------------------------------------------
MPI Initialization.
--------------------------------------------------------------------------------------------*/
Expand All @@ -26,9 +27,10 @@ int main(int argc, char *argv[]) {

if (size != 2) {
if (rank == 0) {
printf("This program requires exactly 2 MPI ranks, but you are "
"attempting to use %d! Exiting...\n",
size);
printf(
"This program requires exactly 2 MPI ranks, "
"but you are attempting to use %d! Exiting...\n",
size);
}
MPI_Finalize();
exit(0);
Expand Down Expand Up @@ -86,8 +88,7 @@ int main(int argc, char *argv[]) {
sycl::free(devp, q);

// Check the values.
for (int i = 0; i < nelem; ++i)
assert(data[i] == -2);
for (int i = 0; i < nelem; ++i) assert(data[i] == -2);
}
MPI_Finalize();
return 0;
Expand Down
26 changes: 12 additions & 14 deletions src/fluid/fluid.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ class SYCLFluidContainer {
img{sycl::range<1>(size * size)},
// Initialize queue with default selector and asynchronous exception
// handler.
queue{sycl::default_selector_v,
[](sycl::exception_list exceptions) {
queue{sycl::default_selector_v, [](sycl::exception_list exceptions) {
for (const std::exception_ptr& e : exceptions) {
try {
std::rethrow_exception(e);
Expand Down Expand Up @@ -208,10 +207,8 @@ class SYCLFluidContainer {
// Update the image pixel data with the appropriate color for a given
// density.
queue.submit([&](sycl::handler& cgh) {
auto img_acc{
img.template get_access(cgh, sycl::write_only)};
auto density_a{
density_b.template get_access(cgh, sycl::read_write)};
auto img_acc{img.template get_access(cgh, sycl::write_only)};
auto density_a{density_b.template get_access(cgh, sycl::read_write)};
cgh.parallel_for<image_kernal>(
sycl::range<1>(size * size), [=](sycl::item<1> item) {
auto index{item.get_id(0)};
Expand All @@ -229,8 +226,8 @@ class SYCLFluidContainer {
using float_buffer = sycl::buffer<float, 1>;
using read_write_accessor =
sycl::accessor<float, 1, sycl::access::mode::read_write,
sycl::access::target::device,
sycl::access::placeholder::false_t>;
sycl::access::target::device,
sycl::access::placeholder::false_t>;

// Wrapper around queue submission.
template <typename T, typename... Ts>
Expand Down Expand Up @@ -297,9 +294,9 @@ class SYCLFluidContainer {
}

// Solve linear differential equation of density / velocity. (SYCL VERSION).
static void LinearSolve(int /*b*/, read_write_accessor x, read_write_accessor x0,
float a, float c_reciprocal, std::size_t N,
sycl::handler& cgh) {
static void LinearSolve(int /*b*/, read_write_accessor x,
read_write_accessor x0, float a, float c_reciprocal,
std::size_t N, sycl::handler& cgh) {
cgh.parallel_for<fluid_linear_solve>(
sycl::range<2>(N - 2, N - 2), [=](sycl::item<2> item) {
auto i{1 + item.get_id(0)};
Expand Down Expand Up @@ -402,9 +399,10 @@ class SYCLFluidContainer {
}

// Move density / velocity within the field to the next step. (SYCL VERSION).
static void AdvectImpl(int /*b*/, read_write_accessor d, read_write_accessor d0,
read_write_accessor u, read_write_accessor v,
float dt0, std::size_t N, sycl::handler& cgh) {
static void AdvectImpl(int /*b*/, read_write_accessor d,
read_write_accessor d0, read_write_accessor u,
read_write_accessor v, float dt0, std::size_t N,
sycl::handler& cgh) {
cgh.parallel_for<fluid_advect>(
sycl::range<2>(N - 2, N - 2), [=](sycl::item<2> item) {
auto i{1 + item.get_id(0)};
Expand Down
70 changes: 38 additions & 32 deletions src/fluid/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,31 @@
*
**************************************************************************/

#include "fluid.h"

#include <Magnum/Platform/Sdl2Application.h>
#include <Corrade/Containers/StringStlView.h>
#include <Magnum/GL/DefaultFramebuffer.h>
#include <Magnum/GL/Mesh.h>
#include <Magnum/GL/Texture.h>
#include <Magnum/GL/TextureFormat.h>
#include <Magnum/PixelFormat.h>
#include <Magnum/ImageView.h>
#include <Magnum/Shaders/FlatGL.h>
#include <Magnum/Primitives/Square.h>
#include <Magnum/MeshTools/Compile.h>
#include <Magnum/PixelFormat.h>
#include <Magnum/Platform/Sdl2Application.h>
#include <Magnum/Primitives/Square.h>
#include <Magnum/Shaders/FlatGL.h>
#include <Magnum/Trade/MeshData.h>

#include <sycl/sycl.hpp>

#include "fluid.h"

constexpr Magnum::PixelFormat PIXELFORMAT{Magnum::PixelFormat::RGBA8Unorm};

// Title bar text
constexpr std::string_view WINDOWTITLE{
"Codeplay Fluid Simulation"
" - Move mouse to add fluid"
" - Press space to clear fluid"};

// Size of the fluid container edge (always square shaped).
constexpr int SIZE{300};

Expand All @@ -45,27 +52,26 @@ constexpr int SCALE{3};
class FluidSimulationApp : public Magnum::Platform::Application {
public:
FluidSimulationApp(const Arguments& arguments)
: Magnum::Platform::Application{
arguments,
Configuration{}.setTitle("Codeplay Fluid Simulation"
" - Move mouse to add fluid - Press space to clear fluid"),
GLConfiguration{}.setFlags(GLConfiguration::Flag::QuietLog)},
size_{SIZE},
fluid_{SIZE, 0.2f, 0.0f, 0.0000001f},
mesh_{Magnum::MeshTools::compile(Magnum::Primitives::squareSolid(
Magnum::Primitives::SquareFlag::TextureCoordinates))},
shader_{Magnum::Shaders::FlatGL2D::Configuration{}.setFlags(
Magnum::Shaders::FlatGL2D::Flag::Textured |
Magnum::Shaders::FlatGL2D::Flag::TextureTransformation)} {

: Magnum::Platform::Application{arguments,
Configuration{}.setTitle(WINDOWTITLE),
GLConfiguration{}.setFlags(
GLConfiguration::Flag::QuietLog)},
size_{SIZE},
fluid_{SIZE, 0.2f, 0.0f, 0.0000001f},
mesh_{Magnum::MeshTools::compile(Magnum::Primitives::squareSolid(
Magnum::Primitives::SquareFlag::TextureCoordinates))},
shader_{Magnum::Shaders::FlatGL2D::Configuration{}.setFlags(
Magnum::Shaders::FlatGL2D::Flag::Textured |
Magnum::Shaders::FlatGL2D::Flag::TextureTransformation)} {
// Set window size.
setWindowSize({SIZE*SCALE, SIZE*SCALE});
Magnum::GL::defaultFramebuffer.setViewport({{0,0},{SIZE*SCALE, SIZE*SCALE}});
setWindowSize({SIZE * SCALE, SIZE * SCALE});
Magnum::GL::defaultFramebuffer.setViewport(
{{0, 0}, {SIZE * SCALE, SIZE * SCALE}});

texture_.setWrapping(Magnum::GL::SamplerWrapping::ClampToEdge)
.setMagnificationFilter(Magnum::GL::SamplerFilter::Linear)
.setMinificationFilter(Magnum::GL::SamplerFilter::Linear)
.setStorage(1, Magnum::GL::textureFormat(PIXELFORMAT), {size_, size_});
.setMagnificationFilter(Magnum::GL::SamplerFilter::Linear)
.setMinificationFilter(Magnum::GL::SamplerFilter::Linear)
.setStorage(1, Magnum::GL::textureFormat(PIXELFORMAT), {size_, size_});
shader_.bindTexture(texture_);
}

Expand All @@ -89,18 +95,18 @@ class FluidSimulationApp : public Magnum::Platform::Application {
// Draws fluid to the screen.
void drawEvent() override final {
// Clear screen.
Magnum::GL::defaultFramebuffer.clear(
Magnum::GL::FramebufferClear::Color |
Magnum::GL::FramebufferClear::Depth);
Magnum::GL::defaultFramebuffer.clear(Magnum::GL::FramebufferClear::Color |
Magnum::GL::FramebufferClear::Depth);

// Update texture with pixel data array.
fluid_.WithData([&](sycl::uchar4 const* data) {
Magnum::ImageView2D img{
PIXELFORMAT, {size_, size_},
Corrade::Containers::ArrayView{
Magnum::ImageView2D img{
PIXELFORMAT,
{size_, size_},
Corrade::Containers::ArrayView{
reinterpret_cast<const char*>(data),
size_*size_*Magnum::pixelFormatSize(PIXELFORMAT)}};
texture_.setSubImage(0, {0,0}, img);
size_ * size_ * Magnum::pixelFormatSize(PIXELFORMAT)}};
texture_.setSubImage(0, {0, 0}, img);
});

// Draw texture to screen.
Expand Down
Loading

0 comments on commit 8601834

Please sign in to comment.