summaryrefslogtreecommitdiff
path: root/hpc/mlti_threads.py
blob: 9eb73ac5d89ff2db57a698094723c84f99a05793 (plain)
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
import os
from multiprocessing import Process, Pool, Manager
from random import random
from threading import Thread
import os



def monte_carlo_pi(i, n=5000_0000):
    print('epoch: {}'.format(i))
    cnt = 0
    for i in range(n):
        x, y = random(), random()
        if x**2 + y**2 <= 1:
            cnt += 1
    return 4*cnt/n


if __name__ == '__main__':
    # pool = Pool(os.cpu_count()//2)
    # pool.map(monte_carlo_pi, range(10))

    thread_list = []
    n_threads = 10
    for i in range(n_threads):
        t = Thread(target=monte_carlo_pi, args=(i, ))
        thread_list.append(t)

    for t in thread_list:
        t.start()

    for t in thread_list:
        t.join()