Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #69 from mlcommons/cpp-lint
Browse files Browse the repository at this point in the history
Reformat C++ codes with clang-format and introduce C++ linting via GitHub actions
  • Loading branch information
srinivas212 committed Dec 6, 2023
2 parents ac2fb60 + 263515e commit e6e0a34
Show file tree
Hide file tree
Showing 8 changed files with 467 additions and 374 deletions.
88 changes: 88 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---
AccessModifierOffset: -1
AlignAfterOpenBracket: AlwaysBreak
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlinesLeft: true
AlignOperands: false
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: true
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: false
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ForEachMacros: [ FOR_EACH_RANGE, FOR_EACH, ]
IncludeCategories:
- Regex: '^<.*\.h(pp)?>'
Priority: 1
- Regex: '^<.*'
Priority: 2
- Regex: '.*'
Priority: 3
IndentCaseLabels: true
IndentWidth: 2
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: false
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 2000000
PointerAlignment: Left
ReflowComments: true
SortIncludes: true
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 8
UseTab: Never
...
15 changes: 15 additions & 0 deletions .github/workflows/cpp_lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Clang-format Full Tree
on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: DoozyX/clang-format-lint-action@v0.11
with:
source: '.'
extensions: 'cc,cpp,h,hh'
clangFormatVersion: 11
style: file
44 changes: 23 additions & 21 deletions et_feeder/et_feeder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using namespace std;
using namespace Chakra;

ETFeeder::ETFeeder(string filename)
: trace_(filename), window_size_(4096 * 256), et_complete_(false) {
: trace_(filename), window_size_(4096 * 256), et_complete_(false) {
if (!trace_.is_open()) { // Assuming a method to check if file is open
throw std::runtime_error("Failed to open trace file: " + filename);
}
Expand All @@ -14,12 +14,11 @@ ETFeeder::ETFeeder(string filename)
readNextWindow();
} catch (const std::exception& e) {
cerr << "Error in constructor: " << e.what() << endl;
throw; // Rethrow the exception for caller to handle
throw; // Rethrow the exception for caller to handle
}
}

ETFeeder::~ETFeeder() {
}
ETFeeder::~ETFeeder() {}

