summaryrefslogtreecommitdiff
path: root/hpc/mlti_process.py
diff options
context:
space:
mode:
authorzhang <zch921005@126.com>2022-05-21 14:23:49 +0800
committerzhang <zch921005@126.com>2022-05-21 14:23:49 +0800
commit678fab50280b647d95213a9695d07c49542696f2 (patch)
tree74ca60de14311a8a2ff58dbf82d9b7574c9cd3ef /hpc/mlti_process.py
parent2180c68999eb8dc0c7bcec015b2703f5b8b20223 (diff)
0521
Diffstat (limited to 'hpc/mlti_process.py')
-rw-r--r--hpc/mlti_process.py18
1 files changed, 18 insertions, 0 deletions
diff --git a/hpc/mlti_process.py b/hpc/mlti_process.py
new file mode 100644
index 0000000..4a129c4
--- /dev/null
+++ b/hpc/mlti_process.py
@@ -0,0 +1,18 @@
+import os
+from multiprocessing import Process, Pool, Manager
+from random import random
+
+
+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))