Exemple #1
0
    def fn(self, pkt: Optional[Packet], r: WorkerTaskRec) -> Task:
        w: WorkerTaskRec = r
        if pkt is None:
            return self.waitTask()

        if w.destination == int64(I_HANDLERA):
            dest: int64 = int64(I_HANDLERB)
        else:
            dest = int64(I_HANDLERA)

        w.destination = dest
        pkt.ident = dest
        pkt.datum = 0

        i = 0
        while i < BUFSIZE:
            x: int64 = w.count + 1
            w.count = x
            if w.count > 26:
                w.count = 1
            # TODO: Fix compiler error when w.count is boxed
            pkt.data[i] = int64(A) + w.count - 1
            i = i + 1

        return self.qpkt(pkt)
 def fn(self, pkt: Optional[Packet], r: TaskRec) -> Optional[Task]:
     i: IdleTaskRec = cast(IdleTaskRec, r)
     i.count -= 1
     if i.count == 0:
         return self.hold()
     elif i.control & 1 == 0:
         i.control //= 2
         # TODO: We should automatically support an INVOKE_FUNCTION here
         # if we make IdleTask final, and support primitives in method calls
         return Task.release(self, int64(I_DEVA))
     else:
         i.control = i.control // 2 ^ 0xd008
         return Task.release(self, int64(I_DEVB))
Exemple #3
0
    def __init__(self, i: int, p: int, w: Optional[Packet], initialState: TaskState, r: TaskRec) -> None:
        wa: TaskWorkArea = taskWorkArea
        self.link: Optional[Task] = wa.taskList
        self.ident: int64 = int64(i)
        self.priority: int64 = int64(p)
        self.input: Optional[Packet] = w

        self.packet_pending = initialState.isPacketPending()
        self.task_waiting = initialState.isTaskWaiting()
        self.task_holding = initialState.isTaskHolding()

        self.handle = r

        wa.taskList = self
        wa.taskTab[i] = self
Exemple #4
0
def permutations(pool: ArrayI64, r: int64 = -1) -> Iterator[ArrayI64]:
    n = clen(pool)
    if r == -1:
        r = n
    rb = box(r)
    indices: ArrayI64 = create_array(0, n, 1)
    cycles: ArrayI64 = create_array(n, n - r, -1)
    per: ArrayI64 = ArrayI64(rb)
    idx: int64 = 0
    while idx < r:
        per[idx] = pool[indices[idx]]
        idx += 1

    yield per
    while n:
        i = rb - 1
        while i >= 0:
            cycles[i] -= 1
            if cycles[i] == 0:
                indices[i:] = ArrayI64(indices[i + 1:] + indices[i:i + 1])
                cycles[i] = n - int64(i)
            else:
                j = cycles[i]
                tmp: int64 = indices[-j]
                indices[-j] = indices[i]
                indices[i] = tmp
                idx = 0
                while idx < r:
                    per[idx] = pool[indices[idx]]
                    idx += 1
                yield per
                break
            i -= 1
        if i == -1:
            return
    def fn(self, pkt: Optional[Packet], r: TaskRec) -> Task:
        h: HandlerTaskRec = cast(HandlerTaskRec, r)
        if pkt is not None:
            if pkt.kind == int64(K_WORK):
                h.workInAdd(pkt)
            else:
                h.deviceInAdd(pkt)
        work: Optional[Packet] = h.work_in
        if work is None:
            return self.waitTask()
        count: int64 = work.datum
        if count >= int64(BUFSIZE):
            h.work_in = work.link
            return self.qpkt(work)

        dev: Optional[Packet] = h.device_in
        if dev is None:
            return self.waitTask()

        h.device_in = dev.link
        dev.datum = work.data[count]
        work.datum = count + 1
        return self.qpkt(dev)
def fannkuch(nb: int) -> int:
    n: int64 = int64(nb)
    count: ArrayI64 = ArrayI64(range(1, nb + 1))
    max_flips: int64 = 0
    m: int64 = n - 1
    r: int64 = n
    perm1: ArrayI64 = ArrayI64(range(nb))
    perm: ArrayI64 = ArrayI64(range(nb))
    perm0: ArrayI64 = ArrayI64(range(nb))

    while 1:
        while r != 1:
            count[r - 1] = r
            r -= 1

        if perm1[0] != 0 and perm1[m] != m:
            i: int64 = 0
            while i < n:
                perm[i] = perm1[i]
                i += 1
            flips_count: int64 = 0
            k: int64 = perm[0]
            while k:
                i = k
                while i >= 0:
                    perm0[i] = perm[k-i]
                    i -= 1
                i = k
                while i >= 0:
                    perm[i] = perm0[i]
                    i -= 1
                flips_count += 1
                k = perm[0]

            if flips_count > max_flips:
                max_flips = flips_count

        while r != n:
            first: int64 = perm1[0]
            i = 1
            while i <= r:
                perm1[i-1] = perm1[i]
                i += 1
            perm1[r] = first
            count[r] -= 1
            if count[r] > 0:
                break
            r += 1
        else:
            return box(max_flips)
