1 | /*************************************************************************** |
---|
2 | * blitz/traversal.cc Space-filling curve based traversal orders |
---|
3 | * |
---|
4 | * $Id$ |
---|
5 | * |
---|
6 | * Copyright (C) 1997-2011 Todd Veldhuizen <tveldhui@acm.org> |
---|
7 | * |
---|
8 | * This file is a part of Blitz. |
---|
9 | * |
---|
10 | * Blitz is free software: you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU Lesser General Public License |
---|
12 | * as published by the Free Software Foundation, either version 3 |
---|
13 | * of the License, or (at your option) any later version. |
---|
14 | * |
---|
15 | * Blitz is distributed in the hope that it will be useful, |
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
18 | * GNU Lesser General Public License for more details. |
---|
19 | * |
---|
20 | * You should have received a copy of the GNU Lesser General Public |
---|
21 | * License along with Blitz. If not, see <http://www.gnu.org/licenses/>. |
---|
22 | * |
---|
23 | * Suggestions: blitz-devel@lists.sourceforge.net |
---|
24 | * Bugs: blitz-support@lists.sourceforge.net |
---|
25 | * |
---|
26 | * For more information, please see the Blitz++ Home Page: |
---|
27 | * https://sourceforge.net/projects/blitz/ |
---|
28 | * |
---|
29 | ***************************************************************************/ |
---|
30 | |
---|
31 | #ifndef BZ_TRAVERSAL_CC |
---|
32 | #define BZ_TRAVERSAL_CC |
---|
33 | |
---|
34 | #ifndef BZ_TRAVERSAL_H |
---|
35 | #error <blitz/traversal.cc> must be included via <blitz/traversal.h> |
---|
36 | #endif |
---|
37 | |
---|
38 | BZ_NAMESPACE(blitz) |
---|
39 | |
---|
40 | // Next line is a workaround for Intel C++ V4.0 oddity, due |
---|
41 | // to Allan Stokes. |
---|
42 | static set<TraversalOrder<2> > *_bz_intel_kludge; |
---|
43 | |
---|
44 | //template<int N_dimensions> |
---|
45 | //_bz_typename TraversalOrderCollection<N_dimensions>::T_set |
---|
46 | // TraversalOrderCollection<N_dimensions>::traversals_; |
---|
47 | |
---|
48 | template<int N> |
---|
49 | void makeHilbert(Vector<TinyVector<int,N> >& coord, |
---|
50 | int x0, int y0, int xis, int xjs, |
---|
51 | int yis, int yjs, int n, int& i) |
---|
52 | { |
---|
53 | // N != 2 is not yet implemented. |
---|
54 | BZPRECONDITION(N == 2); |
---|
55 | |
---|
56 | if (!n) |
---|
57 | { |
---|
58 | if (i > coord.length(firstDim)) |
---|
59 | { |
---|
60 | cerr << "makeHilbert: vector not long enough" << endl; |
---|
61 | exit(1); |
---|
62 | } |
---|
63 | coord(i)[0] = x0 + (xis+yis)/2; |
---|
64 | coord(i)[1] = y0 + (xjs+yjs)/2; |
---|
65 | ++i; |
---|
66 | } |
---|
67 | else { |
---|
68 | makeHilbert(coord,x0,y0,yis/2, yjs/2, xis/2, xjs/2, n-1, i); |
---|
69 | makeHilbert(coord,x0+xis/2,y0+xjs/2,xis/2,xjs/2,yis/2,yjs/2,n-1,i); |
---|
70 | makeHilbert(coord,x0+xis/2+yis/2,y0+xjs/2+yjs/2,xis/2,xjs/2,yis/2, |
---|
71 | yjs/2,n-1,i); |
---|
72 | makeHilbert(coord,x0+xis/2+yis, y0+xjs/2+yjs, -yis/2,-yjs/2,-xis/2, |
---|
73 | -xjs/2,n-1,i); |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | template<int N_dimensions> |
---|
78 | void MakeHilbertTraversal(Vector<TinyVector<int,N_dimensions> >& coord, |
---|
79 | int length) |
---|
80 | { |
---|
81 | // N_dimensions != 2 not yet implemented |
---|
82 | BZPRECONDITION(N_dimensions == 2); |
---|
83 | |
---|
84 | // The constant on the next line is ln(2) |
---|
85 | int d = (int)(::ceil(::log((double)length) / 0.693147180559945309417)); |
---|
86 | |
---|
87 | int N = 1 << d; |
---|
88 | const int Npoints = N*N; |
---|
89 | Vector<TinyVector<int,2> > coord2(Npoints); |
---|
90 | |
---|
91 | int i=0; |
---|
92 | makeHilbert(coord2,0,0,32768,0,0,32768,d,i); |
---|
93 | |
---|
94 | int xp0 = coord2(0)[0]; |
---|
95 | int yp0 = coord2(0)[1]; |
---|
96 | |
---|
97 | int j=0; |
---|
98 | |
---|
99 | coord.resize(length * length); |
---|
100 | |
---|
101 | for (int i=0; i < Npoints; ++i) |
---|
102 | { |
---|
103 | coord2(i)[0] = (coord2(i)[0]-xp0)/(2*xp0); |
---|
104 | coord2(i)[1] = (coord2(i)[1]-yp0)/(2*yp0); |
---|
105 | |
---|
106 | if ((coord2(i)[0] < length) && (coord2(i)[1] < length) |
---|
107 | && (coord2(i)[0] >= 0) && (coord2(i)[1] >= 0)) |
---|
108 | { |
---|
109 | coord(j)[0] = coord2(i)[0]; |
---|
110 | coord(j)[1] = coord2(i)[1]; |
---|
111 | ++j; |
---|
112 | } |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | template<int N_dimensions> |
---|
117 | void generateFastTraversalOrder(const TinyVector<int,N_dimensions>& size) |
---|
118 | { |
---|
119 | BZPRECONDITION(N_dimensions == 2); |
---|
120 | BZPRECONDITION(size[0] == size[1]); |
---|
121 | |
---|
122 | TraversalOrderCollection<2> travCol; |
---|
123 | if (travCol.find(size)) |
---|
124 | return; |
---|
125 | |
---|
126 | Vector<TinyVector<int,2> > ordering(size[0]); |
---|
127 | |
---|
128 | MakeHilbertTraversal(ordering, size[0]); |
---|
129 | travCol.insert(TraversalOrder<2>(size, ordering)); |
---|
130 | } |
---|
131 | |
---|
132 | BZ_NAMESPACE_END |
---|
133 | |
---|
134 | #endif // BZ_TRAVERSAL_CC |
---|