Skip to content

Commit 3239441

Browse files
AndreySorokin7AndreySorokin7
andauthored
Add Kokkos library to repository (#256)
Co-authored-by: AndreySorokin7 <andrey_sorokin_nn@mail,ru>
1 parent 131db58 commit 3239441

File tree

14 files changed

+379
-194
lines changed

14 files changed

+379
-194
lines changed

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@
1313
[submodule "3rdparty/oneDNN"]
1414
path = 3rdparty/oneDNN
1515
url = https://github.com/uxlfoundation/oneDNN
16+
[submodule "3rdparty/kokkos"]
17+
path = 3rdparty/kokkos
18+
url = https://github.com/kokkos/kokkos.git
19+
branch = release-candidate-5.0.1

3rdparty/kokkos

Submodule kokkos added at f572302

CMakeLists.txt

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ if(ENABLE_STATISTIC_WEIGHTS)
2222
add_definitions(-DENABLE_STATISTIC_WEIGHTS)
2323
endif()
2424

25-
set(CMAKE_CXX_STANDARD 17)
25+
set(CMAKE_CXX_STANDARD 20)
2626

2727
enable_testing()
2828

@@ -43,6 +43,28 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
4343
add_subdirectory(3rdparty)
4444

4545
include(cmake/opencv_config.cmake)
46+
include(cmake/kokkos_config.cmake)
47+
48+
include_directories("${KOKKOS_INSTALL_DIR}/include")
49+
50+
add_library(Kokkos_imported INTERFACE)
51+
add_dependencies(Kokkos_imported kokkos_external)
52+
53+
target_include_directories(Kokkos_imported INTERFACE
54+
"${KOKKOS_INSTALL_DIR}/include"
55+
)
56+
57+
target_link_directories(Kokkos_imported INTERFACE
58+
"${KOKKOS_INSTALL_DIR}/lib"
59+
)
60+
61+
62+
target_link_libraries(Kokkos_imported INTERFACE kokkoscore kokkoscontainers)
63+
64+
65+
if(MSVC)
66+
add_compile_options(/wd4267 /wd4244 /wd4127 /wd4324)
67+
endif()
4668

4769
if (NOT WIN32)
4870
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Werror")

app/Accuracy/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ target_link_libraries( ACCLib ${OpenCV_LIBS} )
1010
target_link_libraries( ACCLib TBB_unified)
1111
target_link_libraries( ACCLib layers_lib)
1212
target_link_libraries( ACCLib gtest_main)
13+
target_link_libraries( ACCLib Kokkos_imported)
1314

1415
add_executable(Accuracy_Check accuracy_check.cpp)
1516
target_link_libraries(Accuracy_Check ACCLib)

app/Graph/build.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -194,21 +194,23 @@ void build_graph(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input,
194194
}
195195

196196
try {
197-
std::sort(
198-
connection_list.begin(), connection_list.end(),
199-
[&](const auto& a, const auto& b) {
200-
if (!name_to_layer.count(a.first) || !name_to_layer.count(b.first)) {
201-
return false;
202-
}
203-
return name_to_layer[a.first]->getID() <
204-
name_to_layer[b.first]->getID();
205-
});
197+
std::sort(connection_list.begin(), connection_list.end(),
198+
[&](const auto& a, const auto& b) {
199+
if (!name_to_layer.contains(a.first) ||
200+
!name_to_layer.contains(b.first)) {
201+
return false;
202+
}
203+
return name_to_layer[a.first]->getID() <
204+
name_to_layer[b.first]->getID();
205+
});
206+
206207
} catch (const std::exception& e) {
207208
std::cerr << "ERROR during sorting: " << e.what() << '\n';
208209
}
209210

210211
for (const auto& [source_name, target_name] : connection_list) {
211-
if (name_to_layer.count(source_name) && name_to_layer.count(target_name)) {
212+
if (name_to_layer.contains(source_name) &&
213+
name_to_layer.contains(target_name)) {
212214
if (target_name.find("Concat") != std::string::npos ||
213215
name_to_layer[target_name]->getName() == it_lab_ai::kConcat) {
214216
if (concat_connections.find(target_name) != concat_connections.end()) {
@@ -532,7 +534,7 @@ ParseResult parse_json_model(RuntimeOptions options,
532534
std::string constant_name = inputs[1].get<std::string>();
533535
constant_name = get_base_layer_name(constant_name);
534536

535-
if (layer_parameters.count(constant_name)) {
537+
if (layer_parameters.contains(constant_name)) {
536538
splits = layer_parameters[constant_name];
537539
} else if (constant_name.find("onnx::") != std::string::npos) {
538540
splits = last_constant_value;
@@ -735,7 +737,7 @@ ParseResult parse_json_model(RuntimeOptions options,
735737
std::string constant_name = inputs[1].get<std::string>();
736738
constant_name = get_base_layer_name(constant_name);
737739

738-
if (layer_parameters.count(constant_name)) {
740+
if (layer_parameters.contains(constant_name)) {
739741
shape = layer_parameters[constant_name];
740742
}
741743
}
@@ -797,7 +799,7 @@ ParseResult parse_json_model(RuntimeOptions options,
797799
std::string constant_name = inputs[1].get<std::string>();
798800
constant_name = get_base_layer_name(constant_name);
799801

800-
if (layer_parameters.count(constant_name)) {
802+
if (layer_parameters.contains(constant_name)) {
801803
axes = layer_parameters[constant_name];
802804
} else if (constant_name.find("onnx::") != std::string::npos) {
803805
axes = last_constant_value;

cmake/kokkos_config.cmake

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
include(ExternalProject)
2+
3+
set(KOKKOS_BUILD_DIR "${CMAKE_BINARY_DIR}/3rdparty/kokkos_build")
4+
set(KOKKOS_INSTALL_DIR "${CMAKE_BINARY_DIR}/3rdparty/kokkos_install")
5+
6+
ExternalProject_Add(
7+
kokkos_external
8+
SOURCE_DIR "${CMAKE_SOURCE_DIR}/3rdparty/kokkos"
9+
BINARY_DIR "${KOKKOS_BUILD_DIR}"
10+
INSTALL_DIR "${KOKKOS_INSTALL_DIR}"
11+
12+
CMAKE_ARGS
13+
-G "${CMAKE_GENERATOR}"
14+
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
15+
-DCMAKE_INSTALL_PREFIX=${KOKKOS_INSTALL_DIR}
16+
17+
-DKokkos_ENABLE_SERIAL=ON
18+
-DKokkos_ARCH_NATIVE=OFF
19+
-DKokkos_ENABLE_OPENMP=OFF
20+
-DKokkos_ENABLE_THREADS=ON
21+
-DKokkos_ENABLE_CUDA=OFF
22+
-DKokkos_ENABLE_HIP=OFF
23+
-DKokkos_ENABLE_TESTS=OFF
24+
-DKokkos_ENABLE_EXAMPLES=OFF
25+
26+
-DKokkos_ENABLE_AGGRESSIVE_VECTORIZATION=ON
27+
-DKokkos_ENABLE_LIBDL=OFF
28+
29+
BUILD_COMMAND ${CMAKE_COMMAND} --build "${KOKKOS_BUILD_DIR}" --config ${CMAKE_BUILD_TYPE} -j${NPROC}
30+
31+
INSTALL_COMMAND ${CMAKE_COMMAND} --install "${KOKKOS_BUILD_DIR}" --config ${CMAKE_BUILD_TYPE}
32+
33+
BUILD_ALWAYS OFF
34+
LOG_CONFIGURE ON
35+
LOG_BUILD ON
36+
LOG_INSTALL ON
37+
)
38+
39+
set(Kokkos_DIR "${KOKKOS_INSTALL_DIR}/lib/cmake/Kokkos" CACHE PATH "Path to Kokkos CMake config")

include/parallel/backends.hpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <oneapi/tbb/info.h>
44
#include <oneapi/tbb/parallel_for.h>
55

6+
// NOLINTNEXTLINE(misc-header-include-cycle)
7+
#include <Kokkos_Core.hpp>
68
#include <cstddef>
79
#include <cstdint>
810
#include <functional>
@@ -17,7 +19,8 @@ enum class Backend : std::uint8_t {
1719
kSeq = 0,
1820
kThreads = 1,
1921
kTbb = 2,
20-
kOmp = 3
22+
kOmp = 3,
23+
kKokkos = 4
2124
};
2225

2326
struct Options {
@@ -116,5 +119,28 @@ inline void impl_omp(std::size_t count,
116119
}
117120
#endif
118121

122+
inline void impl_kokkos(std::size_t count,
123+
const std::function<void(std::size_t)>& func,
124+
const Options& opt) {
125+
if (count == 0) return;
126+
static std::once_flag init_flag;
127+
std::call_once(init_flag, [&opt]() {
128+
int num_threads =
129+
opt.max_threads > 0
130+
? opt.max_threads
131+
: static_cast<int>(std::thread::hardware_concurrency());
132+
133+
Kokkos::InitializationSettings args;
134+
args.set_num_threads(num_threads);
135+
Kokkos::initialize(args);
136+
137+
std::atexit([]() { Kokkos::finalize(); });
138+
});
139+
140+
auto kokkos_func = [&func](const std::size_t i) { func(i); };
141+
Kokkos::parallel_for("parallel_for", count, kokkos_func);
142+
Kokkos::fence();
143+
}
144+
119145
} // namespace parallel
120146
} // namespace it_lab_ai

include/parallel/parallel.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ inline Backend select_backend(const Options& opt, std::size_t n) {
2929
}
3030

3131
if (opt.backend == Backend::kSeq || opt.backend == Backend::kThreads ||
32-
opt.backend == Backend::kTbb || opt.backend == Backend::kOmp) {
32+
opt.backend == Backend::kTbb || opt.backend == Backend::kOmp ||
33+
opt.backend == Backend::kKokkos) {
3334
return opt.backend;
3435
}
3536

@@ -56,6 +57,9 @@ inline void parallel_for(std::size_t count, Func&& func,
5657
case Backend::kOmp:
5758
impl_omp(count, std::forward<Func>(func), opt);
5859
break;
60+
case Backend::kKokkos:
61+
impl_kokkos(count, std::forward<Func>(func), opt);
62+
break;
5963
}
6064
}
6165

src/graph/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
file(GLOB_RECURSE graph_src *.cpp)
22
add_library(graph_lib STATIC "${GRAPH_HEADERS}" "${graph_src}")
33
target_link_libraries(graph_lib PUBLIC TBB_unified)
4+
add_dependencies(graph_lib kokkos_external)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
file(GLOB_RECURSE graphT_src *.cpp)
22
add_library(graphT_lib STATIC "${GRAPHT_HEADERS}" "${graphT_src}")
33
target_link_libraries(graphT_lib PUBLIC TBB_unified)
4+
add_dependencies(graphT_lib kokkos_external)

0 commit comments

Comments
 (0)