int main(void) {
	int i, j, k;
	double time;
	FILE *output;
  struct timeval lt, ll;

	/* allocate memory and read input data */
	
	read_world();
	change_list = (cells**)malloc(sizeof(cells*)* world_size*world_size*world_size);
	next_change_list = (cells**)malloc(sizeof(cells*)* world_size*world_size*world_size);

	/* set timer */
  gettimeofday(&lt, NULL);
	

	/* core part */
	init_change_list();	
	for (i = 0; i < num_of_steps; i++)
	{	
		next_generation();
    if (change_list_size == 0){
			break;
		}	
	}

	/* set timer and print measured time*/
	gettimeofday(&ll, NULL);
	time = (double)(ll.tv_sec - lt.tv_sec) + (double)(ll.tv_usec - lt.tv_usec) / 1000000.0;
	fprintf(stderr, "Time : %.6lf\n", time);
  
  /* write output file */
	output = fopen("output.life", "w");
	fprintf(output, "%d %d %d %d %d %d 1\n", world_size, D1, D2, L1, L2, num_of_steps);
	for (i = 0; i < world_size; i++)
	{
		for (j = 0; j < world_size; j++)
		{
			for (k = 0; k < world_size; k++)
			{
				fprintf(output, "%d ", cube[i][j][k].status);
			}
			fprintf(output, "\n");
		}
	}
	fclose(output);
	return 0;
}
Example #2
0
File: main.c Project: bueti/my_gol
/*
 * Main
 */
