Skip to content

Commit b3680f4

Browse files
authored
Merge pull request #122 from artiomn/master
CUDA and Python
2 parents 225aa74 + f92fc35 commit b3680f4

112 files changed

Lines changed: 7977 additions & 1386 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,12 @@ repos:
172172
# language_version: python3
173173

174174
- repo: https://github.com/pre-commit/pre-commit
175-
rev: v4.5.0
175+
rev: v4.5.1
176176
hooks:
177177
- id: validate_manifest
178178

179179
- repo: https://github.com/pre-commit/mirrors-mypy
180-
rev: v1.19.0
180+
rev: v1.19.1
181181
hooks:
182182
- id: mypy
183183
args: ["--config-file", "knp/.mypy.ini", "--install-types", "--explicit-package-bases", "--check-untyped-defs", "--scripts-are-modules", "--strict"]

CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ limitations under the License.
1515
]]
1616

1717
cmake_minimum_required(VERSION 3.25)
18+
set(CMAKE_POLICY_VERSION_MINIMUM 3.5 CACHED)
1819

1920
include_guard(GLOBAL)
2021

@@ -159,6 +160,7 @@ else()
159160
endif()
160161

161162
cmake_dependent_option(KNP_BUILD_DOCUMENTATION "Build doxygen auto documentation" ${KNP_BUILD_AUTONOMOUS} "DOXYGEN_FOUND" OFF)
163+
option(KNP_BUILD_CUDA "Enable CUDA backend build" ON)
162164
option(KNP_BUILD_EXAMPLES "Build usage examples" ${KNP_BUILD_AUTONOMOUS})
163165
option(KNP_BUILD_TESTS "Build tests" ${KNP_BUILD_AUTONOMOUS})
164166
option(KNP_ENABLE_AVX "Enable AVX and other CPU-specific extensions in the release build" ${KNP_ENABLE_AVX_DEFAULT})
@@ -196,7 +198,8 @@ if (KNP_ENABLE_AVX)
196198
# endif()
197199

198200
set(CMAKE_CXX_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE})
199-
add_compile_options(-include x86intrin.h)
201+
add_compile_options($<$<COMPILE_LANGUAGE:CXX,C>:-include>)
202+
add_compile_options($<$<COMPILE_LANGUAGE:CXX,C>:x86intrin.h>)
200203
# -include bits/stdc++.h
201204
endif()
202205

CPPLINT.cfg

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ filter=-legal/copyright,-whitespace/braces
22
linelength=120
33
root=knp
44

5-
65
# - Do not check access modifier indentation.
76
# CPPLint enforces +1, but our rule is no indentation.
87
# - Do not check NOLINT* directives.
@@ -17,7 +16,9 @@ root=knp
1716
# Chromium team uses own headers instead of standard.
1817
# - whitespace/newline
1918
# An else should appear on the same line as the preceding }
20-
filter=-whitespace/indent,-readability/nolint,-readability/todo,-build/include_subdir,-runtime/references,-build/c++11,-whitespace/newline, -whitespace/parens
19+
# - build/include_order
20+
# Need to skip CUDA headers.
21+
filter=-whitespace/indent,-readability/nolint,-readability/todo,-build/include_subdir,-runtime/references,-build/c++11,-whitespace/newline, -whitespace/parens,-build/include_order
2122

2223
# Python framework macroses.
2324
filter=-readability/braces

build.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ while getopts "rcvsh" o; do
7474
SINGLE_PROCESS=1
7575
;;
7676
*)
77-
usage
78-
exit 1
77+
shift $((OPTIND-1))
78+
CMAKE_ADD_OPTS="${CMAKE_ADD_OPTS} $*"
79+
break;
7980
;;
8081
esac
8182
done

cmake/knp-functions.cmake

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,16 @@ function(knp_set_target_parameters target visibility)
4242
else()
4343
set(pub_visibility "PUBLIC")
4444
if(KNP_IPO_ENABLED)
45-
check_ipo_supported(RESULT _ipo_result OUTPUT _ipo_output)
45+
check_ipo_supported(RESULT _ipo_result OUTPUT _ipo_output LANGUAGES C CXX)
4646
if (_ipo_result)
47-
set_target_properties("${target}" PROPERTIES INTERPROCEDURAL_OPTIMIZATION TRUE)
47+
set_target_properties("${target}" PROPERTIES
48+
$<$<NOT:$<COMPILE_LANGUAGE:C,CXX>>:INTERPROCEDURAL_OPTIMIZATION TRUE>)
4849
else()
4950
message(WARNING "IPO is not supported: ${_ipo_output}.")
5051
endif()
5152
endif()
5253
endif()
5354

