99#include < time.h>
1010#include < unistd.h>
1111
12- int TS = 4 ;
12+ #include " common.h"
13+
1314int M = 0 , N = 0 , K = 0 ;
1415
1516static 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) {
144143int 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}
0 commit comments