File: psort_merge.h

package info (click to toggle)
combblas 2.0.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 190,476 kB
  • sloc: cpp: 55,912; ansic: 25,134; sh: 3,691; makefile: 548; csh: 66; python: 49; perl: 21
file content (238 lines) | stat: -rw-r--r-- 6,789 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
Copyright (c) 2009, David Cheng, Viral B. Shah.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#ifndef PSORT_MERGE_H
#define PSORT_MERGE_H

#ifdef USE_FUNNEL
#include "funnel.h"
#endif

namespace vpsort {
  template<typename MergeType>
    class Merge {
    public:
    template<typename _ValueType, typename _Compare, typename _Distance>
      void merge (_ValueType *in, _ValueType *out,
		  _Distance *disps, int nproc, _Compare comp) {
      
      MergeType *m = static_cast<MergeType *>(this);
      m->real_merge (in, out, disps, nproc, comp);
    }
    
    char *description () {
      MergeType *m = static_cast<MergeType *>(this);
      return m->real_description ();
    }
  };
  
  // p-way flat merge
  class FlatMerge : public Merge<FlatMerge> {
  public:
    char *real_description () {
      string s("Flat merge");
      return const_cast<char*>(s.c_str());
    };

    template<typename _ValueType, typename _Compare, typename _Distance>
      void real_merge (_ValueType *in, _ValueType *out,
		       _Distance *disps, int nproc, _Compare comp) {
      
      _Distance heads[nproc];
      copy (disps, disps + nproc, heads);
      for (int i = 0; i < disps[nproc]; ++i) {
	int min_head = -1;
	for (int j = 0; j < nproc; ++j) {
	  if (heads[j] < disps[j+1]
	      && (min_head < 0
		  || comp (in[heads[j]], in[heads[min_head]]))) {
	    min_head = j;
	  }
	}
	out[i] = in[heads[min_head]++];
      }
    }    
  };


  // A tree merge
  class TreeMerge : public Merge<TreeMerge> {
  public:
    char *real_description () {
	string s("Tree merge");
      return const_cast<char*>(s.c_str());
    };

    template<typename _ValueType, typename _Compare, typename _Distance>
      void real_merge (_ValueType *in, _ValueType *out,
		       _Distance *disps, int nproc, _Compare comp) {
      
      // round nproc up to next power of two, pad disps
      int nproc_p;
      for (nproc_p = 1; nproc_p < nproc; nproc_p *= 2);
      _Distance disps_p[nproc_p + 1];
      copy (disps, disps + nproc + 1, disps_p);
      fill (disps_p + nproc + 1, disps_p + nproc_p + 1, disps[nproc]);
      
      int merged = 0;
      for (int i = 1; i * 2 < nproc_p + 1; i = i * 2) {
	for (int j = 0; j + 2*i < nproc_p + 1; j += 2*i) {
	  inplace_merge (in + disps_p[j], 
			 in + disps_p[j + i],
			 in + disps_p[j + 2*i], 
			 comp);
	}
	merged = 2*i;
      }
      std::merge (in, in + disps_p[merged],
		  in + disps_p[merged], in + disps_p[nproc_p],
		  out, comp);
    }
  };

  // An out of place tree merge
  class OOPTreeMerge : public Merge<OOPTreeMerge> {
  public:
    char *real_description () {
      string s ("Out-of-place tree merge");
	return const_cast<char*>(s.c_str());
    };


    template<typename _RandomAccessIter, typename _Compare, typename _Distance>
      void real_merge (_RandomAccessIter in, _RandomAccessIter out,
			      _Distance *disps, int nproc, _Compare comp) {

      if (nproc == 1) {
	copy (in, in + disps[nproc], out);
	return;
      }
    
      _RandomAccessIter bufs[2] = { in, out };
      _Distance *locs = new _Distance[nproc];
      for (int i = 0; i < nproc; ++i) {
	locs[i] = 0;
      }
    
      _Distance next = 1;
      while (true) {
	_Distance stride = next * 2;
	if (stride >= nproc)
	  break;
      
	for (_Distance i = 0; i + next < nproc; i += stride) {
	  _Distance end_ind = min (i + stride, (_Distance) nproc);
        
	  std::merge (bufs[locs[i]] + disps[i], 
		      bufs[locs[i]] + disps[i + next],
		      bufs[locs[i + next]] + disps[i + next], 
		      bufs[locs[i + next]] + disps[end_ind],
		      bufs[1 - locs[i]] + disps[i],
		      comp);
	  locs[i] = 1 - locs[i];
	}
      
	next = stride;
      }
    
      // now have 4 cases for final merge
      if (locs[0] == 0) {
	// 00, 01 => out of place
	std::merge (in, in + disps[next],
		    bufs[locs[next]] + disps[next],
		    bufs[locs[next]] + disps[nproc],
		    out, comp);
      } else if (locs[next] == 0) {
	// 10 => backwards out of place
	std::merge (std::reverse_iterator<_RandomAccessIter> (in + disps[nproc]),
		    std::reverse_iterator<_RandomAccessIter> (in + disps[next]),
		    std::reverse_iterator<_RandomAccessIter> (out + disps[next]),
		    std::reverse_iterator<_RandomAccessIter> (out),
		    std::reverse_iterator<_RandomAccessIter> (out + disps[nproc]),
		    not2 (comp));
      } else {
	// 11 => in-place
	std::inplace_merge (out, out + disps[next], out + disps[nproc], comp);
      }
	  delete [] locs;
    }
  };  


#ifdef USE_FUNNEL

  class FunnelMerge2 : public Merge<FunnelMerge2> {
  public:
    char *real_description () {
      return ("Funnel(2) merge");
    };

    template<typename _RandomAccessIter, typename _Compare, typename _Distance>
      void real_merge (_RandomAccessIter in, _RandomAccessIter out,
			      _Distance *disps, int nproc, _Compare comp) {

      if (nproc == 1) {
	copy (in, in + disps[nproc], out);
	return;
      }

      iosort::merge_tree<_RandomAccessIter, 2> merger (nproc);

      for (int i=0; i<nproc; ++i) {
	merger.add_stream (in + disps[i], in + disps[i+1]);
      }

      merger (out);
    }
  };

  class FunnelMerge4 : public Merge<FunnelMerge4> {
  public:
    char *real_description () {
      std::string s ("Funnel(4) merge");
	return const_cast<char*>(s.c_str());
    };

    template<typename _RandomAccessIter, typename _Compare, typename _Distance>
      void real_merge (_RandomAccessIter in, _RandomAccessIter out,
			      _Distance *disps, int nproc, _Compare comp) {

      if (nproc == 1) {
	copy (in, in + disps[nproc], out);
	return;
      }

      iosort::merge_tree<_RandomAccessIter, 4> merger (nproc);

      for (int i=0; i<nproc; ++i) {
	merger.add_stream (in + disps[i], in + disps[i+1]);
      }

      merger (out);
    }
  };
#endif

}


#endif /*PSORT_MERGE_H */