libmcl-sys 0.1.2

This system crate provides Rust language bindings to the Minos Compute Library (MCL)
Documentation
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#include <fcntl.h>
#include <getopt.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include "utils.h"
#include <minos.h>

// #define VERBOSE

static inline int test_results(float *z, size_t n) {
    int i;

    for (i = 0; i < n; i++)
        if (z[i] != 2.0f) {
            printf("Element %d: %f != %f!\n", i, z[i], 2.0f);
            return 1;
        }

    return 0;
}

int saxpy_seq(float a, float *x, float *y, float *z, size_t size) {
    int i;

    for (i = 0; i < size; i++)
        z[i] = a * x[i] + y[i];

    return 0;
}

#ifdef __TEST_OCL
int test_ocl(float a, float *x, float *y, float *z, size_t size) {
    cl_device_id *device;
    struct timespec start, end;
    char *src_code = NULL;
    size_t src_size = 0;
    size_t num_kernels = 0;
    cl_int ret = 0;
    cl_context context;
    cl_command_queue queue;
    cl_mem x_o, y_o, z_o;
    cl_program program = NULL;
    cl_kernel kernel = NULL;
    size_t global_item_size = size;
    size_t local_item_size = 4;
    int i;
    size_t klen;
    char *knames;
    char src_path[1024];

    strcpy(src_path, XSTR(_MCL_TEST_PATH));
    strcat(src_path, "/saxpy.cl");
    printf("OpenCL Test...");

#ifdef VERBOSE
    printf("\nx = [ ");
    for (i = 0; i < size; i++)
        printf("%f ", x[i]);
    printf("]\n");

    printf("y = [ ");
    for (i = 0; i < size; i++)
        printf("%f ", y[i]);
    printf("]\n");
#endif

    if (mcl_load(src_path, &src_code)) {
        printf("Error loading OpenCL kernel! Aborting.\n");
        goto err;
    }

#ifdef VERBOSE
    printf("\n\n %s \n\n", src_code);
#endif

    if (ocl_setup(&device, &context, &queue, flags, 0))
        goto err_src;

    clock_gettime(CLOCK_MONOTONIC, &start);
    for (i = 0; i < rep; i++) {
        x_o = clCreateBuffer(context, CL_MEM_READ_ONLY, size * sizeof(float), NULL, &ret);
        if (ret != CL_SUCCESS) {
            printf("Error creating memory object for vector x! Aborting. (%d)\n", ret);
            goto err_setup;
        }

        y_o = clCreateBuffer(context, CL_MEM_READ_ONLY, size * sizeof(float), NULL, &ret);
        if (ret != CL_SUCCESS) {
            printf("Error creating memory object for vector y! Aborting. (%d)\n", ret);
            goto err_x;
        }

        z_o = clCreateBuffer(context, CL_MEM_WRITE_ONLY, size * sizeof(float), NULL, &ret);
        if (ret != CL_SUCCESS) {
            printf("Error creating memory object for vector z! Aborting. (%d)\n", ret);
            goto err_y;
        }

        ret = clEnqueueWriteBuffer(queue, x_o, CL_TRUE, 0, size * sizeof(float), x, 0, NULL, NULL);
        if (ret != CL_SUCCESS) {
            printf("Error copying data to CL device buffer for vector x! Aborting. (%d)\n",
                   ret);
            goto err_z;
        }

        ret = clEnqueueWriteBuffer(queue, y_o, CL_TRUE, 0, size * sizeof(float), y, 0, NULL, NULL);
        if (ret != CL_SUCCESS) {
            printf("Error copying data to CL device buffer for vector y! Aborting. (%d)\n",
                   ret);
            goto err_z;
        }
        clFlush(queue);

        src_size = strlen(src_code);
        program = clCreateProgramWithSource(context, 1, (const char **)&src_code,
                                            (const size_t *)&src_size, &ret);
        if (ret != CL_SUCCESS) {
            printf("Error creating program! Aborting. (%d)\n", ret);
            goto err_z;
        }

        ret = clBuildProgram(program, 1, &(device[0]), NULL, NULL, NULL);
        if (ret != CL_SUCCESS) {
            printf("Error building program! Aborting. (%d)\n", ret);
            goto err_program;
        }

        ret = clGetProgramInfo(program, CL_PROGRAM_NUM_KERNELS, sizeof(size_t), &num_kernels, NULL);
        if (ret != CL_SUCCESS) {
            printf("Error quering number of kernels in program (%d)\n", ret);
            goto err_program;
        }
        printf("Found %lu kernels in program\n", num_kernels);

        ret = clGetProgramInfo(program, CL_PROGRAM_KERNEL_NAMES, 0, NULL, &klen);
        if (ret != CL_SUCCESS) {
            printf("Error quering size of program names (%d)\n", ret);
            goto err_program;
        }

        knames = (char *)malloc(klen);
        if (!knames) {
            printf("Error allocating memory\n");
            goto err_program;
        }

        ret = clGetProgramInfo(program, CL_PROGRAM_KERNEL_NAMES, klen, knames, NULL);
        if (ret != CL_SUCCESS) {
            printf("Error quering kernel names (%d)\n", ret);
            goto err_program;
        }

        printf("Kernels in program %s (size %lu)\n", knames, klen);

        kernel = clCreateKernel(program, "SAXPY", &ret);
        if (ret != CL_SUCCESS) {
            printf("Error creating kernel! Aborting. (%d)\n", ret);
            goto err_program;
        }

        ret = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&x_o);
        ret |= clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&y_o);
        ret |= clSetKernelArg(kernel, 2, sizeof(float), (void *)&a);
        ret |= clSetKernelArg(kernel, 3, sizeof(cl_mem), (void *)&z_o);
        if (ret != CL_SUCCESS) {
            printf("Error setting kernel parameters! Aborting. (%d)\n", ret);
            goto err_kernel;
        }

        ret = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global_item_size, &local_item_size,
                                     0, NULL, NULL);
        if (ret != CL_SUCCESS) {
            printf("Error enqueuing kernel! Aborting. (%d)\n", ret);
            goto err_kernel;
        }

        ret = clEnqueueReadBuffer(queue, z_o, CL_TRUE, 0, size * sizeof(float), z, 0, NULL, NULL);
        if (ret != CL_SUCCESS) {
            printf("Error copying data back from device! (%d)\n", ret);
            goto err_kernel;
        }
        clFlush(queue);
    }
    clock_gettime(CLOCK_MONOTONIC, &end);

    printf("Done.\n  Test time: %f seconds\n", ((float)tdiff(end, start)) / BILLION);

    clFlush(queue);
    clFinish(queue);

    clReleaseKernel(kernel);
    clReleaseProgram(program);
    clReleaseMemObject(z_o);
    clReleaseMemObject(y_o);
    clReleaseMemObject(x_o);

    clReleaseCommandQueue(queue);
    clReleaseContext(context);

    free(src_code);

    return 0;

