#include <stdio.h>
#include <math.h>
+
+/* tree broadcast routine:
+ * 0 -(gen=1)-> 1
+ *
+ * 0 -(gen=2)-> 2
+ * 1 -(gen=2)-> 3
+ *
+ * 0 -(gen=4)-> 4
+ * 1 -(gen=4)-> 5
+ * 2 -(gen=4)-> 6
+ * 3 -(gen=4)-> 7
+ * ...
+ *
+ * In gen, processes (gen <= rank < 2*gen) will receive the broadcast data.
+ */
int main(int argc, char *argv[])
{
- int two_to_generation;
- int rank, size;
+ int gen, rank, size;
int to, from;
/* scan over a hypothetical virtual machine of 15 nodes */
for (rank = 0; rank < size; rank++) {
printf("rank %d", rank);
- /* two_to_generation reflects the steps in the tree broadcast */
- two_to_generation = 1;
- while (two_to_generation < size) {
-
- /* receive message */
- if (rank >= two_to_generation
- && rank < two_to_generation * 2) {
- from = rank - two_to_generation;
- if (from < size)
- printf(" -- from %d", from);
- }
-
- /* send message */
- if (rank < two_to_generation) {
- to = rank + two_to_generation;
+ /* gen reflects the generation (depth) in the tree broadcast */
+ gen = 1;
+ while (gen < size) {
+ if (rank >= 2 * gen) { /* wait for the data to come out */
+ printf("\t");
+ } else if (rank >= gen && rank < 2 * gen) { /* receive message */
+ from = rank - gen;
+ printf("\tfrom %d", from);
+ } else if (rank < gen) { /* send message */
+ to = rank + gen;
if (to < size)
- printf(" -- to %d", to);
+ printf("\tto %d", to);
}
- two_to_generation = 2 * two_to_generation;
+ gen *= 2;
}
/* done for a given rank */