-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathhost.c
388 lines (309 loc) · 14.6 KB
/
host.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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*******************************************************************************
** HOST Code
*******************************************************************************/
#include "host.h"
#include <iostream>
int main(int argc, char* argv[])
{
cl_int err;
cl_uint check_status = 0;
const cl_uint DATA_SIZE = 4096;
cl_platform_id platform_id;
cl_device_id device_id;
cl_context context;
cl_command_queue commands;
cl_program program;
cl_kernel kernel_vector_add;
cl_mem buffer_in1;
cl_mem buffer_in2;
cl_mem buffer_output;
cl_mem pt[2];
// cl_mem* buffer_in[2];
cl_uint* source_in1;
cl_uint* source_in2;
cl_uint* source_sw_results;
char cl_platform_vendor[1001];
if (argc != 3) {
printf("Usage: %s xclbin\n", argv[0]);
return EXIT_FAILURE;
}
const char* target_device_name = argv[2];
cl_platform_id platforms[16];
cl_uint platform_count;
cl_uint platform_found = 0;
cl_uint num_devices;
cl_uint device_found = 0;
cl_device_id devices[16];
char cl_device_name[1001];
source_in1 = (cl_uint*)aligned_alloc(MEM_ALIGNMENT,MAX_LENGTH * sizeof(cl_uint*));
source_in2 = (cl_uint*)aligned_alloc(MEM_ALIGNMENT,MAX_LENGTH * sizeof(cl_uint*));
source_sw_results = (cl_uint*)aligned_alloc(MEM_ALIGNMENT,MAX_LENGTH * sizeof(cl_uint*));
cl_uint* source_hw_results = (cl_uint*)aligned_alloc(MEM_ALIGNMENT,MAX_LENGTH * sizeof(cl_uint*));
for(cl_uint i = 0; i < MAX_LENGTH; i++) {
source_in1[i] = rand() % DATA_SIZE;
std::cout <<"input "<< source_in1[i]<<std::endl;
source_in2[i] = rand() % DATA_SIZE ;
source_sw_results[i] = source_in1[i] + source_in2[i];
source_hw_results[i] = 0;
}
// ------------------------------------------------------------------------------------
// Step 1: Get All PLATFORMS, then search for Target_Platform_Vendor (CL_PLATFORM_VENDOR)
// ------------------------------------------------------------------------------------
// Get the number of platforms
// ..................................................
err = clGetPlatformIDs(16, platforms, &platform_count);
if (err != CL_SUCCESS) {
printf("Error: Failed to find an OpenCL platform!\n");
printf("Test failed\n");
return EXIT_FAILURE;
}
printf("INFO: Found %d platforms\n", platform_count);
// ....................................................................................
// step 1: Search for Platform (ex: Xilinx) using: CL_PLATFORM_VENDOR = Target_Platform_Vendor
// Check if the current platform matches Target_Platform_Vendor
// ....................................................................................
for (cl_uint iplat=0; iplat<platform_count; iplat++) {
err = clGetPlatformInfo(platforms[iplat], CL_PLATFORM_VENDOR, 1000, (void *)cl_platform_vendor,NULL);
if (err != CL_SUCCESS) {
printf("Error: clGetPlatformInfo(CL_PLATFORM_VENDOR) failed!\n");
printf("Test failed\n");
return EXIT_FAILURE;
}
if (strcmp(cl_platform_vendor, "Xilinx") == 0) {
printf("INFO: Selected platform %d from %s\n", iplat, cl_platform_vendor);
platform_id = platforms[iplat];
platform_found = 1;
}
}
if (!platform_found) {
printf("ERROR: Platform Xilinx not found. Exit.\n");
return EXIT_FAILURE;
}
// ------------------------------------------------------------------------------------
// Step 1: Get All Devices for selected platform Target_Platform_ID
// then search for Xilinx platform (CL_DEVICE_TYPE_ACCELERATOR = Target_Device_Name)
// ------------------------------------------------------------------------------------
err = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_ACCELERATOR, 16, devices, &num_devices);
printf("INFO: Found %d devices\n", num_devices);
if (err != CL_SUCCESS) {
printf("ERROR: Failed to create a device group!\n");
printf("ERROR: Test failed\n");
return -1;
}
// ------------------------------------------------------------------------------------
// Step 1: Search for CL_DEVICE_NAME = Target_Device_Name
// ............................................................................
for (cl_uint i=0; i<num_devices; i++) {
err = clGetDeviceInfo(devices[i], CL_DEVICE_NAME, 1024, cl_device_name, 0);
if (err != CL_SUCCESS) {
printf("Error: Failed to get device name for device %d!\n", i);
printf("Test failed\n");
return EXIT_FAILURE;
}
printf("CL_DEVICE_NAME %s\n", cl_device_name);
// ............................................................................
// Step 1: Check if the current device matches Target_Device_Name
// ............................................................................
if(strcmp(cl_device_name, target_device_name) == 0) {
device_id = devices[i];
device_found = 1;
printf("Selected %s as the target device\n", cl_device_name);
}
}
// ------------------------------------------------------------------------------------
// Step 1: Create Context
// ------------------------------------------------------------------------------------
context = clCreateContext(0, 1, &device_id, NULL, NULL, &err);
if (!context) {
printf("Error: Failed to create a compute context!\n");
printf("Test failed\n");
return EXIT_FAILURE;
}
// ------------------------------------------------------------------------------------
// Step 1: Create Command Queue
// ------------------------------------------------------------------------------------
commands = clCreateCommandQueue(context, device_id, CL_QUEUE_PROFILING_ENABLE, &err);
if (!commands) {
printf("Error: Failed to create a command commands!\n");
printf("Error: code %i\n",err);
printf("Test failed\n");
return EXIT_FAILURE;
}
cl_int status;
unsigned char *kernelbinary;
char *xclbin = argv[1];
// ------------------------------------------------------------------
// Step 1: Load Binary File from a disk to Memory
// ------------------------------------------------------------------
printf("INFO: loading xclbin %s\n", xclbin);
cl_uint n_i0 = load_file_to_memory(xclbin, (char **) &kernelbinary);
if (n_i0 < 0) {
printf("failed to load kernel from xclbin: %s\n", xclbin);
printf("Test failed\n");
return EXIT_FAILURE;
}
size_t n0 = n_i0;
// ------------------------------------------------------------
// Step 1: Create a program using a Binary File
// ------------------------------------------------------------
program = clCreateProgramWithBinary(context, 1, &device_id, &n0,
(const unsigned char **) &kernelbinary, &status, &err);
free(kernelbinary);
// ============================================================================
// Step 2: Create Program and Kernels
// ============================================================================
// o) Build a Program from a Binary File
// o) Create Kernels
// ============================================================================
if ((!program) || (err!=CL_SUCCESS)) {
printf("Error: Failed to create compute program from binary %d!\n", err);
printf("Test failed\n");
return EXIT_FAILURE;
}
// -------------------------------------------------------------
// Step 2: Build (compiles and links) a program executable from binary
// -------------------------------------------------------------
err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
if (err != CL_SUCCESS) {
size_t len;
char buffer[2048];
printf("Error: Failed to build program executable!\n");
clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len);
printf("%s\n", buffer);
printf("Test failed\n");
return EXIT_FAILURE;
}
// -------------------------------------------------------------
// Step 2: Create Kernels
// -------------------------------------------------------------
kernel_vector_add = clCreateKernel(program, "vadd", &err);
if (!kernel_vector_add || err != CL_SUCCESS) {
printf("Error: Failed to create compute kernel_vector_add!\n");
printf("Test failed\n");
return EXIT_FAILURE;
}
// ================================================================
// Step 2: Setup Buffers and un Kernels
// ================================================================
// o) Allocate Memory to store the results
// o) Create Buffers in Global Memory to store data
// ================================================================
// ------------------------------------------------------------------
// Step 2: Create Buffers in Global Memory to store data
// o) buffer_in1 - stores source_in1
// o) buffer_in2 - stores source_in2
// o) buffer_ouput - stores Results
// ------------------------------------------------------------------
// .......................................................
// Allocate Global Memory for source_in1
// .......................................................
buffer_in1 = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, sizeof(cl_uint) * DATA_SIZE, source_in1, &err);
if (err != CL_SUCCESS) {
std::cout << "Return code for clCreateBuffer - in1" << err << std::endl;
}
// .......................................................
// Allocate Global Memory for source_in2
// .......................................................
buffer_in2 = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, sizeof(cl_uint) * DATA_SIZE, source_in2, &err);
if (err != CL_SUCCESS) {
std::cout << "Return code for clCreateBuffer -in2" << err << std::endl;
}
// .......................................................
// Allocate Global Memory for sourcce_hw_results
// .......................................................
buffer_output = clCreateBuffer(context, CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR, sizeof(cl_uint) * DATA_SIZE,source_hw_results , &err);
if (err != CL_SUCCESS) {
std::cout << "Return code for clCreateBuffer -ouput" << err << std::endl;
}
if (!(buffer_in1&&buffer_in2&&buffer_output)) {
printf("Error: Failed to allocate device memory!\n");
printf("Test failed\n");
return EXIT_FAILURE;
}
// ------------------------------------------------------
// Step 2: Copy Input data from Host to Global Memory
// ------------------------------------------------------
pt[0] = buffer_in1;
pt[1] = buffer_in2;
pt[2] = buffer_output;
err = clEnqueueMigrateMemObjects(commands,(cl_uint)2,pt, 0 ,0,NULL, NULL);
// ============================================================================
// Step 2: Set Kernel Arguments and Run the Application
// o) Set Kernel Arguments
// ----------------------------------------------------
// Kernel Argument Nb Description
// ----------------------------------------------------
// in1 (input) --> Input Vector1
// in2 (input) --> Input Vector2
// out (output) --> Output Vector
// size (input) --> Size of Vector in Integer
// o) Copy Input Data from Host to Global Memory
// o) Submit Kernels for Execution
// o) Copy Results from Global Memory to Host
// ============================================================================
err = 0;
cl_int size = DATA_SIZE;
err |= clSetKernelArg(kernel_vector_add, 0, sizeof(cl_mem), &buffer_in1);
err |= clSetKernelArg(kernel_vector_add, 1, sizeof(cl_mem), &buffer_in2);
err |= clSetKernelArg(kernel_vector_add, 2, sizeof(cl_mem), &buffer_output);
err |= clSetKernelArg(kernel_vector_add, 3, sizeof(cl_int), &size);
if (err != CL_SUCCESS) {
printf("Error: Failed to set kernel_vector_add arguments! %d\n", err);
printf("Test failed\n");
}
// ----------------------------------------
// Step 2: Submit Kernels for Execution
// ----------------------------------------
err = clEnqueueTask(commands, kernel_vector_add, 0, NULL, NULL);
if (err) {
printf("Error: Failed to execute kernel! %d\n", err);
printf("Test failed\n");
return EXIT_FAILURE;
}
err = 0;
// --------------------------------------------------
// Step 2: Copy Results from Global Memory to Host
// --------------------------------------------------
// err |= clEnqueueReadBuffer( commands,buffer_output, CL_TRUE, 0, sizeof(cl_uint) * DATA_SIZE, source_hw_results, 0, NULL, NULL );
err |= clEnqueueMigrateMemObjects(commands,(cl_uint)1,&pt[2], CL_MIGRATE_MEM_OBJECT_HOST,0,NULL, NULL);
if (err != CL_SUCCESS) {
printf("Error: Failed to write to source array: %d!\n", err);
printf("Test failed\n");
return EXIT_FAILURE;
}
clFinish(commands);
// ============================================================================
// Step 2: Processing Output Results
// o) Check correctness of the output results
// ============================================================================
for (cl_uint i = 0; i < DATA_SIZE; i++) {
if ((source_hw_results[i] != source_sw_results[i])) {
printf("\n Expected %d",source_hw_results[i] );
printf("----- Got %d\n",source_sw_results[i]);
printf(" Error: Reuslt mismatch" );
check_status = 1;
break;
}
}
// ============================================================================
// Step 3: Release Allocated Resources
// ============================================================================
clReleaseMemObject(buffer_in1);
clReleaseMemObject(buffer_in2);
clReleaseMemObject(buffer_output);
free(source_in1);
free(source_in2);
free(source_sw_results);
free(source_hw_results);
clReleaseProgram(program);
clReleaseKernel(kernel_vector_add);
clReleaseCommandQueue(commands);
clReleaseContext(context);
if (check_status) {
printf("INFO: Test failed\n");
return EXIT_FAILURE;
} else {
printf("INFO: Test completed successfully.\n");
return EXIT_SUCCESS;
}
}