forked from Xilinx/XRT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclSVMAlloc.cpp
122 lines (103 loc) · 3.23 KB
/
clSVMAlloc.cpp
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
/**
* Copyright (C) 2016-2020 Xilinx, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may
* not use this file except in compliance with the License. A copy of the
* License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
// Copyright 2017-2020 Xilinx, Inc. All rights reserved.
#include "xocl/config.h"
#include "xocl/core/memory.h"
#include "xocl/core/context.h"
#include "xocl/core/device.h"
#include "detail/memory.h"
#include "detail/context.h"
#include <bitset>
#include "plugin/xdp/profile_v2.h"
namespace {
// Hack to determine if a context is associated with exactly one
// device. Additionally, in emulation mode, the device must be
// active, e.g. loaded through a call to loadBinary.
//
// This works around a problem where clCreateBuffer is called in
// emulation mode before clCreateProgramWithBinary->loadBinary has
// been called. The call to loadBinary can end up switching the
// device from swEm to hwEm.
//
// In non emulation mode it is sufficient to check that the context
// has only one device.
static xocl::device*
singleContextDevice(cl_context context)
{
auto device = xocl::xocl(context)->get_device_if_one();
if (!device)
return nullptr;
return (device->is_active())
? device
: nullptr;
}
}
namespace xocl {
static void
validOrError(cl_context context,
cl_svm_mem_flags flags,
size_t size,
unsigned int alignment)
{
if (!config::api_checks())
return;
// CL_INVALID_CONTEXT if context is not a valid context
detail::context::validOrError(context);
// CL_INVALID_VALUE if values specified in flags are not valid as
// defined in the table above
// TODO: Check SVM flags when we going to support fine grain SVM buffer
detail::memory::validOrError(flags);
// CL_INVALID_BUFFER_SIZE if size is 0.
if (!size)
throw error(CL_INVALID_BUFFER_SIZE,"size==0");
// CL_INVALID_VALUE if values specified in alignment are not 4096
if (alignment != 4096)
throw error(CL_INVALID_VALUE,"alignment need to be 4096");
}
static void*
clSVMAlloc(cl_context context,
cl_svm_mem_flags flags,
size_t size,
unsigned int alignment)
{
if (!flags)
flags = CL_MEM_READ_WRITE;
validOrError(context,flags,size,alignment);
if (auto device = singleContextDevice(context))
return device->get_xdevice()->alloc_svm(size);
return nullptr;
}
} // xocl
void*
clSVMAlloc(cl_context context,
cl_svm_mem_flags flags,
size_t size,
unsigned int alignment)
{
try {
PROFILE_LOG_FUNCTION_CALL;
LOP_LOG_FUNCTION_CALL;
return xocl::clSVMAlloc
(context,flags,size,alignment);
}
catch (const xrt_xocl::error& ex) {
xocl::send_exception_message(ex.what());
}
catch (const std::exception& ex) {
xocl::send_exception_message(ex.what());
}
return nullptr;
}