Example #1
0
int main(int argc,char * argv[])
{
	check_input(argc,argv);					// Simple argument number checking

	struct timespec start, end;				// Initialize
	long coords_within_lim = 0;				// vars needed for
	int coll = atoi(argv[1]);				// configuration 
	int runtime = atoi(argv[2]);			// and basic		
	char *file = argv[3];					// calculations
	int threads_num = atoi(argv[4]);		// in order to get			   
	int proc_num = atoi(argv[5]);			// the desired output
	float coords_val[3] = {0, 0, 0};
	long time_elapsed = 0;
	
	long loop_count = calc_lines(file);
	if(coll != -1)
	{
		if(coll>loop_count)
		{
			printf("[!] Warning: Specified collisions to be tested exceed the ones in input file\n");
			printf("[!] Setting the number of collisions to the maximum (taken from input file)\n");
		}
		else
		{
			loop_count = coll;
		}
	}

	FILE *input = fopen(file, "r");			// File opening
	if(!input)
	{
		printf("[!] Input file does not exist.\nExiting...\n");
		exit(3);
	}

	clock_gettime(CLOCK_MONOTONIC, &start);		// Initialize time calculation
	int i;
	for(i=0; i<loop_count; i++)					// The main loop of the program
	{
		fscanf(input, "%f %f %f", &coords_val[0], &coords_val[1], &coords_val[2]);
		if(coords_val[0] >= MIN_LIM && coords_val[0] <= MAX_LIM && coords_val[1] >= MIN_LIM && coords_val[1] <= MAX_LIM && coords_val[2] >= MIN_LIM && coords_val[2] <= MAX_LIM)
		{
			coords_within_lim++;		// If the current coordinate is within the accepted limits, update the number of accepted coordinates
		}
	}
	clock_gettime(CLOCK_MONOTONIC, &end);		// Stop the timer 
	time_elapsed = calc_time(start, end, 1);	// Calculate the time elapsed
	printf("[+] %ld coordinates have been read\n[+] %ld cooordinates were inside the area of interest\n[+] %ld coordinates read per second\n", loop_count, coords_within_lim, loop_count/time_elapsed);

	fclose(input);						// Close the file
	printf("[+] Done! \n" );

	return 0;
}
Example #2
0
int main(int argc, char* argv[])
{
  int numprocs, rank, edge, pixel_count, start, end;
  double max_values_sq;
  Uint32 max_iter;

#ifdef MANUAL
  VT_USER_START("main");
#endif
  
  MPI_Init(&argc, &argv);
  MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);

  if(numprocs <= 1)
  {
    fprintf(stderr, "%s: error: requires at least two MPI processes",
            argv[0]);
#ifdef MANUAL
  VT_USER_END("main");
#endif
    return 1;
  }
  
  max_values_sq = 4.0;
  max_iter = 5000;

  edge = (MAX_X * MAX_Y) / (numprocs - 1);

  if(rank > 0)
  {
    int i = rank - 1;

    Uint32* pixels;

    start = i * edge;
    end = (i == numprocs - 2) ? MAX_X * MAX_Y : (i + 1) * edge;
    pixel_count = end - start;

    pixels = malloc(pixel_count * sizeof(Uint32));
    calc_lines(start, end, pixels, max_values_sq, max_iter);

    MPI_Send((void*)pixels, pixel_count, MPI_INT, 0, 0, MPI_COMM_WORLD);
    free(pixels);
  }
  else /* rank == 0 */
  {
    int i, recv_count = (edge + 1);

    Uint32* field = malloc(MAX_X * MAX_Y * sizeof(Uint32));
    Uint32* fieldpos;

    SDL_Surface* pic;
    SDL_Event event;
        
    MPI_Status status;

    for(i = 1; i < numprocs; i++)
    {
      start = (i - 1) * edge;
      end = (i == numprocs - 1) ? MAX_X * MAX_Y : i * edge;

      pixel_count = end - start;
      recv_count = pixel_count;

      fieldpos = field+start;

      MPI_Recv(fieldpos, recv_count, MPI_INT, i, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
    }
    
    SDL_Init(SDL_INIT_EVERYTHING);

    pic = SDL_SetVideoMode(MAX_X, MAX_Y, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
    SDL_WM_SetCaption("Mandelbrot", "Mandelbrot");

    draw(pic, field);

    SDL_Flip(pic);
   
    do
    {
      SDL_Delay(50);
      SDL_PollEvent(&event);
    } while( event.type != SDL_QUIT && event.type != SDL_KEYDOWN );
        
    SDL_FreeSurface(pic);
    SDL_Quit();

    free(field);
  }

  MPI_Finalize();

#ifdef MANUAL
  VT_USER_END("main");
#endif

  return 0;
}
int main(int argc, char* argv[])
{
  std::chrono::time_point<std::chrono::high_resolution_clock> tStart;
  std::chrono::time_point<std::chrono::high_resolution_clock> tStop;
  typedef std::chrono::duration<int,std::milli> millisecs_t ;

  int numprocs, rank, edge, pixel_count, start, end;
  double max_values_sq;
  Uint32 max_iter;
  
  MPI_Init(&argc, &argv);
  MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);

  if(numprocs <= 1)
  {
    std::cerr << argv[0] << ": error: requires at least two MPI processes\n";
    return 1;
  }
  
  max_values_sq = 4.0;
  max_iter = 5000;

  edge = (MAX_X * MAX_Y) / (numprocs - 1);

  if(rank > 0)
  {
    int tile = rank - 1;

    Uint32* pixels;

    start = tile * edge;
    end = (tile == numprocs - 2) ? MAX_X * MAX_Y : (tile + 1) * edge;
    pixel_count = end - start;

    pixels = (Uint32*) malloc(pixel_count * sizeof(Uint32));
    calc_lines(start, end, pixels, max_values_sq, max_iter);

    MPI_Send((void*)pixels, pixel_count, MPI_INT, 0, 0, MPI_COMM_WORLD);
    free(pixels);
  }
  else /* rank == 0 */
  {

    int tile, recv_count = (edge + 1);
    char title[100];

    Uint32* field = (Uint32*) malloc(MAX_X * MAX_Y * sizeof(Uint32));
    Uint32* fieldpos;

    SDL_Surface* sdlSurface;
    SDL_Event event;
        
    MPI_Status status;

    tStart = std::chrono::high_resolution_clock::now();
    for(tile = 1; tile < numprocs; tile++)
    {
      start = (tile - 1) * edge;
      end = (tile == numprocs - 1) ? MAX_X * MAX_Y : tile * edge;

      pixel_count = end - start;
      recv_count = pixel_count;

      fieldpos = field+start;

      MPI_Recv(fieldpos, recv_count, MPI_INT, tile, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
    }
    tStop = std::chrono::high_resolution_clock::now();
    millisecs_t duration( std::chrono::duration_cast<millisecs_t>(tStop-tStart) ) ;
    long elapsed = duration.count();
    
    SDL_Init(SDL_INIT_EVERYTHING);

    sdlSurface = SDL_SetVideoMode(MAX_X, MAX_Y, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
    
    std::stringstream ss;
    ss << argv[0] << " " 
    << numprocs << " processes "
    << elapsed*1.e-3 << " sec."
    << "\n";

    SDL_WM_SetCaption(ss.str().c_str(), title);
    std::cout << ss.str().c_str() << "\n";

    draw(sdlSurface, field);

    SDL_Flip(sdlSurface);
   
    do {
      SDL_Delay(50);
      SDL_PollEvent(&event);
    } while( event.type != SDL_QUIT && event.type != SDL_KEYDOWN );
        
    SDL_FreeSurface(sdlSurface);
    SDL_Quit();

    free(field);
  }

  MPI_Finalize();

  return 0;
}