Skip to content

Commit

Permalink
Fixed Port::find_impl to avoid unexpected cache collision
Browse files Browse the repository at this point in the history
  • Loading branch information
Fixstars-iizuka committed Jan 18, 2024
1 parent 11ba17e commit b91091b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 51 deletions.
36 changes: 5 additions & 31 deletions include/ion/port.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Port {

private:
struct Impl {
std::string id;
Channel pred_chan;
std::set<Channel> succ_chans;

Expand All @@ -62,20 +63,8 @@ class Port {
std::unordered_map<int32_t, Halide::Internal::Parameter> params;
std::unordered_map<int32_t, const void *> instances;

Impl() {}

Impl(const std::string& pid, const std::string& pn, const Halide::Type& t, int32_t d)
: pred_chan{pid, pn}, succ_chans{}, type(t), dimensions(d)
{
params[0] = Halide::Internal::Parameter(type, dimensions != 0, dimensions, argument_name(pid, pn, 0));
}

Impl(const Channel& chan, const Halide::Type& t, int32_t d)
: pred_chan(chan), succ_chans{chan}, type(t), dimensions(d)
{
params[0] = Halide::Internal::Parameter(type, dimensions != 0, dimensions, argument_name(std::get<0>(chan), std::get<1>(chan), 0));
}

Impl();
Impl(const std::string& pid, const std::string& pn, const Halide::Type& t, int32_t d);
};

public:
Expand Down Expand Up @@ -125,6 +114,7 @@ class Port {
}

// Getter
const std::string& id() const { return impl_->id; }
const Channel& pred_chan() const { return impl_->pred_chan; }
const std::string& pred_id() const { return std::get<0>(impl_->pred_chan); }
const std::string& pred_name() const { return std::get<1>(impl_->pred_chan); }
Expand All @@ -133,7 +123,6 @@ class Port {
int32_t dimensions() const { return impl_->dimensions; }
int32_t size() const { return static_cast<int32_t>(impl_->params.size()); }
int32_t index() const { return index_; }
uintptr_t impl_ptr() const { return reinterpret_cast<uintptr_t>(impl_.get()); }

// Setter
void set_index(int index) { index_ = index; }
Expand Down Expand Up @@ -198,17 +187,7 @@ class Port {
}
}

static std::tuple<std::shared_ptr<Impl>, bool> find_impl(uintptr_t ptr) {
static std::unordered_map<uintptr_t, std::shared_ptr<Impl>> impls;
static std::mutex mutex;
std::scoped_lock lock(mutex);
bool found = true;
if (!impls.count(ptr)) {
impls[ptr] = std::make_shared<Impl>();
found = false;
}
return std::make_tuple(impls[ptr], found);
}
static std::tuple<std::shared_ptr<Impl>, bool> find_impl(const std::string& id);

private:
/**
Expand All @@ -217,12 +196,7 @@ class Port {
* pid and pn is stored in both pred and succ,
* then it will determined through pipeline build process.
*/
#if 1
Port(const std::string& pid, const std::string& pn) : impl_(new Impl(pid, pn, Halide::Type(), 0)), index_(-1) {}
#else
Port(const std::string& pid, const std::string& pn) : impl_(new Impl(Channel{pid, pn}, Halide::Type(), 0)), index_(-1) {}
#endif


std::vector<Halide::Argument> as_argument() const {
std::vector<Halide::Argument> args;
Expand Down
24 changes: 24 additions & 0 deletions src/port.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
#include "ion/port.h"

#include "uuid/sole.hpp"
#include "log.h"

namespace ion {

Port::Impl::Impl()
: id(sole::uuid4().str()), pred_chan{"", ""}, succ_chans{}, type(), dimensions(-1)
{
}

Port::Impl::Impl(const std::string& pid, const std::string& pn, const Halide::Type& t, int32_t d)
: id(sole::uuid4().str()), pred_chan{pid, pn}, succ_chans{}, type(t), dimensions(d)
{
params[0] = Halide::Internal::Parameter(type, dimensions != 0, dimensions, argument_name(pid, pn, 0));
}

void Port::determine_succ(const std::string& nid, const std::string& old_pn, const std::string& new_pn) {
auto it = std::find(impl_->succ_chans.begin(), impl_->succ_chans.end(), Channel{nid, old_pn});
if (it == impl_->succ_chans.end()) {
Expand All @@ -16,5 +28,17 @@ void Port::determine_succ(const std::string& nid, const std::string& old_pn, con
impl_->succ_chans.insert(Channel{nid, new_pn});
}

std::tuple<std::shared_ptr<Port::Impl>, bool> Port::find_impl(const std::string& id) {
static std::unordered_map<std::string, std::shared_ptr<Impl>> impls;
static std::mutex mutex;
std::scoped_lock lock(mutex);
bool found = true;
if (!impls.count(id)) {
impls[id] = std::make_shared<Impl>();
found = false;
}
log::debug("Port {} is {}found", id, found ? "" : "not ");
return std::make_tuple(impls[id], found);
}

} // namespace ion
4 changes: 2 additions & 2 deletions src/serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ static void from_json(const json& j, ion::Param& v) {
template<>
struct adl_serializer<ion::Port> {
static void to_json(json& j, const ion::Port& v) {
j["id"] = v.id();
j["pred_chan"] = v.pred_chan();
j["succ_chans"] = v.succ_chans();
j["type"] = static_cast<halide_type_t>(v.type());
j["dimensions"] = v.dimensions();
j["size"] = v.size();
j["impl_ptr"] = v.impl_ptr();
j["index"] = v.index();
}

static void from_json(const json& j, ion::Port& v) {
auto [impl, found] = ion::Port::find_impl(j["impl_ptr"].get<uintptr_t>());
auto [impl, found] = ion::Port::find_impl(j["id"].get<std::string>());
if (!found) {
impl->pred_chan = j["pred_chan"].get<ion::Port::Channel>();
impl->succ_chans = j["succ_chans"].get<std::set<ion::Port::Channel>>();
Expand Down
36 changes: 18 additions & 18 deletions test/inverted_dep.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ int main()
{
"nodes": [
{
"id": "827bd8eb-b51c-4f0a-b94d-58dd3c521464",
"id": "3de72ac3-d7e4-4de1-b73e-49856f8b5fc7",
"name": "test_consumer",
"params": [],
"ports": [
{
"dimensions": 0,
"impl_ptr": 93824992939168,
"id": "2792b187-a42f-4c02-9399-25fc3acddd8e",
"index": -1,
"pred_chan": [
"51917e77-d626-47ff-b1be-37957a7d0706",
"c4fcbdba-7da4-4149-80ab-4ad5da37b435",
"output"
],
"size": 1,
"succ_chans": [
[
"827bd8eb-b51c-4f0a-b94d-58dd3c521464",
"3de72ac3-d7e4-4de1-b73e-49856f8b5fc7",
"input"
]
],
Expand All @@ -44,7 +44,7 @@ int main()
},
{
"dimensions": 0,
"impl_ptr": 93824992925424,
"id": "b44a2f84-b7a2-40a4-9fbf-ed80078b6123",
"index": -1,
"pred_chan": [
"",
Expand All @@ -53,7 +53,7 @@ int main()
"size": 1,
"succ_chans": [
[
"827bd8eb-b51c-4f0a-b94d-58dd3c521464",
"3de72ac3-d7e4-4de1-b73e-49856f8b5fc7",
"min0"
]
],
Expand All @@ -65,7 +65,7 @@ int main()
},
{
"dimensions": 0,
"impl_ptr": 93824992925968,
"id": "2f9ab162-f72a-42c8-8b92-2cbcf5ce71f7",
"index": -1,
"pred_chan": [
"",
Expand All @@ -74,7 +74,7 @@ int main()
"size": 1,
"succ_chans": [
[
"827bd8eb-b51c-4f0a-b94d-58dd3c521464",
"3de72ac3-d7e4-4de1-b73e-49856f8b5fc7",
"extent0"
]
],
Expand All @@ -86,7 +86,7 @@ int main()
},
{
"dimensions": 0,
"impl_ptr": 93824992926512,
"id": "ba2f373c-2dd7-436f-b816-0ca59ca83037",
"index": -1,
"pred_chan": [
"",
Expand All @@ -95,7 +95,7 @@ int main()
"size": 1,
"succ_chans": [
[
"827bd8eb-b51c-4f0a-b94d-58dd3c521464",
"3de72ac3-d7e4-4de1-b73e-49856f8b5fc7",
"min1"
]
],
Expand All @@ -107,7 +107,7 @@ int main()
},
{
"dimensions": 0,
"impl_ptr": 93824992927056,
"id": "537fd4b2-eef1-4c69-a04f-bd09adf3c93f",
"index": -1,
"pred_chan": [
"",
Expand All @@ -116,7 +116,7 @@ int main()
"size": 1,
"succ_chans": [
[
"827bd8eb-b51c-4f0a-b94d-58dd3c521464",
"3de72ac3-d7e4-4de1-b73e-49856f8b5fc7",
"extent1"
]
],
Expand All @@ -128,7 +128,7 @@ int main()
},
{
"dimensions": 0,
"impl_ptr": 93824992927664,
"id": "80f24262-a521-43b7-8063-3b410fb5c509",
"index": -1,
"pred_chan": [
"",
Expand All @@ -137,7 +137,7 @@ int main()
"size": 1,
"succ_chans": [
[
"827bd8eb-b51c-4f0a-b94d-58dd3c521464",
"3de72ac3-d7e4-4de1-b73e-49856f8b5fc7",
"v"
]
],
Expand All @@ -151,7 +151,7 @@ int main()
"target": "host-profile"
},
{
"id": "51917e77-d626-47ff-b1be-37957a7d0706",
"id": "c4fcbdba-7da4-4149-80ab-4ad5da37b435",
"name": "test_producer",
"params": [
{
Expand All @@ -162,16 +162,16 @@ int main()
"ports": [
{
"dimensions": 0,
"impl_ptr": 93824992939168,
"id": "2792b187-a42f-4c02-9399-25fc3acddd8e",
"index": -1,
"pred_chan": [
"51917e77-d626-47ff-b1be-37957a7d0706",
"c4fcbdba-7da4-4149-80ab-4ad5da37b435",
"output"
],
"size": 1,
"succ_chans": [
[
"827bd8eb-b51c-4f0a-b94d-58dd3c521464",
"3de72ac3-d7e4-4de1-b73e-49856f8b5fc7",
"input"
]
],
Expand Down

0 comments on commit b91091b

Please sign in to comment.