def test_pointer():
    if ti.get_os_name() == 'win':
        # This test not supported on Windows due to the VirtualAlloc issue #251
        return
    x = ti.var(ti.f32)
    s = ti.var(ti.i32)

    n = 128

    @ti.layout
    def place():
        ti.root.dense(ti.i, n).pointer().dense(ti.i, n).place(x)
        ti.root.place(s)

    @ti.kernel
    def activate():
        for i in range(n):
            x[i * n] = 0

    @ti.kernel
    def func():
        for i in x:
            s[None] += 1

    activate()
    func()
    assert s[None] == n * n
def test_pointer2():
    if ti.get_os_name() == 'win':
        # This test not supported on Windows due to the VirtualAlloc issue #251
        return
    x = ti.var(ti.f32)
    s = ti.var(ti.i32)

    n = 128

    @ti.layout
    def place():
        ti.root.dense(ti.i, n).pointer().dense(ti.i,
                                               n).pointer().dense(ti.i,
                                                                  n).place(x)
        ti.root.place(s)

    @ti.kernel
    def func():
        for i in x:
            s[None] += 1

    x[0] = 1
    x[127] = 1
    x[254] = 1
    x[256 + n * n] = 1

    x[257 + n * n] = 1
    x[257 + n * n * 2] = 1
    x[257 + n * n * 5] = 1

    func()
    assert s[None] == 5 * n
    print(x[257 + n * n * 7])
    assert s[None] == 5 * n
def test_bitmasked():
    if ti.get_os_name() == 'win':
        # This test not supported on Windows due to the VirtualAlloc issue #251
        return
    x = ti.var(ti.f32)
    s = ti.var(ti.i32)

    n = 128

    @ti.layout
    def place():
        ti.root.dense(ti.i, n).bitmasked().dense(ti.i, n).place(x)
        ti.root.place(s)

    @ti.kernel
    def func():
        for i in x:
            s[None] += 1

    x[0] = 1
    x[127] = 1
    x[256] = 1
    x[257] = 1

    func()
    assert s[None] == 256
Beispiel #4
0
def test_dense_dynamic():
    if ti.get_os_name() == 'win':
        return
    n = 128
    x = ti.var(ti.i32)
    l = ti.var(ti.i32, shape=n)

    @ti.layout
    def place():
        ti.root.dense(ti.i, n).dynamic(ti.j, n, 8).place(x)

    @ti.kernel
    def func():
        ti.serialize()
        for i in range(n):
            for j in range(n):
                ti.append(x, j, i)

        for i in range(n):
            l[i] = ti.length(x, i)

    func()

    for i in range(n):
        assert l[i] == n
Beispiel #5
0
def test_append_ret_value():
    if ti.get_os_name() == 'win':
        return
    x = ti.var(ti.i32)
    y = ti.var(ti.i32)
    z = ti.var(ti.i32)
    n = 128

    @ti.layout
    def place():
        ti.root.dynamic(ti.i, n, 32).place(x)
        ti.root.dynamic(ti.i, n, 32).place(y)
        ti.root.dynamic(ti.i, n, 32).place(z)

    @ti.kernel
    def func():
        for i in range(n):
            u = ti.append(x, [], i)
            y[u] = i + 1
            z[u] = i + 3

    func()

    for i in range(n):
        assert x[i] + 1 == y[i]
        assert x[i] + 3 == z[i]
