summaryrefslogtreecommitdiff
path: root/sc/source/core/opencl/opencl_device_selection.h
blob: d6f775a49722f6755a02d00179f5eab02bf2f27c (plain)
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#ifndef SC_DEVICE_SELECTION_H
#define SC_DEVICE_SELECTION_H


#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "clcc/clew.h"

#define DS_DEVICE_NAME_LENGTH 256

enum ds_status
{
    DS_SUCCESS = 0
    ,DS_INVALID_PROFILE = 1000
    ,DS_MEMORY_ERROR
    , DS_INVALID_PERF_EVALUATOR_TYPE
    , DS_INVALID_PERF_EVALUATOR
    , DS_PERF_EVALUATOR_ERROR
    , DS_FILE_ERROR
    , DS_UNKNOWN_DEVICE_TYPE
    , DS_PROFILE_FILE_ERROR
    , DS_SCORE_SERIALIZER_ERROR
    , DS_SCORE_DESERIALIZER_ERROR
};

// device type
enum ds_device_type
{
    DS_DEVICE_NATIVE_CPU = 0
    ,DS_DEVICE_OPENCL_DEVICE
};


struct ds_device
{
    ds_device_type  type;
    cl_device_id    oclDeviceID;
    char*           oclDeviceName;
    char*           oclDriverVersion;
    void*           score;            // a pointer to the score data, the content/format is application defined
};

struct ds_profile
{
    unsigned int  numDevices;
    ds_device*    devices;
    const char*   version;
};

// deallocate memory used by score
typedef ds_status(* ds_score_release)(void* score);
inline ds_status releaseDSProfile(ds_profile* profile, ds_score_release sr)
{
    ds_status status = DS_SUCCESS;
    if (profile != NULL)
    {
        if (profile->devices != NULL && sr != NULL)
        {
            unsigned int i;
            for (i = 0; i < profile->numDevices; i++)
            {
                free(profile->devices[i].oclDeviceName);
                free(profile->devices[i].oclDriverVersion);
                status = sr(profile->devices[i].score);
                if (status != DS_SUCCESS) break;
            }
            free(profile->devices);
        }
        free(profile);
    }
    return status;
}


inline ds_status initDSProfile(ds_profile** p, const char* version)
{
    int numDevices;
    cl_uint numPlatforms;
    cl_platform_id* platforms = NULL;
    cl_device_id*   devices = NULL;
    ds_status status = DS_SUCCESS;
    ds_profile* profile = NULL;
    unsigned int next;
    unsigned int i;

    if (p == NULL) return DS_INVALID_PROFILE;

    profile = (ds_profile*)malloc(sizeof(ds_profile));
    if (profile == NULL) return DS_MEMORY_ERROR;

    memset(profile, 0, sizeof(ds_profile));

    clGetPlatformIDs(0, NULL, &numPlatforms);
    if (numPlatforms != 0)
    {
        platforms = (cl_platform_id*)malloc(numPlatforms * sizeof(cl_platform_id));
        if (platforms == NULL)
        {
            status = DS_MEMORY_ERROR;
            goto cleanup;
        }
        clGetPlatformIDs(numPlatforms, platforms, NULL);
    }

    numDevices = 0;
    for (i = 0; i < (unsigned int)numPlatforms; i++)
    {
        cl_uint num;
        clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 0, NULL, &num);
        numDevices += num;
    }
    if (numDevices != 0)
    {
        devices = (cl_device_id*)malloc(numDevices * sizeof(cl_device_id));
        if (devices == NULL)
        {
            status = DS_MEMORY_ERROR;
            goto cleanup;
        }
    }

    profile->numDevices = numDevices + 1;     // +1 to numDevices to include the native CPU
    profile->devices = (ds_device*)malloc(profile->numDevices * sizeof(ds_device));
    if (profile->devices == NULL)
    {
        profile->numDevices = 0;
        status = DS_MEMORY_ERROR;
        goto cleanup;
    }
    memset(profile->devices, 0, profile->numDevices * sizeof(ds_device));

    next = 0;
    for (i = 0; i < (unsigned int)numPlatforms; i++)
    {
        cl_uint num;
        unsigned j;
        clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, numDevices, devices, &num);
        for (j = 0; j < num; j++, next++)
        {
            char buffer[DS_DEVICE_NAME_LENGTH];
            size_t length;

            profile->devices[next].type = DS_DEVICE_OPENCL_DEVICE;
            profile->devices[next].oclDeviceID = devices[j];

            clGetDeviceInfo(profile->devices[next].oclDeviceID, CL_DEVICE_NAME
                            , DS_DEVICE_NAME_LENGTH, &buffer, NULL);
            length = strlen(buffer);
            profile->devices[next].oclDeviceName = (char*)malloc(length + 1);
            memcpy(profile->devices[next].oclDeviceName, buffer, length + 1);

            clGetDeviceInfo(profile->devices[next].oclDeviceID, CL_DRIVER_VERSION
                            , DS_DEVICE_NAME_LENGTH, &buffer, NULL);
            length = strlen(buffer);
            profile->devices[next].oclDriverVersion = (char*)malloc(length + 1);
            memcpy(profile->devices[next].oclDriverVersion, buffer, length + 1);
        }
    }
    profile->devices[next].type = DS_DEVICE_NATIVE_CPU;
    profile->version = version;

