/
StopsHandler.py
467 lines (381 loc) · 24.4 KB
/
StopsHandler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import globalConstants
import logging
import csv
from Stop import Stop
import time
import numpy as np
import random
import pyopencl as cl
import pyopencl.array as cl_array
import ctypes
class StopsHandler:
def __init__(self):
print('Load program from cl source file')
f = open('c_code/kernels_struct.c', 'r', encoding='utf-8')
kernels = ''.join(f.readlines())
f.close()
ocl_platforms = (platform.name
for platform in cl.get_platforms())
# print("\n".join(ocl_platforms))
cl_platform = cl.get_platforms()[-1]
cl_devices = cl_platform.get_devices()
if globalConstants.USE_PYOPENCL:
for i, dev in enumerate(cl_devices):
print('OpenCL device[%d]: %s, %s, max_cu: %d ' % (i, dev.name, dev.vendor, dev.max_compute_units))
globalConstants.results['OpenCL_device_name'] = \
'"OpenCL device[%d]: %s, %s, max_cu: %d"' % (i, dev.name, dev.vendor, dev.max_compute_units)
if globalConstants.LIMIT_MAX_CPUS > 0:
# emulate multiple devices
cl_devices = cl_devices[0].create_sub_devices(
[cl.device_partition_property.BY_COUNTS, globalConstants.LIMIT_MAX_CPUS])
for i, dev in enumerate(cl_devices):
print('Emulated OpenCL device[%d]: %s, %s, max_cu: %d ' % (i, dev.name, dev.vendor, dev.max_compute_units))
globalConstants.results['OpenCL_device_name'] = \
'"Emulated OpenCL device[%d]: %s, %s, max_cu: %d"' % (i, dev.name, dev.vendor, dev.max_compute_units)
print()
globalConstants.results['LIMIT_MAX_CPUS'] = globalConstants.LIMIT_MAX_CPUS
self.ctx = cl.Context(devices=cl_devices)
self.queue = cl.CommandQueue(self.ctx)
self.mf = cl.mem_flags
self.prg = cl.Program(self.ctx, kernels).build()
self.knl = self.prg.masivo_runner
self.knl_gen_pass = self.prg.generate_pass
self.np_total_stops = 0
self.buses_struc_list = 0
self.buses_handler = 0
self.grand_total_passengers = 0
self.stops_object_list = []
self.open_stops_file(globalConstants.ODM_FILE)
self.stops_queue_list = np.zeros(len(self.stops_object_list), globalConstants.spsl_type)
self.stops_arrival_list = np.zeros(len(self.stops_object_list), globalConstants.spsl_type)
self.stops_alight_list = np.zeros(len(self.stops_object_list), globalConstants.spsl_type)
self.buses_struc_list_g = 0
for i in range(len(self.stops_object_list)):
self.stops_queue_list[i]['stop_num'] = self.stops_object_list[i].number
for i in range(len(self.stops_object_list)):
self.stops_object_list[i].set_stop_lists(self.stops_queue_list[i], self.stops_arrival_list[i],
self.stops_alight_list[i])
self.generate_pass_input()
self.stops_queue_list_g = cl_array.to_device(self.queue, self.stops_queue_list)
self.stops_arrival_list_g = cl_array.to_device(self.queue, self.stops_arrival_list)
self.stops_alight_list_g = cl_array.to_device(self.queue, self.stops_alight_list)
for i in range(len(self.stops_object_list)):
self.stops_object_list[i].set_cl_lists(self.stops_queue_list_g[i], self.stops_arrival_list_g[i],
self.stops_alight_list_g[i])
def get_cl_queue(self):
return self.queue
def get_stops_list(self):
return self.stops_object_list
def get_stops_arrival_list(self):
if globalConstants.cl_enabled:
return np.array(self.stops_arrival_list_g.get(), dtype=globalConstants.spsl_type)
else:
return self.stops_arrival_list
def get_stops_queue_list(self):
if globalConstants.cl_enabled:
return np.array(self.stops_queue_list_g.get(), dtype=globalConstants.spsl_type)
else:
return self.stops_queue_list
def get_stops_alight_list(self):
if globalConstants.cl_enabled:
return np.array(self.stops_alight_list_g.get(), dtype=globalConstants.spsl_type)
else:
return self.stops_alight_list
def set_buses_struc_list(self, buses_struc_list):
self.buses_struc_list = buses_struc_list
def set_buses_struc_list_g(self, buses_struc_list_g):
self.buses_struc_list_g = buses_struc_list_g
def set_buses_handler(self, buses_handler):
self.buses_handler = buses_handler
def open_stops_file(self, file_name):
logging.info("Opening stops file: %s" % file_name)
# Get the stops number, name, poss and max capacity
with open(file_name, newline='') as csvfile:
reader = csv.DictReader(csvfile)
# Get stop first columns
for row in reader:
stop = Stop(int(row['stop_number']), row['stop_name'],
int(row['x_pos']), int(row['y_pos']), int(row['max_capacity']))
self.stops_object_list.append(stop)
globalConstants.stops_name_to_num[row['stop_name']] = int(row['stop_number'])
# Get stop destination vector
with open(file_name, newline='') as csvfile:
reader = csv.DictReader(csvfile)
# Rows have the destinations of the users' stops
for row in reader:
i = int(row['stop_number'])
self.stops_object_list[i].destination_vector = np.zeros(len(self.stops_object_list),
globalConstants.dest_vec_type)
for stop in self.stops_object_list:
self.stops_object_list[i].destination_vector[stop.number]['dest_total'] = int(row[stop.name])
self.stops_object_list[stop.number].expected_alight_pass += int(row[stop.name])
# Calculate total pass in and input queue
for stop in self.stops_object_list:
stop.calculate_total_pass_in()
globalConstants.results['Total_stops'] = len(self.stops_object_list)
def generate_pass_input(self):
start_time = time.time()
if 0:
# For each destination
print("Setting empty pass lists")
for i in range(len(self.stops_queue_list)):
for j in range(len(self.stops_queue_list[i]['spl'])):
self.stops_queue_list[i]['spl'][j]['status'] = globalConstants.PASS_STATUS_EMPTY_255
self.stops_arrival_list[i]['spl'][j]['status'] = globalConstants.PASS_STATUS_EMPTY_255
# Calculate total pass in and input queue
for i in range(len(self.stops_object_list)):
print("Generating pass input queue for stop name %s" % self.stops_object_list[i].name)
# For each destination
for key, val in enumerate(self.stops_object_list[i].destination_vector):
for j in range(val["dest_total"]):
k = self.stops_arrival_list[i]['last_empty']
self.stops_arrival_list[i]['spl'][k]['pass_id'] = globalConstants.pass_num
self.stops_arrival_list[i]['spl'][k]['orig_stop'] = int(self.stops_object_list[i].number)
self.stops_arrival_list[i]['spl'][k]['dest_stop'] = int(key)
self.stops_arrival_list[i]['spl'][k]['arrival_time'] = j*globalConstants.PASS_TOTAL_ARRIVAL_TIME/val["dest_total"]
self.stops_arrival_list[i]['spl'][k]['status'] = globalConstants.PASS_STATUS_TO_ARRIVE
self.stops_arrival_list[i]['total'] += 1
self.stops_arrival_list[i]['last_empty'] += 1
globalConstants.pass_num += 1
# Sort items by arrival time ascending
self.stops_arrival_list[i]['spl'].sort(order=['status', 'arrival_time'])
if 0:
for p in self.stops_arrival_list[i]['spl']:
if p['status'] == globalConstants.PASS_STATUS_TO_ARRIVE:
print(p)
if 1:
# For each destination
print("\nGenerating pass input queue all stops: %d" % len(self.stops_queue_list))
for i in range(len(self.stops_queue_list)):
masivo_c = ctypes.CDLL('./c_code/masivo_c.so')
masivo_c.generate_pass.argtypes = (np.ctypeslib.ndpointer(dtype=globalConstants.spsl_type),
np.ctypeslib.ndpointer(dtype=globalConstants.spsl_type),
np.ctypeslib.ndpointer(dtype=globalConstants.dest_vec_type),
ctypes.c_uint32, ctypes.c_uint32, ctypes.c_uint32, ctypes.c_uint32)
masivo_c.generate_pass(self.stops_queue_list,
self.stops_arrival_list,
self.stops_object_list[i].destination_vector,
i,
len(self.stops_object_list),
len(self.stops_object_list[i].destination_vector),
globalConstants.PASS_TOTAL_ARRIVAL_TIME)
self.grand_total_passengers += int(self.stops_arrival_list[i]['total'])
if 0:
for p in self.stops_arrival_list[i]['spl']:
if p['status'] == globalConstants.PASS_STATUS_TO_ARRIVE:
print(p)
end_time = time.time()
print("generate_pass_input_queue stops time %d ms, grand total passengers: %d"
% ((end_time - start_time) * 1000, self.grand_total_passengers))
globalConstants.results['Total_passengers'] = self.grand_total_passengers
def prepare_cl(self):
self.np_total_stops = np.uint32(len(self.stops_queue_list_g))
self.total_work_items = self.np_total_stops + 10
np_total_buses = np.uint32(len(self.buses_struc_list_g))
self.knl.set_arg(0, self.stops_queue_list_g.data)
self.knl.set_arg(1, self.stops_arrival_list_g.data)
self.knl.set_arg(2, self.stops_alight_list_g.data)
self.knl.set_arg(3, self.buses_struc_list_g.data)
self.knl.set_arg(4, self.np_total_stops)
self.knl.set_arg(5, np_total_buses)
def runner_cl(self, sim_time):
np_sim_time = np.uint32(sim_time)
self.knl.set_arg(6, np_sim_time)
evt = cl.enqueue_nd_range_kernel(self.queue, self.knl, (self.total_work_items,), None)
evt.wait()
def runner_c(self, sim_time):
masivo_c = ctypes.CDLL('./c_code/masivo_c.so')
masivo_c.masivo_runner.argtypes = (np.ctypeslib.ndpointer(dtype=globalConstants.spsl_type),
np.ctypeslib.ndpointer(dtype=globalConstants.spsl_type),
np.ctypeslib.ndpointer(dtype=globalConstants.spsl_type),
np.ctypeslib.ndpointer(dtype=globalConstants.bpsl_type),
ctypes.c_uint32, ctypes.c_uint32, ctypes.c_uint32)
masivo_c.masivo_runner(self.stops_queue_list,
self.stops_arrival_list,
self.stops_alight_list,
self.buses_struc_list,
len(self.stops_queue_list_g),
len(self.buses_struc_list),
sim_time)
def runner(self, sim_time):
# Update stop pass list from arrival list
# For each stop
for i in range(len(self.stops_queue_list)):
# STOPS ARRIVAL:
if self.stops_arrival_list[i]['total'] > 0:
while True:
# Check if the list is finished
if self.stops_arrival_list[i]['w_index'] >= len(self.stops_arrival_list[i]['spl']):
break
# Check pass status
if self.stops_arrival_list[i]['spl'][self.stops_arrival_list[i]['w_index']]['status'] \
!= globalConstants.PASS_STATUS_TO_ARRIVE:
break
# Check arrival time
if sim_time < self.stops_arrival_list[i]['spl'][self.stops_arrival_list[i]['w_index']][
'arrival_time']:
break
self.stops_queue_list[i]['spl'][self.stops_queue_list[i]['last_empty']] = \
np.copy(self.stops_arrival_list[i]['spl'][self.stops_arrival_list[i]['w_index']])
self.stops_queue_list[i]['spl'][self.stops_queue_list[i]['last_empty']][
'status'] = globalConstants.PASS_STATUS_ARRIVED
self.stops_queue_list[i]['last_empty'] += 1
self.stops_queue_list[i]['total'] += 1
self.stops_arrival_list[i]['spl'][self.stops_arrival_list[i]['w_index']]['status'] = \
globalConstants.PASS_STATUS_EMPTY
self.stops_arrival_list[i]['last_empty'] -= 1
self.stops_arrival_list[i]['total'] -= 1
self.stops_arrival_list[i]['w_index'] += 1
# Handle the buses
# For each bus
for j in range(len(self.buses_struc_list)):
# If the bus is in the stop
if self.stops_queue_list[i]['stop_num'] == self.buses_struc_list[j]['curr_stop']:
if 1:
# ALIGHTING
# Only if there are passengers in the bus
if self.buses_struc_list[j]['total'] > 0:
# For each pass in the bus
for k in range(len(self.buses_struc_list[j]['bpl'])):
# print("pass to check(%d): %s " %(k, str(self.buses_struc_list[j]['bpl'][k])))
if self.buses_struc_list[j]['bpl'][k]['status'] == globalConstants.PASS_STATUS_IN_BUS:
if self.buses_struc_list[j]['bpl'][k]['dest_stop'] == \
self.stops_queue_list[i]['stop_num']:
logging.info("ALIGHTING pass %s from bus %d to stop %d" %
(str(self.buses_struc_list[j]['bpl'][k]), j,
self.stops_queue_list[i]['stop_num']))
self.buses_struc_list[j]['bpl'][k][
'status'] = globalConstants.PASS_STATUS_ALIGHTED
self.buses_struc_list[j]['bpl'][k]['alight_time'] = sim_time
self.buses_struc_list[j]['total'] -= 1
self.buses_struc_list[j]['last_empty'] -= 1
m = self.stops_alight_list[i]['last_empty']
self.stops_alight_list[i]['spl'][m] = np.copy(
self.buses_struc_list[j]['bpl'][k])
self.stops_alight_list[i]['total'] += 1
self.stops_alight_list[i]['last_empty'] += 1
# BOARDING
# If there are pass in the stop, do not look for more buses
if self.stops_queue_list[i]['total'] == 0:
break
# If the bus is full, continue with the next bus
if self.buses_struc_list[j]['total'] >= globalConstants.BUS_MAX_PASS:
continue
# For this bus, begin the free space search from the beginning
self.buses_struc_list[j]['last_empty'] = 0
# For each pass in the stop
for k in range(len(self.stops_queue_list[i]['spl'])):
# print("Check for board: %s" % (self.pass_list[i]['spl'][k]))
# If the bus is full, continue with the next bus
if self.buses_struc_list[j]['total'] >= globalConstants.BUS_MAX_PASS:
break
# If we are at the end of the pass list
if self.stops_queue_list[i]['spl'][k]['status'] == globalConstants.PASS_STATUS_EMPTY_255:
break
# If the pass has arrived to the stop
if self.stops_queue_list[i]['spl'][k]['status'] == globalConstants.PASS_STATUS_ARRIVED:
# Check if the bus route has the pass destination stop
bus_for_dest = False
# print("Bus %d in stop %d, last stop i %d" %
# (j, i, self.buses_struc_list[j]['last_stop_i']))
for l in (range(self.buses_struc_list[j]['last_stop_table_i'],
self.buses_struc_list[j]['total_stops'])):
if self.stops_queue_list[i]['spl'][k]['dest_stop'] == \
self.buses_struc_list[j]['stops_num'][l]:
bus_for_dest = True
break
# If the bus has the destination stop, pass boards
if bus_for_dest:
# Look for a free space in the bus
for n in range(self.buses_struc_list[j]['last_empty'],
len(self.buses_struc_list[j]['bpl'])):
# print("bus seat: %d, %s" % (n, str(self.buses_struc_list[j]['bpl'][n])))
if self.buses_struc_list[j]['bpl'][n]['status'] == \
globalConstants.PASS_STATUS_IN_BUS:
# print("busy")
continue
logging.info("BOARDING pass %s to the bus %d, in poss %d" %
(str(self.stops_queue_list[i]['spl'][k]), j, n))
self.stops_queue_list[i]['spl'][k]['status'] = globalConstants.PASS_STATUS_IN_BUS
self.stops_queue_list[i]['total'] -= 1
self.buses_struc_list[j]['bpl'][n] = np.copy(self.stops_queue_list[i]['spl'][k])
self.buses_struc_list[j]['total'] += 1
self.buses_struc_list[j]['last_empty'] = n
break
# If bus is full break to go to the next bus in the stop
if self.buses_struc_list[j]['total'] >= globalConstants.BUS_MAX_PASS:
break
# Update buses
if 1:
# For each bus
for i in range(len(self.buses_struc_list)):
# Do not process finished buses
if self.buses_struc_list[i]['curr_stop'] == globalConstants.BUS_FINISHED:
continue
# Check if start the bus
if self.buses_struc_list[i]['curr_stop'] == globalConstants.BUS_NOT_STARTED_STOP:
if sim_time >= self.buses_struc_list[i]['start_time']:
self.buses_struc_list[i]['in_the_stop'] = True
self.buses_struc_list[i]['curr_stop'] = self.buses_struc_list[i]['stops_num'][0]
self.buses_struc_list[i]["last_stop_i"] = self.buses_struc_list[i]['stops_num'][0]
self.buses_struc_list[i]["curr_pos"] = self.buses_struc_list[i]["start_pos"]
self.buses_struc_list[i]['last_stop_pos'] = self.buses_struc_list[i]["curr_pos"]
logging.info("Starting bus %d in stop %d start time %d",
self.buses_struc_list[i]["number"],
self.buses_struc_list[i]['curr_stop'],
self.buses_struc_list[i]['start_time'])
# self.bl_head += 1
continue
# self.buses_struc_list[i].update_pos(self.stops_list)
# Update bus possition
# If waiting in the stop
# print("Bus: %d, in_the_stop_flag: %d, curr_stop %d, pos: %d" %
# (self.buses_struc_list[i]['number'],
# self.buses_struc_list[i]["in_the_stop"],
# self.buses_struc_list[i]['curr_stop'],
# self.buses_struc_list[i]['curr_pos']))
# Check if we have to depart from the stop
if self.buses_struc_list[i]["in_the_stop"]:
self.buses_struc_list[i]["in_the_stop_counter"] -= 1
if self.buses_struc_list[i]["in_the_stop_counter"] == 0:
self.buses_struc_list[i]["last_stop_table_i"] += 1
self.buses_struc_list[i]["in_the_stop"] = False
self.buses_struc_list[i]["curr_stop"] = globalConstants.BUS_TRAVELING
# If I am not waiting in a stop, go ahead
if self.buses_struc_list[i]["curr_stop"] == globalConstants.BUS_TRAVELING:
self.buses_struc_list[i]["curr_pos"] += self.buses_struc_list[i]["travel_speed_m_s"]
# Check if the bus has to leave the current stop, if not, do not check for other stop
if abs(self.buses_struc_list[i]['last_stop_pos'] - self.buses_struc_list[i]["curr_pos"]) < \
globalConstants.STOP_BUS_WINDOW_DISTANCE:
continue
# Check if the bus is at the next stop
next_stop_i = self.buses_struc_list[i]["last_stop_i"] + self.buses_struc_list[i]['stop_inc']
# print("next_stop_i: %d/%d" % (next_stop_i, self.buses_struc_list[i]['total_stops']))
# Check if the next stop is the last one
if next_stop_i >= self.buses_struc_list[i]['total_stops'] or next_stop_i < 0:
# Finish the bus and put in the rest position
self.buses_struc_list[i]["curr_stop"] = globalConstants.BUS_FINISHED
self.buses_struc_list[i]["curr_pos"] = 0
continue
# Look if the bus is inside the stop window of the next stop
if abs(self.stops_queue_list[next_stop_i]['stop_pos'] - self.buses_struc_list[i]["curr_pos"]) \
< globalConstants.STOP_BUS_WINDOW_DISTANCE:
# Passing the stop, update last stop index
self.buses_struc_list[i]["last_stop_i"] = self.stops_queue_list[next_stop_i]['stop_num']
# Check if the stop is in the routes table to stop de bus
in_the_route = False
for j in range(self.buses_struc_list[i]["stops_num_i"], self.buses_struc_list[i]["total_stops"]):
if self.stops_queue_list[next_stop_i]['stop_num'] == self.buses_struc_list[i]["stops_num"][j]:
self.buses_struc_list[i]["stops_num_i"] = j
in_the_route = True
# If this stop is in the stops table
if in_the_route:
logging.info("i: %d, Bus %d, in the stop: %d, pos %d" % (i,
self.buses_struc_list[i]['number'],
self.stops_queue_list[next_stop_i][
'stop_num'],
self.buses_struc_list[i]['curr_pos']))
self.buses_struc_list[i]['curr_stop'] = self.stops_queue_list[next_stop_i]['stop_num']
self.buses_struc_list[i]['last_stop_pos'] = self.stops_queue_list[next_stop_i]['stop_pos']
self.buses_struc_list[i]['in_the_stop'] = True
self.buses_struc_list[i]['in_the_stop_counter'] = globalConstants.BUS_STOPPING_TIME