def compute(self, input, output):

        cupy_ref_input = cupy_ref.Cupy_Ref(ptr=input.data.ptr,
                                           shape=input.shape,
                                           dtype=input.dtype,
                                           typestr=input.dtype.str)
        cupy_ref_output = cupy_ref.Cupy_Ref(ptr=output.data.ptr,
                                            shape=output.shape,
                                            dtype=output.dtype,
                                            typestr=output.dtype.str)
        self.algocuda.compute(cupy_ref_input, cupy_ref_output)
    def compute(self, input=None, output=None):

        if (input is None and output is None):
            self.algocuda.compute(self.algocuda.get_input_memory(),
                                  self.algocuda.get_output_memory())
        else:
            cupy_ref_input = cupy_ref.Cupy_Ref(ptr=input.data.ptr,
                                               shape=input.shape,
                                               dtype=input.dtype,
                                               typestr=input.dtype.str)
            cupy_ref_output = cupy_ref.Cupy_Ref(ptr=output.data.ptr,
                                                shape=output.shape,
                                                dtype=output.dtype,
                                                typestr=output.dtype.str)
            self.algocuda.compute(cupy_ref_input, cupy_ref_output)
def test_return_as_cupy_not_copy():
    a = cp.array([1.1, 3.14, 42.69])
    b = cupy_ref.Cupy_Ref(ptr = a.data.ptr, shape = a.shape, dtype = a.dtype, typestr = a.dtype.str)

    c = cp.array(b, dtype=b.dtype, copy=False) # we dont use copy to get the original "a"
     
    assert(cp.array_equal(a,c))
    assert(a.data.ptr == c.data.ptr) # make sure it is not copied
def test_return_as_cupy_copy():
    a = cp.array([1.1, 3.14, 42.69])
    b = cupy_ref.Cupy_Ref(ptr = a.data.ptr, shape = a.shape, dtype = a.dtype, typestr = a.dtype.str)

    c = cp.array(b, dtype=b.dtype, copy=True) # we use copy to create a new cupy object with the same value of "a"
    
    assert(cp.array_equal(a,c))
    assert(a.data.ptr != c.data.ptr) # make sure it is copied
def test_return_as_cupy_using_cupy_ref_class():
    a = cp.array([1.1, 3.14, 42.69])
    b = cupy_ref.Cupy_Ref(ptr = a.data.ptr, shape = a.shape, dtype = a.dtype, typestr = a.dtype.str)

    c = b.get_cupy_array()
     
    assert(cp.array_equal(a,c))
    assert(a.data.ptr == c.data.ptr) # make sure it is not copied
def test_create_a_custom_cupy_with_flexible_dimension():
    a = cp.ones((2, 2, 2), dtype=cp.complex128)
    b = cupy_ref.Cupy_Ref(ptr=a.data.ptr,
                          shape=a.shape,
                          dtype=a.dtype,
                          typestr=a.dtype.str)
    c = Test_Cupy.test_create_a_custom_cupy_with_flexible_dimension(b)

    assert (c == 3)

    a = cp.ones((3, 3, 3, 3), dtype=cp.complex128)
    b = cupy_ref.Cupy_Ref(ptr=a.data.ptr,
                          shape=a.shape,
                          dtype=a.dtype,
                          typestr=a.dtype.str)
    c = Test_Cupy.test_create_a_custom_cupy_with_flexible_dimension(b)

    assert (c == 4)
def test_create_a_custom_cupy_with_fixed_dimension_success():
    a = cp.ones((2, 2, 2), dtype=cp.complex128)
    b = cupy_ref.Cupy_Ref(ptr=a.data.ptr,
                          shape=a.shape,
                          dtype=a.dtype,
                          typestr=a.dtype.str)
    c = Test_Cupy.test_create_a_custom_cupy_with_fixed_dimension_success(b)

    assert (c == 3)
def test_create_a_custom_cupy_with_fixed_dimension_fail():
    with pytest.raises(Exception):
        a = cp.ones((2, 2, 2), dtype=cp.complex128)
        b = cupy_ref.Cupy_Ref(ptr=a.data.ptr,
                              shape=a.shape,
                              dtype=a.dtype,
                              typestr=a.dtype.str)
        c = Test_Cupy.test_create_a_custom_cupy_with_fixed_dimension_fail(b)

        assert (c == 3)
def test_send_cupy_caster_to_c_and_get_it_back():
    a = cp.array([[3.14, 4.25, 5.36], [4, 5, 6], [1.23, 4.56, 7.89]],
                 dtype=cp.complex128)
    b = cupy_ref.Cupy_Ref(ptr=a.data.ptr,
                          shape=a.shape,
                          dtype=a.dtype,
                          typestr=a.dtype.str)
    c = Test_Cupy.test_send_cupy_caster_to_c_and_get_it_back(b)

    assert (a.data.ptr == c.ptr and a.dtype == c.dtype and a.shape == c.shape)
def test_custom_cupy_template_function():
    cp.cuda.Device(0).use()
    a = cp.array([3, 4, 5], dtype=cp.complex128)
    b = cupy_ref.Cupy_Ref(ptr=a.data.ptr,
                          shape=a.shape,
                          dtype=a.dtype,
                          typestr=a.dtype.str)
    c = Test_Custom_Cupy.test_custom_cupy_template_function(a.data.ptr, b)

    assert (c == True)
