+ All Categories
Home > Documents > Author Name - Computer & Information Science & Engineeringsahni/papers/c.pdfA glucose clamp...

Author Name - Computer & Information Science & Engineeringsahni/papers/c.pdfA glucose clamp...

Date post: 22-Oct-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
96
Author Name Book title goes here
Transcript
  • Author Name

    Book title goes here

  • 2

  • Foreward

    I am delighted to introduce the first book on Multimedia Data Mining. WhenI came to know about this book project undertaken by two of the most activeyoung researchers in the field, I was pleased that this book is coming in earlystage of a field that will need it more than most fields do. In most emergingresearch fields, a book can play a significant role in bringing some maturity tothe field. Research fields advance through research papers. In research papers,however, only a limited perspective could be provided about the field, itsapplication potential, and the techniques required and already developed inthe field. A book gives such a chance. I liked the idea that there will be a bookthat will try to unify the field by bringing in disparate topics already availablein several papers that are not easy to find and understand. I was supportiveof this book project even before I had seen any material on it. The projectwas a brilliant and a bold idea by two active researchers. Now that I have iton my screen, it appears to be even a better idea.

    Multimedia started gaining recognition in 1990s as a field. Processing,storage, communication, and capture and display technologies had advancedenough that researchers and technologists started building approaches to com-bine information in multiple types of signals such as audio, images, video, andtext. Multimedia computing and communication techniques recognize corre-lated information in multiple sources as well as insufficiency of information inany individual source. By properly selecting sources to provide complemen-tary information, such systems aspire, much like human perception system,to create a holistic picture of a situation using only partial information fromseparate sources.

    Data mining is a direct outgrowth of progress in data storage and process-ing speeds. When it became possible to store large volume of data and rundifferent statistical computations to explore all possible and even unlikely cor-relations among data, the field of data mining was born. Data mining allowedpeople to hypothesize relationships among data entities and explore supportfor those. This field has been put to applications in many diverse domains andkeeps getting more applications. In fact many new fields are direct outgrowthof data mining and it is likely to become a powerful computational tool.

    i

  • Preface

    Approximately 17 million people in the USA (6% of the population) and 140million people worldwide (this number is expected to rise to almost 300 mil-lion by the year 2025) suffer from diabetes mellitus. Currently, there a fewdozens of commercialised devices for detecting blood glucose levels [1]. How-ever, most of them are invasive. The development of a noninvasive methodwould considerably improve the quality of life for diabetic patients, facilitatetheir compliance for glucose monitoring, and reduce complications and mor-tality associated with this disease. Noninvasive and continuous monitoring ofglucose concentration in blood and tissues is one of the most challenging andexciting applications of optics in medicine. The major difficulty in develop-ment and clinical application of optical noninvasive blood glucose sensors isassociated with very low signal produced by glucose molecules. This results inlow sensitivity and specificity of glucose monitoring by optical methods andneeds a lot of efforts to overcome this difficulty.

    A wide range of optical technologies have been designed in attempts todevelop robust noninvasive methods for glucose sensing. The methods includeinfrared absorption, near-infrared scattering, Raman, fluorescent, and thermalgradient spectroscopies, as well as polarimetric, polarization heterodyning,photonic crystal, optoacoustic, optothermal, and optical coherence tomogra-phy (OCT) techniques [1-31].

    For example, the polarimetric quantification of glucose is based on thephenomenon of optical rotatory dispersion, whereby a chiral molecule in anaqueous solution rotates the plane of linearly polarized light passing throughthe solution. The angle of rotation depends linearly on the concentration of thechiral species, the pathlength through the sample, and the molecule specificrotation. However, polarization sensitive optical technique makes it difficultto measure in vivo glucose concentration in blood through the skin because ofthe strong light scattering which causes light depolarization. For this reason,the anterior chamber of the eye has been suggested as a sight well suited forpolarimetric measurements, since scattering in the eye is generally very lowcompared to that in other tissues, and a high correlation exists between theglucose in the blood and in the aqueous humor. The high accuracy of anterioreye chamber measurements is also due to the low concentration of opticallyactive aqueous proteins within the aqueous humor.

    On the other hand, the concept of noninvasive blood glucose sensing usingthe scattering properties of blood and tissues as an alternative to spectral

    iii

  • iv

    absorption and polarization methods for monitoring of physiological glucoseconcentrations in diabetic patients has been under intensive discussion forthe last decade. Many of the considered effects, such as changing of the size,refractive index, packing, and aggregation of RBC under glucose variation, areimportant for glucose monitoring in diabetic patients. Indeed, at physiologicalconcentrations of glucose, ranging from 40 to 400 mg/dl, the role of some of theeffects may be modified, and some other effects, such as glucose penetrationinside the RBC and the followed hemoglobin glycation, may be important[30-32].

    Noninvasive determination of glucose was attempted using light scatteringof skin tissue components measured by a spatially-resolved diffuse reflectanceor NIR frequency-domain reflectance techniques. Both approaches are basedon change in glucose concentration, which affects the refractive index mis-match between the interstitial fluid and tissue fibers, and hence reduces scat-tering coefficient. A glucose clamp experiment showed that reduced scatteringcoefficient measured in the visible range qualitatively tracked changes in bloodglucose concentration for the volunteer with diabetes studied.

  • List of Figures

    1.1 Architecture of the Cell Broadband Engine [9] . . . . . . . . 41.2 Comparisons for mySpu cmpswap skew . . . . . . . . . . . 71.3 Comb sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101.4 Single SPU column-major AA-sort [16] . . . . . . . . . . . . 111.5 Collecting numbers from a 4 × 4 matrix . . . . . . . . . . . . 111.6 Column major to row major . . . . . . . . . . . . . . . . . . 121.7 Column-major brick sort . . . . . . . . . . . . . . . . . . . . 131.8 Column-major shaker sort . . . . . . . . . . . . . . . . . . . 141.9 Merge sort example . . . . . . . . . . . . . . . . . . . . . . . . 151.10 Phase 2 of merge sort . . . . . . . . . . . . . . . . . . . . . . 161.11 Phase 3 counters . . . . . . . . . . . . . . . . . . . . . . . . . 181.12 Phase 3 of merge sort . . . . . . . . . . . . . . . . . . . . . . 191.13 Phase 4 counters . . . . . . . . . . . . . . . . . . . . . . . . . 211.14 Phase 4 of merge sort with counters in different columns . . 221.15 Plot of average time to sort 4-byte integers . . . . . . . . . . 241.16 SIMD odd-even merge of two vectors [16] . . . . . . . . . . . 251.17 SIMD 2-way merge of 2 vectors v1 and v2 . . . . . . . . . . 261.18 Plot of average time to sort 1 to 67 million integers . . . . . 271.19 The findmin operation for records . . . . . . . . . . . . . . 291.20 fields select operation in ByField layout . . . . . . . . . . . 291.21 fields select operation in ByRecord layout . . . . . . . . . . 291.22 Shuffling two records in ByField layout . . . . . . . . . . . . 301.23 Shuffling two records in ByRecord layout . . . . . . . . . . . 301.24 4-way merge . . . . . . . . . . . . . . . . . . . . . . . . . . . 311.25 4-way merge . . . . . . . . . . . . . . . . . . . . . . . . . . . 321.26 SIMD 2-way merge of 2 vectors v1 and v2 . . . . . . . . . . 331.27 2-way Sorts (ByField) . . . . . . . . . . . . . . . . . . . . . 351.28 4-way sorts (ByField) . . . . . . . . . . . . . . . . . . . . . . 361.29 2-way and 4-way Sorts (ByField), 1M records . . . . . . . . 371.30 4-way sorts (ByField), 1M records . . . . . . . . . . . . . . . 381.31 2-way Sorts (ByRecord) . . . . . . . . . . . . . . . . . . . . 391.32 4-way sorts (ByRecord) . . . . . . . . . . . . . . . . . . . . . 401.33 2-way and 4-way Sorts (ByRecord), 1M records . . . . . . . 411.34 4-way sorts (ByRecord), 1M records . . . . . . . . . . . . . . 421.35 4-way sorts using the best algorithms for different layouts . . 43

    v

  • vi

    2.1 NVIDIA’s Tesla GPU [26] . . . . . . . . . . . . . . . . . . . 462.2 Cuda programming model [34] . . . . . . . . . . . . . . . . . 472.3 Bit-split scheme for sorting numbers on 4 bits [5] . . . . . . . 512.4 Divergence free warp scan algorithm . . . . . . . . . . . . . . 522.5 Warp scan of 8 numbers . . . . . . . . . . . . . . . . . . . . . 522.6 Calculating histogram offsets . . . . . . . . . . . . . . . . . . 532.7 Column major histogram . . . . . . . . . . . . . . . . . . . . 532.8 Algorithm to compute the histograms and ranks of 64 tiles . 562.9 Reading the numbers from global memory . . . . . . . . . . 572.10 Processing an element of sHist4[] . . . . . . . . . . . . . . 582.11 Writing the ranks to global memory . . . . . . . . . . . . . . 582.12 Writing the histograms to global memory . . . . . . . . . . . 592.13 Rearranging Data . . . . . . . . . . . . . . . . . . . . . . . . 602.14 Bottom Level Reduction . . . . . . . . . . . . . . . . . . . . 612.15 SRTS steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632.16 Serial sample sort . . . . . . . . . . . . . . . . . . . . . . . . 642.17 An iteration of GPU Sample sort . . . . . . . . . . . . . . . 662.18 Bitonic merge sort of 8 elements . . . . . . . . . . . . . . . . 672.19 Warpsort steps . . . . . . . . . . . . . . . . . . . . . . . . . . 682.20 Sorting numbers using radix sorts . . . . . . . . . . . . . . . 692.21 Moving records in ByField layout . . . . . . . . . . . . . . . 712.22 Moving records in ByRecord layout . . . . . . . . . . . . . . 712.23 Optimized version of moving records in ByRecord layout . . 722.24 Sorting records in ByField format . . . . . . . . . . . . . . . 762.25 Sorting records in Hybrid format . . . . . . . . . . . . . . . 77

  • List of Tables

    1.1 Comparison of various SPU sorting algorithms . . . . . . . . 23

    vii

  • viii

  • Contents

    I This is a Part 1

    1 Sorting On The Cell Broadband Engine 3Shibdas Bandyopadhyay, Dolly Sharma, Read A. Ammar, Sanguthevar Ra-

    jasekaran, and Sartaj Sahni1.1 The Cell Broadband Engine . . . . . . . . . . . . . . . . . . 41.2 High-level Strategies For Sorting . . . . . . . . . . . . . . . . 51.3 SPU Vector and Memory Operations . . . . . . . . . . . . . 51.4 Sorting Numbers . . . . . . . . . . . . . . . . . . . . . . . . . 8

    1.4.1 Single SPU sort . . . . . . . . . . . . . . . . . . . . . . 81.4.1.1 Shellsort variants . . . . . . . . . . . . . . . 91.4.1.2 Merge Sort . . . . . . . . . . . . . . . . . . . 141.4.1.3 Comparison of Single SPU sorting algorithms 21

    1.4.2 Hierarchical sort . . . . . . . . . . . . . . . . . . . . . 231.4.3 Master-slave sort . . . . . . . . . . . . . . . . . . . . . 27

    1.5 Sorting Records . . . . . . . . . . . . . . . . . . . . . . . . . 281.5.1 Record layout . . . . . . . . . . . . . . . . . . . . . . . 281.5.2 High-level strategies for sorting records . . . . . . . . 281.5.3 Single SPU record sorting . . . . . . . . . . . . . . . . 291.5.4 Hierarchical sorting for records . . . . . . . . . . . . . 30

    1.5.4.1 4-Way merge for records . . . . . . . . . . . 301.5.5 Comparison of record sorting algorithms . . . . . . . . 34

    1.5.5.1 Run times for ByField layout . . . . . . . . 351.5.5.2 Run times for ByRecord layout . . . . . . . 381.5.5.3 Cross layout comparison . . . . . . . . . . . 42

    2 Sorting On A Graphics Processing Unit(GPU) 45Shibdas Bandyopadhyay and Sartaj Sahni2.1 Graphics Processing Units . . . . . . . . . . . . . . . . . . . 462.2 Sorting Numbers on GPUs . . . . . . . . . . . . . . . . . . . 48

    2.2.1 SDK Radix Sort Algorithm . . . . . . . . . . . . . . . 502.2.1.1 Step 1–Sorting tiles . . . . . . . . . . . . . . 512.2.1.2 Step 2–Calculating histogram . . . . . . . . . 522.2.1.3 Step 3–Prefix sum of histogram . . . . . . . 532.2.1.4 Step 4–Rearrangement . . . . . . . . . . . . 54

    2.2.2 GPU Radix Sort(GRS) . . . . . . . . . . . . . . . . . 54

    ix

  • x

    2.2.2.1 Step 1–Histogram and Ranks . . . . . . . . . 552.2.2.2 Step 2–Prefix sum of tile histograms . . . . . 592.2.2.3 Step 3–Positioning numbers in a tile . . . . . 59

    2.2.3 SRTS Radix Sort . . . . . . . . . . . . . . . . . . . . . 592.2.3.1 Step 1–Bottom level reduce . . . . . . . . . . 622.2.3.2 Step 2–Top level scan . . . . . . . . . . . . . 622.2.3.3 Step 3–Bottom level scan . . . . . . . . . . . 62

    2.2.4 GPU Sample Sort . . . . . . . . . . . . . . . . . . . . 642.2.4.1 Step 1–Splitter selection . . . . . . . . . . . . 652.2.4.2 Step 2–Finding buckets . . . . . . . . . . . . 652.2.4.3 Step 3–Prefix sum . . . . . . . . . . . . . . . 652.2.4.4 Step 4–Placing elements into buckets . . . . 65

    2.2.5 Warpsort . . . . . . . . . . . . . . . . . . . . . . . . . 652.2.5.1 Step 1–Bitonic sort by warps . . . . . . . . . 652.2.5.2 Step 2–Bitonic merge by warps . . . . . . . . 662.2.5.3 Step 3–Splitting long sequences . . . . . . . . 672.2.5.4 Step 4–Final merge by warps . . . . . . . . . 67

    2.2.6 Comparison of number sorting algorithms . . . . . . . 682.3 Sorting Records on GPUs . . . . . . . . . . . . . . . . . . . . 69

    2.3.1 Record Layouts . . . . . . . . . . . . . . . . . . . . . . 692.3.2 High level Strategies for sorting records . . . . . . . . 702.3.3 Sample Sort For Sorting Records . . . . . . . . . . . . 702.3.4 SRTS For Sorting Records . . . . . . . . . . . . . . . . 712.3.5 GRS For Sorting Records . . . . . . . . . . . . . . . . 722.3.6 Comparison of record sorting algorithms . . . . . . . . 732.3.7 Run Times for ByField layout . . . . . . . . . . . . . 742.3.8 Run Times for Hybrid layout . . . . . . . . . . . . . . 75

    Bibliography 79

    Index 83

  • Symbol Description

    α To solve the generator main-tenance scheduling, in thepast, several mathematicaltechniques have been ap-plied.

    σ2 These include integer pro-gramming, integer linearprogramming, dynamic pro-gramming, branch andbound etc.∑Several heuristic search al-gorithms have also been de-veloped. In recent years ex-pert systems,

    abc fuzzy approaches, simulated

    annealing and genetic algo-rithms have also been tested.

    θ√

    abc This paper presents a surveyof the literature

    ζ over the past fifteen years inthe generator

    ∂ maintenance scheduling.The objective is to

    sdf present a clear picture of theavailable recent literature

    ewq of the problem, the con-straints and the other as-pects of

    bvcn the generator maintenanceschedule.

  • Part I

    This is a Part

    1

  • Chapter 1

    Sorting On The Cell BroadbandEngine

    Shibdas Bandyopadhyay

    University of Florida

    Dolly Sharma

    University of Connecticut

    Read A. Ammar

    University of Connecticut

    Sanguthevar Rajasekaran

    University of Connecticut

    Sartaj Sahni

    University of Florida

    1.1 The Cell Broadband Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.2 High-level Strategies For Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.3 SPU Vector and Memory Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51.4 Sorting Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

    1.4.1 Single SPU sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81.4.1.1 Shellsort variants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

    Comb and AA sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Brick sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Shaker sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

    1.4.1.2 Merge Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14Merge Sort Phase 1–Transpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15Merge Sort Phase 2–Sort Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15Merge Sort Phase 3–Merge Pairs of Columns . . . . . . . . . . . . . . . . . . 16Merge Sort Phase 4–Final Merge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

    1.4.1.3 Comparison of Single SPU sorting algorithms . . . . . . . . . . 211.4.2 Hierarchical sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231.4.3 Master-slave sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

    1.5 Sorting Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281.5.1 Record layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281.5.2 High-level strategies for sorting records . . . . . . . . . . . . . . . . . . . . . . . . . . . 281.5.3 Single SPU record sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291.5.4 Hierarchical sorting for records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

    1.5.4.1 4-Way merge for records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30Scalar 4-way merge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31SIMD 4-way merge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

    3

  • 4 Book title goes here

    1.5.5 Comparison of record sorting algorithms . . . . . . . . . . . . . . . . . . . . . . . . . 331.5.5.1 Run times for ByField layout . . . . . . . . . . . . . . . . . . . . . . . . . . . 341.5.5.2 Run times for ByRecord layout . . . . . . . . . . . . . . . . . . . . . . . . . 381.5.5.3 Cross layout comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

    1.1 The Cell Broadband Engine

    The Cell Broadband Engine (CBE) is a heterogeneous multicore architec-ture developed by IBM, Sony, and Toshiba. A CBE (Figure 1.1) consists of aPower PC (PPU) core, eight Synergistic Processing Elements or Units (SPEsor SPUs), and associated memory transfer mechanisms [14]. The SPUs areconnected in a ring topology and each SPU has its own local store. However,SPUs have no local cache and no branch prediction logic. Data may be movedbetween an SPUs local store and central memory via a DMA transfer, which ishandled by a Memory Flow Control (MFC). Since the MFC runs independentof the SPUs, data transfer can be done concurrently with computation. Theabsence of branch prediction logic in an SPU and the availability of SIMD in-structions that can operate on vectors that are comprised of 4 numbers posesa challenge when developing high performance CBE algorithms.

    FIGURE 1.1: Architecture of the Cell Broadband Engine [9]

  • Sorting On The Cell Broadband Engine 5

    1.2 High-level Strategies For Sorting

    As noted in [11], a logical way to develop a sorting algorithm for a hetero-geneous multicore computer such as the CBE is to (1) begin with a sortingalgorithm for a single SPU, then (2) using this as a core, develop a sort al-gorithm for the case when data fits in all available cores, then (3) use thismulti-SPU algorithm to develop a sort algorithm for the case when the datato be sorted does not fit in the local stores of all available SPEs but fits inmain memory. The strategy would be to extend this hierarchical plan to thecase where data to be sorted is so large that it is distributed over the mainmemories of a cluster of CBEs.

    An alternative strategy to sort is to use the master-slave model in which thePPU serves as the master processor and the SPUs are the slave processors. ThePPU partitions the data to be sorted and sends each partition to a differentSPU; the SPUs sort their partition using a single SPU sort; the PPU mergesthe sorted data from the SPUs so as to complete the sort of the entire dataset. This strategy is used in [32] to sort on the nCube hypercube and in [29]to sort on the CBE.

    Regardless of whether we sort large data sets using the hierarchical strat-egy of [11] or the master-slave strategy of [29, 32], it is important to have a fastalgorithm to sort within a single SPU. The absence of any branch predictioncapability and the availability of vector instructions that support SIMD par-allelism on an SPU make the development of efficient SPU sort algorithms achallenge. SPUs also have two instruction pipelines which make them capableof issuing two instructions in the same cycle if they fall in different pipelines.It pays to hand-tune the generated assembly code so that two instructions areissued in as many of the cycles as possible.

    1.3 SPU Vector and Memory Operations

    We shall use several SIMD functions that operate on a vector of 4 numbersto describe the SPU adaptation of sorting algorithms. We describe these inthis section. In the following, v1, v2, min, max, and temp are vectors, eachcomprised of 4 numbers and p, p1, and p2 are bit patterns.Also, dest (destina-tion) and src (source) are addresses in the local store of an SPU and bufferAis a buffer in local store while streamA is a data stream in main memory.Furthermore, Function names that begin with spu are standard C/C++ CellSPU intrinsics while those that begin with mySpu are defined by us. Ourdescription of these functions is tailored to the sorting application.

    1. spu shuffle(v1, v2, p) · · · This function returns a vector comprised of a

  • 6 Book title goes here

    subset of the 8 numbers in v1 and v2. The returned subset is determinedby the bit pattern p. Let W , X , Y , and Z denote the 4 numbers (left toright) of v1 and let A, B, C, and D denote those of v2. The bit patternp = XCCW , for example, returns a vector comprised of the secondnumber in v1 followed by two copies of the third number of v2 followedby the first number in v1. In the following, we assume that constantpatterns such as XYZD have been pre-defined.

    2. spu cmpgt(v1, v2) · · · A 128-bit vector representing the pairwise com-parison of the 4 numbers of v1 with those of v2 is returned. If an elementof v1 is greater than the corresponding element of v2, the corresponding32 bits of the returned vector are 1; otherwise, these bits are 0.

    3. spu add(v1, v2) · · · Returns the vector obtained by pairwise adding thenumbers of v1 with the corresponding numbers of v2.

    4. spu sub(v1, v2) · · · Returns the vector obtained by pairwise subtractingthe numbers of v2 from the corresponding numbers of v1.

    5. spu and(p1, p2) · · · Returns the vector obtained by pairwise anding thebits of p1 and p2.

    6. mySpu not(p) · · · Returns the vector obtained by complementing eachof the bits of p. Although the CBE does not have a not instruction,we can perform this operation using the nor function that is supportedby the CBE and which computes the complement of the bitwise or oftwo vectors. It is easy to see that spu nor(p, v0) where v0 is an all zerovector, correctly computes the complement of the bits of p.

    7. spu select(v1, v2, p) · · · Returns a vector whose ith bit comes from v1(v2) when the ith bit of p is 0 (1).

    8. spu slqwbyte(v1, n) · · · Returns a vector obtained by shifting the bytesof v1 m bytes to the left, where m is the number represented by the 5least significant bits of n. The left shift is done with zero fill. So, therightmost m bytes of the returned vector are 0.

    9. spu splat(s) · · · Returns a vector comprised of 4 copies of the numbers.

    10. mySpu cmpswap(v1, v2) · · · Pairwise compares the numbers of v1 andv2 and swaps so that v1 has the smaller number of each compare andv2 has the larger number Specifically, the following instructions are ex-ecuted:p = spu cmpgt(v1, v2);min = spu select(v1, v2, p);v2 = spu select(v2, v1, p);v1 = min;

  • Sorting On The Cell Broadband Engine 7

    11. mySpu cmpswap skew(v1, v2) · · · Performs the comparisons and swapsshown in Figure 1.2. Specifically, the following instructions are executed:temp = spu slqwbyte(v2, 4);p = spu cmpgt(v1, temp);min = spu select(v1, temp, p);v1 = spu shuffle(min, v1, WXY D);max = spu select(temp, v1, p);v2 = spu shuffle(max, v2, AWXY );

    Min

    Max

    v10 v11 v12 v13

    v21 v22 v23v20

    FIGURE 1.2: Comparisons for mySpu cmpswap skew

    12. mySpu gather(vArray, v1) · · · Here vArray is an array of vectors. LetW , X , Y and Z be the numbers of v1. The function returns a vectorwhose first number is the first number of vArray[W ], its second numberis the second number of vArray[X ], its third number is the third numberof vArray[Y ], and its fourth number is the fourth number of vArray[Z].One implementation of this function first extracts W , X , Y , and Z fromv1 using the function spu extract and then executes the code:temp = spu shuffle(vArray[W ], vArray[X ], WBWW );temp = spu shuffle(temp, vArray[Y ], WXCC);return spu shuffle(temp, vArray[Z], WXY D);

    13. mySpu gather12(vArray, v1) · · · This function, which is similar tomySpu gather, returns a vector whose first number is the first num-ber of vArray[W ] and whose second number is the second number ofvArray[X ]. The third and fourth numbers of the returned vector are setarbitrarily. Its code is:return spu shuffle(vArray[W ], vArray[X ], WBWW );

    14. mySpu gather34(vArray, v1) · · · This function, which is similar tomySpu gather12, returns a vector whose first number is the third num-ber of vArray[W ] and whose second number is the fourth number ofvArray[X ]. The third and fourth numbers of the returned vector are setarbitrarily. Its code is:

  • 8 Book title goes here

    return spu shuffle(vArray[W ], vArray[X ], Y DY Y );

    15. mySpu gatherA(vArray, v1) · · · This function is similar to mySpu gatherand returns a vector whose first number is the first number ofvArray[W ], its second number is the third number of vArray[X ], itsthird number is the first number of vArray[Y ], and its fourth numberis the third number of vArray[Z]. The code:temp = spu shuffle(vArray[W ], vArray[X ], WCWW );temp = spu shuffle(temp, vArray[Y ], WXAA);return spu shuffle(temp, vArray[Z], WXY C);

    16. mySpu gatherB(vArray, v1) · · · This too is similar to mySpu gather.The function returns a vector whose first number is the second numberof vArray[W ], its second number is the fourth number of vArray[X ], itsthird number is the second number of vArray[Y ], and its fourth numberis the fourth number of vArray[Z]. The code is:temp = spu shuffle(vArray[W ], vArray[X ], XDXX);temp = spu shuffle(temp, vArray[Y ], WXBB);return spu shuffle(temp, vArray[Z], WXY D);

    17. memcpy(dest, src, size) · · · copies the size number of bytes from thelocal store location beginning at src to dest.

    18. dmaIn(bufferA, streamA) · · · This function triggers a DMA transferof the next buffer load of data from streamA in main memory intobufferA in the local store. This is done asynchronously and concur-rently with SPU execution.

    19. dmaOut(bufferA, streamA) · · · This function is similar to dmaInexcept that a buffer load of data is transferred asynchronously frombufferA in the local store to streamA in main memory.

    1.4 Sorting Numbers

    1.4.1 Single SPU sort

    Recently, three sorting algorithms–AA-sort [16] ,CellSort [11] and Mergesort [3]–were proposed for the CBE. AA-sort is an adaptation of combsort, which was originally proposed by Knuth [17] and rediscovered by Do-bosiewicz [8] and Box and Lacey [6]. CellSort is an adaptation of bitonic sort

  • Sorting On The Cell Broadband Engine 9

    (e.g., [17]). Both AA-sort and CellSort are based on sorting algorithms thatare inefficient on a single processor. Hence, parallelizing these algorithms be-gins with a handicap relative to the fastest serial sorting algorithms–mergesort for worst-case behavior and quick sort for average behavior. Comb sortis known to have a worst-case complexity that is O(n2) [10]. Although thebest upper bound known for its average complexity is also O(n2), experimen-tal results indicate an average complexity of O(n log n) [6, 10]. On the otherhand, the average complexity of quick sort is known to be O(n log n). Sinceexperiments indicate that comb sort runs in about twice as much time on asingle processor as does quick sort [10], attempts such as [16] to develop a fastaverage-case sort, for a single SPU of the CBE that begin with comb sort, arehandicapped by a factor of two compared to attempts that begin with quicksort. This handicap is overcome by the CBE adaptation of the Merge sortdescribed in [3].

    For integers and floats, the CBE supports 4-way parallelism within a singleSPU as 4 integers (floats) may be stored in each of the SPU’s 128-bit vectorregisters. Hence, we expect at best two-fold speed up over a conventionalimplementation of quick sort. However, due to possible anomalous behaviorresulting from such factors as the absence of branch prediction, we may actu-ally observe a greater speed up [18]. Similarly, attempts such as [11] to developa fast worst-case sort for a single SPU starting with bitonic sort are handi-capped relative to starting with merge sort because the worst-case complexityof bitonic sort is O(n log2 n) while that of merge sort is O(n log n).

    1.4.1.1 Shellsort variants

    Shellsort [17] sorts a sequence of n numbers in m passes employing a de-creasing increment sequence i1 > i2 > · · · > im = 1. In the jth pass, incrementh = ij is used; the sequence is viewed as comprised of h subsequences with thekth subsequence comprised of the numbers in positions k, k+h, k+2h, · · · , ofthe overall sequence, 0 ≤ k < h; and each subsequence is sorted. The sortingof the subsequences done in each pass is called an h-sort. While an h-sort istypically accomplished using insertion sort, other simple sorting algorithmssuch as bubble sort may also be used. With the proper choice of increments,the complexity of Shellsort is O(n log2 n) [17]. Shellsort variants replace theh-sort used in each pass of Shellsort with an h-pass that only partially sortsthe subsequences. For example, in an h-bubble pass we make only the firstpass of bubble sort on each subsequence. Since replacing h-sort by h-pass inShellsort no longer guarantees a complete sort, we follow with some simplesorting algorithm such as bubble sort to complete the sort. So, the h-passesmay be viewed as preprocessing passes done so as to improve the performanceof the ensuing sort algorithm In Shellsort, im = 1 is used to assure that thesequence is sorted following the final h-sort. However, in a Shellsort variant,this assurance comes from the sort algorithm run following the preprocessing

  • 10 Book title goes here

    h-passes. So, the h-pass with h = 1 is typically skipped. The general structureof a Shellsort variant is:

    Step 1 [Preprocess] Perform h-passes for h = ij, 1 ≤ j < m.

    Step 2 [Sort] Sort the preprocessed sequence

    Comb and AA sort

    Knuth [17] proposed a Shellsort variant in which each h-pass is a bubblepass (Figure 1.3). This variant was rediscovered later by Dobosiewicz [8] andBox and Lacey [6] named this variant comb sort. The increment sequenceused by comb sort is geometric with factor s. Dobosiewicz [8] has shown thatthe preprocessing step sorts a[0 : n − 1] with very high probability whenevers < 1.33. As a result, s = 1.3 is recommended in practice (note that a larger srequires a smaller number of h-passes). With this choice, the outer for loop ofthe second step (bubble sort) is entered only once with high probability andthe complexity of comb sort is O(n log n) with high probability. Experimentsindicate that the algorithm’s average run time is close to that of quick sort[10]. However, the worst-case complexity of comb sort is O(n2) [10].

    Algorithm combsort(a,n){// sort a[0:n-1]

    // Step 1: Preprocessingfor (h = n/s; h > 1; h /= s) {

    // h-bubble passfor (i = 0; i < n-h; i++)

    if (a[i] > a[i+h]) swap(a[i],a[i+h]);}sorted = false;// Step 2: Bubble sortfor (pass = 1; pass < n && !sorted; pass++) {

    sorted = true;for (i = 0; i < n-pass; i++)

    if (a[i] > a[i+1]) {swap(a[i],a[i+1]); sorted = false;}}

    }

    FIGURE 1.3: Comb sort

    Inoue et al. [16] have adapted comb sort to the CBE to obtain the sortmethod AA-sort, which efficiently sorts numbers using all 8 SPUs of a CBE.The single SPU version begins with a vector array d[0 : r−1] of numbers; eachvector d[i] has 4 numbers. Hence, d is an r×4 matrix of numbers. This matrixis first sorted into column-major order and then the numbers permuted so as tobe sorted in row-major order. Figure 1.4 gives the algorithm for the column-

  • Sorting On The Cell Broadband Engine 11

    major sort and Figure 1.6 gives the column-major to row-major reorderingalgorithm.

    Algorithm AA(d,r){// sort d[0:r-1] into column-major order

    // Step 1: Preprocessingfor (i = 0; i < r; i++) sort(d[i]);for (h = r; h > 1; h /= s) {

    // h-bubble passfor (i = 0; i < r-h; i++)

    mySpu cmpswap(d[i],d[i+h]);for (i = r-h; i < r; i++)

    mySpu cmpswap skew(d[i],d[i+h-r]);}sorted = false;// Step 2: Bubble sortdo {

    for (i = 0; i < r-1; i++)mySpu cmpswap(d[i],d[i+1]);

    mySpu cmpswap skew(d[r-1],d[0]);} while (not sorted)

    }

    FIGURE 1.4: Single SPU column-major AA-sort [16]

    The column-major to row-major reordering is done in two steps. In thefirst step, the numbers in each 4×4 submatrix of the r×4 matrix of numbersare transposed so that each vector now has the 4 numbers in some row ofthe result. For simplicity, we assume that r is a multiple of 4. In the secondstep, the vectors are permuted into the correct order. For the first step, wecollect the first and second numbers in rows 0 and 2 of the 4× 4 matrix beingtransposed into the vector row02A. The third and fourth numbers of thesetwo rows are collected into row02b. The same is done for rows 1 and 3 usingvectors row13A and row13B. Figure 1.5 shows this rearrangement. Then, thetranspose is constructed from the just computed 4 vectors.

    FIGURE 1.5: Collecting numbers from a 4 × 4 matrix

  • 12 Book title goes here

    Algorithm transpose(d,r){// Column major to row major reordering

    // Step 1: Transpose 4 x 4 submatricesfor (i = 0; i < r; i += 4) {

    // Compute row02A, row02B, row13A, and row13Brow02A = spu shuffle(d[i], d[i+2], WAXB);row02B = spu shuffle(d[i], d[i+2], YCZD);row13A = spu shuffle(d[i+1], d[i+3], WAXB);row13B = spu shuffle(d[i+1], d[i+3], YCZD);// Complete the transposed[i] = spu shuffle(row02A, row13A, WAXB);d[i+1] = spu shuffle(row02A, row13A, YCZD);d[i+2] = spu shuffle(row02B, row13B, WAXB);d[i+3] = spu shuffle(row02B, row13B, YCZD);

    }// Step 2: Reorder vectorsfor (i = 0; i < r; i++)

    if (!inPlace[i]) {current = i;next = i/(r/4) + (i mod (r/4))*4;temp = d[i];while (next != i) {// follow cycle

    d[current] = d[next];inPlace[current] = true;current = next;next = current/(r/4) + (current mod (r/4))*4;

    }d[current] = temp;inPlace[current] = true;

    }}

    FIGURE 1.6: Column major to row major

  • Sorting On The Cell Broadband Engine 13

    Brick sort

    In brick sort, we replace the h-bubble pass of comb sort by an h-brick pass[21, 24] in which we first compare-exchange positions i, i + 2h, i + 4h, · · ·with positions i + h, i + 3h, i + 5h, · · · , 0 ≤ i < h and then we compare-exchange positions i+h, i+3h, i+5h, · · · with positions i+2h, i+4h, i+6h,· · · , 0 ≤ i < h. Figure 1.7 gives our CBE adaptation of the preprocessing step(Step 1) for brick sort. Step 2 is a bubble sort as was the case for AA-sort. Thebubble sort needs to be followed by a column-major to row-major reorderingstep (Figure 1.6). It is known that the preprocessing step of brick sort nearlyalways does a complete sort when the increment sequence is geometric withshrink factor (i.e., s) less than 1.22 [21, 24]. Hence, when we use s < 1.22, thedo-while loop of Step 2 (bubble sort) is entered only once (to verify the dataare sorted) with high probability.

    Algorithm Brick(d,r){// sort d[0:r-1] into column-major order

    // Step 1: Preprocessingfor (i = 0; i < r; i++) sort(d[i]);for (h = r; h > 1; h /= s) {

    // h-brick pass// compare-exchange even:odd bricksfor (i = 0; i < r-2*h; i += 2*h)

    for (j = i; j < i + h; j++)mySpu cmpswap(d[j],d[j+h]);

    // handle end conditionsif (j < n - h) {// More than 1 brick remainsend = j + h;for (; j < n - h; j++)

    mySpu cmpswap(d[j],d[j+h]);}else end = r;while (j < end) {

    mySpu cmpswap skew(d[j],d[j+h-n]);j++;

    }// compare-exchange odd:even bricks beginning with i = h// similar to even:odd bricks

    // Step 2: Bubble sort// same as for AA-sort

    }

    FIGURE 1.7: Column-major brick sort

  • 14 Book title goes here

    Shaker sort

    Shaker sort differs from comb sort in that h-bubble passes are replaced byh-shake passes. An h-shake pass is a left-to-right bubble pass as in comb sortfollowed by a right-to-left bubble pass. Figure 1.8 gives our CBE adaptationof shaker sort. The preprocessing step of shaker sort almost always sorts thedata when the shrink factor s is less than 1.7.

    Algorithm Shaker(d,r){// sort d[0:r-1] into column-major order

    // Step 1: Preprocessingfor (i = 0; i < r; i++) sort(d[i]);for (h = r; h > 1; h /= s) {

    // h-shake pass// left-to-right bubble passfor (i = 0; i < r-h; i++)

    mySpu cmpswap(d[i],d[i+h]);for (i = r-h; i < r; i++)

    mySpu cmpswap skew(d[i],d[i+h-r]);// right-to-left bubble passfor (i = r-h-1; i > 0; i–)

    mySpu cmpswap(d[i],d[i+h]);for (i = r-1; i >= r - h; i–)

    mySpu cmpswap skew(d[i],d[i+h-r]);}// Step 2: Bubble sort// Same as for AA-sort

    }

    FIGURE 1.8: Column-major shaker sort

    1.4.1.2 Merge Sort

    Unlike the Shellsort variants comb, brick, and shaker sort of Section 1.4.1.1whose complexity is O(n log n) with high probability, the worst-case complex-ity of merge sort is O(n log n). Further, merge sort is a stable sort (i.e., therelative order of elements that have the same key is preserved). While thisproperty of merge sort isn’t relevant when we are simply sorting numbers (asyou can’t tell two equal numbers apart), this property is useful in some ap-plications where each element has several fields, only one of which is the sortkey. The Shellsort variants of Section 1.4.1.1 are not stable sorts. On the downside, efficient implementations of merge sort require added space. When sort-ing numbers in the vector array d[0 : r−1] we need an additional vector arrayt[0 : r − 1] to support the merge. CBE merge sort adaptation is presented asa stable sort and later we point out the simplifications that are possible when

  • Sorting On The Cell Broadband Engine 15

    FIGURE 1.9: Merge sort example

    we wish to sort numbers rather than elements that have multiple fields. Weagain assume that the numbers are in the vector array d[0 : r − 1].

    There are 4 phases to our stable merge sort adaptation:

    Phase 1: Transpose the elements of d[0 : r − 1], which represents a r × 4matrix, from row-major to column-major order.

    Phase 2: Sort the 4 columns of the r×4 matrix independently and in parallel.

    Phase 3: In parallel, merge the first 2 columns together and the last 2columns together to get two sorted sequences of length 2r each.

    Phase 4: Merge the two sorted sequences of length 2r each into a row-majorsorted sequence of length 4r.

    Merge Sort Phase 1–Transpose

    We note that Phase 1 is needed only when we desire a stable sort. Figure 1.9shows an initial 8 × 4 matrix of numbers and the result following each of the4 phases of our merge sort adaptation.

    The Phase 1 transformation is the inverse of the column-major to row-major transformation done in Figure 1.6 and we do not provide its details.Details for the remaining 3 phases are provided in the following subsections.

  • 16 Book title goes here

    Merge Sort Phase 2–Sort Columns

    Phase 2 operates in log r subphases characterized by the size of the sortedsegments being merged. For instance, in the first subphase, we merge togetherpairs of sorted segments of size 1 each, in the next subphase the segment sizeis 2, in the third it is 4, and so forth. At any time, the two segments beingmerged have the same physical locations in all 4 columns. So, for our 8 × 4example, when merging together segments of size 2, we shall first merge, inparallel, 4 pairs of segments, one pair from each column. The first segment ofa pair is in rows 0 and 1 of the r × 4 matrix and the second in rows 2 and3. Then, we shall merge together the segments of size 2 that are in rows 4through 7. Following this, the segment size becomes 4.

    To merge 4 pairs of segments in parallel, we employ 8 counters to keeptrack of where we are in the 8 segments being merged. The counters are calleda0, · · · , a3, b0, · · · , b3. (ai, bi) are the counters for the segments of columni, 0 ≤ i ≤ 3 that are being merged. When the segment size is s and thesegments being merged occupy rows i through i + 2s − 1, the a counters areinitialized to i and the b counters to i + s. Although all a counters have thesame value initially as do all b counters, as merging progresses, these countershave different values. Figure 1.10 gives the Phase 2 algorithm. For simplicity,we assume that r is a power of 2.

    Algorithm Phase2(d,r){// sort the 4 columns of d[0:r-1], use additional array t[0:r-1]

    for (s = 1; s < r; s *= 2)for (i = 0; i < r; i += 2*s) {

    A = spu splats(i); // initialize a countersB = spu splats(i+s); // initialize b countersfor (k = i; k < i + 2*s; k++) {// merge the segments

    // one round of comparesaData = mySpu gather(d,A);bData = mySpu gather(d,B);p = spu cmpgt(aData,bData);t[k] = spu select(aData,bData,p);// update countersnotP = mySpu not(p);A = spu sub(A,notP);B = spu sub(B,p);

    }swap(d,t); // swap roles

    }}

    FIGURE 1.10: Phase 2 of merge sort

  • Sorting On The Cell Broadband Engine 17

    Merge Sort Phase 3–Merge Pairs of Columns

    In Phase 3 we merge the first two and last two columns of the r×4 matrixtogether to obtain 2 sorted sequences, each of size 2r. The first sequence is incolumns 0 and 1 and the second in columns 2 and 3 of an output matrix. Wedo this merging by using 8 counters. Counters a0, b0, a1, b1 start at the top ofthe 4 columns of our matrix and move downwards while counters a2, b2, a3, b3start at the bottom and move up (see Figure 1.11(a)). Let e(c) be the matrixelement that counter c is at. The comparisons e(ai) : e(bi), 0 ≤ i ≤ 3 aredone in parallel and depending on the outcome of these comparisons, 4 of the8 elements compared are moved to the output matrix. When e(a0) ≤ e(b0)(e(a1) ≤ e(b1)), e(a0) (e(a1)) is moved to the output and a0 (a1) incrementedby 1; otherwise, e(b0) (e(b1)) is moved to the output and b0 (b1) incrementedby 1. Similarly, when e(a2) ≤ e(b2) (e(a3) ≤ e(b3)), e(b2) (e(b3)) is moved tothe output and b2 (b3) decremented by 1; otherwise, e(a2) (e(a3)) is movedto the output and a2 (a3) decremented by 1. The merge is complete when wehave done r rounds of comparisons. Figure 1.12 gives the algorithm for Phase3.

    Theorem 1 Algorithm Phase3 correctly merges 4 sorted columns into 2.

    Proof 1 To prove the correctness of Algorithm Phase3 we need to show thateach element of the first (last) two columns of the input r× 4 matrix is copiedinto the first (last) two columns of the output matrix exactly once and thatthe elements of the first (third) output column followed by those of the sec-ond (fourth) are in sorted order. It is sufficient to show this for the first twocolumns of the input and output matrices. First, observe that when a0 ≤ a2(b0 ≤ b2), these counters are at input elements that have yet to be copied to theoutput. Further, when a0 > a2 (b0 > b2) all elements of the respective columnhave been copied from the input to the output (note that a counter is updatedonly when its element has been copied to the output matrix). We consider 4cases: a0 < a2, a0 = a2, a0 = a2 + 1, and a0 > a2 + 1.

    Case a0 < a2 When b0 < b2 (Figure 1.11(a)), exactly one of e(a0) and e(b0)and one of e(a2) and e(b2) are copied to the output and the correspondingcounters are advanced. No element is copied to the output twice.

    Next, consider the case b0 = b2 (Figure 1.11(b)). If e(a0) ≤ e(b0), e(a0)and one of e(a2) and e(b2) are copied to the output and the correspondingcounters advanced. Again no element is copied to the output twice. Ife(a0) > e(b0) = e(b2), e(b2) < e(a0) ≤ e(a2) and e(b0) and e(a2) arecopied to the output and their counters advanced. Again, no element iscopied twice.

    The next case we consider has b0 = b2 + 1. Let the values of b0 and b2be b′0 and b

    2 just before the update(s) that resulted in b0 = b2 + 1 andlet a′0 and a

    2 be the values of the a counters at this time. One of thefollowing must be true: (a) b′2 = b

    0 + 1 (both b0 and b2 were advanced,

  • 18 Book title goes here

    a0

    a2

    b0, b2

    a0 b0

    a2 b2

    (a)

    a0

    a2

    b0, b′

    2

    (c)

    b2, b′

    0

    (b)

    a0

    a2

    b0, b′

    0, b′

    2

    (d)

    b2

    a0

    a2 b0

    (f)

    b2a0

    a2

    b0

    (e)

    b2, b′

    0, b′

    2

    b2

    (g)

    b0

    (h)

    b0, b2a0, a2 a0, a2

    b0

    (i)

    b2

    (j)

    b0

    a0, a2 a0, a2

    b2

    FIGURE 1.11: Phase 3 counters

  • Sorting On The Cell Broadband Engine 19

    Algorithm Phase3(d,r){// merge the 4 sorted columns of d[0:r-1] into 2 sorted sequences// use additional array t[0:r-1]

    A = {0, 0, r-1, r-1}; // initialize a countersB = {0, 0, r-1, r-1}; // initialize b countersfor (k = 0; k < r; i++) {

    aData = mySpu gatherA(d,A);bData = mySpu gatherB(d,B);p = spu cmpgt(aData,bData);e = spu equal(aData,bData);e = spu and(e, vector(0,0,-1,-1));p = spu or(p, e);min = spu select(aData, bData, p);max = spu select(bData, aData, p);t[k] = spu shuffle(min,t[k],WBXD);t[r-k-1] = spu shuffle(max,t[r-k-1],AYCZ);// update countersnotP = mySpu not(p);f1 = spu and(p,vector(-1,-1,0,0));s1 = spu and(p,vector(0,0,-1,-1));f2 = spu and(notP,vector(-1,-1,0,0));s2 = spu and(notP,vector(0,0,-1,-1));A = spu sub(A,f2);A = spu add(A,s2);B = spu sub(B,f1);B = spu add(B,s1);

    }}

    FIGURE 1.12: Phase 3 of merge sort

  • 20 Book title goes here

    Figure 1.11(c)), (b) b′0 = b′

    2 = b0 (only b2 was advanced, Figure 1.11(d)),or (c) b′0 = b

    2 = b2 (only b0 was advanced, Figure 1.11(e)). In (a), itmust be that b2 = b

    0 and b0 = b′

    2. So, e(a0) > e(b′

    0) and e(a2) ≤ e(b′2).Hence, e(a0) ≤ e(a2) ≤ e(b′2) = e(b0) and e(a2) ≥ e(a0) > e(b′0) =e(b2). Therefore, e(a0) and e(a2) are copied to the output and a0 anda2 advanced. Again, only previously uncopied elements are copied to theoutput and each is copied once. For subcase (b), when b′2 was decrementedto b2, a

    0 was incremented to a0, e(b′

    2) ≥ e(a2) and e(a′0) ≤ a(b′0). Sinceb0 > b2, all elements of the second column have been copied to the output.We see that e(a0) ≤ e(a2) ≤ e(b′2) = e(b0). So, e(a0) is copied and a0is advanced. Further, as a result of some previous comparison, b0 wasadvanced to its current position from the present position of b2. So, thereis an a′′0 ≤ a0 such that e(b2) < e(a′′0 ) ≤ e(a0) ≤ e(a2). Therefore, e(a2)is copied and a2 advanced. Again, no previously copied element is copiedto the output and no element is copied twice. Subcase (c) is symmetricto subcase (b).

    The final case has b0 > b2+1 (Figure 1.11(f)). From the proof of subcasesb0 = b2 and b0 = b2 + 1, it follows that this case cannot arise.

    Case a0 = a2 There are 4 subcases to consider–(a) b0 < b2, (b) b0 = b2, (c)b0 = b2 + 1, and (d) b0 > b2 + 1 (Figures 1.11(g–j)). Subcase (a) issymmetric to the case a0 < a2 and b0 = b2 considered earlier. In subcase(b), independent of the outcome of the comparison e(a0) : e(b0), whichis the same as the comparison e(a2) : e(b2), e(a0) (equivalently e(a2))and e(b0) (equivalently e(b2)) are copied to the output. For subcase (c),we notice that when a0 = a2, these two counters have had a cumulativeadvance of r−1 from their initial values and when b0 = b2 +1 these twocounters have together advanced by r. So, the 4 counters together haveadvanced by 2r − 1 from their initial values. This isn’t possible as the 4counters advance by a total of 2 in each iteration of the for loop. So,subcase (c) cannot arise. Next, consider subcase (d). From the proof forthe case a0 < a2, we know that we cannot have b0 > b2+1 while a0 < a2.So, we must have got into this state from a state in which a0 = a2 andb0 ≤ b2. It isn’t possible to get into this state from subcase (a) as subcase(a), at worst increases b0 by 1 and decreases b2 by 1 each time we are inthis subcase. So, it is possible to get into this subcase only from subcase(b). However, subcase (b) only arises at the last iteration of the for loop.Even otherwise, subcase (b) either increments b0 by 1 or decrements b2by 1 and so cannot result in b0 > b2 + 1.

    Case a0 > a2 + 1 From the proofs of the remaining cases, it follows that thiscase cannot arise.

    From the proof of Theorem 1, it follows that when we are sorting numbersrather than records with numeric keys, algorithm Phase3 works correctly evenwith the statements

  • Sorting On The Cell Broadband Engine 21

    e = spu equal(aData,bData);e = spu and(e, vector(0,0,-1,-1));p = spu or(p, e);omitted.

    Merge Sort Phase 4–Final Merge

    For the Phase 4 merge, we employ 4 counters. Counters a0 and a1, respec-tively begin at the first and last element of the first sorted sequence (i.e., atthe top of the first column and bottom of the second column, respectively)while b0 and b1 begin at the first and last elements of the second sequence(Figure 1.13). In each round, the comparisons a0 : b0 and a1 : b1 are done inparallel. e(a0) (e(b1)) is moved to the output if e(a0) ≤ e(b0) (e(b1) ≥ e(a1)).Otherwise, e(b0) (e(a1)) is moved to the output. The sorted output is assem-bled in row-major order in the vector array t. We use the variables k and posto keep track of the row and column in t in which to place the output elementfrom the comparison e(a0) : e(b0). The output element from e(a1) : e(b1) goesinto row (r−k−1) and column (3−pos) of t. Figure 1.14 gives the algorithmfor the case when the counters remain within the bounds of their respectivecolumns. mask[pos], 0 ≤ pos ≤ 3 is defined so as to change only the numberin position pos of a t[] vector.

    Column 0 1 2 3

    a0

    a1 b1

    b0

    FIGURE 1.13: Phase 4 counters

    As was the case in Phase 3, the statementse = spu equal(aData,bData);e = spu and(e, vector(0,0,-1,-1));p = spu or(p, e);may be omitted when we are sorting numbers rather than records with numerickeys.

    1.4.1.3 Comparison of Single SPU sorting algorithms

    We programmed our merge sort, brick sort, and shaker sort adaptationsusing the CBE SDK Version 3.0. For comparison purposes, we used an AA sortcode developed by us, the Cell sort code of [11], a non-vectorized merge sortcode developed by us, and the quick sort routine available in the CBE SDK.

  • 22 Book title goes here

    Algorithm Phase4(d,r){// partial algorithm to merge 2 sorted sequences of d[0:r-1]// into 1 sorted sequence// use additional array t[0:r-1]

    A = {0, r-1, 0, 0}; // initialize a countersB = {0, r-1, 0, 0}; // initialize b countersk = 0; pos = 0;while (no column is exhausted) {

    aData = mySpu gather12(d,A);bData = mySpu gather34(d,B);p = spu cmpgt(aData,bData);e = spu equal(aData,bData);e = spu and(e, vector(0,-1,0,0));p = spu or(p, e);min = spu select(aData, bData, p);max = spu select(bData, aData, p);max = spu slqwbyte(max, 4);t[k] = spu shuffle(min,t[k],mask[pos]);t[r-k-1] = spu shuffle(max,t[r-k-1],mask[3-pos]);// update countersnotP = mySpu not(p);f1 = spu and(p,vector(-1,0,0,0));s1 = spu and(p,vector(0,-1,0,0));f2 = spu and(notP,vector(-1,0,0,0));s2 = spu and(notP,vector(0,-1,0,0));A = spu sub(A,f2);A = spu add(A,s1);B = spu sub(B,f1);B = spu add(B,s2);k += (pos+1)/4;pos = (pos+1)%4;

    }}

    FIGURE 1.14: Phase 4 of merge sort with counters in different columns

  • Sorting On The Cell Broadband Engine 23

    TABLE 1.1: Comparison of various SPU sorting algorithms

    #In

    tegers

    AA

    Sort

    Shaker

    sort

    Bri

    ckSort

    Bit

    onic

    Sort

    Merg

    eSort

    Merg

    eSort

    (Sequenti

    al)

    Quic

    kSort

    128 52 53.6 53 47.8 50.8 146.6 145.6256 62.4 65.4 63.4 65.6 57.6 178.6 206.8512 81.8 86.4 81.4 72.6 70.4 272.2 3321024 123.8 142.2 116.8 125.4 97 315.4 605.62048 222.8 262 190.2 165.8 142 543 11644096 438.6 494.8 332.6 297.8 268.4 989.8 2416.68192 912.4 1033.6 663.8 609.6 508 2011.2 4686.616384 1906.4 2228 1361 1331.2 1017 4103 9485.2

    The codes were first debugged and optimized using the CBE simulator that isavailable with the SDK. The optimized codes were run on the Georgia Tech-STI Cellbuzz cluster to obtain actual run times. Table 1.1 gives the averagetime required to sort n 4-byte integers for various values of n. The average foreach n is taken over 5 randomly generated sequences. The variance in the sorttime from one sequence to the next is rather small and so the reported averageis not much affected by taking the average of a larger number of random inputsequences. Figure 1.15 is a plot of the average times reported in Figure 1.1.The shown run times include the time required to fetch the data to be sortedfrom main memory and to store the sorted results back to main memory.

    Our experiments reveal that a standard non-vectorized textbook imple-mentation of merge sort takes about 4 times the time taken by the vectorizedmerge sort adaptation. Further, the quick sort method that is part of the CBESDK takes about 9 times the time taken by our merge sort adaptation. Bricksort is the fastest of the shell sort like algorithms–AA sort, shaker sort andbrick sort–considered in this chapter, taking about 71% the time taken by AAsort to sort 16384 integers. Although cell (bitonic) sort is slightly faster thanbrick sort, it takes about 31% more time to sort 16384 integers than taken bymerge sort.

    1.4.2 Hierarchical sort

    For sorting a large set of numbers, a hierarchical approach similar to ex-ternal sort is employed where first each SPU sorts a local memory load of

  • 24 Book title goes here

    0 2000 4000 6000 8000 10000 12000 14000 16000 180000

    1000

    2000

    3000

    4000

    5000

    6000

    7000

    8000

    9000

    10000

    # of Integers

    Tim

    e in

    use

    c

    AASortShakersortBricksortBitonicsortMergesortMergesort(seq)Quicksort(SDK)

    FIGURE 1.15: Plot of average time to sort 4-byte integers

    data to generate sorted sequences called runs. These runs are then merged bythe SPUs to produce the final sorted sequence. Depending on how many runsare merged at a time there will be multiple rounds of merging before gener-ating the final sorted sequence. The PPU dispatches the runs to the SPUswhich do the merging and return the results back to the PPU. In the runmerging phase, each SPU independently merges a different set of runs. So,one needs to develop only a run merging algorithm for a single SPU. Inoueet al. [16] propose a single SPU merging algorithm that merges runs in pairs(i.e., a 2-way merge) using an adaptation of odd-even merge. Odd-even mergeof two four-number sorted sequences is done in 3 stages. First, the two sortedsequences are concatenated to get an 8-number sequence where each half isin sorted order. During the first stage, numbers that are 4 positions apart arecompare-exchanged 1. In the second stage, numbers that are 2 positions apartare compare-exchanged and in the last stage alternate numbers are compare-exchanged if needed. This scheme can be effectively SIMDized by beginningwith two vectors, each containing one of the two sorted 4-number sequences.

    1In a compare-exhange, two numbers are compared and swapped if the first number islarger than the second

  • Sorting On The Cell Broadband Engine 25

    Vector compare instructions are used so that 4 compare-exchange may bedone at a time. Figure 1.16 shows the process with two sorted vectors A andB and Figure 1.17 gives the pseudocode for this adaptation.

    output

    A0 A1 A2 A3 B0 B1 B2

    stage 3

    <

    < <

    B0 B1 B2A0 A1 A2 A3

    <

    < < < <

    <

    <

    Vector Register A Vector Register B

    sorted sorted

    Vector Register A Vector Register Bsorted

    : no operation :comparisonMIN MAX

    input

    stage 1

    stage 2

    B3

    B3

    FIGURE 1.16: SIMD odd-even merge of two vectors [16]

    As the runs are too long to fit in the local memory of an SPU, buffersare used to hold a portion of each of the runs currently being merged. Multi-buffering techniques are employed to overlap the computation with the datatransfer. Figure 1.18 is the plot of the average times taken for sorting 1 to 67Mintegers with different SPU sorting algorithms followed by SIMD odd-even 2-way merge to merge the runs except in the case of Bitonic Sort [11] wherebitonic merge is used to merge the runs and in the case of sequential mergesort where a texbook merge is done to combine the runs. Similar to singleSPU sorting, Brick sort is the fastest among shell sort like algorithms taking82% of the time taken by AA Sort for sorting 67M numbers while Shaker sortis the slowest of the bunch running 21% slower than Brick sort for sorting67M numbers. Merge sort is the fastest of all algorithms tested taking 84% ofthe time taken by Brick sort and runs 24% faster than Bitonic sort for sorting67M numbers. Compared to sequential sorts, the SIMD version of merge sortruns 3 times faster than the textbook merge sort and 24 times faster than theSDK quick sort.

  • 26 Book title goes here

    Algorithm oddEvenMerge(v1, v2){// Merge two vectors v1 and v2

    vector f1, f2;vetor f3, f4;vector p; // for storing patternp = spu cmpgt(v1, v2);f1 = spu select(v1, v2, pattern);f2 = spu select(v2, v1, pattern);// Stage 2f3 = spu rotate(f1, 8);f1 = spu select(f3, f2, p);f4 = spu select(f2, f3, p);f2 = spu shuffle(f1, f4, WACY);f3 = spu shuffle(f1, f4, ZXBD);// Stage 3p = spu cmpgt(f2, f3);p = spu shuffle(p, vZero, WXYA);f1 = spu select(f2, f3, p);f4 = spu select(f3, f2, p);// Outputv1 = spu shuffle(f1, f4 ZWAX);v2 = spu shuffle(f1, f4, BYCD);

    }

    FIGURE 1.17: SIMD 2-way merge of 2 vectors v1 and v2

  • Sorting On The Cell Broadband Engine 27

    0 10 20 30 40 50 60 700

    0.5

    1

    1.5

    2

    2.5

    3

    3.5

    4x 10

    7

    # of Integers (in millions)

    Tim

    e in

    use

    c

    AASortShakersortBricksortMergesortMergesort(seq)Bitonic sortQuicksort(SDK)

    FIGURE 1.18: Plot of average time to sort 1 to 67 million integers

    1.4.3 Master-slave sort

  • 28 Book title goes here

    1.5 Sorting Records

    1.5.1 Record layout

    A record R is comprised of a key k and m other fields f1, f2, · · · , fm. Forsimplicity, we assume that the key and each other field occupies 32 bits. Hence,a 128-bit CBE vector may hold up to 4 of these 32-bit values. Although thedevelopment in this section relies heavily on storing 4 keys in a vector (eachkey occupying 32 bits), the size of the other fields isn’t very significant. Letki be the key of record Ri and let fij , 1 ≤ j ≤ m be this record’s otherfields. With our simplifying assumption of uniform size fields, we may viewthe n records to be sorted as a two-dimensional array fieldsArray[][] withfieldsArray[i][0] = ki and fieldsArray[i][j] = fij , 1 ≤ j ≤ m, 1 ≤ i ≤ n.When this array is mapped to memory in column-major order, we get thefirst layout considered in [16]. We call this layout the ByField layout as, inthis layout, the n keys come first. Next we have the n values for the first fieldof the records followed by the n second fields, and so on. When the fieldsarray is mapped to memory in row-major order, we get the second layoutconsidered in [16]. This layout, which is a more common layout for records,is called the ByRecord layout as, in this layout, all the fields of R1 come first,then we have all fields of R2 and so on. When the sort begins with data in theByField (ByRecord) layout, the result of the sort must also be in the ByField(ByRecord) layout.

    1.5.2 High-level strategies for sorting records

    There are two high-level strategies to sort multifield records. In the first,we strip the keys from the records and create n tuples of the form (ki, i).We then sort the tuples by their first component. The second component ofthe tuples in the sorted sequence defines a permutation of the record indexesthat corresponds to the sorted order for the initial records. The records arerearranged into this permutation by either copying from fieldsArray to anew space or inplace using a cycle chasing algorithm as described for a tablesort in [15]. This strategy has the advantage of requiring only a linear numberof record moves. So, if the size of each record is s and if the time to sortthe tuples is O(n log n), the entire sort of the n records can be completedin O(n log n + ns) time. The second high-level strategy is to move all thefields of a record each time its key is moved by the sort algorithm. In thiscase, if the time to sort the keys alone is O(n log n), the time to sort therecords is O(ns log n). For relatively small s, the first strategy outperforms thesecond when the records are stored in uniform access memory. However, sincereordering records according to a prescribed permutation with a linear numberof moves makes random accesses to memory, the second scheme outperforms

  • Sorting On The Cell Broadband Engine 29

    the first (unless s is very large) when the records to be rearranged are inrelatively slow memory such as disk or the main memory of the CBE. For thisreason, we focus, in this chapter, on using the second strategy. That is, thesort algorithm moves all the fields of a record whenever its key is moved.

    1.5.3 Single SPU record sorting

    Two SIMD vector operations used frequently in number sorting algorithmsare findmin and shuffle. The findmin operation compares correspondingelements in two vectors and returns a vector min that contains, for each com-pared pair, the smaller. For example, when the two vectors being comparedare (4, 6, 2, 9) and (1, 8, 5, 3), the min is (1, 6, 2, 3). Suppose that vi and vjare vectors that, respectively, contain the keys for records Ri:i+3 and Rj:j+3.Figure 1.19 shows how we may move the records with the smaller keys to ablock of memory beginning at minRecords.

    pattern = spu cmpgt(vi, vj);minRecords = fields select(vi, vj , pattern);

    FIGURE 1.19: The findmin operation for records

    When the ByField layout is used, fields select takes the form given inFigure 1.20.

    for(p = 1; p

  • 30 Book title goes here

    numbers may be extended to the case of multifield records using the codeof Figure 1.22 for the ByField layout and that of Figure 1.23 for theByRecord layout. Both codes are for the case when the shuffle pattern isWY AC. Other shuffle patterns are done in a similar way. This extension ofspu shuffle to records is referred as fields shuffle. Other vector operationslike spu slqwbyte can be thought of as a spu shuffle operation with a cer-tain pattern and one can define a similar operation fields rotate along thoselines for the records in both layouts e.g. fields rotate(v, 8) is equivalent tofields shuffle(v, v, CDWX).

    for(p = 0; p

  • Sorting On The Cell Broadband Engine 31

    merge and double buffering. So, for multifield records, there is merit to de-veloping a higher-order merge. Correspondingly, two 4-way merge algorithmsare proposed in [2]. One is a scalar algorithm and the other is a vectorizedSIMD algorithm. Both algorithms are based on the high-level strategy shownin Figure 1.24.

    2−way Merge

    Main Alt

    Main Alt

    Main

    Main

    Main

    Alt

    Alt

    Alt

    Additional Buffer

    Additional Buffer

    2−way Merge

    2−way Merge

    FIGURE 1.24: 4-way merge

    The 4-way merge strategy involves performing 3 2-way merges in a singleSPU using two buffers (main and alt) for each of the 4 input streams A, B,C, and D as well as 2 buffers for the output stream O. An additional bufferis used for the output (E and F, respectively) of each of the two left 2-waymerge nodes of Figure 1.24. So, we employ a total of 12 buffers. Runs A andB are pairwise merged using the top left 2-way merge node while runs C andD are pairwise merged using the bottom left 2-way merge node. The former2-way merge generates the intermediate run E while the latter generates theintermediate run F. The intermediate runs E and F are merged by the right2-way merge node to produce the output run O, which is written back to mainmemory. Run generation is done one block or buffer load at a time. Doublebuffering is employed for the input of A, B, C, and D from main memory andthe output of O to main memory. By using double buffering and asynchronousDMA transfers to and from main memory, we are able to overlap much of theIO time with computation time.

    Scalar 4-way merge

    Figure 1.25 gives the pseudocode for the scalar 4-way merge algorithm. Forsimplicity, algorithm 4wayPipelinedMerge assumes that we have an integralnumber of blocks of data in each run. So, if each of the runs A, B, C, andD is (say) 10 blocks long, the output run O will be n = 40 blocks long.4wayPipelinedMerge generates these output n blocks one block at a time.Even blocks are accumulated in one of the output buffers and odd blocksin the other. When an output buffer becomes full, we write the block tomemory using an asynchronous DMA transfer (dmaOut) and continue output

  • 32 Book title goes here

    run generation using the other outbut buffer. So, other than when the firstoutput block is being generated and the last being written to main memory,one of the output blocks is being written to main memory while the other oneis being filled with records for the next block. At the end of each iteration ofthe outer for loop, we switch the roles of the two output buffers–the one thatwas being written to main memory becomes the buffer to place records forthe next block and the one that was being filled is written out. Of course, thisswitch may entail some delay as we must wait for the ongoing (if any) dmaOutto complete before we use this buffer for the records of the next block. Whengenerating a block of the output run, we merge from the buffers bufferE andbufferF to the output buffer bufferO that is currently designated for thispurpose. The number of records in a full buffer (i.e., the block size) is bSize.In case either bufferE or bufferF is empty, the generation of the outputblock is suspended and we proceed to fill the empty buffer using the methodmergeEF , which merges from either input streams A and B to bufferE orfrom streams C and D to bufferF . The algorithm mergeEF merges for eitherthe input streams A and B to bufferE or from E and F to bufferF . It usesdouble buffering on the streams A, B, C, and D and ensures that there isalways an active dmaIn for these four input streams. Since the pseudocode issimilar to that for 4wayPipelinedMerge, we do not provide this pseudocodehere. Records are moved between buffers using the memcpy instructions whenthe ByRecord layout is used and moved one field at a time when the layoutis ByField.

    Algorithm 4wayPipelinedMerge(A, B, C, D, O, n){// Merge runs/streams A, B, C, and D to produce O// with n blocks of size bSize

    // bufferA is a buffer for Ainitiate a dmaIn for bufferA, bufferB,bufferC and bufferD;for (i = 0; i < n; i++) {

    for (j = 0; j < bSize; j++) { // do block iif(bufferE is empty)

    mergeEF(A, B, E);if(bufferF is empty)

    mergeEF(C, D, F);move smaller record from front of bufferEand bufferF to bufferO

    }dmaOut(bufferO, O);switch the roles of the output buffers;

    }}

    FIGURE 1.25: 4-way merge

  • Sorting On The Cell Broadband Engine 33

    SIMD 4-way merge

    The SIMD version differs from the scalar version only in the way each ofthe three 2-way merges comprising a 4-way merge works. These 2-way mergesmove 4 records at a time from input buffers to the output buffer using odd-evenmerge scheme on the keys of those records. Two sorted vectors each consistingof keys from 4 sorted records are merged using odd-even merge. The fields arealso moved correspondingly using the fields operations introduced in theprevious sections. The odd-even merge of two vectors is essentially the sameprocess as in case of merging numbers described in Section 1.4.2. Figure 1.26gives the pseudocode of the adaptation for merging records.

    Algorithm oddEvenMerge(v1, v2){// Merge records whose keys are in v1 and v2

    fields f1[], f2[];fields f3[], f4[];vector p; // for storing patternp = spu cmpgt(v1, v2);f1 = fields select(v1, v2, pattern);f2 = fields select(v2, v1, pattern);// Stage 2f3 = fields rotate(f1, 8);f1 = fields select(f3, f2, p);f4 = fields select(f2, f3, p);f2 = fields shuffle(f1, f4, WACY);f3 = fields shuffle(f1, f4, ZXBD);// Stage 3p = spu cmpgt(f2, f3);p = spu shuffle(p, vZero, WXYA);f1 = fields select(f2, f3, p);f4 = fields select(f3, f2, p);// Outputv1 = fields shuffle(f1, f4 ZWAX);v2 = fields shuffle(f1, f4, BYCD);

    }

    FIGURE 1.26: SIMD 2-way merge of 2 vectors v1 and v2

    In Algorithm oddEvenMerge, v1 and v2 are two vectors each containingthe keys of the next 4 records in the input buffers for the two streams beingmerged. It is easy to see that the next four records in the merged output area subset of these 8 records and in fact are the 4 records (of these 8) with thesmallest keys. Algorithm oddEvenMerge determines these 4 smallest recordsand moves these to the output buffer.

  • 34 Book title goes here

    1.5.5 Comparison of record sorting algorithms

    We programmed several multifield record sorting algorithms using CellBE SDK 3.1. Specifically, the following algorithms were implemented andevaluated:

    1. 2-way AA Sort ... this is the multifield record sorting algorithm of Inoueet al. [16]. This uses a comb sort variant for run generation and 2-wayodd-even merge for run merging.

    2. 4-way AA Sort ... this uses a comb sort variant for run generation as in[16] and our 4-way odd-even merge for run merging (Section 1.5.4.1).

    3. 2-way Bitonic Sort ... this is an adaptation of the CellSort algorithm ofGedik et al. [11] to multifield records (Section 2.3). It uses bitonic sortfor run generation and 2-way bitonic merge for run merging.

    4. 4-way Bitonic Sort ... this uses bitonic sort for tun generation as in [11]and our 4-way odd-even merge for run merging (Section 1.5.4.1).

    5. 2-way Merge Sort ... this uses an adaptation of the SPU merge sortalgorithm of Bandyopadhyay and Sahni [3] to multifield records (Sec-tion 2.3) for run generation and the 2-way odd-even merge of [16] forrun merging.

    6. 4-way Merge Sort ... this uses an adaptation of the SPU merge sort algo-rithm of Bandyopadhyay and Sahni [3] to multifield records (Section 2.3)for run generation and our 4-way odd-even merge for run merging (Sec-tion 1.5.4.1).

    7. 2-way Scalar Merge Sort ... this uses an adaptation of the SPU mergesort algorithm of Bandyopadhyay and Sahni [3] to multifield records(Section 2.3) for run generation. Run merging is done using a 2-wayscalar merging algorithm derived from the 4-way scalar merging algo-rithm of Section 1.5.4.1 by eliminating the bottom left and the right2-way merge nodes.

    8. 4-way Scalar Merge Sort ... this uses an adaptation of the SPU mergesort algorithm of Bandyopadhyay and Sahni [3] to multifield records(Section 2.3) for run generation and our 4-way scalar merge for runmerging (Section 1.5.4.1).

    We experimented with the above 8 multifield sorting algorithms using ran-domly generated input sequences. In our experiments, the number of 32-bitfields per record is varied from 5 to 15 (in addition to the key field) and thenumber of records varied from 4K to 1M. Also, we tried both layouts–ByFieldand ByRecord. For each combination of number of fields, number of records,and layout type, the time to sort 10 random sequences was obtained. Thestandard deviation in the observed run times was small and we report onlythe average times.

  • Sorting On The Cell Broadband Engine 35

    1.5.5.1 Run times for ByField layout

    Figures 1.27(a) through 1.28(d) give the average run times for our 8 sortingalgorithms using the ByField layout and Figures 1.29(a) through 1.29(d)compare the average run times for the 2-way and 4-way versions of each ofour sorting algorithms for the case when the number of records to be sorted is1M. For all our data, the 4-way version outperformed the 2-way version. For1M records with 5 32-bit fields (in addition to a 32-bit key), the 4-way versionsof AA Sort, Bitonic Sort, Merge Sort, and Scalar Merge Sort, respectively, took5%, 4%, 7%, and 4% less time than taken by their 2-way counterparts andthese percentages for 15 fields were 9%, 6%, 9%, and 6% respectively.

    (a) 2-way AA-Sort (ByF ield) (b) 2-way Bitonic Sort (ByF ield)

    (c) 2-way Merge Sort (ByF ield) (d) 2-way Scalar Merge Sort (ByF ield)

    FIGURE 1.27: 2-way Sorts (ByField)

  • 36 Book title goes here

    (a) 4-way AA-Sort (ByF ield) (b) 4-way Bitonic Sort (ByF ield)

    (c) 4-way Merge Sort (ByF ield) (d) 4-way Scalar Merge Sort (ByF ield)

    FIGURE 1.28: 4-way sorts (ByField)

  • Sorting On The Cell Broadband Engine 37

    5 6 7 8 9 10 11 12 13 14 153

    4

    5

    6

    7

    8

    9x 10

    5

    # Fields

    Exe

    cutio

    n tim

    e (in

    mic

    rose

    c)

    2−way 4−way

    (a) 2-way and 4-way AA Sort (ByF ield), 1Mrecords

    5 6 7 8 9 10 11 12 13 14 154

    5

    6

    7

    8

    9

    10

    11

    12x 10

    5

    # Fields

    Exe

    cutio

    n tim

    e (in

    mic

    rose

    c)

    4−way2−way

    (b) 2-way and 4-way Bitonic sort (ByF ield), 1Mrecords

    5 6 7 8 9 10 11 12 13 14 152

    3

    4

    5

    6

    7

    8x 10

    5

    # Fields

    Exe

    cutio

    n tim

    e (in

    mic

    rose

    c)

    2−way 4−way

    (c) 2-way and 4-way Merge Sort (ByF ield), 1Mrecords

    5 6 7 8 9 10 11 12 13 14 153

    4

    5

    6

    7

    8

    9

    10x 10

    5

    # Fields

    Exe

    cutio

    n tim

    e (in

    mic

    rose

    c)

    2−way4−way

    (d) 2-way and 4-way Scalar Merge Sort

    (ByF ield), 1M records

    FIGURE 1.29: 2-way and 4-way Sorts (ByField), 1M records

  • 38 Book title goes here

    Figure 1.30 shows the run times for the 4 4-way sort algorithms for 1Mrecords. As can be seen, 4-way Bitonic Sort is the slowest, followed by 4-wayScalar Merge Sort, followed by 4-way AA Sort; 4-way Merge Sort was thefastest. In fact, across all our data sets, 4-way Bitonic Sort took between 17%and 23% more time than taken by 4-way Scalar Merge Sort, which in turntook between 18% and 19% more time than taken by 4-way AA Sort. Thefastest 4-way sort algorithm, 4-way Merge Sort took, respectively, between40% and 35%, 26% and 25%, 13% and 10% less time than taken by 4-wayBitonic Sort, 4-way Scalar Merge Sort, and 4-way AA Sort.

    5 6 7 8 9 10 11 12 13 14 152

    3

    4

    5

    6

    7

    8

    9

    10

    11x 10

    5

    # Fields

    Exe

    cutio

    n tim

    e (in

    mic

    rose

    c)

    Bitonic Scalar Merge

    AA

    Merge

    FIGURE 1.30: 4-way sorts (ByField), 1M records

    1.5.5.2 Run times for ByRecord layout

    Figures 1.31(a) through 1.32(d) give the average run times for sortingalgorithms using the ByRecord layout and Figures 1.33(a) through 1.33(d)present the comparison of average run times for the 2-way and 4-way versionsof each sorting algorithm when the number of records to be sorted is 1M. Inthis layout as well, the 4-way version outperformed the 2-way version for all

  • Sorting On The Cell Broadband Engine 39

    the data sets. For 1M records with 5 32-bit fields (in addition to a 32-bit key),the 4-way versions of AA Sort, Bitonic Sort, Merge Sort, and Scalar MergeSort, respectively, took 5%, 4%, 7%, and 0.1% less time than taken by their2-way counterparts and these percentages for 15 fields were 9%, 6%, 9%, and4% respectively.

    (a) 2-way AA-Sort (ByRecord) (b) 2-way Bitonic Sort (ByRecord)

    (c) 2-way Merge Sort (ByRecord) (d) 2-way Scalar Merge Sort (ByRecord)

    FIGURE 1.31: 2-way Sorts (ByRecord)

  • 40 Book title goes here

    (a) 4-way AA-Sort (ByRecord) (b) 4-way Bitonic Sort (ByRecord)

    (c) 4-way Merge Sort (ByRecord) (d) 4-way Scalar Merge Sort (ByRecord)

    FIGURE 1.32: 4-way sorts (ByRecord)

  • Sorting On The Cell Broadband Engine 41

    5 6 7 8 9 10 11 12 13 14 151.5

    1.6

    1.7

    1.8

    1.9

    2

    2.1

    2.2x 10

    6

    #Fields

    Exe

    cutio

    n tim

    e (in

    mic

    rose

    c)

    2−way

    4−way

    (a) 2-way and 4-way AA Sort (ByRecord), 1Mrecords

    5 6 7 8 9 10 11 12 13 14 151.7

    1.8

    1.9

    2

    2.1

    2.2

    2.3

    2.4

    2.5x 10

    6

    #Fields

    Exe

    cutio

    n tim

    e(in

    mic

    rose

    c) 4−way

    2−way

    (b) 2-way and 4-way Bitonic sort (ByRecord),1M records

    5 6 7 8 9 10 11 12 13 14 151.1

    1.2

    1.3

    1.4

    1.5

    1.6

    1.7

    1.8x 10

    6

    #Fields

    Exe

    cutio

    n tim

    e(in

    mic

    rose

    c)

    2−way

    4−way

    (c) 2-way and 4-way Merge Sort (ByRecord),1M records

    5 6 7 8 9 10 11 12 13 14 152.1

    2.2

    2.3

    2.4

    2.5

    2.6

    2.7

    2.8

    2.9x 10

    5

    #Fields

    Exe

    cutio

    n tim

    e(in

    mic

    rose

    c)

    2−way

    4−way

    (d) 2-way and 4-way Scalar Merge Sort

    (ByRecord), 1M records

    FIGURE 1.33: 2-way and 4-way Sorts (ByRecord), 1M records

  • 42 Book title goes here

    Figure 1.34 shows the run times for the 4 4-way sort algorithms for 1Mrecords. As we can observe, 4-way Bitonic Sort is the slowest, followed by 4-way AA Sort, followed by 4-way Merge Sort; 4-way Scalar Merge Sort was thefastest. In fact, across all our data sets, 4-way Bitonic Sort took between 16%and 17% more time than taken by 4-way AA Sort, which in turn took between24% and 35% more time than taken by 4-way Merge Sort. The fastest of themin ByRecord format, 4-way Scalar Merge Sort took, respectively, 88%, 86%,and between 81% and 88% less time than taken by 4-way Bitonic Sort, 4-wayAA Sort, and 4-way Merge Sort.

    5 6 7 8 9 10 11 12 13 14 150

    0.5

    1

    1.5

    2

    2.5x 10

    6

    #Fields

    Exe

    cutio

    n tim

    e(in

    mic

    rose

    c)

    Bitonic

    AA

    Merge

    Scalar Merge

    FIGURE 1.34: 4-way sorts (ByRecord), 1M records

    1.5.5.3 Cross layout comparison

    Although in a real application one may not be able to choose the layoutformat for the data to be sorted, it is worthwhile to compare the relativeperformance of the 8 sort methods using the better layout for each. Thismeans that we use the ByField layout for AA Sort and Merge Sort and theByRecord layout for Merge Sort and Scalar Merge Sort. Figure 1.35 gives therun times for the 4-way versions using these formats for the case of 1M records.Although Figure 1.35 is only for the case of 1M records, 4-way Scalar MergeSort was the fastest for all of our data sets. For 5 32-bit fields (in addition tothe key field) 4-way Scalar Merge Sort (ByRecord) ran 81% faster than 4-wayMerge Sort (ByRecord), 30% faster than 4-way AA Sort (ByField), and 20%faster than 4-way Merge Sort (ByField). When the number of fields was 15,these percentages were 88%, 64% and 60% respectively.

  • Sorting On The Cell Broadband Engine 43

    5 6 7 8 9 10 11 12 13 14 150.2

    0.4

    0.6

    0.8

    1

    1.2

    1.4

    1.6

    1.8x 10

    6

    #Fields

    Exe

    cutio

    n tim

    e(in

    mic

    rose

    c)

    Merge (ByRecord)

    AA (ByField)

    Merge (ByField)

    Scalar Merge(ByField)

    FIGURE 1.35: 4-way sorts using the best algorithms for different layouts

    Experiments indicate that the 4-way Scalar Merge Sort is the fastestmethod (from among those tested) to sort multifield records on the CBE.

  • 44 Book title goes here

  • Chapter 2

    Sorting On A Graphics ProcessingUnit(GPU)

    Shibdas Bandyopadhyay

    University of Florida

    Sartaj Sahni

    University of Florida

    2.1 Graphics Processing Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462.2 Sorting Numbers on GPUs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

    2.2.1 SDK Radix Sort Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502.2.1.1 Step 1–Sorting tiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512.2.1.2 Step 2–Calculating histogram . . . . . . . . . . . . . . . . . . . . . . . . . . . 522.2.1.3 Step 3–Prefix sum of histogram . . . . . . . . . . . . . . . . . . . . . . . . . 532.2.1.4 Step 4–Rearrangement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

    2.2.2 GPU Radix Sort(GRS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542.2.2.1 Step 1–Histogram and Ranks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552.2.2.2 Step 2–Prefix sum of tile histograms . . . . . . . . . . . . . . . . . . . . 582.2.2.3 Step 3–Positioning numbers in a tile . . . . . . . . . . . . . . . . . . . . 59

    2.2.3 SRTS Radix Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592.2.3.1 Step 1–Bottom level reduce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622.2.3.2 Step 2–Top level scan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622.2.3.3 Step 3–Bottom level scan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

    2.2.4 GPU Sample Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632.2.4.1 Step 1–Splitter selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 642.2.4.2 Step 2–Finding buckets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652.2.4.3 Step 3–Prefix sum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652.2.4.4 Step 4–Placing elements into buckets . . . . . . . . . . . . . . . . . . . 65

    2.2.5 Warpsort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652.2.5.1 Step 1–Bitonic sort by warps . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652.2.5.2 Step 2–Bitonic merge by warps . . . . . . . . . . . . . . . . . . . . . . . . . . 662.2.5.3 Step 3–Splitting long sequences . . . . . . . . . . . . . . . . . . . . . . . . . 672.2.5.4 Step 4–Final merge by warps . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

    2.2.6 Comparison of number sorting algorithms . . . . . . . . . . . . . . . . . . . . . . . . 682.3 Sorting Records on GPUs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69

    2.3.1 Record Layouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692.3.2 High level Strategies for sorting records . . . . . . . . . . . . . . . . . . . . . . . . . . 702.3.3 Sample Sort For Sorting Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702.3.4 SRTS For Sorting Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 712.3.5 GRS For Sorting Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722.3.6 Comparison of record sorting algorithms . . . . . . . . . . . . . . . . . . . . . . . . . 732.3.7 Run Times for ByField layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742.3.8 Run Times for Hybrid layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

    45

  • 46 Book title goes here

    2.1 Graphics Processing Units

    Contemporary graphics processing units (GPUs) are massively parallelmanycore processors. NVIDIA’s Tesla GPUs, for example, have 240 scalar pro-cessing cores (SPs) per chip [22]. These cores are partitioned into 30 Stream-ing Multiprocessors (SMs) with each SM comprising 8 SPs. Each SM shares a16KB local memory (called shared memory) and has a total of 16,384 32-bitregisters that may be utilized by the threads running on this SM. Besidesregisters and shared memory, on-chip memory shared by the cores in an SMalso includes constant and texture caches. The 240 on-chip cores also share a4GB off-chip global (or device) memory. Figure 2.1 shows a schematic of theTesla architecture. With the introduction of CUDA (Compute Unified DriverArchitecture) [34], it has become possible to program GPUs using C. Thishas resulted in an explosion of research directed toward expanding the appli-cability of GPUs from their native computer graphics applications to a widevariety of high-performance computing applications.

    FIGURE 2.1: NVIDIA’s Tesla GPU [26]

    GPUs operate under the master-slave computing model (see [25] for e.g.)in which there is a host or master processor to which is attached a collectionof slave processors. A possible configuration would have a GPU card attachedto the bus of a PC. The PC CPU would be the host or master and the GPUprocessors would be the slaves. The CUDA programming model requires theuser to write a program that runs on the host processor. At present, CUDAsupports host programs written in C and C++ only though there are plansto expand the set of available languages [34]. The host program may invokekernels, which are C functions, that run on the GPU slaves. A kernel maybe instantiated in synchronous (the CPU waits for the kernel to completebefore proceeding with other tasks) or asynchronous (the CPU continues withother tasks following the spawning of a kernel) mode. A kernel specifies thecomputation to be


Recommended