-
Notifications
You must be signed in to change notification settings - Fork 4
/
multi_plot_test.py
118 lines (68 loc) · 1.98 KB
/
multi_plot_test.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python
# coding: utf-8
# In[1]:
#demonstrator for plotting with matplotlib with multiprocessing
#https://docs.python.org/3/library/multiprocessing.html
import multiprocessing as mp
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import time
import sys
import os
matplotlib.use('Agg')
#### check for system type
#server
if sys.platform == 'linux':
print('system is linux')
used=50
#mac
if sys.platform =='darwin':
print('system is mac')
print(os.system('sysctl -n hw.physicalcpu'))
print(os.system('sysctl -n hw.logicalcpu'))
used=8
print()
plotsdir='plots_multi'
if os.path.isdir(plotsdir) == False: os.mkdir(plotsdir)
# In[9]:
def make_plot(i):
plt.figure(1,figsize=(20,10),dpi=100)
plt.plot(data,'ok',markersize=0.1,alpha=0.1)
plt.title(str(i)+title)
plt.savefig(plotsdir+'/'+str(i)+'.png',dpi=100)
plt.close(1)
counter = np.arange(0,10,1) # pool counter
#global variables
title ='Hello'
data= np.random.rand(1000000) #1 mio random numbers
print(data)
print(counter)
#print(len(np.arange(0,100,1e-4)))
#p=zip([parameters,data])
# In[10]:
t0 = time.time()
#define pool using fork and number of processes
#using fork works on both mac and linux
pool=mp.get_context('fork').Pool(processes=used)
print('Using multiprocessing, nr of cores',mp.cpu_count(), ', nr of processes used: ',used)
# Map the worker function onto the parameters
pool.map(make_plot, counter)
pool.close()
pool.join()
t1 = time.time()
multi_time=np.round(t1-t0,2)
print('done')
print('plotting takes', np.round(multi_time,2), 'seconds')
# In[11]:
t0 = time.time()
for i in counter:
make_plot(i)
t1 = time.time()
single_time=np.round(t1-t0,2)
print('plotting takes', np.round(single_time,2), 'seconds')
print('multiprocessing is a factor,',np.round(single_time/multi_time,2), ' faster')
# In[ ]:
##example for writing in the same array with multiple processes
####
# In[ ]: