浏览代码

r.cost, r.walk: change percent_memory to memory (in MB)

git-svn-id: https://svn.osgeo.org/grass/grass/branches/releasebranch_7_0@64288 15284696-431f-4ddb-bdfa-cd5b030d7da7
Markus Metz 10 年之前
父节点
当前提交
c53ce62673
共有 7 个文件被更改,包括 99 次插入106 次删除
  1. 0 22
      raster/r.cost/cost.h
  2. 33 26
      raster/r.cost/main.c
  3. 10 10
      raster/r.cost/r.cost.html
  4. 10 21
      raster/r.cost/stash.h
  5. 33 26
      raster/r.walk/main.c
  6. 11 0
      raster/r.walk/r.walk.html
  7. 2 1
      raster/r.walk/stash.h

+ 0 - 22
raster/r.cost/cost.h

@@ -1,26 +1,4 @@
 
 
-/****************************************************************************
- *
- * MODULE:       r.cost
- *
- * AUTHOR(S):    Antony Awaida - IESL - M.I.T.
- *               James Westervelt - CERL
- *               Pierre de Mouveaux <pmx audiovu com>
- *               Eric G. Miller <egm2 jps net>
- *
- * PURPOSE:      Outputs a raster map layer showing the cumulative cost
- *               of moving between different geographic locations on an
- *               input raster map layer whose cell category values
- *               represent cost.
- *
- * COPYRIGHT:    (C) 2006 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *               License (>=v2). Read the file COPYING that comes with GRASS
- *               for details.
- *
- ***************************************************************************/
-
 /***************************************************************/
 /***************************************************************/
 /*                                                             */
 /*                                                             */
 /*        cost.h    in   ~/src/Gcost                           */
 /*        cost.h    in   ~/src/Gcost                           */

+ 33 - 26
raster/r.cost/main.c

@@ -18,7 +18,7 @@
  *               input raster map layer whose cell category values 
  *               input raster map layer whose cell category values 
  *               represent cost.
  *               represent cost.
  *
  *
- * COPYRIGHT:    (C) 2006-2009 by the GRASS Development Team
+ * COPYRIGHT:    (C) 2006-2015 by the GRASS Development Team
  *
  *
  *               This program is free software under the GNU General Public
  *               This program is free software under the GNU General Public
  *               License (>=v2). Read the file COPYING that comes with GRASS
  *               License (>=v2). Read the file COPYING that comes with GRASS
@@ -132,7 +132,7 @@ int main(int argc, char *argv[])
     struct History history;
     struct History history;
     double peak = 0.0;
     double peak = 0.0;
     int dsize, nearest_size;
     int dsize, nearest_size;
-    double disk_mb, mem_mb;
+    double disk_mb, mem_mb, pq_mb;
 
 
     G_gisinit(argv[0]);
     G_gisinit(argv[0]);
 
 
@@ -219,14 +219,13 @@ int main(int argc, char *argv[])
     opt6->guisection = _("NULL cells");
     opt6->guisection = _("NULL cells");
 
 
     opt10 = G_define_option();
     opt10 = G_define_option();
-    opt10->key = "percent_memory";
+    opt10->key = "memory";
     opt10->type = TYPE_INTEGER;
     opt10->type = TYPE_INTEGER;
     opt10->key_desc = "value";
     opt10->key_desc = "value";
     opt10->required = NO;
     opt10->required = NO;
     opt10->multiple = NO;
     opt10->multiple = NO;
-    opt10->answer = "40";
-    opt10->options = "0-100";
-    opt10->description = _("Percent of map to keep in memory");
+    opt10->answer = "300";
+    opt10->description = _("Maximum memory to be used in MB");
 
 
     flag2 = G_define_flag();
     flag2 = G_define_flag();
     flag2->key = 'k';
     flag2->key = 'k';
@@ -309,9 +308,8 @@ int main(int argc, char *argv[])
     if (sscanf(opt5->answer, "%d", &maxcost) != 1 || maxcost < 0)
     if (sscanf(opt5->answer, "%d", &maxcost) != 1 || maxcost < 0)
 	G_fatal_error(_("Inappropriate maximum cost: %d"), maxcost);
 	G_fatal_error(_("Inappropriate maximum cost: %d"), maxcost);
 
 
