This repository has been archived by the owner on May 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Utils.cpp
245 lines (202 loc) · 5.83 KB
/
Utils.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
#include <cfloat>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <sys/time.h>
#include <string>
#include <algorithm>
#include "Utils.hpp"
using namespace std;
namespace SpMP
{
double get_cpu_freq()
{
static double freq = DBL_MAX;
if (DBL_MAX == freq) {
volatile double a = rand()%1024, b = rand()%1024;
struct timeval tv1, tv2;
gettimeofday(&tv1, NULL);
unsigned long long t1 = __rdtsc();
for (size_t i = 0; i < 1024L*1024; i++) {
a += a*b + b/a;
}
unsigned long long dt = __rdtsc() - t1;
gettimeofday(&tv2, NULL);
freq = dt/((tv2.tv_sec - tv1.tv_sec) + (tv2.tv_usec - tv1.tv_usec)/1.e6);
}
return freq;
}
void getSimpleThreadPartition(int* begin, int *end, int n)
{
int nthreads = omp_get_num_threads();
int tid = omp_get_thread_num();
int n_per_thread = (n + nthreads - 1)/nthreads;
*begin = std::min(n_per_thread*tid, n);
*end = std::min(*begin + n_per_thread, n);
}
void getLoadBalancedPartition(int *begin, int *end, const int *prefixSum, int n)
{
int nthreads = omp_get_num_threads();
int tid = omp_get_thread_num();
int base = prefixSum[0];
int total_work = prefixSum[n] - base;
int work_per_thread = (total_work + nthreads - 1)/nthreads;
*begin = tid == 0 ? 0 : lower_bound(prefixSum, prefixSum + n, work_per_thread*tid + base) - prefixSum;
*end = tid == nthreads - 1 ? n : lower_bound(prefixSum, prefixSum + n, work_per_thread*(tid + 1) + base) - prefixSum;
assert(*begin <= *end);
assert(*begin >= 0 && *begin <= n);
assert(*end >= 0 && *end <= n);
}
void parMemset(void *ptr, int c, size_t n)
{
#pragma omp parallel if (!omp_in_parallel())
{
int nCacheLines = n/64;
int nCacheLineBegin, nCacheLineEnd;
getSimpleThreadPartition(&nCacheLineBegin, &nCacheLineEnd, nCacheLines);
int nBegin = nCacheLineBegin*64;
int nEnd = omp_get_thread_num() == omp_get_num_threads() - 1 ? n : nCacheLineEnd*64;
memset((char *)ptr + nBegin, c, nEnd - nBegin);
}
}
void parMemcpy(void *dst, const void *src, size_t n)
{
#pragma omp parallel if (!omp_in_parallel())
{
int nCacheLines = n/64;
int nCacheLineBegin, nCacheLineEnd;
getSimpleThreadPartition(&nCacheLineBegin, &nCacheLineEnd, nCacheLines);
int nBegin = nCacheLineBegin*64;
int nEnd = omp_get_thread_num() == omp_get_num_threads() - 1 ? n : nCacheLineEnd*64;
memcpy((char *)dst + nBegin, (char *)src + nBegin, nEnd - nBegin);
}
}
void getInversePerm(int *inversePerm, const int *perm, int n)
{
#pragma omp parallel for
for (int i = 0; i < n; ++i) {
inversePerm[perm[i]] = i;
}
}
bool isPerm(const int *perm, int n)
{
int *temp = new int[n];
memcpy(temp, perm, sizeof(int)*n);
sort(temp, temp + n);
int *last = unique(temp, temp + n);
if (last != temp + n) {
memcpy(temp, perm, sizeof(int)*n);
sort(temp, temp + n);
for (int i = 0; i < n; ++i) {
if (temp[i] == i - 1) {
printf("%d duplicated\n", i - 1);
assert(false);
return false;
}
else if (temp[i] != i) {
printf("%d missed\n", i);
assert(false);
return false;
}
}
}
delete[] temp;
return true;
}
bool isInversePerm(const int *perm, const int *inversePerm, int len)
{
for (int i = 0; i < len; ++i) {
if (inversePerm[perm[i]] != i) return false;
}
return true;
}
template<class T>
void CopyVector(T *dst, const T *src, int len)
{
#pragma omp parallel for
for (int i = 0; i < len; ++i) {
dst[i] = src[i];
}
}
template<class T>
void reorderVectorOutOfPlace_(T *dst, const T *src, const int *perm, int len)
{
if (perm) {
#pragma omp parallel for
for (int i = 0; i < len; ++i) {
assert(perm[i] >= 0 && perm[i] < len);
dst[perm[i]] = src[i];
}
}
else {
CopyVector(dst, src, len);
}
}
void reorderVectorOutOfPlace(double *dst, const double *src, const int *perm, int len)
{
return reorderVectorOutOfPlace_(dst, src, perm, len);
}
void reorderVectorOutOfPlace(int *dst, const int *src, const int *perm, int len)
{
return reorderVectorOutOfPlace_(dst, src, perm, len);
}
template<class T>
void reorderVectorOutOfPlaceWithInversePerm_(T *dst, const T *src, const int *inversePerm, int len)
{
if (inversePerm) {
#pragma omp parallel for
for (int i = 0; i < len; ++i) {
assert(inversePerm[i] >= 0 && inversePerm[i] < len);
dst[i] = src[inversePerm[i]];
}
}
else {
CopyVector(dst, src, len);
}
}
void reorderVectorOutOfPlaceWithInversePerm(double *dst, const double *src, const int *inversePerm, int len)
{
return reorderVectorOutOfPlaceWithInversePerm_(dst, src, inversePerm, len);
}
void reorderVectorOutOfPlaceWithInversePerm(int *dst, const int *src, const int *inversePerm, int len)
{
return reorderVectorOutOfPlaceWithInversePerm_(dst, src, inversePerm, len);
}
double *getReorderVector(const double *v, const int *perm, int len)
{
double *ret = MALLOC(double, len);
reorderVectorOutOfPlace(ret, v, perm, len);
return ret;
}
double *getReorderVectorWithInversePerm(const double *v, const int *perm, int len)
{
double *ret = MALLOC(double, len);
reorderVectorOutOfPlaceWithInversePerm(ret, v, perm, len);
return ret;
}
void reorderVector(double *v, double *tmp, const int *perm, int len)
{
if (!perm) return;
reorderVectorOutOfPlace(tmp, v, perm, len);
CopyVector(v, tmp, len);
}
void reorderVectorWithInversePerm(double *v, double *tmp, const int *inversePerm, int len)
{
if (!inversePerm) return;
reorderVectorOutOfPlaceWithInversePerm(tmp, v, inversePerm, len);
CopyVector(v, tmp, len);
}
void reorderVector(double *v, const int *perm, int len)
{
double *tmp = MALLOC(double, len);
reorderVector(v, tmp, perm, len);
FREE(tmp);
}
void reorderVectorWithInversePerm(double *v, const int *perm, int len)
{
double *tmp = MALLOC(double, len);
reorderVectorWithInversePerm(v, tmp, perm, len);
FREE(tmp);
}
} // namespace SpMP