42 lines
1.6 KiB
Python
42 lines
1.6 KiB
Python
from dragon import *
|
|
|
|
kBegPitch = 0.3
|
|
kEndPitch = 0.55
|
|
kTotalSteps = 10000
|
|
kStepAlpha = (kEndPitch - kBegPitch) / kTotalSteps
|
|
kParallelNum=24
|
|
tasks_list = [kBegPitch + kStepAlpha * i for i in range(kTotalSteps)]
|
|
task_list_per_process=[tasks_list[i::kParallelNum] for i in range(kParallelNum)]
|
|
print(f"len(task_list_per_thread)={len(task_list_per_process)}",file=sys.stderr)
|
|
def ProcessEntryPoint(arg):
|
|
pitch_list, process_id, shared_dict, lock = arg
|
|
logf=open(f"low_bound_cal_{process_id}.log","w")
|
|
print(f"calculating pitch_list={pitch_list} with process_id={process_id}",file=logf)
|
|
cnt = 0
|
|
tmp_res={}
|
|
for pitch in pitch_list:
|
|
dragen = Dragon(mp.mpf(pitch)/(2*mp.pi))
|
|
status = CheckCollision(status2blocks(dragen.CalcMoveList()))
|
|
tmp_res[pitch]=status
|
|
print(f"res={status}",file=logf)
|
|
with lock: # 添加锁保护对共享字典的操作
|
|
shared_dict.update(tmp_res)
|
|
|
|
if __name__ == "__main__":
|
|
manager = multiprocessing.Manager()
|
|
shared_dict = manager.dict() # 创建一个共享字典
|
|
lock = manager.Lock() # 创建一个共享的锁
|
|
task_args_list = [(task_list_per_process[i], i, shared_dict, lock) for i in range(kParallelNum)]
|
|
with multiprocessing.Pool(processes=kParallelNum) as pool:
|
|
pool.map(ProcessEntryPoint, task_args_list)
|
|
print(f"\nFinal Results: \n")
|
|
res_array=sorted(shared_dict.items())
|
|
for item in res_array:
|
|
time_point, status = item
|
|
print(f"time point={time_point}",end=" ")
|
|
if status==-1:
|
|
print("NoCollision")
|
|
elif status==0:
|
|
print("CollisionOnEdge")
|
|
else:
|
|
print("CollisionInBlock") |