Skip to content

Commit 413a8a6

Browse files
committed
Modernize stencil example
1 parent 5940aad commit 413a8a6

13 files changed

Lines changed: 333 additions & 308 deletions

examples/03_stencil/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
# Distributed under the Boost Software License, Version 1.0. (See accompanying
44
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
55

6-
cmake_minimum_required(VERSION 2.8)
6+
cmake_minimum_required(VERSION 3.14)
77

88
project(stencil CXX)
99

10-
find_package(HPX 1.7.0 REQUIRED)
10+
find_package(HPX 1.9.0 REQUIRED)
1111

1212
add_executable(stencil_serial stencil_serial.cpp)
1313
target_link_libraries(stencil_serial PRIVATE HPX::hpx HPX::wrap_main)

examples/03_stencil/communicator.hpp

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
// Distributed under the Boost Software License, Version 1.0. (See accompanying
44
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
55

6-
#ifndef STENCIL_COMMUNICATOR_HPP
7-
#define STENCIL_COMMUNICATOR_HPP
6+
#pragma once
87

98
#include <hpx/include/lcos.hpp>
109

@@ -13,45 +12,56 @@
1312
template <typename T>
1413
struct communicator
1514
{
16-
enum neighbor {
15+
enum neighbor
16+
{
1717
up = 0,
1818
down = 1,
1919
};
2020

21-
typedef hpx::lcos::channel<T> channel_type;
21+
using channel_type = hpx::distributed::channel<T>;
2222

2323
// rank: our rank in the system
2424
// num: number of participating partners
2525
communicator(std::size_t rank, std::size_t num)
2626
{
27-
static const char* up_name = "/stencil/up/";
28-
static const char* down_name = "/stencil/down/";
29-
3027
// Only set up channels if we have more than one partner
3128
if (num > 1)
3229
{
30+
constexpr char const* down_name = "/stencil/down/";
31+
constexpr char const* up_name = "/stencil/up/";
32+
3333
// We have an upper neighbor if our rank is greater than zero.
3434
if (rank > 0)
3535
{
36-
// Retrieve the channel from our upper neighbor from which we receive
37-
// the row we need to update the first row in our partition.
38-
recv[up] = hpx::find_from_basename<channel_type>(down_name, rank - 1);
36+
// Retrieve the channel from our upper neighbor from which we
37+
// receive the row we need to update the first row in our
38+
// partition.
39+
recv[up] =
40+
hpx::find_from_basename<channel_type>(down_name, rank - 1);
3941

4042
// Create the channel we use to send our first row to our upper
4143
// neighbor
4244
send[up] = channel_type(hpx::find_here());
43-
// Register the channel with a name such that our neighbor can find it.
45+
46+
// Register the channel with a name such that our neighbor can
47+
// find it.
4448
hpx::register_with_basename(up_name, send[up], rank);
4549
}
50+
4651
if (rank < num - 1)
4752
{
48-
// Retrieve the channel from our neighbor below from which we receive
49-
// the row we need to update the last row in our partition.
50-
recv[down] = hpx::find_from_basename<channel_type>(up_name, rank + 1);
51-
// Create the channel we use to send our last row to our neighbor
52-
// below
53+
// Retrieve the channel from our neighbor below from which we
54+
// receive the row we need to update the last row in our
55+
// partition.
56+
recv[down] =
57+
hpx::find_from_basename<channel_type>(up_name, rank + 1);
58+
59+
// Create the channel we use to send our last row to our
60+
// neighbor below
5361
send[down] = channel_type(hpx::find_here());
54-
// Register the channel with a name such that our neighbor can find it.
62+
63+
// Register the channel with a name such that our neighbor can
64+
// find it.
5565
hpx::register_with_basename(down_name, send[down], rank);
5666
}
5767
}
@@ -76,8 +86,6 @@ struct communicator
7686
return recv[n].get(hpx::launch::async, step);
7787
}
7888

79-
std::array<hpx::lcos::channel<T>, 2> recv;
80-
std::array<hpx::lcos::channel<T>, 2> send;
89+
std::array<hpx::distributed::channel<T>, 2> recv;
90+
std::array<hpx::distributed::channel<T>, 2> send;
8191
};
82-
83-
#endif

examples/03_stencil/line_iterator.hpp

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,40 @@
33
// Distributed under the Boost Software License, Version 1.0. (See accompanying
44
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
55

6-
#ifndef STENCIL_LINE_ITERATOR_HPP
7-
#define STENCIL_LINE_ITERATOR_HPP
6+
#pragma once
87

98
#include <hpx/include/util.hpp>
109

11-
template <typename UpIter, typename MiddleIter = UpIter, typename DownIter = UpIter>
10+
template <typename UpIter, typename MiddleIter = UpIter,
11+
typename DownIter = UpIter>
1212
struct line_iterator
13-
// iterator_facade is a facade class that defines the boilerplate needed for
14-
// a proper standard C++ iterator. As a user, we only have to define basic
15-
// functions
13+
// iterator_facade is a facade class that defines the boilerplate needed for a
14+
// proper standard C++ iterator. As a user, we only have to define basic
15+
// functions
1616
: hpx::util::iterator_facade<
1717
// Our type:
1818
line_iterator<UpIter, MiddleIter, DownIter>,
1919
// Value type (When dereferencing the iterator)
2020
double,
2121
// Our iterator is random access.
22-
std::random_access_iterator_tag
23-
>
22+
std::random_access_iterator_tag>
2423
{
2524
private:
26-
typedef
27-
hpx::util::iterator_facade<
28-
// Our type:
29-
line_iterator<UpIter, MiddleIter, DownIter>,
30-
// Value type (When dereferencing the iterator)
31-
double,
32-
// Our iterator is random access.
33-
std::random_access_iterator_tag
34-
>
35-
base_type;
25+
using base_type = hpx::util::iterator_facade<
26+
// Our type:
27+
line_iterator<UpIter, MiddleIter, DownIter>,
28+
// Value type (When dereferencing the iterator)
29+
double,
30+
// Our iterator is random access.
31+
std::random_access_iterator_tag>;
3632

3733
public:
3834
line_iterator(UpIter up_, MiddleIter middle_, DownIter down_)
3935
: up(up_)
4036
, middle(middle_)
4137
, down(down_)
42-
{}
38+
{
39+
}
4340

4441
UpIter up;
4542
MiddleIter middle;
@@ -84,5 +81,3 @@ struct line_iterator
8481
return other.middle - middle;
8582
}
8683
};
87-
88-
#endif

