-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindiceshelperfunctions.h
More file actions
161 lines (135 loc) · 3.8 KB
/
indiceshelperfunctions.h
File metadata and controls
161 lines (135 loc) · 3.8 KB
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
#ifndef HELPERFUNCTIONS
#define HELPERFUNCTIONS
#include "omp.h"
#include <cstddef>
#pragma omp begin declare target
inline size_t compute_offset(const size_t row, const size_t col, const size_t* __restrict strides)
{
return row * strides[0]+col*strides[1];
}
#pragma omp end declare target
#pragma omp begin declare target
inline size_t compute_offset(const size_t row, const size_t col, const size_t matrixstride0, const size_t matrixstride1)
{
return row * matrixstride0+col*matrixstride1;
}
#pragma omp end declare target
#pragma omp begin declare target
inline size_t compute_offset_w(const size_t * __restrict indices, const size_t* __restrict strides,const size_t r)
{
size_t offset = 0;
// Row-major layout: iterate outermost to innermost
#pragma omp parallel for simd reduction(+ : offset)
for (size_t i = 0; i < r; ++i)
{
offset += indices[i] * strides[i];
}
return offset;
}
#pragma omp end declare target
#pragma omp begin declare target
inline size_t compute_offset_s(const size_t * __restrict indices, const size_t* __restrict strides,const size_t r)
{
size_t offset = 0;
// Row-major layout: iterate outermost to innermost
#pragma omp unroll partial
for (size_t i = 0; i < r; ++i)
{
offset += indices[i] * strides[i];
}
return offset;
}
#pragma omp end declare target
#pragma omp begin declare target
inline size_t compute_offset_v(const size_t * __restrict indices, const size_t* __restrict strides,const size_t r)
{
size_t offset = 0;
// Row-major layout: iterate outermost to innermost
#pragma omp simd reduction(+ : offset)
for (size_t i = 0; i < r; ++i)
{
offset += indices[i] * strides[i];
}
return offset;
}
#pragma omp end declare target
#pragma omp begin declare target
inline size_t compute_data_length_w(const size_t*__restrict extents, const size_t*__restrict strides,const size_t rank)
{
size_t offset=0;
#pragma omp parallel for simd reduction(+:offset)
for (size_t i = 0; i < rank; ++i)
{
offset += (extents[i]-1) * strides[i];
}
return offset+1;
}
#pragma omp end declare target
#pragma omp begin declare target
inline size_t compute_data_length_v(const size_t*__restrict extents, const size_t*__restrict strides,const size_t rank)
{
size_t offset=0;
#pragma omp simd reduction(+:offset)
for (size_t i = 0; i < rank; ++i)
{
offset += (extents[i]-1) * strides[i];
}
return offset+1;
}
#pragma omp end declare target
#pragma omp begin declare target
inline size_t compute_data_length_s(const size_t*__restrict extents, const size_t*__restrict strides,const size_t rank)
{
size_t offset=0;
for (size_t i = 0; i < rank; ++i)
{
offset += (extents[i]-1) * strides[i];
}
return offset+1;
}
#pragma omp end declare target
#pragma omp begin declare target
inline bool is_row_major(const size_t*extents, const size_t* strides, const size_t rank)
{
size_t expected = 1;
for (size_t i = 0; i < rank; ++i)
{
if (extents[i] == 1)
continue;
if (strides[i] != expected)
return false;
expected *= extents[i];
}
return true;
}
#pragma omp end declare target
#pragma omp begin declare target
inline bool checkPrimeNumber(int n)
{
bool isPrime = true;
switch (n)
{
case 0:
case 1:
isPrime=false;
break;
case 2:
case 3:
isPrime=false;
break;
default:
{
for (int i = 2; i <= n / 2; ++i)
{
if (n % i == 0)
{
isPrime = false;
break;
}
}
}
}
return isPrime;
}
#pragma omp end declare target
#endif