From 8099f8d6212984080a621f36d31e4d187403f79b Mon Sep 17 00:00:00 2001 From: Jun Zhang Date: Sat, 11 Jan 2025 11:12:25 +0800 Subject: [PATCH] v1.2.5: speed up fetch_persons() API --- pyproject.toml | 2 +- src/entity/person/person.cu | 79 ++++++++++++++++- src/entity/person/person.cuh | 25 ++++++ src/python_api.cu | 165 ++++++++++++++--------------------- 4 files changed, 165 insertions(+), 106 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 4c11f96..fa7c844 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "python-moss" -version = "1.2.4" +version = "1.2.5" description = "MObility Simulation System" authors = [ { name = "Jun Zhang", email = "zhangjun990222@gmail.com" }, diff --git a/src/entity/person/person.cu b/src/entity/person/person.cu index a07e6af..6955d6f 100644 --- a/src/entity/person/person.cu +++ b/src/entity/person/person.cu @@ -172,6 +172,7 @@ __global__ void Prepare(Person* persons, PersonOutput* outputs, uint size, } break; } p.snapshot = p.runtime; + o.t = t; o.id = p.id; o.x = p.runtime.x; @@ -198,13 +199,22 @@ __global__ void Prepare(Person* persons, PersonOutput* outputs, uint size, p.node.sides[0][1] = nullptr; } -__global__ void Update(Person* persons, uint size, float t, float dt, +__global__ void Update(Person* persons, int8_t* s_enable, int* s_status, + int* s_lane_id, int* s_lane_parent_id, float* s_s, + int* s_aoi_id, float* s_v, int* s_shadow_lane_id, + float* s_shadow_s, float* s_lc_yaw, + float* s_lc_completed_ratio, int8_t* s_is_forward, + float* s_x, float* s_y, float* s_dir, + int* s_schedule_index, int* s_trip_index, + float* s_departure_time, float* s_traveling_time, + float* s_total_distance, uint size, float t, float dt, float X_MIN, float X_MAX, float Y_MIN, float Y_MAX) { uint index = THREAD_ID; if (index >= size) { return; } auto& p = persons[index]; + s_enable[index] = int8_t(p.enable); if (!p.enable) { return; } @@ -303,10 +313,42 @@ __global__ void Update(Person* persons, uint size, float t, float dt, } } break; default: { - return; + break; } } p.runtime.UpdatePositionDir(); + + // copy runtime to column storage + + s_status[index] = int(p.runtime.status); + if (p.runtime.lane) { + s_lane_id[index] = int(p.runtime.lane->id); + if (p.runtime.lane->parent_road) { + s_lane_parent_id[index] = int(p.runtime.lane->parent_road->id); + } else { + s_lane_parent_id[index] = int(p.runtime.lane->parent_junction->id); + } + } else { + s_lane_id[index] = -1; + s_lane_parent_id[index] = -1; + } + s_s[index] = p.runtime.s; + s_aoi_id[index] = p.runtime.aoi ? int(p.runtime.aoi->id) : -1; + s_v[index] = p.runtime.v; + s_shadow_lane_id[index] = + p.runtime.shadow_lane ? int(p.runtime.shadow_lane->id) : -1; + s_shadow_s[index] = p.runtime.shadow_s; + s_lc_yaw[index] = p.runtime.lc_yaw; + s_lc_completed_ratio[index] = p.runtime.lc_completed_ratio; + s_is_forward[index] = int8_t(p.runtime.is_forward); + s_x[index] = p.runtime.x; + s_y[index] = p.runtime.y; + s_dir[index] = p.runtime.dir; + s_schedule_index[index] = p.schedule_index; + s_trip_index[index] = p.trip_index; + s_departure_time[index] = p.departure_time; + s_traveling_time[index] = p.traveling_time; + s_total_distance[index] = p.total_distance; } void Data::Init(Moss* S, const PbPersons& pb, uint person_limit) { @@ -345,6 +387,7 @@ void Data::Init(Moss* S, const PbPersons& pb, uint person_limit) { stream = NewStream(); person_limit = min(person_limit, uint(pb_valid_persons.size())); persons.New(S->mem, person_limit); + ids.reserve(person_limit); SetGridBlockSize(Prepare, persons.size, S->sm_count, g_prepare, b_prepare); SetGridBlockSize(Update, persons.size, S->sm_count, g_update, b_update); uint index = 0; @@ -358,6 +401,7 @@ void Data::Init(Moss* S, const PbPersons& pb, uint person_limit) { p.id = pb.id(); p.enable = true; person_map[p.id] = &p; + ids.push_back(p.id); p.node.index = p.shadow_node.index = index - 1; p.node.add_node.data = &p.node; p.node.add_node.next = nullptr; @@ -606,6 +650,27 @@ void Data::Init(Moss* S, const PbPersons& pb, uint person_limit) { CHECK; Info("Earliest person departure: ", earliest_departure); + s_enable.New(S->mem, persons.size); + s_status.New(S->mem, persons.size); + s_lane_id.New(S->mem, persons.size); + s_lane_parent_id.New(S->mem, persons.size); + s_s.New(S->mem, persons.size); + s_aoi_id.New(S->mem, persons.size); + s_v.New(S->mem, persons.size); + s_shadow_lane_id.New(S->mem, persons.size); + s_shadow_s.New(S->mem, persons.size); + s_lc_yaw.New(S->mem, persons.size); + s_lc_completed_ratio.New(S->mem, persons.size); + s_is_forward.New(S->mem, persons.size); + s_x.New(S->mem, persons.size); + s_y.New(S->mem, persons.size); + s_dir.New(S->mem, persons.size); + s_schedule_index.New(S->mem, persons.size); + s_trip_index.New(S->mem, persons.size); + s_departure_time.New(S->mem, persons.size); + s_traveling_time.New(S->mem, persons.size); + s_total_distance.New(S->mem, persons.size); + outputs.New(S->mem, persons.size); } @@ -627,8 +692,14 @@ void Data::UpdateAsync() { return; } Update<<>>( - persons.data, persons.size, S->time, S->config.step_interval, - S->config.x_min, S->config.x_max, S->config.y_min, S->config.y_max); + persons.data, s_enable.data, s_status.data, s_lane_id.data, + s_lane_parent_id.data, s_s.data, s_aoi_id.data, s_v.data, + s_shadow_lane_id.data, s_shadow_s.data, s_lc_yaw.data, + s_lc_completed_ratio.data, s_is_forward.data, s_x.data, s_y.data, + s_dir.data, s_schedule_index.data, s_trip_index.data, + s_departure_time.data, s_traveling_time.data, s_total_distance.data, + persons.size, S->time, S->config.step_interval, S->config.x_min, + S->config.x_max, S->config.y_min, S->config.y_max); } } // namespace person diff --git a/src/entity/person/person.cuh b/src/entity/person/person.cuh index dfb9a98..bea917e 100644 --- a/src/entity/person/person.cuh +++ b/src/entity/person/person.cuh @@ -321,7 +321,32 @@ struct MData { #endif }; struct Data { + std::vector ids; + MArr persons; + + // use columns to store the snapshot data + MArr s_enable; + MArr s_status; + MArr s_lane_id; + MArr s_lane_parent_id; + MArr s_s; + MArr s_aoi_id; + MArr s_v; + MArr s_shadow_lane_id; + MArr s_shadow_s; + MArr s_lc_yaw; + MArr s_lc_completed_ratio; + MArr s_is_forward; + MArr s_x; + MArr s_y; + MArr s_dir; + MArr s_schedule_index; + MArr s_trip_index; + MArr s_departure_time; + MArr s_traveling_time; + MArr s_total_distance; + MArr outputs; std::map person_map; cudaStream_t stream; diff --git a/src/python_api.cu b/src/python_api.cu index 2fa3f47..8137e01 100644 --- a/src/python_api.cu +++ b/src/python_api.cu @@ -174,108 +174,71 @@ class Engine { auto* departure_times = new vec(); auto* traveling_times = new vec(); auto* total_distances = new vec(); - // reserve space - ids->reserve(S.person.persons.size); - enable->reserve(S.person.persons.size); - statuses->reserve(S.person.persons.size); - lane_ids->reserve(S.person.persons.size); - lane_parent_ids->reserve(S.person.persons.size); - ss->reserve(S.person.persons.size); - aoi_ids->reserve(S.person.persons.size); - vs->reserve(S.person.persons.size); - shadow_lane_ids->reserve(S.person.persons.size); - shadow_ss->reserve(S.person.persons.size); - lc_yaws->reserve(S.person.persons.size); - lc_completed_ratios->reserve(S.person.persons.size); - is_forwards->reserve(S.person.persons.size); - xs->reserve(S.person.persons.size); - ys->reserve(S.person.persons.size); - dirs->reserve(S.person.persons.size); - schedule_indexs->reserve(S.person.persons.size); - trip_indexs->reserve(S.person.persons.size); - departure_times->reserve(S.person.persons.size); - traveling_times->reserve(S.person.persons.size); - total_distances->reserve(S.person.persons.size); Info("Call fetch_persons: reserve space"); - // copy - for (auto& p : S.person.persons) { - if (fetch_id) { - ids->push_back(int(p.id)); - } - if (fetch_enable) { - enable->push_back(int8_t(p.enable)); - } - if (fetch_status) { - statuses->push_back(int(p.runtime.status)); - } - if (p.runtime.lane) { - if (fetch_lane_id) { - lane_ids->push_back(p.runtime.lane->id); - } - if (fetch_lane_parent_id) { - if (p.runtime.lane->parent_is_road) { - lane_parent_ids->push_back(int(p.runtime.lane->parent_road->id)); - } else { - lane_parent_ids->push_back(int(p.runtime.lane->parent_junction->id)); - } - } - } else { - if (fetch_lane_id) { - lane_ids->push_back(-1); - } - if (fetch_lane_parent_id) { - lane_parent_ids->push_back(-1); - } - } - if (fetch_s) { - ss->push_back(p.runtime.s); - } - if (fetch_aoi_id) { - aoi_ids->push_back(p.runtime.aoi ? int(p.runtime.aoi->id) : -1); - } - if (fetch_v) { - vs->push_back(p.runtime.v); - } - if (fetch_shadow_lane_id) { - shadow_lane_ids->push_back( - p.runtime.shadow_lane ? int(p.runtime.shadow_lane->id) : -1); - } - if (fetch_shadow_s) { - shadow_ss->push_back(p.runtime.shadow_s); - } - if (fetch_lc_yaw) { - lc_yaws->push_back(p.runtime.lc_yaw); - } - if (fetch_lc_completed_ratio) { - lc_completed_ratios->push_back(p.runtime.lc_completed_ratio); - } - if (fetch_is_forward) { - is_forwards->push_back(p.runtime.is_forward); - } - if (fetch_x) { - xs->push_back(p.runtime.x); - } - if (fetch_y) { - ys->push_back(p.runtime.y); - } - if (fetch_dir) { - dirs->push_back(p.runtime.dir); - } - if (fetch_schedule_index) { - schedule_indexs->push_back(p.schedule_index); - } - if (fetch_trip_index) { - trip_indexs->push_back(p.trip_index); - } - if (fetch_departure_time) { - departure_times->push_back(p.departure_time); - } - if (fetch_traveling_time) { - traveling_times->push_back(p.traveling_time); - } - if (fetch_total_distance) { - total_distances->push_back(p.total_distance); - } + + // copy from column storage in cuda + if (fetch_id) { + ids->assign(S.person.ids.begin(), S.person.ids.end()); + } + if (fetch_enable) { + S.person.s_enable.Save(*enable); + } + if (fetch_status) { + S.person.s_status.Save(*statuses); + } + if (fetch_lane_id) { + S.person.s_lane_id.Save(*lane_ids); + } + if (fetch_lane_parent_id) { + S.person.s_lane_parent_id.Save(*lane_parent_ids); + } + if (fetch_s) { + S.person.s_s.Save(*ss); + } + if (fetch_aoi_id) { + S.person.s_aoi_id.Save(*aoi_ids); + } + if (fetch_v) { + S.person.s_v.Save(*vs); + } + if (fetch_shadow_lane_id) { + S.person.s_shadow_lane_id.Save(*shadow_lane_ids); + } + if (fetch_shadow_s) { + S.person.s_shadow_s.Save(*shadow_ss); + } + if (fetch_lc_yaw) { + S.person.s_lc_yaw.Save(*lc_yaws); + } + if (fetch_lc_completed_ratio) { + S.person.s_lc_completed_ratio.Save(*lc_completed_ratios); + } + if (fetch_is_forward) { + S.person.s_is_forward.Save(*is_forwards); + } + if (fetch_x) { + S.person.s_x.Save(*xs); + } + if (fetch_y) { + S.person.s_y.Save(*ys); + } + if (fetch_dir) { + S.person.s_dir.Save(*dirs); + } + if (fetch_schedule_index) { + S.person.s_schedule_index.Save(*schedule_indexs); + } + if (fetch_trip_index) { + S.person.s_trip_index.Save(*trip_indexs); + } + if (fetch_departure_time) { + S.person.s_departure_time.Save(*departure_times); + } + if (fetch_traveling_time) { + S.person.s_traveling_time.Save(*traveling_times); + } + if (fetch_total_distance) { + S.person.s_total_distance.Save(*total_distances); } return std::make_tuple(