Skip to content

Commit

Permalink
Feat: force inline.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adversarr committed Aug 4, 2024
1 parent fd85d7d commit 5802cd9
Show file tree
Hide file tree
Showing 14 changed files with 300 additions and 166 deletions.
72 changes: 51 additions & 21 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,34 +1,64 @@
cmake_minimum_required(VERSION 3.10)
project(compute_graph)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_library(compute_graph INTERFACE)
project(compute_graph
LANGUAGES CXX
VERSION 0.0.1)

# low version cmake does not support PROJECT_IS_TOP_LEVEL
if (${CMAKE_VERSION} VERSION_LESS "3.12.0")
if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
set(PROJECT_IS_TOP_LEVEL TRUE)
else()
set(PROJECT_IS_TOP_LEVEL FALSE)
endif ()
endif()


# ================ options ================
option(CG_BUILD_EXAMPLES "Build examples" ${PROJECT_IS_TOP_LEVEL})

target_include_directories(compute_graph INTERFACE
${CMAKE_CURRENT_LIST_DIR}/compute_graph/include)

add_executable(compute_graph_exec main.cpp)
target_link_libraries(compute_graph_exec PUBLIC compute_graph)
# ================ target ================
add_library(compute_graph INTERFACE)
target_include_directories(compute_graph INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/compute_graph/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

if (MSVC)
target_compile_options(compute_graph INTERFACE /Zc:preprocessor)
endif()

if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
target_compile_options(compute_graph_exec PRIVATE
-Wall -Wextra -Weffc++
-Werror=uninitialized
-Werror=return-type
-Wconversion -Wsign-compare
-Werror=unused-result
-Werror=suggest-override
-Wzero-as-null-pointer-constant
-Wmissing-declarations
-Wold-style-cast -Werror=vla
-Wnon-virtual-dtor)
# ================ examples ================
if (CG_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()

if (MSVC)
target_compile_options(compute_graph_exec PRIVATE /W4)
endif()
# ================ install ================
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
set(version_config_file ${CMAKE_CURRENT_BINARY_DIR}/compute_graphConfigVersion.cmake)
set(project_config_file ${CMAKE_CURRENT_BINARY_DIR}/compute_graphConfig.cmake)

write_basic_package_version_file(
${version_config_file}
VERSION ${PACKAGE_VERSION}
COMPATIBILITY AnyNewerVersion
)

install(DIRECTORY compute_graph/include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(TARGETS compute_graph
EXPORT compute_graphTargets
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(EXPORT compute_graphTargets
FILE compute_graphTargets.cmake
NAMESPACE compute_graph::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/compute_graph)
install(FILES ${project_config_file} ${version_config_file}
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/compute_graph)

configure_file(${CMAKE_CURRENT_LIST_DIR}/cmake/compute_graphConfig.cmake.in
${project_config_file}
@ONLY)
1 change: 1 addition & 0 deletions cmake/compute_graphConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include("${CMAKE_CURRENT_LIST_DIR}/compute_graphTargets.cmake")
26 changes: 26 additions & 0 deletions compute_graph/include/compute_graph/compute_graph.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// The MIT License (MIT)
// Copyright © 2024 Adversarr
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the “Software”), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#pragma once

#include "socket.hpp"
#include "node.hpp"
#include "graph.hpp"
102 changes: 54 additions & 48 deletions compute_graph/include/compute_graph/graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,27 @@
#include <memory>
#include <vector>

#define CG_PP_HANDLE_COMMON(HandleType) \
HandleType(HandleType const &) noexcept = default; \
HandleType(HandleType &&) noexcept = default; \
HandleType &operator=(HandleType const &) = delete; \
HandleType &operator=(HandleType &&) = delete

namespace compute_graph {

class InputSocketHandle;
class OutputSocketHandle;

class NodeHandle {
public:
NodeHandle(NodeHandle const &) = default;
NodeHandle(NodeHandle &&) = default;
NodeHandle &operator=(NodeHandle const &) = delete;
NodeHandle &operator=(NodeHandle &&) = delete;

NodeBase const &operator*() const noexcept { return node_; }
NodeBase &operator*() noexcept { return node_; }
NodeBase const *operator->() const noexcept { return &node_; }
NodeBase *operator->() noexcept { return &node_; }
NodeBase &node() noexcept { return node_; }
NodeBase const &node() const noexcept { return node_; }

size_t index() const noexcept { return index_; }

CG_PP_HANDLE_COMMON(NodeHandle);
CG_STRONG_INLINE NodeBase const &operator*() const noexcept { return node_; }
CG_STRONG_INLINE NodeBase &operator*() noexcept { return node_; }
CG_STRONG_INLINE NodeBase const *operator->() const noexcept { return &node_; }
CG_STRONG_INLINE NodeBase *operator->() noexcept { return &node_; }
CG_STRONG_INLINE NodeBase &node() noexcept { return node_; }
CG_STRONG_INLINE NodeBase const &node() const noexcept { return node_; }
CG_STRONG_INLINE size_t index() const noexcept { return index_; }
InputSocketHandle input(size_t index);
OutputSocketHandle output(size_t index);

Expand All @@ -64,19 +64,21 @@ class NodeHandle {

class InputSocketHandle {
public:
InputSocket const &operator*() const noexcept {
CG_PP_HANDLE_COMMON(InputSocketHandle);

CG_STRONG_INLINE InputSocket const &operator*() const noexcept {
return node_.inputs()[index_];
}
InputSocket const *operator->() const noexcept {
CG_STRONG_INLINE InputSocket const *operator->() const noexcept {
return &node_.inputs()[index_];
}

NodeBase &node() noexcept { return node_; }
NodeBase const &node() const noexcept { return node_; }
size_t index() const noexcept { return index_; }
CG_STRONG_INLINE NodeBase &node() noexcept { return node_; }
CG_STRONG_INLINE NodeBase const &node() const noexcept { return node_; }
CG_STRONG_INLINE size_t index() const noexcept { return index_; }

private:
InputSocketHandle(NodeBase &node, size_t index):
CG_STRONG_INLINE InputSocketHandle(NodeBase &node, size_t index):
node_(node), index_(index) {}

friend class Graph;
Expand All @@ -89,19 +91,21 @@ class InputSocketHandle {

class OutputSocketHandle {
public:
OutputSocket const &operator*() const noexcept {
CG_PP_HANDLE_COMMON(OutputSocketHandle);

CG_STRONG_INLINE OutputSocket const &operator*() const noexcept {
return node_.outputs()[index_];
}
OutputSocket const *operator->() const noexcept {
CG_STRONG_INLINE OutputSocket const *operator->() const noexcept {
return &node_.outputs()[index_];
}

NodeBase &node() noexcept { return node_; }
NodeBase const &node() const noexcept { return node_; }
size_t index() const noexcept { return index_; }
CG_STRONG_INLINE NodeBase &node() noexcept { return node_; }
CG_STRONG_INLINE NodeBase const &node() const noexcept { return node_; }
CG_STRONG_INLINE size_t index() const noexcept { return index_; }

private:
OutputSocketHandle(NodeBase &node, size_t index):
CG_STRONG_INLINE OutputSocketHandle(NodeBase &node, size_t index):
node_(node), index_(index) {}

friend class Graph;
Expand All @@ -113,15 +117,17 @@ class OutputSocketHandle {

class Link {
public:
OutputSocketHandle from() const noexcept {
CG_PP_HANDLE_COMMON(Link);

CG_STRONG_INLINE OutputSocketHandle from() const noexcept {
return {from_, from_index_};
}
InputSocketHandle to() const noexcept {
CG_STRONG_INLINE InputSocketHandle to() const noexcept {
return {to_, to_index_};
}

private:
Link(NodeBase &from, size_t from_index, NodeBase &to, size_t to_index):
CG_STRONG_INLINE Link(NodeBase &from, size_t from_index, NodeBase &to, size_t to_index):
from_(from), to_(to), from_index_(from_index), to_index_(to_index) {}

friend class Graph;
Expand All @@ -143,11 +149,11 @@ class Graph {
Graph() = default;
~Graph() { clear(); }
void clear();
size_t num_nodes() const noexcept { return nodes_.size() - free_ids_.size(); }
size_t num_links() const noexcept { return link_size_; }
CG_STRONG_INLINE size_t num_nodes() const noexcept { return nodes_.size() - free_ids_.size(); }
CG_STRONG_INLINE size_t num_links() const noexcept { return link_size_; }

node_container const &nodes() const noexcept { return nodes_; }
node_container &nodes() noexcept { return nodes_; }
CG_STRONG_INLINE node_container const &nodes() const noexcept { return nodes_; }
CG_STRONG_INLINE node_container &nodes() noexcept { return nodes_; }

// node op.
NodeHandle add(std::unique_ptr<NodeBase> node);
Expand Down Expand Up @@ -177,27 +183,27 @@ class Graph {
};


inline bool can_connect(OutputSocket const &from, InputSocket const &to) noexcept {
CG_STRONG_INLINE bool can_connect(OutputSocket const &from, InputSocket const &to) noexcept {
return from.type() == to.type();
}

inline InputSocketHandle NodeHandle::input(size_t index) {
CG_STRONG_INLINE InputSocketHandle NodeHandle::input(size_t index) {
return {node_, index};
}

template<typename MT> InputSocketHandle NodeHandle::input(MT) {
template<typename MT> CG_STRONG_INLINE InputSocketHandle NodeHandle::input(MT) {
return input(MT::index);
}

inline OutputSocketHandle NodeHandle::output(size_t index) {
CG_STRONG_INLINE OutputSocketHandle NodeHandle::output(size_t index) {
return {node_, index};
}

template<typename MT> OutputSocketHandle NodeHandle::output(MT) {
template<typename MT> CG_STRONG_INLINE OutputSocketHandle NodeHandle::output(MT) {
return output(MT::index);
}

inline void Graph::clear() {
CG_STRONG_INLINE void Graph::clear() {
while (!nodes_.empty()) {
if (nodes_.back()) {
erase(NodeHandle{nodes_.size() - 1, *nodes_.back()});
Expand All @@ -212,7 +218,7 @@ inline void Graph::clear() {
ctx_.clear();
}

inline NodeHandle Graph::add(std::unique_ptr<NodeBase> node) {
CG_STRONG_INLINE NodeHandle Graph::add(std::unique_ptr<NodeBase> node) {
if (free_ids_.empty()) {
nodes_.push_back(std::move(node));
addr_to_index_.insert({nodes_.back().get(), nodes_.size() - 1});
Expand All @@ -226,7 +232,7 @@ inline NodeHandle Graph::add(std::unique_ptr<NodeBase> node) {
}
}

inline void Graph::erase(NodeHandle handle) {
CG_STRONG_INLINE void Graph::erase(NodeHandle handle) {
size_t const index = handle.index();
for (size_t i = 0; i < handle->inputs().size(); ++i) {
auto const &input_sock = handle->inputs()[i];
Expand All @@ -249,7 +255,7 @@ inline void Graph::erase(NodeHandle handle) {
free_ids_.push_back(index);
}

inline Link Graph::connect(OutputSocketHandle from, InputSocketHandle to) {
CG_STRONG_INLINE Link Graph::connect(OutputSocketHandle from, InputSocketHandle to) {
if (!can_connect(*from, *to)) {
throw std::invalid_argument("Cannot connect sockets of different types." + to_string(from->type()) + " and " + to_string(to->type()));
}
Expand All @@ -270,11 +276,11 @@ inline Link Graph::connect(OutputSocketHandle from, InputSocketHandle to) {
return Link{from_node, from.index(), to_node, to.index()};
}

inline bool Graph::has_connect(OutputSocketHandle from, InputSocketHandle to) const noexcept {
CG_STRONG_INLINE bool Graph::has_connect(OutputSocketHandle from, InputSocketHandle to) const noexcept {
return to->from() == from.operator->();
}

inline void Graph::erase(Link link) {
CG_STRONG_INLINE void Graph::erase(Link link) {
auto &from = link.from().node();
auto &to = link.to().node();
auto &from_sock = from.outputs_[link.from().index()];
Expand All @@ -287,19 +293,19 @@ inline void Graph::erase(Link link) {
}


inline bool Graph::has_cycle() const noexcept {
CG_STRONG_INLINE bool Graph::has_cycle() const noexcept {
return topology_order().empty();
}

inline void Graph::rebuild_addr_to_index() noexcept {
CG_STRONG_INLINE void Graph::rebuild_addr_to_index() noexcept {
addr_to_index_.clear();
addr_to_index_.reserve(nodes_.size());
for (size_t i = 0; i < nodes_.size(); ++i) {
addr_to_index_[nodes_[i].get()] = i;
}
}

inline void Graph::topology_sort() {
CG_STRONG_INLINE void Graph::topology_sort() {
auto const order = topology_order();
node_container new_nodes;
new_nodes.reserve(nodes_.size());
Expand All @@ -309,7 +315,7 @@ inline void Graph::topology_sort() {
nodes_ = std::move(new_nodes);
}

inline std::vector<size_t> Graph::topology_order() const noexcept {
CG_STRONG_INLINE std::vector<size_t> Graph::topology_order() const noexcept {
std::vector<size_t> result;
size_t const n = nodes_.size();
result.reserve(n);
Expand Down
10 changes: 10 additions & 0 deletions compute_graph/include/compute_graph/intern/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
// | controls whether to use __forceinline or not, see below. |
// | Define this macro to disable __forceinline, otherwise just `inline`. |
// +------------------------------------------------------------------------+
// CG_NODE_EXTENSION:-------------------------------------------------------+
// | A control macro that can be defined to add extra code to the node |
// | class. This is useful for adding extra methods or members to the |
// | node class. |
// +------------------------------------------------------------------------+


#pragma once

#include <cstddef>
Expand All @@ -49,6 +56,9 @@
#endif
#endif

#ifndef CG_NODE_EXTENSION
#define CG_NODE_EXTENSION /* empty */
#endif

namespace compute_graph {

Expand Down
Loading

0 comments on commit 5802cd9

Please sign in to comment.