Esempio n. 1
0
int main(int argc, char **argv)
{
  // Mensaje de bienvenida
  printf("¡¡Bienvenido al Juego De La Vida!!\n");

  // Argumentos de main
  int n = 10; // Valor del tamaño del mundo por defecto

  int option_index = 0;
  int c;

  static struct option long_options[] =
  {
    {"size", optional_argument, 0, 's'},
    {"manual", optional_argument, 0, 'm'},
    {"help", optional_argument, 0, 'h'},
    { 0, 0, 0, 0 }
  };

  while ((c = getopt_long(argc, argv, "s:m::h::", long_options,
                          &option_index)) != -1) {
    switch (c) {
    case 's':
      // Recogemos tamaño por argumento de main
      n = strtol(optarg, NULL, 0); 
      break;
    case 'm':
      // Recogemos tamaño por consola, pidiendolo al usuario
      printf("Ingresa el tamaño de tu mundo: ");
      scanf("%d", &n);
      break;
    case 'h':
      printf("++ Ayuda del Juego de La Vida -- Fran ++\n");
      printf("Ingresa -s para dar el tamaño de tu tablero directamente\n");
      printf("Ingresa --manual para dar el tamaño de tu tablero de manera manual por consola");
      return 0;
    default:
      printf("ERROR EN LA RECOGIDA DE ARGUMENTOS\n");
      exit(EXIT_FAILURE);
    }
  }


  printf("\n");

  struct gol *w;
  w = gol_alloc(n);
  if (!w) {
    perror("Error en gol_alloc -- main.c");
    exit(EXIT_FAILURE);
  }
  gol_init(w);
  
  int i;
  int pausa;
  for (i = 0; i < ITS; i++) {
    print(w);
    itera(w);
    printf("Escribe un número para continuar: ");
    scanf("%d",&pausa);
    printf("\n");
  }

  gol_free(w);
  
  return 0;
}
Esempio n. 2
0
int main(int argc, char *argv[])
{
    // TODO Initialize MPI.
    MPI_Init(&argc, &argv);

    // TODO Get process rank.
    int rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    // TODO Get number of processes.
    int num_procs;
    MPI_Comm_size(MPI_COMM_WORLD, &num_procs);


    // Initialize random number generator.
    srand(1 + rank);

    // Parse command line on rank 0 and broadcast to all.
    int width = 0, height = 0, generations = 0;
    if (rank == 0)
    {
        // Parse command line.
        if (argc == 4)
        {
            height = atoi(argv[1]);
            width = atoi(argv[2]);
            generations = atoi(argv[3]);
        }
        else
        {
            printf("Usage: %s  HEIGHT  WIDTH  GENERATIONS\n", argv[0]);
            int error = 0;
            // TODO Abort MPI.
            MPI_Abort(MPI_COMM_WORLD, error);
        }
    }
    
    // TODO Broadcast height.
    MPI_Bcast(&height, 1, MPI_INT, 0, MPI_COMM_WORLD);
    
    // TODO Broadcast width.
    MPI_Bcast(&width, 1, MPI_INT, 0, MPI_COMM_WORLD);
    
    // TODO Broadcast generations.
    MPI_Bcast(&generations, 1, MPI_INT, 0, MPI_COMM_WORLD);
    
    // Compute the width of my local grid.
    int my_width = width / num_procs;
    if (rank == num_procs - 1)
    {
        my_width = width - (num_procs - 1) * my_width;
    }
 
    // Initialize my local grid.
    gol_init(height, my_width);


    // Initialize my local grid.
    gol_init(height, my_width);

    // Randomize the global grid.
    gol_randomize();

    // Print the global grid.
    gol_print(height, width);

    // Count and print the global number of live cells.
    int num_alive = gol_count_alive();
    if (rank == 0)
    {
        printf("Number of live cells: %d\n\n", num_alive);
    }

    // Simulate the given number of generations.
    for (int gen = 0; gen < generations; ++gen)
    {
        // Sleep for a while to get an animation effect.
        sleep(1);

        // Advance the global grid.
        gol_advance();

        // Print the global grid.
        gol_print(height, width);

        // Count and print the global number of live cells.
        int num_alive = gol_count_alive();
        if (rank == 0)
        {
            printf("Number of live cells: %d\n\n", num_alive);
        }
    }

    // Clean up.
    gol_finalize();

    // TODO Finalize MPI.
    MPI_Finalize();
    return 0;
}