void ETFeeder::addNode(shared_ptr<ETFeederNode> node) {
dep_graph_[node->getChakraNode()->id()] = node;
Expand All @@ -28,8 +27,7 @@ void ETFeeder::addNode(shared_ptr<ETFeederNode> node) {
void ETFeeder::removeNode(uint64_t node_id) {
dep_graph_.erase(node_id);

if (!et_complete_
&& (dep_free_node_queue_.size() < window_size_)) {
if (!et_complete_ && (dep_free_node_queue_.size() < window_size_)) {
readNextWindow();
}
}
Expand Down Expand Up @@ -61,11 +59,11 @@ shared_ptr<ETFeederNode> ETFeeder::lookupNode(uint64_t node_id) {

void ETFeeder::freeChildrenNodes(uint64_t node_id) {
shared_ptr<ETFeederNode> node = dep_graph_[node_id];
for (auto child: node->getChildren()) {
for (auto child : node->getChildren()) {
auto child_chakra = child->getChakraNode();
for (auto it = child_chakra->mutable_data_deps()->begin();
it != child_chakra->mutable_data_deps()->end();
++it) {
it != child_chakra->mutable_data_deps()->end();
++it) {
if (*it == node_id) {
child_chakra->mutable_data_deps()->erase(it);
break;
Expand All @@ -80,14 +78,17 @@ void ETFeeder::freeChildrenNodes(uint64_t node_id) {

void ETFeeder::readGlobalMetadata() {
if (!trace_.is_open()) {
throw runtime_error("Trace file closed unexpectedly during reading global metadata.");
throw runtime_error(
"Trace file closed unexpectedly during reading global metadata.");
}
shared_ptr<ChakraProtoMsg::GlobalMetadata> pkt_msg = make_shared<ChakraProtoMsg::GlobalMetadata>();
shared_ptr<ChakraProtoMsg::GlobalMetadata> pkt_msg =
make_shared<ChakraProtoMsg::GlobalMetadata>();
trace_.read(*pkt_msg);
}

shared_ptr<ETFeederNode> ETFeeder::readNode() {
shared_ptr<ChakraProtoMsg::Node> pkt_msg = make_shared<ChakraProtoMsg::Node>();
shared_ptr<ChakraProtoMsg::Node> pkt_msg =
make_shared<ChakraProtoMsg::Node>();
if (!trace_.read(*pkt_msg)) {
return nullptr;
}
Expand All @@ -113,11 +114,12 @@ shared_ptr<ETFeederNode> ETFeeder::readNode() {

void ETFeeder::resolveDep() {
for (auto it = dep_unresolved_node_set_.begin();
it != dep_unresolved_node_set_.end();) {
it != dep_unresolved_node_set_.end();) {
shared_ptr<ETFeederNode> node = *it;
vector<uint64_t> dep_unresolved_parent_ids = node->getDepUnresolvedParentIDs();
vector<uint64_t> dep_unresolved_parent_ids =
node->getDepUnresolvedParentIDs();
for (auto inner_it = dep_unresolved_parent_ids.begin();
inner_it != dep_unresolved_parent_ids.end();) {
inner_it != dep_unresolved_parent_ids.end();) {
auto parent_node = dep_graph_.find(*inner_it);
if (parent_node != dep_graph_.end()) {
parent_node->second->addChild(node);
Expand All @@ -137,7 +139,8 @@ void ETFeeder::resolveDep() {

void ETFeeder::readNextWindow() {
if (!trace_.is_open()) {
throw runtime_error("Trace file closed unexpectedly during reading next window.");
throw runtime_error(
"Trace file closed unexpectedly during reading next window.");
}
uint32_t num_read = 0;
do {
Expand All @@ -151,14 +154,13 @@ void ETFeeder::readNextWindow() {
++num_read;

resolveDep();
} while ((num_read < window_size_)
|| (dep_unresolved_node_set_.size() != 0));
} while ((num_read < window_size_) || (dep_unresolved_node_set_.size() != 0));

for (auto node_id_node: dep_graph_) {
for (auto node_id_node : dep_graph_) {
uint64_t node_id = node_id_node.first;
shared_ptr<ETFeederNode> node = node_id_node.second;
if ((dep_free_node_id_set_.count(node_id) == 0)
&& (node->getChakraNode()->data_deps().size() == 0)) {
if ((dep_free_node_id_set_.count(node_id) == 0) &&
(node->getChakraNode()->data_deps().size() == 0)) {
dep_free_node_id_set_.emplace(node_id);
dep_free_node_queue_.emplace(node);
}
Expand Down
19 changes: 13 additions & 6 deletions et_feeder/et_feeder.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
#include <unordered_set>
#include <vector>

#include "third_party/utils/protoio.hh"
#include "et_feeder/et_feeder_node.h"
#include "third_party/utils/protoio.hh"

namespace Chakra {
struct CompareNodes: public std::binary_function<std::shared_ptr<ETFeederNode>, std::shared_ptr<ETFeederNode>, bool>
{
bool operator()(const std::shared_ptr<ETFeederNode> lhs, const std::shared_ptr<ETFeederNode> rhs) const
{
struct CompareNodes : public std::binary_function<
std::shared_ptr<ETFeederNode>,
std::shared_ptr<ETFeederNode>,
bool> {
bool operator()(
const std::shared_ptr<ETFeederNode> lhs,
const std::shared_ptr<ETFeederNode> rhs) const {
return lhs->getChakraNode()->id() > rhs->getChakraNode()->id();
}
};
Expand Down Expand Up @@ -43,7 +46,11 @@ class ETFeeder {

std::unordered_map<uint64_t, std::shared_ptr<ETFeederNode>> dep_graph_{};
std::unordered_set<uint64_t> dep_free_node_id_set_{};
std::priority_queue<std::shared_ptr<ETFeederNode>, std::vector<std::shared_ptr<ETFeederNode>>, CompareNodes> dep_free_node_queue_{};
std::priority_queue<
std::shared_ptr<ETFeederNode>,
std::vector<std::shared_ptr<ETFeederNode>>,
CompareNodes>
dep_free_node_queue_{};
std::unordered_set<std::shared_ptr<ETFeederNode>> dep_unresolved_node_set_{};
};

Expand Down
Loading

0 comments on commit e6e0a34

Please sign in to comment.