def test_pow(thr, out_code, in_codes): out_dtype, in_dtypes = generate_dtypes(out_code, in_codes) if len(in_dtypes) == 1: func = functions.pow(in_dtypes[0]) if dtypes.is_real(in_dtypes[0]): in_dtypes.append(in_dtypes[0]) else: in_dtypes.append(dtypes.real_for(in_dtypes[0])) else: func = functions.pow(in_dtypes[0], power_dtype=in_dtypes[1]) check_func(thr, func, numpy.power, out_dtype, in_dtypes)
def test_pow_zero_exponent(some_thr, out_code, in_codes): """ Regression test for the bug where pow(0, 0) returned 0. """ N = 256 out_dtype, in_dtypes = generate_dtypes(out_code, in_codes) func_module = functions.pow(in_dtypes[0], exponent_dtype=in_dtypes[1], output_dtype=out_dtype) test = get_func_kernel(some_thr, func_module, out_dtype, in_dtypes) bases = some_thr.to_device(numpy.zeros(N, in_dtypes[0])) exponents = some_thr.to_device(numpy.zeros(N, in_dtypes[1])) dest_dev = some_thr.array(N, out_dtype) test(dest_dev, bases, exponents, global_size=N) assert diff_is_negligible(dest_dev.get(), numpy.ones(N, in_dtypes[0]))
def get_procs(thr, N): fft = FFTFactory.create(thr, (N,), compile_=False) unimod_trans = Transformation( [Parameter('output', Annotation(Type(np.complex128, N), 'o')), Parameter('input', Annotation(Type(np.complex128, N), 'i'))], """ VSIZE_T idx = ${idxs[0]}; ${input.ctype} val = ${input.load_same}; if (idx>${N}/2){ val.x = 0.0; val.y = 0.0; ${output.store_same}(val); }else ${output.store_same}(${polar_unit}(atan2(val.y, val.x))); """, render_kwds=dict(polar_unit=functions.polar_unit(dtype=np.float64), N=N) ) fft.parameter.output.connect(unimod_trans, unimod_trans.input, uni=unimod_trans.output) fft_unimod = fft.compile(thr) mag_square = PureParallel( [Parameter('output', Annotation(Type(np.complex128, N), 'o')), Parameter('input', Annotation(Type(np.complex128, N), 'i'))], ''' VSIZE_T idx = ${idxs[0]}; ${input.ctype} val = ${input.load_idx}(idx); val.x = val.x*val.x + val.y*val.y; val.y = 0; ${output.store_idx}(idx, val); ''' ) mag_square = mag_square.compile(thr) apply_mask = PureParallel( [Parameter('output', Annotation(Type(np.complex128, N), 'o')), Parameter('origin', Annotation(Type(np.complex128, N), 'i')), Parameter('mask', Annotation(Type(np.double, N), 'i'))], ''' VSIZE_T idx = ${idxs[0]}; ${output.store_idx}(idx, ${mul}(${origin.load_idx}(idx), ${mask.load_idx}(idx))); ''', render_kwds=dict(mul=functions.mul(np.complex128, np.double)) ) apply_mask = apply_mask.compile(thr) combine_mag_phi = PureParallel( [Parameter('output', Annotation(Type(np.complex128, N), 'o')), Parameter('mag_square', Annotation(Type(np.complex128, N), 'i')), Parameter('phase', Annotation(Type(np.complex128, N), 'i'))], ''' VSIZE_T idx = ${idxs[0]}; double r = ${mag_square.load_idx}(idx).x; r = r<0.0 ? 0.0 : ${pow}(r, 0.5); double2 v = ${phase.load_idx}(idx); double angle = atan2(v.y, v.x); ${output.store_idx}(idx, ${polar}(r, angle)); ''', render_kwds=dict(pow=functions.pow(np.double), polar=functions.polar(np.double)) ) combine_mag_phi = combine_mag_phi.compile(thr) return fft_unimod, mag_square, apply_mask, combine_mag_phi
def test_pow(thr, out_code, in_codes): out_dtype, in_dtypes = generate_dtypes(out_code, in_codes) func = functions.pow(in_dtypes[0], exponent_dtype=in_dtypes[1], output_dtype=out_dtype) check_func(thr, func, numpy.power, out_dtype, in_dtypes)
def get_procs(thr, N): fft = FFTFactory.create(thr, (N, ), compile_=False) unimod_trans = Transformation( [ Parameter('output', Annotation(Type(np.complex128, N), 'o')), Parameter('input', Annotation(Type(np.complex128, N), 'i')) ], """ VSIZE_T idx = ${idxs[0]}; ${input.ctype} val = ${input.load_same}; if (idx>${N}/2){ val.x = 0.0; val.y = 0.0; ${output.store_same}(val); }else ${output.store_same}(${polar_unit}(atan2(val.y, val.x))); """, render_kwds=dict(polar_unit=functions.polar_unit(dtype=np.float64), N=N)) fft.parameter.output.connect(unimod_trans, unimod_trans.input, uni=unimod_trans.output) fft_unimod = fft.compile(thr) mag_square = PureParallel([ Parameter('output', Annotation(Type(np.complex128, N), 'o')), Parameter('input', Annotation(Type(np.complex128, N), 'i')) ], ''' VSIZE_T idx = ${idxs[0]}; ${input.ctype} val = ${input.load_idx}(idx); val.x = val.x*val.x + val.y*val.y; val.y = 0; ${output.store_idx}(idx, val); ''') mag_square = mag_square.compile(thr) apply_mask = PureParallel( [ Parameter('output', Annotation(Type(np.complex128, N), 'o')), Parameter('origin', Annotation(Type(np.complex128, N), 'i')), Parameter('mask', Annotation(Type(np.double, N), 'i')) ], ''' VSIZE_T idx = ${idxs[0]}; ${output.store_idx}(idx, ${mul}(${origin.load_idx}(idx), ${mask.load_idx}(idx))); ''', render_kwds=dict(mul=functions.mul(np.complex128, np.double))) apply_mask = apply_mask.compile(thr) combine_mag_phi = PureParallel([ Parameter('output', Annotation(Type(np.complex128, N), 'o')), Parameter('mag_square', Annotation(Type(np.complex128, N), 'i')), Parameter('phase', Annotation(Type(np.complex128, N), 'i')) ], ''' VSIZE_T idx = ${idxs[0]}; double r = ${mag_square.load_idx}(idx).x; r = r<0.0 ? 0.0 : ${pow}(r, 0.5); double2 v = ${phase.load_idx}(idx); double angle = atan2(v.y, v.x); ${output.store_idx}(idx, ${polar}(r, angle)); ''', render_kwds=dict( pow=functions.pow(np.double), polar=functions.polar(np.double))) combine_mag_phi = combine_mag_phi.compile(thr) return fft_unimod, mag_square, apply_mask, combine_mag_phi