2021-03-30 18:56:55 +02:00
|
|
|
from threading import Thread
|
2021-03-30 17:09:29 +02:00
|
|
|
from datetime import timedelta
|
|
|
|
from datetime import datetime
|
|
|
|
import random
|
|
|
|
import time
|
|
|
|
|
2021-03-31 09:51:42 +02:00
|
|
|
local_threads_number = 7 # in a more realistic model: local threads number << global arrows state size
|
|
|
|
# then, locations would be picked at random in this great global arrows state
|
|
|
|
# but the goal of this "micro-model" is not to explore the algorithms of choice of an arrow in the state
|
|
|
|
# it is to fix the way the scheduler terminates the local threads that have done their task
|
|
|
|
# in a realistic model, the number of cycles is infinite (the user decides when to stop a running simulation)
|
|
|
|
# and the size of the "renew" list (the list of the active local threads) depends on the computational power available
|
|
|
|
# this "micro-model" allows to explore the ineractions [scheduler <-> local threads] when the the "renew" list size varies
|
|
|
|
renew_length = 16 # renew_length can vary from 1 to number_of_cycles (if more, a part of it remains empty)
|
|
|
|
number_of_cycles = 20 # in a realistic model: number_of_cycles >> renew_length
|
|
|
|
|
2021-03-30 18:56:55 +02:00
|
|
|
renew = []
|
|
|
|
done = []
|
2021-03-31 08:02:27 +02:00
|
|
|
arrows = []
|
2021-03-30 18:56:55 +02:00
|
|
|
copy = []
|
|
|
|
|
|
|
|
def init():
|
2021-03-30 21:36:01 +02:00
|
|
|
for i in range(0, renew_length):
|
|
|
|
renew.append(0)
|
|
|
|
for j in range(0, local_threads_number):
|
2021-03-31 09:51:42 +02:00
|
|
|
arrows.append(random.randint(10,99))
|
2021-03-31 08:02:27 +02:00
|
|
|
copy.append(arrows[j])
|
2021-03-31 09:51:42 +02:00
|
|
|
print(' ',arrows,' < initial global arrows state',' '*11,'now start delta ',end='[')
|
2021-03-30 21:36:01 +02:00
|
|
|
for i in range(0, renew_length): print('{:>4}'.format(renew[i]), end='')
|
|
|
|
print(']')
|
2021-03-30 18:56:55 +02:00
|
|
|
|
|
|
|
def disp(coord, id, start, prev, next):
|
2021-03-31 09:51:42 +02:00
|
|
|
print(' {} at [{}] {} > {} by thread n°{:>3} {: >.3f} - {: >.3f} = {!s:.4} {}'.format(
|
2021-03-31 08:02:27 +02:00
|
|
|
arrows,
|
2021-03-30 18:56:55 +02:00
|
|
|
coord,
|
|
|
|
prev,
|
|
|
|
next,
|
|
|
|
str(id),
|
2021-03-31 09:51:42 +02:00
|
|
|
datetime.now().timestamp() / 10 - int(datetime.now().timestamp() / 10),
|
|
|
|
start / 10 - int(start / 10),
|
2021-03-30 18:56:55 +02:00
|
|
|
datetime.now().timestamp() - start,
|
2021-03-30 21:36:01 +02:00
|
|
|
'[', # renew
|
|
|
|
),
|
|
|
|
end =''
|
2021-03-30 18:56:55 +02:00
|
|
|
)
|
2021-03-30 21:36:01 +02:00
|
|
|
for i in range(0, renew_length): print('{:>4}'.format(renew[i]), end='')
|
|
|
|
print(']')
|
2021-03-30 18:56:55 +02:00
|
|
|
|
|
|
|
|
2021-03-30 21:36:01 +02:00
|
|
|
def local_thread(coord, id):
|
2021-03-30 17:09:29 +02:00
|
|
|
start = datetime.now().timestamp()
|
|
|
|
val = random.randint(1,1000)
|
|
|
|
time.sleep(val / 1000)
|
2021-03-31 08:02:27 +02:00
|
|
|
prev = arrows[coord]
|
2021-03-31 09:51:42 +02:00
|
|
|
next = arrows[coord] = 10 + val % 89 # ou n'importe quelle autre modification !...
|
2021-03-30 18:56:55 +02:00
|
|
|
done.append(id)
|
2021-03-30 21:36:01 +02:00
|
|
|
for i in range(0, renew_length):
|
|
|
|
if renew[i] == id:
|
|
|
|
renew[i] = 0
|
2021-03-30 18:56:55 +02:00
|
|
|
break
|
|
|
|
disp(coord, id, start, prev, next)
|
2021-03-30 17:09:29 +02:00
|
|
|
|
2021-03-30 21:36:01 +02:00
|
|
|
init() # Scheduler
|
2021-03-31 08:02:27 +02:00
|
|
|
for id in range (0, number_of_cycles):
|
|
|
|
for i in range (0, renew_length):
|
|
|
|
if renew[i] == 0:
|
|
|
|
renew[i] = id
|
|
|
|
break
|
|
|
|
# là où les local_thread qui se terminent ont écrit un zéro,
|
|
|
|
# les local_thread nouvellement créés devraient apparaitre !?
|
|
|
|
t = Thread(target=local_thread, args=(random.randint(0, len(arrows) - 1), id))
|
2021-03-30 17:09:29 +02:00
|
|
|
t.start()
|
2021-03-30 18:56:55 +02:00
|
|
|
time.sleep(1)
|
2021-03-31 09:51:42 +02:00
|
|
|
print(' ',copy,' < initial global arrows state (to compare)')
|
2021-03-30 21:36:01 +02:00
|
|
|
print('history: ',done) # done.sort() # print(done)
|
2021-03-30 18:56:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-30 17:09:29 +02:00
|
|
|
|
|
|
|
|
2021-03-31 09:51:42 +02:00
|
|
|
|
|
|
|
|
2021-03-30 17:09:29 +02:00
|
|
|
"""
|
2021-03-30 12:24:04 +02:00
|
|
|
Le **scheduler**, ou processus principal, effectue un calcul sur l'**état global**.
|
2021-03-30 21:36:01 +02:00
|
|
|
Pour cela, il génère des threads de calcul locaux ou '**local_threads**'
|
2021-03-30 12:24:04 +02:00
|
|
|
auxquels il confie une partie de l'état global.
|
2021-03-30 21:36:01 +02:00
|
|
|
Il est seul à avoir accès à l'état global et à la liste des local_threads.
|
2021-03-30 12:24:04 +02:00
|
|
|
Il n'a pas accès aux **règles de transition**.
|
2021-03-30 21:36:01 +02:00
|
|
|
Chaque local_thread effectue un calcul local puis en soumet le résultat au scheduler.
|
|
|
|
Le scheduler assigne les résultats des local_threads à l'état global.
|
2021-03-30 12:24:04 +02:00
|
|
|
Il délègue à un **superviseur** des vérification périodiques de l'intégrité de l'état global et peut interrompre le processus en cas d'erreur.
|
|
|
|
Il délègue à la **CLI** les fonctions de communications avec les modules périphériques.
|
|
|
|
|
|
|
|
Il exécute un **cycle** qui comporte **deux étapes** principales:
|
|
|
|
|
|
|
|
1. recherche aléatoire d'un espace local
|
|
|
|
+ si trouvé:
|
2021-03-30 18:56:55 +02:00
|
|
|
- arrêt de cette recherche
|
|
|
|
- préemption de cet espace local
|
2021-03-30 12:24:04 +02:00
|
|
|
- initiation d'un nouveau thread de calcul auquel est attibué cet espace local
|
|
|
|
+ sinon arrêt de cette recherche en un temps fini
|
2021-03-30 18:56:55 +02:00
|
|
|
|
|
|
|
2. recherche des threads de calcul en fin d'exécution
|
2021-03-30 12:24:04 +02:00
|
|
|
(ces threads se signalent dans une liste; leur temps de calcul est aléatoire)
|
|
|
|
+ si trouvé(s):
|
2021-03-30 18:56:55 +02:00
|
|
|
- arrêt de cette recherche
|
2021-03-30 12:24:04 +02:00
|
|
|
- mise à jour de l'état global (insertion du ou des états locaux calculés)
|
|
|
|
- terminaison des threads de calcul concernés et libération des verrous associés
|
|
|
|
+ sinon arrêt de cette recherche en un temps fini
|
2021-03-30 18:56:55 +02:00
|
|
|
|
2021-03-30 12:24:04 +02:00
|
|
|
3. mesures / recueil des commandes / retour d'information
|
|
|
|
|
2021-03-30 18:56:55 +02:00
|
|
|
"""
|