cleanup:
    if (platforms)  free(platforms);
    if (devices)    free(devices);
    if (status == DS_SUCCESS)
    {
        *p = profile;
    }
    else
    {
        if (profile)
        {
            if (profile->devices) free(profile->devices);
            free(profile);
        }
    }
    return status;
}

// Pointer to a function that calculates the score of a device (ex: device->score)
// update the data size of score. The encoding and the format of the score data
// is implementation defined. The function should return DS_SUCCESS if there's no error to be reported.
typedef ds_status(* ds_perf_evaluator)(ds_device* device, void* data);

typedef enum {
    DS_EVALUATE_ALL
    , DS_EVALUATE_NEW_ONLY
} ds_evaluation_type;

inline ds_status profileDevices(ds_profile* profile, const ds_evaluation_type type,
                                ds_perf_evaluator evaluator, void* evaluatorData, unsigned int* numUpdates)
{
    ds_status status = DS_SUCCESS;
    unsigned int i;
    unsigned int updates = 0;

    if (profile == NULL)
    {
        return DS_INVALID_PROFILE;
    }
    if (evaluator == NULL)
    {
        return DS_INVALID_PERF_EVALUATOR;
    }

    for (i = 0; i < profile->numDevices; i++)
    {
        ds_status evaluatorStatus;

        switch (type)
        {
            case DS_EVALUATE_NEW_ONLY:
                if (profile->devices[i].score != NULL) break;
                //  else fall through
            case DS_EVALUATE_ALL:
                evaluatorStatus = evaluator(profile->devices + i, evaluatorData);
                if (evaluatorStatus != DS_SUCCESS)
                {
                    status = evaluatorStatus;
                    return status;
                }
                updates++;
                break;
            default:
                return DS_INVALID_PERF_EVALUATOR_TYPE;
                break;
        };
    }
    if (numUpdates) *numUpdates = updates;
    return status;
}


#define DS_TAG_VERSION                      "<version>"
#define DS_TAG_VERSION_END                  "</version>"
#define DS_TAG_DEVICE                       "<device>"
#define DS_TAG_DEVICE_END                   "</device>"
#define DS_TAG_SCORE                        "<score>"
#define DS_TAG_SCORE_END                    "</score>"
#define DS_TAG_DEVICE_TYPE                  "<type>"
#define DS_TAG_DEVICE_TYPE_END              "</type>"
#define DS_TAG_DEVICE_NAME                  "<name>"
#define DS_TAG_DEVICE_NAME_END              "</name>"
#define DS_TAG_DEVICE_DRIVER_VERSION        "<driver>"
#define DS_TAG_DEVICE_DRIVER_VERSION_END    "</driver>"

#define DS_DEVICE_NATIVE_CPU_STRING  "native_cpu"



