diff --git a/include/aidge/utils/FileManagement.hpp b/include/aidge/utils/FileManagement.hpp new file mode 100644 index 0000000000000000000000000000000000000000..8158fbf19f9e6c62ee5cc967c4a2bb03ba09d0a2 --- /dev/null +++ b/include/aidge/utils/FileManagement.hpp @@ -0,0 +1,28 @@ +/******************************************************************************** + * Copyright (c) 2023 CEA-List + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * SPDX-License-Identifier: EPL-2.0 + * + ********************************************************************************/ + +#include <cstdio> // std::fclose, std::fopen +#include <memory> +#include <string> + +namespace Aidge { +struct FileDeleter { + void operator()(FILE* fp) const { + if (fp) { + std::fclose(fp); + } + } +}; + +inline std::unique_ptr<FILE, FileDeleter> createFile(const std::string& fileName, const char* accessibility = "w") { + return std::unique_ptr<FILE, FileDeleter>(std::fopen(fileName.c_str(), accessibility)); +} +} diff --git a/src/graph/GraphView.cpp b/src/graph/GraphView.cpp index 315844858103cbce91049ec2195ff0a3bd7a9d81..d0b539182fc308c87f0fac11ab8fcdf15793c1f2 100644 --- a/src/graph/GraphView.cpp +++ b/src/graph/GraphView.cpp @@ -33,6 +33,7 @@ #include "aidge/operator/Producer.hpp" #include "aidge/operator/Memorize.hpp" #include "aidge/utils/Directories.hpp" +#include "aidge/utils/FileManagement.hpp" #include "aidge/utils/ErrorHandling.hpp" #include "aidge/utils/Types.h" @@ -85,7 +86,7 @@ bool Aidge::GraphView::inView(const std::string& nodeName) const { } void Aidge::GraphView::save(const std::string& path, bool verbose, bool showProducers) const { - auto fp = std::unique_ptr<FILE, decltype(&std::fclose)>(std::fopen((path + ".mmd").c_str(), "w"), &std::fclose); + auto fp = createFile(path + ".mmd", "w"); if (!fp) { AIDGE_THROW_OR_ABORT(std::runtime_error, @@ -261,7 +262,7 @@ void Aidge::GraphView::logOutputs(const std::string& dirName) const { for (IOIndex_t outIdx = 0; outIdx < nodePtr->nbOutputs(); ++outIdx) { const std::string& inputPath = nodePath +"output_" + std::to_string(outIdx) + ".log"; - auto fp = std::unique_ptr<FILE, decltype(&std::fclose)>(std::fopen(inputPath.c_str(), "w"), &std::fclose); + auto fp = createFile(inputPath, "w"); if (!fp) { AIDGE_THROW_OR_ABORT(std::runtime_error, "Could not create graph view log file: {}", inputPath); @@ -1122,7 +1123,7 @@ void Aidge::GraphView::insertParent(NodePtr childNode, * | >1 node, 1 input | trivial | trivial | broadcast | broadcast | * | 1 node, >1 inputs | (take first) | (take first) | same order | X | * | >1 node, >1 inputs | X | X | X | X | - * + * * Outputs conditions: * | old \ new | 1 node, 1 output | >1 node, 1 output | 1 node, >1 outputs | >1 node, >1 outputs | * | ------------------- | ---------------- | ----------------- | ------------------ | ------------------- | @@ -1130,7 +1131,7 @@ void Aidge::GraphView::insertParent(NodePtr childNode, * | >1 node, 1 output | trivial | trivial | take first | X | * | 1 node, >1 outputs | (take first) | (take first) | same order | X | * | >1 node, >1 outputs | X | X | X | X | - * + * * Only the X cases cannot possibly be resolved deterministically with sets of node. * These cases are therefore forbidden for the set-based `replace()` interface. * The remaining cases are handled by the GraphView-based `replace()` interface. diff --git a/src/scheduler/Scheduler.cpp b/src/scheduler/Scheduler.cpp index fabdc7ad2a897708297f6fac25f036b45bd3b2b2..eba195ad967983c08ca7247bd5ef2f37362dfa70 100644 --- a/src/scheduler/Scheduler.cpp +++ b/src/scheduler/Scheduler.cpp @@ -34,6 +34,7 @@ #include "aidge/operator/OperatorTensor.hpp" #include "aidge/operator/Producer.hpp" #include "aidge/operator/Concat.hpp" +#include "aidge/utils/FileManagement.hpp" #include "aidge/utils/Log.hpp" #include "aidge/utils/Types.h" @@ -672,7 +673,7 @@ Aidge::MemoryManager Aidge::Scheduler::generateMemoryAutoConcat(bool incProducer std::shared_ptr<Node> concat = nullptr; // If the only child is a concatenation, check if we can allocate - // the concatenation result directly and skip allocation for this + // the concatenation result directly and skip allocation for this // node output: if (childs.size() == 1 && (*childs.begin())->type() == Concat_Op::Type) { concat = *childs.begin(); @@ -901,7 +902,7 @@ void Aidge::Scheduler::connectInputs(const std::vector<std::shared_ptr<Aidge::Te } void Aidge::Scheduler::saveSchedulingDiagram(const std::string& fileName) const { - auto fp = std::unique_ptr<FILE, decltype(&std::fclose)>(std::fopen((fileName + ".mmd").c_str(), "w"), &std::fclose); + auto fp = createFile(fileName + ".mmd", "w"); if (!fp) { AIDGE_THROW_OR_ABORT(std::runtime_error, @@ -931,7 +932,7 @@ void Aidge::Scheduler::saveSchedulingDiagram(const std::string& fileName) const } void Aidge::Scheduler::saveStaticSchedulingDiagram(const std::string& fileName) const { - auto fp = std::unique_ptr<FILE, decltype(&std::fclose)>(std::fopen((fileName + ".mmd").c_str(), "w"), &std::fclose); + auto fp = createFile(fileName + ".mmd", "w"); if (!fp) { AIDGE_THROW_OR_ABORT(std::runtime_error, @@ -966,7 +967,7 @@ void Aidge::Scheduler::saveStaticSchedulingDiagram(const std::string& fileName) } void Aidge::Scheduler::saveFactorizedStaticSchedulingDiagram(const std::string& fileName, size_t minRepeat) const { - auto fp = std::unique_ptr<FILE, decltype(&std::fclose)>(std::fopen((fileName + ".mmd").c_str(), "w"), &std::fclose); + auto fp = createFile(fileName + ".mmd", "w"); if (!fp) { AIDGE_THROW_OR_ABORT(std::runtime_error,