Skip to content

Commit 76d38cf

Browse files
committed
Started to add implementation based on HPX
- added build system support and test infrastructure - added .clang-format configuration file - added BLAS1 add (1d and 2d) - added BLAS1 scale (1d and 2d) - added GitHub action
1 parent 5969eed commit 76d38cf

File tree

27 files changed

+1969
-10
lines changed

27 files changed

+1969
-10
lines changed

.clang-format

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Copyright (c) 2016 Thomas Heller
2+
# Copyright (c) 2016-2022 Hartmut Kaiser
3+
4+
---
5+
AccessModifierOffset: -4
6+
AlignAfterOpenBracket: DontAlign
7+
AlignConsecutiveAssignments: false
8+
AlignConsecutiveDeclarations: false
9+
AlignEscapedNewlines: Right
10+
AlignOperands: false
11+
AlignTrailingComments: true
12+
AllowAllParametersOfDeclarationOnNextLine: false
13+
AllowShortBlocksOnASingleLine: false
14+
AllowShortCaseLabelsOnASingleLine: false
15+
AllowShortFunctionsOnASingleLine: Empty
16+
AllowShortIfStatementsOnASingleLine: false
17+
AllowShortLoopsOnASingleLine: false
18+
AlwaysBreakAfterDefinitionReturnType: None
19+
AlwaysBreakAfterReturnType: None
20+
AlwaysBreakBeforeMultilineStrings: false
21+
AlwaysBreakTemplateDeclarations: true
22+
BinPackArguments: true
23+
BinPackParameters: true
24+
BraceWrapping:
25+
AfterCaseLabel: true
26+
AfterClass: true
27+
AfterControlStatement: true
28+
AfterEnum: true
29+
AfterFunction: true
30+
AfterNamespace: false
31+
AfterObjCDeclaration: false
32+
AfterStruct: true
33+
AfterUnion: true
34+
AfterExternBlock: false
35+
BeforeCatch: true
36+
BeforeElse: true
37+
IndentBraces: false
38+
SplitEmptyFunction: false
39+
SplitEmptyRecord: false
40+
SplitEmptyNamespace: false
41+
BeforeLambdaBody: false
42+
BeforeWhile: false
43+
BreakBeforeBinaryOperators: None
44+
BreakBeforeBraces: Custom
45+
BreakBeforeInheritanceComma: true
46+
BreakBeforeTernaryOperators: false
47+
BreakStringLiterals: true
48+
ColumnLimit: 80
49+
CommentPragmas: ///
50+
CompactNamespaces: true
51+
ConstructorInitializerAllOnOneLineOrOnePerLine: false
52+
ConstructorInitializerIndentWidth : 2
53+
ContinuationIndentWidth: 4
54+
Cpp11BracedListStyle: true
55+
DerivePointerAlignment: false
56+
FixNamespaceComments: true
57+
IncludeCategories:
58+
- Regex: '^<hpx/config\.hpp>'
59+
Priority: 1
60+
- Regex: '^<hpx/config/.*\.hpp>'
61+
Priority: 2
62+
- Regex: '^<hpx/.*/config\.hpp>'
63+
Priority: 3
64+
- Regex: '^<hpx/.*/config/defines\.hpp>'
65+
Priority: 4
66+
- Regex: '^<hpx/.*\.hpp>'
67+
Priority: 5
68+
- Regex: '^<hpx/parallel/.*\.hpp>'
69+
Priority: 6
70+
- Regex: '^<.*'
71+
Priority: 7
72+
- Regex: '.*'
73+
Priority: 8
74+
IndentCaseLabels: false
75+
IndentWidth: 4
76+
IndentWrappedFunctionNames: false
77+
KeepEmptyLinesAtTheStartOfBlocks: false
78+
Language: Cpp
79+
MaxEmptyLinesToKeep: 1
80+
NamespaceIndentation: None
81+
PenaltyBreakBeforeFirstCallParameter: 1
82+
PenaltyBreakComment: 300
83+
PenaltyBreakFirstLessLess: 120
84+
PenaltyBreakString: 1000
85+
PenaltyExcessCharacter: 1000000
86+
PenaltyReturnTypeOnItsOwnLine: 200
87+
PointerAlignment: Left
88+
ReflowComments: false
89+
SortIncludes: true
90+
SpaceAfterCStyleCast: true
91+
SpaceAfterTemplateKeyword: true
92+
SpaceBeforeAssignmentOperators: true
93+
SpaceBeforeParens: ControlStatements
94+
SpaceInEmptyParentheses: false
95+
SpacesBeforeTrailingComments: 4
96+
SpacesInAngles: false
97+
SpacesInContainerLiterals: false
98+
SpacesInCStyleCastParentheses: false
99+
SpacesInParentheses: false
100+
SpacesInSquareBrackets: false
101+
Standard: Cpp11
102+
TabWidth: 4
103+
UseTab: Never
104+
...

