#include <cm/cmmd.h>
#include <cm/timers.h>

#include "fish.h"

#define SIZE 256
#define VSIZE  	(SIZE+2)
/* the display window */
float ocean[SIZE+2][SIZE+2];

/* clear everything in the window */
zapshow()
{
	int i,j;

	for (i=0;i<VSIZE;++i)
		for (j=0;j<VSIZE;++j)
			ocean[i][j] = 0;
}

main(argc,argv)
int argc;
char **argv;
{
    struct arg_data args;
	int fish,tsteps,psteps,myfish;
	int ROW;
	int i,j,k;

        if (argc<4) {
                printf("syntax: %s #fish duration display_interval\n", argv[0]);
                return;
        }
        CMMD_enable();
        fish = atoi(argv[1]);
	tsteps = atoi(argv[2]);
	psteps = atoi(argv[3]);
	myfish = fish / CMMD_partition_size();
	ROW = SIZE / CMMD_partition_size();

	/* initialize X display */
	openX(VSIZE);
	openWindow(VSIZE,"Fish1");
	imageXregister(ocean,VSIZE);

	/* start the node program */
    args.fish = fish;
    args.tsteps = tsteps;
    args.psteps = psteps;
    CMMD_bc_from_host (&args, sizeof (struct arg_data));

	/* looping over the time steps */
	for (k=0,i=0;i<tsteps;++i) {
		/* display fish positions */
		if (++k > psteps) {
			k=0;
			zapshow();
			for (j=0;j<CMMD_partition_size();++j)
				CMMD_receive(j,0,&ocean[j*ROW+1],
					sizeof(float)*ROW*(SIZE+2));
			imageXdraw(ocean,VSIZE);
		}
	}

    printf ("Elapsed Time = %lf\n",
	    CMMD_reduce_from_nodes_double (0, CMMD_combiner_dmax));
}