Manuscript Offprint
arXiv:2611.04914Sorting in PRAM, Parallel Comparison Model and Parallel External Memory Model
GOPALAN SAJITH*, MANAS KUMAR PANDA†, and RANAJIT SENKO†, Indian Institute of Technology Guwahati, India
We study the problem of sorting on the Parallel Random Access Machine (PRAM), Parallel Comparison Model (PCM) and Parallel External Memory Model (PEM). On PRAM, we present a Concurrent Read Exclusive Write algorithm that runs in time with processors; our algorithm is very different from Cole's Merge Sort. On PCM, we present the first optimal algorithm for sorting after AKS; our algorithm runs in comparison rounds with processors, for . On PEM, we present a Concurrent Read Exclusive Write algorithm that sorts elements in I/O rounds with processors.
CCS Concepts: • Theory of computation → Shared memory algorithms; Sorting and searching.
Additional Key Words and Phrases: external memory algorithms, models of computation
ACM Reference Format:
Gopalan Sajith, Manas Kumar Panda, and Ranajit Senko. 2026. Sorting in PRAM, Parallel Comparison Model and Parallel External Memory Model. 1, 1 (November 2026), 20 pages. https://doi.org/10.1145/nnnnnnn.nnnnnnn
1 Introduction
Sequential algorithms, studied in traditional algorithms, are designed to run on a single processor [2, 13]. Improvements in processor speeds have hitherto enabled us to solve increasingly larger problem instances using such algorithms. However, processor speeds are approaching their inherent physical limits, making it difficult to achieve further speedups through sequential algorithms alone. As technological advances continue to spawn ever larger problem instances, the use of multiple processors will become essential. Algorithms specifically designed to run on multiple processors are known as parallel algorithms.
The PRAM model envisions a set of synchronous processors that collectively share a random-access memory; each of these processors resembles the processor of the Random Access Machine model [2]. Each PRAM instruction comprises three phases: Read, Compute, and Write. Processors transition through these phases synchronously, ensuring that all
*This author contributed the most to this research. †Both of these authors contributed equally to this research.
Authors' Contact Information: Gopalan Sajith, sajith@iitg.ac.in; Manas Kumar Panda, manas.panda@iitg.ac.in; Ranajit Senko, ranajit@iitg.ac.in, Indian Institute of Technology Guwahati, Guwahati, Assam, India.
Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. Copyrights for components of this work owned by others than ACM must be honored. Abstracting with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. Request permissions from permissions@acm.org.
© 2026 ACM.
G. Sajith, M. K. Panda and R. Senko
processors are in the same phase at any given time. The shared memory is assumed to be large enough to accommodate all the necessary data. Multiple processors can concurrently read from or write to the memory. Depending on how conflicts from concurrent reads or writes to the same memory location are handled, different variants of the PRAM model exist: the EREW (exclusive read, exclusive write) PRAM prohibits multiple processors from reading or writing the same memory location simultaneously; the CREW (concurrent read, exclusive write) PRAM allows concurrent reads but disallows concurrent writes; and the COMMON CRCW (concurrent read, concurrent write) PRAM permits concurrent reads and concurrent writes, provided that all processors write the same value. Many other variants of CRCW PRAM can be found in the literature [18, 28]. The performance of a PRAM algorithm is typically expressed in terms of the number of processors used and the amount of time, measured in instruction cycles, required to complete the computation.
In the Parallel Comparison Model (PCM), each comparison incurs unit cost, while all other operations are considered free. A comparison round involves multiple comparisons performed concurrently. The performance of a PCM algorithm is expressed in terms of the number of comparison rounds. Lower bounds established for PCM are applicable to all PRAM models.
The Parallel External Memory (PEM) model [4, 6] combines elements from both PRAM and external memory (EM) models [1]. Like PRAM, PEM employs a set of synchronous processors, and, like EM, it assumes that the data exceed the capacity of the main memory. In PEM, processors share an external memory, while each processor has an individual main memory of size . Computation proceeds in synchronized read and write I/O rounds. In each read (write) I/O round, a block of size is transferred from (resp., to) the shared external memory to (resp., from) the main memory of each processor. The performance of a PEM algorithm is typically measured in terms of the number of processors used and the amount of time, measured in I/O rounds, required to complete the computation.
1.2 Sorting and Merging
Sorting a set of items drawn from a linearly ordered set is the problem of arranging them in ascending or descending order. When comparisons are the only operations allowed on the items, sorting is referred to as comparison-based. The sequential time complexity of comparison-based sorting is [2, 13].
To merge two sorted arrays is to combine them into a single one. In the multiway merging problem, we combine multiple sorted arrays into one. When there are arrays, the problem is called -way merging. The cases and correspond to ordinary merging and sorting, respectively. When only comparisons are allowed on the input elements, these problems are also referred to as comparison-based. Comparison-based 2-way merging requires only time in the sequential setting [2, 13].
Sorting, merging, and multiway merging are fundamental operations that are widely used in databases, search engines, big data processing, computer graphics, and network log analysis, to name a few. This makes them important and extensively studied in algorithms.
1.2.1 On PRAM and related models. Batcher gave -depth, -cost comparator networks for sorting [19]. Later, Ajtai, Komlós, and Szemerédi [3, 24, 25] built a sorting network (known as AKS) that has -depth, -size, but with large constants hidden in the -notation. The same bounds apply on EREW PRAM. Cook et al. [12] showed that with any number of processors, OR needs time on CREW PRAM, implying the same for sorting. Hence, AKS sorting is optimal even for CREW PRAM. Cole gave a simpler pipeline-based EREW PRAM algorithm with time, processors, and small constants [10].
Azar and Vishkin proved that on the parallel comparison model, sorting with processors needs at least steps [7]. Alon gave a matching upper bound (via AKS) with large constants [7]. Cole's merge sort achieves a time of . Columnsort [22] runs in time, and Cubesort [14] in time with processors.
In CRCW PRAM, Beame and Hastad [8] showed that parity needs time, implying that sorting needs time with processors. No algorithm that matches this lower bound is known. The best known algorithm runs in time [15], while Cole's merge sort on CRCW PRAM takes time.
Sajith [26] and Sajith and Saxena [27] gave a parallel merge sort algorithm, that, unlike Cole's merge sort [10] avoids pipelining and features small constant factors. In the parallel comparison model with processors (), it sorts items in comparison rounds. On a CREW PRAM with processors, this algorithm runs in time, and on a CRCW PRAM with processors, in time.
Comparison-based 2-way merging takes time sequentially. In parallel, Batcher gave depth, size merging networks [19], which are optimal due to an lower bound [19]. On an EREW PRAM, Hagerup and Rüb [16] used Batcher's networks to get an time optimal algorithm. A reduction from searching [31] shows 2-way merging needs time on EREW PRAM. Borodin and Hopcroft [9] showed merging needs time on the parallel comparison model with processors. This matches the CREW PRAM upper bound [9, 32]. Kruskal [20] improved it to time, optimally.
Sequential -way merging needs time [33]. Using heaps, it can be done in time [19]. Lee and Batcher [21] extended odd-even merge to depth for -way merging. Wen [33] and Sajith [26] gave time optimal CREW PRAM algorithms.
1.2.2 On PEM. Arge et al. [5] introduce the Parallel External Memory (PEM) model and establish a lower bound of parallel I/O rounds for sorting. Aggarwal and Vitter [1] show a lower bound of in the external memory model, implying in PEM.
Thus, the combined lower bound is .
They present distribution sort and merge sort algorithms that match this bound for many parameter values; the merge sort achieves an I/O complexity of , where .
Cole and Ramachandran [11] propose SPMS, a deterministic, resource-oblivious sorting algorithm with work, cache complexity under tall-cache assumptions, and parallel time. SPMS minimizes false sharing, runs efficiently under work-stealing schedulers, and scales well on modern multicore architectures without manual tuning.
Sitchinava and Zeh present a PEM merge algorithm that achieves an I/O complexity of using processors [30].
1.2.3 Other results on PRAM and PEM. Algorithms on the PRAM model are well studied; see the textbook by JáJá [18]. In the PEM model, Arge et al. [6] applied list ranking to graph problems using Delayed Pointer Processing, achieving parallel I/Os. This approach is central to solving connected components, minimum spanning tree, and biconnectivity problems in PEM. Jacob et al. [17] established that permitting lower bounds apply for list ranking in
G. Sajith, M. K. Panda and R. Senko
PEM and provided tight algorithms for a wider range of parameters. Sitchinava [29] investigated distribution sweeping and proposed a parallel buffer tree with I/O-optimal performance for range and membership queries. Subsequently, Sitchinava and Zeh [30] proposed the Parallel Buffer Tree, a batched I/O-optimal data structure supporting insertions, deletions, membership and range queries with parallel I/Os, where is the size of the output. Lieber [23] proposed a PEM-efficient list ranking algorithm that matches known sorting bounds and introduced the Guided Interval Fusion (GIF) problem, establishing a stronger lower bound in specific PEM settings.
1.3 Our Results
In this paper, we present efficient parallel algorithms for the sorting problem under three different computational models. Our main results are the following:
(1) an time algorithm for sorting using processors on a CREW PRAM.
(2) an time algorithm for sorting using processors on the Parallel Comparison Model (PCM), where .
(3) an algorithm for sorting in the Parallel External Memory (PEM) model with processors that requires I/O rounds under the CREW constraint.
1.4 Organization of this paper
The remainder of the paper is organized as follows. Section 2 presents the necessary preliminaries and formal definitions used throughout the paper. Section 3 introduces the key procedure Stratify, which forms the core of the proposed algorithms. In Section 4, we describe an efficient algorithm for sorting on the CREW PRAM model. Section 5 develops a sorting algorithm for the Parallel Comparison Model (PCM), focusing on minimizing the number of parallel comparison rounds. Section 6 extends the approach to the CREW PEM model and presents parallel algorithms for both sorting and multiway merging in an I/O-efficient manner. Finally, Section 7 concludes the paper with a summary of contributions and potential directions for future work.
2 Preliminaries
Some notation and preliminary concepts are presented in this section.
The following definitions are from Cole [10]. For three keys and drawn from a linearly ordered set, we say that is between and , if . For a key and a sorted array , the rank of in is the number of elements in that are less than or equal to . For two sorted arrays and , is said to be ranked in if, for every element of , we know the rank of in ; and are said to be cross ranked if they are ranked into each other.
The rest of this section draws on Sajith [26] and Sajith and Saxena [27].
We use the following expressions:
-
MERGE: the time complexity of merging two sorted arrays of a total size of with processors
-
PREFIX: the time complexity of finding the prefix sums over an array of size of -bit numbers using processors
-
SORT: the time complexity of sorting an array of size using processors
-
SEARCH: the time complexity of searching for an item in a sorted array of items using processors
The model of computation on which the time complexity is measured will become clear from the context.
We assume that for all the computational models considered, the upper bounds for merging, prefix summing, and sorting follow a "regularity" condition. Specifically, larger problem sizes with the same processor-to-input ratio (i.e., ) should not complete faster. That is, if , then ; and the same principle applies to sorting and prefix summing.
Over the course of this paper, we shall often encounter the following allocation problem [26]. Given an array of elements , where , fill an array so that, for , iff is the smallest index so that (or, in other words, ).
Using prefix sum and merge, problem can be solved as follows. Compute the array of the prefix sums of . Let , for . Cross rank and . Now, can be filled as required in the problem: set as the rank of in . Hence, the following lemma of [26, 27]:
Lemma 1. The problem can be solved in time when processors are available on the PRAM models and PCM.
Given arrays of sizes , respectively, where , and processors, on the PRAM models and PCM, we can assign one processor for every elements of the input using Lemma 1. Imagine that the arrays are sequenced in that order contiguously. For , let the -th processor handle the elements numbered to in this imagined sequence. An algorithm for can be used to translate and into pointers to the corresponding elements in the input. Processor now knows the identity of all the elements it is to handle [26, 27].
3 Procedure Stratify
Our algorithms use a procedure called STRATIFY, which we describe at a high level in this section. The details of its implementation on specific models will be discussed in other sections. This is adapted from a similar procedure by Sajith and Saxena [26, 27].
Procedure STRATIFY(n, k, , )
Input: Parameters and are positive integers. Array is of size , for , and is sorted in increasing order; . Without loss of generality, we assume that the elements of the arrays are all distinct, because otherwise, for each , the -th element of , can be replaced by a triplet , with the linear ordering extended lexicographically. Parameters and are integers so that .
Output: A permutation of the input elements, given in an array so that has (not necessarily sorted) subarrays of size , for , given one after another contiguously, so that for all and all , each element of is larger than every element of .
Step 1: Divide , for , into segments of size each. There are segments in . If is not divisible by , the last segment is of a size smaller than . So the total number of segments formed satisfies . In each segment, designate its least element as a bristle. Let an array , for , contain the set of bristles from arranged in increasing order.
For , cross-rank the bristle arrays and . Each bristle now has ranks, the rank of in for , assigned to it.
G. Sajith, M. K. Panda and R. Senko
Step 2: For each bristle , in parallel, add together all 's. Consequently, each bristle knows its rank among all the bristles; that is, now, all the bristles can be placed together in an array in ascending order. Let .
Step 3: Each bristle now knows, for each , the segment of into which it falls. For each and for each array , search in parallel to find the rank of in it. From this can infer its rank in .
Step 4: For each bristle , compute , the rank of in , the output array.
REMARK: For , there can be at most elements in , , that are in the interval ; these elements are deemed to be under the charge of . That is, , for all . Hence, .
Step 5: If , then choose and remove (See Section 6.2 for details) some elements of , and then compact the remaining elements of into an array (of size, say, ) so that for . Set and . Assume that .
REMARK: For each removed element, the elements that were under its charge are transferred to the largest smaller bristle that remains. (This step is relevant only for the PEM implementation.)
Step 6: For , and , let
Note that is the number of elements that are larger than but smaller than in .
Let be the prefix sums computed over is set to zero.
If , then copy to .
That is, we isolate all elements that are larger than but smaller than into an array .
REMARK: Note that .
Step 7: Copy , for , in parallel, into the -st location of . Copy , for , in parallel, into the subarray of of equal size that begins at index .
REMARK: Array X is now in the desired form; and together form the required subarray of X.
4 Sorting on a CREW PRAM
We use Procedure Stratify to design an optimal CREW PRAM sorting algorithm, which is described in this section. We begin with an analysis of the procedure.
4.1 Procedure Stratify on a CREW PRAM when
We can partition the input arrays and form the bristle arrays in time using processors, once they are assigned one per element. Assign one processor for every elements of the input, using Lemma 1. Each processor can now sequentially select bristles from the elements assigned to it. All this takes time. Cross-ranking of the bristle arrays involves merges, each of size at most , to be performed in parallel. Each bristle participates in merges. Use Lemma 1 to assign processors per bristle; -th of those processors will participate in each of the merges of the bristle. Solve each merge instance independently. The time taken is .
Steps 2 and 4 each requires every bristle to perform an addition of size , of bit numbers. Use Lemma 1 to assign processors per bristle. Find the sums using a prefix summation algorithm in time. The total time taken is .
Each bristle has to perform searches in Step 3, each over a segment of size ; that is, a total of searches have to be performed in parallel. At most processors are available per search. Hence, we can finish this step in time.
Since , Step 5 takes time.
The subtractions in Step 6 can be performed in time with processors. With processors, this would take time. Step 6 also requires every bristle to perform a prefix summation of size , of bit numbers. With a processor allocation similar to that in Step 2, the sums can be found in time. Since the bristles are now merged into array , the allocation incurs only time.
In Step 6, we also prepare the subarrays . Each bristle , for , if , tags element with the address of the location ; note that , the leftmost element of in 's charge, is to be copied to . Assign processors to each segment; and for each element find the largest tagged element smaller than it in its array. As the second element in the segment is guaranteed to be tagged, the first being a bristle, this does not require any inter-segment information access; a prefix minima over each segment, done in parallel, will do. Thus, each element can compute the address of the cell it must be copied to. So, the total time taken is .
In Step 7, we copy the bristles and 's into to form the 's. Since every element knows where it is to be copied, this takes time.
On a PRAM that allows concurrent reads, and [18, 32]. Hence, for a constant ,
and
That is, if , then .
Also, as , if , then .
Thus, we have the following lemma:
Lemma 2. The procedure call “STRATIFY” with takes
time with processors on a PRAM that allows concurrent reads.
4.2 A Special Case of Stratify
In this subsection we describe Procedure SLICE_AND_STRATIFY. This procedure call takes as input an array with a small number of large sorted subarrays given one after another contiguously, and produces as output a permutation of the input elements that has a large number of small subarrays so that all elements of a subarray are larger than every element of a preceding subarray. The procedure is described below.
Procedure SLICE_AND_STRATIFY
G. Sajith, M. K. Panda and R. Senko
Input: is an array of distinct elements with sorted subarrays , , of size each, given one after another contiguously, where is a multiple of .
Output: The output of the procedure is a permutation of the input elements, given in array itself, that would have (not necessarily sorted) subarrays of size , , , given one after another contiguously, so that all elements of a subarray are larger than every element of a preceding subarray.
Processors: The number of processors available is .
Step: Invoke STRATIFY. Overwrite with the output.
Lemma 3. The procedure call “SLICE_AND_STRATIFY” uses processors and takes
time on a PRAM that allows concurrent reads.
4.3 A CREW PRAM algorithm for sorting
We construct a sequence of sorting algorithms using the following idea:
If there exists an time processors algorithm for sorting items, where , for some such that , there exists an time processors algorithm for sorting items.
A high level description of the new algorithm would be as follows:
Choose a parameter .
Divide the input array of size into subarrays of size each.
Sort each subarray using algorithm .
for to do /* There are sorted subarrays of size each. */
Make groups of of the existing subarrays.
Apply procedure SLICE_AND_STRATIFY on all groups in parallel.
/* The elements of each group now appear in a sequence of (not necessarily sorted) subarrays of size at most each, so that all elements of a subarray are larger than every element of a preceding subarray within the group. */
Sort the resultant subarrays in parallel using algorithm .
/* Now each group has merged into a single sorted array. That is, now there are sorted subarrays of size each. */
endfor
/* Now a single sorted array is left. */
For an appropriate choice of , this scheme gives the desired time bound.
We now give the construction scheme more formally. The basic building block of the scheme is Valiant's algorithm that merges two arrays of a total size in time with processors on a CREW PRAM [18, 32]; let be the constant hidden by the -notation here. Guided by a complete binary tree on leaves, proceeding bottom up, and using Valiant's algorithm at each level, the standard merge sort thus runs in time on an processor CREW PRAM [18]. This is the 0-th algorithm in our sequence. We denote this algorithm by CREW_SSORT.
For , CREW_SSORT is given below.
We partition hierarchically as follows. For level , define the -th subarray of size in by
where and .
Each subarray at level is composed of consecutive subarrays at level :
Procedure CREW_SSORT_h(I, n) begin , where ; for in parallel do / CREW_SSORT_{h-1}(I_i^{(0)}(h), k_h^2); for to do begin for in parallel do begin SLICE_AND_STRATIFY(); for in parallel do CREW_SSORT_{h-1}(G_x(I_i^{(j)}(h)), g_x(I_i^{(j)}(h))); end end end end
On a CREW PRAM, is , is [18], and is . Thus, by Lemma 3, with one processor per input element, the procedure call “SLICE_AND_STRATIFY ” takes time on a CREW PRAM. Let be the time taken by CREW_SSORT_h(I, n) with n processors. Then, for h > 0,
Since on a CREW PRAM, is a lower bound on sorting elements with any number of processors [12], . Thus, for sufficiently large constants and ,
In particular,
Since , this means that .
G. Sajith, M. K. Panda and R. Senko
Similarly,
Since , this means that , when .
Lemma 4. For , the procedure call CREW_SSORT sorts an array of size , with processors, on a CREW PRAM, in time, when .
PROOF. The proof is by induction. The basis is when ; this was argued above. Suppose the procedure call CREW_SSORT sorts an array of size , with processors, on a CREW PRAM, in time, when . Arguing as in the basis case, we find that , and , which is at most , when .
Define .
By Lemma 4, when , for constant , CREW_SSORT sorts an array of size , with processors, on a CREW PRAM, in time. In particular, when , use CREW_SSORT to sort; this runs in time, for a constant . (Denote by ; then implies that , and hence as well.) When , use CREW_SSORT to sort; this too runs in time. (This is because .) In general, when , CREW_SSORT sorts items in time.
Given a sorting instance of size , from the above sequence of algorithms we choose the appropriate one to be invoked on it; the algorithms are structurally identical, and are parameterized by . The parameters can be precomputed offline. For each fixed , the resulting lookup table may be reused across instances.
Thus we have the following theorem:
Theorem 1. Using one of the procedures CREW_SSORT, for , an array of size can be sorted with processors, on a CREW PRAM, in time.
5 An Algorithm for sorting on the Parallel Comparison Model
A generalization of the CREW PRAM algorithm, given in the previous section, works on the parallel comparison model.
Here again, the basic building block is Valiant's algorithm that merges two arrays of a size of each in time with processors on the parallel comparison model [18, 32] (for a processor advantage of ): pick every -th element from the two arrays as bristles, cross-rank the bristles in time, rank the bristles into the two original arrays in time, and recurse with the subproblems created at the same processor advantage, with the recursion ending when the subproblems have sizes upperbounded by , are provided with at least a quadratic number of processors each, and therefore are solvable in time.
Guided by a complete -ary tree on leaves, proceeding bottom up, and using Valiant's algorithm at each level, the standard merge sort runs in time on an processor PCM. (At each non-leaf in the recursion Manuscript submitted to ACM
Procedure PCM_SSORT begin , where ;
for in parallel do / PCM_SSORT; for to do begin for in parallel do begin SLICE_AND_STRATIFY(); for in parallel do PCM_SSORT; end end end end
tree, a -way merge has to be performed. For example, at the root of the recursion tree a -way merge of sorted arrays of size each is to be performed with processors. This can be achieved through a pairwise cross-ranking of the arrays; there are pairs; processors can be assigned to each pair; the cross-ranking can be achieved in time; as addition comes for free on PCM, the merge is achieved at no further cost. Let be the constant hidden by the -notation here. This is the first algorithm in our sequence. We denote this algorithm by PCM_SSORT.
For , PCM_SSORT is given below. We use the notation introduced in the last section. For level , define the -th subarray of size in by , where and . Each subarray at level is composed of consecutive subarrays at level : .
On the parallel comparison model, prefix sums come for free, MERGE, and SEARCH. Thus, by Lemma 3, with processors per input element, the procedure call "SLICE_AND_STRATIFY()" takes time on a PCM.
Let be the time taken by PCM_SSORT with processors. Then, for ,
Since on a PCM, is a lower bound on sorting elements with processors [12], . Thus, for sufficiently large constants and ,
G. Sajith, M. K. Panda and R. Senko
In particular,
Since , this means that .
Similarly,
Since , this means that
, when .
Lemma 5. For , the procedure call PCM_SSORT sorts an array of size , with processors, on PCM, in time, when .
PROOF. The proof is by induction. The basis is when ; this was argued above. Suppose the procedure call PCM_SSORT sorts an array of size , with processors, on PCM, in time, when . Arguing as in the basis case, we find that , and , which is at most , when .
Define .
By Lemma 5, when , for , PCM_SSORT sorts an array of size , with processors, on a PCM, in time. Arguing as we did in the case of CREW PRAM in the previous section, we conclude that when , PCM_SSORT sorts items in time.
Given a sorting instance of size , from the above sequence of algorithms we choose the appropriate one to be invoked on it; the algorithms are structurally identical, and are parameterized by . The parameters can be precomputed offline. For each fixed , the resulting lookup table may be reused across instances.
Thus we have the following theorem:
Theorem 2. Using one of the procedures PCM_SSORT, for , an array of size can be sorted with processors, on a PCM, in time.
6 Sorting and Multiway-merging on CREW PEM
A PEM model is said to be self-simulating, if for all and , a PEM of the model with a size of can simulate a single I/O round of another PEM of the model with a size of in I/O rounds. All Exclusive Write PEMs are self-simulating.
For example, the write phase of an -processor CREW PEM can be simulated on an -processor CREW PEM by letting the “actual” processors simulate the “virtual” processors in groups of size each. Since no two processors write in the same memory block, it is feasible to stagger the write into several I/O rounds.
The cost or work of an algorithm that runs in I/O rounds using processors is the time-processor product . If Seq() is the worst-case running time (in I/O rounds) of the fastest known sequential external memory algorithm for a problem of size , an optimal PEM algorithm for the same problem runs in time (in I/O rounds) using processors.
The degree of parallelism (DoP) of an I/O round is the number of I/Os in it.
For a PEM algorithm presented in I/O rounds, where the DoP of the -th I/O round is , the total number of blocks transferred is . If we use processors, the algorithm runs in I/O rounds, but may be . If our model is self-simulating, and we have processors, then the -th I/O round can be simulated in I/O rounds. So, the total time (in I/O rounds) taken by the simulation is
With , , and the total cost of the execution is I/Os.
Hence, as with PRAM algorithms, we can describe our PEM algorithms in a number of I/O rounds of varying DoPs, without bothering about the actual number of processors available, provided, our presentation makes clear the DoP of each step. We shall refer to this principle as the Brent's Theorem or WT Scheduling Principle as applied to PEM.
6.1 Some Basic PEM Algorithms
Now we discuss PEM solutions for a number of algorithmic problems. These algorithms follow easily from the corresponding PRAM algorithms. For each problem, we consider two scenarios, that we call sparse and compact. In a sparse instance, we assume that each element of the input (as well as the output) occupies the last word of a distinct block, with the rest of the block unused. In a compact instance, the input as well as the output is presented in contiguous locations of the shared secondary memory. The compact presentation of an array of size occupies blocks.
Prefix Sums. The input is an array of elements, and the output is to be an array such that , for .
If the problem instance is sparse (with element occupying the last word of the -th disk block, with the remaining words of that block unused), we can use the EREW PRAM balanced binary tree algorithm on an EREW PEM without any change, save that each instruction in the PRAM algorithm should correspond to an I/O in the PEM algorithm. This algorithm, which we call PREFIX-SUMS-1, takes I/O rounds for a total of I/Os. Thus by Brent's Theorem, with processors, this can be run in I/O rounds.
Instead if the instance were compact, then we proceed in three stages. In stage 1, for , in parallel, find the local prefix-sums of the -th block of . In stage 2, invoke PREFIX-SUMS-1 on the block sums. In stage 3, for , in parallel, offset the local prefix sums of block (computed in stage 1) with the global prefix sum of block (computed in stage 2) and write the values in . We call this above algorithm PREFIX-SUMS-2. It takes I/O rounds for a total of I/Os. Thus by Brent's Theorem, with processors, PREFIX-SUMS-2 can be run in I/O rounds.
This bound improves on the bound in [5] because when , the two bounds are identical, and when , .
G. Sajith, M. K. Panda and R. Senko
Searching. If the input array has a sparse presentation, to find the rank of element in , on a CREW PEM with processors, initialize to 0, and then for , in parallel, if , set to . This algorithm, which we call SEARCH-1, runs in I/O rounds with processors on a CREW PEM.
Again, if has a sparse presentation, with the occupied blocks spaced evenly in the shared external memory, on a CREW PEM with processors, we would use a straightforward generalization of the ordinary binary search. If , then return SEARCH-1. Otherwise, divide into segments of size each; in parallel, compare with the last element of each segment, thus identifying the segment in which falls; and recurse with that segment. The search space reduces by a factor of in each I/O round. Therefore, this algorithm, which we call SEARCH-2, runs in I/O rounds.
If has a compact presentation, then first find the block that contains using SEARCH-2, and then search for within that block in one additional I/O. This algorithm, which we call SEARCH-3, runs in I/O rounds on a CREW PEM with processors.
Merging. Suppose two sorted arrays and of sizes and are given in sparse presentation, with the occupied blocks spaced equally in the shared external memory. We can find, in parallel, for , the rank of in , and for , the rank of in , using SEARCH-2, with processors per item in I/O rounds, where . We call this algorithm CROSS-RANK.
To merge and we can use Valiant's merge algorithm [18]. Divide (resp., ) into segments each of size (resp., ). Elect the first element from each segment as a bristle. Cross rank the bristles in I/O rounds using CROSS-RANK; this requires only processors. For each bristle in (resp. ), search the segment in (resp. ) in which it falls using (resp. ) processors. Now the problem is partitioned. Solve each partition (of a size at most ) recursively. The number of I/O rounds taken here is for some constants and . So, . The number of processors is linear. We call this algorithm MERGE-1.
Using MERGE-1, an cost I/O rounds CREW PEM algorithm for merging can be designed [18]. Divide and into segments of size each. Elect the first element from each segment as a bristle. Cross rank the bristles in I/Os using MERGE-1; this requires only processors. For each bristle in (resp. ), search the segment in (resp. ) using one processor. Now the problem is partitioned. Solve each partition (of a size at most ) sequentially. We call this algorithm MERGE-2.
Now we describe an algorithm we call MERGE-3, in which the input arrays are assumed to be in compact presentation. From each block of each array choose the first element as a bristle. Say, and are the arrays of bristles from and respectively. Arrays and are then cross-ranked using MERGE-2. Now each bristle knows the block on the other side in which it falls. For each bristle search this block to find its rank on the other side. We say, this is where a bristle “reflects” in the other array. As in MERGE-2, entrust to each bristle the items consecutive in its own array from itself to either a bristle or a reflection of a bristle whichever appears first, and the items consecutive in the other array from the reflection of to either a bristle or a reflection of a bristle whichever appears first. Each bristle is entrusted a maximum of items. Let each bristle merge its entrusted elements sequentially.
If a bristle is entrusted elements from its own array and items from the other array, then the smallest bristle larger than gets at least items, and the largest bristle smaller than gets at least items. This means that in the target array , a block can contain items entrusted to at most three bristles. Once the bristles merge their entrusted elements in the main memory, therefore, writing them onto the external memory will take at most three I/O rounds with processors. So, with processors, MERGE-3 runs in I/O rounds.
Compaction. Suppose a sequence of elements is given in an array of blocks in the shared external memory, allowing an arbitrary number of empty memory locations between two consecutive elements. Using a balanced binary tree algorithm can be compacted into a single block in I/O rounds for a total of I/Os on an EREW PEM. We call this algorithm COMPACT-1.
If instead of , the sequence has a length of , then the following algorithm can be used.
COMPACT-2
Input: A sequence of elements given in that order in an array of blocks in the shared external memory. There could be an arbitrary number of empty memory locations between two consecutive elements.
Output: An array of blocks that contains in that order.
REMARK: In the following, and are arrays, initialized to zero, in sparse presentation.
Step 1: For , let hold the number of elements in block . Invoke PREFIX-SUMS-1 on . Let hold the prefix-sums. Let .
Step 2: pardo for , if there exists a positive integer such that , (as , there exists at most one such ) then set boolean , move the first elements of into , set to and set to 1.
REMARK: If an element of is to be the last element of a block of , then the elements of that precede along with are moved into and deleted from . Now is the first block with elements to be moved into the -st block of .
Step 3: Compute the PREFIX-MAXIMA on into .
REMARK: The elements remaining in are to be moved to the -th block of . For , the elements in are to be moved to the end of the -st block of .
Step 4: Let ; pardo for , . Compute the PREFIX-MAXIMA of into .
REMARK: If , then is the first block to contribute elements to the block of in .
Step 5: Set , and pardo for , if , then set .
REMARK: is to be the -th block participating in the -th block of .
Step 6: Parallely invoke instances of COMPACT-1; the output of the -th instance will be written to .
Step 7: pardo for , if , then append to the elements already present in block .
Steps 2, 5 and 7 takes I/O rounds, while the remaining take I/O rounds. The cost of each I/O round is I/Os. Thus by Brent's Theorem, with processors, COMPACT-2 can be run in I/O rounds. If , then with processors, COMPACT-2 can be run in I/O rounds.
Problem of Section 2. Using COMPACT-2, PREFIX-SUMS-1, PREFIX-SUMS-2 and MERGE-3, we obtain the following lemma:
Lemma 6. On a CREW PEM with processors, the problem can be solved in I/O rounds, if the input is sparse; and I/O rounds, if the input is compact.
PROOF. Please refer the solution to presented in Section 2. If the input array is compact, then keep arrays and all in compact form; the bound follows. If is sparse, then invoke COMPACT-2 to get a compacted version of it; then proceed as above.
G. Sajith, M. K. Panda and R. Senko
Procedure Stratify can be used to design efficient algorithms for sorting and multiway-merging on CREW PEM.
6.2 Procedure Stratify on CREW PEM with and
Consider the procedure call “STRATIFY ”, where are sorted arrays of sizes , given contiguously one after another in that order in the shared external memory. Without loss of generality, assume that each block of the input spans over at most three of the input arrays, because, if a block spans , with and spanned partially, for , then can all be sorted together in place in one I/O by a single processor. Therefore, .
We can both partition the input arrays and form the bristle arrays in I/O rounds, using processors, once they are assigned one per block. (With , at most three bristles can be selected from every block; if a block spans and , the last elements of all of them could be bristles and belong to it.) By Lemma 6, with processors, this takes I/O rounds. An invocation of prefix sums over can be used to put the bristle arrays in sparse presentation, with the bristles distributed one per block. Cross-ranking of the bristle arrays involves merges, each of size at most , to be performed in parallel. Each bristle participates in merges. Use Lemma 1 to assign processors per bristle; -th of those processors will participate in each of the merges of the bristle. Solve each merge instance independently using MERGE-2. The time taken is I/O rounds.
Steps 2 and 4 each requires every bristle to perform an addition of size k, of bit numbers, which are in sparse presentation. There are s bristles. With processors per bristle, find the sums using PREFIX-SUMS-1 in I/O rounds.
Each bristle has to perform searches in Step 3, each over a segment of size ; that is, a total of searches have to be performed in parallel. At most processors are available per search. Hence, using SEARCH-3, we can finish this step in I/O rounds.
Alternatively, with processors, an array of size can be searched in I/O rounds. With processors, this search can be simulated in I/O rounds.
In Step 5 we rebalance the bristles so that the number of elements associated with each surviving bristle is . The value of a bristle is , the number of elements in its charge. We classify the bristles as follows: (i) small: bristles with value less than , (ii) large: all remaining bristles. If a small bristle is flanked by large bristles on both sides, then move its elements to the smaller of the two large bristles. The receiving bristle would then have at most elements in its charge. Partition into segments of size and assign processors to each segment. Within each segment we consider every maximal contiguous run of small bristles and compute the prefix sums of their values. Within these runs, we upgrade certain small bristles to large so that the number of elements between consecutive large bristles becomes . Whenever a prefix sum first reaches or exceeds a multiple of , the corresponding bristle is labelled large. Formally, for every pair of consecutive prefix sums satisfying for some integer , the bristle corresponding to is labelled large; in this case, we say causes to be labelled large. Suppose integer causes a bristle to be labelled large. Consider the first two prefix sums that are labelled large. The smaller of them is in , whereas the prefix sum immediately preceding the larger of them is in . It follows that the value accumulated between consecutive large bristles lies in the range . Remove all small bristles, and compact the remaining ones to form . With , the entire step requires I/O rounds.
The subtractions in Step 6 can be performed in I/O rounds with s processors. With processors, this would take I/O rounds. Step 6 also requires every bristle to perform a prefix summation of size , of bit numbers. With a processor allocation similar to that in Step 2, the sums can be found in I/O rounds. Since the bristles are now merged into the array , the allocation incurs only time.
In Step 6, we also prepare the subarrays . Here we proceed as we did in the case of CREW PRAM; however some modifications are necessary to respect block boundaries. When some elements of a block are to be included in , we would initially copy the entire block. For bristle , for , the elements that are strictly between and would span at most blocks. (For brevity, let denote .) Therefore, the number of elements copied into is at most . Over all the bristles, the number of elements copied is at most . Divide the available processors into two equal lots. From one lot assign processors to each bristle. As in Step 1, distribute the other lot over the bristles so that gets processors. Thus, gets a total of processors. The number of I/O rounds for the copying is . These arrays have their unwanted elements marked and compacted out next. With the same processor distribution this would take I/O rounds. Since , Step 6 would take I/O rounds.
In Step 7, we copy the 's into to form the 's. Since each of them has a size of at least , the copying can be done in I/O rounds; the second term signifies the cost of handling the fractional blocks in parallel.
Lemma 7. On a PEM that allows concurrent reads, the procedure call “STRATIFY ” takes I/O rounds with processors, while “STRATIFY ” takes I/O rounds with processors,
PROOF. If , then use first implementation of Step 3; otherwise, use the second one.
6.3 Sorting on CREW PEM
Consider Procedure SLICE_AND_STRATIFY described in Section 4.1. From Lemma 7 it follows that an invocation SLICE_AND_STRATIFY(N, k, A, B, kB) of it runs in I/O rounds with processors on a PEM that allows concurrent reads, where .
We now give the construction of a sorting scheme as before. The basic building block of the scheme is MERGE-3 of Subsection 6.1; this merges two arrays of a total size in time with processors on a CREW PEM; let be the constant hidden by the -notation here. Guided by a complete binary tree on leaves, proceeding bottom up, and using this algorithm at each level, the standard merge sort thus runs in time on an processor CREW PEM. This is the first algorithm in our sequence. We denote this algorithm by .
For , is given below. We use the notation introduced in the previous sections. For level , define the -th subarray of size in by , where and . Each subarray at level is composed of consecutive subarrays at level : . Thus, with one processor per input block, for , the procedure call “SLICE_AND_STRATIFY” takes time on a CREW PEM. Let be the time taken by with processors. Then, for ,
G. Sajith, M. K. Panda and R. Senko
Procedure PEM_SORT_h(I, N) begin Let ; , where ; for in parallel do /* */ PEM_SORT_h-1(); for to do begin for in parallel do begin SLICE_AND_STRATIFY(); for in parallel do PEM_SORT_h-1(); end end end end
Therefore, the rest of the analysis can proceed as in the case of CREW PRAM.
Thus, we have the following theorem:
Theorem 3. Using one of the procedures PEM_SORT_2h(I, n), for , an array of size can be sorted with processors, on a CREW PEM, in time.
Arge et al. [5] give bounds for sorting in the PEM model with processors, including a lower bound of parallel I/Os. Aggarwal and Vitter [1] show a lower bound of in the external memory model, implying in PEM. Thus, the combined lower bound is
For , this is . Our algorithm matches this bound for . The upper bounds of Arge et al. [5] are tight only for ; hence, our algorithm is faster at higher parallelism.
6.4 Multiway-merging on CREW PEM
Given sorted arrays of sizes , contiguously one after another in that order in the shared external memory, to merge them into a single sorted array with processors proceed as follows:
Invoke the procedure call "STRATIFY()". The output array has (not necessarily sorted) subarrays of size , for , given one after another contiguously, so that for all and all , each element of is larger than every element of . If subarrays is sorted in place, then would be sorted, as required. Suppose processors are assigned to the -th subarray. The sorting can be completed in I/O rounds.
Therefore, we have the following lemma:
Lemma 8. Given sorted arrays of sizes , contiguously one after another in that order in the shared external memory, the merge of them into a single sorted array can be computed in
I/O rounds with processors on a PEM that allows concurrent reads.
Corollary 1. If and , then by Corollary 4 of [5], the above bound becomes
I/O rounds with processors on a PEM that allows concurrent reads.
Corollary 2. If the sorting algorithm of Theorem 3 is used, then the above bound becomes
I/O rounds with processors on a PEM that allows concurrent reads.
7 Conclusion
In this paper, we have presented efficient parallel algorithms for the sorting problem under three different computational models. Our main results are the following:
(1) an time algorithm for sorting using processors on a CREW PRAM.
(2) an time algorithm for sorting using processors on the Parallel Comparison Model (PCM), where .
(3) an algorithm for sorting in the Parallel External Memory (PEM) model with processors that requires I/O rounds under the CREW constraint.
Recursive versions of the first two algorithms, along with closing the gap between the lower and upper bounds in the PEM model, remain directions for future work.
References
[1] Alok Aggarwal and Jeffrey S. Vitter. 1988. The Input/Output Complexity of Sorting and Related Problems. Commun. ACM 31, 9 (sep 1988), 1116–1127. doi:10.1145/48529.48535
[2] Alfred V. Aho, John E. Hopcroft, and Jeffrey D. Ullman. 1974. The Design and Analysis of Computer Algorithms. Addison-Wesley, Reading, MA, USA.
[3] Miklós Ajtai, János Komlós, and Endre Szemerédi. 1983. Sorting in c log n parallel steps. Combinatorica 3 (1983), 1–48. doi:10.1007/BF02579338
[4] Deepak Ajwani, Nodari Sitchinava, and Norbert Zeh. 2010. Geometric algorithms for private-cache chip multiprocessors. In Proceedings of the 18th European Symposium on Algorithms (ESA '10). 75–86. doi:10.1007/978-3-642-15781-3_7
[5] Lars Arge, Michael T. Goodrich, Michael Nelson, and Nodari Sitchinava. 2008. Fundamental Parallel Algorithms for Private-Cache Chip Multiprocessors. In Proceedings of the Twentieth Annual Symposium on Parallelism in Algorithms and Architectures (Munich, Germany) (SPAA '08). 197–206. doi:10.1145/1378533.1378573
[6] Lars Arge, Michael T. Goodrich, and Nodari Sitchinava. 2010. Parallel External Memory Graph Algorithms. In 2010 IEEE International Symposium on Parallel and Distributed Processing (IPDPS). 1–11. doi:10.1109/IPDPS.2010.5470440
[7] Yossi Azar and Uzi Vishkin. 1987. Tight Comparison Bounds on the Complexity of Parallel Sorting. SIAM J. Comput. 16 (1987), 458–464. doi:10.1137/0216032
[8] Paul W. Beame and Johan Håstad. 1989. Optimal bounds for decision problems on the CRCW PRAM. J. ACM 36, 3 (1989), 643–670. doi:10.1145/28395.28405
[9] Allan B. Borodin and John E. Hopcroft. 1985. Routing, Merging and Sorting on Parallel Models of Computation. J. Comput. System Sci. 30 (1985), 130–145. doi:10.1016/0022-0000(85)90008-X
[10] Richard Cole. 1988. Parallel Merge Sort. SIAM J. Comput. 17, 4 (1988), 770–785. doi:10.1137/0217049
G. Sajith, M. K. Panda and R. Senko
[11] Richard Cole and Vijaya Ramachandran. 2017. Resource Oblivious Sorting on Multicores. ACM Transactions on Parallel Computing 3, 4 (mar 2017), 23:1–23:31. doi:10.1145/3040221
[12] Stephen Cook, Cynthia Dwork, and Rüdiger Reischuk. 1986. Upper and lower time bounds for parallel random access machines without simultaneous writes. SIAM J. Comput. 15 (1986), 87–97. doi:10.1137/0215006
[13] Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. 2009. Introduction to Algorithms (3 ed.). MIT Press.
[14] Robert Cypher and Jorge L. C. Sanz. 1992. Cubesort: a parallel algorithm for sorting n data items with s-sorters. Journal of Algorithms 13 (1992), 211–234. doi:10.1016/0196-6774(92)90016-6
[15] Torben Hagerup and Rajeev Raman. 1993. Fast deterministic approximate and exact parallel sorting. In Proceedings of the 5th Annual ACM Symposium on Parallel Algorithms and Architectures (SPAA). ACM, 1–10. doi:10.1145/165231.157380
[16] Torben Hagerup and Christine Rüb. 1989. Optimal Merging and Sorting on the EREW PRAM. Inform. Process. Lett. 33, 4 (1989), 181–185. doi:10.1016/0020-0190(89)90138-5
[17] Riko Jacob, Tobias Lieber, and Nodari Sitchinava. 2014. On the Complexity of List Ranking in the Parallel External Memory Model. In Mathematical Foundations of Computer Science 2014. Springer Berlin Heidelberg, 384–395. doi:10.1007/978-3-662-44465-8_33
[18] Joseph Jája. 1992. An Introduction to Parallel Algorithms. Addison-Wesley, Redwood City, CA, USA.
[19] Donald E. Knuth. 1973. Sorting and Searching. Addison-Wesley.
[20] C. P. Kruskal. 1983. Searching, Merging, and Sorting in Parallel Computation. IEEE Trans. Comput. C-32 (1983), 942–946. doi:10.1109/TC.1983.1676138
[21] De-Lei Lee and Kenneth E. Batcher. 1994. A Multiway Merging Network. In Proc. 5th Intl. Symp. on Algorithms and Computation (ISAAC) (Lecture Notes in Computer Science, Vol. 834). Springer-Verlag, 643–651. doi:10.1007/3-540-58325-4_233
[22] Frank Thomson Leighton. 1985. Tight Bounds on the Complexity of Parallel Sorting. IEEE Trans. Comput. C-34, 4 (1985), 344–354. doi:10.1109/TC.1985.5009385
[23] Tobias Lieber. 2014. On Optimal Algorithms for List Ranking in the Parallel External Memory Model with Applications to Treewidth and other Elementary Graph Problems. Ph.D. Dissertation. ETH Zürich, Zürich. doi:10.3929/ethz-a-010361860
[24] Ian Parberry. 1987. Parallel Complexity Theory. John Wiley & Sons.
[25] Nicholas J. Pippenger. 1990. Communication networks. In Handbook of Theoretical Computer Science, J. van Leeuwen (Ed.). Elsevier Science Publishers.
[26] G. Sajith. 1997. Parallel Algorithms for Multiway Merging, Sorting and Graph Colouring. Ph.D. Dissertation. Indian Institute of Technology Kanpur.
[27] G. Sajith and Sanjay Saxena. 1997. Optimal Multiway Merging and Near Optimal Sorting on CREW PRAM and PCM. Technical Report. Indian Institute of Technology Kanpur. Unpublished manuscript.
[28] G. Sajith and Sanjeev Saxena. 2000. Optimal Sublogarithmic Time Parallel Algorithms on Rooted Forests. Algorithmica 27, 2 (2000), 187–197. doi:10.1007/s004530010012
[29] Nodari Sitchinava. 2012. Computational Geometry in the Parallel External Memory Model. SIGSPATIAL Special 4, 2 (2012), 18–23. doi:10.1145/2367574.2367578
[30] Nodari Sitchinava and Norbert Zeh. 2012. A Parallel Buffer Tree. In Proc. 24th ACM Symp. on Parallelism in Algorithms and Architectures (SPAA). ACM, 214–223. doi:10.1145/2312005.2312046
[31] Marc Snir. 1985. On Parallel Searching. SIAM J. Comput. 14 (1985), 688–708. doi:10.1137/0214051
[32] Leslie G. Valiant. 1975. Parallelism in Comparison Problems. SIAM J. Comput. 4, 3 (1975), 348–355. doi:10.1137/0204030
[33] Zhaofang Wen. 1996. Multiway Merging in Parallel. IEEE Transactions on Parallel and Distributed Systems 7, 1 (1996), 11–17. doi:10.1109/71.481593