Docsity

Optimizing the Sieve of Eratosthenes: Algorithm Analysis and Parallel Programming - Prof. , Study notes of Computer Science

Partial preview of the text

Download Optimizing the Sieve of Eratosthenes: Algorithm Analysis and Parallel Programming - Prof. and more Study notes Computer Science in PDF only on Docsity!

Optimization & Tiling

(Chapter 5)

Sanjay Rajopadhye

Colorado State University

Fall 2008 Week 5

Objectives

 Sieve of Eratosthenes: algorithm analysis

 Optimizations:

 Broadcast removal

 Loop interchange (for caches)

 Marking only odd numbers

Analysis  Sequential complexity

 Almost linear time complexity

! T ( n ) = O ( n ln ln n ) Analysis

 Sequential complexity

 log 10 n: Number of digits in decimal representation of n

 log 2 n: Number of bits in the binary representation of n

 log log 10 n: Digits in decimal representation of that

 log log 2 n: bits in the binary representation of that

 Almost linear time complexity

! T ( n ) = O ( n ln ln n )

Initial sequential part for (i=0; i<=sqrt(n); i++) marked[i] = 0; k = index = 2; j=0; marked[0] = marked[1] = 1; while (kk<=sqrt(n)) { for (i=kk; i<=sqrt(n); i+= k) marked[i]=1; while (marked[++index]); primes[j++] = k = index; } /* loop through marked & store additional values into primes / numprimes = “total number of primes found”; Parallel part for (j=0; j<=numprimes; j++){ f = “first multiple of primes[j] in my section”; for (i=f; i<=mysize; i+= primes[j]) marked[i]=1; } / code to count the number of primes and MPI_Reduce it to processor 0 */

Locality Improvement  Loop blocking/tiling/partitioning  Typically applied to compute-intensive loops to optimize

 Communication cost

 Memory locality (register, cache, main-memory, disk)

 parallel machine utilization (processors, cores, etc.)

Matrix multiplication (ideal) 3 O ( n ) Real machine 5 O ( n ) “The uniform Memory Hierarchy Model of Computation” B. Alpern, L. Carter, E. Feig, T. Selker (Algorithmica 1992) Tiling the sieve for (j=0; j<=numprimes; j++){ f = “first multiple of primes[j] in my section”; for (i=f; i<=mysize; i+= primes[j]) marked[i]=1; }

Pull the ii loop outside the j loop for (j=0; j<=numprimes; j++){ f = “first multiple of primes[j] in my section”; start = “largest multiple of BKSIZE leq f”; for (ii=start; ii <= mysize, ii += BKSIZE){ f’ = first multiple of primes[j] geq ii; for (i=f’; i<=min(ii+BKSIZE-1,mysize); i+=primes[j]){ marked[i]=1; } } } Pull the ii loop outside the j loop for (j=0; j<=numprimes; j++){ f = “first multiple of primes[j] in my section”; start = “largest multiple of BKSIZE leq f”; for (ii=start; ii <= mysize, ii += BKSIZE){ f’ = first multiple of primes[j] geq ii; for (i=f’; i<=min(ii+BKSIZE-1,mysize); i+=primes[j]){ marked[i]=1; } } }

But is this legal?  That’s the trick  You need to ensure that the code correctly does what the original one did. Performance

Summary  More involved problem  Reductions and Broadcasts  Block Distribution  Sequential Performance is crucial

 Key to high performance computing