SDSU CS 662: Theory of Parallel Algorithms
Tree Broadcast

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

Contents of Tree Broadcast Lecture

  1. Tree Broadcast
  2. Tree Broadcast No Global Control
    1. BinaryNode - The Parent Resource
    2. Root Resource, Child of BinaryNode
    3. Leaf, Child of BinaryNode
    4. Sample Program Using BinaryNode, Root & Leaf
    5. Execution Order of Sample Program
  3. Tree Broadcast, No Global Create
    1. BinaryNode
    2. Leaf
    3. Root
    4. Using New BinaryNode
  4. Binary Search Tree
    1. Using SortNode

Tree Broadcast

resource BinaryNode

	op fromParent(value: int)
	op receiveValueFromParentFromParent()
	op sendValueToChildren()
	op setLinks(Left, Right: cap BinaryNode)

body BinaryNode(ID: int)
	var LeftChild:cap BinaryNode
	var RightChild:cap BinaryNode

	var MyValue := 0

	proc setLinks(Left, Right )
		LeftChild := Left
		RightChild := Right
	end

	proc receiveValueFromParent()
		receive fromParent(MyValue)
	end

	proc sendValueToChildren()
		send LeftChild.fromParent(MyValue)
		send RightChild.fromParent(MyValue)
	end

	final 
		write(ID, MyValue); 
	end
end
Tree Broadcast Main
resource Main()

	import BinaryNode

	var N: int; 
	writes("Size "); read(N)

	var StartValue: int; 
	writes("Start "); read(StartValue)

	var Tree[N]: cap BinaryNode

	fa i := 1 to N ->
		Tree[i] := create BinaryNode(i)
	af


	#Simulate Tree Structure, N must be odd

	fa i := 1 to floor(N/2)
		Tree[i].setLinks(Tree[2*i],Tree[2*i+1])	af

#Now we can broadcast 

	send Tree[1].fromParent(StartValue)



	fa J := 0 to floor(log(N,2)) ->

		co (I := 2**J to 2**(J+1) -1 st I <= N)

			Tree[I].receiveValueFromParent()

		oc


		co (I := 2**J to 2**(J+1) -1 st I <= N)

			Tree[I].sendValueToChildren()

		oc

	af

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

end

Tree Broadcast No Global Control


Use BinaryNode, Root and Leaf resources


BinaryNode - The Parent Resource

resource BinaryNode

	op broadcast(senderID, Value: int)

	op setLinks(Mom, Left, Right: cap BinaryNode)


body BinaryNode(ID: int)

	var LeftChild: 	cap BinaryNode
	var RightChild: 	cap BinaryNode
	var Parent: 	cap BinaryNode
	var MyValue := 0

	proc setLinks(Mom, Left, Right)
		LeftChild := Left
		RightChild := Right
		Parent := Mom
	end


	proc broadcast(senderID, Value)

		MyValue := Value

		if senderID = floor(ID/2) ->

			send LeftChild.broadcast(ID,MyValue)
			send RightChild.broadcast(ID,MyValue)

		[] senderID = 2*ID ->

			send RightChild.broadcast(ID,MyValue)
			send Parent.broadcast(ID,MyValue)

		[] senderID = 2*ID + 1 ->

			send LeftChild.broadcast(ID,MyValue)
			send Parent.broadcast(ID,MyValue)

		[] senderID = -1 ->

			send LeftChild.broadcast(ID,MyValue)
			send RightChild.broadcast(ID,MyValue)
			send Parent.broadcast(ID,MyValue)

		fi
	end broadcast

	final
		write(ID, MyValue);
	end

end


Root Resource, Child of BinaryNode


resource Root
	extend BinaryNode

body BinaryNode(ID: int)
	var LeftChild: 	cap BinaryNode
	var RightChild: 	cap BinaryNode
	var Parent: 	cap BinaryNode
	var MyValue := 0

	proc setLinks(Mom, Left, Right)
		LeftChild := Left
		RightChild := Right
	end

	proc broadcast(senderID, Value)
		MyValue := Value

		if senderID = floor(ID/2) ->

			send LeftChild.broadcast(ID,MyValue)
			send RightChild.broadcast(ID,MyValue)

		[] senderID = 2*ID ->
			send RightChild.broadcast(ID,MyValue)

		[] senderID = 2*ID + 1 ->
			send LeftChild.broadcast(ID,MyValue)

		[] senderID = -1 ->

			send LeftChild.broadcast(ID,MyValue)
			send RightChild.broadcast(ID,MyValue)
		fi
	end

	final  
		write(ID, MyValue)
	end
end

Leaf, Child of BinaryNode

resource Leaf
	extend BinaryNode

body Leaf(ID: int)
	var LeftChild: 	cap BinaryNode
	var RightChild: 	cap BinaryNode
	var Parent: 	cap BinaryNode
	var MyValue := 0

	proc setLinks(Mom, Left, Right)
		Parent := Mom
	end

	proc broadcast(senderID, Value)

		MyValue := Value

		if senderID = floor(ID/2) ->

			noop

		[] else ->

			send Parent.broadcast(ID,MyValue)

		fi
	end

	final
		write(ID, MyValue)
	end
end


Sample Program Using BinaryNode, Root & Leaf


