Prefix Scan • Toolchain: • First install Visual Studio, then Open MPI • Compile with: cl |
|
Prefix Scan • Given a sequence of numbers, e.g.: (𝑎0, 𝑎1, 𝑎2, 𝑎3,…, 𝑎𝑛−1) • Compute the output sequence: (𝑏 , 𝑏 , 𝑏 , 𝑏 ,…, 𝑏 ) • Where 𝑏=𝑖𝑎 𝑖 𝑗=0𝑗 • Example: 𝑛 = 4 0123 𝑛−1 (𝑎 )3 = 1,3,2,2 𝑖 𝑖=0 (𝑏 )3 = (1,4,6,8) 𝑖 𝑖=0 |
|
Prefix Scan • Serial Algorithm: 0: <initialise a and b> 1: b[0] = a[0]; for(int i=1; i<n; ++i) 2: b[i] = b[i-1] + a[i]; • Variable values:
b = (0,0,0,0) b = (1,0,0,0) b = (1,4,0,0) b = (1,4,6,0) b = (1,4,6,8) |
|
Prefix Scan • Alternative algorithm:
up down |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1210
Prefix Scan • Example: a = (1,2,2,3,1,4,2)
up down |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1210
Prefix Scan
|
|
Prefix Scan • Sequential algorithm: one process performs n-1 additions. • Parallel algorithm: n processes perform a maximum of 𝑅 + 1 = log 𝑛 computations each. |
|
Prefix Scan • Implement the prefix scan algorithm in c/mpi:
|
|