err_kernel:
    clReleaseKernel(kernel);
err_program:
    clReleaseProgram(program);
err_z:
    clReleaseMemObject(z_o);
err_y:
    clReleaseMemObject(y_o);
err_x:
    clReleaseMemObject(x_o);
err_setup:
    clReleaseCommandQueue(queue);
    clReleaseContext(context);
err_src:
    free(src_code);
err:
    return -1;
}
#endif

#ifdef __TEST_MCL
int test_mcl(float a, float *x, float *y, float *z, size_t size) {
    struct timespec start, end;
    mcl_handle **hdl = NULL;
    uint64_t pes[MCL_DEV_DIMS] = {size, 1, 1};
    uint64_t i;
    unsigned int errs = 0;
    char src_path[1024];

    strcpy(src_path, XSTR(_MCL_TEST_PATH));
    strcat(src_path, "/saxpy.cl");

    printf("MCL Test...");

#ifdef VERBOSE
    printf("\nx = [ ");
    for (i = 0; i < size; i++)
        printf("%f ", x[i]);
    printf("]\n");

    printf("y = [ ");
    for (i = 0; i < size; i++)
        printf("%f ", y[i]);
    printf("]\n");
#endif
    hdl = (mcl_handle **)malloc(sizeof(mcl_handle *) * rep);
    if (!hdl) {
        printf("Error allocating memmory for MCL hanlders. Aborting.\n");
        goto err;
    }

    if (mcl_prg_load(src_path, "", MCL_PRG_SRC)) {
        printf("Error loading program. Aborting.\n");
        goto err;
    }

    clock_gettime(CLOCK_MONOTONIC, &start);
    for (i = 0; i < rep; i++) {
        hdl[i] = mcl_task_create();
        if (!hdl[i]) {
            printf("Error creating MCL task. Aborting.");
            goto err_hdl;
        }

        if (mcl_task_set_kernel(hdl[i], "SAXPY", 4)) {
            printf("Error setting %s kernel. Aborting.", "SAXPY");
            goto err_hdl;
        }

        if (mcl_task_set_arg(hdl[i], 0, (void *)x, size * sizeof(float),
                             MCL_ARG_INPUT | MCL_ARG_BUFFER)) {
            printf("Error setting up task input. Aborting.");
            goto err_hdl;
        }

        if (mcl_task_set_arg(hdl[i], 1, (void *)y, size * sizeof(float),
                             MCL_ARG_INPUT | MCL_ARG_BUFFER)) {
            printf("Error setting up task input. Aborting.");
            goto err_hdl;
        }

        if (mcl_task_set_arg(hdl[i], 2, (void *)&a, sizeof(float),
                             MCL_ARG_INPUT | MCL_ARG_SCALAR)) {
            printf("Error setting up task input. Aborting.");
            goto err_hdl;
        }

        if (mcl_task_set_arg(hdl[i], 3, (void *)z, size * sizeof(float),
                             MCL_ARG_OUTPUT | MCL_ARG_BUFFER)) {
            printf("Error setting up task output. Aborting.");
            goto err_hdl;
        }

        if (mcl_exec(hdl[i], pes, NULL, flags)) {
            printf("Error submitting task! Aborting.");
            goto err_hdl;
        }

        if (synct)
            if (mcl_wait(hdl[i])) {
                printf("Request timed out!\n");
                goto err_hdl;
            }
    }
    clock_gettime(CLOCK_MONOTONIC, &end);

    if (!synct)
        if (mcl_wait_all()) {
            printf("Error waiting for requests to complete!\n");
            goto err_hdl;
        }

    for (i = 0; i < rep; i++)
        if (hdl[i]->ret == MCL_RET_ERROR) {
            printf("Error executing task %" PRIu64 "!\n", i);
            errs++;
        }
    if (errs)
        printf("Detected %u errors!\n", errs);
    else
        printf("Done.\n  Test time: %f seconds\n", ((float)tdiff(end, start)) / BILLION);

    for (i = 0; i < rep; i++)
        mcl_hdl_free(hdl[i]);

    free(hdl);

    return 0;

err_hdl:
    free(hdl);
err:
    return -1;
}
#endif