54-
# if (MSVC) # AND ($<COMPILE_LANGUAGE> STREQUAL CXX OR $<COMPILE_LANGUAGE> STREQUAL C))
55-
# set_property(TARGET "${target}" PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
56-
# endif()
57-
5855
target_compile_options("${target}" ${visibility} $<$<COMPILE_LANG_AND_ID:C,Clang>:-Wdocumentation>)
5956
target_compile_options("${target}" ${visibility} $<$<COMPILE_LANG_AND_ID:CXX,Clang>:-Wdocumentation>)
6057

@@ -236,6 +233,12 @@ function(knp_add_python_module name)
236233
target_include_directories("${LIB_NAME}" PRIVATE ${Python3_INCLUDE_DIRS})
237234
message(TRACE "Linking ${LIB_NAME} with Boost::python${PARSED_ARGS_PY_VER}...")
238235
target_link_libraries("${LIB_NAME}" PRIVATE Boost::disable_autolinking Boost::headers Boost::python${PARSED_ARGS_PY_VER} ${PARSED_ARGS_LINK_LIBRARIES})
236+
target_compile_options("${LIB_NAME}" PRIVATE $<$<CONFIG:Debug>:-DBOOST_PYTHON_DEBUG>)
237+
238+
# if (MSVC) # AND ($<COMPILE_LANGUAGE> STREQUAL CXX OR $<COMPILE_LANGUAGE> STREQUAL C))
239+
# # Dynamic MSVC runtime.
240+
# set_property(TARGET "${LIB_NAME}" PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
241+
# endif()
239242

240243
# set_target_properties(${LIB_NAME} PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}")
241244
# set_target_properties(${LIB_NAME} PROPERTIES SUFFIX "${PYTHON_MODULE_EXTENSION}")
@@ -247,8 +250,6 @@ function(knp_add_python_module name)
247250

248251
if (NOT WIN32)
249252
target_compile_definitions("${LIB_NAME}" PRIVATE Py_NO_ENABLE_SHARED)
250-
else()
251-
target_compile_definitions("${LIB_NAME}" PRIVATE CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS=ON)
252253
endif()
253254

