1from ctypes
import CDLL, c_double, c_void_p, c_int
6from utils.pbutils
import make_nei_ele, read_input
11def run(params, comm, lib):
13 nvars, nface, ndims = 5, 6, 3
15 nx, ny, nz, npx, npy, npz, nstep = params
16 m, n, l = nx//npx, ny//npy, nz//npz
18 rank = comm.Get_rank()
21 update = lib.serial_update
22 pre_lusgs = lib.serial_pre_lusgs
23 lower_sweep = lib.ns_serial_lower_sweep
24 upper_sweep = lib.ns_serial_upper_sweep
26 update.argtypes = [c_int, c_int, c_void_p, c_void_p]
27 pre_lusgs.argtypes = [c_int, c_int, c_double, \
28 c_void_p, c_void_p, c_void_p, c_void_p]
29 lower_sweep.argtypes = [c_int, c_int, c_int, c_int, \
30 c_void_p, c_void_p, c_void_p, c_void_p, c_void_p, \
31 c_void_p, c_void_p, c_void_p, c_void_p, c_void_p]
32 upper_sweep.argtypes = [c_int, c_int, c_int, c_int, \
33 c_void_p, c_void_p, c_void_p, c_void_p, c_void_p, \
34 c_void_p, c_void_p, c_void_p, c_void_p, c_void_p]
37 print(
"[Run] Allocating arrays...")
40 nei_ele = np.empty((nface, nele), dtype=np.int32)
41 mapping = np.arange(nele, dtype=np.int32)
42 unmapping = np.arange(nele, dtype=np.int32)
45 rho, u, v, w, p = gamma, 1.5, 0, 0, 1
46 et = p / (gamma - 1) + 0.5*rho*u**2
47 upts = np.empty((nvars, nele), dtype=np.float64)
48 rhs = np.empty_like(upts)
49 dub = np.empty_like(upts)
50 diag = np.ones((nele,), dtype=np.float64)
51 fspr = np.ones((6, nele), dtype=np.float64)
52 dt = np.ones((nele,), dtype=np.float64)*0.1
53 fnorm_vol = np.ones((nface, nele), dtype=np.float64)
54 vfi = np.array([[0, -1, 0], [-1, 0, 0], [0, 0, 1], [1, 0, 0], [0, 0, -1], [0, 1, 0]], dtype=np.float64)
55 vec_fnorm = np.repeat(vfi[:, :, np.newaxis], nele, axis=2)
59 print(
"[Run] Initializing arrays...")
60 upts[0] = rhs[0] = dub[0] = rho
61 upts[1] = rhs[1] = dub[1] = rho*u
62 upts[2] = rhs[2] = dub[2] = rho*v
63 upts[3] = rhs[3] = dub[3] = rho*w
64 upts[4] = rhs[4] = dub[4] = et
66 make_nei_ele(m, n, l, nei_ele)
70 print(
"[Run] Starting iteration...")
71 tarr = np.zeros(nstep, dtype=np.float64)
73 for i
in range(nstep):
75 pre_lusgs(nele, nface, 1.0, fnorm_vol.ctypes.data, dt.ctypes.data, \
76 diag.ctypes.data, fspr.ctypes.data)
77 lower_sweep(nele, nvars, nface, ndims, nei_ele.ctypes.data, mapping.ctypes.data, \
78 unmapping.ctypes.data, fnorm_vol.ctypes.data, vec_fnorm.ctypes.data, \
79 upts.ctypes.data, rhs.ctypes.data, dub.ctypes.data, \
80 diag.ctypes.data, fspr.ctypes.data)
81 upper_sweep(nele, nvars, nface, ndims, nei_ele.ctypes.data, mapping.ctypes.data, \
82 unmapping.ctypes.data, fnorm_vol.ctypes.data, vec_fnorm.ctypes.data, \
83 upts.ctypes.data, rhs.ctypes.data, dub.ctypes.data, \
84 diag.ctypes.data, fspr.ctypes.data)
85 update(nele, nvars, upts.ctypes.data, rhs.ctypes.data)
87 tarr[i] = MPI.Wtime() - start
90 return tarr[15:].mean()*1000
92 return tarr.mean()*1000
96 m, n, l, dm, dn, dl, nstep = params
99 f = open(
"MPIOUTPUT_py_{}_{}.txt".format(nprocs, neles),
'w')
100 f.write(
"========= LU-SGS example output =========\n\n")
101 f.write(
"*** Problem setup ***\n")
102 f.write(
"Number of cores: {}\n".format(nprocs))
103 f.write(
"Number of iteration step: {}\n".format(nstep))
104 f.write(
"Number of elements: {} = {} x {} x {}\n".format(neles, m, n, l))
105 f.write(
"Partitioning with {}: {} x {} x {}\n".format(dm*dn*dl, dm, dn, dl))
106 f.write(
"Elements per core: {} = {} x {} x {}\n\n".format(neles//nprocs, m//dm, n//dn, l//dl))
107 f.write(
"*** Average runtime for each processor [ms] ***\n")
108 for i
in range(nprocs):
109 f.write(
"{:.6f}\n".format(times[i]))
110 f.write(
"\n*** Average runtime for entire processors [ms] ***\n")
111 f.write(
"{:.6f}\n".format(sum(times)/nprocs))
115if __name__ ==
"__main__":
117 comm = MPI.COMM_WORLD
118 rank = comm.Get_rank()
119 nproc = comm.Get_size()
122 if (len(sys.argv)) < 3:
123 print(
"[Error] Input file, dynamic library argument is needed")
126 inputname = sys.argv[1]
127 params = read_input(inputname)
135 if nproc != dm*dn*dl:
136 print(
"[Error] Partitioning number mismatch")
137 print(
"{} processors, but total {} partitioning".format(nproc, dm*dn*dl))
140 print(
"[Main] 3D hexahedral example starts...")
144 params = np.empty(7, dtype=np.int64)
146 err = comm.bcast(err, root=0)
150 params = comm.bcast(params, root=0)
154 lib = CDLL(sys.argv[2])
155 except FileNotFoundError:
156 print(
"[Error] Dynamic library file not found")
159 avtimei =
run(params, comm, lib)
160 tbuf = comm.gather(avtimei)
163 print(
"[Main] Run finished. Writing result...")
def run(params, comm, lib)
def write_output(nprocs, params, times)