Esempio n. 1
0
void Segment_outfile (char* map_name, char* mapset, int* map_fd,
		         char* seg_name, int* seg_fd, SEGMENT* seg, char* prompt, int fract,
                 RASTER_MAP_TYPE data_type)
{
    int nrows;
    char* mapset_address;

    mapset_address = G_ask_cell_new (prompt, map_name);
    
	if (mapset_address == NULL)
	  G_fatal_error ("Can't continue without filename ");
	
    strcpy (mapset, mapset_address);
    
	if ((*map_fd = G_open_cell_new (map_name)) < 0)
	  G_fatal_error ("Can't create output layer ");

    nrows = G_window_rows ();
    strcpy (seg_name, Create_segment_file (nrows, fract, data_type));
    *seg_fd = open (seg_name, 2);
    
	if (*seg_fd < 0)
	  G_fatal_error ("Can't open segment disk file ");
	
    segment_init (seg, *seg_fd, 4);
}
Esempio n. 2
0
/* store a raster map in a mem segment on disk */
void Segment_infile (char* map_name, char* mapset, char* search_mapset, int* map_fd, 
		char* seg_name, int* seg_fd, SEGMENT* seg, void *cell_buf, int fract, 
        RASTER_MAP_TYPE data_type)
{
    int nrows, row;

    strcpy (mapset, G_find_cell (map_name, search_mapset));
    if (mapset == NULL)
	  G_fatal_error ("Can't find displayed data layer ");
    if ((*map_fd = G_open_cell_old (map_name, mapset)) < 0)
	  G_fatal_error ("Can't open displayed data layer ");

    nrows = G_window_rows ();

	/* next line creates the actual segment, returns name of disk file */
    strcpy (seg_name, Create_segment_file (nrows, fract, data_type));
    *seg_fd = open (seg_name, O_RDWR);

    if (*seg_fd < 0)
	  G_fatal_error ("Cannot open segment disk file ");
	
    segment_init (seg, *seg_fd, 4);
	
	/* store map data in segment file IGNORE MASK */
	/* cell_buf must be pre-allocated by caller! */
    for (row = 0; row < nrows; row++) {
	    if (G_get_raster_row_nomask (*map_fd, cell_buf, row, data_type) < 0) {
	        G_fatal_error ("Unable to read data layer into segment file ");
        }		
	    segment_put_row (seg, cell_buf, row); /* store raster map in segment file */
    }
	
}
Esempio n. 3
0
struct MPIR_Segment *MPIR_Segment_alloc(const void *buf, MPI_Aint count, MPI_Datatype handle)
{
    struct MPIR_Segment *segp;

    segp = (struct MPIR_Segment *) MPL_malloc(sizeof(struct MPIR_Segment), MPL_MEM_DATATYPE);
    if (segp)
        segment_init(buf, count, handle, segp);

