-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
297 lines (246 loc) · 9.49 KB
/
main.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
// reference: https://www.fixstars.com/en/opencl/book/OpenCLProgrammingBook/first-opencl-program/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <CL/cl.h>
#ifdef AOCL
#include "CL/opencl.h"
#include "AOCLUtils/aocl_utils.h"
using namespace aocl_utils;
void cleanup();
#endif
// #define MEM_SIZE (128)
#define MAX_SOURCE_SIZE (0x100000)
#define DEVICE_NAME_LEN 128 //MATRIX
static char dev_name[DEVICE_NAME_LEN]; // MATRIX
//MATRIX
static float A[8] = {
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f };
//MATRIX
static float B[24] = {
2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f,
2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f,
2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f,
2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f };
int main()
{
printf("MatrixMulti.\n");
// MATRIX
cl_uint platformCount;
cl_platform_id* platforms; //expecting an array of platforms from devcloud
cl_device_id device_id = NULL;
cl_uint ret_num_devices;
cl_int ret;
cl_context context = NULL;
cl_command_queue command_queue = NULL;
cl_program program = NULL;
cl_kernel kernel = NULL;
// from HELLO
// cl_mem memobj = NULL; // likely unecessary
cl_platform_id platform_id = NULL;
cl_uint ret_num_platforms;
// char string[MEM_SIZE];
FILE* fp;
char fileName[] = "mykernel.cl"; // used to have ./
char* source_str;
size_t source_size;
// MATRIX
printf("Creating Height / Width ints.\n");
int wA = 4;
int hA = 2;
int wB = 6;
int hB = 4;
int wC = wB;
int hC = hA;
printf("Created Height / Width ints.\n\n");
// end MATRIX
// MATRIX
#ifdef AOCL // Altera FPGA
// get all platforms
clGetPlatformIDs(0, NULL, &platformCount);
platforms = (cl_platform_id*)malloc(sizeof(cl_platform_id) * platformCount);
// Get the OpenCL platform.
platforms[0] = findPlatform("Intel(R) FPGA");
if (platforms[0] == NULL) {
printf("ERROR: Unable to find Intel(R) FPGA OpenCL platform.\n");
return false;
}
// Query the available OpenCL device.
getDevices(platforms[0], CL_DEVICE_TYPE_ALL, &ret_num_devices);
printf("Platform: %s\n", getPlatformName(platforms[0]).c_str());
printf("Using one out of %d device(s)\n", ret_num_devices);
ret = clGetDeviceIDs(platforms[0], CL_DEVICE_TYPE_DEFAULT, 1, &device_id, &ret_num_devices);
printf("device name= %s\n", getDeviceName(device_id).c_str());
#else // for local machine use
// Get Platform and Device Info HELLO
ret = clGetPlatformIDs(1, &platform_id, &ret_num_platforms);
ret = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_DEFAULT, 1, &device_id, &ret_num_devices);
#endif // end MATRIX
// MATRIX
printf("Creating OpenCL context.\n");
// Create OpenCL context
context = clCreateContext(NULL, 1, &device_id, NULL, NULL, &ret);
printf("Created OpenCL context.\n\n");
//system("pause");
printf("Create Command queue.\n");
// Create Command Queue
command_queue = clCreateCommandQueue(context, device_id, 0, &ret);
printf("Created Command queue.\n\n");
//system("pause");
// end MATRIX
// MATRIX
#ifdef AOCL /* on FPGA we need to create kernel from binary */
/* Create Kernel Program from the binary */
std::string binary_file = getBoardBinaryFile("mykernel", device_id);
printf("Using AOCX: %s\n", binary_file.c_str());
program = createProgramFromBinary(context, binary_file.c_str(), &device_id, 1);
#else
// end MATRIX
// Load the source code containing kernel MATRIX (APPLE)
fp = fopen(fileName, "r");
if (!fp) {
fprintf(stderr, "Failed to load source code containing kernel.\n");
exit(1);
}
else {
printf("loaded source code contanining kernel.\n\n");
}
source_str = (char*)malloc(MAX_SOURCE_SIZE);
source_size = fread(source_str, 1, MAX_SOURCE_SIZE, fp);
fclose(fp);
// Create Kernel Program from the source
program = clCreateProgramWithSource(context, 1, (const char**)&source_str,
(const size_t*)&source_size, &ret);
if (ret != CL_SUCCESS) {
printf("Failed to create program from source.\n");
exit(1);
} // end MATRIX (APPLE)
else {
printf("Created program from source.\n\n");
}
#endif
/*
// MATRIX // exists above on 113
#ifdef AOCL // on FPGA we need to create kernel from binary
// Create Kernel Program from the binary
std::string binary_file = getBoardBinaryFile("mykernel", device_id);
printf("Using AOCX: %s\n", binary_file.c_str());
program = createProgramFromBinary(context, binary_file.c_str(), &device_id, 1);
#endif
//end MATRIX
*/
// Build Kernel Program in hello & MATRIX
printf("build kernel program.\n");
ret = clBuildProgram(program, 1, &device_id, NULL, NULL, NULL);
if (ret != CL_SUCCESS) {
printf("Failed to build kernel program.\n");
// exit(1);
}
else {
printf("built kernel program.\n");
}
//system("pause");
// Create OpenCL Kernel in hellow & MATRIX
kernel = clCreateKernel(program, "simpleMultiply", &ret);
if (ret != CL_SUCCESS) {
printf("Failed to create kernel.\n");
//exit(1);
}
else {
printf("created OpenCL kernel.\n");
}
//system("pause");
// MATRIX cretaes an array of size height by width of c with floats and sets them to zeros in local memory
float* C = (float*)calloc(hC * wC, sizeof(float));
for (int i = 0; i < wC * hC; i++) {
printf("%f ", C[i]); // prints the array of float zeroes
}
printf("\n");
// end MATRIX
// MATRIX
// PREVIOUSLY // Create Memory Buffer memobj = clCreateBuffer(context, CL_MEM_READ_WRITE, MEM_SIZE * sizeof(char), NULL, &ret);
// We assume A, B, C are float arrays which have been declared and initialized
// allocate space for MATRIX A on the device
cl_mem bufferA = clCreateBuffer(context, CL_MEM_READ_ONLY,
wA * hA * sizeof(float), NULL, &ret);
// copy MATRIX A to the device
clEnqueueWriteBuffer(command_queue, bufferA, CL_TRUE, 0,
wA * hA * sizeof(float), (void*)A, 0, NULL, NULL);
// allocate space for MATRIX B on the device
cl_mem bufferB = clCreateBuffer(context, CL_MEM_READ_ONLY,
wB * hB * sizeof(float), NULL, &ret);
// copy MATRIX B to the device
clEnqueueWriteBuffer(command_queue, bufferB, CL_TRUE, 0,
wB * hB * sizeof(float), (void*)B, 0, NULL, NULL);
// allocate space for MATRIX C on the device
cl_mem bufferC = clCreateBuffer(context, CL_MEM_WRITE_ONLY,
wC * hC * sizeof(float), NULL, &ret);
// end MATRIX
// Set the kernel arguments JOHN
clSetKernelArg(kernel, 0, sizeof(cl_mem), (void*)&bufferC);
clSetKernelArg(kernel, 1, sizeof(cl_int), (void*)&wA);
clSetKernelArg(kernel, 2, sizeof(cl_int), (void*)&hA);
clSetKernelArg(kernel, 3, sizeof(cl_int), (void*)&wB);
clSetKernelArg(kernel, 4, sizeof(cl_int), (void*)&hB);
clSetKernelArg(kernel, 5, sizeof(cl_mem), (void*)&bufferA);
clSetKernelArg(kernel, 6, sizeof(cl_mem), (void*)&bufferB); // end john
// Create Kernel Program from the source
// program = clCreateProgramWithSource(context, 1, (const char**)&source_str,
// (const size_t*)&source_size, &ret);
// Set OpenCL Kernel Parameters HELLO
// ret = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void*)&memobj);
// Execute OpenCL Kernel HELLO
// ret = clEnqueueTask(command_queue, kernel, 0, NULL, NULL);
// Execute the kernel MATRIX
size_t globalws[2] = { wC, hC };
size_t localws[2] = { 2, 2 };
ret = clEnqueueNDRangeKernel(command_queue, kernel, 2, NULL,
globalws, localws, 0, NULL, NULL);
// it is important to check the return value.
// e.g. enqueueNDRangeKernel may fail when Work group size does not divide evenly into global work size
if (ret != CL_SUCCESS) {
printf("Failed to enqueueNDRangeKernel.\n");
//exit(1);
} // end MATRIX
else {
printf("executed kernel & enqueueNDRangeKernel.\n");
}
// system("pause");
// Copy the output data back to the host MATRIX
clEnqueueReadBuffer(command_queue, bufferC, CL_TRUE, 0, wC * hC * sizeof(float),
(void*)C, 0, NULL, NULL); // end MATRIX
// Copy results from the memory buffer HELLO
//ret = clEnqueueReadBuffer(command_queue, memobj, CL_TRUE, 0,
// MEM_SIZE * sizeof(char), string, 0, NULL, NULL);
// Verify result MATRIX
for (int i = 0; i < wC * hC; i++) {
printf("%f ", C[i]);
}
printf("\n"); // end MATRIX
// Display Result
//puts(string); // HELLO
system("pause"); // last chance to look
// free(source_str); // HELLO
// free resources
free(C);
// MATRIX // Finalization HELLO
clReleaseMemObject(bufferA);
clReleaseMemObject(bufferB);
clReleaseMemObject(bufferC); // ret = clReleaseMemObject(memobj);
clReleaseCommandQueue(command_queue); // ret = clReleaseCommandQueue(command_queue);
clReleaseKernel(kernel); // ret = clReleaseKernel(kernel);
clReleaseProgram(program); // ret = clReleaseProgram(program);
clReleaseContext(context); // ret = clReleaseContext(context);
// end MATRIX
// Finalization HELLO
// ret = clFlush(command_queue);
// ret = clFinish(command_queue);
return 0;
}
#ifdef AOCL
// Altera OpenCL needs this callback function implemented in main.c
// Free the resources allocated during initialization
void cleanup() {
}
#endif