-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcpuPermutationKernels.cpp
352 lines (337 loc) · 11.4 KB
/
cpuPermutationKernels.cpp
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/**
* @file cpuPermutationKernels.cpp
* @author Shaked Regev (regevs@ornl.gov)
* @brief Kernels for matrix and vector permutations
*
*/
#include "cpuPermutationKernels.hpp"
#include <resolve/resolve_defs.hpp>
#ifdef RESOLVE_USE_CUDA
#include <resolve/hykkt/CUDAPermutationKernels.hpp>
#endif
namespace ReSolve
{
namespace hykkt
{
/**
* @brief wrapper for the *MapIdx functions which map values based on perm
*
* calls cpuMapIdx, cudaMapIdx, or hipMapIdx based on the
* whether the code is compiled with cpu only, CUDA, or HIP
*
* @param[in] n - size of the matrix
* @param[in] perm - desired permutation
* @param[in] old_val - the array to be permuted
* @param[out] new_val - the permuted array
*
* @pre n is a positive integer, perm is an array of 0 to n-1
* (in some order), old_val is initialized
*
* @post new_val contains the permuted old_val
*/
void mapIdxWrapper(int n, const int* perm, const double* old_val, double* new_val)
{
#if !defined(RESOLVE_USE_CUDA) && !defined(RESOLVE_USE_HIP)
// CPU implementation
cpuMapIdx(n, perm, old_val, new_val);
#elif RESOLVE_WITH_CUDA
const int blockSize = 512;
const int numBlocks = (n + blockSize - 1) / blockSize;
cudaMapIdx<<<numBlocks, blockSize>>>(n, perm, old_val, new_val);
#endif
}
/**
* @brief maps the values in old_val to new_val based on perm
*
* @param[in] n - matrix size
* @param[in] perm - desired permutation
* @param[in] old_val - the array to be permuted
* @param[out] new_val - the permuted array
*
* @pre n is a positive integer, perm is an array of 0 to n-1
* (in some order), old_val is initialized
*
* @post new_val contains the permuted old_val
*/
void cpuMapIdx(int n, const int* perm, const double* old_val, double* new_val)
{
for (int i = 0; i < n; i++) {
new_val[i] = old_val[perm[i]];
}
}
/**
* @brief Selection sorts arr1 and arr2 w/indices
*
* The complexity of selection sort is O(n^2) in all cases.
* In the future the user will be given the option to choose between
* selection sort, insertion sort, and quicksort.
*
* @param[in] len - Size n of the matrix,
* @param[in,out] arr1 - the array that determines the sorting order,
* @param[in,out] arr2 - sorted based on arr1
*
* @pre arr1 and arr2 are arrays of length n
*
* @post arr1 and arr2 are sorted based on increasing values in arr1
*/
void selectionSort(int len, int* arr1, int* arr2)
{
int min_ind;
int temp;
for(int i = 0; i < len - 1; i++) {
min_ind = i;
for(int j = i + 1; j < len; j++) {
if(arr1[j] < arr1[min_ind]) {
min_ind = j;
}
}
if(i != min_ind) {
temp = arr1[i];
arr1[i] = arr1[min_ind];
arr1[min_ind] = temp;
temp = arr2[i];
arr2[i] = arr2[min_ind];
arr2[min_ind] = temp;
}
}
}
/**
* @brief swaps arr1[i] with arr1[j] and arr2[i] with arr2[j]
*
* @param[in,out] arr1 - first array to have values swapped
* @param[in,out] arr2 - second array to have values swapped
* @param[in] i - index of first value to be swapped
* @param[in] j - index of second value to be swapped
*/
inline void swap(int* arr1, int* arr2, int i, int j)
{
int temp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = temp;
temp = arr2[i];
arr2[i] = arr2[j];
arr2[j] = temp;
}
/**
* @brief helper function for quicksort
*
* @param[in,out] arr1 - array to be sorted based on itself
* @param[in,out] arr2 - array to be sorted based on other array
* @param[in] low - lower index bound of array slice
* @param[in] high - higher index bound of array slice
* @return int - index of the pivot
*/
inline int partition(int* arr1, int* arr2, int low, int high)
{
int pivot = arr1[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {
if (arr1[j] < pivot) {
i++;
swap(arr1, arr2, i, j);
}
}
swap(arr1, arr2, i + 1, high);
return (i + 1);
}
/**
* @brief quicksorts arr1 and arr2 between indices low and high
*
* The complexity of quicksort is O(n log n) in the average case,
* but O(n^2) in the worst case. For our test cases, n is small,
* so quicksort is not a good choice, therefore we use insertion sort.
* In the future the user will be given the option to choose between
* selection sort, insertion sort, and quicksort.
*
*
* @param[in,out] arr1 - input array to be sorted
* @param[in,out] arr2 - array to be sorted based on other array
* @param[in] low - lower index bound of array slice
* @param[in] high - higher index bound of array slice
*/
void quickSort(int* arr1, int* arr2, int low, int high)
{
if (low < high) {
int pi = partition(arr1, arr2, low, high);
quickSort(arr1, arr2, low, pi - 1);
quickSort(arr1, arr2, pi + 1, high);
}
}
/**
* @brief Insertion sorts arr1 and arr2 w/indices
* based on increasing value in arr1
*
* The complexity of insertion sort is O(n^2) in the worst case.
* It is chosen here because it is simple and efficient for small n.
* In the future the user will be given the option to choose between
* selection sort, insertion sort, and quicksort.
*
* @param[in] n - Size of the matrix,
* @param[in,out] arr1 - the array that determines the sorting order,
* @param[in,out] arr2 - sorted based on arr1
*
* @pre arr1 and arr2 are arrays of length n
*
* @post arr1 and arr2 are sorted based on increasing values in arr1
*/
void insertionSort(int n, int* arr1, int* arr2)
{
int i, key1, key2, j;
for (i = 1; i < n; i++) {
key1 = arr1[i];
key2 = arr2[i];
j = i - 1;
while (j >= 0 && arr1[j] > key1) {
arr1[j + 1] = arr1[j];
arr2[j + 1] = arr2[j];
j = j - 1;
}
arr1[j + 1] = key1;
arr2[j + 1] = key2;
}
}
/**
* @brief Permutes the columns in a matrix represented by rows and cols
*
* @param[in] n - size of the matrix
* @param[in] rows - row offsets of matrix
* @param[in] cols - column indices of matrix
* @param[in] rev_perm - reverse permutation
* @param[out] perm_cols - permuted column array
* @param[out] perm_map - corresponding indices to facilitate permuting the values
*
* @pre rev_perm has integers 0 to n-1 (permuted),
* rows and cols present valid csr storage array
*
* @post perm_cols is now the permuted column array and perm_map stores
* the corresponding indices to facilitate permuting the values
*/
void makeVecMapC(int n,
const int* rows,
const int* cols,
const int* rev_perm,
int* perm_cols,
int* perm_map)
{
int row_s;
int rowlen;
for(int i = 0; i < n; i++) {
row_s = rows[i];
rowlen = rows[i + 1] - row_s;
for(int j = 0; j < rowlen; j++) {
perm_map[row_s + j] = row_s + j;
perm_cols[row_s + j] = rev_perm[cols[row_s + j]];
}
insertionSort(rowlen, &perm_cols[row_s], &perm_map[row_s]);
}
}
/**
* @brief Creates a reverse permutation based on a given permutation
*
* @param[in] n - size of the permutation
* @param[in] perm - permutation array
* @param[out] rev_perm - reversed permutation array
*
* @pre perm has integers 0 to n-1 (permuted),
*
* @post rev_perm now contains the reverse permutation
*/
void reversePerm(int n, const int* perm, int* rev_perm)
{
for(int i = 0; i < n; i++) {
rev_perm[perm[i]] = i;
}
}
/**
* @brief Permutes the rows in a matrix represented by rows and cols
*
* @param[in] n - size of the matrix
* @param[in] rows - row offsets of matrix
* @param[in] cols - column indices of matrix
* @param[in] perm - permutation array
* @param[out] perm_rows - row offsets of permuted matrix
* @param[out] perm_cols - column indices of permuted matrix
* @param[out] perm_map - corresponding indices to facilitate permuting the values
*
* @pre perm has integers 0 to n-1 (permuted),
* rows and cols present valid csr storage array
*
* @post perm_rows and perm_cols are now the permuted rows and column arrays,
* perm_map stores the corresponding indices to facilitate permuting the values
*/
void makeVecMapR(int n,
const int* rows,
const int* cols,
const int* perm,
int* perm_rows,
int* perm_cols,
int* perm_map)
{
perm_rows[0] = 0;
int count = 0;
int idx;
int row_s;
int rowlen;
for(int i = 0; i < n; i++) {
idx = perm[i];
row_s = rows[idx];
rowlen = rows[idx + 1] - row_s;
perm_rows[i + 1] = perm_rows[i] + rowlen;
for(int j = 0; j < rowlen; j++)
{
perm_map[count + j] = row_s + j;
perm_cols[count + j] = cols[row_s + j];
}
count += rowlen;
}
}
/**
* @brief Permutes the rows and columns in a matrix represented by rows
* and cols
*
* @param[in] n - size of the matrix
* @param[in] rows - row offsets of matrix
* @param[in] cols - column indices of matrix
* @param[in] perm - permutation array
* @param[in] rev_perm - reverse permutation array
* @param[out] perm_rows - row offsets of permuted matrix
* @param[out] perm_cols - column indices of permuted matrix
* @param[out] perm_map - corresponding indices to facilitate permuting the values
*
* @pre perm and rev_perm have corresponding integers 0 to n-1 (permuted),
* rows and cols present valid csr storage array
*
* @post perm_rows and perm_cols are now the permuted rows and column
* arrays, perm_map stores the corresponding indices to facilitate
* permuting the values
*/
void makeVecMapRC(int n,
const int* rows,
const int* cols,
const int* perm,
const int* rev_perm,
int* perm_rows,
int* perm_cols,
int* perm_map)
{
perm_rows[0] = 0;
int count = 0;
int idx;
int row_s;
int rowlen;
for(int i = 0; i < n; i++) {
idx = perm[i];
row_s = rows[idx];
rowlen = rows[idx + 1] - row_s;
perm_rows[i + 1] = perm_rows[i] + rowlen;
for(int j = 0; j < rowlen; j++)
{
perm_map[count + j] = row_s + j;
perm_cols[count + j] = rev_perm[cols[row_s + j]];
}
insertionSort(rowlen, &perm_cols[count], &perm_map[count]);
count += rowlen;
}
}
} // namespace hykkt
} // namespace ReSolve