/* mod_gen Version 1 */ /* Module Name: "Life_WT" (Input) (Subroutine) */ /* Author: Steve Thorpe,NCSC.ext 161 */ /* Date Created: Wed Apr 8 15:29:42 1992 */ /* Based on Wavetracer's lifeWT.mc */ #include #include #include #include #include static int gens; extern void life(); int dims0[2]; int *imagei; char *multi_image; int frequency; AVSfield_char *imagep; AVSfield_char **image = &imagep; #define INDEX(i,j) ((i)*ny+(j)) /* #define DEBUG_MODE */ /* *****************************************/ /* Module Specification */ /* *****************************************/ int Life_WT_spec() { int in_port, out_port, param; extern int Life_WT_compute(); AVSset_module_name("Life_WT", MODULE_DATA); credits(); out_port = AVScreate_output_port("image", "field 2D 2-space 4-vector byte"); param = AVSadd_parameter("X Procs", "integer", 150, 3, 1024); AVSconnect_widget(param, "typein_integer"); AVSadd_parameter_prop(param, "width", "integer", 3); param = AVSadd_parameter("Y Procs", "integer", 150, 3, 1024); AVSconnect_widget(param, "typein_integer"); AVSadd_parameter_prop(param, "width", "integer", 3); param = AVSadd_parameter("Generations", "integer", 50, 1, 1000); AVSconnect_widget(param, "typein_integer"); AVSadd_parameter_prop(param, "width", "integer", 3); param = AVSadd_parameter("Display Every", "integer", 1, 1, 100); AVSconnect_widget(param, "typein_integer"); AVSadd_parameter_prop(param, "width", "integer", 3); param = AVSadd_parameter("Go for it!", "oneshot", 0, 0, 0); AVSconnect_widget(param, "oneshot"); AVSadd_parameter_prop(param, "width", "integer", 3); AVSadd_parameter_prop(param, "layout", "string_block", "port_vis $Module:\"Go for it!\" -on"); } /* *****************************************/ /* Main Compute Routine */ /* *****************************************/ main (argc, argv) int argc; char *argv[]; { int x, y, generations, go; AVScorout_init(argc,argv,Life_WT_spec); for (;;) { AVScorout_wait(); AVScorout_input(&x, &y, &generations, &frequency, &go); if (go == 1) Life_WT_compute( x, y, generations, go ); } } /* *****************************************/ /* Module Compute Routine */ /* *****************************************/ int Life_WT_compute( x, y, generations, go ) int x, y, generations, go; { int z = 1; #ifdef DEBUG_MODE printf("x == %d\n", x); printf("y == %d\n", y); printf("generations == %d\n", generations); printf("go == %d\n", go); #endif gens = generations; printf("LifeWT using %dx%dx%d processors for %d generations\n", x, y, z, generations ); /* Allocate space for image output */ dims0[0] = x; dims0[1] = y; *image = (AVSfield_char *) AVSdata_alloc("field 2D 2-space 4-vector byte", dims0); imagei = (int *) (*image)->data; multi_image = malloc((size_t) x*y); if (*image == NULL || multi_image == NULL) perror("lifeWT can't allocate enough space!\n"); memset(multi_image, '\0', x*y); if (multi_perform(life,x,y,z) != 0) perror("lifeWT multi_perform()\n"); AVSdata_free("field", *image); free(multi_image); return(1); } typedef unsigned cell :1; typedef unsigned count :3; typedef unsigned coor :12; int print_live_cells(gen, nx, ny) int gen, nx, ny; { int states = 0; int i, j, index; for (i = 0; i < nx; i++) { for (j = 0; j < ny; j++) { index = INDEX(i,j); if (imagei[index]) states++; #ifdef DEBUG_MODE if (imagei[index]) printf("X"); else printf("-"); #endif } #ifdef DEBUG_MODE printf("\n"); #endif } printf("Generation %d, %d live cells\n", gen, states); } void life (void) { multi cell last,state,border; multi count neigh; coor cx,cy; int i, gen = gens; int x, y; int nx = multi_x_size(); int ny = multi_y_size(); int index; border = multi_x_le(0) || !multi_x_le(multi_x_size()-2) || multi_y_le(0) || !multi_y_le(multi_y_size()-2); cx = multi_x_size()/2; cy = multi_y_size()/2; state = multi_x_le(cx) && !multi_x_le(cx-1) && multi_y_le(cy) && !multi_y_le(cy-1); if (!border) state = state | [-1;-1] state | [ 0;-1] state | [ 0;+1] state | [+1; 0] state; printf("Generation %d, %d live cells\n",0,(+=state)); last = 0; if (!border) { for (i =1; i <= gen; i++) { last = state; neigh = [-1;+1] last + [ 0;+1] last + [+1;+1] last + [-1; 0] last + [+1; 0] last + [-1;-1] last + [ 0;-1] last + [+1;-1] last; state = ( last & (neigh == 2 | neigh == 3)) | (!last & (neigh == 3)); if (i%frequency == 0) { unsigned int *ip = (unsigned int *) imagei; char *cp = multi_image; { multi char cimage = state; /* see example program A.3 in WT's multi C book */ printf("Generation %d, %d live cells\n", i, (+=state)); multi_to_uni_char(&cimage, multi_image, dims0[0], dims0[1], 1); } for (x = 1; x < nx - 1; x++) for (y = 1; y < ny - 1; y++) { index = INDEX(x,y); if (cp[index] != 0) ip[index] = 0XFFFFFFFF; else ip[index] = 0; } /*print_live_cells(i, nx, ny);*/ AVScorout_output(imagep); AVScorout_exec(); } } } } credits() { fprintf ( stderr,"\n\nLifeWT: Version 1.0 5/92\n"); fprintf ( stderr,"Module developed at the International AVS Center\n" ); fprintf ( stderr,"North Carolina Supercomputing Center, RTP, NC\n\n" ); }