• No results found

3.3 Type-Driven Priority Scheduling

3.3.2 Priority Queues

For the scheduling of tasks along the critical path of any latency-critical algorithm a priority queue can be used. For the development of a new type-driven prior- ity queue two classical solutions for the implementation of priority queues are discussed in the following.

Corresponding to [55, p. 351] priority queues can be subdivided into two groups. The first group describes bounded range priority queues using a discrete set of priorities, respectively a small set of priorities. The second group describes unbounded range priority queues with a very large set of priorities like all values of an integral type. A priority queue requires the following basic methods:

find-max Retrieve the element with the highest priority from the queue. delete-max Remove the element with the highest priority from the queue. insert Insert a new element into the queue.

These methods have different names in literature, but the semantic stays always the same.

Bounded Range Priority Queues

For applications requiring only a few priorities bounded range priority queues are sufficient. The restriction of available priorities leads to priority queues with lower complexities for the three main methods (see Table 3.1). A classical data structure for these queue is the bucket-based priority queue [33].

priority 1 priority 2 priority 3

Figure 3.8:An example of a bucket-based priority queue consisting of three priorities.

Method Complexity

find-max O(1)

delete-max O(1) insert O(1)

Table 3.1: Bucket-based priority queue complexities of the three main methods.

A bucket priority queue consists of several ordered sub-queues (see Figure 3.8). The order of the sub-queues represents the priorities. The number of sub-queues and therefore the number of different priorities is denoted with k. Most often the priorities are represented as integral values:

0 ≤ prio ≤ (k − 1) .

For inserting a new element, the correct sub-queue is retrieved by comparing the priorities of the new element and the sub-queues. This exhibits a maximum of k comparisons and since the number of sub-queues is fixed the complexity is O(1).

For the retrieval of the next element the sub-queues are iterated in the order of priorities. The first non-empty sub-queue is used and the next element is retrieved. This has the same complexity as the insert method of O(1). Strictly speaking, the prefactor is exactly the number of empty sub-queues before the first non-empty sub-queue.

This approach leads to a single-ended priority queue. However, a double-ended priority queue can be trivially implemented by iterating over the sub-queues in reverse order.

Most task engines provide priority queues with only a few priorities based on bucket priority queues (e.g. Argobots [102]).

Unbounded Range Priority Queues

Unbounded priority queues use a large set of priorities. For the implementation of those queues min and max heaps are often used. Since min and max heaps work similar, only max heaps will be discussed in the following.

Before describing max heaps, heap itself needs to be defined. The definition of a heap differs in the literature. The following definition is used in the remainder of this work and was adapted from [8].

38 19 1 8 11 2 35 27 16

(a)Max heap graph diagram

42 38 35 19 11 27 16 1 8 2 (b)Max heap memory layout Figure 3.9:An example of a max heap graph diagram and the corresponding memory

layout. The memory layout lists the tree levels from top downwards and the elements per level from left to right.

fulfilling the heap condition. The heap condition requires that all leaves are at most on two levels.

Heaps with n elements can be stored implicitly in an array of n elements [87]. For this purpose, the elements are listed top down, from left to right. Using this storage scheme, the root node can be called the first element and the rightmost element on the lowest level can be called the last element. Figure 3.9a shows an example of a max heap and the corresponding memory layout.

Definition 3.6 A max heap is a heap with an additional ordering condition. For every node, the stored value is greater or equal to all of its descendants.

Therefore, the maximum value can be found at the root node. This results in constant complexity for the find-max method (see Table 3.2). For inserting a new element, the element is inserted at the leftmost position on the lowest level. Afterwards it is compared to the value of the parent node. If the ordering condition is not fulfilled the elements are exchanged until the ordering is fulfilled or the root node is reached. This implies a maximum of log n comparisons and exchanges, hence the complexity for inserting is O(log n). For deleting the maximum element the tree needs to be restructured to fulfill the heap condition again. For this, the last element is inserted at the root node. Afterwards it is compared to both child elements. If the ordering condition is not fulfilled it is exchanged with the smallest element of both. This will be continued until the ordering is fulfilled or a leaf node is reached. This exhibits logarithmic complexity. With this approach single-ended priority queues can be implemented.

For the implementation of double-ended priority queues a combination of min and max heaps is required. One combination is the min-max heap [8]. This combination of min and max heaps exposes the same complexity for the main methods (see Table 3.3). This means, it requires O(log n) complexity forinsert, delete-min anddelete-maxand O(1) complexity for find-minandfind-max. To reach these complexities, an alternating order is used in the tree. The tree is

Method Complexity

find-max O(1)

delete-max O(log n) insert O(log n)

Table 3.2:Max heap-based single ended priority queue complexities of the three main methods. 1 42 8 19 27 2 35 38 16 11

(a)Min-Max heap graph diagram

1 42 38 8 2 16 11 19 27 35 (b)Min-Max heap memory layout Figure 3.10:An example of a min-max heap and the corresponding memory layout.

distinguished into even and odd levels. For even levels the nodes are smaller or equal to their descendants (min heap) and for odd levels the descendants are greater or equal to their descendants (max heap). An example of a min-max heap can be found in Figure 3.10.