.github/workflows/linux_hpx.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright (c) 2022 Hartmut Kaiser
2+
3+
name: HPX Backend CI (Debug)
4+
5+
on: [pull_request]
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
container: stellargroup/hpx:dev
11+
12+
steps:
13+
- uses: actions/checkout@v2
14+
- name: Configure
15+
shell: bash
16+
run: |
17+
cmake \
18+
. \
19+
-Bbuild \
20+
-GNinja \
21+
-DCMAKE_BUILD_TYPE=Debug \
22+
-DLINALG_ENABLE_TESTS=On \
23+
-DLINALG_ENABLE_HPX=On \
24+
-DLINALG_ENABLE_HPX_DEFAULT=On \
25+
-DHPX_DIR=/hpx/build/lib/cmake/HPX
26+
- name: Build
27+
shell: bash
28+
run: |
29+
make install
30+
- name: Test
31+
shell: bash
32+
run: |
33+
cd build
34+
ctest --output-on-failure

CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ project(LinAlg
55
LANGUAGES CXX
66
)
77

8+
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
9+
810
################################################################################
911

1012
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
@@ -152,6 +154,22 @@ if(LINALG_ENABLE_KOKKOS)
152154
)
153155
endif()
154156

157+
if(LINALG_ENABLE_HPX)
158+
find_package(HPX 1.8 REQUIRED)
159+
target_link_libraries(linalg INTERFACE HPX::hpx)
160+
target_include_directories(linalg INTERFACE
161+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/tpl-implementations/include>
162+
)
163+
if(MSVC)
164+
target_compile_definitions(linalg INTERFACE NOMINMAX _CRT_SECURE_NO_WARNINGS)
165+
endif()
166+
message(STATUS "HPX version: " ${HPX_VERSION_STRING})
167+
endif()
168+
169+
if(LINALG_ENABLE_KOKKOS_DEFAULT AND LINALG_ENABLE_HPX_DEFAULT)
170+
message(FATAL_ERROR "Only one of the Kokkos and HPX backends can be marked as default. Please set either LINALG_ENABLE_KOKKOS_DEFAULT or LINALG_ENABLE_HPX_DEFAULT to OFF")
171+
endif()
172+
155173
target_include_directories(linalg INTERFACE
156174
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
157175
$<INSTALL_INTERFACE:include>

examples/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11

22
function(linalg_add_example EXENAME)
33
add_executable(${EXENAME} ${EXENAME}.cpp)
4-
target_link_libraries(${EXENAME} linalg)
4+
target_link_libraries(${EXENAME} PRIVATE linalg)
5+
set_target_properties(${EXENAME} PROPERTIES FOLDER "Examples")
56
endfunction(linalg_add_example)
67

78
linalg_add_example(01_scale)
@@ -11,3 +12,6 @@ linalg_add_example(03_matrix_vector_product_mixedprec)
1112
if(LINALG_ENABLE_KOKKOS)
1213
add_subdirectory(kokkos-based)
1314
endif()
15+
if(LINALG_ENABLE_HPX)
16+
add_subdirectory(hpx-based)
17+
endif()

examples/hpx-based/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright (c) 2022 Hartmut Kaiser
2+
3+
function(linalg_add_example_hpx EXENAME)
4+
linalg_add_example(${EXENAME})
5+
if(LINALG_ENABLE_HPX)
6+
target_link_libraries(${EXENAME} PRIVATE HPX::wrap_main)
7+
endif()
8+
set_target_properties(${EXENAME} PROPERTIES FOLDER "Examples/HPX")
9+
endfunction(linalg_add_example_hpx)
10+
11+
linalg_add_example_hpx(add_hpx)
12+
#linalg_add_example_hpx(dot_hpx)
13+
#linalg_add_example_hpx(dotc_hpx)
14+
#linalg_add_example_hpx(idx_abs_max_hpx)
15+
#linalg_add_example_hpx(vector_norm2_hpx)
16+
#linalg_add_example_hpx(vector_abs_sum_hpx)
17+
#linalg_add_example_hpx(vector_sum_of_squares_hpx)
18+
linalg_add_example_hpx(scale_hpx)
19+
#linalg_add_example_hpx(matrix_vector_product_hpx)

examples/hpx-based/add_hpx.cpp

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright (c) 2022 Hartmut Kaiser
2+
3+
#include <hpx/execution.hpp>
4+
#include <hpx/hpx_main.hpp>
5+
6+
#include <experimental/linalg>
7+
#include <experimental/mdspan>
8+
9+
#include <cstddef>
10+
#include <execution>
11+
#include <iostream>
12+
#include <vector>
13+
14+
template <class T1, class ScalarType>
15+
void print_elements(
16+
const T1& v, const std::vector<ScalarType>& gold, char const* policy_str)
17+
{
18+
std::cout << "Using policy: " << policy_str << "\n";
19+
for (std::size_t i = 0; i < v.size(); i++)
20+
{
21+
std::cout << "computed = " << v(i) << " , gold = " << gold[i] << "\n";
22+
}
23+
}
24+
25+
void reset(auto z)
26+
{
27+
for (std::size_t i = 0; i < z.extent(0); i++)
28+
{
29+
z(i) = 0;
30+
}
31+
}
32+
33+
int main(int argc, char* argv[])
34+
{
35+
std::cout << "add example: calling HPX-kernels" << std::endl;
36+
37+
std::size_t N = 50;
38+
39+
using value_type = double;
40+
41+
std::vector<value_type> x_data(N);
42+
std::vector<value_type> y_data(N);
43+
std::vector<value_type> z_data(N);
44+
45+
value_type* x_ptr = x_data.data();
46+
value_type* y_ptr = y_data.data();
47+
value_type* z_ptr = z_data.data();
48+
49+
using dyn_1d_ext_type =
50+
std::experimental::extents<std::experimental::dynamic_extent>;
51+
using mdspan_type = std::experimental::mdspan<value_type, dyn_1d_ext_type>;
52+
mdspan_type x(x_ptr, N);
53+
mdspan_type y(y_ptr, N);
54+
mdspan_type z(z_ptr, N);
55+
56+
std::vector<value_type> gold(N);
57+
for (std::size_t i = 0; i < x.extent(0); i++)
58+
{
59+
x(i) = static_cast<value_type>(i);
60+
y(i) = i + static_cast<value_type>(10);
61+
z(i) = 0;
62+
gold[i] = x(i) + y(i);
63+
}
64+
65+
namespace stdla = std::experimental::linalg;
66+
const value_type init_value = 2.0;
67+
68+
// This goes to the base implementation
69+
{
70+
stdla::add(std::execution::seq, x, y, z);
71+
print_elements(z, gold, "std::execution::seq");
72+
}
73+
74+
// This also goes to the base implementation
75+
{
76+
reset(z); // reset z since it is modified above
77+
stdla::add(hpx::execution::seq, x, y, z);
78+
print_elements(z, gold, "hpx::execution::seq");
79+
}
80+
81+
// This forwards to HPXKernels
82+
{
83+
reset(z); // reset z since it is modified above
84+
stdla::add(HPXKernelsSTD::hpx_exec<>(), x, y, z);
85+
print_elements(z, gold, "HPXKernelsSTD::hpx_exec<>()");
86+
}
87+
88+
// This forwards to HPXKernels if LINALG_ENABLE_HPX_DEFAULT is ON
89+
{
90+
reset(z); // reset z since it is modified above
91+
stdla::add(std::execution::par, x, y, z);
92+
print_elements(z, gold, "std::execution::par");
93+
}
94+
95+
// This forwards to HPXKernels
96+
{
97+
reset(z); // reset z since it is modified above
98+
stdla::add(hpx::execution::par, x, y, z);
99+
print_elements(z, gold, "hpx::execution::par");
100+
}
101+
102+
#if defined(HPX_HAVE_DATAPAR)
103+
// this invokes a explicitly vectorized HPX versions
104+
{
105+
reset(z); // reset z since it is modified above
106+
stdla::add(hpx::execution::simd, x, y, z);
107+
print_elements(z, gold, "hpx::execution::simd");
108+
}
109+
110+
{
111+
reset(z); // reset z since it is modified above
112+
stdla::add(hpx::execution::par_simd, x, y, z);
113+
print_elements(z, gold, "hpx::execution::par_simd");
114+
}
115+
#endif
116+
117+
return 0;
118+
}

examples/hpx-based/scale_hpx.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) 2022 Hartmut Kaiser
2+
3+
#include <hpx/execution.hpp>
4+
#include <hpx/hpx_main.hpp>
5+
6+
#include <experimental/linalg>
7+
#include <experimental/mdspan>
8+
9+
#include <iostream>
10+
11+
int main(int argc, char* argv[])
12+
{
13+
std::cout << "dot example: calling hpx-kernels" << std::endl;
14+
15+
std::size_t N = 40;
16+
{
17+
std::vector<double> data(N);
18+
double* a_ptr = data.data();
19+
20+
// Requires CTAD working, GCC 11.1 works but some others are buggy
21+
// std::experimental::mdspan a(a_ptr,N);
22+
std::experimental::mdspan<double,
23+
std::experimental::extents<std::experimental::dynamic_extent>>
24+
a(a_ptr, N);
25+
for (std::size_t i = 0; i < a.extent(0); i++)
26+
a(i) = double(i);
27+
28+
// This forwards to HPXKernels
29+
std::experimental::linalg::scale(HPXKernelsSTD::hpx_exec<>(), 2.0, a);
30+
// This forwards to HPXKernels if LINALG_ENABLE_HPX_DEFAULT is ON
31+
std::experimental::linalg::scale(std::execution::par, 2.0, a);
32+
// This always forwards to HPXKernels
33+
std::experimental::linalg::scale(hpx::execution::par, 2.0, a);
34+
// This goes to the base implementation
35+
std::experimental::linalg::scale(std::execution::seq, 2.0, a);
36+
// This also goes to the base implementation
37+
std::experimental::linalg::scale(hpx::execution::seq, 2.0, a);
38+
#if defined(HPX_HAVE_DATAPAR)
39+
// this invokes a explicitly vectorized version
40+
std::experimental::linalg::scale(hpx::execution::simd, 2.0, a);
41+
std::experimental::linalg::scale(hpx::execution::par_simd, 2.0, a);
42+
#endif
43+
for (std::size_t i = 0; i < a.extent(0); i++)
44+
printf("%zi %lf\n", i, a(i));
45+
}
46+
return 0;
47+
}

0 commit comments

Comments
 (0)