330 likes | 450 Views
Partitioning and Divide-and-Conquer Strategies. Data partitioning (or Domain decomposition) Functional decomposition. x 0 …… .x (n/m)-1. x n/m …… .x (2n/m)-1. x (m-1)n/m …… .x n-1. +. +. +. ……. +. sum. Partitioning and Divide-and-Conquer Strategies. Addition of a sequence of numbers.
E N D
Partitioning and Divide-and-Conquer Strategies Data partitioning (or Domain decomposition) Functional decomposition
x0…….x(n/m)-1 xn/m…….x(2n/m)-1 x(m-1)n/m…….xn-1 + + + ……. + sum Partitioning and Divide-and-Conquer Strategies
Addition of a sequence of numbers Slave recv(numbers, s, Pmaster); sum = 0; for(i=0 ; i<s ; i++) part_sum = part_sum + numbers[i]; send(&part_sum, Pmaster); Master s = n / m; for(i=0, x=0 ; i<m ; i++, x=x+s) send(&number[x], s, Pi); result = 0; for(i=0 ; i<m ; i++){ recv(&part_sum, Pany); sum = sum + part_sum; }
Addition of a sequence of numbers Master s = n / m; bcast(numbers, s, Pslave_group); result = 0; for(i=0 ; i<m ; i++){ recv(&part_sum, Pany); sum = sum + part_sum; } Slave bcast(numbers, s, Pmaster); start = slave_number * s; end = start + s; sum = 0; for(i=start ; i<end ; i++) part_sum = part_sum + numbers[i]; send(&part_sum, Pmaster);
Addition of a sequence of numbers Analysis. Phase I. communication Phase II. computation
Addition of a sequence of numbers Analysis. Phase III. communication Phase VI. computation
Addition of a sequence of numbers Analysis.
Divide-and-Conquer int add(int* s) { if(number(s)=<2) return (n1+n2); else{ divide(s, s1, s2); part_sum1 = add(s1); part_sum2 = add(s2); retunr(part_sum1 + part_sum2); } }
Divide-and-Conquer Divide problem
P0 P0 P4 P0 P2 P4 P6 P0 P1 P2 P3 P4 P5 P6 P7 Divide-and-Conquer
P0 P0 P4 P0 P2 P4 P6 P0 P1 P2 P3 P4 P5 P6 P7 Divide-and-Conquer
Divide-and-Conquer Process P0 divide(s1, s1, s2); send(s2, P4); divide(s1, s1, s2); send(s2, P2); divide(s1, s1, s2); send(s2, P0); part_sum = *s1; recv(&part_sum1, P0); part_sum = part_sum + part_sum1; recv(&part_sum1, P2); part_sum = part_sum + part_sum1; recv(&part_sum1, P4); part_sum = part_sum + part_sum1; Process P4 recv(s1, P0) divide(s1, s1, s2); send(s2, P6); divide(s1, s1, s2); send(s2, P5); part_sum = *s1; recv(&part_sum1, P5); part_sum = part_sum + part_sum1; recv(&part_sum1, P6); part_sum = part_sum + part_sum1; send(&part_sum, P0);
Divide-and-Conquer analysis communication
Divide-and-Conquer analysis computation
M-way Divide-and-Conquer int add(int* s) { if(number(s) =<4) return(n1+n2+n3+n4); esle{ divide(s, s1, s2, s3, s4); part_sum1 = add(s1); part_sum2 = add(s2); part_sum3 = add(s3); part_sum4 = add(s4); return(part_sum1 + part_sum2 + part_sum3 + part_sum4); } }
Numerical Integration f(x) f(q) f(p) a p q b
Numerical Integration f(x) f(q) f(p) a p q b
Numerical Integration f(x) f(q) f(p) a p q b
Numerical Integration Process Pi if( i== master ){ printf("Enter number of intervals"); scanf("%d", &n); } bcast(&n, Pgroup); region = (b-a) / p; start = a + region * i; end = start + region; d = (b-a) / n; area = 0.0; for(x=start ; x<end ; x = x+d) area = area + 0.5 * (f(x)+f(x+d)) * d; reduce_add(&integral, &area, Pgroup); area = 0.0; for(x=start ; x<end ; x=x+d) area = area + f(x) + f(x+d); area = 0.5 * area * d; area = 0.5 * (f(start) + f(end)); for(x=start+d ; x<end ; x=x+d) area = area + f(x); area = area * d;
N-body problem Three dimension space
N-body problem Sequential Code for(t=0 ; t<tmax ; t++) for(i=0 ; i<N ; i++) { F=FORCE_ROUTINE(i); v[i]new = v[i] + F * dt; x[i]new = x[i] + v[i]new * dt; } for(i=0 ; i<nmax ; i++){ x[i] = x[i]new; v[i] = v[i]new; }
N-body problem Center of mass distance cluster of bodies
N-body problem Barnes-Hut algorithm
N-body problem for( t=0 ; t<tmax ; t++) { Build_OCTTREE(); Tot_Mass_center(); comp_Force(); Update(); }