make example consistent with blog

This commit is contained in:
Maarten A. Breddels 2021-01-06 19:21:57 +01:00
parent 27ebe7646c
commit 9e72e0fb3d
3 changed files with 29 additions and 13 deletions

View File

@ -133,15 +133,15 @@ N = 1024*1024*32
M = 4 M = 4
x = np.arange(N, dtype='f8') x = np.arange(N, dtype='f8')
def run(): def some_numpy_computation():
total = 0 total = 0
for i in range(M): for i in range(M):
total += x.sum() total += x.sum()
return total return total
thread1 = threading.Thread(target=run) thread1 = threading.Thread(target=some_numpy_computation)
thread2 = threading.Thread(target=run) thread2 = threading.Thread(target=some_numpy_computation)
def main(args=None): def main(args=None):
thread1.start() thread1.start()

View File

@ -1,21 +1,35 @@
# same as example1, but without explicit viztracer calls # same as example1, but without explicit viztracer calls
import threading import threading
import time import time
import time
# if we don't run with gil_load, we just skip it
import gil_load
try:
gil_load.init()
gil_load.start()
use_gil_load = True
except RuntimeError:
use_gil_load = False
def run(): def some_computation():
total = 0 total = 0
for i in range(1_000_000): for i in range(1_000_000):
total += i total += i
return total return total
thread1 = threading.Thread(target=run) thread1 = threading.Thread(target=some_computation)
thread2 = threading.Thread(target=run) thread2 = threading.Thread(target=some_computation)
def main(args=None): def main(args=None):
thread1.start() thread1.start()
thread2.start() thread2.start()
time.sleep(0.2) time.sleep(0.2)
for thread in [thread1, thread2]: for thread in [thread1, thread2]:
thread.join() thread.join()
if use_gil_load:
gil_load.stop()
stats = gil_load.get()
print(gil_load.format(stats))
if __name__ == "__main__":
main()

View File

@ -7,6 +7,7 @@ import numpy as np
import gil_load import gil_load
try: try:
gil_load.init() gil_load.init()
gil_load.start()
use_gil_load = True use_gil_load = True
except RuntimeError: except RuntimeError:
use_gil_load = False use_gil_load = False
@ -16,24 +17,23 @@ N = 1024*1024*32
M = 4 M = 4
x = np.arange(N, dtype='f8') x = np.arange(N, dtype='f8')
def run():
def some_numpy_computation():
total = 0 total = 0
for i in range(M): for i in range(M):
total += x.sum() total += x.sum()
return total return total
if use_gil_load: thread1 = threading.Thread(target=some_numpy_computation)
gil_load.start() thread2 = threading.Thread(target=some_numpy_computation)
thread1 = threading.Thread(target=run)
thread2 = threading.Thread(target=run)
def main(args=None): def main(args=None):
thread1.start() thread1.start()
thread2.start() thread2.start()
total = 0 total = 0
for i in range(1_000_000): for i in range(2_000_000):
total += i total += i
for thread in [thread1, thread2]: for thread in [thread1, thread2]:
thread.join() thread.join()
@ -42,3 +42,5 @@ def main(args=None):
gil_load.stop() gil_load.stop()
stats = gil_load.get() stats = gil_load.get()
print(gil_load.format(stats)) print(gil_load.format(stats))
if __name__ == "__main__":
main()