Пример #1
0
int main(void) {
	char c;
	
	init_robot();
	fflush(stdin);
	
	printf("Usage: %c to exit, %c to move forward, %c to move backward, %c to rotate to left, %c to rotate to right!\n\n", EXIT_S, MF_S, MB_S, RL_S, RR_S);

    print_world();
	scanf("%c", &c);
	
	while(c != EXIT_S) {
		switch(c) {
			case MF_S:
				move_robot(MOVE_FWD);
				print_world();
				break;
			case MB_S:
				move_robot(MOVE_BWD);
				print_world();
				break;
			case RL_S:
				rotate_90_left();
				print_world();
				break;
			case RR_S:
				rotate_90_right();
				print_world();
				break;
		}
		scanf("%c", &c);
	}
	
	return 0;
} 
Пример #2
0
int main(int argc, char **argv){

	#ifdef GETTIME
    double start = omp_get_wtime();
    #endif
        
	/* Maybe check for invalid input? */
	wolf_breeding_period = atoi(argv[2]);
	squirrel_breeding_period = atoi(argv[3]);
	wolf_starvation_period = atoi(argv[4]);
	number_of_generations = atoi(argv[5]);
		
	parse_input(argv[1]); /* Filename */

	start_world_simulation();

	print_world();

	#ifdef GETTIME
    printf("OpenMP time: %fs\n", omp_get_wtime() - start);
    #endif
    
    freemem();

	return 0;
}
Пример #3
0
int
main()
{
    char path[20];
    FILE *f;
    int i;

    memset(world, '.', W * H);

    for (i = 0; i < 14000; ++i)
    {
        if (i % 8 == 0)
        {
            fprintf(stderr, ".");
            sprintf(path, "349_%05d.pgm", i);
            f = fopen(path, "w");
            print_world(f);
            fclose(f);
        }

        iterate();
    }

    return 0;
}
Пример #4
0
void gather(){
	MPI_Status status;

	//Sync to master
	if(taskid == MASTER){
		int i = top + 2;

		for(; i < grid_size; i++)
			MPI_Recv(world[i], grid_size, mpi_world_cell_type, MPI_ANY_SOURCE, RECV_TAG+i, MPI_COMM_WORLD, &status);

		print_world(grid_size);
	} else {
		int i = bottom;

		for(; i < payload; i++)
			MPI_Send(world[i], grid_size, mpi_world_cell_type, MASTER, RECV_TAG+GET_X(world[i][0].number), MPI_COMM_WORLD);
	}
}
Пример #5
0
int main(int argc, char * argv[]) {
	WINDOW * win = init_screen();
#ifdef ENABLE_CURSOR
	MEVENT event;
#endif

	/* predefined patterns */
	struct pattern patterns[] = {
		{3, 3, (uint8_t *) start},
		{3, 3, (uint8_t *) glider},
		{5, 4, (uint8_t *) segler},
		{3, 7, (uint8_t *) buffer},
		{8, 4, (uint8_t *) kreuz},
		{6, 6, (uint8_t *) ship}
	};
	struct cursor cur = {0, 0};
	struct config cfg;

	int generation = 0, input, framerate = 17;
	uint8_t width, height;
	uint8_t ** worlds[2], ** world;
#if defined(RANDOM_SPAWNS)
	int idle_gens = 0;
	srand(time(NULL));
#endif

	memset(&cfg, '\0', sizeof(struct config));
	/* initialize world */
	world = init_world(win, worlds, &width, &height);
	/* make the world real */
	inhabit_world(patterns[3], width/2, height/2, worlds[0], width, height);

	/* simulation loop */
	while(!cfg.quit) {
		if (!cfg.paused) {
			/* calc next generation */
			usleep(1 / (float) framerate * 1000000); /* sleep */
			calc_next_gen(world, worlds[++generation % 2], width, height);
			world = worlds[generation % 2]; /* new world */
		}

		/* handle events */
		switch (input = getch()) {
#ifdef ENABLE_HOTKEYS
			case '+': /* increase framerate */
				framerate++;
				break;

			case '-': /* decrease framerate */
				if (framerate > 1) framerate--;
				break;

			case 'q': /* quit */
				cfg.quit = 1;
				break;
#if defined(RANDOM_SPAWNS)
			case 'd': /* disable random spawn */
				rnd_spawns = ~rnd_spawns;
				break;
#endif
			case 'p': /* pause */
				cfg.paused ^= 1;
				break;

			case 'c': /* clean world */
				clean_world(world, width, height);
				generation = 0;
				break;
#endif
#if defined(ENABLE_HOTKEYS) || defined(ENABLE_CURSOR)
			case '0': /* insert pattern */
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
				inhabit_world(patterns[input - '0'], cur.x, cur.y, world, width, height);
				break;
#endif
#ifdef ENABLE_CURSOR
			case ' ': /* toggle cell at cursor position */
				world[cur.x][cur.y] = (world[cur.x][cur.y]) ? 0 : 1;
				break;

			case KEY_MOUSE: /* move cursor to mouse posititon */
				if (getmouse(&event) == OK && event.bstate & BUTTON1_PRESSED) {
					cur.x = event.x;
					cur.y = event.y;
					if (cur.x >= width) cur.x = width - 1;
					if (cur.y >= height) cur.y = height - 1;
					world[cur.x][cur.y] = (world[cur.x][cur.y]) ? 0 : 1;
				}
				break;

			case KEY_UP:
				if (cur.y > 0) {
					cur.y--;
				}
				break;

			case KEY_DOWN:
				if (cur.y < height-1) {
					cur.y++;
				}
				break;

			case KEY_LEFT:
				if (cur.x > 0) {
					cur.x--;
				}
				break;

			case KEY_RIGHT:
				if (cur.x < width-1) {
					cur.x++;
				}
				break;
			case KEY_RESIZE:
				world = resized(win, worlds, &width, &height);
				break;
#endif
		}

#if defined(RANDOM_SPAWNS)
		if (rnd_spawns) {
			/* spawn a new pattern at a random position, if nothing happens */
			idle_gens++;
			if (idle_gens >= RANDOM_SPAWNS && !cfg.paused)
			{
				idle_gens = 0;
				inhabit_world(patterns[rand() % (sizeof(patterns)/sizeof(patterns[0]))], rand() % (width - 1), rand() % (height - 1), world, width, height);
			}
		}
#endif

		/* update screen */
		print_world(world, width, height);
#ifdef ENABLE_CURSOR
		print_cursor(world, cur);
#endif
#ifdef ENABLE_STATUS
		attron(COLOR_PAIR(1));
		for (int i = 0; i < width; i++) mvprintw(0, width - i, " ");
		mvprintw(0, 0, "[generation:%4d] [cells:%3d] [fps:%2d] [width:%d] [height:%d] [cursor:%2d|%2d]", generation, calc_cell_count(world, width, height), framerate, width, height, cur.x, cur.y);
		if (cfg   .paused) mvprintw(0, width-6, "PAUSED");
		attroff(COLOR_PAIR(1));
#endif

#ifdef ENABLE_HOTKEYS
		if (cfg.paused) {
			print_menu(width, height);
			usleep(1);
		}
#endif
		refresh();
	}

	free_all(worlds, width, height);
	delwin(win);
	endwin(); /* exit ncurses mode */
	return (EXIT_SUCCESS);
}
Пример #6
0
int main(int argc, char **argv){

	int task, len, chunks = CHUNK;
	MPI_Status status;
	char hostname[MPI_MAX_PROCESSOR_NAME];

	#ifdef GETTIME
	double start = MPI_Wtime();
	#endif

	const int nitems=3;
	int blocklengths[3] = {2,2,1};
	MPI_Datatype types[3] = {MPI_CHAR, MPI_UNSIGNED_SHORT, MPI_UNSIGNED};
	MPI_Aint offsets[3];

	offsets[0] = offsetof(world_cell, type);
	offsets[1] = offsetof(world_cell, breeding_period);
	offsets[2] = offsetof(world_cell, number);

	/* MPI Initialization */
	if (MPI_Init(&argc, &argv) != MPI_SUCCESS) {
	printf ("Error starting MPI program. Terminating.\n");
		/*MPI_Abort(MPI_COMM_WORLD, ret);*/
		return -1;
	}

	MPI_Get_processor_name(hostname, &len);

	MPI_Type_create_struct(nitems, blocklengths, offsets, types, &mpi_world_cell_type);
	MPI_Type_commit(&mpi_world_cell_type);
        
    MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
    MPI_Comm_rank(MPI_COMM_WORLD, &taskid);

	if(taskid == MASTER){
		MPI_Request size_reqs[numtasks-1];

		info[1] = wolf_breeding_period = atoi(argv[2]);
		info[2] = squirrel_breeding_period = atoi(argv[3]);
		info[3] = wolf_starvation_period = atoi(argv[4]);
		info[4] = number_of_generations = atoi(argv[5]);

		parse_input(argv[1]);

		info[0] = grid_size;

		bottom = 0;
		top = chunk_size = CHUNK;
		payload = top + 2;

		for(task = 1; task < numtasks; task++)
			MPI_Isend(info, 5, MPI_INT, task, INIT_TAG, MPI_COMM_WORLD, &size_reqs[task-1]);

		MPI_Waitall(numtasks - 1, size_reqs, MPI_STATUS_IGNORE);

		for(task = 1; task < numtasks; task++){
			int bottom_task = FLIMIT_INF_CHUNK(task),
				top_task = FLIMIT_SUP_CHUNK(task),
				chunk_size = top_task-bottom_task;
			
			bottom_task -= 2;

			if (task == numtasks-1)
				top_task += CHUNK_REMAINDER;
			else
				top_task += 2;

			for( ; bottom_task < top_task; bottom_task++)
				MPI_Send(world[bottom_task], grid_size, mpi_world_cell_type, task, FILL_TAG, MPI_COMM_WORLD);
		}

	} else {
		int j = 0;

		MPI_Recv(info, 5, MPI_INT, MASTER, INIT_TAG, MPI_COMM_WORLD, &status);

		grid_size = info[0];
		wolf_breeding_period = info[1];
		squirrel_breeding_period = info[2];
		wolf_starvation_period = info[3];
		number_of_generations = info[4];
	
		bottom = 2;
		if(taskid == numtasks-1){
			chunk_size = CHUNK + CHUNK_REMAINDER;
			payload = top = chunk_size+bottom;
		} else {
			chunk_size = CHUNK;
			top = chunk_size+bottom;
			payload = top + 2;
		}

		initialize_world_array(payload );
		
		for( ; j < payload; j++)
			MPI_Recv(world[j], grid_size, mpi_world_cell_type, MASTER, FILL_TAG, MPI_COMM_WORLD, &status);
	}

	start_world_simulation();

	gather();

	#ifdef GETTIME
	if(taskid == MASTER){
	  printf("MPI time: %lf\n", MPI_Wtime() - start);
	  print_world(grid_size);
	}
	#endif

	//freemem();
	MPI_Finalize();

	return 0;
}