SDSU CS 662: Theory of Parallel Algorithms
SR Scan

[To Course Home Page]
San Diego State University -- This page last updated March 8, 1995
----------

Contents of SR Scan Lecture

  1. Concurrent All Sums
  2. "Parallel" All Sums
  3. All Sums Driver Program with Timing Problem
  4. All Sums Driver Program using Multiple Processors
  5. All Sums without Global Control

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 -> 
			receive data(X)
			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)

	var Adder[N]: cap All_Sums

	fa i := 1 to N ->
		Adder[i] := create All_Sums(i,N)
	af


	var Jump := 1


	do Jump < N ->
		co (k := 1 to N )
			Adder[i].ExchangeData(Adder, Jump )
		oc

		Jump +:= Jump
	od
	

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

end

All Sums Driver Program with Timing Problem

resource Add()
	import All_Sums

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

	var Adder[N]: cap All_Sums

	fa i := 1 to N ->
		Adder[i] := create All_Sums(i,N)
	af

	var Jump := 1


	do Jump < N ->
		fa (k := 1 to N )
			send Adder[i].ExchangeData(Adder, Jump)
		af
		Jump +:= Jump
	od
	
	fa i := 1 to N ->
		destroy Adder[i] 
	af

end

All Sums Driver Program using Multiple Processors

resource Add()
	import All_Sums

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

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

	var Machines[N]: cap vm
	var Adder[N]: cap All_Sums

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

	var Jump := 1

	do Jump < N ->
		co (k := 1 to N )
			Adder[i].ExchangeData(Adder, Jump )
		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

resource Add()
	import All_Sums

	var N := 8;

	var Adder[N]: cap All_Sums

	fa i := 1 to N ->
		Adder[i] := create All_Sums(i)
	af


	fa i := 1 to N ->
		send Adder[i].computeAllSums(Adder)
	af

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

end