Exemple #7
0
def create_array(start: int64, end: int64, step: int64) -> ArrayI64:
    """
    Function that creates an array that contains elements from start (inclusive) to end (non-inclusve) increasing the given steps
    Note: if It is not possible to go from start to end, an empty array will be returned.
    For example: create_array(2,7,2) -> (2,4,6) ; create_array(1,4,1)->(1,2,3)
    """
    c: int64 = start
    i: int64 = 0
    if (end - start) * step <= 0:
        return ArrayI64(0)
    size: int64 = int64((static_abs(end - start) - 1) / static_abs(step) + 1)
    a: ArrayI64 = ArrayI64(box(size))
    while i < size:
        a[i] = c
        c = c + step
        i = i + 1
    return a
Exemple #8
0
def solve(queen_count: int) -> Iterator[ArrayI64]:
    """N-Queens solver.

    Args:
        queen_count: the number of queens to solve for. This is also the
            board size.

    Yields:
        Solutions to the problem. Each yielded value is looks like
        (3, 8, 2, 1, 4, ..., 6) where each number is the column position for the
        queen, and the index into the tuple indicates the row.
    """

    # The generator is still needed as it is being used to check if it is a valid configuration using sets
    cols: Iterator[int] = range(queen_count)
    static_cols: ArrayI64 = create_array(0, int64(queen_count), 1)
    for vec in permutations(static_cols):
        if (queen_count == len(set(vec[i] + i for i in cols))  # noqa: C401
                == len(set(vec[i] - i for i in cols))  # noqa: C401
            ):
            yield vec
Exemple #9
0
def fannkuch(n: int) -> int:
    count: List[int] = list(range(1, n + 1))
    max_flips: int64 = 0
    m: int = n - 1
    n64: int64 = int64(n)
    r: int64 = n64
    perm1: List[int] = list(range(n))
    perm: List[int] = list(range(n))
    perm1_ins: Callable[[int, int], None] = perm1.insert
    perm1_pop: Callable[[int], int] = perm1.pop

    while 1:
        while r != 1:
            count[r - 1] = box(r)
            r -= 1

        if perm1[0] != 0 and perm1[m] != m:
            perm = perm1[:]
            flips_count: int64 = 0
            k: int = perm[0]
            while k:
                perm[: k + 1] = perm[k::-1]
                flips_count += 1
                k = perm[0]

            if flips_count > max_flips:
                max_flips = flips_count

        while r != n64:
            perm1_ins(box(r), perm1_pop(0))
            count[r] -= 1
            if count[r] > 0:
                break
            r += 1
        else:
            return box(max_flips)
 def __init__(self) -> None:
     self.destination: int64 = int64(I_HANDLERA)
     self.count: int64 = 0
    def run(self, iterations: int) -> bool:
        for i in range(iterations):
            taskWorkArea.holdCount = 0
            taskWorkArea.qpktCount = 0

            IdleTask(int64(I_IDLE), 1, 10000,
                     TaskState().running(), IdleTaskRec())

            wkq: Optional[Packet] = Packet(None, 0, int64(K_WORK))
            wkq = Packet(wkq, 0, int64(K_WORK))
            WorkTask(int64(I_WORK), 1000, wkq,
                     TaskState().waitingWithPacket(), WorkerTaskRec())

            wkq = Packet(None, int64(I_DEVA), int64(K_DEV))
            wkq = Packet(wkq, int64(I_DEVA), int64(K_DEV))
            wkq = Packet(wkq, int64(I_DEVA), int64(K_DEV))
            HandlerTask(int64(I_HANDLERA), 2000, wkq,
                        TaskState().waitingWithPacket(), HandlerTaskRec())

            wkq = Packet(None, int64(I_DEVB), int64(K_DEV))
            wkq = Packet(wkq, int64(I_DEVB), int64(K_DEV))
            wkq = Packet(wkq, int64(I_DEVB), int64(K_DEV))
            HandlerTask(int64(I_HANDLERB), 3000, wkq,
                        TaskState().waitingWithPacket(), HandlerTaskRec())

            wkq = None
            DeviceTask(int64(I_DEVA), 4000, wkq,
                       TaskState().waiting(), DeviceTaskRec())
            DeviceTask(int64(I_DEVB), 5000, wkq,
                       TaskState().waiting(), DeviceTaskRec())

            schedule()

            if taskWorkArea.holdCount == 9297 and taskWorkArea.qpktCount == 23246:
                pass
            else:
                print('err')
                return False
        return True
Exemple #12
0
 def __init__(self, l: Optional[Packet], i: int, k: int) -> None:
     self.link: Optional[Packet] = l
     self.ident: int64  = int64(i)
     self.kind: int64  = int64(k)
     self.datum: int64 = 0
     self.data: ArrayI64 = ArrayI64(BUFSIZE)