/* ****************************************************************************** * * * This program and the accompanying materials are made available under the * terms of the Apache License, Version 2.0 which is available at * https://www.apache.org/licenses/LICENSE-2.0. * * See the NOTICE file distributed with this work for additional * information regarding copyright ownership. * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. * * SPDX-License-Identifier: Apache-2.0 ******************************************************************************/ // // @author raver119@gmail.com // #ifndef LIBND4J_GRAPH_H #define LIBND4J_GRAPH_H #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace sd { namespace graph { class SD_LIB_EXPORT Graph { protected: ExecutorConfiguration *_configuration; VariableSpace *_variableSpace; Stash *_stash; // this list holds references to Node ptrs, which should be free'd in Graph destructor std::vector _handles; // vector holds ID's of top nodes only std::vector *_nodes; SD_MAP_IMPL *_mapped; SD_MAP_IMPL *> *_onion; SD_MAP_IMPL _unmapped; std::vector _unmappedMap; // macOS? std::mutex _mutexPreprocessing; std::atomic _built; std::vector _output; std::vector _autos; SD_MAP_IMPL _mappedScopes; std::vector _scopes; void expandOnion(int newLayer); void injectNode(Node *node); void pushToOutputOnce(int id); void printOutNode(Node *node); void prepareOutputs(); public: Graph(const ::graph::FlatGraph *flatGraph = nullptr, VariableSpace *variableSpace = nullptr); ~Graph(); // this method applies toposort to nodes void toposortNodes(); // method that'll print out graph Status validate(); // this method will build structured representation of graph Status buildGraph(); // this method will return estimated memory size (in bytes) required for 1 full graph execution round LongType estimateRequiredMemory(); // this method returns number of root nodes in this graph int rootNodes(); // this method returns total number of nodes in this graph int totalNodes(); int numberOfPlaceholders(); std::vector *getPlaceholders(); /** * This method returns pointer to thread_local VariableSpace * @return */ VariableSpace *getVariableSpace(); /** * This method adds given node to the graph * * @param node */ void addNode(Node *node); /** * This method returns layered representation of the graph * * @return */ SD_MAP_IMPL *> *getOnion(); /** * This method returns map of all nodes of the graph * @return */ SD_MAP_IMPL *getMapped(); /** * This method returns outputs of this graph * @return */ std::vector *fetchOutputs(); /** * This method returns pointer to ExecutorConfiguration * * @return */ ExecutorConfiguration *getExecutorConfiguration(); /** * This method returns all nodes at once (order is NOT guaranteed) * @return */ std::vector *getAllNodes(); /** * This method prints out Graph op-by-op, and respective inputs */ void printOut(); /** * This method returns OpScope ptr specified with id * * @param id * @return */ Scope *scopeById(int id); /** * This method returns TRUE if specified ID refers to OpScope, and false otherwise * @param id * @return */ bool hasScope(int id); /** * This method returns clone of the graph */ Graph *clone(); /** * This method returns clone of the graph, backed by VariableProxy instead of VariableSpace */ Graph *cloneWithProxy(); /** * This method removes reference to VariableSpace from this Graph */ void forgetVariableSpace(); /** * This method returns Node with given Id */ Node *nodeById(int nodeId); /** * This method returns True if node with given ID exists, False otherwise * @param nodeId * @return */ bool hasNode(int nodeId); /** * This method returns hash of given Graph instance */ LongType hashCode(); /** * PLEASE NOTE: This method will be moved to private section */ void tagInplaceNodes(); void replaceState(VariableSpace *state, ExecutorConfiguration *configuration); SD_INLINE std::vector *nodes() { return _nodes; } SD_INLINE std::vector *output() { return &_output; } }; } // namespace graph } // namespace sd #endif // LIBND4J_GRAPH_H