StencilStream v3.0.0
SYCL-based Stencil Simulation Framework Targeting FPGAs
Loading...
Searching...
No Matches
Grid.hpp
Go to the documentation of this file.
1/*
2 * Copyright © 2020-2024 Jan-Oliver Opdenhövel, Paderborn Center for Parallel Computing, Paderborn
3 * University
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
6 * associated documentation files (the “Software”), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
8 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in all copies or
12 * substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
15 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 */
20#pragma once
21#include "../Index.hpp"
22#include <memory>
23
24namespace stencil {
25namespace cpu {
26
51template <typename Cell> class Grid {
52 public:
58 static constexpr uindex_t dimensions = 2;
59
67 Grid(uindex_t c, uindex_t r) : buffer(sycl::range<2>(c, r)) {}
68
75 Grid(sycl::range<2> range) : buffer(range) {}
76
85 Grid(sycl::buffer<Cell, 2> other_buffer) : buffer(other_buffer.get_range()) {
86 copy_from_buffer(other_buffer);
87 }
88
98 Grid(Grid const &other_grid) : buffer(other_grid.buffer) {}
99
110 void copy_from_buffer(sycl::buffer<Cell, 2> other_buffer) {
111 if (buffer.get_range() != other_buffer.get_range()) {
112 throw std::range_error("The target buffer has not the same size as the grid");
113 }
114 sycl::host_accessor buffer_ac(buffer, sycl::write_only);
115 sycl::host_accessor other_ac(other_buffer, sycl::read_only);
116 std::memcpy(buffer_ac.get_pointer(), other_ac.get_pointer(), buffer_ac.byte_size());
117 }
118
128 void copy_to_buffer(sycl::buffer<Cell, 2> other_buffer) {
129 if (buffer.get_range() != other_buffer.get_range()) {
130 throw std::range_error("The target buffer has not the same size as the grid");
131 }
132 sycl::host_accessor buffer_ac(buffer, sycl::read_only);
133 sycl::host_accessor other_ac(other_buffer, sycl::write_only);
134 std::memcpy(other_ac.get_pointer(), buffer_ac.get_pointer(), buffer_ac.byte_size());
135 }
136
146 template <sycl::access::mode access_mode = sycl::access::mode::read_write>
147 class GridAccessor : public sycl::host_accessor<Cell, Grid::dimensions, access_mode> {
148 public:
153 : sycl::host_accessor<Cell, Grid::dimensions, access_mode>(grid.buffer) {}
154 };
155
159 uindex_t get_grid_width() const { return buffer.get_range()[0]; }
160
164 uindex_t get_grid_height() const { return buffer.get_range()[1]; }
165
170
171 sycl::buffer<Cell, 2> &get_buffer() { return buffer; }
172
173 private:
174 sycl::buffer<Cell, 2> buffer;
175};
176} // namespace cpu
177} // namespace stencil
An accessor for the grid.
Definition Grid.hpp:147
GridAccessor(Grid &grid)
Create a new accessor to the given grid.
Definition Grid.hpp:152
A grid class for the CPU backend.
Definition Grid.hpp:51
Grid(uindex_t c, uindex_t r)
Create a new, uninitialized grid with the given dimensions.
Definition Grid.hpp:67
void copy_to_buffer(sycl::buffer< Cell, 2 > other_buffer)
Copy the contents of the grid into the SYCL buffer.
Definition Grid.hpp:128
Grid(sycl::range< 2 > range)
Create a new, uninitialized grid with the given dimensions.
Definition Grid.hpp:75
void copy_from_buffer(sycl::buffer< Cell, 2 > other_buffer)
Copy the contents of the SYCL buffer into the grid.
Definition Grid.hpp:110
Grid(Grid const &other_grid)
Create a new reference to the given grid.
Definition Grid.hpp:98
Grid make_similar() const
Create an new, uninitialized grid with the same size as the current one.
Definition Grid.hpp:169
sycl::buffer< Cell, 2 > & get_buffer()
Definition Grid.hpp:171
uindex_t get_grid_height() const
Return the height, or number of rows, of the grid.
Definition Grid.hpp:164
uindex_t get_grid_width() const
Return the width, or number of columns, of the grid.
Definition Grid.hpp:159
static constexpr uindex_t dimensions
The number of dimensions of the grid.
Definition Grid.hpp:58
Grid(sycl::buffer< Cell, 2 > other_buffer)
Create a new grid with the same size and contents as the given SYCL buffer.
Definition Grid.hpp:85
Definition AccessorSubscript.hpp:24
BOOST_PP_CAT(BOOST_PP_CAT(uint, STENCIL_INDEX_WIDTH), _t) uindex_t
An unsigned integer of configurable width.
Definition Index.hpp:42