typedef ds_status(* ds_score_serializer)(ds_device* device, void** serializedScore, unsigned int* serializedScoreSize);
inline ds_status writeProfileToFile(ds_profile* profile, ds_score_serializer serializer, const char* file)
{
    ds_status status = DS_SUCCESS;
    FILE* profileFile = NULL;


    if (profile == NULL) return DS_INVALID_PROFILE;

    profileFile = fopen(file, "wb");
    if (profileFile == NULL)
    {
        status = DS_FILE_ERROR;
    }
    else
    {
        unsigned int i;

        // write version string
        fwrite(DS_TAG_VERSION, sizeof(char), strlen(DS_TAG_VERSION), profileFile);
        fwrite(profile->version, sizeof(char), strlen(profile->version), profileFile);
        fwrite(DS_TAG_VERSION_END, sizeof(char), strlen(DS_TAG_VERSION_END), profileFile);
        fwrite("\n", sizeof(char), 1, profileFile);

        for (i = 0; i < profile->numDevices && status == DS_SUCCESS; i++)
        {
            void* serializedScore;
            unsigned int serializedScoreSize;

            fwrite(DS_TAG_DEVICE, sizeof(char), strlen(DS_TAG_DEVICE), profileFile);

            fwrite(DS_TAG_DEVICE_TYPE, sizeof(char), strlen(DS_TAG_DEVICE_TYPE), profileFile);
            fwrite(&profile->devices[i].type, sizeof(ds_device_type), 1, profileFile);
            fwrite(DS_TAG_DEVICE_TYPE_END, sizeof(char), strlen(DS_TAG_DEVICE_TYPE_END), profileFile);

            switch (profile->devices[i].type)
            {
                case DS_DEVICE_NATIVE_CPU:
                {
                    // There's no need to emit a device name for the native CPU device.
                    /*
                    fwrite(DS_TAG_DEVICE_NAME, sizeof(char), strlen(DS_TAG_DEVICE_NAME), profileFile);
                    fwrite(DS_DEVICE_NATIVE_CPU_STRING,sizeof(char),strlen(DS_DEVICE_NATIVE_CPU_STRING), profileFile);
                    fwrite(DS_TAG_DEVICE_NAME_END, sizeof(char), strlen(DS_TAG_DEVICE_NAME_END), profileFile);
                    */
                }
                    break;
                case DS_DEVICE_OPENCL_DEVICE:
                {
                    fwrite(DS_TAG_DEVICE_NAME, sizeof(char), strlen(DS_TAG_DEVICE_NAME), profileFile);
                    fwrite(profile->devices[i].oclDeviceName, sizeof(char), strlen(profile->devices[i].oclDeviceName), profileFile);
                    fwrite(DS_TAG_DEVICE_NAME_END, sizeof(char), strlen(DS_TAG_DEVICE_NAME_END), profileFile);

                    fwrite(DS_TAG_DEVICE_DRIVER_VERSION, sizeof(char), strlen(DS_TAG_DEVICE_DRIVER_VERSION), profileFile);
                    fwrite(profile->devices[i].oclDriverVersion, sizeof(char), strlen(profile->devices[i].oclDriverVersion), profileFile);
                    fwrite(DS_TAG_DEVICE_DRIVER_VERSION_END, sizeof(char), strlen(DS_TAG_DEVICE_DRIVER_VERSION_END), profileFile);
                }
                    break;
                default:
                    status = DS_UNKNOWN_DEVICE_TYPE;
                    break;
            };

            fwrite(DS_TAG_SCORE, sizeof(char), strlen(DS_TAG_SCORE), profileFile);
            status = serializer(profile->devices + i, &serializedScore, &serializedScoreSize);
            if (status == DS_SUCCESS && serializedScore != NULL && serializedScoreSize > 0)
            {
                fwrite(serializedScore, sizeof(char), serializedScoreSize, profileFile);
                free(serializedScore);
            }
            fwrite(DS_TAG_SCORE_END, sizeof(char), strlen(DS_TAG_SCORE_END), profileFile);
            fwrite(DS_TAG_DEVICE_END, sizeof(char), strlen(DS_TAG_DEVICE_END), profileFile);
            fwrite("\n", sizeof(char), 1, profileFile);
        }
        fclose(profileFile);
    }
    return status;
}


inline ds_status readProFile(const char* fileName, char** content, size_t* contentSize)
{
    FILE* input = NULL;
    size_t size = 0;
    char* binary = NULL;

    *contentSize = 0;
    *content = NULL;

    input = fopen(fileName, "rb");
    if (input == NULL)
    {
        return DS_FILE_ERROR;
    }

    fseek(input, 0L, SEEK_END);
    size = ftell(input);
    rewind(input);
    binary = (char*)malloc(size);
    if (binary == NULL)
    {
        return DS_FILE_ERROR;
    }
    size_t bytesRead = fread(binary, sizeof(char), size, input);
    (void) bytesRead; // avoid warning
    fclose(input);

    *contentSize = size;
    *content = binary;
    return DS_SUCCESS;
}