    return segp;
}
Esempio n. 4
0
int Init_segment_file (char* seg_name, SEGMENT* seg)
{
    int seg_fd;

    seg_fd = open (seg_name, 2);
    if (seg_fd < 0)
	G_fatal_error ("Cannot open segment disk file");
    segment_init (seg, seg_fd, 4);
    return seg_fd;
}
Esempio n. 5
0
void accept_requests()
{
	int i, myThread;
	job j;
	segment* s;
	
	#ifdef DEBUG
		printf("[DEBUG] starting to select\n");
	#endif
	
	while (1)
	{
		pthread_mutex_lock(&pikachu);
		if (toShutdown)
		{
			pthread_mutex_unlock(&pikachu);
			#ifdef DEBUG
				printf("[DEBUG] main THREAD left select()\n");
			#endif
			break;
		}
		pthread_mutex_unlock(&pikachu);
		
		fd_set read_set;
		FD_ZERO(&read_set);
		
		for (i=0; i<N_PRINTERS; i++)
			FD_SET(fds[i], &read_set);
		
		if ( select(fds[N_PRINTERS-1]+1, &read_set, NULL, NULL, NULL) > 0 )
		{
			for (i=0 ; i<N_PRINTERS ; i++)
			{
				if (FD_ISSET(fds[i], &read_set))
				{
					if (read(fds[i], &j, sizeof(j)) > 0)
					{
						s = segment_init(j.id, j.page, j.txt, j.end);
						#ifdef DEBUG
							printf("[DEBUG] Received job%d page%d\n", j.id, j.page);
						#endif
						
						myThread = j.id % N_WORKERS;
						
						pthread_mutex_lock(&workers[myThread].mutex);
							enqueue(&workers[myThread].q, s);
							pthread_cond_signal(&workers[myThread].work);
						pthread_mutex_unlock(&workers[myThread].mutex);
					}
				}
			}
		}
	}
}
Esempio n. 6
0
int
seg_open(SSEG * sseg, int nrows, int ncols, int row_in_seg, int col_in_seg,
	 int nsegs_in_memory, int size_struct)
{
    char *filename;
    int errflag;
    int fd;

    sseg->filename = NULL;
    sseg->fd = -1;

    filename = G_tempfile();
    if (-1 == (fd = creat(filename, 0666))) {
	G_warning("seg_open(): unable to create segment file");
	return -2;
    }
    if (0 > (errflag = segment_format(fd, nrows, ncols,
				      row_in_seg, col_in_seg, size_struct))) {
	close(fd);
	unlink(filename);
	if (errflag == -1) {
	    G_warning("seg_open(): could not write segment file");
	    return -1;
	}
	else {
	    G_warning("seg_open(): illegal configuration parameter(s)");
	    return -3;
	}
    }
    close(fd);
    if (-1 == (fd = open(filename, 2))) {
	unlink(filename);
	G_warning("seg_open(): unable to re-open segment file");
	return -4;
    }
    if (0 > (errflag = segment_init(&(sseg->seg), fd, nsegs_in_memory))) {
	close(fd);
	unlink(filename);
	if (errflag == -1) {
	    G_warning("seg_open(): could not read segment file");
	    return -5;
	}
	else {
	    G_warning("seg_open(): out of memory");
	    return -6;
	}
    }
    sseg->filename = filename;
    sseg->fd = fd;
    return 0;
}
Esempio n. 7
0
void Segment_tmpfile (char* seg_name, int* seg_fd, SEGMENT* seg, int fract, 
                 RASTER_MAP_TYPE data_type)
{
    int nrows;

    nrows = G_window_rows ();

    strcpy (seg_name, Create_segment_file (nrows, fract, data_type));
    *seg_fd = open (seg_name, O_RDWR);
    if (*seg_fd < 0)
	G_fatal_error ("Can't open segment disk file ");
	
	/* get a handle to read segment file data values */
	/* dimensions of segment and size of stored items are read */
	/* automatically from the file header and stored in 'seg' */
    segment_init (seg, *seg_fd, 4);
}
Esempio n. 8
0
void Segment_named_outfile (char* map_name, char* mapset, int* map_fd,
			           char* seg_name, int* seg_fd, SEGMENT* seg, int overwrite, 
                       int terse, int fract, RASTER_MAP_TYPE data_type)
{
    int nrows;
    char* mapset_address;
    char message [64];

    mapset_address = G_find_cell (map_name, G_mapset ());
    strcpy (mapset, G_mapset ());
    if (mapset_address != NULL)
    {
      if (!overwrite)
	{
	  sprintf (message, "Raster map '%s' exists ", map_name);
	  G_fatal_error ("%s",message);
	}
      else
	{
	  if (!terse)
	    fprintf (stdout, "\nOverwriting raster map '%s' \n", map_name);
	}
    }
    else
      {
	if (!terse)
	  fprintf (stdout, "\nCreating raster map '%s' ", map_name);
      }

    if ((*map_fd = G_open_cell_new (map_name)) < 0)
	  G_fatal_error ("Can't create output layer ");

    nrows = G_window_rows ();
    
    strcpy (seg_name, Create_segment_file (nrows, fract, data_type));
    
    *seg_fd = open (seg_name, O_RDWR);
	
    if (*seg_fd < 0)
	  G_fatal_error ("Can't open segment disk file ");
	
    segment_init (seg, *seg_fd, 4);
}
Esempio n. 9
0
int main(void)
{
	physics_init(LCD_WIDTH, LCD_HEIGHT);
	input_init();
	display_init();
	segment_init();
	players_init();
	
	playerA = players_get(0);
	playerB = players_get(1);
	
	ball = physics_create(LCD_WIDTH / 2, LCD_HEIGHT / 2, BALL_RADIUS, BALL_MASS);
	
	for (;;)
	{
		input_update();
		players_update(0.01f);
		physics_update(0.01f);
		game_update();
		game_draw();
	}
	
	return 0;
}
Esempio n. 10
0
bool baekwha(const char *pcArgv0)
{
	time_t t_start, t_end;
	double t_elapsed;
	bool ok;


	if( cfg_showHelp )
	{
		showHelp(pcArgv0);
		return true;
	} else if( cfg_showVersion ) {
		showVersion();
		return true;
	}

	/*
	 * Open Debug log if '-d' was set
	 */
	if( cfg_debug==true )
	{
		/* catch special filename "-" for stdout */
		if( strcmp(pcDebugLogName, "-")==0 )
		{
			/* set output file to stdout */
			debugLog = stdout;
		}
		else
		{
			debugLog = fopen(pcDebugLogName, "w");
			if( debugLog==NULL )
			{
				fprintf(stderr, "error opening debug log '%s' : %s\n", pcDebugLogName, strerror(errno));
				return false;
			}
		}
		fprintf(debugLog, "<html><head><title>DreamAss debug log</title></head><body>\n");
	}

	/*
	 * No Sourecfiles means nothing to do
	 */
	if( srcFileNames_count==0 )
	{
		printf("No sourcefiles - nothing to do.\n");
		return true;
	}

	if(
		filelist_init(16) &&
		macro_init(16) &&
		srcstack_init(16) &&
		termlist_init(1024) &&
		new_variable(1024) &&
		segment_init(16)
	  )
	{
		/* Stop assembly time from now on */
		t_start = time(NULL);

		ok = assembleAllFiles();
		if( ok )
		{
			if( cfg_verbose )
			{
				t_end = time(NULL);
				t_elapsed = difftime(t_end, t_start);
				printf("%f seconds used\n", t_elapsed);
			}

			if( warningcnt>=cfg_maxwarnings )
			{
				printf("further warnings suppressed.\n");
			}

			if( cfg_verbose )
			{
				if( warningcnt )
					printf("%u",warningcnt);
				else
					printf("no");
				printf(" warning%s, ",(warningcnt==1)?"":"s");
				if( errorcnt )
					printf("%u",errorcnt);
				else
					printf("no");
				printf(" error%s.\n",(errorcnt==1)?"":"s");
			}

			if( errorcnt==0 )
			{
				if( pcLabelLogName!=NULL )
					dumpLabels( pcLabelLogName );

				if( !allBytesResolved )
				{
					fprintf(stderr, "Some vars still undefined. Dump not yet supported, sorry!\n");
					pass_showUndefs(topLevelSrc);
				}
				else
					dumpObject(topLevelSrc, pcOutFileName);
			}
		}
		if( topLevelSrc!=NULL )
			delSourcefile(topLevelSrc);

		return ok;
	}

	return false;
}
Esempio n. 11
0
int main(int argc, char **argv)
{
    int n, verbose = 1,
	backrow, backcol,
	col, row,
	len, flag,
	srows, scols,
	backrow_fd, backcol_fd, path_fd, in_row_fd, in_col_fd, out_fd;
    const char *current_mapset,
	*search_mapset,
	*path_mapset,
	*backrow_mapset,
	*backcol_mapset, *in_row_file, *in_col_file, *out_file;
    CELL *cell;
    POINT *PRES_PT, *PRESENT_PT, *OLD_PT;
    struct Cell_head window;
    double east, north;
    struct Option *opt1, *opt2, *opt3, *opt4;
    struct Flag *flag1;
    struct GModule *module;

    G_gisinit(argv[0]);

    /* Set description */
    module = G_define_module();
    G_add_keyword(_("raster"));
    G_add_keyword(_("fire"));
    G_add_keyword(_("cumulative costs"));
    module->description =
	_("Recursively traces the least cost path backwards to "
	  "cells from which the cumulative cost was determined.");

    opt1 = G_define_option();
    opt1->key = "x_input";
    opt1->type = TYPE_STRING;
    opt1->required = YES;
    opt1->gisprompt = "old,cell,raster";
    opt1->description =
	_("Name of raster map containing back-path easting information");

    opt2 = G_define_option();
    opt2->key = "y_input";
    opt2->type = TYPE_STRING;
    opt2->required = YES;
    opt2->gisprompt = "old,cell,raster";
    opt2->description =
	_("Name of raster map containing back-path northing information");

    opt3 = G_define_option();
    opt3->key = "coordinate";
    opt3->type = TYPE_STRING;
    opt3->multiple = YES;
    opt3->key_desc = "x,y";
    opt3->description =
	_("The map E and N grid coordinates of starting points");

    opt4 = G_define_option();
    opt4->key = "output";
    opt4->type = TYPE_STRING;
    opt4->required = YES;
    opt4->gisprompt = "new,cell,raster";
    opt4->description = _("Name of spread path raster map");

    flag1 = G_define_flag();
    flag1->key = 'v';
    flag1->description = _("Run verbosely");

    /*   Do command line parsing    */
    if (G_parser(argc, argv))
	exit(EXIT_FAILURE);

    current_mapset = G_mapset();
    in_row_file = G_tempfile();
    in_col_file = G_tempfile();
    out_file = G_tempfile();

    /*  Get database window parameters      */
    G_get_window(&window);

    verbose = flag1->answer;

    /*  Check if backrow layer exists in data base  */
    search_mapset = "";

    strcpy(backrow_layer, opt2->answer);
    strcpy(backcol_layer, opt1->answer);

    backrow_mapset = G_find_raster(backrow_layer, search_mapset);
    backcol_mapset = G_find_raster(backcol_layer, search_mapset);

    if (backrow_mapset == NULL)
	G_fatal_error("%s - not found", backrow_layer);

    if (backcol_mapset == NULL)
	G_fatal_error("%s - not found", backcol_layer);

    search_mapset = "";

    strcpy(path_layer, opt4->answer);

    path_mapset = G_find_raster(path_layer, search_mapset);

    /*  find number of rows and cols in window    */
    nrows = Rast_window_rows();
    ncols = Rast_window_cols();

    cell = Rast_allocate_c_buf();

    /*  Open back cell layers for reading  */
    backrow_fd = Rast_open_old(backrow_layer, backrow_mapset);
    backcol_fd = Rast_open_old(backcol_layer, backcol_mapset);

    /*   Parameters for map submatrices   */
    len = sizeof(CELL);

    srows = nrows / 4 + 1;
    scols = ncols / 4 + 1;

    if (verbose)
	G_message
	    ("\nReading the input map -%s- and -%s- and creating some temporary files...",
	     backrow_layer, backcol_layer);

    /* Create segmented files for back cell and output layers  */
    in_row_fd = creat(in_row_file, 0666);
    segment_format(in_row_fd, nrows, ncols, srows, scols, len);
    close(in_row_fd);
    in_col_fd = creat(in_col_file, 0666);
    segment_format(in_col_fd, nrows, ncols, srows, scols, len);
    close(in_col_fd);

    out_fd = creat(out_file, 0666);
    segment_format(out_fd, nrows, ncols, srows, scols, len);
    close(out_fd);

    /*   Open initialize and segment all files  */
    in_row_fd = open(in_row_file, 2);
    segment_init(&in_row_seg, in_row_fd, 4);
    in_col_fd = open(in_col_file, 2);
    segment_init(&in_col_seg, in_col_fd, 4);

    out_fd = open(out_file, 2);
    segment_init(&out_seg, out_fd, 4);

    /*   Write the back cell layers in the segmented files, and  
     *   Change UTM coordinates to ROWs and COLUMNs */
    for (row = 0; row < nrows; row++) {
	Rast_get_c_row(backrow_fd, cell, row);

	for (col = 0; col < ncols; col++)
	    if (cell[col] > 0)
		cell[col] =
		    (window.north - cell[col]) / window.ns_res /* - 0.5 */ ;
	    else
		cell[col] = -1;
	segment_put_row(&in_row_seg, cell, row);
	Rast_get_c_row(backcol_fd, cell, row);

	for (col = 0; col < ncols; col++)
	    if (cell[col] > 0)
		cell[col] =
		    (cell[col] - window.west) / window.ew_res /* - 0.5 */ ;
	segment_put_row(&in_col_seg, cell, row);
    }

    /* Convert easting and northing from the command line to row and col */
    if (opt3->answer) {
	for (n = 0; opt3->answers[n] != NULL; n += 2) {
	    G_scan_easting(opt3->answers[n], &east, G_projection());
	    G_scan_northing(opt3->answers[n + 1], &north, G_projection());
	    row = (window.north - north) / window.ns_res;
	    col = (east - window.west) / window.ew_res;
	    /* ignore pt outside window */
	    if (east < window.west || east > window.east ||
		north < window.south || north > window.north) {
		G_warning("Ignoring point outside window: ");
		G_warning("   %.4f,%.4f", east, north);
		continue;
	    }

	    value = (char *)&backrow;
	    segment_get(&in_row_seg, value, row, col);
	    /* ignore pt in no-data area */
	    if (backrow < 0) {
		G_warning("Ignoring point in NO-DATA area :");
		G_warning("   %.4f,%.4f", east, north);
		continue;
	    }
	    value = (char *)&backcol;
	    segment_get(&in_col_seg, value, row, col);

	    insert(&PRESENT_PT, row, col, backrow, backcol);
	}
    }

    /*  Set flag according to input */
    if (path_mapset != NULL) {
	if (head_start_pt == NULL)
	    /*output layer exists and start pts are not given on cmd line */
	    flag = 1;

	/* output layer exists and starting pts are given on cmd line */
	else
	    flag = 2;
    }
    else
	flag = 3;		/* output layer does not previously exist */

    /* If the output layer containing the starting positions */
    /* create a linked list of of them  */
    if (flag == 1) {
	path_fd = Rast_open_old(path_layer, path_mapset);

	/*  Search for the marked starting pts and make list    */
	for (row = 0; row < nrows; row++) {
	    Rast_get_c_row(path_fd, cell, row);

	    for (col = 0; col < ncols; col++) {
		if (cell[col] > 0) {
		    value = (char *)&backrow;
		    segment_get(&in_row_seg, value, row, col);
		    /* ignore pt in no-data area */
		    if (backrow < 0) {
			G_warning("Ignoring point in NO-DATA area:");
			G_warning("   %.4f,%.4f\n",
				  window.west + window.ew_res * (col + 0.5),
				  window.north - window.ns_res * (row + 0.5));
			continue;
		    }
		    value = (char *)&backcol;
		    segment_get(&in_col_seg, value, row, col);
		    insert(&PRESENT_PT, row, col, backrow, backcol);
		}
	    }			/* loop over cols */
	}			/* loop over rows */

	Rast_close(path_fd);
    }

    /* loop over the starting points to find the least cost paths */
    if (verbose)
	G_message("\nFinding the least cost paths ...");

    PRES_PT = head_start_pt;
    while (PRES_PT != NULL) {
	path_finder(PRES_PT->row, PRES_PT->col, PRES_PT->backrow,
		    PRES_PT->backcol);

	OLD_PT = PRES_PT;
	PRES_PT = NEXT_PT;
	G_free(OLD_PT);
    }

    /* Write pending updates by segment_put() to outputmap */
    segment_flush(&out_seg);

    if (verbose)
	G_message("\nWriting the output map  -%s-...", path_layer);

    path_fd = Rast_open_c_new(path_layer);
    for (row = 0; row < nrows; row++) {
	segment_get_row(&out_seg, cell, row);
	Rast_put_row(path_fd, cell, CELL_TYPE);
    }

    if (verbose)
	G_message("finished.");

    segment_release(&in_row_seg);	/* release memory  */
    segment_release(&in_col_seg);
    segment_release(&out_seg);

    close(in_row_fd);		/* close all files */
    close(in_col_fd);
    close(out_fd);

    Rast_close(path_fd);
    Rast_close(backrow_fd);
    Rast_close(backcol_fd);

    unlink(in_row_file);	/* remove submatrix files  */
    unlink(in_col_file);
    unlink(out_file);

    exit(EXIT_SUCCESS);
}