-
Notifications
You must be signed in to change notification settings - Fork 1
/
GeneralizationKernel.cu
224 lines (193 loc) · 5.54 KB
/
GeneralizationKernel.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
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
#define GPU_GLOBAL_ALIGN 128
#define ADS_INFO_SIZE GPU_GLOBAL_ALIGN
#define ADS_INFO_ELEMS (ADS_INFO_SIZE / sizeof(uint))
/*!
* MLADSInfo
*/
struct MLADSInfo {
uint size; ///< ADS buffer size in bytes
uint rowElems; ///< ADS row elems
uint solElems; ///< Solution elems
uint solCost; ///< Solution cost
uint tour; ///< Tour (0/1)
uint round; ///< Round: 0=0.0, 1=0.5
};
/*!
* MLADSData
*/
union MLADSData {
MLADSInfo s;
uint v[ADS_INFO_ELEMS];
};
/*!
* MLMove64
*/
/*
struct MLMove64 {
uint id : 4;
uint i : 14;
uint j : 14;
int cost : 32;
};
*/
struct MLMove64 {
unsigned int id : 4;
unsigned int i : 14;
unsigned int j : 14;
int cost; // REMOVED :32 !! Goddamn Eyder...
};
/*!
* MLMovePack
*/
union MLMovePack {
MLMove64 s;
ulong w;
int i[2];
uint u[2];
long l[1];
};
__global__ void kernel2Opt(const MLADSData* gm_ads, MLMovePack* gm_move,
const int size) {
/*!
* Shared memory variables
*/
extern __shared__ int sm_buffer[]; // Dynamic shared memory buffer
/*!
* Local variables
*/
__shared__ int *sm_soldist, // Client distances
*sm_coordx, // Clients x-coordinates
*sm_coordy, // Clients y-coordinates
*sm_move, // Thread movement cost
sm_rsize, // ADS row size
sm_tour; // Tour/path
uint* gm_data; // Points to some ADS data
int c, // Chunk no
ctx, // Tx index for chunk
cmax, // Number of chunks
csize; // Chunk size
int cost, // Cost improvement
i, j, // Movement indexes
k; // Last solution index
if (tx >= size) return;
/*
* Dynamic shared memory buffer usage
*
* buffer
* |
* v
* +---------+--------+--------+------------------+
* | soldist | coordx | coordy | movcost | moveid |
* +---------+--------+--------+------------------+
*/
/*
* Set pointers to proper location in 'sm_buffer'
* Only thread 0 initializes shared variables
*/
if (tx == 0) {
sm_soldist = sm_buffer;
sm_coordx = sm_soldist + size;
sm_coordy = sm_coordx + size;
sm_move = sm_coordy + size;
sm_rsize = gm_ads->s.rowElems;
sm_tour = gm_ads->s.tour;
}
__syncthreads();
// Number of chunks
cmax = GPU_DIVCEIL(size, blockDim.x);
// Chunk size
csize = GPU_MIN(size, int(blockDim.x));
// Get clients coordinates
gm_data = ADS_COORD_PTR(gm_ads);
for (c = 0; (c < cmax) && ((ctx = c * blockDim.x + tx) < size); c++) {
sm_coordx[ctx] = GPU_HI_USHORT(gm_data[ctx]);
sm_coordy[ctx] = GPU_LO_USHORT(gm_data[ctx]);
}
// Get solution distances
gm_data = ADS_SOLUTION_PTR(gm_ads, sm_rsize);
for (c = 0; (c < cmax) && ((ctx = c * blockDim.x + tx) < size); c++)
sm_soldist[ctx] = GPU_LO_USHORT(gm_data[ctx]);
// Initialize movement/cost
sm_move[tx] = COST_INFTY;
// Wait all threads arrange data
__syncthreads();
/*
if(tx == 0 && by == 0) {
kprintf("COORDS\n");
for(i=0;i < size;i++)
kprintf("%d\t(%d,%d)\n",i,sm_coordx[i],sm_coordy[i]);
kprintf("\n");
kprintf("DIST\n");
for(i=0;i < size;i++)
kprintf(" %d",sm_soldist[i]);
kprintf("\n");
}
*/
// Get solution distances
for (c = 0; c < cmax; c++) {
ctx = c * blockDim.x + tx;
if (ctx < size - sm_tour - 2) {
// Movement indexes
k = ctx >= by;
i = k * (ctx - by + 1) + (!k) * (by - ctx);
j = k * (ctx + 2) + (!k) * (size - ctx - sm_tour - 1);
cost = (size - i) * (int(GPU_DIST_COORD(i - 1, j)) - sm_soldist[i]);
// When computing PATHs and j = size-1, there's no j+1 element
if (j + 1 < size)
cost += (size - j - 1) *
(int(GPU_DIST_COORD(i, j + 1)) - sm_soldist[j + 1]);
for (k = 1; k <= j - i; k++)
cost +=
(size - i - k) * (int(sm_soldist[j - k + 1]) - sm_soldist[i + k]);
k4printf("GPU_2OPT(%d,%d) = %d\n", i, j, cost);
if (cost < sm_move[tx]) {
sm_move[tx] = cost;
sm_move[tx + csize] = GPU_MOVE_PACKID(i, j, MLMI_2OPT);
}
}
}
__syncthreads();
/*
* Minimum cost reduction
*/
for (i = GPU_DIVCEIL(csize, 2); i > 1; i = GPU_DIVCEIL(i, 2)) {
if (tx < i) {
if ((tx + i < csize) && (sm_move[tx] > sm_move[tx + i])) {
sm_move[tx] = sm_move[tx + i];
sm_move[tx + csize] = sm_move[tx + csize + i];
}
}
__syncthreads();
}
if (tx == 0) {
// The first 2 elements was not compared
if (sm_move[0] > sm_move[1]) {
sm_move[0] = sm_move[1];
sm_move[csize] = sm_move[csize + 1];
}
gm_move[by].w = GPU_MOVE_PACK64(sm_move[0], sm_move[csize]);
k4printf("Block %d: GPU_2OPT(%u,%u) = %d\n", by, gm_move[by].s.i,
gm_move[by].s.j, gm_move[by].s.cost);
}
/*
// MIN cost reduction
for(i=GPU_DIVCEIL(csize,2);i > 0;i >>= 1) {
if(tx < i) {
if(tx + i < csize) {
if(sm_move[tx] > sm_move[tx + i]) {
sm_move[tx] = sm_move[tx + i];
sm_move[csize + tx] = sm_move[csize + tx + i];
}
}
}
__syncthreads();
}
if(tx == 0) {
gm_move[by].w = GPU_MOVE_PACK64(sm_move[0],sm_move[csize]);
k4printf("Block %d: MIN 2OPT(%u,%u) = %d\n",by,
gm_move[by].s.i,
gm_move[by].s.j,
gm_move[by].s.cost);
}
*/
}