examples/03_stencil/output.hpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,21 @@
33
// Distributed under the Boost Software License, Version 1.0. (See accompanying
44
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
55

6-
#ifndef STENCIL_OUTPUT_HPP
7-
#define STENCIL_OUTPUT_HPP
6+
#pragma once
87

98
#include <fstream>
109
#include <string>
1110

1211
template <typename Container>
13-
void output(std::string name, Container const& data, std::size_t Nx, std::size_t Ny)
12+
void output(std::string const& name, Container const& data, std::size_t Nx,
13+
std::size_t Ny)
1414
{
1515
std::ofstream file(name);
16-
17-
for(std::size_t y = 0; y != Ny; ++y)
16+
for (std::size_t y = 0; y != Ny; ++y)
1817
{
19-
for(std::size_t x = 0; x != Nx; ++x)
18+
for (std::size_t x = 0; x != Nx; ++x)
2019
{
2120
file << x << " " << y << " " << data[y * Nx + x] << '\n';
2221
}
2322
}
2423
}
25-
26-
#endif

examples/03_stencil/row_iterator.hpp

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
// Distributed under the Boost Software License, Version 1.0. (See accompanying
44
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
55

6-
#ifndef STENCIL_ROW_ITERATOR_HPP
7-
#define STENCIL_ROW_ITERATOR_HPP
6+
#pragma once
87

98
#include "line_iterator.hpp"
109

1110
#include <hpx/include/util.hpp>
1211

