3 ms·
I tried implementing the same and I am getting 500ms vs 20ms with wrong answer in the first call in taichi but correct in subsequent calls. I guess I found some
by vishal0123 4y ago
I tried implementing the same and I am getting 500ms vs 20ms with wrong answer in the first call in taichi but correct in subsequent calls. I guess I found some bug in taichi: https://imgur.com/a/lpK2iVF https://imgur.com/a/lpK2iVF
Could you share your code as well.
N = 1000000
isnotprime = [0] * N
def count_primes(n: int) -> int:
count = 0
for k in range(2, n):
if isnotprime[k] == 0:
count += 1
for l in range(2, n // k):
isnotprime[l * k] = 1
return count
import taichi as ti
ti.init(arch=ti.cpu)
isnotprime = ti.field(ti.i8, shape=(N, ))
@ti.kernel
def count_primes(n: ti.i32) -> int:
count = 0
for k in range(2, n):
if isnotprime[k] == 0:
count += 1
for l in range(2, n // k):
isnotprime[l * k] = 1
return count
- dgacmu 4y agoPython: import time import math import numpy as np N = 1000000 SN = math.floor(math.sqrt(N)) sieve = [False] * N def init_sieve(): for i in range(2, SN): if not sieve[i]: k = i*2 while k < N: sieve[k] = True k += i def count_primes(n: int) -> int: return (N-2) - sum(sieve) start = time.perf_counter() init_sieve() print(f"Number of primes: {count_primes(N)}") print(f"time elapsed: {time.perf_counter() - start}/s") Taichi: import taichi as ti import time import math ti.init(arch=ti.cpu) N = 1000000 SN = math.floor(math.sqrt(N)) sieve = ti.field(ti.i8, shape=N) @ti.kernel def init_sieve(): for i in range(2, SN): if sieve[i] == 0: k = i*2 while k < N: sieve[k] = 1 k += i @ti.kernel def count_primes(n: int) -> int: count = 0 for i in range(2, N): if (sieve[i] == 0): count += 1 return count start = time.perf_counter() init_sieve() print(f"Number of primes: {count_primes(N)}") print(f"time elapsed: {time.perf_counter() - start}/s") (The difference of using 0 vs False is tiny; I had just been poking at the python code to think about how I'd make it more pythonic and see if that made it worse to do taichi)