int main(int argc, char *argv[]) {
  // Parse command line
  if(!optparse(argc, argv))
    abort();

  /* Nanosleep Setup */
  int milisec = 250; // length of time to sleep, in miliseconds
  struct timespec req = {0};
  req.tv_sec = 0;
  req.tv_nsec = milisec * 1000000L;

  // Initialisieren der Welt und den Zellen
  world_t world;

  /* Allocate Memory */
  allocateMemory(&world);

  /* Welt befüllen */
  if(path) {
    read_world(&world, path);
  } else if (!create_world(&world)) {
      printf("error creating world\n");
      exit(-2);
  } else {
    fillWorld(&world);
  }

  /* Welt ausgeben */
  printWorld(&world);

  /*
   * Nächster Schritt ausführen
   * TODO: Beenden der Schleife wenn sich nichts mehr ändert
   */
  while (true) {
    (automode) ? nanosleep(&req, (struct timespec *)NULL) : bwait();
    nextStep(&world);
    printWorld(&world);
  }
  return 0;
}
Example #3
0
int main (int argc, char ** argv)
{
	struct world *world;
	FILE *f;
	int c, ret;
	int head_steps, tail_steps, skip_steps, next_step;
	int ants_count, resolution;
	int step;
	int performance_test;
	int window_width;
	int window_height;
	int hex_size;

	dump_format = 0;
	step = 0;
	head_steps = 0; 
	tail_steps = 0; 
	skip_steps = 1;
	performance_test = 0;

	window_width = 800;
	window_height = 614;
	hex_size = 3;

/* These are used for dump format, since it is difficult to figure them out. */
	resolution = 10;
	ants_count = 32;

	world = NULL;

	while ((c=getopt (argc, argv, "wdh:t:s:a:p:W:H:c:")) != EOF) {
		switch (c) {
		case 'd': dump_format = 1; break;
		case 't': 
			if (!optarg) usage_and_exit (argv[0]);
			tail_steps = atoi (optarg); break;
		case 's': 
			if (!optarg) usage_and_exit (argv[0]);
			skip_steps = atoi (optarg); break;
                case 'r':
                        if (!optarg) usage_and_exit (argv[0]);
                        resolution = atoi (optarg); break;
                case 'a':
                        if (!optarg) usage_and_exit (argv[0]);
                        ants_count = atoi (optarg); break;
		case 'h': 
			if (!optarg) usage_and_exit (argv[0]);
			head_steps = atoi (optarg); break;
		case 'w': warnings = 1; break;
		case 'p': 
			if (!optarg) usage_and_exit (argv[0]);
			performance_test = atoi (optarg); 
			break;
		case 'W': 
			if (!optarg) usage_and_exit (argv[0]);
			window_width = atoi (optarg); break;
		case 'H': 
			if (!optarg) usage_and_exit (argv[0]);
			window_height = atoi (optarg); break;
		case 'c': 
			if (!optarg) usage_and_exit (argv[0]);
			hex_size = atoi (optarg); break;
		
		case '?': 
		default: usage_and_exit (argv[0]);
		}
	}

	if (skip_steps < 1 || tail_steps < head_steps) {
		fprintf (stderr, "Illegal step setting.\n");
		exit (-1);
	}

	if (dump_format) {
		next_step = head_steps;
		f = open_next_file_from_argv (argv);

		v_set_geometry (window_width, window_height, hex_size);
		v_initialize ();
		while (!feof (f)) {
			if (read_world_as_icfp_dump (&world, f, resolution, resolution, ants_count) == 0) {
				if (step <= head_steps ||
                                    step >= tail_steps ||
                                    step == next_step) {
					printf ("Step %d ...\n", step);
					display_world (world);
		    	    	    	v_refit_view ();
					if ((ret = v_poll_event (0))) {
					    if (ret == -1) {
						break;
					    }
					}

					next_step = skip_steps+step;
				}
			}
			step++;
		}
		fclose (f);
	} else {
		f = open_next_file_from_argv (argv);
		if (read_world (&world, f) != 0) {
			fprintf (stderr, "Error in reading world map.\n");
			exit (-1);
		}
		fclose (f);

		f = open_next_file_from_argv (argv);
		if (parse_world_trace (world, f) != 0) {
			fprintf (stderr, "Error in reading world trace.\n");
			exit (-1);
		}
		fclose (f);

		v_set_geometry (window_width, window_height, hex_size);
		v_initialize ();
		display_world (world);
		// v_refit_view ();
	}

	if (performance_test) {
		return run_performance_test (world, performance_test);
	} else {
		return run_main_loop (world);
	}
}
int main(void) {
	int i, j, k;
	double time;
	FILE *output;
	long thread, thread_c = 0;
	pthread_t *thread_handles;
	struct timeval lt, ll;
	int my_position;	

	//Muttex init
	pthread_mutexattr_init(&attr);
	pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_TIMED_NP);
	pthread_mutex_init(&lock, &attr);

	//Thread init
	thread_handles = (pthread_t *)malloc(num_of_threads*sizeof(pthread_t));



	/* allocate memory and read input data */
	read_world();
	change_list = (cells**)malloc(sizeof(cells*)* world_size*world_size*world_size);
	next_change_list = (cells**)malloc(sizeof(cells*)* world_size*world_size*world_size);

	/* set timer */
	gettimeofday(&lt, NULL);

	/* core part */
	init_change_list();	
	//clock_t start = clock();
	/* Whole lifecycle. hehe */
	for (i = 0; i < num_of_steps; i++)
	{
		printf("Generation number %d\n", i);
		size_to_handle = 1 + ((change_list_size - 1) / num_of_threads); //Single part to solve
		
		/*Update generation and every single neighbour count joint to change of state*/
		for (thread = 0; thread < num_of_threads; thread++)
		{
			pthread_create(&thread_handles[thread], NULL, next_generation, (void *)thread);
		}
		for (thread = 0; thread < num_of_threads; thread++)
		{
			pthread_join(thread_handles[thread], NULL);
		}

		/* Create new change list */
		for (thread = 0; thread < num_of_threads; thread++)
		{
			pthread_create(&thread_handles[thread], NULL, next_change_list_cr, (void *)thread);
		}
		for (thread = 0; thread < num_of_threads; thread++)
		{
			pthread_join(thread_handles[thread], NULL);
		}
		
		/* Copy next change list into change list */
		/*change_list = next_change_list;
		for (int  n= 0; n < next_change_list_size; n++)
		{
			change_list[n] = next_change_list[n];
		}*/
		//clock_t memstart = clock();
		
		memcpy(change_list, next_change_list, next_change_list_size*sizeof(cells**));
		
	//	clock_t memfinish = clock();
	//	float memtime = (float)(memfinish - memstart)/CLOCKS_PER_SEC;
	//	printf("time %f\n", memtime);

		//Reset for next generation
		change_list_size = next_change_list_size;
		next_change_list_size = 0;		
		if (change_list_size == 0){
			break;
		}
				
	}
