Skip to content
Snippets Groups Projects
Commit a4d96433 authored by Chichi Lalescu's avatar Chichi Lalescu
Browse files

add py3 test file

test where only some CPUs read data seems to be working.
parent 34b2ecc4
No related branches found
No related tags found
No related merge requests found
MPICXX = mpicxx MPICXX = mpicxx
LINKER = mpicxx LINKER = mpicxx
DEFINES = #-DNDEBUG DEFINES = -DNDEBUG
CFLAGS = -Wall \ CFLAGS = -Wall \
-O2 \ -O2 \
#-pg \ #-pg \
......
{
"metadata": {
"name": "",
"signature": "sha256:e744ea7dc72564d6f526a0a73b97b6f43c1ccd7d5c6c2767d8b6c5a5ec7f4487"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np\n",
"import subprocess\n",
"%matplotlib nbagg\n",
"import matplotlib.pyplot as plt\n",
"import pyfftw"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def generate_data_3D(\n",
" n,\n",
" dtype = np.complex128,\n",
" p = 1.5):\n",
" \"\"\"\n",
" generate something that has the proper shape\n",
" \"\"\"\n",
" assert(n % 2 == 0)\n",
" a = np.zeros((n, n, n/2+1), dtype = dtype)\n",
" a[:] = np.random.randn(*a.shape) + 1j*np.random.randn(*a.shape)\n",
" k, j, i = np.mgrid[-n/2+1:n/2+1, -n/2+1:n/2+1, 0:n/2+1]\n",
" k = (k**2 + j**2 + i**2)**.5\n",
" k = np.roll(k, n//2+1, axis = 0)\n",
" k = np.roll(k, n//2+1, axis = 1)\n",
" a /= k**p\n",
" a[0, :, :] = 0\n",
" a[:, 0, :] = 0\n",
" a[:, :, 0] = 0\n",
" ii = np.where(k == 0)\n",
" a[ii] = 0\n",
" ii = np.where(k > n/3)\n",
" a[ii] = 0\n",
" return a\n",
"\n",
"n = 31*4\n",
"N = 256\n",
"\n",
"Kdata0 = generate_data_3D(n, p = 2).astype(np.complex64)\n",
"Kdata1 = generate_data_3D(n, p = 2).astype(np.complex64)\n",
"Kdata2 = generate_data_3D(n, p = 2).astype(np.complex64)\n",
"Kdata0.T.copy().tofile(\"Kdata0\")\n",
"Kdata1.T.copy().tofile(\"Kdata1\")\n",
"Kdata2.T.copy().tofile(\"Kdata2\")"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 14
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def padd_with_zeros(\n",
" a,\n",
" n,\n",
" odtype = None):\n",
" if (type(odtype) == type(None)):\n",
" odtype = a.dtype\n",
" assert(a.shape[0] <= n)\n",
" b = np.zeros((n, n, n/2 + 1), dtype = odtype)\n",
" m = a.shape[0]\n",
" b[ :m/2, :m/2, :m/2+1] = a[ :m/2, :m/2, :m/2+1]\n",
" b[ :m/2, n-m/2: , :m/2+1] = a[ :m/2, m-m/2: , :m/2+1]\n",
" b[n-m/2: , :m/2, :m/2+1] = a[m-m/2: , :m/2, :m/2+1]\n",
" b[n-m/2: , n-m/2: , :m/2+1] = a[m-m/2: , m-m/2: , :m/2+1]\n",
" return b\n",
"\n",
"def transform_py(bla):\n",
" b = padd_with_zeros(bla, N)\n",
" c = np.zeros((N, N, N), np.float32)\n",
" t = pyfftw.FFTW(\n",
" b, c,\n",
" axes = (0, 1, 2),\n",
" direction = 'FFTW_BACKWARD',\n",
" flags = ('FFTW_ESTIMATE',),\n",
" threads = 2)\n",
" t.execute()\n",
" return c\n",
"\n",
"import chichi_misc as cm\n",
"\n",
"def array_to_8cubes(\n",
" a,\n",
" odtype = None):\n",
" assert(len(a.shape) >= 3)\n",
" assert((a.shape[0] % 8 == 0) and\n",
" (a.shape[1] % 8 == 0) and\n",
" (a.shape[2] % 8 == 0))\n",
" if type(odtype) == type(None):\n",
" odtype = a.dtype\n",
" c = np.zeros(\n",
" ((((a.shape[0] // 8)*(a.shape[1] // 8)*(a.shape[2] // 8)),) +\n",
" (8, 8, 8) +\n",
" a.shape[3:]),\n",
" dtype = odtype)\n",
" bk, bj, bi = np.mgrid[0:a.shape[0]//8,\n",
" 0:a.shape[1]//8,\n",
" 0:a.shape[2]//8]\n",
" bindices = np.array([bi, bj, bk])\n",
" cindices = cm.grid3D_to_zindex(bindices)\n",
" plist = []\n",
" zlist = []\n",
" for k in range(a.shape[0]//8):\n",
" for j in range(a.shape[1]//8):\n",
" for i in range(a.shape[2]//8):\n",
" z = cm.grid3D_to_zindex(np.array([k, j, i]))\n",
" c[z] = a[8*k:8*(k+1), 8*j:8*(j+1), 8*i:8*(i+1)]\n",
" plist.append([i, j, k])\n",
" zlist.append(z)\n",
" plist = np.array(plist)\n",
" zlist = np.array(zlist)\n",
" i = np.argsort(zlist)\n",
" return c, zlist[i], plist[i]\n",
"\n",
"d0 = transform_py(Kdata0)\n",
"d1 = transform_py(Kdata1)\n",
"d2 = transform_py(Kdata2)\n",
"\n",
"Rdata_py_tmp = np.array([d0, d1, d2]).transpose((1, 2, 3, 0))\n",
"\n",
"Rdata_py, zlist, plist = array_to_8cubes(Rdata_py_tmp)\n",
"\n",
"# i0 = np.random.randint(16)\n",
"# i1 = np.random.randint(16)\n",
"# i2 = np.random.randint(16)\n",
"# z = cm.grid3D_to_zindex(np.array([i0, i1, i2]))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 15
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def compute_cpp_data(branch = None):\n",
" if not (type(branch) == type(None)):\n",
" subprocess.call(['git', 'checkout', branch])\n",
" if subprocess.call(['make', 'full.elf']) == 0:\n",
" subprocess.call([#'valgrind',\n",
" #'--tool=callgrind',\n",
" #'--callgrind-out-file=tmp.txt',\n",
" 'time',\n",
" 'mpirun.mpich',\n",
" '-np',\n",
" '32',\n",
" './full.elf',\n",
" '{0}'.format(n),\n",
" '{0}'.format(N),\n",
" '2',\n",
" '3'])\n",
" else:\n",
" print ('compilation error')\n",
" return None\n",
" \n",
"def get_cpp_data(branch = None, run = True):\n",
" if run:\n",
" subprocess.call(['rm',\n",
" 'Rdata_z{0:0>7x}'.format(0),\n",
" 'Rdata_z{0:0>7x}'.format(Rdata_py.shape[0]//2)])\n",
" compute_cpp_data(branch)\n",
" Rdata0 = np.fromfile(\n",
" 'Rdata_z{0:0>7x}'.format(0),\n",
" dtype = np.float32).reshape(-1, 8, 8, 8, 3)\n",
" Rdata1 = np.fromfile(\n",
" 'Rdata_z{0:0>7x}'.format(Rdata_py.shape[0]//2),\n",
" dtype = np.float32).reshape(-1, 8, 8, 8, 3)\n",
" return np.concatenate([Rdata0, Rdata1])\n",
"\n",
"#Rdata = get_cpp_data(branch = 'develop')\n",
"# develop says 30 secs, inplace fft is 28 secs\n",
"#Rdata = get_cpp_data(branch = 'feature-inplace_fft')\n",
"Rdata = get_cpp_data(run = True)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 16
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"distance = np.max(np.abs(Rdata_py - Rdata), axis = (1, 2, 3, 4))\n",
"if np.max(distance) > 1e-5:\n",
" ax = plt.figure(figsize=(6,2)).add_subplot(111)\n",
" ax.plot(distance)\n",
" i0 = np.random.randint(8)\n",
" i1 = np.random.randint(8)\n",
" i2 = np.random.randint(8)\n",
" z = cm.grid3D_to_zindex(np.array([i0, i1, i2]))\n",
" #z = 0\n",
" print(cm.zindex_to_grid3D(z))\n",
" s = np.max(np.abs(Rdata_py[None, z, :, :, :, 1] - Rdata[..., 1]),\n",
" axis = (1, 2, 3))\n",
" z1 = np.argmin(s)\n",
" print(z, z1, s[z1])\n",
" #print(Rdata[z1] - Rdata_py[z1])\n",
" ta0 = Rdata_py.ravel()\n",
" ta1 = Rdata.ravel()\n",
" print (Rdata_py[254:259, 7, 4, 3, 1])\n",
" print (Rdata[254:259, 7, 4, 3, 1])\n",
" print (ta0[ta0.shape[0]/2-1:ta0.shape[0]/2+7])\n",
" print (ta1[ta1.shape[0]/2-1:ta1.shape[0]/2+7])\n",
"else:\n",
" print('distance is small')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"distance is small\n"
]
}
],
"prompt_number": 17
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment