From cohen@myri.com Mon Mar 13 16:39:30 2000
To: finucane@myri.com
Cc: cohen@myri.com
Subject: my message passing program
Date: Mon, 13 Mar 2000 16:33:42 -0800
From: Danny Cohen <cohen@myri.com>

//-*- c++ -*-

//#include "d-lib.h"

#define RIGHT 1
#define LEFT  2
#define UP    3
#define DOWN  4

#define N 10

struct NODE
{
  int x,y ;         // coordinates
  double T[5] ;     // T[0] own temp. and neighbors'
  int neighbors ;   // number of neighbors inside
  int flag[5] ;     // 1: if msg received from that neighbor
  int ready ;       // 1: ready to send
} ;

void init_node(struct NODE * node, int x, int y, double temp)
{
  node->x = x ;
  node->y = y ;
  node->T[0] = temp ;
  node->ready = 1 ;
  node->T[RIGHT]   =node->T[LEFT]   =node->T[UP]   =node->T[DOWN]    = 0 ;
  node->flag[RIGHT]=node->flag[LEFT]=node->flag[UP]=node->flag[DOWN] = 0 ;
  node->neighbors = 4 ;
  if (x==0) { node->neighbors-- ; node->flag[LEFT ] = 1 ; }
  if (x==N) { node->neighbors-- ; node->flag[RIGHT] = 1 ; }
  if (y==0) { node->neighbors-- ; node->flag[DOWN ] = 1 ; }
  if (y==N) { node->neighbors-- ; node->flag[UP   ] = 1 ; }
}

void run_node(struct NODE * node) 
{
  int i ;
  if (node->ready)  // if ready to send
    {
      /*
      if (x>0) { node->flag[LEFT ] = 0; send (to=x-1,y) (data=x,y,node->T[0]);}
      if (x<N) { node->flag[RIGHT] = 0; send (to=x+1,y) (data=x,y,node->T[0]);}
      if (y>0) { node->flag[DOWN ] = 0; send (to=x,y-1) (data=x,y,node->T[0]);}
      if (y<N) { node->flag[UP   ] = 0; send (to=x,y+1) (data=x,y,node->T[0]);}
      */
      node->ready = 0 ;
      node_rcv() ;
      return;
    }
  if (node->neighbors<4) { node->ready = 1 ; return ; }


  if (node->ready==0)
    if (node->flag[RIGHT])
      if (node->flag[LEFT])
	if (node->flag[UP])
	  if (node->flag[DOWN])
	    {
	      node->T[0] = (node->T[RIGHT] + node->T[LEFT] 
                          + node->T[UP] + node->T[DOWN]) / node->neighbors ;
	      node->ready = 1 ;
	    }
}


int main(void)
{
  int i,j,k,n,L = 9 ;
  double tmp ;
  struct NODE * node ;

  node = (struct NODE *)malloc(200*sizeof(struct NODE));
  for (k=0 ; k<100 ; k++)
    {
      tmp = 0 ;
      if (k<10) tmp = 100 ;
      if (k>89) tmp = 100 ;
      init_node(&node[k],(k%10),(k/10),tmp) ;
    }

  for (n=0 ; n<20 ; n++) for (k=0 ; k<100 ; k++) run_node(&node[k]) ;

  printf("\n") ;
  for (i=0; i<10 ; i++)
    {
      printf("\n") ;
      for (j=0 ; j<10 ; j++) printf("%7.2f)",node[10*i+j].T[0]) ;
    }
  printf("\n\n") ;
}
    

/**

The receive function:

When a msg is received by node(x,y)
{
  if (from=x-1,y) {flag[LEFT ]=1 ; node->T[LEFT ] = temp from the msg ; }
  if (from=x+1,y) {flag[RIGHT]=1 ; node->T[RIGHT] = temp from the msg ; }
  if (from=x,y-1) {flag[DOWN ]=1 ; node->T[DOWN ] = temp from the msg ; }
  if (from=x,y+1) {flag[UP   ]=1 ; node->T[UP   ] = temp from the msg ; }
}

**/

--------


