Update Concurrent Programming
This commit is contained in:
parent
9d729412d4
commit
a467610eae
|
@ -4,9 +4,16 @@ from datetime import datetime
|
|||
import random
|
||||
import time
|
||||
|
||||
renew_length = 12
|
||||
local_threads_number = 10
|
||||
number_of_cycles = 20
|
||||
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
|
||||
|
||||
renew = []
|
||||
done = []
|
||||
arrows = []
|
||||
|
@ -16,19 +23,21 @@ def init():
|
|||
for i in range(0, renew_length):
|
||||
renew.append(0)
|
||||
for j in range(0, local_threads_number):
|
||||
arrows.append(random.randint(10,99)) # nombres à deux chiffres pour simplifier l'affichage
|
||||
arrows.append(random.randint(10,99))
|
||||
copy.append(arrows[j])
|
||||
print(' ',arrows,' < initial global state',' '*27,end='[')
|
||||
print(' ',arrows,' < initial global arrows state',' '*11,'now start delta ',end='[')
|
||||
for i in range(0, renew_length): print('{:>4}'.format(renew[i]), end='')
|
||||
print(']')
|
||||
|
||||
def disp(coord, id, start, prev, next):
|
||||
print(' {} at [{}] {} > {} by thread n°{:>3} {!s:.4} {}'.format(
|
||||
print(' {} at [{}] {} > {} by thread n°{:>3} {: >.3f} - {: >.3f} = {!s:.4} {}'.format(
|
||||
arrows,
|
||||
coord,
|
||||
prev,
|
||||
next,
|
||||
str(id),
|
||||
datetime.now().timestamp() / 10 - int(datetime.now().timestamp() / 10),
|
||||
start / 10 - int(start / 10),
|
||||
datetime.now().timestamp() - start,
|
||||
'[', # renew
|
||||
),
|
||||
|
@ -43,7 +52,7 @@ def local_thread(coord, id):
|
|||
val = random.randint(1,1000)
|
||||
time.sleep(val / 1000)
|
||||
prev = arrows[coord]
|
||||
next = arrows[coord] = 10 + val % 89
|
||||
next = arrows[coord] = 10 + val % 89 # ou n'importe quelle autre modification !...
|
||||
done.append(id)
|
||||
for i in range(0, renew_length):
|
||||
if renew[i] == id:
|
||||
|
@ -62,13 +71,15 @@ for id in range (0, number_of_cycles):
|
|||
t = Thread(target=local_thread, args=(random.randint(0, len(arrows) - 1), id))
|
||||
t.start()
|
||||
time.sleep(1)
|
||||
print(' ',copy,' < initial global state (to compare)')
|
||||
print(' ',copy,' < initial global arrows state (to compare)')
|
||||
print('history: ',done) # done.sort() # print(done)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
"""
|
||||
Le **scheduler**, ou processus principal, effectue un calcul sur l'**état global**.
|
||||
Pour cela, il génère des threads de calcul locaux ou '**local_threads**'
|
||||
|
|
Loading…
Reference in New Issue