This repository has been archived by the owner on Apr 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v1.1.1.c
192 lines (155 loc) · 6.46 KB
/
v1.1.1.c
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
#include <stdio.h>
#include <stdlib.h>
#define CL_TARGET_OPENCL_VERSION 120
#include "ocl_boiler.h"
#include "setup.h"
cl_event kernel(
cl_command_queue q, cl_kernel k, cl_event *waiting_event, cl_uint waiting_list_length,
size_t preferred_multiple_init, cl_mem d_permutations, cl_mem d_adj, cl_mem d_costs, cl_int v,
cl_int work_size) {
cl_int err;
AddKernelArg(k, 0, sizeof(d_permutations), &d_permutations);
AddKernelArg(k, 1, sizeof(d_adj), &d_adj);
AddKernelArg(k, 2, sizeof(d_costs), &d_costs);
AddKernelArg(k, 3, sizeof(v), &v);
AddKernelArg(k, 4, sizeof(work_size), &work_size);
size_t gws[] = {round_mul_up(work_size, preferred_multiple_init)};
cl_event kernel_evt;
err = clEnqueueNDRangeKernel(q, k,
1, NULL, gws, NULL,
waiting_list_length, waiting_event, &kernel_evt);
ocl_check(err, "launch kernel");
return kernel_evt;
}
int main(int argc, char *argv[]) {
struct Task task = {0};
cl_int err;
//region Params
if (argc < 2 || argc > 6) {
fprintf(stderr, "Usage: %s <nVertexes> [chunkSize] [seed] [missCoeficient] [maxWeight]\n", argv[0]);
return 1;
}
int p = 1;
const int v = atoi(argv[p++]);
const int chunks = argc > p ? atoi(argv[p++]) : 1024;
const int seed = argc > p ? atoi(argv[p++]) : 42;
const int missCoeficient = argc > p ? atoi(argv[p++]) : 2;
const int maxWeight = argc > p ? atoi(argv[p++]) : 100;
task.vertexes = v;
task.chunkCoeficient = chunks;
task.seed = seed;
task.missCoeficient = missCoeficient;
task.maxWeight = maxWeight;
if (v < 2) {
fprintf(stderr, "Number of vertices must be at least 2\n");
return 2;
}
//endregion
struct Info info = initialize("Cached_GlobalArray_SingleResult_char");
//region Initialize Graph
size_t adjsize = v * v * sizeof(int);
int *adj = malloc(adjsize);
init_graph(adj, v, seed, missCoeficient, maxWeight);
cl_mem d_adj = clCreateBuffer(info.context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR | CL_MEM_ALLOC_HOST_PTR,
adjsize, adj,
&err);
ocl_check(err, "create d_adj");
//endregion
//region Kernel Execution
int i;
int minCost = INFINITY;
unsigned long long chunk_size = info.preferred_multiple_init * chunks;
unsigned long long permutations_size = chunk_size * (v - 1) * sizeof(int);
int costs_size = chunk_size * sizeof(int);
char *permutations = malloc(permutations_size);
int *costs = malloc(costs_size);
char *path = malloc((v - 1) * sizeof(char));
for (i = 1; i < v; ++i) {
path[i - 1] = i;
}
task.chunkSize = chunk_size;
task.totalPermutations = factorial(v - 1);
task.totalChunks = (task.totalPermutations + chunk_size - 1) / chunk_size;
task.chunkRuns = malloc(task.totalChunks * sizeof(struct ChunkRun));
cl_mem d_permutations = clCreateBuffer(info.context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, permutations_size,
permutations, &err);
ocl_check(err, "create d_permutations");
cl_mem d_costs = clCreateBuffer(info.context, CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR, costs_size, costs,
&err);
ocl_check(err, "create d_costs");
cl_event write_evt;
cl_event kernel_evt;
cl_event read_evt;
cl_event unmap_evt;
cl_event first_evt;
cl_event last_evt;
char continueLoop = 1;
char first = 1;
int nChunk = 0;
int percentageUpadte = task.totalChunks / 10 + 1;
printf("Total chunks: %llu\nUpdate each %d chunks\n", task.totalChunks, percentageUpadte);
while (continueLoop) {
nChunk++;
int current_number_of_permutations = 0;
do {
for (i = 0; i < v - 1; ++i) {
permutations[current_number_of_permutations * (v - 1) + i] = path[i];
}
++current_number_of_permutations;
if (current_number_of_permutations == chunk_size) {
continueLoop = next_permutation_chars(path, v - 1);
break;
}
continueLoop = next_permutation_chars(path, v - 1);
} while (continueLoop);
err = clEnqueueWriteBuffer(info.queue, d_permutations, CL_FALSE, 0,
current_number_of_permutations * (v - 1) * sizeof(char),
permutations, 0, NULL, &write_evt);
ocl_check(err, "enqueue write");
kernel_evt = kernel(
info.queue, info.kernel, &write_evt, 1, info.preferred_multiple_init,
d_permutations, d_adj, d_costs, v, current_number_of_permutations);
clEnqueueMapBuffer(info.queue, d_costs, CL_FALSE, CL_MAP_READ, 0, costs_size, 1, &kernel_evt, &read_evt,
&err);
ocl_check(err, "map costs");
err = clEnqueueUnmapMemObject(info.queue, d_costs, costs, 1, &read_evt, &unmap_evt);
ocl_check(err, "unmap costs");
err = clWaitForEvents(1, &unmap_evt);
ocl_check(err, "wait for events");
for (i = 0; i < current_number_of_permutations; ++i) {
if (costs[i] < minCost) {
minCost = costs[i];
}
}
struct ChunkRun chunkRun = {0};
chunkRun.size = current_number_of_permutations;
chunkRun.write_runtime = runtime_ms(write_evt);
chunkRun.kernel_runtime = runtime_ms(kernel_evt);
chunkRun.read_runtime = total_runtime_ms(read_evt, unmap_evt);
chunkRun.write_bw = current_number_of_permutations * (v - 1) * sizeof(char);
chunkRun.kernel_bw = 2.0 * v * current_number_of_permutations * sizeof(char);
chunkRun.read_bw = costs_size;
if (nChunk % percentageUpadte == 0) {
printf("Chunk %d/%llu\n", nChunk, task.totalChunks);
}
task.chunkRuns[nChunk - 1] = chunkRun;
if (first) {
first = 0;
first_evt = write_evt;
}
}
last_evt = unmap_evt;
task.cost = minCost;
task.runtime = total_runtime_ms(first_evt, last_evt);
printf("Chunk %d/%llu\n", nChunk, task.totalChunks);
printf("Total runtime: %.3f ms\n", task.runtime);
//endregion
FILE *f = fopen("v1.1.1.csv", "a");
printResult(f, task);
fclose(f);
freeInfo(info);
clReleaseMemObject(d_adj);
clReleaseMemObject(d_permutations);
clReleaseMemObject(d_costs);
return 0;
}