Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Fixstars-iizuka committed Dec 21, 2023
1 parent 39ca5dc commit df33e8b
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 76 deletions.
10 changes: 5 additions & 5 deletions include/ion/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,39 +91,39 @@ class Node {
return Port(name, impl_->id);
}

std::string id() const {
const std::string& id() const {
return impl_->id;
}

std::string& id() {
return impl_->id;
}

std::string name() const {
const std::string& name() const {
return impl_->name;
}

std::string& name(){
return impl_->name;
}

Halide::Target target() const {
const Halide::Target& target() const {
return impl_->target;
}

Halide::Target& target() {
return impl_->target;
}

std::vector<Param> params() const {
const std::vector<Param>& params() const {
return impl_->params;
}

std::vector<Param>& params() {
return impl_->params;
}

std::vector<Port> ports() const {
const std::vector<Port>& ports() const {
return impl_->ports;
}

Expand Down
35 changes: 16 additions & 19 deletions include/ion/port.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,34 @@ class Port {
std::string node_id;
std::vector<Halide::Internal::Parameter> 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>;

Port() : impl_(new Impl), index_(-1) {};
Port(const std::shared_ptr<Impl>& impl) : impl_(impl), index_(-1) {};
Port() : impl_(new Impl("", Halide::Type(), 0, "")), index_(-1) {}
Port(const std::shared_ptr<Impl>& 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.
* @arg k: The key of the port which should be matched with BuildingBlock Input/Output name.
* @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; }
Expand Down Expand Up @@ -97,21 +94,24 @@ class Port {
template<typename T>
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<typename T>
void bind(const Halide::Buffer<T>& 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<typename T>
void bind(const std::vector<Halide::Buffer<T>>& 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; i<bufs.size(); ++i) {
impl_->params[i] = Halide::Internal::Parameter{type(), dimensions() != 0, dimensions(), argument_name(node_id(), name())};
impl_->params[i].set_buffer(bufs[i]);
}
}
Expand All @@ -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> impl_;

Expand Down
37 changes: 21 additions & 16 deletions src/serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,40 @@ template <>
class adl_serializer<ion::Param> {
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<std::string>();
v.val() = j["val_"].get<std::string>();
v.key() = j["key"].get<std::string>();
v.val() = j["val"].get<std::string>();
}
};

template<>
class adl_serializer<ion::Port> {
public:
static void to_json(json& j, const ion::Port& v) {
j["name_"] = v.name();
j["type_"] = static_cast<halide_type_t>(v.type());
j["dimensions_"] = v.dimensions();
j["index_"] = v.index();
j["node_id_"] = v.node_id();
j["impl_"] = reinterpret_cast<uintptr_t>(v.impl_.get());
j["name"] = v.name();
j["type"] = static_cast<halide_type_t>(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<uintptr_t>(v.impl_.get());
}

static void from_json(const json& j, ion::Port& v) {
v = ion::Port(ion::Port::find_impl(j["impl_"].get<uintptr_t>()));
v.name() = j["name_"].get<std::string>();
v.type() = j["type_"].get<halide_type_t>();
v.dimensions() = j["dimensions_"];
v.index() = j["index_"];
v.node_id() = j["node_id_"].get<std::string>();
v = ion::Port(ion::Port::find_impl(j["impl"].get<uintptr_t>()));
v.name() = j["name"].get<std::string>();
v.type() = j["type"].get<halide_type_t>();
v.dimensions() = j["dimensions"];
v.node_id() = j["node_id"].get<std::string>();
v.params() = std::vector<Halide::Internal::Parameter>(
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"];
}
};

Expand Down
1 change: 1 addition & 0 deletions test/c_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ int main()
if (ret != 0)
return ret;


ret = ion_port_destroy(extent1);
if (ret != 0)
return ret;
Expand Down
78 changes: 42 additions & 36 deletions test/inverted_dep.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit df33e8b

Please sign in to comment.