Docsity

Parallelization of Floyd's Algorithm for All-Pair Shortest-Path Problem, Study notes of Computer Science

Partial preview of the text

Download Parallelization of Floyd's Algorithm for All-Pair Shortest-Path Problem and more Study notes Computer Science in PDF only on Docsity!

COSC 4397 – Parallel ComputationEdgar Gabriel

COSC 4397

Parallel ComputationGraph Algorithms (I)

Edgar GabrielSpring 2006

2

Definitions and Representations (I)^ COSC 4397 – Parallel ComputationEdgar Gabriel

-^

A^

graph

G is a pair

(V,E)

with

-^

V : finite set of

vertices

-^

E : finite set of

edges

between pairs of vertices

-^

Directed vs. undirected graphs^ – Undirected: an edge

e

E^

is an unordered pair (u,v)

  • Directed: an edge

e

E^

is an ordered pair (u,v)

(connection from u to v does not imply connectionfrom v to u) 1

(^23)

4

1

(^23)

4

4

Definitions and Representations (III)^ COSC 4397 – Parallel ComputationEdgar Gabriel

-^

Weighted Graphs G=(V,E,

ω ):

  • Weights are associated with each edge in E– Weights

ω

are real numbers representing costs or

benefits of traversing the associated edge

1

(^23)

4

(^35)

8

5

COSC 4397 – Parallel ComputationEdgar Gabriel

Representation of Graphs in

Computer Programs

-^

Adjacency matrix A = (a

) such thatij

-^

Note: the adjacency matrix of an undirected graph issymmetric

⎧ ⎪ ⎨ ⎪⎩

=

∈

∞

=

otherwise

j i

E

v v if if v v

a

j i

j i

ij

) , (

0

) , (

7

COSC 4397 – Parallel ComputationEdgar Gabriel

All-pair shortest-path

-^

All-pair shortest-path:– find the length of the shortest path between each pair

of vertices

-^

Floyd’s Algorithm– Transforms the adjacency matrix into a matrix

containing the shortest path between all pairs ofvertices

  • Checks in iteration k, whether path between edges i

and j is shorter if going through edge k than thecurrently stored shortest path between i and j

  • O(n

3 )

8

COSC 4397 – Parallel ComputationEdgar Gabriel

Floyd’s Algorithm

-^

Sequential algorithm^ – Input

  • A:

adjacency

matrix

  • n:

number

of

vertices

for

k=0,n- for

i=0,n-1for

j=0,n- A[i,j]=

min(A[i,j],A[i,k]+A[k,j])

end

for

end

for end

for

10

Parallelizing Floyd’s Algorithm (I) COSC 4397 – Parallel ComputationEdgar Gabriel

-^

Data Parallel Problem – the same operation applied todifferent data items

-^

Initial guess: consider one element of the adjacencymatrix on a separate processor

-^

E.g. for k=1update of element A[3,4] requires A[3,1] andA[1,4]

11

Parallelizing Floyd’s Algorithm (II) COSC 4397 – Parallel ComputationEdgar Gabriel

-^

Generalizing:– At iteration k, every task in row k has to broadcast its

value to all processes in the same columne.g k=1, A[i,j] needs A[i,1] and - At iteration k, every task in column k needs to

broadcast its value to all processes in the same rowe.g. k=1, A[i,j] needs A[1,j]

i , j

i,k

0,0 needs 0,10,1 needs 0,10,2 needs 0,1i , j^

k, j

0,0 needs 1,01,0 needs 1,02,0 needs 1,

13

COSC 4397 – Parallel ComputationEdgar Gabriel

Preliminary algorithm

Given A

, n,xy

c

,cx

y

int rank, rowtemp, coltemp;MPI_Comm rowcomm, colcomm;MPI_Comm_rank (MPI_COMM_WORLD, &rank);MPI_Comm_split (MPI_COMM_WORLD,

c

,^ y rank, &rowcomm);

MPI_Comm_split (MPI_COMM_WORLD,

c

,^ x rank, &colcomm);

for (k=0; k<n; k++ ){

if (

cx

== k ) rowtemp= A

xy^

;

MPI_Bcast (rowtemp, 1, MPI_INT, k, rowcomm );if (

cy

== k ) coltemp = A

;xy

MPI_Bcast (coltemp 1, MPI_INT, k, colcomm );Axy

=min ( A

, rowtemp+coltemp );xy

}

14

COSC 4397 – Parallel ComputationEdgar Gabriel

Problems to solve

-^

Allocating a 2-D matrix in C

-^

Reading the data from a file and distributing it to allprocesses

-^

Displaying the Matrix (for development purposes)

16

COSC 4397 – Parallel ComputationEdgar Gabriel

Allocating a 2-D Matrix (II)

matrix[0]matrix[1]matrix[2]matrix[3]matrix[4] Note: the rows of the matrix do not have to form a contiguous array in memory!

17

COSC 4397 – Parallel ComputationEdgar Gabriel

Allocating a 2-D Matrix (III)

{

int **matrix=NULL;int array=NULL;/ Allocate an array which is

large enough to hold

the entire matrix / array = (int ) malloc(n1n2sizeof(int));/* Allocate an array of pointers*/matrix = (int *) malloc (n1sizeof(int ));/ Set the pointers such that they

point to the right

elements of the overall data array

*/

for (i=0; i<n1; i++) {

matrix[i] = &(array[i*n2]); } }

19

A more realistic data decomposition^ COSC 4397 – Parallel ComputationEdgar Gabriel

-^

1-D column wise striped data distribution– A process holds a column of the adjacency matrix– No need to broadcast data in column-communicator

  • No need to create column-wise communicators• No need to use sub-communicators at all!

20

COSC 4397 – Parallel ComputationEdgar Gabriel

Preliminary algorithm (IIa)

Given A

… Ax

xn^

n,

c

,x

int temp[n];/* note: rank in MPI_COMM_WORLD =

c

x^

*/

for (k=0; k<n; k++ ){

if (

cx

== k ) { for (i=0; i<n; i++ ) {

temp[i]= A

;xi

} } MPI_Bcast (temp, n, MPI_INT, k, MPI_COMM_WORLD );for (i=0; i<n; i++) {

Axi

=min ( A

, Axi

xk^

+temp[i] );

} }