Skip to content

Commit

Permalink
[tag] v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunelFeng committed Sep 15, 2024
1 parent 2f037b0 commit a2b98e1
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 33 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024-2025 Chunel Feng

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.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# CGraph-lite
head-only [CGraph](https://github.com/ChunelFeng/CGraph)-lite, simplest dag executor with cpp14 only.
CGraph-lite is a lite and head-only CGraph-API-liked project, with only simple DAG executor and param transfer function.

If you want more, click here : [CGraph](https://github.com/ChunelFeng/CGraph)
3 changes: 1 addition & 2 deletions T02-Param.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
const std::string& kParamKey = "param_key";

struct MyParam : public GParam {
CStatus setup() override {
void reset(const CStatus& curStatus) override {
val_ = 0;
return CStatus();
}

int val_ { 0 };
Expand Down
5 changes: 5 additions & 0 deletions src/param.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
#ifndef CGRAPH_LITE_PARAM_H
#define CGRAPH_LITE_PARAM_H

#include <mutex>

#include "status.h"

class GParam {
public:
std::mutex _param_shared_lock_;

protected:
/**
* exec before all node run.
Expand Down
55 changes: 25 additions & 30 deletions src/pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ class GPipeline {
* @return
*/
CStatus process(size_t times = 1) {
auto status = init();
while (times-- && status.isOK()) {
status += run();
init();
while (times-- && status_.isOK()) {
run();
}
status += destroy();
return status;
destroy();
return status_;
}

/**
Expand All @@ -53,8 +53,7 @@ class GPipeline {
if (std::any_of(depends.begin(), depends.end(), [](GElement* ptr) {
return !ptr;
})) {
// no allow empty input
return CStatus("input is null");
return CStatus("input is null"); // no allow empty input
}

(*elementRef) = new(std::nothrow) T();
Expand All @@ -64,27 +63,25 @@ class GPipeline {
}

protected:
CStatus init() {
CStatus status;
void init() {
status_ = CStatus();
for (auto* element : elements_) {
status += element->init();
status_ += element->init();
}
schedule_ = std::make_unique<Schedule>();
return status;
}

CStatus run() {
void run() {
setup();
executeAll();
return fetchResult();
reset();
}

CStatus destroy() {
CStatus status;
void destroy() {
for (auto* element : elements_) {
status += element->destroy();
status_ += element->destroy();
}
return status;
schedule_.reset();
}

void executeAll() {
Expand Down Expand Up @@ -117,20 +114,7 @@ class GPipeline {
}
}

CStatus fetchResult() {
{
std::unique_lock<std::mutex> lk(execute_mutex_);
execute_cv_.wait(lk, [this] {
return finished_size_ >= elements_.size() || !status_.isOK();
});
}

param_manager_.reset(status_);
return status_;
}

void setup() {
status_ = CStatus();
finished_size_ = 0;
for (auto* element : elements_) {
element->left_depend_ = element->dependence_.size();
Expand All @@ -139,6 +123,17 @@ class GPipeline {
status_ += param_manager_.setup();
}

void reset() {
// wait for all node finished
{
std::unique_lock<std::mutex> lk(execute_mutex_);
execute_cv_.wait(lk, [this] {
return finished_size_ >= elements_.size() || !status_.isOK();
});
}

param_manager_.reset(status_);
}

private:
std::vector<GElement *> elements_ {};
Expand Down

0 comments on commit a2b98e1

Please sign in to comment.