reminisce commented on a change in pull request #10451: [WIP] Add Foreach URL: https://github.com/apache/incubator-mxnet/pull/10451#discussion_r188507490
########## File path: src/c_api/c_api_symbolic.cc ########## @@ -344,6 +345,33 @@ int MXSymbolGetAtomicSymbolName(AtomicSymbolCreator creator, API_END(); } +int MXSymbolGetInputSymbols(SymbolHandle sym, SymbolHandle **out_arr, int *out_size) { + API_BEGIN(); + nnvm::Symbol *s = static_cast<nnvm::Symbol*>(sym); + nnvm::Graph g; + g.outputs = s->outputs; + std::vector<nnvm::Symbol *> input_syms; + const nnvm::IndexedGraph& idx = g.indexed_graph(); + size_t max_out_size = *out_size; + // Go through all nodes and return the ones representing variables. + for (size_t i = 0; i < idx.num_nodes(); i++) { + const nnvm::Node &n = *idx[i].source; + for (const nnvm::NodeEntry &e : n.inputs) { + auto p = e.node; + if (p->is_variable()) { + nnvm::Symbol *s = new nnvm::Symbol(); + s->outputs.push_back(e); + input_syms.push_back(s); + std::cout << p->attrs.name << std::endl; + } + } + } + CHECK(input_syms.size() <= max_out_size); + *out_size = input_syms.size(); + memcpy(out_arr, input_syms.data(), sizeof(*out_arr) * input_syms.size()); + API_END(); Review comment: Use `API_END_HANDLE_ERROR` to cleanup `input_syms` if an exception is thrown to prevent memory leak. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services