//	clock_t end = clock();
//	float seconds = (float)(end - start) / CLOCKS_PER_SEC;
//	printf("time %f", seconds);
	/* set timer and print measured time*/

	gettimeofday(&ll, NULL);
	time = (double)(ll.tv_sec - lt.tv_sec) + (double)(ll.tv_usec - lt.tv_usec) / 1000000.0;
	fprintf(stderr, "Time : %.6lf\n", time);

	/* write output file */

	output = fopen("output.life", "w");
	fprintf(output, "%d %d %d %d %d %d %d\n", world_size, D1, D2, L1, L2, num_of_steps, num_of_threads);
	for (i = 0; i < world_size; i++)
	{
		for (j = 0; j < world_size; j++)
		{
			for (k = 0; k < world_size; k++)
			{
				fprintf(output, "%d ", cube[i][j][k].status);
			}
			fprintf(output, "\n");
		}
	}	
	fclose(output);	
	return 0;
}
int main(void) {
    int i, j, k;
    double time;
    FILE *output;
    long thread;
    pthread_t *thread_handles;
    struct timeval lt, ll;

    /* allocate memory and read input data */
    //read_world("input.life");
    read_world("input-300.life");
    thread_handles = malloc(num_of_threads*sizeof(pthread_t));
    /* set timer */
    gettimeofday(&lt, NULL);

    /* core part */
    init_change_list();
    for (i = 0; i < num_of_steps; i++)
    {
        size_to_handle = change_list_size / num_of_threads;
        list * current_node_p = (list *) malloc(sizeof(list));
        current_node_p = change_list;
        for (thread = 0; thread < num_of_threads; thread++)
        {
            for (j = 0; j < thread*size_to_handle; j++)
            {
                current_node_p = current_node_p->next;
            }
            pthread_create(&thread_handles[thread], NULL, update_from_change_list, (void *)current_node_p);
        }
        for (thread = 0; thread < num_of_threads; thread++)
        {
            pthread_join(thread_handles[thread, NULL]);
        }

        current_node_p = change_list;

        for (thread = 0; thread < num_of_threads; thread++)
        {
            for (j = 0; j < thread*size_to_handle; j++)
            {
                current_node_p = current_node_p->next;
            }
            pthread_create(&thread_handles[thread], NULL, check_next_gen_t, (void *)current_node_p);
        }
        for (thread = 0; thread < num_of_threads; thread++)
        {
            pthread_join(thread_handles[thread, NULL]);
        }
        //print_lists();
        change_list = next_change_list;
        current_node = next_change_list;
        //	printf("\n");
        change_list_size = next_change_list_size;
        next_change_list_size = 0;
        next_change_list = NULL;
        //next_generation();
        printf("Pruchod cislo %d\n", i);
        //print_matricies();

    }

    /* set timer and print measured time*/
    gettimeofday(&ll, NULL);
    time = (double)(ll.tv_sec - lt.tv_sec) + (double)(ll.tv_usec - lt.tv_usec) / 1000000.0;
    fprintf(stderr, "Time : %.6lf\n", time);
    /* write output file */
    output = fopen("output_user.life", "w");
    fprintf(output, "%d %d %d %d %d %d %d\n", world_size, D1, D2, L1, L2, num_of_steps, num_of_threads);
    for (i = 0; i < world_size; i++)
    {
        for (j = 0; j < world_size; j++)
        {
            for (k = 0; k < world_size; k++)
            {

                fprintf(output, "%d ", cube[i][j][k].status);
            }
            fprintf(output, "\n");
        }
    }
    printf("%d %d", cube[0][0][2].num_of_neighbours, cube[0][0][2]);
    fclose(output);
    //print_matricies();
    return 0;
}
Example #6
0
int
main(int argc, char **argv)
{
#ifdef THREADS
    int my_index;
    int *my_index_p;
    pthread_key_create (&index_key, (void*)free_registers);
#endif

#ifdef THREADS
    my_index_p = (int *)malloc (sizeof (int));
    *my_index_p = get_next_index();
    pthread_setspecific (index_key, (void*)my_index_p);
    my_index_p = pthread_getspecific(index_key);
    my_index = *my_index_p;
    gc_ready[my_index] = 0;
    /* inc_next_index();*/
    value_stack_array[my_index] = (oakstack*)malloc (sizeof (oakstack));
    cntxt_stack_array[my_index] = (oakstack*)malloc(sizeof (oakstack));
    value_stack.size = 1024;
    value_stack.filltarget = 1024/2;
    context_stack.size = 512;
    context_stack.filltarget = 512/2;
    gc_examine_ptr = gc_examine_buffer;
#endif

    parse_cmd_line(argc, argv);

    init_weakpointer_tables();

    init_stacks();

    read_world(world_file_name);

    new_space.size = e_next_newspace_size = original_newspace_size;
    alloc_space(&new_space, new_space.size);
    free_point = new_space.start;

#ifdef THREADS
    register_array[my_index] = (register_set_t*)malloc(sizeof(register_set_t));
#else
    reg_set = (register_set_t*)malloc(sizeof(register_set_t));
#endif

    /* Set the registers to the boot code */

    e_current_method = e_boot_code;
    e_env = REF_TO_PTR(REF_SLOT(e_current_method, METHOD_ENV_OFF));
    e_code_segment = REF_SLOT(e_current_method, METHOD_CODE_OFF);
    e_pc = CODE_SEG_FIRST_INSTR(e_code_segment);

    /* Put a reasonable thing in e_bp to avoid confusing GC */
    e_bp = e_env;

    /* Tell the boot function the truth */
    e_nargs = 0;

    /* Big virtual machine interpreter loop */
    loop(INT_TO_REF(54321));

    return 0;
}