Example #1
0
def test_calc_nc():
    n_pts = 16

    for i in range(100):

        # OpenCL
        u = numpy.random.uniform(low=1.0, high=10.0,
                                 size=(n_pts,
                                       n_pts)).astype(dtype=numpy.float32)
        otf_mask_shift = pyRef.createOTFMask()

        nc = numpy.zeros(1, dtype=numpy.float32)

        u_buffer = cl.Buffer(context,
                             cl.mem_flags.READ_ONLY
                             | cl.mem_flags.COPY_HOST_PTR,
                             hostbuf=u)
        otf_mask_buffer = cl.Buffer(context,
                                    cl.mem_flags.READ_ONLY
                                    | cl.mem_flags.COPY_HOST_PTR,
                                    hostbuf=otf_mask_shift)
        nc_buffer = cl.Buffer(context,
                              cl.mem_flags.WRITE_ONLY
                              | cl.mem_flags.COPY_HOST_PTR,
                              hostbuf=nc)

        program.calc_nc_test(queue, (1, ), (1, ), u_buffer, otf_mask_buffer,
                             nc_buffer)
        cl.enqueue_copy(queue, nc, nc_buffer).wait()
        queue.finish()

        # Reference 1
        otf_mask = numpy.fft.fftshift(otf_mask_shift.reshape(16, 16))
        ncs_sr = ncsC.NCSCSubRegion(r_size=n_pts)
        ncs_sr.setOTFMask(otf_mask)
        ncs_sr.setU(u)
        ref1_nc = ncs_sr.calcNoiseContribution()
        ncs_sr.cleanup()

        norm_diff = abs(nc[0] - ref1_nc) / abs(ref1_nc)
        assert (norm_diff <
                1.0e-3), "Difference in results! {0:.6f}".format(norm_diff)

        # Reference 2
        u_r = numpy.copy(u).flatten()
        u_c = numpy.zeros_like(u_r)
        u_fft_r = numpy.zeros_like(u_r)
        u_fft_c = numpy.zeros_like(u_c)
        otf_mask_sqr = (otf_mask_shift * otf_mask_shift).flatten()

        pyRef.fft_16x16(u_r, u_c, u_fft_r, u_fft_c)
        ref2_nc = pyRef.calcNoiseContribution(u_fft_r, u_fft_c, otf_mask_sqr)

        norm_diff = abs(nc[0] - ref2_nc) / abs(ref2_nc)
        assert (norm_diff <
                1.0e-3), "Difference in results! {0:.6f}".format(norm_diff)
Example #2
0
def test_calc_nc_grad_1():
   n_pts = 16
   
   for i in range(10):
      
      # OpenCL gradient calculation.
      u = numpy.random.uniform(low = 1.0, high = 10.0, size = (n_pts, n_pts)).astype(dtype = numpy.float32)
      otf_mask_shift = pyRef.createOTFMask()
      grad = numpy.zeros((n_pts, n_pts)).astype(numpy.float32)
      
      u_buffer = cl.Buffer(context, cl.mem_flags.READ_ONLY | cl.mem_flags.COPY_HOST_PTR, hostbuf = u)
      otf_mask_buffer = cl.Buffer(context, cl.mem_flags.READ_ONLY | cl.mem_flags.COPY_HOST_PTR, hostbuf = otf_mask_shift)
      grad_buffer = cl.Buffer(context, cl.mem_flags.WRITE_ONLY | cl.mem_flags.COPY_HOST_PTR, hostbuf = grad)
      
      program.calc_nc_grad_test(queue, (16,), (16,),
                                u_buffer,
                                otf_mask_buffer,
                                grad_buffer) 

      cl.enqueue_copy(queue, grad, grad_buffer).wait()
      queue.finish()
      
      # Reference 1
      otf_mask = numpy.fft.fftshift(otf_mask_shift.reshape(16, 16))
      ncs_sr = ncsC.NCSCSubRegion(r_size = n_pts)
      ncs_sr.setOTFMask(otf_mask)
      ncs_sr.setU(u)
      ncs_sr.calcNoiseContribution()
      ref1_grad = ncs_sr.calcNCGradient().reshape(grad.shape)
      ncs_sr.cleanup()
      
      ref_norm = numpy.abs(ref1_grad)
      ref_norm[(ref_norm<1.0)] = 1.0

      max_diff = numpy.max(numpy.abs(grad - ref1_grad)/ref_norm)
      assert (max_diff < 1.0e-5), "Difference in results! {0:.8f}".format(max_diff)

      # Reference 2
      u_r = numpy.copy(u).flatten()
      u_c = numpy.zeros_like(u_r)
      u_fft_r = numpy.zeros_like(u_r)
      u_fft_c = numpy.zeros_like(u_r)
      ref2_grad = numpy.zeros_like(u_r)
      otf_mask_sqr = otf_mask_shift * otf_mask_shift
      
      pyRef.fft_16x16(u_r, u_c, u_fft_r, u_fft_c)
      pyRef.calcNCGradientIFFT(u_fft_r, u_fft_c, otf_mask_sqr, ref2_grad)

      ref_norm = numpy.abs(ref2_grad)
      ref_norm[(ref_norm<1.0)] = 1.0

      max_diff = numpy.max(numpy.abs(grad.flatten() - ref2_grad)/ref_norm)
      assert (max_diff < 1.0e-5), "Difference in results! {0:.8f}".format(max_diff)
Example #3
0
def test_py_ref_fft_16x16():
    x_r = numpy.random.uniform(size=256).astype(dtype=numpy.float32)
    x_c = numpy.random.uniform(size=256).astype(dtype=numpy.float32)

    y_r = numpy.zeros_like(x_r)
    y_c = numpy.zeros_like(x_c)

    y_r_c = numpy.zeros_like(x_r)
    y_c_c = numpy.zeros_like(x_c)

    x_r_buffer = cl.Buffer(context,
                           cl.mem_flags.READ_ONLY | cl.mem_flags.COPY_HOST_PTR,
                           hostbuf=x_r)
    x_c_buffer = cl.Buffer(context,
                           cl.mem_flags.READ_ONLY | cl.mem_flags.COPY_HOST_PTR,
                           hostbuf=x_c)

    y_r_buffer = cl.Buffer(context,
                           cl.mem_flags.WRITE_ONLY
                           | cl.mem_flags.COPY_HOST_PTR,
                           hostbuf=y_r)
    y_c_buffer = cl.Buffer(context,
                           cl.mem_flags.WRITE_ONLY
                           | cl.mem_flags.COPY_HOST_PTR,
                           hostbuf=y_c)

    program.fft_16x16_test(queue, (1, ), (1, ), x_r_buffer, x_c_buffer,
                           y_r_buffer, y_c_buffer)
    cl.enqueue_copy(queue, y_r, y_r_buffer).wait()
    cl.enqueue_copy(queue, y_c, y_c_buffer).wait()
    queue.finish()

    pyRef.fft_16x16(x_r, x_c, y_r_c, y_c_c)

    assert (numpy.allclose(y_r, y_r_c, atol=1.0e-5))
    assert (numpy.allclose(y_c, y_c_c, atol=1.0e-5))