Skip to content

Commit

Permalink
v1.2.5: speed up fetch_persons() API
Browse files Browse the repository at this point in the history
  • Loading branch information
Sweetnow committed Jan 11, 2025
1 parent 6f271c7 commit 8099f8d
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 106 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]" },
Expand Down
79 changes: 75 additions & 4 deletions src/entity/person/person.cu
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

Expand All @@ -627,8 +692,14 @@ void Data::UpdateAsync() {
return;
}
Update<<<g_update, b_update, 0, stream>>>(
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
Expand Down
25 changes: 25 additions & 0 deletions src/entity/person/person.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,32 @@ struct MData {
#endif
};
struct Data {
std::vector<int> ids;

MArr<Person> persons;

// use columns to store the snapshot data
MArr<int8_t> s_enable;
MArr<int> s_status;
MArr<int> s_lane_id;
MArr<int> s_lane_parent_id;
MArr<float> s_s;
MArr<int> s_aoi_id;
MArr<float> s_v;
MArr<int> s_shadow_lane_id;
MArr<float> s_shadow_s;
MArr<float> s_lc_yaw;
MArr<float> s_lc_completed_ratio;
MArr<int8_t> s_is_forward;
MArr<float> s_x;
MArr<float> s_y;
MArr<float> s_dir;
MArr<int> s_schedule_index;
MArr<int> s_trip_index;
MArr<float> s_departure_time;
MArr<float> s_traveling_time;
MArr<float> s_total_distance;

MArr<PersonOutput> outputs;
std::map<uint, Person*> person_map;
cudaStream_t stream;
Expand Down
165 changes: 64 additions & 101 deletions src/python_api.cu
Original file line number Diff line number Diff line change
Expand Up @@ -174,108 +174,71 @@ class Engine {
auto* departure_times = new vec<float>();
auto* traveling_times = new vec<float>();
auto* total_distances = new vec<float>();
// 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(
Expand Down

0 comments on commit 8099f8d

Please sign in to comment.