Skip to content

Commit 192748f

Browse files
committed
Translate link loads from compressed IDs to graph IDs when link
Add decorators as well
1 parent 19f0d7e commit 192748f

File tree

2 files changed

+54
-11
lines changed

2 files changed

+54
-11
lines changed

aequilibrae/paths/route_choice.pxd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ cdef class RouteChoiceSet:
135135
double [:] lat_view
136136
double [:] lon_view
137137
long long [:] ids_graph_view
138+
long long [:] graph_compressed_id_view
138139
long long [:] compressed_link_ids
139140
long long num_nodes
140141
long long num_links
@@ -214,14 +215,13 @@ cdef class RouteChoiceSet:
214215
double theta
215216
) noexcept nogil
216217

217-
# cdef void link_loading(self, double[:, :] matrix_view) nogil
218-
219218
@staticmethod
220219
cdef vector[vector[double] *] *compute_path_files(
221220
vector[pair[long long, long long]] &ods,
222221
vector[RouteSet_t *] &results,
223222
vector[vector[long long] *] &link_union_set,
224-
vector[vector[double] *] &prob_set
223+
vector[vector[double] *] &prob_set,
224+
unsigned int cores
225225
) noexcept nogil
226226

227227
cdef vector[double] *apply_link_loading(RouteChoiceSet self, double[:, :] matrix_view) noexcept nogil

aequilibrae/paths/route_choice.pyx

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ from libc.stdio cimport fprintf, printf, stderr
8888

8989
# It would really be nice if these were modules. The 'include' syntax is long deprecated and adds a lot to compilation times
9090
include 'basic_path_finding.pyx'
91+
include 'parallel_numpy.pyx'
9192

9293
@cython.embedsignature(True)
9394
cdef class RouteChoiceSet:
@@ -136,6 +137,7 @@ cdef class RouteChoiceSet:
136137
self.a_star = False
137138

138139
self.ids_graph_view = graph.compact_graph.id.values
140+
self.graph_compressed_id_view = graph.graph.__compressed_id__.values
139141
self.num_nodes = graph.compact_num_nodes
140142
self.num_links = graph.compact_num_links
141143
self.zones = graph.num_zones
@@ -820,7 +822,8 @@ cdef class RouteChoiceSet:
820822

821823
return prob_vec
822824

823-
def link_loading(RouteChoiceSet self, matrix, generate_path_files: bool = False):
825+
@cython.embedsignature(True)
826+
def link_loading(RouteChoiceSet self, matrix, generate_path_files: bool = False, cores: int = 0):
824827
if self.ods == nullptr \
825828
or self.link_union_set == nullptr \
826829
or self.prob_set == nullptr:
@@ -829,6 +832,8 @@ cdef class RouteChoiceSet:
829832
if not isinstance(matrix, AequilibraeMatrix):
830833
raise ValueError("`matrix` is not an AequilibraE matrix")
831834

835+
cores = cores if cores > 0 else openmp.omp_get_num_threads()
836+
832837
cdef:
833838
vector[vector[double] *] *path_files = <vector[vector[double] *] *>nullptr
834839
vector[double] *ll
@@ -838,7 +843,8 @@ cdef class RouteChoiceSet:
838843
deref(self.ods),
839844
deref(self.results),
840845
deref(self.link_union_set),
841-
deref(self.prob_set)
846+
deref(self.prob_set),
847+
cores,
842848
)
843849
tmp = []
844850
for vec in deref(path_files):
@@ -853,7 +859,17 @@ cdef class RouteChoiceSet:
853859
)
854860
else:
855861
ll = self.apply_link_loading(m)
856-
return deref(ll)
862+
863+
actual = np.zeros((self.graph_compressed_id_view.shape[0], 1), dtype=np.float64)
864+
assign_link_loads_cython(
865+
actual,
866+
# This incantation creates a 2d (ll.size() x 1) memory view object around the underlying vector data without transferring owner ship.
867+
<double[:ll.size(), :1]>&deref(ll)[0],
868+
self.graph_compressed_id_view,
869+
cores
870+
)
871+
del ll
872+
return actual
857873

858874
if len(matrix.view_names) == 1:
859875
link_loads = apply_link_loading_func(matrix.matrix_view)
@@ -865,14 +881,23 @@ cdef class RouteChoiceSet:
865881

866882
return link_loads
867883

868-
884+
@cython.boundscheck(False)
885+
@cython.wraparound(False)
886+
@cython.embedsignature(True)
887+
@cython.initializedcheck(False)
869888
@staticmethod
870889
cdef vector[vector[double] *] *compute_path_files(
871890
vector[pair[long long, long long]] &ods,
872891
vector[RouteSet_t *] &results,
873892
vector[vector[long long] *] &link_union_set,
874-
vector[vector[double] *] &prob_set
893+
vector[vector[double] *] &prob_set,
894+
unsigned int cores
875895
) noexcept nogil:
896+
"""
897+
Computes the path files for the provided vector of RouteSets.
898+
899+
Returns vector of vectors of link loads corresponding to each link in it's link_union_set.
900+
"""
876901
cdef:
877902
vector[vector[double] *] *link_loads = new vector[vector[double] *](ods.size()) # FIXME FREE ME
878903
vector[long long] *link_union
@@ -886,7 +911,7 @@ cdef class RouteChoiceSet:
886911
double prob
887912
long long i
888913

889-
with parallel(num_threads=6):
914+
with parallel(num_threads=cores):
890915
# The link union needs to be allocated per thread as scratch space, as its of unknown length we can't allocated a matrix of them.
891916
# Additionally getting them to be reused between batches is complicated, instead we just get a new one each batch
892917

@@ -932,11 +957,22 @@ cdef class RouteChoiceSet:
932957

933958
return link_loads
934959

960+
@cython.boundscheck(False)
961+
@cython.wraparound(False)
962+
@cython.embedsignature(True)
963+
@cython.initializedcheck(False)
935964
cdef vector[double] *apply_link_loading_from_path_files(
936965
RouteChoiceSet self,
937966
double[:, :] matrix_view,
938967
vector[vector[double] *] &path_files
939968
) noexcept nogil:
969+
"""
970+
Apply link loading from path files.
971+
972+
If path files have already been computed then this is a more efficient manner for the link loading.
973+
974+
Returns a vector of link loads indexed by compressed link ID.
975+
"""
940976
cdef:
941977
vector[double] *loads
942978
vector[long long] *link_union
@@ -959,7 +995,16 @@ cdef class RouteChoiceSet:
959995

960996
return link_loads
961997

998+
@cython.boundscheck(False)
999+
@cython.wraparound(False)
1000+
@cython.embedsignature(True)
1001+
@cython.initializedcheck(False)
9621002
cdef vector[double] *apply_link_loading(self, double[:, :] matrix_view) noexcept nogil:
1003+
"""
1004+
Apply link loading.
1005+
1006+
Returns a vector of link loads indexed by compressed link ID.
1007+
"""
9631008
cdef:
9641009
RouteSet_t *route_set
9651010
vector[double] *route_set_prob
@@ -987,8 +1032,6 @@ cdef class RouteChoiceSet:
9871032

9881033
return link_loads
9891034

990-
991-
9921035
@cython.wraparound(False)
9931036
@cython.embedsignature(True)
9941037
@cython.boundscheck(False)

0 commit comments

Comments
 (0)