diff --git a/py_template/end.cpp b/py_template/end.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6b694184d95ad934769e712e499b3bcb3a53006f --- /dev/null +++ b/py_template/end.cpp @@ -0,0 +1,7 @@ + // clean up + fftwf_mpi_cleanup(); + fftw_mpi_cleanup(); + MPI_Finalize(); + return EXIT_SUCCESS; +} + diff --git a/py_template/start.cpp b/py_template/start.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ab45302bd079b2332db7a3ade0e8397b36e51e2 --- /dev/null +++ b/py_template/start.cpp @@ -0,0 +1,13 @@ +#include "base.hpp" +#include "fluid_solver.hpp" +#include <iostream> +#include <fftw3-mpi.h> + +int myrank, nprocs; + +int main(int argc, char *argv[]) +{ + MPI_Init(&argc, &argv); + MPI_Comm_rank(MPI_COMM_WORLD, &myrank); + MPI_Comm_size(MPI_COMM_WORLD, &nprocs); + diff --git a/py_template/test_FFT.cpp b/py_template/test_FFT.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34d6f95453898b065654cea34a5f67f5a8dfbd0e --- /dev/null +++ b/py_template/test_FFT.cpp @@ -0,0 +1,16 @@ +fluid_solver<float> *fs; +fs = new fluid_solver<float>(32, 32, 32); +DEBUG_MSG("fluid_solver object created\n"); + +fs->fc->read( + "Kdata0", + (void*)fs->cvorticity); +fftwf_execute(*(fftwf_plan*)fs->c2r_vorticity); +fftwf_execute(*(fftwf_plan*)fs->r2c_vorticity); +fs->fc->write( + "Kdata1", + (void*)fs->cvorticity); + +delete fs; +DEBUG_MSG("fluid_solver object deleted\n"); + diff --git a/test.py b/test.py new file mode 100644 index 0000000000000000000000000000000000000000..bc3d31ff2443e747768397a246e5183f4419b00a --- /dev/null +++ b/test.py @@ -0,0 +1,58 @@ +import numpy as np +import subprocess +import pyfftw + +def run_test( + test_name = 'test_FFT', + ncpu = 4): + # first, write file + with open('src/' + test_name + '.cpp', 'w') as outfile: + for fname in ['py_template/start.cpp', + 'py_template/' + test_name + '.cpp', + 'py_template/end.cpp']: + with open(fname) as infile: + outfile.write(infile.read() + '\n') + + # now compile code and run + if subprocess.call(['make', test_name + '.elf']) == 0: + subprocess.call(['time', + 'mpirun', + '-np', + '{0}'.format(ncpu), + './' + test_name + '.elf']) + return None + +def generate_data_3D( + n, + dtype = np.complex128, + p = 1.5): + """ + generate something that has the proper shape + """ + assert(n % 2 == 0) + a = np.zeros((n, n, n/2+1), dtype = dtype) + a[:] = np.random.randn(*a.shape) + 1j*np.random.randn(*a.shape) + k, j, i = np.mgrid[-n/2+1:n/2+1, -n/2+1:n/2+1, 0:n/2+1] + k = (k**2 + j**2 + i**2)**.5 + k = np.roll(k, n//2+1, axis = 0) + k = np.roll(k, n//2+1, axis = 1) + a /= k**p + a[0, :, :] = 0 + a[:, 0, :] = 0 + a[:, :, 0] = 0 + ii = np.where(k == 0) + a[ii] = 0 + ii = np.where(k > n/3) + a[ii] = 0 + return a + +Kdata0 = generate_data_3D(32, p = 2).astype(np.complex64) +Kdata0.tofile("Kdata0") +run_test('test_FFT') +Kdata1 = np.fromfile('Kdata1', dtype = np.complex64).reshape(Kdata0.shape) + +print np.max(np.abs(Kdata0 - Kdata1)) + + + +