Beispiel #6
0
 def render(self, out: ti.ext_arr(), res: ti.template()):
     for i in range(res[0] * res[1]):
         r, g, b = self.image_at(i % res[0], res[1] - 1 - i // res[0])
         if ti.static(ti.get_os_name() != 'osx'):
             out[i] = (r << 16) + (g << 8) + b
         else:
             alpha = -16777216
             out[i] = (b << 16) + (g << 8) + r + alpha
Beispiel #7
0
 def get_events(self, *args):
     events = super().get_events(*args)
     if ti.get_os_name() == 'linux':
         return events
     for e in events:
         if e.key != GUI.MOVE:
             yield e
     curr_mpos = tuple(self.get_cursor_pos())
     if curr_mpos != self._last_mpos:
         self._last_mpos = curr_mpos
         e = GUI.Event()
         e.type = GUI.MOTION
         e.key = GUI.MOVE
         e.pos = curr_mpos
         e.modifier = []
         yield e
Beispiel #8
0
def vector_to_fast_image(img: ti.template(), out: ti.ext_arr()):
    # FIXME: Why is ``for i, j in img:`` slower than:
    for i, j in ti.ndrange(*img.shape):
        r, g, b = min(255, max(0, int(img[i, img.shape[1] - 1 - j] * 255)))
        idx = j * img.shape[0] + i
        # We use i32 for |out| since OpenGL and Metal doesn't support u8 types
        if ti.static(ti.get_os_name() != 'osx'):
            out[idx] = (r << 16) + (g << 8) + b
        else:
            # What's -16777216?
            #
            # On Mac, we need to set the alpha channel to 0xff. Since Mac's GUI
            # is big-endian, the color is stored in ABGR order, and we need to
            # add 0xff000000, which is -16777216 in I32's legit range. (Albeit
            # the clarity, adding 0xff000000 doesn't work.)
            alpha = -16777216
            out[idx] = (b << 16) + (g << 8) + r + alpha
Beispiel #9
0
init_args = {
    # 'key': [default, choices],
    'print_preprocessed': [False, TF],
    'log_level': ['info', ['error', 'warn', 'info', 'debug', 'trace']],
    'gdb_trigger': [False, TF],
    'excepthook': [False, TF],
    'advanced_optimization': [True, TF],
    'debug': [False, TF],
    'print_ir': [False, TF],
    'verbose': [True, TF],
    'fast_math': [True, TF],
    'async_mode': [False, TF],
    'flatten_if': [False, TF],
    'simplify_before_lower_access': [True, TF],
    'simplify_after_lower_access': [True, TF],
    'use_unified_memory': [ti.get_os_name() != 'win', TF],
    'print_benchmark_stat': [False, TF],
    'kernel_profiler': [False, TF],
    'check_out_of_bound': [False, TF],
    'print_accessor_ir': [False, TF],
    'print_evaluator_ir': [False, TF],
    'print_struct_llvm_ir': [False, TF],
    'print_kernel_llvm_ir': [False, TF],
    'print_kernel_llvm_ir_optimized': [False, TF],
    # FIXME: figure out why these two failed test:
    #'device_memory_fraction': [0.0, [0.5, 1, 0]],
    #'device_memory_GB': [1.0, [0.5, 1, 1.5, 2]],
}

env_configs = ['TI_' + key.upper() for key in init_args.keys()]
Beispiel #10
0
init_args = {
    # 'key': [default, choices],
    'print_preprocessed': [False, TF],
    'log_level': ['info', ['error', 'warn', 'info', 'debug', 'trace']],
    'gdb_trigger': [False, TF],
    'excepthook': [False, TF],
    'advanced_optimization': [True, TF],
    'debug': [False, TF],
    'print_ir': [False, TF],
    'verbose': [True, TF],
    'fast_math': [True, TF],
    'async_mode': [False, TF],
    'flatten_if': [False, TF],
    'simplify_before_lower_access': [True, TF],
    'simplify_after_lower_access': [True, TF],
    'use_unified_memory': [ti.get_os_name() == 'linux', TF],
    'print_benchmark_stat': [False, TF],
    'kernel_profiler': [False, TF],
    'check_out_of_bound': [False, TF],
    'print_accessor_ir': [False, TF],
    'print_evaluator_ir': [False, TF],
    'print_struct_llvm_ir': [False, TF],
    'print_kernel_llvm_ir': [False, TF],
    'print_kernel_llvm_ir_optimized': [False, TF],
    # FIXME: figure out why these two failed test:
    #'device_memory_fraction': [0.0, [0.5, 1, 0]],
    #'device_memory_GB': [1.0, [0.5, 1, 1.5, 2]],
}

env_configs = ['TI_' + key.upper() for key in init_args.keys()]
Beispiel #11
0
def please_install(name):
    if ti.get_os_name() == 'win':
        pip = 'python -m pip'
    else:
        pip = 'python3 -m pip'
    raise ImportError(f'Please run `{pip} install -U {name}`') from None