Skip to content

Introduce BlockSize #3716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions cpp/demo/custom_kernel/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <map>
#include <stdint.h>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -167,9 +168,9 @@ double assemble_vector1(const mesh::Geometry<T>& g, const fem::DofMap& dofmap,
md::mdspan<const T, md::extents<std::size_t, md::dynamic_extent, 3>> x(
g.x().data(), g.x().size() / 3, 3);
common::Timer timer("Assembler1 lambda (vector)");
fem::impl::assemble_cells<T, 1>([](auto, auto, auto, auto) {},
b.mutable_array(), g.dofmap(), x, cells,
{dofmap.map(), 1, cells}, kernel, {}, {}, {});
fem::impl::assemble_cells<T, BS<1>>(
[](auto, auto, auto, auto) {}, b.mutable_array(), g.dofmap(), x, cells,
{dofmap.map(), BS<1>(), cells}, kernel, {}, {}, {});
b.scatter_rev(std::plus<T>());
return la::squared_norm(b);
}
Expand Down
1 change: 1 addition & 0 deletions cpp/dolfinx/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
set(HEADERS_common
${CMAKE_CURRENT_SOURCE_DIR}/defines.h
${CMAKE_CURRENT_SOURCE_DIR}/constexpr_type.h
${CMAKE_CURRENT_SOURCE_DIR}/dolfinx_common.h
${CMAKE_CURRENT_SOURCE_DIR}/dolfinx_doc.h
${CMAKE_CURRENT_SOURCE_DIR}/IndexMap.h
Expand Down
53 changes: 53 additions & 0 deletions cpp/dolfinx/common/constexpr_type.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (C) 2025 Paul T. Kühner
//
// This file is part of DOLFINx (https://www.fenicsproject.org)
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#pragma once

#include <type_traits>

namespace dolfinx::common
{
/// @private Concept defining a variadic compile time or runtime variable. T
/// indicates the type that is stored and V the value. Either V equals T, i.e.
/// it is a runtime variable or V defines a compile time value V::value of type
/// T.
/// @tparam T type of the value to be stored.
/// @tparam V container type. Usually T for a runtime variable or a
/// std::integral_constant<T, ...> for a compile time constant.
template <typename T, typename V>
concept ConstexprType = std::is_same_v<T, V> || (requires {
typename V::value_type;
requires std::is_same_v<typename V::value_type, T>;
});

/// @private Check if ConstexprType holds a compile time constant.
template <typename T, typename V>
requires ConstexprType<T, V>
constexpr bool is_compile_time_v = !std::is_same_v<T, V>;

/// @private Check if ConstexprType holds a run time variable.
template <typename T, typename V>
requires ConstexprType<T, V>
constexpr bool is_runtime_v = std::is_same_v<T, V>;

/// @private Retrieve value of a compile time constant form a ConstexprType.
template <typename T, typename V>
requires ConstexprType<T, V>
constexpr T value(V /* container */,
typename std::enable_if_t<is_compile_time_v<T, V>>* = 0)
{
return V::value;
}

/// @private Retrieve value of runtime variable form a ConstexprType.
template <typename T, typename V>
requires ConstexprType<T, V>
T value(V container, typename std::enable_if_t<is_runtime_v<T, V>>* = 0)
{
return container;
}

} // namespace dolfinx::common
27 changes: 27 additions & 0 deletions cpp/dolfinx/common/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <basix/mdspan.hpp>
#include <complex>
#include <concepts>
#include <dolfinx/common/constexpr_type.h>
#include <type_traits>

namespace dolfinx
Expand Down Expand Up @@ -41,4 +42,30 @@ using scalar_value_t = typename scalar_value<T>::type;
/// @private mdspan/mdarray namespace
namespace md = MDSPAN_IMPL_STANDARD_NAMESPACE;

/// @private Concept capturing both compile time defined block sizes and runtime
/// ones.
template <typename V>
concept BlockSize = common::ConstexprType<int, V>;

/// @private Short notation for a compile time block size.
template <int N>
using BS = std::integral_constant<int, N>;

/// @private Retrieves the integral block size of a compile time block size.
template <BlockSize V>
constexpr int
block_size(V bs,
typename std::enable_if_t<common::is_compile_time_v<int, V>>* = 0)
{
return common::value<int, V>(bs);
}

/// @private Retrieves the integral block size of a runtime block size.
template <BlockSize V>
int block_size(V bs,
typename std::enable_if_t<common::is_runtime_v<int, V>>* = 0)
{
return common::value<int, V>(bs);
}

} // namespace dolfinx
Loading
Loading