def test_copy_custom_cupy_to_cpu():
    cp.cuda.Device(0).use()
    a = cp.array([3.14, 4.25, 5.36], dtype=cp.float64)
    b = cupy_ref.Cupy_Ref(ptr=a.data.ptr,
                          shape=a.shape,
                          dtype=a.dtype,
                          typestr=a.dtype.str)
    c = Test_Custom_Cupy.test_copy_custom_cupy_to_cpu(b)

    assert (c == True)
def test_if_reinterpret_ptr_is_the_same():
    cp.cuda.Device(0).use()
    a = cp.array([3, 4, 5], dtype=cp.float64)
    b = cupy_ref.Cupy_Ref(ptr=a.data.ptr,
                          shape=a.shape,
                          dtype=a.dtype,
                          typestr=a.dtype.str)
    c = Test_Custom_Cupy.test_if_reinterpret_ptr_is_the_same(a.data.ptr, b)

    assert (c == True)
def test_copy_custom_cupy_to_custom_cupy():

    a = cp.array([3.14, 4.25, 5.36], dtype=cp.float64)
    b = cupy_ref.Cupy_Ref(ptr=a.data.ptr,
                          shape=a.shape,
                          dtype=a.dtype,
                          typestr=a.dtype.str)
    c = Test_Custom_Cupy.test_copy_custom_cupy_to_custom_cupy(b)

    assert (b.ptr == c.ptr and b.dtype == c.dtype)
def test_if_custom_cupy_reinterpret_ptr_is_a_gpu_array():
    cp.cuda.Device(0).use()
    a = cp.array([3.14, 4.25, 5.36], dtype=cp.float64)
    b = cupy_ref.Cupy_Ref(ptr=a.data.ptr,
                          shape=a.shape,
                          dtype=a.dtype,
                          typestr=a.dtype.str)
    res = Test_Custom_Cupy.test_if_custom_cupy_reinterpret_ptr_is_a_gpu_array(
        b)

    assert (res == True)
def test_wrong_dtype_complex():
    with pytest.raises(TypeError):
        a = cp.array(
            [2. + 3.j, 0. + 0.j, 4. + 1.j], dtype=cp.complex64
        )  #this cupy is set to float32, but the c++ function is float64
        b = cupy_ref.Cupy_Ref(ptr=a.data.ptr,
                              shape=a.shape,
                              dtype=a.dtype,
                              typestr=a.dtype.str)
        Test_Custom_Cupy.test_wrong_dtype_complex(
            b)  #this should raise an exception
def test_wrong_dtype_int():
    with pytest.raises(TypeError):
        a = cp.array(
            [3, 4, 5], dtype=cp.uint32
        )  #this cupy is set to uint32, but the c++ function is uint16
        b = cupy_ref.Cupy_Ref(ptr=a.data.ptr,
                              shape=a.shape,
                              dtype=a.dtype,
                              typestr=a.dtype.str)
        Test_Custom_Cupy.test_wrong_dtype_int(
            b)  #this should raise an exception
def test_wrong_dtype_float():
    with pytest.raises(TypeError):
        a = cp.array(
            [3.14, 4.25, 5.36], dtype=cp.float32
        )  #this cupy is set to float32, but the c++ function is float64
        b = cupy_ref.Cupy_Ref(ptr=a.data.ptr,
                              shape=a.shape,
                              dtype=a.dtype,
                              typestr=a.dtype.str)
        Test_Custom_Cupy.test_wrong_dtype_float(
            b)  #this should raise an exception
def test_custom_cupy_pointer_with_cuda_kernel():
    a = cp.array([3.14, 4.25, 5.36], dtype=cp.float64)
    b = cupy_ref.Cupy_Ref(ptr=a.data.ptr,
                          shape=a.shape,
                          dtype=a.dtype,
                          typestr=a.dtype.str)
    c = Test_Custom_Cupy.custom_cupy_increment_all_data_by_1(b)

    assert (c == True)

    # python side test
    # compare float with tolerance
    # By using "b", each element of "a" should be incremented by 1 at this point, same with test 8. But because of cuda kernel, there is a very very small value change,
    # so allclose is needed insteall of array_equal
    a_inc = cp.array([4.14, 5.25, 6.36], dtype=cp.float64)
    assert (cp.allclose(a, a_inc))
def test_cupy_cufft_inverse_forward_with_caster():
    a = cp.array([[3.14, 4.25, 5.36], [4, 5, 6], [1.23, 4.56, 7.89]],
                 dtype=cp.complex128)
    b = cupy_ref.Cupy_Ref(ptr=a.data.ptr,
                          shape=a.shape,
                          dtype=a.dtype,
                          typestr=a.dtype.str)
    c = Test_Cupy.test_cupy_cufft_inverse_forward_with_caster(b)

    print()
    print("Test 6")
    print(a)
    print(c)

    assert (
        cp.allclose(a, c)
    )  #array_uqual wont work because there is still a very very small difference