Skip to content

Commit

Permalink
Merge branch 'master' into multidim
Browse files Browse the repository at this point in the history
  • Loading branch information
yungyuc authored Sep 8, 2024
2 parents 9c4e122 + da4328f commit 405b41f
Show file tree
Hide file tree
Showing 38 changed files with 1,405 additions and 1,415 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,12 @@ jobs:
strategy:
matrix:
# https://github.com/actions/runner-images/blob/main/images/macos/macos-13-Readme.md
os: [macos-13, macos-14]
# https://github.com/actions/runner-images/blob/main/images/macos/macos-14-arm64-Readme.md
# 20240811: clang-tidy (llvm@16) runs extremely slowly on macos-14
# (i.e., macos-14-arm64), but not macos-13. Temporarily turn off the
# linter run on macos-14 and revisit in the future.
#os: [macos-13, macos-14]
os: [macos-13]
cmake_build_type: [Debug]

fail-fast: false
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Click the Windows release run and scroll down to the "artifacts" section to
download the zip file (login to `GitHub <https://github.com/>`__ is required).
A direct download link can be found in https://doc.solvcon.net/.

Refenreces
References
==========

* The numerical notes: https://github.com/solvcon/mmnote.
Expand Down
28 changes: 24 additions & 4 deletions STYLE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ style when adding new code and changing existing code. The rules of thumb are:
1. The linters must be clean. Before creating and updating a
`pull request <https://docs.github.com/en/pull-requests/>`__, run:

.. code-block::
.. code-block:: bash
make lint
Expand Down Expand Up @@ -259,7 +259,7 @@ The inclusion guard uses ``#pragma once`` in the first line before everything.
Always use path-first inclusion (angle branket). Do not use current-first
(double quote).

.. code-block::
.. code-block:: cpp
// Use this: search for include file start with the paths to the compiler.
#include <modmesh/base.hpp>
Expand Down Expand Up @@ -440,6 +440,26 @@ hundreds of lines) to keep track of.
} /* end namespace modmesh */
C++ Curly Braces
====

Always add curly braces and always add them in standalone lines:

.. code-block:: cpp
if (condition)
{
return;
}
That is, never drop curly braces even when you can:

.. code-block:: cpp
// NEVER DROP CURLY BRACES
if (condition)
return;
Copyright Notice
====

Expand All @@ -448,7 +468,7 @@ When creating a new file, put the following text at the top of the file
(replace ``<Year>`` with the year you create the file and ``<Your Name>`` with
your name and maybe email). The license text formatted for C++ files:

.. code-block::
.. code-block:: cpp
/*
* Copyright (c) <Year>, <Your Name>
Expand Down Expand Up @@ -480,7 +500,7 @@ your name and maybe email). The license text formatted for C++ files:
The license text formatted for Python files:

.. code-block::
.. code-block:: python
# -*- coding: UTF-8 -*-
#
Expand Down
2 changes: 2 additions & 0 deletions cpp/modmesh/buffer/SimpleArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,3 +515,5 @@ SimpleArrayPlex::~SimpleArrayPlex()
}

} /* end namespace modmesh */

// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4:
2 changes: 2 additions & 0 deletions cpp/modmesh/buffer/pymod/TypeBroadcast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,5 @@ struct TypeBroadcast

} /* end namespace python */
} /* end namespace modmesh */

// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4:
2 changes: 2 additions & 0 deletions cpp/modmesh/inout/pymod/wrap_Gmsh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ void wrap_Gmsh(pybind11::module & mod)
} /* end namespace python */

} /* end namespace modmesh */

// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4:
2 changes: 2 additions & 0 deletions cpp/modmesh/inout/pymod/wrap_Plot3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ void wrap_Plot3d(pybind11::module & mod)
} /* end namespace python */

} /* end namespace modmesh */

// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4:
2 changes: 2 additions & 0 deletions cpp/modmesh/onedim/Euler1DCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,5 @@ SimpleArray<double> Euler1DCore::entropy() const

} /* end namespace onedim */
} /* end namespace modmesh */

// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4:
4 changes: 3 additions & 1 deletion cpp/modmesh/onedim/Euler1DCore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,4 +344,6 @@ inline void Euler1DCore::march_alpha(size_t steps)
}

} /* end namespace onedim */
} /* end namespace modmesh */
} /* end namespace modmesh */

// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4:
2 changes: 2 additions & 0 deletions cpp/modmesh/spacetime/kernel/BadEuler1DSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,5 @@ void BadEuler1DSolver::treat_boundary_so1()

} /* end namespace spacetime */
} /* end namespace modmesh */

// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4:
4 changes: 3 additions & 1 deletion cpp/modmesh/spacetime/kernel/BadEuler1DSolver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,6 @@ inline void BadEuler1DSolver::march_alpha(size_t steps)
}

} /* end namespace spacetime */
} /* end namespace modmesh */
} /* end namespace modmesh */

// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4:
2 changes: 2 additions & 0 deletions cpp/modmesh/testhelper/pymod/testbuffer_pymod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,5 @@ void initialize_testbuffer(pybind11::module & mod)
} /* end namespace python */

} /* end namespace modmesh */

// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4:
1 change: 1 addition & 0 deletions cpp/modmesh/toggle/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set(MODMESH_TOGGLE_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/profile.hpp
${CMAKE_CURRENT_SOURCE_DIR}/RadixTree.hpp
${CMAKE_CURRENT_SOURCE_DIR}/toggle.hpp
${CMAKE_CURRENT_SOURCE_DIR}/SerializableProfiler.hpp
CACHE FILEPATH "" FORCE)

set(MODMESH_TOGGLE_SOURCES
Expand Down
188 changes: 25 additions & 163 deletions cpp/modmesh/toggle/RadixTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,33 @@ void CallProfiler::reset()
}
m_radix_tree.reset();
m_cancel_callbacks.clear();
m_pending_nodes.clear();
}

// Called when a function starts
void CallProfiler::start_caller(const std::string & caller_name, const std::function<void()> & cancel_callback)
{
m_cancel_callbacks.push_back(cancel_callback);
m_radix_tree.entry(caller_name);
CallerProfile & callProfile = m_radix_tree.get_current_node()->data();
callProfile.caller_name = caller_name;
callProfile.start_stopwatch();
}

