예제 #1
0
def test_malloc():
    m = occa.malloc(10, dtype=np.int32)

    assert isinstance(m, occa.Memory)
    assert m.dtype == np.int32
    assert len(m) == 10

    m = occa.malloc(np.zeros(5, dtype=np.float32))

    assert isinstance(m, occa.Memory)
    assert m.dtype == np.float32
    assert len(m) == 5
예제 #2
0
 def m_ones(dtype):
     return occa.malloc(ones(dtype))
예제 #3
0
 def m_zeros(dtype):
     return occa.malloc(zeros(dtype))
예제 #4
0
def test_memcpy():
    def zeros(dtype):
        return np.zeros(5, dtype=dtype)
    def ones(dtype):
        return np.ones(5, dtype=dtype)
    def m_zeros(dtype):
        return occa.malloc(zeros(dtype))
    def m_ones(dtype):
        return occa.malloc(ones(dtype))

    # D# -> D#
    d1 = ones(np.float32)
    d2 = zeros(np.int32)
    occa.memcpy(d2, d1)

    assert d2.dtype == np.int32
    assert all(d2 == 1)

    # M1 -> D1
    m = m_ones(np.float32)
    d = zeros(np.int32)
    occa.memcpy(d, m)

    assert d.dtype == np.int32
    assert all(d == 1)

    # M1 -> D2
    m = m_ones(np.float32)
    d = zeros(np.int32)
    occa.memcpy(d, m)

    assert d.dtype == np.int32
    assert all(d == 1)

    # D1 -> M1
    d = ones(np.float32)
    m = m_zeros(np.int32)
    occa.memcpy(m, d)

    assert m.dtype == np.int32
    assert all(m.to_ndarray() == 1)

    # D1 -> M2
    d = ones(np.float32)
    m = m_zeros(np.int32)
    occa.memcpy(m, d)

    assert m.dtype == np.int32
    assert all(m.to_ndarray() == 1)

    # M1 -> M1
    m1 = m_ones(np.float32)
    m2 = m_zeros(np.int32)
    occa.memcpy(m2, m1)

    assert m2.dtype == np.int32
    assert all(m.to_ndarray() == 1)

    # M1 -> M2
    m1 = m_ones(np.float32)
    m2 = m_zeros(np.int32)
    occa.memcpy(m2, m1)

    assert m2.dtype == np.int32
    assert all(m.to_ndarray() == 1)

    # Ambiguous size
    m1 = occa.malloc(5, dtype=np.int32)
    m2 = occa.malloc(10, dtype=np.int32)
    with pytest.raises(ValueError):
        occa.memcpy(m1, m2)