Docsity

Schwartz's Algorithm and Generalized Reduce & Scan, Study notes of Computer Science

Partial preview of the text

Download Schwartz's Algorithm and Generalized Reduce & Scan and more Study notes Computer Science in PDF only on Docsity!

Schwartz’s Algorithm

In essence: don’t assume unbounded parallelism

Schwartz’s Algorithm

int nodeval'[t

];

forall(index

in (0..t

int tally;tally = ... + ...;for (int stride = 1; stride < t

;^ stride

*=^

2)^ {

if (index % (2 * stride) == 0)

tally += nodeval'[index + stride];} else {nodeval'[index] = tally;break;}

Generalized Reduce

What if you have an associate, commutative

〈op〉

that isn’t built in?• Implement Schwartz’s algorithm yourself, or• Use a

generalized reduce

template

5-

Generalized Reduce

data_t

array

[n]; result_t

result

tally_t

nodeval'[t

];

forall(index

in^ (0..t

tally_t

tally; tally

=^ localTally(mySize(array

,^ 0),

localize(array

for^ (int stride

=^ 1;

stride

<^ t;

stride

*=^ 2)

if^ (index

%^ (

*^ stride)

==^

0)^ {

tally

=^ combine

(tally,

nodeval'[index

+^ stride]);

}^ else

nodeval'[index]

=^ tally;

break;} } if^ (index

result

=^ reduceGen

(tally);

Generalized Reduce

You provide:•^ data_t

— type of input data

•^ tally_t

— type of intermediate data

•^ result_t

— type of result data

•^ tally_t

init

()^ — initialize local

•^ tally_t

accum

(tally_t, int, data_t[])

— local

•^ tally_t

combine

(tally_t, tally_t)

— global

•^ result_t

reduceGen

(tally_t)

— post-process

Generalized Reduce

Generalized Scan

data_t

array

[n];^

result_t

result

[n];

tally_t

nodeval'[t

],^ pval'[t

];

forall(index

in^ (0..t

-1))^

{

tally_t

tally

=^ localTally(mySize(array

,^ 0), localize(array

));

for^ (int

stride

=^ 1;

stride

<^ t;

stride

*=^ 2) {

if^ (index

%^ (

*^ stride)

==^ 0)

{

pval'[index+stride]

=^ tally;

/*^ left

total to right child */

tally

=^ combine

(tally,

nodeval'[index

  • stride]);

}^ else

{ nodeval'[index]

=^ tally;

break;} } tally_t^ ptally

=^ propagateParentTally(index);

localScanGen(ptally,

mySize(array

,^ 0),

localize(array

),^ localize(result

));

}

Generalized Scan

tally_t

propagateParentTally(int

index)

{ tally_t

ptally; if^ (index

==^

ptally

=^ init

else/* Parent

supplies

left

total

then

parent

ptally

=^ combine

(pval'[index],

pval'[index]);

/*^ Propagate

total

from

parent

to^ right

children

for^ (int stride

=^ 1;

stride

<^ t ;^ stride

*=^

2)^ {

if^ (index

%^ (

*^ stride)

==^ 0)

pval'[index+stride]

=^ ptally;

elsebreak;} return^

ptally; }

Generalized Scan

You provide:•^ data_t

— type of input data

•^ tally_t

— type of intermediate data

•^ result_t

— type of result data

•^ tally_t

init

()^ — initialize local

•^ tally_t

accum

(tally_t, int, data_t[])

— local

•^ tally_t

combine

(tally_t, tally_t)

— global

•^ tally_t

scanGen

(tally_t, int,data_t[], result_t[])

— finish