from __future__ import division, print_function __copyright__ = "Copyright (C) 2015 James Stevens" __license__ = """ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ import sys from pyopencl.tools import ( # noqa pytest_generate_tests_for_pyopencl as pytest_generate_tests) import loopy as lp from loopy.statistics import get_op_poly, get_gmem_access_poly, get_barrier_poly from loopy.statistics import get_op_poly2, get_regs_per_thread import numpy as np def test_op_counter_basic(): knl = lp.make_kernel( "[n,m,l] -> {[i,k,j]: 0<=i6 or k/2==l, g[i,k]*2, g[i,k]+h[i,k]/2) """ ], name="logic", assumptions="n,m,l >= 1") knl = lp.add_and_infer_dtypes(knl, dict(g=np.float32, h=np.float64)) poly = get_op_poly(knl) n = 512 m = 256 l = 128 params = {'n': n, 'm': m, 'l': l} f32 = poly[np.dtype(np.float32)].eval_with_dict(params) f64 = poly[np.dtype(np.float64)].eval_with_dict(params) i32 = poly[np.dtype(np.int32)].eval_with_dict(params) assert f32 == n*m assert f64 == 3*n*m assert i32 == n*m def test_op_counter_specialops(): knl = lp.make_kernel( "{[i,k,j]: 0<=i> k)) """ ], name="bitwise", assumptions="n,m,l >= 1") knl = lp.add_and_infer_dtypes( knl, dict( a=np.int32, b=np.int32, g=np.int64, h=np.int64)) poly = get_op_poly(knl) n = 512 m = 256 l = 128 params = {'n': n, 'm': m, 'l': l} i32 = poly[np.dtype(np.int32)].eval_with_dict(params) i64 = poly[np.dtype(np.int64)].eval_with_dict(params) # noqa assert np.dtype(np.float64) not in poly assert i32 == n*m+3*n*m*l assert i64 == 6*n*m def test_op_counter_triangular_domain(): knl = lp.make_kernel( "{[i,j]: 0<=i {[i,k,j]: 0<=i6 or k/2==l, g[i,k]*2, g[i,k]+h[i,k]/2) """ ], name="logic", assumptions="n,m,l >= 1") knl = lp.add_and_infer_dtypes(knl, dict(g=np.float32, h=np.float64)) poly = get_op_poly2(knl) n = 512 m = 256 l = 128 params = {'n': n, 'm': m, 'l': l} f32mul = poly[(np.dtype(np.float32), 'mul')].eval_with_dict(params) f64add = poly[(np.dtype(np.float64), 'add')].eval_with_dict(params) f64div = poly[(np.dtype(np.float64), 'div')].eval_with_dict(params) i32add = poly[(np.dtype(np.int32), 'add')].eval_with_dict(params) #f32 = poly[np.dtype(np.float32)].eval_with_dict(params) #f64 = poly[np.dtype(np.float64)].eval_with_dict(params) #i32 = poly[np.dtype(np.int32)].eval_with_dict(params) assert f32mul == n*m #assert f64 == 3*n*m assert f64div == 2*n*m #TODO why? assert f64add == n*m assert i32add == n*m def test_op_counter2_specialops(): knl = lp.make_kernel( "{[i,k,j]: 0<=i> k)) """ ], name="bitwise", assumptions="n,m,l >= 1") knl = lp.add_and_infer_dtypes( knl, dict( a=np.int32, b=np.int32, g=np.int64, h=np.int64)) poly = get_op_poly2(knl) n = 512 m = 256 l = 128 params = {'n': n, 'm': m, 'l': l} i32add = poly[(np.dtype(np.int32), 'add')].eval_with_dict(params) i32bw = poly[(np.dtype(np.int32), 'bw')].eval_with_dict(params) i64bw = poly[(np.dtype(np.int64), 'bw')].eval_with_dict(params) i64mul = poly[(np.dtype(np.int64), 'mul')].eval_with_dict(params) i64add = poly[(np.dtype(np.int64), 'add')].eval_with_dict(params) i64shift = poly[(np.dtype(np.int64), 'shift')].eval_with_dict(params) assert i32add == n*m+n*m*l assert i32bw == 2*n*m*l assert i64bw == 2*n*m assert i64add == i64mul == n*m assert i64shift == 2*n*m def test_gmem_access_counter_basic(): knl = lp.make_kernel( "[n,m,l] -> {[i,k,j]: 0<=i6 or k/2==l, g[i,k]*2, g[i,k]+h[i,k]/2) """ ], name="logic", assumptions="n,m,l >= 1") knl = lp.add_and_infer_dtypes(knl, dict(g=np.float32, h=np.float64)) poly = get_gmem_access_poly(knl) n = 512 m = 256 l = 128 params = {'n': n, 'm': m, 'l': l} f32 = poly[ (np.dtype(np.float32), 'uniform', 'load') ].eval_with_dict(params) f64 = poly[ (np.dtype(np.float64), 'uniform', 'load') ].eval_with_dict(params) assert f32 == 2*n*m assert f64 == n*m f64 = poly[ (np.dtype(np.float64), 'uniform', 'store') ].eval_with_dict(params) assert f64 == n*m def test_gmem_access_counter_specialops(): knl = lp.make_kernel( "{[i,k,j]: 0<=i> k)) """ ], name="bitwise", assumptions="n,m,l >= 1") knl = lp.add_and_infer_dtypes( knl, dict( a=np.int32, b=np.int32, g=np.int32, h=np.int32)) poly = get_gmem_access_poly(knl) n = 512 m = 256 l = 128 params = {'n': n, 'm': m, 'l': l} i32 = poly[ (np.dtype(np.int32), 'uniform', 'load') ].eval_with_dict(params) assert i32 == 4*n*m+2*n*m*l i32 = poly[ (np.dtype(np.int32), 'uniform', 'store') ].eval_with_dict(params) assert i32 == n*m+n*m*l def test_gmem_access_counter_mixed(): knl = lp.make_kernel( "[n,m,l] -> {[i,k,j]: 0<=i {[i,k,j]: 0<=i {[i,k,j]: 0<=i {[i,k,j]: 0<=i {[i,k,j]: 0<=i<50 and 1<=k<98 and 0<=j<10}", [ """ c[i,j,k] = 2*a[i,j,k] {id=first} e[i,j,k] = c[i,j,k+1]+c[i,j,k-1] {dep=first} """ ], [ lp.TemporaryVariable("c", lp.auto, shape=(50, 10, 99)), "..." ], name="weird2", ) knl = lp.add_and_infer_dtypes(knl, dict(a=np.int32)) knl = lp.split_iname(knl, "k", 128, outer_tag="g.0", inner_tag="l.0") poly = get_barrier_poly(knl) n = 512 m = 256 l = 128 params = {'n': n, 'm': m, 'l': l} barrier_count = poly.eval_with_dict(params) assert barrier_count == 50*10*2 def test_reg_counter_basic(): knl = lp.make_kernel( "[n,m,l] -> {[i,k,j]: 0<=i6 or k/2==l, g[i,k]*2, g[i,k]+h[i,k]/2) """ ], name="logic", assumptions="n,m,l >= 1") knl = lp.add_and_infer_dtypes(knl, dict(g=np.float32, h=np.float64)) regs = get_regs_per_thread(knl) assert regs == 6 def test_reg_counter_specialops(): knl = lp.make_kernel( "{[i,k,j]: 0<=i> k)) """ ], name="bitwise", assumptions="n,m,l >= 1") knl = lp.add_and_infer_dtypes( knl, dict( a=np.int32, b=np.int32, g=np.int64, h=np.int64)) regs = get_regs_per_thread(knl) assert regs == 6 def test_all_counters_parallel_matmul(): knl = lp.make_kernel( "{[i,k,j]: 0<=i 1: exec(sys.argv[1]) else: from py.test.cmdline import main main([__file__])