diff --git a/include/ion/node.h b/include/ion/node.h index 1939bc50..5f813163 100644 --- a/include/ion/node.h +++ b/include/ion/node.h @@ -91,7 +91,7 @@ class Node { return Port(name, impl_->id); } - std::string id() const { + const std::string& id() const { return impl_->id; } @@ -99,7 +99,7 @@ class Node { return impl_->id; } - std::string name() const { + const std::string& name() const { return impl_->name; } @@ -107,7 +107,7 @@ class Node { return impl_->name; } - Halide::Target target() const { + const Halide::Target& target() const { return impl_->target; } @@ -115,7 +115,7 @@ class Node { return impl_->target; } - std::vector params() const { + const std::vector& params() const { return impl_->params; } @@ -123,7 +123,7 @@ class Node { return impl_->params; } - std::vector ports() const { + const std::vector& ports() const { return impl_->ports; } diff --git a/include/ion/port.h b/include/ion/port.h index 2be7496e..50ce6020 100644 --- a/include/ion/port.h +++ b/include/ion/port.h @@ -27,25 +27,26 @@ class Port { std::string node_id; std::vector params; - Impl() : name(), type(), dimensions(0), node_id(), params() {} + Impl() {} + + Impl(const std::string& n, const Halide::Type& t, int32_t d, const std::string& nid) + : name(n), type(t), dimensions(d), node_id(nid), params({ Halide::Internal::Parameter(type, dimensions != 0, dimensions, argument_name(node_id, name)) }) + {} }; public: friend class Node; friend class nlohmann::adl_serializer; - Port() : impl_(new Impl), index_(-1) {}; - Port(const std::shared_ptr& impl) : impl_(impl), index_(-1) {}; + Port() : impl_(new Impl("", Halide::Type(), 0, "")), index_(-1) {} + Port(const std::shared_ptr& impl) : impl_(impl), index_(-1) {} /** * Construct new port for scalar value. * @arg k: The key of the port which should be matched with BuildingBlock Input/Output name. * @arg t: The type of the value. */ - Port(const std::string& n, Halide::Type t) : impl_(new Impl), index_(-1) { - impl_->name = n; - impl_->type = t; - } + Port(const std::string& n, Halide::Type t) : impl_(new Impl(n, t, 0, "")), index_(-1) {} /** * Construct new port for vector value. @@ -53,11 +54,7 @@ class Port { * @arg t: The type of the element value. * @arg d: The dimension of the port. The range is 1 to 4. */ - Port(const std::string& n, Halide::Type t, int32_t d) : impl_(new Impl), index_(-1) { - impl_->name = n; - impl_->type = t; - impl_->dimensions = d; - } + Port(const std::string& n, Halide::Type t, int32_t d) : impl_(new Impl(n, t, d, "")), index_(-1) {} const std::string& name() const { return impl_->name; } std::string& name() { return impl_->name; } @@ -97,21 +94,24 @@ class Port { template void bind(T v) { auto i = index_ == -1 ? 0 : index_; - impl_->params.resize(i+1, Halide::Internal::Parameter{type(), dimensions() != 0, dimensions(), argument_name(node_id(), name())}); + impl_->params.resize(i+1); + impl_->params[i] = Halide::Internal::Parameter{type(), dimensions() != 0, dimensions(), argument_name(node_id(), name())}; impl_->params[i].set_scalar(v); } template void bind(const Halide::Buffer& buf) { auto i = index_ == -1 ? 0 : index_; - impl_->params.resize(i+1, Halide::Internal::Parameter{type(), dimensions() != 0, dimensions(), argument_name(node_id(), name())}); + impl_->params.resize(i+1); + impl_->params[i] = Halide::Internal::Parameter{type(), dimensions() != 0, dimensions(), argument_name(node_id(), name())}; impl_->params[i].set_buffer(buf); } template void bind(const std::vector>& bufs) { - impl_->params.resize(bufs.size(), Halide::Internal::Parameter{type(), dimensions() != 0, dimensions(), argument_name(node_id(), name())}); + impl_->params.resize(bufs.size()); for (size_t i=0; iparams[i] = Halide::Internal::Parameter{type(), dimensions() != 0, dimensions(), argument_name(node_id(), name())}; impl_->params[i].set_buffer(bufs[i]); } } @@ -130,10 +130,7 @@ class Port { /** * This port is bound with some node. */ - Port(const std::string& n, const std::string& ni) : impl_(new Impl), index_(-1) { - impl_->name = n; - impl_->node_id = ni; - } + Port(const std::string& n, const std::string& nid) : impl_(new Impl(n, Halide::Type(), 0, nid)), index_(-1) {} std::shared_ptr impl_; diff --git a/src/serializer.h b/src/serializer.h index 2a2500ad..8ca31c74 100644 --- a/src/serializer.h +++ b/src/serializer.h @@ -29,13 +29,13 @@ template <> class adl_serializer { public: static void to_json(json& j, const ion::Param& v) { - j["key_"] = v.key(); - j["val_"] = v.val(); + j["key"] = v.key(); + j["val"] = v.val(); } static void from_json(const json& j, ion::Param& v) { - v.key() = j["key_"].get(); - v.val() = j["val_"].get(); + v.key() = j["key"].get(); + v.val() = j["val"].get(); } }; @@ -43,21 +43,26 @@ template<> class adl_serializer { public: static void to_json(json& j, const ion::Port& v) { - j["name_"] = v.name(); - j["type_"] = static_cast(v.type()); - j["dimensions_"] = v.dimensions(); - j["index_"] = v.index(); - j["node_id_"] = v.node_id(); - j["impl_"] = reinterpret_cast(v.impl_.get()); + j["name"] = v.name(); + j["type"] = static_cast(v.type()); + j["dimensions"] = v.dimensions(); + j["index"] = v.index(); + j["node_id"] = v.node_id(); + j["array_size"] = v.params().size(); + j["impl"] = reinterpret_cast(v.impl_.get()); } static void from_json(const json& j, ion::Port& v) { - v = ion::Port(ion::Port::find_impl(j["impl_"].get())); - v.name() = j["name_"].get(); - v.type() = j["type_"].get(); - v.dimensions() = j["dimensions_"]; - v.index() = j["index_"]; - v.node_id() = j["node_id_"].get(); + v = ion::Port(ion::Port::find_impl(j["impl"].get())); + v.name() = j["name"].get(); + v.type() = j["type"].get(); + v.dimensions() = j["dimensions"]; + v.node_id() = j["node_id"].get(); + v.params() = std::vector( + j["array_size"], + Halide::Internal::Parameter(v.type(), v.dimensions() != 0, v.dimensions(), ion::argument_name(v.node_id(), v.name())) + ); + v.index() = j["index"]; } }; diff --git a/test/c_api.cc b/test/c_api.cc index 35751226..d853cbf7 100644 --- a/test/c_api.cc +++ b/test/c_api.cc @@ -114,6 +114,7 @@ int main() if (ret != 0) return ret; + ret = ion_port_destroy(extent1); if (ret != 0) return ret; diff --git a/test/inverted_dep.cc b/test/inverted_dep.cc index 52b30eff..32a634eb 100644 --- a/test/inverted_dep.cc +++ b/test/inverted_dep.cc @@ -22,72 +22,78 @@ int main() "params": [], "ports": [ { - "dimensions_": 0, - "name_": "output", - "index_" : -1, - "node_id_": "1310589d-1448-4107-ac1d-67c32f482906", - "impl_": 1, - "type_": { + "dimensions": 0, + "name": "output", + "index" : -1, + "node_id": "1310589d-1448-4107-ac1d-67c32f482906", + "array_size": 1, + "impl": 1, + "type": { "bits": 0, "code": 3, "lanes": 0 } }, { - "dimensions_": 0, - "name_": "min0", - "index_" : -1, - "node_id_": "", - "impl_": 2, - "type_": { + "dimensions": 0, + "name": "min0", + "index" : -1, + "node_id": "", + "array_size": 1, + "impl": 2, + "type": { "bits": 32, "code": 0, "lanes": 1 } }, { - "dimensions_": 0, - "name_": "extent0", - "index_" : -1, - "node_id_": "", - "impl_": 3, - "type_": { + "dimensions": 0, + "name": "extent0", + "index" : -1, + "node_id": "", + "array_size": 1, + "impl": 3, + "type": { "bits": 32, "code": 0, "lanes": 1 } }, { - "dimensions_": 0, - "name_": "min1", - "index_" : -1, - "node_id_": "", - "impl_": 5, - "type_": { + "dimensions": 0, + "name": "min1", + "index" : -1, + "node_id": "", + "array_size": 1, + "impl": 5, + "type": { "bits": 32, "code": 0, "lanes": 1 } }, { - "dimensions_": 0, - "name_": "extent1", - "index_" : -1, - "node_id_": "", - "impl_": 6, - "type_": { + "dimensions": 0, + "name": "extent1", + "index" : -1, + "node_id": "", + "array_size": 1, + "impl": 6, + "type": { "bits": 32, "code": 0, "lanes": 1 } }, { - "dimensions_": 0, - "name_": "v", - "index_" : -1, - "node_id_": "", - "impl_": 7, - "type_": { + "dimensions": 0, + "name": "v", + "index" : -1, + "node_id": "", + "array_size": 1, + "impl": 7, + "type": { "bits": 32, "code": 0, "lanes": 1