Skip to content

Commit 0777e5d

Browse files
committed
clean code
1 parent 72f2738 commit 0777e5d

File tree

7 files changed

+78
-273
lines changed

7 files changed

+78
-273
lines changed

tests/opencl/Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ all:
2222
$(MAKE) -C sgemm2
2323
$(MAKE) -C sgemm3
2424
$(MAKE) -C psum
25+
$(MAKE) -C kernel1
26+
$(MAKE) -C kernel2
27+
$(MAKE) -C kernel3
28+
$(MAKE) -C kernel4
2529

2630
run-simx:
2731
$(MAKE) -C vecadd run-simx
@@ -43,6 +47,10 @@ run-simx:
4347
$(MAKE) -C sgemm2 run-simx
4448
$(MAKE) -C sgemm3 run-simx
4549
$(MAKE) -C psum run-simx
50+
$(MAKE) -C kernel1 run-simx
51+
$(MAKE) -C kernel2 run-simx
52+
$(MAKE) -C kernel3 run-simx
53+
$(MAKE) -C kernel4 run-simx
4654

4755
run-rtlsim:
4856
$(MAKE) -C vecadd run-rtlsim
@@ -64,6 +72,10 @@ run-rtlsim:
6472
$(MAKE) -C sgemm2 run-rtlsim
6573
$(MAKE) -C sgemm3 run-rtlsim
6674
$(MAKE) -C psum run-rtlsim
75+
$(MAKE) -C kernel1 run-rtlsimy
76+
$(MAKE) -C kernel2 run-rtlsimy
77+
$(MAKE) -C kernel3 run-rtlsimy
78+
$(MAKE) -C kernel4 run-rtlsimy
6779

6880
run-opae:
6981
$(MAKE) -C vecadd run-opae
@@ -85,6 +97,10 @@ run-opae:
8597
$(MAKE) -C sgemm2 run-opae
8698
$(MAKE) -C sgemm3 run-opae
8799
$(MAKE) -C psum run-opae
100+
$(MAKE) -C kernel1 run-opae
101+
$(MAKE) -C kernel2 run-opae
102+
$(MAKE) -C kernel3 run-opae
103+
$(MAKE) -C kernel4 run-opae
88104

89105
clean:
90106
$(MAKE) -C vecadd clean
@@ -107,3 +123,7 @@ clean:
107123
$(MAKE) -C sgemm2 clean
108124
$(MAKE) -C sgemm3 clean
109125
$(MAKE) -C psum clean
126+
$(MAKE) -C kernel1 clean
127+
$(MAKE) -C kernel2 clean
128+
$(MAKE) -C kernel3 clean
129+
$(MAKE) -C kernel4 clean

tests/opencl/kernel1/main.cc

Lines changed: 15 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
#include <time.h>
1010
#include <unistd.h>
1111

12-
int TS = 4;
12+
#include "common.h"
13+
1314
int M = 0, N = 0, K = 0;
1415