resource Main()

	import BinaryNode, Root, Leaf

	var N: int; writes("Size "); read(N)

	var Tree[N]: cap BinaryNode


	#Create Nodes

	Tree[1] := create Root(1)		#Root

	fa i := 2 to int(N/2) ->			#Internal
		Tree[i] := create BinaryNode(i)
	af

	fa i := int(N/2)+ 1 to N ->		#Leaves
		Tree[i] := create Leaf(i)
	af
	

	#Simulate Tree Structure, N must be odd

	Tree[1].setLinks(noop,Tree[2],Tree[3])


	fa i := 2 to int(N/2)

		Tree[i].setLinks(Tree[int(i/2)],
									Tree[2*i],Tree[2*i+1])

	af

	fa i := int(N/2)+ 1 to N ->

		Tree[i].setLinks(Tree[int(i/2)],noop,noop)

	af

	var StartValue: int; 
	writes("Start ") 
	read(StartValue)

	var StartIndex: int; 
	writes("Start Index ") 
	read(StartIndex)

	Tree[StartIndex].broadcast(-1, StartValue)

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

end

Execution Order of Sample Program


for this output add to broadcast proc the line
	write("My ID ", ID, "From ",senderID, 
			"Value ", Value)


Size 15
Start 5
Start Index 15

My ID 15 From -1 Value 5
My ID 7 From 15 Value 5
My ID 14 From 7 Value 5
My ID 3 From 7 Value 5
My ID 6 From 3 Value 5
My ID 1 From 3 Value 5
My ID 12 From 6 Value 5
My ID 13 From 6 Value 5
My ID 2 From 1 Value 5
My ID 4 From 2 Value 5
My ID 5 From 2 Value 5
My ID 8 From 4 Value 5
My ID 9 From 4 Value 5
My ID 10 From 5 Value 5
My ID 11 From 5 Value 5



Size 15
Start 5
Start Index 2

My ID 2 From -1 Value 5
My ID 4 From 2 Value 5
My ID 5 From 2 Value 5
My ID 1 From 2 Value 5
My ID 8 From 4 Value 5
My ID 9 From 4 Value 5
My ID 10 From 5 Value 5
My ID 11 From 5 Value 5
My ID 3 From 1 Value 5
My ID 6 From 3 Value 5
My ID 7 From 3 Value 5
My ID 12 From 6 Value 5
My ID 13 From 6 Value 5
My ID 14 From 7 Value 5
My ID 15 From 7 Value 5

Tree Broadcast, No Global Create


BinaryNode


resource BinaryNode

	op broadcast(senderID, Value: int)

body BinaryNode(ID, N: int; Parent: cap BinaryNode)

	var LeftChild: 	cap BinaryNode := noop
	var RightChild: 	cap BinaryNode := noop
	var MyValue := 0

	if 2*ID <= int(N/2) ->

		LeftChild := create BinaryNode(2*ID, N, myresource())

		RightChild:= create BinaryNode(2*ID+1, N,myresource())

	[] 2*ID > int(N/2) and 2*ID < N ->

		LeftChild := create Leaf(2*ID, N, myresource())

		RightChild:= create Leaf(2*ID+1, N,myresource())

	[] 2*ID > int(N/2) and 2*ID = N ->

		LeftChild := create Leaf(2*ID, N, myresource())

	fi


	proc broadcast(senderID, Value) # as before


	final
		destroy LeftChild 
		destroy RightChild
	end
end


Leaf

resource Leaf

	extend BinaryNode

body Leaf(ID, N: int; Parent: cap BinaryNode)

	var LeftChild: 	cap BinaryNode := noop
	var RightChild: 	cap BinaryNode := noop
	var MyValue := 0

	proc broadcast(senderID, Value) # as before

end



Root


Add BinaryNode's startup code to the Root resource

Add BinaryNode's final code to the Root resource

Keep broadcast from earlier Root

Using New BinaryNode

resource Main()

	import BinaryNode, Root, Leaf

	var N: int; writes("Size "); read(N)

	var Tree: cap BinaryNode


	#Create Nodes

	Tree := create Root(1, N, noop)		#Root


	var StartValue: int;
 
	writes("Start ") 

	read(StartValue)


	Tree[StartIndex].broadcast(-1, StartValue)


	destroy Tree 

end


Binary Search Tree

(No Root or Leaf Nodes)
resource SortNode

	op find( Value: int)
	op add(Value: int)


body BinaryNode(MyValue: int; Parent: cap BinaryNode)

	var LeftChild: 	cap BinaryNode := noop
	var RightChild: 	cap BinaryNode := noop

	LeftChild := noop
	RightChild := noop

	proc find(Value)
		if Value = MyValue ->
			#found it

		[] Value < MyValue and LeftChild =! noop ->
			LeftChild.find(Value)

		[] Value > MyValue and RightChild=! noop ->
			RightChild.find(Value)

		fi
	end



	proc add(Value)
		if Value = MyValue ->
			noop

		[] Value < MyValue and LeftChild =! noop ->
			LeftChild.add(Value)

		[] Value > MyValue and RightChild=! noop ->
			RightChild.add(Value)

		[] Value < MyValue and LeftChild == noop ->
			LeftChild := create BinaryNode(Value, myresource()) 

		[] Value > MyValue and RightChild == noop ->
			RightChild := create BinaryNode(Value, myresource())

		fi

	end

end

Using SortNode

resource Main()

	import SortNode

	var SearchTree: cap SortNode

	SearchTree := create SortNode(10, noop)

	SearchTree.add(3)

	SearchTree.add(9)

	SearchTree.add(15)

	SearchTree.add(4)

	send SearchTree.add(76)

	send SearchTree.add(53)

	send SearchTree.add(2)

	send SearchTree.add(8)

	send SearchTree.find(76)

end