plot_image.py: use modern print() syntax everywhere
[parallel_computing.git] / src / tree_broadcast / print_tree_2.c
1        /* skeleton for a broadcast routine from node 0 */
2
3 #include <stdio.h>
4 #include <math.h>
5 #include <stdlib.h>
6
7
8 int main ( )
9 {
10   int rank, size;
11                                      /* scan over hypotetical virtual */
12                                      /* machine of 15 nodes           */
13   size = 15; 
14   for ( rank =0 ; rank<size ; rank++ )
15     {
16       my_broadcast( rank, size );
17     }
18 }
19
20
21
22 int my_broadcast( int rank, int size )
23 {
24   int two_to_generation;
25   int Nto, to, from;
26   int * target;
27
28   target = (int *)malloc( (unsigned) size * sizeof(int) );
29
30   printf( " rank = %d ", rank );
31                                      /* two_to_generation reflects the */ 
32                                      /* steps in the tree broadcast    */
33   Nto = 0;
34   two_to_generation = 1;
35   while( two_to_generation < size )
36     {                                /* receive message */
37       if ( rank >= two_to_generation && 
38                rank < two_to_generation*2 )
39         {
40           from = rank - two_to_generation;
41           if ( from < size )
42             printf ( " -- from %d ", from );
43         }
44       if ( rank < two_to_generation )
45         {                            /* send message */
46           to = rank + two_to_generation;
47           if ( to < size )
48             {
49               target[Nto] = to;
50               Nto++;
51             }
52         }
53       two_to_generation = 2 * two_to_generation;
54     }
55                                      /* done for a given rank */
56   printf("\n");
57     
58   if (Nto > 0 )
59     {
60       for ( to=0 ; to<Nto ; to++ )
61         {
62            printf( " -- to %d ", target[ to ] );
63         }
64       printf("\n");
65     }
66
67   free( target );
68     
69   return 0;
70 }