From 4ffa5e4a66fe3e410ced5c7da25706929bce84b9 Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Sat, 20 May 2017 21:30:14 -0700 Subject: [PATCH 1/8] use logging package for output, and remove 'verbose' options --- opencl_fdfd/csr.py | 38 +++++++++++++++++++------------------- opencl_fdfd/main.py | 31 +++++++++++++++++-------------- opencl_fdfd/ops.py | 8 ++++++++ 3 files changed, 44 insertions(+), 33 deletions(-) diff --git a/opencl_fdfd/csr.py b/opencl_fdfd/csr.py index 3ecbac9..9761fc0 100644 --- a/opencl_fdfd/csr.py +++ b/opencl_fdfd/csr.py @@ -16,6 +16,7 @@ satisfy the constraints for the 'conjugate gradient' algorithm from typing import Dict, Any import time +import logging import numpy from numpy.linalg import norm @@ -27,6 +28,11 @@ import fdfd_tools.solvers from . import ops +__author__ = 'Jan Petykiewicz' + +logger = logging.getLogger(__name__) + + class CSRMatrix(object): """ Matrix stored in Compressed Sparse Row format, in GPU RAM. @@ -49,7 +55,6 @@ def cg(A: 'scipy.sparse.csr_matrix', err_threshold: float = 1e-6, context: pyopencl.Context = None, queue: pyopencl.CommandQueue = None, - verbose: bool = False, ) -> numpy.ndarray: """ General conjugate-gradient solver for sparse matrices, where A @ x = b. @@ -60,7 +65,6 @@ def cg(A: 'scipy.sparse.csr_matrix', :param err_threshold: Error threshold for successful solve, relative to norm(b) :param context: PyOpenCL context. Will be created if not given. :param queue: PyOpenCL command queue. Will be created if not given. - :param verbose: Whether to print statistics to screen. :return: Solution vector x; returned even if solve doesn't converge. """ @@ -102,13 +106,11 @@ def cg(A: 'scipy.sparse.csr_matrix', _, err2 = rhoerr_step(r, []) b_norm = numpy.sqrt(err2) - if verbose: - print('b_norm check: ', b_norm) + logging.debug('b_norm check: ', b_norm) success = False for k in range(max_iters): - if verbose: - print('[{:06d}] rho {:.4} alpha {:4.4}'.format(k, rho, alpha), end=' ') + logging.debug('[{:06d}] rho {:.4} alpha {:4.4}'.format(k, rho, alpha)) rho_prev = rho e = xr_step(x, p, r, v, alpha, []) @@ -116,8 +118,7 @@ def cg(A: 'scipy.sparse.csr_matrix', errs += [numpy.sqrt(err2) / b_norm] - if verbose: - print('err', errs[-1]) + logging.debug('err {}'.format(errs[-1])) if errs[-1] < err_threshold: success = True @@ -128,7 +129,7 @@ def cg(A: 'scipy.sparse.csr_matrix', alpha = rho / dot(p, v, e) if verbose and k % 1000 == 0: - print(k) + logging.info('iteration {}'.format(k)) ''' Done solving @@ -137,17 +138,16 @@ def cg(A: 'scipy.sparse.csr_matrix', x = x.get() - if verbose: - if success: - print('Success', end='') - else: - print('Failure', end=', ') - print(', {} iterations in {} sec: {} iterations/sec \ - '.format(k, time_elapsed, k / time_elapsed)) - print('final error', errs[-1]) - print('overhead {} sec'.format(start_time2 - start_time)) + if success: + logging.info('Solve success') + else: + logging.warning('Solve failure') + logging.info('{} iterations in {} sec: {} iterations/sec \ + '.format(k, time_elapsed, k / time_elapsed)) + logging.debug('final error {}'.format(errs[-1])) + logging.debug('overhead {} sec'.format(start_time2 - start_time)) - print('Final residual:', norm(A @ x - b) / norm(b)) + logging.info('Final residual: {}'.format(norm(A @ x - b) / norm(b))) return x diff --git a/opencl_fdfd/main.py b/opencl_fdfd/main.py index 3b79aa9..a48aec0 100644 --- a/opencl_fdfd/main.py +++ b/opencl_fdfd/main.py @@ -8,6 +8,7 @@ a matrix). from typing import List import time +import logging import numpy from numpy.linalg import norm @@ -18,8 +19,11 @@ import fdfd_tools.operators from . import ops + __author__ = 'Jan Petykiewicz' +logger = logging.getLogger(__name__) + def cg_solver(omega: complex, dxes: List[List[numpy.ndarray]], @@ -32,7 +36,6 @@ def cg_solver(omega: complex, max_iters: int = 40000, err_threshold: float = 1e-6, context: pyopencl.Context = None, - verbose: bool = False, ) -> numpy.ndarray: """ OpenCL FDFD solver using the iterative conjugate gradient (cg) method @@ -57,7 +60,6 @@ def cg_solver(omega: complex, :param err_threshold: If (r @ r.conj()) / norm(1j * omega * J) < err_threshold, success. Default 1e-6. :param context: PyOpenCL context to run in. If not given, construct a new context. - :param verbose: If True, print progress to stdout. Default False. :return: E-field which solves the system. Returned even if we did not converge. """ @@ -171,12 +173,13 @@ def cg_solver(omega: complex, _, err2 = rhoerr_step(r, []) b_norm = numpy.sqrt(err2) - print('b_norm check: ', b_norm) + logging.debug('b_norm check: {}'.format(b_norm)) success = False for k in range(max_iters): - if verbose: - print('[{:06d}] rho {:.4} alpha {:4.4}'.format(k, rho, alpha), end=' ') + do_print = (k % 100 == 0) + if do_print: + logger.debug('[{:06d}] rho {:.4} alpha {:4.4}'.format(k, rho, alpha)) rho_prev = rho e = xr_step(x, p, r, v, alpha, []) @@ -184,8 +187,8 @@ def cg_solver(omega: complex, errs += [numpy.sqrt(err2) / b_norm] - if verbose: - print('err', errs[-1]) + if do_print: + logger.debug('err {}'.format(errs[-1])) if errs[-1] < err_threshold: success = True @@ -196,7 +199,7 @@ def cg_solver(omega: complex, alpha = rho / dot(p, v, e) if k % 1000 == 0: - print(k) + logger.info('iteration {}'.format(k)) ''' Done solving @@ -210,18 +213,18 @@ def cg_solver(omega: complex, x = (Pr * x).get() if success: - print('Success', end='') + logger.info('Solve success') else: - print('Failure', end=', ') - print(', {} iterations in {} sec: {} iterations/sec \ + logger.warning('Solve failure') + logger.info('{} iterations in {} sec: {} iterations/sec \ '.format(k, time_elapsed, k / time_elapsed)) - print('final error', errs[-1]) - print('overhead {} sec'.format(start_time2 - start_time)) + logger.debug('final error {}'.format(errs[-1])) + logger.debug('overhead {} sec'.format(start_time2 - start_time)) A0 = fdfd_tools.operators.e_full(omega, dxes, epsilon, mu).tocsr() if adjoint: # Remember we conjugated all the contents of A earlier A0 = A0.T - print('Post-everything residual:', norm(A0 @ x - b) / norm(b)) + logger.info('Post-everything residual: {}'.format(norm(A0 @ x - b) / norm(b))) return x diff --git a/opencl_fdfd/ops.py b/opencl_fdfd/ops.py index 4f81420..2e0f160 100644 --- a/opencl_fdfd/ops.py +++ b/opencl_fdfd/ops.py @@ -8,6 +8,7 @@ See kernels/ for any of the .cl files loaded in this file. """ from typing import List, Callable +import logging import numpy import jinja2 @@ -18,6 +19,8 @@ from pyopencl.elementwise import ElementwiseKernel from pyopencl.reduction import ReductionKernel +logger = logging.getLogger(__name__) + # Create jinja2 env on module load jinja_env = jinja2.Environment(loader=jinja2.PackageLoader(__name__, 'kernels')) @@ -145,6 +148,11 @@ def create_a(context: pyopencl.Context, e2 = H2E_kernel(E, H, oeps, Pl, pec, *idxes[1], wait_for=[e2]) return [e2] + logger.debug('Preamble: \n{}'.format(preamble)) + logger.debug('p2e: \n{}'.format(p2e_source)) + logger.debug('e2h: \n{}'.format(e2h_source)) + logger.debug('h2e: \n{}'.format(h2e_source)) + return spmv From 1d5e7ff3bbf8f4feeed05f74b2fc570aa78c5e18 Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Sat, 20 May 2017 21:30:32 -0700 Subject: [PATCH 2/8] minor cleanup of generated source --- opencl_fdfd/kernels/e2h.cl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/opencl_fdfd/kernels/e2h.cl b/opencl_fdfd/kernels/e2h.cl index c837e64..5716262 100644 --- a/opencl_fdfd/kernels/e2h.cl +++ b/opencl_fdfd/kernels/e2h.cl @@ -31,7 +31,7 @@ __global char *pmc_z = pmc + ZZ; //Update H components; set them to 0 if PMC is enabled at that location. -//Mu division and PMC conditional are only included if {{mu}} and {{pmc}} are true +//Mu division and PMC conditional are only included if {mu} and {pmc} are true {% if pmc -%} if (pmc_x[i] != 0) { Hx[i] = zero; @@ -42,9 +42,9 @@ if (pmc_x[i] != 0) { ctype Dyz = mul(sub(Ey[i + pz], Ey[i]), inv_dez[z]); ctype x_curl = sub(Dzy, Dyz); - {%- if mu -%} + {%- if mu %} Hx[i] = mul(inv_mu_x[i], x_curl); - {%- else -%} + {%- else %} Hx[i] = x_curl; {%- endif %} } @@ -59,9 +59,9 @@ if (pmc_y[i] != 0) { ctype Dzx = mul(sub(Ez[i + px], Ez[i]), inv_dex[x]); ctype y_curl = sub(Dxz, Dzx); - {%- if mu -%} + {%- if mu %} Hy[i] = mul(inv_mu_y[i], y_curl); - {%- else -%} + {%- else %} Hy[i] = y_curl; {%- endif %} } @@ -76,9 +76,9 @@ if (pmc_z[i] != 0) { ctype Dxy = mul(sub(Ex[i + py], Ex[i]), inv_dey[y]); ctype z_curl = sub(Dyx, Dxy); - {%- if mu -%} + {%- if mu %} Hz[i] = mul(inv_mu_z[i], z_curl); - {%- else -%} + {%- else %} Hz[i] = z_curl; {%- endif %} } From 235e0b6365adfb67e8eef4460ce348dda8054333 Mon Sep 17 00:00:00 2001 From: jan Date: Thu, 7 Sep 2017 22:01:38 -0700 Subject: [PATCH 3/8] fixup package info --- setup.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 0661610..a63d274 100644 --- a/setup.py +++ b/setup.py @@ -4,11 +4,14 @@ from setuptools import setup, find_packages setup(name='opencl_fdfd', version='0.3', - description='Opencl FDFD solver', + description='OpenCL FDFD solver', author='Jan Petykiewicz', author_email='anewusername@gmail.com', url='https://mpxd.net/gogs/jan/opencl_fdfd', packages=find_packages(), + package_data={ + 'opencl_fdfd': ['kernels/*'] + }, install_requires=[ 'numpy', 'pyopencl', From c96d36750223a16b282ce4ae8a9d5efa9d94a609 Mon Sep 17 00:00:00 2001 From: jan Date: Thu, 7 Sep 2017 22:01:38 -0700 Subject: [PATCH 4/8] fixup package info --- setup.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 0661610..a63d274 100644 --- a/setup.py +++ b/setup.py @@ -4,11 +4,14 @@ from setuptools import setup, find_packages setup(name='opencl_fdfd', version='0.3', - description='Opencl FDFD solver', + description='OpenCL FDFD solver', author='Jan Petykiewicz', author_email='anewusername@gmail.com', url='https://mpxd.net/gogs/jan/opencl_fdfd', packages=find_packages(), + package_data={ + 'opencl_fdfd': ['kernels/*'] + }, install_requires=[ 'numpy', 'pyopencl', From b62cd6e8676c0a7cb3d09c55e59578bc65d54c2c Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Mon, 15 Jan 2018 22:36:13 -0800 Subject: [PATCH 5/8] move code to new location --- README.md | 4 ++-- opencl_fdfd/__init__.py | 2 +- setup.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e714fbf..896001e 100644 --- a/README.md +++ b/README.md @@ -38,12 +38,12 @@ generalization to multiple GPUs should be pretty straightforward * numpy * pyopencl * jinja2 -* [fdfd_tools](https://mpxd.net/gogs/jan/fdfd_tools) (>=0.2) +* [fdfd_tools](https://mpxd.net/code/jan/fdfd_tools) (>=0.2) Install with pip, via git: ```bash -pip install git+https://mpxd.net/gogs/jan/opencl_fdfd.git@release +pip install git+https://mpxd.net/code/jan/opencl_fdfd.git@release ``` diff --git a/opencl_fdfd/__init__.py b/opencl_fdfd/__init__.py index 908f567..f68a85c 100644 --- a/opencl_fdfd/__init__.py +++ b/opencl_fdfd/__init__.py @@ -31,7 +31,7 @@ Dependencies: - - fdfd_tools ( https://mpxd.net/gogs/jan/fdfd_tools ) + - fdfd_tools ( https://mpxd.net/code/jan/fdfd_tools ) - numpy - pyopencl - jinja2 diff --git a/setup.py b/setup.py index a63d274..ec49bd3 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ setup(name='opencl_fdfd', description='OpenCL FDFD solver', author='Jan Petykiewicz', author_email='anewusername@gmail.com', - url='https://mpxd.net/gogs/jan/opencl_fdfd', + url='https://mpxd.net/code/jan/opencl_fdfd', packages=find_packages(), package_data={ 'opencl_fdfd': ['kernels/*'] From 8360f98395351cbdc0c3cb7d61e17c742b8cc974 Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Sun, 16 Sep 2018 20:14:31 -0700 Subject: [PATCH 6/8] Move version string into module --- opencl_fdfd/__init__.py | 1 + setup.py | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/opencl_fdfd/__init__.py b/opencl_fdfd/__init__.py index f68a85c..a1d54c6 100644 --- a/opencl_fdfd/__init__.py +++ b/opencl_fdfd/__init__.py @@ -41,3 +41,4 @@ from .main import cg_solver __author__ = 'Jan Petykiewicz' +version = '0.3' diff --git a/setup.py b/setup.py index ec49bd3..c3792b8 100644 --- a/setup.py +++ b/setup.py @@ -1,9 +1,10 @@ #!/usr/bin/env python from setuptools import setup, find_packages +import opencl_fdfd setup(name='opencl_fdfd', - version='0.3', + version=opencl_fdfd.version, description='OpenCL FDFD solver', author='Jan Petykiewicz', author_email='anewusername@gmail.com', From 3cd26265bd7958d0ed6bf728462b8a336c64cb9e Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Sun, 16 Sep 2018 20:14:42 -0700 Subject: [PATCH 7/8] Use readme as long_description --- setup.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/setup.py b/setup.py index c3792b8..1801423 100644 --- a/setup.py +++ b/setup.py @@ -3,9 +3,14 @@ from setuptools import setup, find_packages import opencl_fdfd +with open('README.md', 'r') as f: + long_description = f.read() + setup(name='opencl_fdfd', version=opencl_fdfd.version, description='OpenCL FDFD solver', + long_description=long_description, + long_description_content_type='text/markdown', author='Jan Petykiewicz', author_email='anewusername@gmail.com', url='https://mpxd.net/code/jan/opencl_fdfd', From 4b798893bceb0dbf1a97ecda5ff30a7c549bb1d6 Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Sun, 16 Sep 2018 20:15:11 -0700 Subject: [PATCH 8/8] Use python3 for setup --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 1801423..1cbf8f0 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 from setuptools import setup, find_packages import opencl_fdfd