## CS 662: Theory of Parallel Algorithms SR Scan

San Diego State University -- This page last updated March 8, 1995

## Concurrent All Sums

```resource All_Sums()
var	d := 1,
n:	int

getarg(1, n)

var sum[n], old[n]: int

procedure save(i: int)
old[i] := sum[i]
end

procedure update(i: int)
if i > d -> sum[i] +:= old[i-d] fi
end

fa i := 1 to n ->
sum[i] := i
af

do d < n ->
co (i := 1 to n) save(i) oc
co (i := 1 to n) update(i) oc
d +:= d
od

fa i := 1 to n ->
write(i, sum[i])
af

end
```

## "Parallel" All Sums

```
resource All_Sums
op ExchangeData(Processors[*]:cap All_Sums; Pass: int)
op data(value: int)

body All_Sums(ID: int; NumberOfProcess: int)

var MyListItem := 1

proc ExchangeData(Processors,Jump)

if (ID + Jump) <= NumberOfProcess  ->
send Processors[ID+Jump].data(MyListItem)
fi

var X: int

if ID > Jump ->
MyListItem +:= X
fi

end

final
write(ID, MyListItem )
end
end

```

All Sums Driver Program
```resource Add()
import All_Sums

var N := 8; writes("Size "); read(N)

fa i := 1 to N ->
af

var Jump := 1

do Jump < N ->
co (k := 1 to N )
oc

Jump +:= Jump
od

fa i := 1 to N ->
af

end
```

## All Sums Driver Program with Timing Problem

```resource Add()
import All_Sums

var N := 8; writes("Size "); read(N)

fa i := 1 to N ->
af

var Jump := 1

do Jump < N ->
fa (k := 1 to N )
af
Jump +:= Jump
od

fa i := 1 to N ->
af

end
```

## All Sums Driver Program using Multiple Processors

```resource Add()
import All_Sums

var hosts[N]: string[10] := ([N] "saturn")

fa i := 1 to N ->
locate(i, hosts[i])
af

var Machines[N]: cap vm

fa i := 1 to N ->
Machines[i] := create vm() on i
Machines[i]
af

var Jump := 1

do Jump < N ->
co (k := 1 to N )
oc
Jump +:= Jump
od

fa i := 1 to N -> destroy Adder[i] af

end
```

## All Sums without Global Control

```
resource All_Sums

op computeAllSums(Processors[*]:cap All_Sums)
op data(sender: int; value: int)

body All_Sums(ID: int)

var MyItem := 1

proc computeAllSums(Processors)

var N := ub(Processors)
var Jump := 1

do Jump < N ->

if (ID + Jump) <= N  ->
send Processors[ID+Jump].data(ID,MyItem)
fi

if ID > Jump ->
in data(sender,X) st sender = ID - Jump ->
MyItem +:= X
ni
fi

Jump +:= Jump
od
end

final
write(ID, MyItem)
end final
end
```

Driver without Global Control
```
import All_Sums

var N := 8;

fa i := 1 to N ->