1516
static void show_usage() {
@@ -102,8 +103,6 @@ static void cleanup() {
102103
clReleaseContext(context);
103104
if (device_id)
104105
clReleaseDevice(device_id);
105-
// if (platform_id)
106-
// clReleasePlatform(platform_id);
107106
if (kernel_bin)
108107
free(kernel_bin);
109108
}
@@ -144,69 +143,10 @@ static void parse_args(int argc, char **argv) {
144143
int main(int argc, char **argv) {
145144
// parse command arguments
146145
parse_args(argc, argv);
147-
//printf("%d", VX_CAPS_NUM_CORES);
148-
149-
/*
150-
// find device and platform
151-
cl_uint platform_count = 0;
152-
CL_CHECK(clGetPlatformIDs(0, NULL, &platform_count));
153-
cl_platform_id *platforms =
154-
(cl_platform_id *)malloc(platform_count * sizeof(cl_platform_id));
155-
if (platforms == NULL) {
156-
printf("Not enough memory");
157-
cleanup();
158-
exit(-1);
159-
}
160-
CL_CHECK(clGetPlatformIDs(platform_count, platforms, NULL));
161-
162-
bool gpu_device_selected = false;
163-
bool any_device_selected = false;
164-
for (int platform_index = 0; platform_index < (int)platform_count;
165-
++platform_index) {
166-
cl_platform_id platform = platforms[platform_index];
167-
cl_uint devices_count = 0;
168-
169-
CL_CHECK(
170-
clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 0, NULL, &devices_count));
171-
cl_device_id *devices =
172-
(cl_device_id *)malloc(sizeof(cl_device_id) * devices_count);
173-
if (devices == NULL) {
174-
printf("Not enough memory");
175-
cleanup();
176-
return -1;
177-
}
178-
CL_CHECK(clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, devices_count,
179-
devices, NULL));
180-
for (int device_index = 0; device_index < (int)devices_count; ++device_index) {
181-
cl_device_id device = devices[device_index];
182-
cl_device_type device_type;
183-
CL_CHECK(clGetDeviceInfo(device, CL_DEVICE_TYPE, sizeof(device_type),
184-
&device_type, NULL));
185-
186-
if (device_type & CL_DEVICE_TYPE_GPU) {
187-
gpu_device_selected = true;
188-
any_device_selected = true;
189-
platform_id = platform;
190-
device_id = device;
191-
break;
192-
}
193-
if (device_type & CL_DEVICE_TYPE_CPU) {
194-
any_device_selected = true;
195-
platform_id = platform;
196-
device_id = device;
197-
}
198-
}
199-
if (gpu_device_selected)
200-
break;
201-
}
202-
if (!any_device_selected) {
203-
printf("No device found");
204-
cleanup();
205-
return -1;
206-
}*/
207146

208147
CL_CHECK(clGetPlatformIDs(1, &platform_id, NULL));
209148
CL_CHECK(clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_DEFAULT, 1, &device_id, NULL));
149+
210150
// create context
211151
cl_context_properties context_properties[]{
212152
CL_CONTEXT_PLATFORM, cl_context_properties(platform_id), 0};
@@ -216,10 +156,11 @@ int main(int argc, char **argv) {
216156
char device_string[1024];
217157
clGetDeviceInfo(device_id, CL_DEVICE_NAME, sizeof(device_string), &device_string, NULL);
218158
printf("Using device: %s\n", device_string);
159+
219160
// create command queue
220161
command_queue = CL_CHECK2(clCreateCommandQueue(context, device_id, 0, &_err));
221162

222-
// generate data
163+
// generate input data
223164
float *A, *B, *C;
224165
A = (float *)(malloc(M * K * sizeof(float)));
225166
B = (float *)(malloc(N * K * sizeof(float)));
@@ -229,10 +170,11 @@ int main(int argc, char **argv) {
229170
cleanup();
230171
return -1;
231172
}
232-
for (int i = 0; i < M * N; i++) {
233-
A[i] = 1;
234-
B[i] = 1;
235-
}
173+
srand(time(NULL));
174+
for (int i = 0; i < M * K; i++)
175+
A[i] = (int)((float)rand() / (float)RAND_MAX);
176+
for (int i = 0; i < N * K; i++)
177+
B[i] = (int)((float)rand() / (float)RAND_MAX);
236178

237179
// create buffers
238180
a_memobj =
@@ -332,5 +274,10 @@ int main(int argc, char **argv) {
332274

333275
// free resureses
334276
cleanup();
277+
free(A);
278+
free(B);
279+
free(C);
280+
free(log);
281+
free(C_cpu);
335282
return errors;
336283
}

tests/opencl/kernel2/main.cc

Lines changed: 13 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ static void cleanup() {
103103
clReleaseContext(context);
104104
if (device_id)
105105
clReleaseDevice(device_id);
106-
// if (platform_id)
107-
// clReleasePlatform(platform_id);
108106
if (kernel_bin)
109107
free(kernel_bin);
110108
}
@@ -145,69 +143,10 @@ static void parse_args(int argc, char **argv) {
145143
int main(int argc, char **argv) {
146144
// parse command arguments
147145
parse_args(argc, argv);
148-
//printf("%d", VX_CAPS_NUM_CORES);
149-
150-
/*
151-
// find device and platform
152-
cl_uint platform_count = 0;
153-
CL_CHECK(clGetPlatformIDs(0, NULL, &platform_count));
154-
cl_platform_id *platforms =
155-
(cl_platform_id *)malloc(platform_count * sizeof(cl_platform_id));
156-
if (platforms == NULL) {
157-
printf("Not enough memory");
158-
cleanup();
159-
exit(-1);
160-
}
161-
CL_CHECK(clGetPlatformIDs(platform_count, platforms, NULL));
162-
163-
bool gpu_device_selected = false;
164-
bool any_device_selected = false;
165-
for (int platform_index = 0; platform_index < (int)platform_count;
166-
++platform_index) {
167-
cl_platform_id platform = platforms[platform_index];
168-
cl_uint devices_count = 0;
169-
170-
CL_CHECK(
171-
clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 0, NULL, &devices_count));
172-
cl_device_id *devices =
173-
(cl_device_id *)malloc(sizeof(cl_device_id) * devices_count);
174-
if (devices == NULL) {
175-
printf("Not enough memory");
176-
cleanup();
177-
return -1;
178-
}
179-
CL_CHECK(clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, devices_count,
180-
devices, NULL));
181-
for (int device_index = 0; device_index < (int)devices_count; ++device_index) {
182-
cl_device_id device = devices[device_index];
183-
cl_device_type device_type;
184-
CL_CHECK(clGetDeviceInfo(device, CL_DEVICE_TYPE, sizeof(device_type),
185-
&device_type, NULL));
186-
187-
if (device_type & CL_DEVICE_TYPE_GPU) {
188-
gpu_device_selected = true;
189-
any_device_selected = true;
190-
platform_id = platform;
191-
device_id = device;
192-
break;
193-
}
194-
if (device_type & CL_DEVICE_TYPE_CPU) {
195-
any_device_selected = true;
196-
platform_id = platform;
197-
device_id = device;
198-
}
199-
}
200-
if (gpu_device_selected)
201-
break;
202-
}
203-
if (!any_device_selected) {
204-
printf("No device found");
205-
cleanup();
206-
return -1;
207-
}*/
208146

209147
CL_CHECK(clGetPlatformIDs(1, &platform_id, NULL));
210148
CL_CHECK(clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_DEFAULT, 1, &device_id, NULL));
149+
211150
// create context
212151
cl_context_properties context_properties[]{
213152
CL_CONTEXT_PLATFORM, cl_context_properties(platform_id), 0};
@@ -217,10 +156,11 @@ int main(int argc, char **argv) {
217156
char device_string[1024];
218157
clGetDeviceInfo(device_id, CL_DEVICE_NAME, sizeof(device_string), &device_string, NULL);
219158
printf("Using device: %s\n", device_string);
159+
220160
// create command queue
221161
command_queue = CL_CHECK2(clCreateCommandQueue(context, device_id, 0, &_err));
222162

223-
// generate data
163+
// generate input data
224164
float *A, *B, *C;
225165
A = (float *)(malloc(M * K * sizeof(float)));
226166
B = (float *)(malloc(N * K * sizeof(float)));
@@ -230,10 +170,11 @@ int main(int argc, char **argv) {
230170
cleanup();
231171
return -1;
232172
}
233-
for (int i = 0; i < M * N; i++) {
234-
A[i] = 1;
235-
B[i] = 1;
236-
}
173+
srand(time(NULL));
174+
for (int i = 0; i < M * K; i++)
175+
A[i] = (int)((float)rand() / (float)RAND_MAX);
176+
for (int i = 0; i < N * K; i++)
177+
B[i] = (int)((float)rand() / (float)RAND_MAX);
237178

238179
// create buffers
239180
a_memobj =
@@ -333,5 +274,10 @@ int main(int argc, char **argv) {
333274

334275
// free resureses
335276
cleanup();
277+
free(A);
278+
free(B);
279+
free(C);
280+
free(log);
281+
free(C_cpu);
336282
return errors;
337283
}

tests/opencl/kernel3/common.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#ifndef COMMON_H
22
#define COMMON_H
33

4-
#define TS 8
4+
#define TS 4
55
#define WPT 4
6-
#define RTS (TS/WPT)
6+
#define RTS (TS/WPT)
77

8-
#endif // COMMON_H
8+
#endif // COMMON_H

0 commit comments

Comments
 (0)