int main(int argc, char **argv) {
    float *x, *y, *z, a, *z_test;
    int i, ret = -1;
    struct timespec start, end;

    mcl_banner("SAXPY Test");

    parse_global_opts(argc, argv);

    switch (type) {
#ifdef __TEST_MCL
    case 0: {
        flags = MCL_TASK_CPU;
        break;
    }
    case 1: {
        flags = MCL_TASK_GPU;
        break;
    }
    case 2: {
        flags = MCL_TASK_ANY;
        break;
    }
#elif __TEST_OCL
    case 0: {
        flags = CL_DEVICE_TYPE_CPU;
        break;
    }
    case 1: {
        flags = CL_DEVICE_TYPE_GPU;
        break;
    }
#endif
    default: {
        printf("Unrecognized resource type (%" PRIu64 "). Aborting.\n", type);
        return -1;
    }
    }

#ifdef __TEST_MCL
    mcl_init(workers, 0x0);
#endif

    x = (float *)malloc(size * sizeof(float));
    y = (float *)malloc(size * sizeof(float));
    z = (float *)malloc(size * sizeof(float));
    z_test = (float *)malloc(size * sizeof(float));

    if (!x || !y || !z || !z_test) {
        printf("Error allocating vectors. Aborting.");
        goto err;
    }

    a = 1.0f;
    for (i = 0; i < size; i++)
        x[i] = 1.0f;
    for (i = 0; i < size; i++)
        y[i] = 1.0f;

#ifdef VERBOSE
    printf("a = %f\n", a);

    printf("x = [ ");
    for (i = 0; i < size; i++)
        printf("%f ", x[i]);
    printf("]\n");

    printf("y = [ ");
    for (i = 0; i < size; i++)
        printf("%f ", y[i]);
    printf("]\n");
#endif

    printf("Sequential Test...");
    clock_gettime(CLOCK_MONOTONIC, &start);
    saxpy_seq(a, x, y, z_test, size);
    clock_gettime(CLOCK_MONOTONIC, &end);

    printf("Done.\n  Test time: %f seconds\n", ((float)tdiff(end, start)) / BILLION);

#ifdef VERBOSE
    printf("z = [ ");
    for (i = 0; i < size; i++)
        printf("%f ", z_test[i]);
    printf("]\n");
#endif

    printf("\n");

#ifdef __TEST_OCL
    test_ocl(a, x, y, z, size);
#else
    test_mcl(a, x, y, z, size);
#endif

#ifdef VERBOSE
    printf("z = [ ");
    for (i = 0; i < size; i++)
        printf("%f ", z[i]);
    printf("]\n");
#endif

    ret = test_results(z, size);

#ifdef __TEST_MCL
    mcl_finit();
#endif
    mcl_verify(ret);

    free(z_test);
    free(z);
    free(y);
    free(x);

err:
    return ret;
}