[GRASS-dev] Re: [bug #3112] r.watershed hangs

Hamish hamish_nospam at yahoo.com
Wed Oct 25 04:01:26 EDT 2006


Hardeep Singh Rai wrote:
> I was doing watershed analysis for spearfish dataset
> (spearfish_grass60data-0.1.tar.gz), and on giving command:
> 
> r.watershed elevation=elevation.dem threshhold=3 basin=raiBasin
> accumulation=raiAccu it starts processing of 5 steps. After about 20
> minutes, it completes all and throw message: "cloing maps", but do
> nothing; prompt never comes. I used it with option -m and without it.
> With both cases, "top" commands show ram or sed running with 99 usage
> of CPU. Even I waited for 12 hours, but it fail to finish. I am using
> GRASS 6.0.1 on Ubuntu.
> 
> Please suggest some solution.

Hamish wrote:
> It's a known bug (#3112). It gets stuck trying to make a huge set of
> colormap rules for the new map. If you cancel (^C) you might find the
> map was created anyway (???).

Chuck wrote:
> The solution is to include a more realistic threshold size.

Yes.

The attached patch shows the problem quite well. As the treshold gets
smaller, the number of areas (cats) grows exponentially. Not only that,
but as the loop within a loop runs, it takes longer and longer to do the
same thing.

The inner loop gets slower I think due to the increased number of color
rules set with G_set_color() that have to be read each time.


#spearfish
g.region rast=elevation.dem
r.watershed elev=elevation.dem basin=tmp_basin thresh=200
d.rast tmp_basin

thresh=200         # 1488 cats
real    0m38.558s
user    0m37.770s
sys     0m0.110s

thresh=100         # 2858 cats
real    0m41.296s
user    0m41.050s
sys     0m0.120s

thresh=50          # 5482 cats
real    0m57.495s
user    0m55.870s
sys     0m0.180s

thresh=37          # 7258 cats
real    1m20.985s
user    1m20.340s
sys     0m0.200s

thresh=25          # 10648 cats
real    3m5.185s
user    3m4.020s
sys     0m0.280s




there are a lot of loops here, but I think the main problem is with so
many calls to G_{get,set}_color():


raster/r.watershed/ram/close_maps2.c
..
        G_init_colors(&colors);
        G_make_random_colors(&colors, 1, max);
        G_set_color((CELL)0, 0, 0, 0, &colors);
        r = 1;
        incr = 0;
        while(incr >= 0) {
            for (gr=130+incr; gr<=255; gr += 20) {
                for (rd=90+incr; rd<=255; rd += 30) {
                    for (bl=90+incr; bl<=255; bl += 40) {
                        flag = 1;
                        while (flag) { 
                            G_get_color(r,&red,&green,&blue, &colors);
                            if((blue*.11 + red*.30 + green*.59) < 100) {

                                G_set_color(r, rd, gr, bl, &colors);
                                flag = 0;
                            }
                            if (++r > max) {
                                gr = rd = bl = 300;
                                flag = 0;
                                incr = -1;
                            }
                        }
                    }
                }
            }
            if (incr >= 0) {
                incr += 15;
                if (incr > 120)
                    incr = 7;
            }
        }



Hamish
-------------- next part --------------
Index: raster/r.watershed/ram/close_maps2.c
===================================================================
RCS file: /home/grass/grassrepository/grass6/raster/r.watershed/ram/close_maps2.c,v
retrieving revision 2.0
diff -u -r2.0 close_maps2.c
--- raster/r.watershed/ram/close_maps2.c	9 Nov 2004 13:32:28 -0000	2.0
+++ raster/r.watershed/ram/close_maps2.c	25 Oct 2006 07:44:47 -0000
@@ -46,6 +46,7 @@
 				G_set_color(r, rd, gr, bl, &colors);
 			        flag = 0;
 		            }
+if(r % 200 == 0) G_debug(0,"r=%d\tof %d", r, max);
 			    if (++r > max) {
 			        gr = rd = bl = 300;
 				flag = 0;


More information about the grass-dev mailing list