-    if (sscanf(opt10->answer, "%d", &maxmem) != 1 || maxmem < 0 ||
-	maxmem > 100)
-	G_fatal_error(_("Inappropriate percent memory: %d"), maxmem);
+    if (sscanf(opt10->answer, "%d", &maxmem) != 1 || maxmem <= 0)
+	G_fatal_error(_("Inappropriate amount of memory: %d"), maxmem);
 
 
     if ((opt6->answer == NULL) ||
     if ((opt6->answer == NULL) ||
 	(sscanf(opt6->answer, "%lf", &null_cost) != 1)) {
 	(sscanf(opt6->answer, "%lf", &null_cost) != 1)) {
@@ -379,31 +377,35 @@ int main(int argc, char *argv[])
     else
     else
 	srows = scols = SEGCOLSIZE;
 	srows = scols = SEGCOLSIZE;
 
 
-    if (maxmem == 100) {
-	srows = scols = 256;
-    }
-
     /* calculate total number of segments */
     /* calculate total number of segments */
     nseg = ((nrows + srows - 1) / srows) * ((ncols + scols - 1) / scols);
     nseg = ((nrows + srows - 1) / srows) * ((ncols + scols - 1) / scols);
-    if (maxmem > 0)
-	segments_in_memory = (maxmem * nseg) / 100;
-    /* maxmem = 0 */
-    else
-	segments_in_memory = 4 * (nrows / srows + ncols / scols + 2);
 
 
-    if (segments_in_memory == 0)
-	segments_in_memory = 1;
-
-    /* report disk space and memory requirements */
+    /* calculate disk space and memory requirements */
+    /* (nrows + ncols) * 8. * 20.0 / 1048576. for Dijkstra search */
+    pq_mb = ((double)nrows + ncols) * 8. * 20.0 / 1048576.;
+    G_debug(0, "pq MB: %g", pq_mb);
+    maxmem -= pq_mb;
+    if (maxmem < 10)
+	maxmem = 10;
     if (dir == TRUE) {
     if (dir == TRUE) {
 	disk_mb = (double) nrows * ncols * 28. / 1048576.;
 	disk_mb = (double) nrows * ncols * 28. / 1048576.;
-	mem_mb  = (double) srows * scols * 28. / 1048576. * segments_in_memory;
-	mem_mb += nrows * ncols * 0.05 * 20. / 1048576.;    /* for Dijkstra search */
+	segments_in_memory = maxmem / 
+	                     ((double) srows * scols * (28. / 1048576.));
+	if (segments_in_memory < 4)
+	    segments_in_memory = 4;
+	if (segments_in_memory > nseg)
+	    segments_in_memory = nseg;
+	mem_mb = (double) srows * scols * (28. / 1048576.) * segments_in_memory;
     }
     }
     else {
     else {
 	disk_mb = (double) nrows * ncols * 24. / 1048576.;
 	disk_mb = (double) nrows * ncols * 24. / 1048576.;
-	mem_mb  = (double) srows * scols * 24. / 1048576. * segments_in_memory;
-	mem_mb += nrows * ncols * 0.05 * 20. / 1048576.;    /* for Dijkstra search */
+	segments_in_memory = maxmem / 
+	                     ((double) srows * scols * (24. / 1048576.));
+	if (segments_in_memory < 4)
+	    segments_in_memory = 4;
+	if (segments_in_memory > nseg)
+	    segments_in_memory = nseg;
+	mem_mb = (double) srows * scols * (24. / 1048576.) * segments_in_memory;
     }
     }
 
 
     if (flag5->answer) {
     if (flag5->answer) {
@@ -411,6 +413,9 @@ int main(int argc, char *argv[])
         fprintf(stdout, "\n");
         fprintf(stdout, "\n");
 	fprintf(stdout, _("Will need at least %.2f MB of memory"), mem_mb);
 	fprintf(stdout, _("Will need at least %.2f MB of memory"), mem_mb);
         fprintf(stdout, "\n");
         fprintf(stdout, "\n");
+	fprintf(stdout, _("%d of %d segments are kept in memory"),
+	                segments_in_memory, nseg);
+        fprintf(stdout, "\n");
 	Rast_close(cost_fd);
 	Rast_close(cost_fd);
 	exit(EXIT_SUCCESS);
 	exit(EXIT_SUCCESS);
     }
     }
@@ -418,6 +423,8 @@ int main(int argc, char *argv[])
     G_verbose_message("--------------------------------------------");
     G_verbose_message("--------------------------------------------");
     G_verbose_message(_("Will need at least %.2f MB of disk space"), disk_mb);
     G_verbose_message(_("Will need at least %.2f MB of disk space"), disk_mb);
     G_verbose_message(_("Will need at least %.2f MB of memory"), mem_mb);
     G_verbose_message(_("Will need at least %.2f MB of memory"), mem_mb);
+    G_verbose_message(_("%d of %d segments are kept in memory"),
+	              segments_in_memory, nseg);
     G_verbose_message("--------------------------------------------");
     G_verbose_message("--------------------------------------------");
 
 
     /* Create segmented format files for cost layer and output layer */
     /* Create segmented format files for cost layer and output layer */

+ 10 - 10
raster/r.cost/r.cost.html

@@ -140,16 +140,16 @@ initially computed. <em>r.cost</em> uses a binary tree with an linked list
 at each node in the tree for efficiently holding cells with identical
 at each node in the tree for efficiently holding cells with identical
 cumulative costs.
 cumulative costs.
 <p>
 <p>
-<em>r.cost</em>, like most all GRASS raster programs, is also made to be run on
-maps larger that can fit in available computer memory. As the
-algorithm works through the dynamic list of cells it can move almost
-randomly around the entire area. <em>r.cost</em> divides the entire area
-into a number of pieces and swaps these pieces in and out of memory (to
-and from disk) as needed. This provides a virtual memory approach
-optimally designed for 2-D raster maps.
-The amount of map to hold in memory at one time can be controlled with the
-<b>percent_memory</b> option. For large maps this value will have to be set
-to a lower value.
+<em>r.cost</em>, like most all GRASS raster programs, is also made to 
+be run on maps larger that can fit in available computer memory. As the 
+algorithm works through the dynamic list of cells it can move almost 
+randomly around the entire area. <em>r.cost</em> divides the entire 
+area into a number of pieces and swaps these pieces in and out of 
+memory (to and from disk) as needed. This provides a virtual memory 
+approach optimally designed for 2-D raster maps. The amount of memory 
+to be used by <em>r.cost</em> can be controlled with the <b>memory</b> 
+option, default is 300 MB. For systems with less memory this value will 
+have to be set to a lower value.
 
 
 
 
 <h2>EXAMPLES</h2>
 <h2>EXAMPLES</h2>

+ 10 - 21
raster/r.cost/stash.h

@@ -1,25 +1,14 @@
 
 
-/****************************************************************************
- *
- * MODULE:       r.cost
- *
- * AUTHOR(S):    Antony Awaida - IESL - M.I.T.
- *               James Westervelt - CERL
- *               Pierre de Mouveaux <pmx audiovu com>
- *               Eric G. Miller <egm2 jps net>
- *
- * PURPOSE:      Outputs a raster map layer showing the cumulative cost
- *               of moving between different geographic locations on an
- *               input raster map layer whose cell category values
- *               represent cost.
- *
- * COPYRIGHT:    (C) 2006-2009 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *               License (>=v2). Read the file COPYING that comes with GRASS
- *               for details.
- *
- ***************************************************************************/
+/***************************************************************/
+/*                                                             */
+/*       stash.h     in    ~/src/Gcost                         */
+/*                                                             */
+/*       This header file declares the global variables and    */
+/*       the structures that are to be used for command        */
+/*       line processing.                                      */
+/*                                                             */
+
+/***************************************************************/
 
 
 #ifndef __STASH_H__
 #ifndef __STASH_H__
 #define __STASH_H__
 #define __STASH_H__

+ 33 - 26
raster/r.walk/main.c

@@ -30,7 +30,7 @@
  *               Updated for GRASS 7
  *               Updated for GRASS 7
  *                 Markus Metz
  *                 Markus Metz
  * PURPOSE:      anisotropic movements on cost surfaces
  * PURPOSE:      anisotropic movements on cost surfaces
- * COPYRIGHT:    (C) 1999-2014 by the GRASS Development Team
+ * COPYRIGHT:    (C) 1999-2015 by the GRASS Development Team
  *
  *
  *               This program is free software under the GNU General Public
  *               This program is free software under the GNU General Public
  *               License (>=v2). Read the file COPYING that comes with GRASS
  *               License (>=v2). Read the file COPYING that comes with GRASS
@@ -167,7 +167,7 @@ int main(int argc, char *argv[])
     struct History history;
     struct History history;
     double peak = 0.0;
     double peak = 0.0;
     int dtm_dsize, cost_dsize;
     int dtm_dsize, cost_dsize;
-    double disk_mb, mem_mb;
+    double disk_mb, mem_mb, pq_mb;
 
 
     /* Definition for dimension and region check */
     /* Definition for dimension and region check */
     struct Cell_head dtm_cellhd, cost_cellhd;
     struct Cell_head dtm_cellhd, cost_cellhd;
@@ -259,9 +259,8 @@ int main(int argc, char *argv[])
     opt10->key_desc = "value";
     opt10->key_desc = "value";
     opt10->required = NO;
     opt10->required = NO;
     opt10->multiple = NO;
     opt10->multiple = NO;
-    opt10->answer = "40";
-    opt10->options = "0-100";
-    opt10->description = _("Percent of map to keep in memory");
+    opt10->answer = "300";
+    opt10->description = _("Maximum memory to be used in MB");
 
 
     opt15 = G_define_option();
     opt15 = G_define_option();
     opt15->key = "walk_coeff";
     opt15->key = "walk_coeff";
@@ -369,9 +368,8 @@ int main(int argc, char *argv[])
     if (sscanf(opt5->answer, "%d", &maxcost) != 1 || maxcost < 0)
     if (sscanf(opt5->answer, "%d", &maxcost) != 1 || maxcost < 0)
 	G_fatal_error(_("Inappropriate maximum cost: %d"), maxcost);
 	G_fatal_error(_("Inappropriate maximum cost: %d"), maxcost);
 
 
-    if (sscanf(opt10->answer, "%d", &maxmem) != 1 || maxmem < 0 ||
-	maxmem > 100)
-	G_fatal_error(_("Inappropriate percent memory: %d"), maxmem);
+    if (sscanf(opt10->answer, "%d", &maxmem) != 1 || maxmem <= 0)
+	G_fatal_error(_("Inappropriate amount of memory: %d"), maxmem);
 
 
     /* Getting walking energy formula parameters */
     /* Getting walking energy formula parameters */
     if ((par_number =
     if ((par_number =
@@ -492,31 +490,35 @@ int main(int argc, char *argv[])
     else
     else
 	srows = scols = SEGCOLSIZE;
 	srows = scols = SEGCOLSIZE;
 
 
-    if (maxmem == 100) {
-	srows = scols = 256;
-    }
-
     /* calculate total number of segments */
     /* calculate total number of segments */
     nseg = ((nrows + srows - 1) / srows) * ((ncols + scols - 1) / scols);
     nseg = ((nrows + srows - 1) / srows) * ((ncols + scols - 1) / scols);
-    if (maxmem > 0)
-	segments_in_memory = (maxmem * nseg) / 100;
-    /* maxmem = 0 */
-    else
-	segments_in_memory = 4 * (nrows / srows + ncols / scols + 2);
 
 
-    if (segments_in_memory == 0)
-	segments_in_memory = 1;
-
-    /* report disk space and memory requirements */
-    if (dir == 1) {
+    /* calculate disk space and memory requirements */
+    /* (nrows + ncols) * 8. * 20.0 / 1048576. for Dijkstra search */
+    pq_mb = ((double)nrows + ncols) * 8. * 20.0 / 1048576.;
+    G_debug(0, "pq MB: %g", pq_mb);
+    maxmem -= pq_mb;
+    if (maxmem < 10)
+	maxmem = 10;
+    if (dir == TRUE) {
 	disk_mb = (double) nrows * ncols * 28. / 1048576.;
 	disk_mb = (double) nrows * ncols * 28. / 1048576.;
-	mem_mb  = (double) srows * scols * 28. / 1048576. * segments_in_memory;
-	mem_mb += nrows * ncols * 0.05 * 20. / 1048576.;    /* for Dijkstra search */
+	segments_in_memory = maxmem / 
+	                     ((double) srows * scols * (28. / 1048576.));
+	if (segments_in_memory < 4)
+	    segments_in_memory = 4;
+	if (segments_in_memory > nseg)
+	    segments_in_memory = nseg;
+	mem_mb = (double) srows * scols * (28. / 1048576.) * segments_in_memory;
     }
     }
     else {
     else {
 	disk_mb = (double) nrows * ncols * 24. / 1048576.;
 	disk_mb = (double) nrows * ncols * 24. / 1048576.;
-	mem_mb  = (double) srows * scols * 24. / 1048576. * segments_in_memory;
-	mem_mb += nrows * ncols * 0.05 * 20. / 1048576.;    /* for Dijkstra search */
+	segments_in_memory = maxmem / 
+	                     ((double) srows * scols * (24. / 1048576.));
+	if (segments_in_memory < 4)
+	    segments_in_memory = 4;
+	if (segments_in_memory > nseg)
+	    segments_in_memory = nseg;
+	mem_mb = (double) srows * scols * (24. / 1048576.) * segments_in_memory;
     }
     }
 
 
     if (flag5->answer) {
     if (flag5->answer) {
@@ -524,6 +526,9 @@ int main(int argc, char *argv[])
         fprintf(stdout, "\n");
         fprintf(stdout, "\n");
 	fprintf(stdout, _("Will need at least %.2f MB of memory"), mem_mb);
 	fprintf(stdout, _("Will need at least %.2f MB of memory"), mem_mb);
         fprintf(stdout, "\n");
         fprintf(stdout, "\n");
+	fprintf(stdout, _("%d of %d segments are kept in memory"),
+	                segments_in_memory, nseg);
+        fprintf(stdout, "\n");
 	Rast_close(cost_fd);
 	Rast_close(cost_fd);
 	Rast_close(dtm_fd);
 	Rast_close(dtm_fd);
 	exit(EXIT_SUCCESS);
 	exit(EXIT_SUCCESS);
@@ -532,6 +537,8 @@ int main(int argc, char *argv[])
     G_verbose_message("--------------------------------------------");
     G_verbose_message("--------------------------------------------");
     G_verbose_message(_("Will need at least %.2f MB of disk space"), disk_mb);
     G_verbose_message(_("Will need at least %.2f MB of disk space"), disk_mb);
     G_verbose_message(_("Will need at least %.2f MB of memory"), mem_mb);
     G_verbose_message(_("Will need at least %.2f MB of memory"), mem_mb);
+    G_verbose_message(_("%d of %d segments are kept in memory"),
+	              segments_in_memory, nseg);
     G_verbose_message("--------------------------------------------");
     G_verbose_message("--------------------------------------------");
 
 
     /* Create segmented format files for cost layer and output layer */
     /* Create segmented format files for cost layer and output layer */

+ 11 - 0
raster/r.walk/r.walk.html

@@ -110,6 +110,17 @@ the movement direction raster map when
 running <em><a href="r.drain.html">r.drain</a></em> to ensure the path
 running <em><a href="r.drain.html">r.drain</a></em> to ensure the path
 is computed according to the proper movement directions.
 is computed according to the proper movement directions.
 
 
+<p>
+<em>r.walk</em>, like most all GRASS raster programs, is also made to 
+be run on maps larger that can fit in available computer memory. As the 
+algorithm works through the dynamic list of cells it can move almost 
+randomly around the entire area. <em>r.walk</em> divides the entire 
+area into a number of pieces and swaps these pieces in and out of 
+memory (to and from disk) as needed. This provides a virtual memory 
+approach optimally designed for 2-D raster maps. The amount of memory 
+to be used by <em>r.walk</em> can be controlled with the <b>memory</b> 
+option, default is 300 MB. For systems with less memory this value will 
+have to be set to a lower value.
 
 
 <h2>REFERENCES</h2>
 <h2>REFERENCES</h2>
 
 

+ 2 - 1
raster/r.walk/stash.h

@@ -14,6 +14,7 @@
 #define __STASH_H__
 #define __STASH_H__
 
 
 #include <stdio.h>
 #include <stdio.h>
+
 #define      CUM_COST_LAYER        1
 #define      CUM_COST_LAYER        1
 #define      COST_LAYER            2
 #define      COST_LAYER            2
 #define      START_PT              3
 #define      START_PT              3
@@ -32,4 +33,4 @@ extern struct start_pt *head_end_pt;
 int process_answers(char **, struct start_pt **, struct start_pt **);
 int process_answers(char **, struct start_pt **, struct start_pt **);
 int time_to_stop(int, int);
 int time_to_stop(int, int);
 
 
-#endif
+#endif /* __STASH_H__ */