254255
add_custom_command(TARGET "${LIB_NAME}" POST_BUILD
@@ -291,7 +292,7 @@ function(knp_packaging_set_parameters component_name project_name)
291292

292293
if (PARSED_ARGS_DEPENDS)
293294
string(REPLACE ";" ", " PARSED_ARGS_DEPENDS "${PARSED_ARGS_DEPENDS}")
294-
set(CPACK_COMPONENT_${COMPONENT_VAR_NAME}_DEPE NDS "${PARSED_ARGS_DEPENDS}" CACHE STRING "${project_name} dependencies" FORCE)
295+
set(CPACK_COMPONENT_${COMPONENT_VAR_NAME}_DEPENDS "${PARSED_ARGS_DEPENDS}" CACHE STRING "${project_name} dependencies" FORCE)
295296
else()
296297
unset(CPACK_COMPONENT_${COMPONENT_VAR_NAME}_DEPENDS CACHE)
297298
endif()

knp/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,5 @@ if (PVS_BINARY)
8686
else()
8787
message(WARNING "PVS analyzer was not found, static analysis with PVS Studio disabled.")
8888
endif()
89+
90+

knp/backends/CMakeLists.txt

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,4 @@ limitations under the License.
1717
cmake_minimum_required(VERSION 3.25)
1818

1919
add_subdirectory(cpu)
20-
21-
file(GLOB backends LIST_DIRECTORIES true "[a-zA-Z0-9_]*-backend")
22-
23-
if(NOT backends)
24-
message(WARNING "No non-CPU backend was found.")
25-
return()
26-
endif()
27-
28-
foreach(backend ${backends})
29-
message(STATUS "Adding backend \"${backend}\"...")
30-
get_filename_component(BACKEND_NAME "${backend}" NAME)
31-
add_subdirectory("${backend}")
32-
endforeach()
20+
add_subdirectory(gpu)

knp/backends/cpu/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ if(NOT backends)
2828
endif()
2929

3030
foreach(backend ${backends})
31-
message(STATUS "Adding backend \"${backend}\"...")
31+
message(STATUS "Adding CPU backend \"${backend}\"...")
3232
get_filename_component(BACKEND_NAME "${backend}" NAME)
3333
add_subdirectory("${backend}")
3434
endforeach()

knp/backends/cpu/cpu-devices/impl/unknown_cpu.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,15 @@ CPU::CPU(uint32_t cpu_num) : cpu_num_(cpu_num), power_meter_{std::make_unique<Cp
5252
}
5353

5454

55-
CPU::CPU(CPU&& other) : cpu_name_{std::move(other.cpu_name_)} {}
55+
CPU::CPU(CPU&& other) : cpu_name_{std::move(other.cpu_name_)}, cpu_num_{std::move(other.cpu_num_)} {}
5656

5757

5858
CPU::~CPU() {}
5959

6060

6161
CPU& CPU::operator=(CPU&& other) noexcept
6262
{
63+
cpu_num_.swap(other.cpu_num_);
6364
cpu_name_.swap(other.cpu_name_);
6465
power_meter_.swap(other.power_meter_);
6566
return *this;

knp/backends/cpu/cpu-library/include/knp/backends/cpu-library/impl/delta_synapse_projection_impl.h

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <knp/synapse-traits/stdp_synaptic_resource_rule.h>
2828

2929
#include <spdlog/spdlog.h>
30+
#include <spdlog/fmt/fmt.h>
3031

3132
#include <algorithm>
3233
#include <mutex>
@@ -65,12 +66,12 @@ constexpr bool is_forcing<knp::core::Projection<synapse_traits::DeltaSynapse>>()
6566
template <typename ProjectionType>
6667
MessageQueue::const_iterator calculate_delta_synapse_projection_data(
6768
ProjectionType &projection, std::vector<core::messaging::SpikeMessage> &messages, MessageQueue &future_messages,
68-
size_t step_n,
69+
const size_t step_n,
6970
std::function<knp::synapse_traits::synapse_parameters<knp::synapse_traits::DeltaSynapse>(
7071
const typename ProjectionType::SynapseParameters &)>
7172
sp_getter = [](const typename ProjectionType::SynapseParameters &synapse_params) { return synapse_params; })
7273
{
73-
SPDLOG_TRACE("Calculating delta synapse projection data...");
74+
SPDLOG_TRACE("Calculating delta synapse projection data for the step = {}", step_n);
7475
using SynapseType = typename ProjectionType::ProjectionSynapseType;
7576
WeightUpdateSTDP<SynapseType>::init_projection(projection, messages, step_n);
7677

@@ -80,6 +81,7 @@ MessageQueue::const_iterator calculate_delta_synapse_projection_data(
8081
for (const auto &spiked_neuron_index : message_data)
8182
{
8283
auto synapses = projection.find_synapses(spiked_neuron_index, ProjectionType::Search::by_presynaptic);
84+
SPDLOG_TRACE("Projection synapse count for the spike = {}", synapses.size());
8385
for (auto synapse_index : synapses)
8486
{
8587
auto &synapse = projection[synapse_index];
@@ -94,8 +96,13 @@ MessageQueue::const_iterator calculate_delta_synapse_projection_data(
9496
static_cast<uint32_t>(std::get<core::target_neuron_id>(synapse))};
9597

9698
auto iter = future_messages.find(future_step);
99+
100+
SPDLOG_TRACE("Synapse index = {}, synapse delay = {}, synapse weight = {}, step = {}, future step = {}",
101+
synapse_index, synapse_params.delay_, synapse_params.weight_, step_n, future_step);
102+
97103
if (iter != future_messages.end())
98104
{
105+
SPDLOG_TRACE("Add existing impact.");
99106
iter->second.impacts_.push_back(impact);
100107
}
101108
else
@@ -106,11 +113,13 @@ MessageQueue::const_iterator calculate_delta_synapse_projection_data(
106113
projection.get_postsynaptic(),
107114
is_forcing<ProjectionType>(),
108115
{impact}};
116+
SPDLOG_TRACE("Add new impact.");
109117
future_messages.insert(std::make_pair(future_step, message_out));
110118
}
111119
}
112120
}
113121
}
122+
114123
WeightUpdateSTDP<SynapseType>::modify_weights(projection);
115124
return future_messages.find(step_n);
116125
}
@@ -208,6 +217,18 @@ void calculate_delta_synapse_projection_impl(
208217
SPDLOG_DEBUG("Calculating delta synapse projection...");
209218

210219
auto messages = endpoint.unload_messages<core::messaging::SpikeMessage>(projection.get_uid());
220+
221+
SPDLOG_DEBUG("Messages count = {}...", messages.size());
222+
223+
#if (SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE)
224+
for (const auto &message : messages)
225+
{
226+
SPDLOG_TRACE("Spike from {} with spiked neurons: {}",
227+
std::string(message.header_.sender_uid_),
228+
fmt::join(message.neuron_indexes_, ", "));
229+
}
230+
#endif
231+
211232
auto out_iter = calculate_delta_synapse_projection_data(projection, messages, future_messages, step_n);
212233
if (out_iter != future_messages.end())
213234
{

0 commit comments

Comments
 (0)