comm = MPI.COMM_WORLD
da = DA(comm, (8, 32, 16), (2, 2, 2), 1)
x, y, z = DA_arange(da, (0, 2 * np.pi), (0, 2 * np.pi), (0, 2 * np.pi))
f = y * np.cos(x * y) + np.sin(z) * y
dfdx_true = (-y**2) * np.sin(x * y)
dfdy_true = -(x * y) * np.sin(x * y) + np.cos(x * y) + np.sin(z)
dfdz_true = y * np.cos(z)

dz = z[1, 0, 0] - z[0, 0, 0]
dy = y[0, 1, 0] - y[0, 0, 0]
dx = x[0, 0, 1] - x[0, 0, 0]

cfd = CompactFiniteDifferenceSolver(da)

dfdx = cfd.dfdx(f, dx)
dfdx_global = np.zeros([16, 64, 32], dtype=np.float64)
dfdx_true_global = np.zeros([16, 64, 32], dtype=np.float64)

DA_gather_blocks(da, dfdx, dfdx_global)
DA_gather_blocks(da, dfdx_true, dfdx_true_global)

if comm.Get_rank() == 0:
    plt.plot(np.linspace(0, 2 * np.pi, 32), dfdx_global[8, 32, :], 'o-')
    plt.plot(np.linspace(0, 2 * np.pi, 32), dfdx_true_global[8, 32, :])

dfdy = cfd.dfdy(f, dy)
dfdy_global = np.zeros([16, 64, 32], dtype=np.float64)
dfdy_true_global = np.zeros([16, 64, 32], dtype=np.float64)

DA_gather_blocks(da, dfdy, dfdy_global)
Esempio n. 2
0
comm = MPI.COMM_WORLD 
da = DA(comm, (8, 32, 16), (2, 2, 2), 1)
x, y, z = DA_arange(da, (0, 2*np.pi), (0, 2*np.pi), (0, 2*np.pi))
f = y*np.cos(x*y) + np.sin(z)*y
dfdx_true = (-y**2)*np.sin(x*y)
dfdy_true = -(x*y)*np.sin(x*y) + np.cos(x*y) + np.sin(z)
dfdz_true = y*np.cos(z)

dz = z[1, 0, 0] - z[0, 0, 0]
dy = y[0, 1, 0] - y[0, 0, 0]
dx = x[0, 0, 1] - x[0, 0, 0]

cfd = CompactFiniteDifferenceSolver(da)

dfdx = cfd.dfdx(f, dx)
dfdx_global = np.zeros([16, 64, 32], dtype=np.float64)
dfdx_true_global = np.zeros([16, 64, 32], dtype=np.float64)

DA_gather_blocks(da, dfdx, dfdx_global)
DA_gather_blocks(da, dfdx_true, dfdx_true_global)

if comm.Get_rank() == 0:
    plt.plot(np.linspace(0, 2*np.pi, 32), dfdx_global[8, 32, :], 'o-')
    plt.plot(np.linspace(0, 2*np.pi, 32), dfdx_true_global[8, 32, :])

dfdy = cfd.dfdy(f, dy)
dfdy_global = np.zeros([16, 64, 32], dtype=np.float64)
dfdy_true_global = np.zeros([16, 64, 32], dtype=np.float64)

DA_gather_blocks(da, dfdy, dfdy_global)
mean_errs = []
max_errs = []
sizes = [16, 32, 64, 128, 256]

for i, N in enumerate(sizes):
    da = DA(comm, (N, N, N), (2, 2, 2), 1)
    line_da = da.get_line_DA(0)
    cfd = CompactFiniteDifferenceSolver(line_da)
    x, y, z = DA_arange(da, (0, 2*np.pi), (0, 2*np.pi), (0, 2*np.pi))
    f = np.sin(x) + np.cos(y*x) + z*x
    f_d = gpuarray.to_gpu(f)
    x_d = da.create_global_vector()
    f_local_d = da.create_local_vector()
    dfdx_true = np.cos(x) + -y*np.sin(y*x) + z
    dx = x[0, 0, 1] - x[0, 0, 0]
    cfd.dfdx(f_d, dx, x_d, f_local_d)
    dfdx = x_d.get()
    err = np.abs(dfdx-dfdx_true)/np.max(abs(dfdx))
    mean_err = np.mean(np.abs(dfdx-dfdx_true)/np.max(abs(dfdx)))
    max_err = np.max(np.abs(dfdx-dfdx_true)/np.max(abs(dfdx)))
    mean_errs.append(mean_err)
    max_errs.append(max_err)

if rank == 0:
    print 
    for i, N in enumerate(sizes[1:]):
        print "Mean err(N={0})/ Mean err(N={1}) = {2}".format(sizes[i], sizes[i+1], mean_errs[i]/mean_errs[i+1])
    print 
    for i, N in enumerate(sizes[1:]):
        print "Max err(N={0})/ Max err(N={1}) = {2}".format(sizes[i], sizes[i+1], max_errs[i]/max_errs[i+1])
MPI.Finalize()
mean_errs = []
max_errs = []
sizes = [16, 32, 64, 128, 256]

for i, N in enumerate(sizes):
    da = DA(comm, (N, N, N), (2, 2, 2), 1)
    line_da = da.get_line_DA(0)
    cfd = CompactFiniteDifferenceSolver(line_da)
    x, y, z = DA_arange(da, (0, 2 * np.pi), (0, 2 * np.pi), (0, 2 * np.pi))
    f = np.sin(x) + np.cos(y * x) + z * x
    f_d = gpuarray.to_gpu(f)
    x_d = da.create_global_vector()
    f_local_d = da.create_local_vector()
    dfdx_true = np.cos(x) + -y * np.sin(y * x) + z
    dx = x[0, 0, 1] - x[0, 0, 0]
    cfd.dfdx(f_d, dx, x_d, f_local_d)
    dfdx = x_d.get()
    err = np.abs(dfdx - dfdx_true) / np.max(abs(dfdx))
    mean_err = np.mean(np.abs(dfdx - dfdx_true) / np.max(abs(dfdx)))
    max_err = np.max(np.abs(dfdx - dfdx_true) / np.max(abs(dfdx)))
    mean_errs.append(mean_err)
    max_errs.append(max_err)

if rank == 0:
    print
    for i, N in enumerate(sizes[1:]):
        print "Mean err(N={0})/ Mean err(N={1}) = {2}".format(
            sizes[i], sizes[i + 1], mean_errs[i] / mean_errs[i + 1])
    print
    for i, N in enumerate(sizes[1:]):
        print "Max err(N={0})/ Max err(N={1}) = {2}".format(