// Called when a function ends
void CallProfiler::end_caller()
{
CallerProfile & call_profile = m_radix_tree.get_current_node()->data();
call_profile.stop_stopwatch(); // Update profiling information to the pending time and count
m_pending_nodes.insert(m_radix_tree.get_current_node());
m_radix_tree.move_current_to_parent(); // Pop the caller from the call stack

if (m_radix_tree.get_current_node() == m_radix_tree.get_root()) // If the root function ends, update all pending nodes and stable items
{
update_pending_nodes();
m_radix_tree.update_stable_items();
}
}
// NOLINTNEXTLINE(misc-no-recursion)
void CallProfiler::print_profiling_result(const RadixTreeNode<CallerProfile> & node, const int depth, std::ostream & outstream) const
{
Expand Down Expand Up @@ -116,169 +141,6 @@ void CallProfiler::print_statistics(const RadixTreeNode<CallerProfile> & node, s
TimeRegistry::me().clear();
}

void CallProfilerSerializer::serialize_call_profiler(const CallProfiler & profiler, std::ostream & outstream)
{
// Serialize the RadixTree in the CallProfiler.
outstream << R"({)" << '\n';
CallProfilerSerializer::serialize_radix_tree(profiler, outstream);
outstream << R"(})" << '\n';
}

void CallProfilerSerializer::serialize_radix_tree(const CallProfiler & profiler, std::ostream & outstream)
{
// Serialize the RadixTree.
outstream << R"( "radix_tree": {)" << '\n';
outstream << R"( "current_node": )" << profiler.radix_tree().get_current_node()->key() << R"(,)" << '\n';
outstream << R"( "unique_id": )" << profiler.radix_tree().get_unique_node() << R"(,)" << '\n';
CallProfilerSerializer::serialize_id_map(profiler.radix_tree().get_id_map(RadixTree<CallerProfile>::CallProfilerPK()), outstream);
CallProfilerSerializer::serialize_radix_tree_nodes(profiler.radix_tree().get_current_node(), outstream);
outstream << R"( })" << '\n';
}

void CallProfilerSerializer::serialize_id_map(const std::unordered_map<std::string, CallProfilerSerializer::key_type> & id_map, std::ostream & outstream)
{
// Serialize the unordered_map in RadixTree.
outstream << R"( "id_map": {)";

// If the id_map is empty, close the map at the same line.
if (id_map.empty())
{
outstream << R"(},)" << '\n';
}
else
{
// Newline after the opening brace.
outstream << '\n';

bool is_first = true;
for (const auto & [key, value] : id_map)
{
// Avoid the trailing comma for the first element.
if (!is_first)
{
outstream << R"(,)" << '\n';
}
is_first = false;
outstream << R"( ")" << key << R"(": )" << value;
}

// Newline after the last element.
outstream << '\n';
outstream << R"( },)" << '\n';
}
}

void CallProfilerSerializer::serialize_radix_tree_nodes(const RadixTreeNode<CallerProfile> * node, std::ostream & outstream)
{
// Serialize all the RadixTreeNodes in RadixTree in a breadth-first manner.
outstream << R"( "nodes": [)";

// Give each node a unique number
int unique_node_number = -1;

std::queue<const RadixTreeNode<CallerProfile> *> nodes_buffer;
CallProfilerSerializer::node_to_number_map_type node_to_unique_number;

nodes_buffer.push(node);
node_to_unique_number[node] = unique_node_number;
bool is_first_node = true;

// BFS algorithm
while (!nodes_buffer.empty())
{
const int nodes_buffer_size = nodes_buffer.size();
for (int i = 0; i < nodes_buffer_size; ++i)
{
const RadixTreeNode<CallerProfile> * current_node = nodes_buffer.front();
nodes_buffer.pop();

for (const auto & child : current_node->children())
{
++unique_node_number;
nodes_buffer.push(child.get());
node_to_unique_number[child.get()] = unique_node_number;
}

CallProfilerSerializer::serialize_radix_tree_node(*current_node, is_first_node, node_to_unique_number, outstream);
is_first_node = false;

// Remove the node from the map
node_to_unique_number.erase(current_node);
}
}

// Newline after the last element.
outstream << '\n';
outstream << R"( ])" << '\n';
}

void CallProfilerSerializer::serialize_radix_tree_node(const RadixTreeNode<CallerProfile> & node, bool is_first_node, CallProfilerSerializer::node_to_number_map_type & node_to_unique_number, std::ostream & outstream)
{
// Serialize the RadixTreeNode to the json format.

// Avoid the trailing comma for the first node.
if (!is_first_node)
{
outstream << R"(,)" << '\n';
outstream << R"( {)" << '\n';
}
else
{
outstream << R"({)" << '\n';
}

outstream << R"( "unique_number": )" << node_to_unique_number[&node] << R"(,)" << '\n';
outstream << R"( "key": )" << node.key() << R"(,)" << '\n';
outstream << R"( "name": ")" << node.name() << R"(",)" << '\n';
CallProfilerSerializer::serialize_caller_profile(node.data(), outstream);
CallProfilerSerializer::serialize_radix_tree_node_children(node.children(), node_to_unique_number, outstream);

outstream << R"( })";
}

void CallProfilerSerializer::serialize_radix_tree_node_children(const CallProfilerSerializer::child_list_type & children, CallProfilerSerializer::node_to_number_map_type & node_to_unique_number, std::ostream & outstream)
{
// Serialize the children list in RadixTreeNode.
outstream << R"( children": [)";

// If the children list is empty, close the list at the same line.
if (children.empty())
{
outstream << R"(])" << '\n';
}

else
{
outstream << '\n';

bool is_first_child = true;
for (const auto & child : children)
{
// Avoid the trailing comma.
if (!is_first_child)
{
outstream << R"(,)" << '\n';
}
is_first_child = false;
outstream << R"( )" << node_to_unique_number[child.get()] << R"()";
}
outstream << '\n';
outstream << R"( ])" << '\n';
}
}

void CallProfilerSerializer::serialize_caller_profile(const CallerProfile & profile, std::ostream & outstream)
{
// Serialize the CallerProfile to the json format.
outstream << R"( "data": {)" << '\n';
outstream << R"( "start_time": )" << profile.start_time.time_since_epoch().count() << R"(,)" << '\n';
outstream << R"( "caller_name": ")" << profile.caller_name << R"(",)" << '\n';
outstream << R"( "total_time": )" << profile.total_time.count() << R"(,)" << '\n';
outstream << R"( "call_count": )" << profile.call_count << R"(,)" << '\n';
outstream << R"( "is_running": )" << profile.is_running << '\n';
outstream << R"( },)" << '\n';
}

} /* end namespace modmesh */

// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4:
Loading

0 comments on commit 405b41f

Please sign in to comment.