13-
template <typename UpIter, typename MiddleIter = UpIter, typename DownIter = UpIter>
12+
template <typename UpIter, typename MiddleIter = UpIter,
13+
typename DownIter = UpIter>
1414
struct row_iterator
15-
// iterator_facade is a facade class that defines the boilerplate needed for
16-
// a proper standard C++ iterator. As a user, we only have to define basic
17-
// functions
15+
// iterator_facade is a facade class that defines the boilerplate needed for a
16+
// proper standard C++ iterator. As a user, we only have to define basic
17+
// functions
1818
: hpx::util::iterator_facade<
1919
// Our type:
2020
row_iterator<UpIter, MiddleIter, DownIter>,
@@ -24,17 +24,17 @@ struct row_iterator
2424
std::random_access_iterator_tag,
2525
// Since dereferencing should return a new line_iterator, we need to
2626
// explicitly set the reference type.
27-
line_iterator<UpIter, MiddleIter, DownIter>
28-
>
27+
line_iterator<UpIter, MiddleIter, DownIter>>
2928
{
30-
typedef line_iterator<UpIter, MiddleIter, DownIter> line_iterator_type;
29+
using line_iterator_type = line_iterator<UpIter, MiddleIter, DownIter>;
3130

3231
row_iterator(std::size_t Nx, MiddleIter middle_)
33-
: up_(middle - Nx)
34-
, middle(middle_)
32+
: middle(middle_)
33+
, up_(middle - Nx)
3534
, down_(middle + Nx)
3635
, Nx_(Nx)
37-
{}
36+
{
37+
}
3838

3939
line_iterator<UpIter, MiddleIter, DownIter> line() const
4040
{
@@ -45,16 +45,16 @@ struct row_iterator
4545
line_iterator<typename Container::const_iterator, MiddleIter, DownIter>
4646
top_boundary(Container const& cont) const
4747
{
48-
return line_iterator<typename Container::const_iterator, MiddleIter, DownIter>(
49-
cont.begin(), middle, down_);
48+
return line_iterator<typename Container::const_iterator, MiddleIter,
49+
DownIter>(cont.begin(), middle, down_);
5050
}
5151

5252
template <typename Container>
5353
line_iterator<UpIter, MiddleIter, typename Container::const_iterator>
5454
bottom_boundary(Container const& cont) const
5555
{
56-
return line_iterator<UpIter, MiddleIter, typename Container::const_iterator>(
57-
up_, middle, cont.begin());
56+
return line_iterator<UpIter, MiddleIter,
57+
typename Container::const_iterator>(up_, middle, cont.begin());
5858
}
5959

6060
MiddleIter middle;
@@ -102,5 +102,3 @@ struct row_iterator
102102
DownIter down_;
103103
std::size_t Nx_;
104104
};
105-
106-
#endif

examples/03_stencil/stencil.hpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
// Distributed under the Boost Software License, Version 1.0. (See accompanying
44
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
55

6+
#pragma once
7+
68
#include "line_iterator.hpp"
79
#include "row_iterator.hpp"
810

911
#include <algorithm>
1012
#include <array>
1113

1214
template <typename Container>
13-
void init(
14-
std::array<Container, 2>& U, std::size_t Nx, std::size_t Ny,
15+
void init(std::array<Container, 2>& U, std::size_t Nx, std::size_t Ny,
1516
std::size_t rank = 0, std::size_t num_localities = 1)
1617
{
1718
// Initialize: Boundaries are set to 1, interior is 0
@@ -20,6 +21,7 @@ void init(
2021
std::fill(U[0].begin(), U[0].begin() + Nx, 1.0);
2122
std::fill(U[1].begin(), U[1].begin() + Nx, 1.0);
2223
}
24+
2325
for (std::size_t y = 0; y < Ny; ++y)
2426
{
2527
U[0][y * Nx + 0] = 1.0;
@@ -28,6 +30,7 @@ void init(
2830
U[0][y * Nx + (Nx - 1)] = 1.0;
2931
U[1][y * Nx + (Nx - 1)] = 1.0;
3032
}
33+
3134
if (rank == num_localities - 1)
3235
{
3336
std::fill(U[0].end() - Nx, U[0].end(), 1.0);
@@ -39,12 +42,14 @@ template <typename InIter, typename OutIter>
3942
OutIter line_update(InIter begin, InIter end, OutIter result)
4043
{
4144
++result;
45+
4246
// Iterate over the interior: skip the last and first element
43-
for(InIter it = begin + 1; it != end - 1; ++it, ++result)
47+
for (InIter it = begin + 1; it != end - 1; ++it, ++result)
4448
{
45-
*result = 0.25 * (it.up[-1] + it.up[+1] + it.down[-1] + it.down[+1])
46-
- *it.middle;
49+
*result = 0.25 * (it.up[-1] + it.up[+1] + it.down[-1] + it.down[+1]) -
50+
*it.middle;
4751
}
52+
4853
++result;
4954

5055
return result;

0 commit comments

Comments
 (0)