Vlasiator ebf0dd394 on dev (v5.4.0 + 1054 commits)
Loading...
Searching...
No Matches
cpu_sort_ids.hpp
Go to the documentation of this file.
1#include <algorithm>
2#include <cmath>
3#include <utility>
4#include <vector>
5#include <iostream>
6#include <map>
7
8#ifndef CPU_SORT_IDS_HPP
9#define CPU_SORT_IDS_HPP
10
11
12// Comparator function for sorting vector of pairs
13template<typename ID> inline bool paircomparator( const std::pair<ID, uint> & l, const std::pair<ID, uint> & r ) {
14 return l.first < r.first;
15}
16
17
18
19template<typename ID, typename LENGTH> inline void sortIds(const uint dimension,
20 const LENGTH meshSize,
21 const std::vector<ID>& ids,
22 std::map<ID, ID>& mapping){
23
24 //sortedIds.resize(ids.size());
25 //TODO conditionally parallel version?
26 //#pragma omp parallel for
27 for (uint i = 0; i < ids.size() ; ++i ) {
28 const ID id = ids[i];
29
30 if (id > meshSize[0] * meshSize[1] * meshSize[2])
31 continue;
32
33 switch( dimension ) {
34 case 0: {
35 const ID idMapped = id; // Mapping the block id to different coordinate system if dimension is not zero:
36 mapping[idMapped] = id;
37 }
38 break;
39 case 1: {
40 // Do operation:
41 // id = x + y*x_max + z*y_max*x_max
42 //=> id' = id - (x + y*x_max) + y + x*y_max = x + y*x_max + z*y_max*x_max - (x + y*x_max) + y + x*y_max
43 // = y + x*y_max + z*y_max*x_max
44 const ID x_index = (id-1) % meshSize[0];
45 const ID y_index = ((id-1) / meshSize[0]) % meshSize[1];
46
47 // Mapping the block id to different coordinate system if dimension is not zero:
48 const ID idMapped = id - (x_index + y_index*meshSize[0]) + y_index + x_index * meshSize[1];
49
50 mapping[idMapped] = id;
51 }
52 break;
53 case 2: {
54 // Do operation:
55 // id = x + y*x_max + z*y_max*x_max
56 //=> id' = z + y*z_max + x*z_max*y_max
57 const ID x_index = (id-1) % meshSize[0];
58 const ID y_index = ((id-1) / meshSize[0]) % meshSize[1];
59 const ID z_index = ((id-1) / (meshSize[0] * meshSize[1]));
60
61 // Mapping the id id to different coordinate system if dimension is not zero:
62 //const uint idMapped
63 // = z_indice
64 // + y_indice * meshSize[2]
65 // + x_indice*meshSize[1]*meshSize[2];
66 const ID idMapped = 1 + z_index + y_index*meshSize[2] + x_index*meshSize[1]*meshSize[2];
67 mapping[idMapped] = id;
68 }
69 break;
70 }
71}
72// Finally, sort the list of pairs
73// std::sort( sortedIds.begin(), sortedIds.end(), paircomparator<ID> );
74
75//for (auto j = 1; j <= mapping.size(); j++)
76// std::cout << j << ", " << mapping[j] << "\n";
77
78}
79
80
81
82
83
84
85template<typename ID, typename LENGTH> void sortIdlistByDimension(const uint dimension, const LENGTH meshSize,
86 std::vector<ID> & ids,
87 std::vector<uint> & columnIdOffsets,
88 std::vector<uint> & columnNumIds,
89 std::vector<uint> & setColumnOffsets,
90 std::vector<uint> & setNumColumns) {
91
92 const uint nIds = ids.size();
93
94 //sort Ids
95 std::vector<std::pair<ID, ID> > sortedIdPairs;
96 sortIds<ID, LENGTH>(dimension, meshSize, ids, sortedIdPairs);
97
98
99 // Put in the sorted ids, and also compute column offsets and lengths:
100 columnIdOffsets.push_back(0); //first offset
101 setColumnOffsets.push_back(0); //first offset
102 uint prev_column_id, prev_dimension_id;
103
104 for (uint i=0; i<nIds; ++i) {
105 // identifies a particular column
106 uint column_id = sortedIdPairs[i].first / meshSize[dimension];
107
108 // identifies a particular id in a column (along the dimension)
109 uint dimension_id = sortedIdPairs[i].first % meshSize[dimension];
110
111 //sorted list
112 ids[i] = sortedIdPairs[i].second;
113
114 if ( i > 0 && ( column_id != prev_column_id || dimension_id != (prev_dimension_id + 1) )){
115 //encountered new column! For i=0, we already entered the correct offset (0).
116 //We also identify it as a new column if there is a break in the column (e.g., gap between two populations)
117 /*add offset where the next column will begin*/
118 columnIdOffsets.push_back(i);
119 /*add length of the current column that now ended*/
120 columnNumIds.push_back(columnIdOffsets[columnIdOffsets.size()-1] - columnIdOffsets[columnIdOffsets.size()-2]);
121
122 if (column_id != prev_column_id ){
123 //encountered new set of columns, add offset to new set starting at present column
124 setColumnOffsets.push_back(columnIdOffsets.size() - 1);
125 /*add length of the previous column set that ended*/
126 setNumColumns.push_back(setColumnOffsets[setColumnOffsets.size()-1] - setColumnOffsets[setColumnOffsets.size()-2]);
127 }
128 }
129 prev_column_id = column_id;
130 prev_dimension_id = dimension_id;
131 }
132
133 columnNumIds.push_back(nIds - columnIdOffsets[columnIdOffsets.size()-1]);
134 setNumColumns.push_back(columnNumIds.size() - setColumnOffsets[setColumnOffsets.size()-1]);
135}
136
137
138
139
140
141
142#endif
for i
Definition Dispersion.m:24
void sortIds(const uint dimension, const LENGTH meshSize, const std::vector< ID > &ids, std::map< ID, ID > &mapping)
void sortIdlistByDimension(const uint dimension, const LENGTH meshSize, std::vector< ID > &ids, std::vector< uint > &columnIdOffsets, std::vector< uint > &columnNumIds, std::vector< uint > &setColumnOffsets, std::vector< uint > &setNumColumns)
bool paircomparator(const std::pair< ID, uint > &l, const std::pair< ID, uint > &r)