-
Notifications
You must be signed in to change notification settings - Fork 1
/
array_utils.cu
94 lines (71 loc) · 1.83 KB
/
array_utils.cu
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
#include "globals.h"
void unstack(int, int*);
void get_r(int id, float* r) {
int i, * n = new int[Dim];
unstack(id, n);
for (i = 0; i < Dim; i++)
r[i] = dx[i] * float(n[i]);
}
float get_k(int id, float* k, int Dim) {
// id between 0 and M-1 (i value in loop), float k kx,ky,ky (that vecotor), Dim is dimensionality
// declare a vector for this
float kmag = 0.0f;
int i, *n;
n = new int[Dim];
unstack(id, n);
for (i = 0; i < Dim; i++) {
if (float(n[i]) < float(Nx[i]) / 2.)
k[i] = PI2 * float(n[i]) / L[i];
else
k[i] = PI2 * float(n[i] - Nx[i]) / L[i];
kmag += k[i] * k[i];
}
delete n;
return kmag;
}
// Receives index id in [0 , M ) and makes array
// nn[Dim] in [ 0 , Nx[Dim] )
void unstack(int id, int* nn) {
if (Dim == 1) {
nn[0] = id;
return;
}
else if (Dim == 2) {
nn[1] = id / Nx[0];
nn[0] = (id - nn[1] * Nx[0]);
return;
}
else if (Dim == 3) {
nn[2] = id / Nx[1] / Nx[0];
nn[1] = id / Nx[0] - nn[2] * Nx[1];
nn[0] = id - (nn[1] + nn[2] * Nx[1]) * Nx[0];
return;
}
else {
cout << "Dim is goofy!" << endl;
return;
}
}
void unstack_like_device(int id, int* nn) {
if (Dim == 1) {
nn[0] = id;
return;
}
else if (Dim == 2) {
nn[1] = id / Nx[0];
nn[0] = (id - nn[1] * Nx[0]);
return;
}
else if (Dim == 3) {
int idx = id;
nn[2] = idx / (Nx[1] * Nx[0]);
idx -= (nn[2] * Nx[0] * Nx[1]);
nn[1] = idx / Nx[0];
nn[0] = idx % Nx[0];
return;
}
else {
cout << "Dim is goofy!" << endl;
return;
}
}