File: cl_memory_properties_helpers.cpp

package info (click to toggle)
intel-compute-runtime 25.48.36300.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 80,652 kB
  • sloc: cpp: 939,022; lisp: 2,090; sh: 722; makefile: 162; python: 21
file content (100 lines) | stat: -rw-r--r-- 4,252 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 2021-2024 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include "shared/source/device/device.h"

#include "opencl/source/cl_device/cl_device.h"
#include "opencl/source/context/context.h"
#include "opencl/source/helpers/cl_memory_properties_helpers_base.inl"
#include "opencl/source/mem_obj/mem_obj_helper.h"
#include "opencl/source/sharings/unified/unified_buffer.h"

namespace NEO {

bool ClMemoryPropertiesHelper::parseMemoryProperties(const cl_mem_properties_intel *properties, MemoryProperties &memoryProperties,
                                                     cl_mem_flags &flags, cl_mem_flags_intel &flagsIntel,
                                                     cl_mem_alloc_flags_intel &allocflags, ClMemoryPropertiesHelper::ObjType objectType, Context &context) {
    bool deviceSet = false;
    Device *pDevice = context.getDevice(0)->getDevice().getRootDevice();
    uint64_t handle = 0;
    uint64_t handleType = 0;
    uintptr_t hostptr = 0;
    std::vector<Device *> devices;

    if (properties != nullptr) {
        for (int i = 0; properties[i] != 0; i += 2) {
            switch (properties[i]) {
            case CL_MEM_FLAGS:
                flags |= static_cast<cl_mem_flags>(properties[i + 1]);
                break;
            case CL_MEM_FLAGS_INTEL:
                flagsIntel |= static_cast<cl_mem_flags_intel>(properties[i + 1]);
                break;
            case CL_MEM_ALLOC_FLAGS_INTEL:
                allocflags |= static_cast<cl_mem_alloc_flags_intel>(properties[i + 1]);
                break;
            case CL_MEM_DEVICE_ID_INTEL_DEPRECATED:
            case CL_MEM_DEVICE_ID_INTEL: {
                if (deviceSet) {
                    return false;
                }
                cl_device_id deviceId = reinterpret_cast<cl_device_id>(properties[i + 1]);
                auto pClDevice = NEO::castToObject<ClDevice>(deviceId);
                if ((pClDevice == nullptr) || (!context.isDeviceAssociated(*pClDevice))) {
                    return false;
                }
                pDevice = &pClDevice->getDevice();
                deviceSet = true;
                break;
            }
            case CL_MEM_ALLOC_USE_HOST_PTR_INTEL:
                hostptr = static_cast<uintptr_t>(properties[i + 1]);
                break;
            case CL_EXTERNAL_MEMORY_HANDLE_DMA_BUF_KHR:
                handle = static_cast<uint64_t>(properties[i + 1]);
                handleType = static_cast<uint64_t>(UnifiedSharingHandleType::linuxFd);
                break;
            case CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KHR:
                handle = static_cast<uint64_t>(properties[i + 1]);
                handleType = static_cast<uint64_t>(UnifiedSharingHandleType::win32Nt);
                break;
            case CL_MEM_DEVICE_HANDLE_LIST_KHR:
                while (properties[i + 1] != CL_MEM_DEVICE_HANDLE_LIST_END_KHR) {
                    cl_device_id deviceId = reinterpret_cast<cl_device_id>(properties[i + 1]);
                    auto pClDevice = NEO::castToObject<ClDevice>(deviceId);
                    if ((pClDevice == nullptr) || (!context.isDeviceAssociated(*pClDevice))) {
                        return false;
                    }
                    devices.push_back(&pClDevice->getDevice());
                    i++;
                }
                break;
            default:
                return false;
            }
        }
    }

    memoryProperties = ClMemoryPropertiesHelper::createMemoryProperties(flags, flagsIntel, allocflags, pDevice);
    memoryProperties.handleType = handleType;
    memoryProperties.handle = handle;
    memoryProperties.hostptr = hostptr;
    memoryProperties.associatedDevices = devices;

    switch (objectType) {
    case ClMemoryPropertiesHelper::ObjType::buffer:
        return isFieldValid(flags, MemObjHelper::validFlagsForBuffer) &&
               isFieldValid(flagsIntel, MemObjHelper::validFlagsForBufferIntel);
    case ClMemoryPropertiesHelper::ObjType::image:
        return isFieldValid(flags, MemObjHelper::validFlagsForImage) &&
               isFieldValid(flagsIntel, MemObjHelper::validFlagsForImageIntel);
    default:
        return true;
    }
}

} // namespace NEO