inline const char* findString(const char* contentStart, const char* contentEnd, const char* string)
{
    size_t stringLength;
    const char* currentPosition;
    const char* found;
    found = NULL;
    stringLength = strlen(string);
    currentPosition = contentStart;
    for (currentPosition = contentStart; currentPosition < contentEnd; currentPosition++)
    {
        if (*currentPosition == string[0])
        {
            if (currentPosition + stringLength < contentEnd)
            {
                if (strncmp(currentPosition, string, stringLength) == 0)
                {
                    found = currentPosition;
                    break;
                }
            }
        }
    }
    return found;
}


typedef ds_status(* ds_score_deserializer)(ds_device* device, const unsigned char* serializedScore, unsigned int serializedScoreSize);
inline ds_status readProfileFromFile(ds_profile* profile, ds_score_deserializer deserializer, const char* file)
{

    ds_status status = DS_SUCCESS;
    char* contentStart = NULL;
    const char* contentEnd = NULL;
    size_t contentSize;

    if (profile == NULL) return DS_INVALID_PROFILE;

    status = readProFile(file, &contentStart, &contentSize);
    if (status == DS_SUCCESS)
    {
        const char* currentPosition;
        const char* dataStart;
        const char* dataEnd;
        size_t versionStringLength;

        contentEnd = contentStart + contentSize;
        currentPosition = contentStart;


        // parse the version string
        dataStart = findString(currentPosition, contentEnd, DS_TAG_VERSION);
        if (dataStart == NULL)
        {
            status = DS_PROFILE_FILE_ERROR;
            goto cleanup;
        }
        dataStart += strlen(DS_TAG_VERSION);

        dataEnd = findString(dataStart, contentEnd, DS_TAG_VERSION_END);
        if (dataEnd == NULL)
        {
            status = DS_PROFILE_FILE_ERROR;
            goto cleanup;
        }

        versionStringLength = strlen(profile->version);
        if (versionStringLength != static_cast<size_t>(dataEnd - dataStart)
            || strncmp(profile->version, dataStart, versionStringLength) != 0)
        {
            // version mismatch
            status = DS_PROFILE_FILE_ERROR;
            goto cleanup;
        }
        currentPosition = dataEnd + strlen(DS_TAG_VERSION_END);

        // parse the device information
        while (1)
        {
            unsigned int i;

            const char* deviceTypeStart;
            const char* deviceTypeEnd;
            ds_device_type deviceType;

            const char* deviceNameStart;
            const char* deviceNameEnd;

            const char* deviceScoreStart;
            const char* deviceScoreEnd;

            const char* deviceDriverStart;
            const char* deviceDriverEnd;

            dataStart = findString(currentPosition, contentEnd, DS_TAG_DEVICE);
            if (dataStart == NULL)
            {
                // nothing useful remain, quit...
                break;
            }
            dataStart += strlen(DS_TAG_DEVICE);
            dataEnd = findString(dataStart, contentEnd, DS_TAG_DEVICE_END);
            if (dataEnd == NULL)
            {
                status = DS_PROFILE_FILE_ERROR;
                goto cleanup;
            }

            // parse the device type
            deviceTypeStart = findString(dataStart, contentEnd, DS_TAG_DEVICE_TYPE);
            if (deviceTypeStart == NULL)
            {
                status = DS_PROFILE_FILE_ERROR;
                goto cleanup;
            }
            deviceTypeStart += strlen(DS_TAG_DEVICE_TYPE);
            deviceTypeEnd = findString(deviceTypeStart, contentEnd, DS_TAG_DEVICE_TYPE_END);
            if (deviceTypeEnd == NULL)
            {
                status = DS_PROFILE_FILE_ERROR;
                goto cleanup;
            }
            memcpy(&deviceType, deviceTypeStart, sizeof(ds_device_type));


            // parse the device name
            if (deviceType == DS_DEVICE_OPENCL_DEVICE)
            {

                deviceNameStart = findString(dataStart, contentEnd, DS_TAG_DEVICE_NAME);
                if (deviceNameStart == NULL)
                {
                    status = DS_PROFILE_FILE_ERROR;
                    goto cleanup;
                }
                deviceNameStart += strlen(DS_TAG_DEVICE_NAME);
                deviceNameEnd = findString(deviceNameStart, contentEnd, DS_TAG_DEVICE_NAME_END);
                if (deviceNameEnd == NULL)
                {
                    status = DS_PROFILE_FILE_ERROR;
                    goto cleanup;
                }


                deviceDriverStart = findString(dataStart, contentEnd, DS_TAG_DEVICE_DRIVER_VERSION);
                if (deviceDriverStart == NULL)
                {
                    status = DS_PROFILE_FILE_ERROR;
                    goto cleanup;
                }
                deviceDriverStart += strlen(DS_TAG_DEVICE_DRIVER_VERSION);
                deviceDriverEnd = findString(deviceDriverStart, contentEnd, DS_TAG_DEVICE_DRIVER_VERSION_END);
                if (deviceDriverEnd == NULL)
                {
                    status = DS_PROFILE_FILE_ERROR;
                    goto cleanup;
                }


                // check if this device is on the system
                for (i = 0; i < profile->numDevices; i++)
                {
                    if (profile->devices[i].type == DS_DEVICE_OPENCL_DEVICE)
                    {
                        size_t actualDeviceNameLength;
                        size_t driverVersionLength;

                        actualDeviceNameLength = strlen(profile->devices[i].oclDeviceName);
                        driverVersionLength = strlen(profile->devices[i].oclDriverVersion);
                        if (actualDeviceNameLength == static_cast<size_t>(deviceNameEnd - deviceNameStart)
                            && driverVersionLength == static_cast<size_t>(deviceDriverEnd - deviceDriverStart)
                            && strncmp(profile->devices[i].oclDeviceName, deviceNameStart, actualDeviceNameLength) == 0
                            && strncmp(profile->devices[i].oclDriverVersion, deviceDriverStart, driverVersionLength) == 0)
                        {

                            deviceScoreStart = findString(dataStart, contentEnd, DS_TAG_SCORE);
                            if (deviceNameStart == NULL)
                            {
                                status = DS_PROFILE_FILE_ERROR;
                                goto cleanup;
                            }
                            deviceScoreStart += strlen(DS_TAG_SCORE);
                            deviceScoreEnd = findString(deviceScoreStart, contentEnd, DS_TAG_SCORE_END);
                            status = deserializer(profile->devices + i, (const unsigned char*)deviceScoreStart, deviceScoreEnd - deviceScoreStart);
                            if (status != DS_SUCCESS)
                            {
                                goto cleanup;
                            }
                        }
                    }
                }

            }
            else if (deviceType == DS_DEVICE_NATIVE_CPU)
            {
                for (i = 0; i < profile->numDevices; i++)
                {
                    if (profile->devices[i].type == DS_DEVICE_NATIVE_CPU)
                    {
                        deviceScoreStart = findString(dataStart, contentEnd, DS_TAG_SCORE);
                        if (deviceScoreStart == NULL)
                        {
                            status = DS_PROFILE_FILE_ERROR;
                            goto cleanup;
                        }
                        deviceScoreStart += strlen(DS_TAG_SCORE);
                        deviceScoreEnd = findString(deviceScoreStart, contentEnd, DS_TAG_SCORE_END);
                        status = deserializer(profile->devices + i, (const unsigned char*)deviceScoreStart, deviceScoreEnd - deviceScoreStart);
                        if (status != DS_SUCCESS)
                        {
                            goto cleanup;
                        }
                    }
                }
            }

            // skip over the current one to find the next device
            currentPosition = dataEnd + strlen(DS_TAG_DEVICE_END);
        }
    }
cleanup:
    if (contentStart != NULL) free(contentStart);
    return status;
}

inline ds_status getNumDeviceWithEmptyScore(ds_profile* profile, unsigned int* num)
{
    unsigned int i;
    if (profile == NULL || num == NULL) return DS_MEMORY_ERROR;
    *num = 0;
    for (i = 0; i < profile->numDevices; i++)
    {
        if (profile->devices[i].score == NULL)
        {
            (*num)++;
        }
    }
    return DS_SUCCESS;
}

#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */