Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
magicwindyyd
mindspore
提交
beba8dc2
M
mindspore
项目概览
magicwindyyd
/
mindspore
与 Fork 源项目一致
Fork自
MindSpore / mindspore
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
mindspore
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
beba8dc2
编写于
8月 28, 2020
作者:
C
Corleone
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fixed double free parameter
上级
9062e0a7
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
64 addition
and
37 deletion
+64
-37
mindspore/lite/src/runtime/kernel/opencl/subgraph_opencl_kernel.cc
.../lite/src/runtime/kernel/opencl/subgraph_opencl_kernel.cc
+0
-5
mindspore/lite/src/runtime/opencl/opencl_allocator.cc
mindspore/lite/src/runtime/opencl/opencl_allocator.cc
+60
-31
mindspore/lite/src/runtime/opencl/opencl_allocator.h
mindspore/lite/src/runtime/opencl/opencl_allocator.h
+3
-0
mindspore/lite/src/runtime/opencl/opencl_runtime.cc
mindspore/lite/src/runtime/opencl/opencl_runtime.cc
+1
-1
未找到文件。
mindspore/lite/src/runtime/kernel/opencl/subgraph_opencl_kernel.cc
浏览文件 @
beba8dc2
...
...
@@ -276,11 +276,6 @@ int SubGraphOpenCLKernel::UnInit() {
delete
op
;
}
}
for
(
const
auto
parameter
:
in_parameters_
)
{
if
(
parameter
!=
nullptr
)
{
delete
parameter
;
}
}
return
RET_OK
;
}
...
...
mindspore/lite/src/runtime/opencl/opencl_allocator.cc
浏览文件 @
beba8dc2
...
...
@@ -24,6 +24,9 @@
namespace
mindspore
::
lite
::
opencl
{
OpenCLAllocator
::
OpenCLAllocator
()
{}
OpenCLAllocator
::
OpenCLAllocator
(
OpenCLRuntime
*
ocl_runtime
)
:
ocl_runtime_
(
ocl_runtime
)
{}
OpenCLAllocator
::~
OpenCLAllocator
()
{
Clear
();
}
void
OpenCLAllocator
::
SetContext
(
const
AllocatorContext
&
ctx
)
{
...
...
@@ -46,14 +49,16 @@ void OpenCLAllocator::UnLock() {
void
*
OpenCLAllocator
::
Malloc
(
size_t
size
)
{
return
Malloc
(
size
,
std
::
vector
<
size_t
>
{});
}
void
*
OpenCLAllocator
::
Malloc
(
size_t
size
,
const
std
::
vector
<
size_t
>
&
img_size
)
{
auto
ocl_runtime
=
opencl
::
OpenCLRuntime
::
GetInstance
();
auto
svm_capabilities
=
ocl_runtime
->
GetSVMCapabilities
();
if
(
ocl_runtime_
==
nullptr
)
{
ocl_runtime_
=
opencl
::
OpenCLRuntime
::
GetInstance
();
}
auto
svm_capabilities
=
ocl_runtime_
->
GetSVMCapabilities
();
size_t
img_pitch
=
0
;
size_t
dtype_size
=
1
;
if
(
!
img_size
.
empty
())
{
dtype_size
=
img_size
[
2
]
==
CL_FLOAT
?
sizeof
(
cl_float4
)
:
sizeof
(
cl_half4
);
uint32_t
image_alignment
=
ocl_runtime
->
GetImagePitchAlignment
();
uint32_t
image_alignment
=
ocl_runtime
_
->
GetImagePitchAlignment
();
img_pitch
=
(
img_size
[
0
]
+
image_alignment
-
1
)
/
image_alignment
*
image_alignment
;
size
=
img_pitch
*
img_size
[
1
]
*
dtype_size
;
}
...
...
@@ -87,28 +92,28 @@ void *OpenCLAllocator::Malloc(size_t size, const std::vector<size_t> &img_size)
cl_svm_mem_flags
flags
=
(
svm_capabilities
&
CL_DEVICE_SVM_FINE_GRAIN_BUFFER
)
?
CL_MEM_SVM_FINE_GRAIN_BUFFER
:
0
;
flags
|=
(
svm_capabilities
&
CL_DEVICE_SVM_ATOMICS
)
?
CL_MEM_SVM_ATOMICS
:
0
;
flags
=
flags
|
CL_MEM_READ_WRITE
;
host_ptr
=
clSVMAlloc
((
*
ocl_runtime
->
Context
())(),
flags
,
size
,
0
);
host_ptr
=
clSVMAlloc
((
*
ocl_runtime
_
->
Context
())(),
flags
,
size
,
0
);
}
else
{
cl_int
ret
=
CL_SUCCESS
;
cl
::
Buffer
*
buffer
=
new
(
std
::
nothrow
)
cl
::
Buffer
(
*
ocl_runtime
->
Context
(),
CL_MEM_READ_WRITE
|
CL_MEM_ALLOC_HOST_PTR
,
size
,
NULL
,
&
ret
);
cl
::
Buffer
(
*
ocl_runtime
_
->
Context
(),
CL_MEM_READ_WRITE
|
CL_MEM_ALLOC_HOST_PTR
,
size
,
NULL
,
&
ret
);
if
(
buffer
==
nullptr
||
ret
!=
CL_SUCCESS
)
{
UnLock
();
MS_LOG
(
ERROR
)
<<
"Create OpenCL buffer failed! (ERROR CODE: "
<<
ret
<<
")"
;
return
nullptr
;
}
device_ptr
=
static_cast
<
void
*>
(
buffer
);
host_ptr
=
ocl_runtime
->
MapBuffer
(
*
buffer
,
CL_MAP_READ
|
CL_MAP_WRITE
,
size
);
host_ptr
=
ocl_runtime
_
->
MapBuffer
(
*
buffer
,
CL_MAP_READ
|
CL_MAP_WRITE
,
size
);
if
(
host_ptr
==
nullptr
)
{
UnLock
();
MS_LOG
(
ERROR
)
<<
"Map buffer failed, can not found buffer :"
<<
device_ptr
<<
", host_ptr="
<<
host_ptr
;
return
nullptr
;
}
cl
::
Memory
*
mem
=
buffer
;
ocl_runtime
->
UnmapBuffer
(
*
mem
,
host_ptr
);
ocl_runtime
_
->
UnmapBuffer
(
*
mem
,
host_ptr
);
if
(
!
img_size
.
empty
())
{
cl
::
ImageFormat
image_format
(
CL_RGBA
,
img_size
[
2
]);
cl
::
Image2D
*
image
=
new
(
std
::
nothrow
)
cl
::
Image2D
(
*
ocl_runtime
->
Context
(),
image_format
,
*
buffer
,
img_size
[
0
],
cl
::
Image2D
*
image
=
new
(
std
::
nothrow
)
cl
::
Image2D
(
*
ocl_runtime
_
->
Context
(),
image_format
,
*
buffer
,
img_size
[
0
],
img_size
[
1
],
img_pitch
*
dtype_size
,
&
ret
);
if
(
image
==
nullptr
||
ret
!=
CL_SUCCESS
)
{
delete
buffer
;
...
...
@@ -139,7 +144,9 @@ void *OpenCLAllocator::CreateImageFromHost(void *data, size_t size, const std::v
MS_LOG
(
ERROR
)
<<
"MallocData out of max_size, size: "
<<
size
;
return
nullptr
;
}
auto
ocl_runtime
=
opencl
::
OpenCLRuntime
::
GetInstance
();
if
(
ocl_runtime_
==
nullptr
)
{
ocl_runtime_
=
opencl
::
OpenCLRuntime
::
GetInstance
();
}
Lock
();
auto
iter
=
free_list_
.
lower_bound
(
size
);
while
(
iter
!=
free_list_
.
end
()
&&
(
iter
->
second
->
size_
>=
size
)
&&
(
iter
->
second
->
size_
<
(
size
<<
shift_factor_
)))
{
...
...
@@ -164,7 +171,7 @@ void *OpenCLAllocator::CreateImageFromHost(void *data, size_t size, const std::v
cl_int
ret
=
CL_SUCCESS
;
// CL_HALF_FLOAT, CL_FLOAT
cl
::
ImageFormat
image_format
(
CL_RGBA
,
img_size
[
2
]);
cl
::
Image2D
*
image
=
new
(
std
::
nothrow
)
cl
::
Image2D
(
*
ocl_runtime
->
Context
(),
CL_MEM_READ_ONLY
|
CL_MEM_COPY_HOST_PTR
,
cl
::
Image2D
*
image
=
new
(
std
::
nothrow
)
cl
::
Image2D
(
*
ocl_runtime
_
->
Context
(),
CL_MEM_READ_ONLY
|
CL_MEM_COPY_HOST_PTR
,
image_format
,
img_size
[
0
],
img_size
[
1
],
0
,
data
,
&
ret
);
if
(
image
==
nullptr
||
ret
!=
CL_SUCCESS
)
{
MS_LOG
(
ERROR
)
<<
"Create OpenCL Image2D failed! (ERROR CODE: "
<<
ret
<<
")"
;
...
...
@@ -174,14 +181,14 @@ void *OpenCLAllocator::CreateImageFromHost(void *data, size_t size, const std::v
}
image_ptr
=
static_cast
<
void
*>
(
image
);
std
::
vector
<
size_t
>
region
{
img_size
[
0
],
img_size
[
1
],
1
};
host_ptr
=
ocl_runtime
->
MapBuffer
(
*
image
,
0
,
CL_MAP_READ
|
CL_MAP_WRITE
,
region
);
host_ptr
=
ocl_runtime
_
->
MapBuffer
(
*
image
,
0
,
CL_MAP_READ
|
CL_MAP_WRITE
,
region
);
if
(
host_ptr
==
nullptr
)
{
MS_LOG
(
ERROR
)
<<
"Map buffer failed, can not found buffer :"
<<
device_ptr
<<
", host_ptr="
<<
host_ptr
;
UnLock
();
return
nullptr
;
}
cl
::
Memory
*
mem
=
image
;
ocl_runtime
->
UnmapBuffer
(
*
mem
,
host_ptr
);
ocl_runtime
_
->
UnmapBuffer
(
*
mem
,
host_ptr
);
std
::
unique_ptr
<
MemBuf
>
mem_buf
=
std
::
make_unique
<
MemBuf
>
();
mem_buf
->
size_
=
size
;
mem_buf
->
device_ptr_
=
device_ptr
;
...
...
@@ -251,28 +258,46 @@ void *OpenCLAllocator::GetBuffer(void *buffer) {
void
OpenCLAllocator
::
Clear
()
{
Lock
();
auto
ocl_runtime
=
opencl
::
OpenCLRuntime
::
GetInstance
();
auto
svm_capabilities
=
ocl_runtime
->
GetSVMCapabilities
();
if
(
ocl_runtime_
==
nullptr
)
{
ocl_runtime_
=
opencl
::
OpenCLRuntime
::
GetInstance
();
}
auto
svm_capabilities
=
ocl_runtime_
->
GetSVMCapabilities
();
for
(
auto
it
=
allocated_list_
.
begin
();
it
!=
allocated_list_
.
end
();
it
++
)
{
if
(
svm_capabilities
)
{
clSVMFree
((
*
ocl_runtime
->
Context
())(),
it
->
second
->
host_ptr_
);
clSVMFree
((
*
ocl_runtime
_
->
Context
())(),
it
->
second
->
host_ptr_
);
MS_LOG
(
DEBUG
)
<<
"OpenCL free svm buffer : "
<<
it
->
second
->
host_ptr_
;
}
else
{
cl
::
Buffer
*
buff
=
static_cast
<
cl
::
Buffer
*>
(
it
->
second
->
device_ptr_
);
MS_LOG
(
DEBUG
)
<<
"OpenCL free device buffer : "
<<
buff
;
delete
buff
;
cl
::
Buffer
*
buffer
=
static_cast
<
cl
::
Buffer
*>
(
it
->
second
->
device_ptr_
);
MS_LOG
(
DEBUG
)
<<
"OpenCL free device buffer : "
<<
buffer
;
if
(
buffer
!=
nullptr
)
{
delete
buffer
;
it
->
second
->
device_ptr_
=
nullptr
;
}
cl
::
Image
*
image
=
static_cast
<
cl
::
Image
*>
(
it
->
second
->
image_ptr_
);
if
(
image
!=
nullptr
)
{
delete
image
;
it
->
second
->
image_ptr_
=
nullptr
;
}
}
}
allocated_list_
.
clear
();
for
(
auto
it
=
free_list_
.
begin
();
it
!=
free_list_
.
end
();
it
++
)
{
if
(
svm_capabilities
)
{
clSVMFree
((
*
ocl_runtime
->
Context
())(),
it
->
second
->
host_ptr_
);
clSVMFree
((
*
ocl_runtime
_
->
Context
())(),
it
->
second
->
host_ptr_
);
MS_LOG
(
DEBUG
)
<<
"OpenCL free svm buffer : "
<<
it
->
second
->
host_ptr_
;
}
else
{
cl
::
Buffer
*
buff
=
static_cast
<
cl
::
Buffer
*>
(
it
->
second
->
device_ptr_
);
MS_LOG
(
DEBUG
)
<<
"OpenCL free device buffer : "
<<
buff
;
delete
buff
;
cl
::
Buffer
*
buffer
=
static_cast
<
cl
::
Buffer
*>
(
it
->
second
->
device_ptr_
);
MS_LOG
(
DEBUG
)
<<
"OpenCL free device buffer : "
<<
buffer
;
if
(
buffer
!=
nullptr
)
{
delete
buffer
;
it
->
second
->
device_ptr_
=
nullptr
;
}
cl
::
Image
*
image
=
static_cast
<
cl
::
Image
*>
(
it
->
second
->
image_ptr_
);
if
(
image
!=
nullptr
)
{
delete
image
;
it
->
second
->
image_ptr_
=
nullptr
;
}
}
}
free_list_
.
clear
();
...
...
@@ -280,8 +305,10 @@ void OpenCLAllocator::Clear() {
}
void
*
OpenCLAllocator
::
MapBuffer
(
void
*
host_ptr
,
int
flags
,
void
*
command_queue
,
bool
sync
)
{
auto
ocl_runtime
=
opencl
::
OpenCLRuntime
::
GetInstance
();
auto
svm_capabilities
=
ocl_runtime
->
GetSVMCapabilities
();
if
(
ocl_runtime_
==
nullptr
)
{
ocl_runtime_
=
opencl
::
OpenCLRuntime
::
GetInstance
();
}
auto
svm_capabilities
=
ocl_runtime_
->
GetSVMCapabilities
();
if
(
svm_capabilities
)
{
if
(
!
(
svm_capabilities
&
CL_DEVICE_SVM_FINE_GRAIN_BUFFER
))
{
auto
it
=
allocated_list_
.
find
(
host_ptr
);
...
...
@@ -289,7 +316,7 @@ void *OpenCLAllocator::MapBuffer(void *host_ptr, int flags, void *command_queue,
MS_LOG
(
ERROR
)
<<
"Map buffer failed, can not found buffer :"
<<
host_ptr
;
return
nullptr
;
}
ocl_runtime
->
MapBuffer
(
host_ptr
,
flags
,
it
->
second
->
size_
,
static_cast
<
cl
::
CommandQueue
*>
(
command_queue
),
sync
);
ocl_runtime
_
->
MapBuffer
(
host_ptr
,
flags
,
it
->
second
->
size_
,
static_cast
<
cl
::
CommandQueue
*>
(
command_queue
),
sync
);
}
return
host_ptr
;
}
...
...
@@ -310,12 +337,12 @@ void *OpenCLAllocator::MapBuffer(void *host_ptr, int flags, void *command_queue,
void
*
new_host_ptr
{
nullptr
};
if
(
mem_buf
->
img_size
.
empty
())
{
cl
::
Buffer
*
buffer
=
static_cast
<
cl
::
Buffer
*>
(
mem_buf
->
device_ptr_
);
new_host_ptr
=
ocl_runtime
->
MapBuffer
(
*
buffer
,
flags
,
mem_buf
->
size_
,
nullptr
,
sync
);
new_host_ptr
=
ocl_runtime
_
->
MapBuffer
(
*
buffer
,
flags
,
mem_buf
->
size_
,
nullptr
,
sync
);
}
else
{
cl
::
ImageFormat
image_format
(
CL_RGBA
,
mem_buf
->
img_size
[
2
]);
std
::
vector
<
size_t
>
region
{
mem_buf
->
img_size
[
0
],
mem_buf
->
img_size
[
1
],
1
};
cl
::
Image2D
*
image
=
static_cast
<
cl
::
Image2D
*>
(
mem_buf
->
image_ptr_
);
new_host_ptr
=
ocl_runtime
->
MapBuffer
(
*
image
,
0
,
CL_MAP_READ
|
CL_MAP_WRITE
,
region
);
new_host_ptr
=
ocl_runtime
_
->
MapBuffer
(
*
image
,
0
,
CL_MAP_READ
|
CL_MAP_WRITE
,
region
);
}
if
(
new_host_ptr
==
nullptr
)
{
UnLock
();
...
...
@@ -334,11 +361,13 @@ void *OpenCLAllocator::MapBuffer(void *host_ptr, int flags, void *command_queue,
}
int
OpenCLAllocator
::
UnmapBuffer
(
void
*
host_ptr
,
void
*
command_queue
)
{
auto
ocl_runtime
=
opencl
::
OpenCLRuntime
::
GetInstance
();
auto
svm_capabilities
=
ocl_runtime
->
GetSVMCapabilities
();
if
(
ocl_runtime_
==
nullptr
)
{
ocl_runtime_
=
opencl
::
OpenCLRuntime
::
GetInstance
();
}
auto
svm_capabilities
=
ocl_runtime_
->
GetSVMCapabilities
();
if
(
svm_capabilities
)
{
if
(
!
(
svm_capabilities
&
CL_DEVICE_SVM_FINE_GRAIN_BUFFER
))
{
return
ocl_runtime
->
UnmapBuffer
(
host_ptr
);
return
ocl_runtime
_
->
UnmapBuffer
(
host_ptr
);
}
return
RET_OK
;
}
...
...
@@ -351,7 +380,7 @@ int OpenCLAllocator::UnmapBuffer(void *host_ptr, void *command_queue) {
it
->
second
->
map_flags
=
false
;
cl
::
Memory
*
mem
=
static_cast
<
cl
::
Memory
*>
(
it
->
second
->
img_size
.
empty
()
?
it
->
second
->
device_ptr_
:
it
->
second
->
image_ptr_
);
return
ocl_runtime
->
UnmapBuffer
(
*
mem
,
it
->
second
->
host_ptr_
,
static_cast
<
cl
::
CommandQueue
*>
(
command_queue
));
return
ocl_runtime
_
->
UnmapBuffer
(
*
mem
,
it
->
second
->
host_ptr_
,
static_cast
<
cl
::
CommandQueue
*>
(
command_queue
));
}
else
{
MS_LOG
(
WARNING
)
<<
"Host ptr "
<<
host_ptr
<<
" do not mapped"
;
return
RET_OK
;
...
...
mindspore/lite/src/runtime/opencl/opencl_allocator.h
浏览文件 @
beba8dc2
...
...
@@ -40,11 +40,13 @@ struct OpenclMemory {
OpenCLMemoryType
mem_type
{
MS_HOST_BUFFER
|
MS_CL_BUFFER
};
};
class
OpenCLRuntime
;
enum
class
MemType
:
char
{
SVM
,
BUF
,
IMG
};
class
OpenCLAllocator
:
public
Allocator
{
public:
OpenCLAllocator
();
explicit
OpenCLAllocator
(
OpenCLRuntime
*
ocl_runtime
);
~
OpenCLAllocator
()
override
;
void
SetContext
(
const
AllocatorContext
&
ctx
)
override
;
void
*
Malloc
(
size_t
size
)
override
;
...
...
@@ -86,6 +88,7 @@ class OpenCLAllocator : public Allocator {
// 6 is empirical value
int
shift_factor_
=
6
;
bool
lock_flag_
=
false
;
OpenCLRuntime
*
ocl_runtime_
{
nullptr
};
};
}
// namespace mindspore::lite::opencl
...
...
mindspore/lite/src/runtime/opencl/opencl_runtime.cc
浏览文件 @
beba8dc2
...
...
@@ -209,7 +209,7 @@ int OpenCLRuntime::Init() {
return
RET_ERROR
;
}
allocator_
=
new
(
std
::
nothrow
)
OpenCLAllocator
();
allocator_
=
new
(
std
::
nothrow
)
OpenCLAllocator
(
this
);
if
(
allocator_
==
nullptr
)
{
MS_LOG
(
ERROR
)
<<
"Command OpenCL allocator failed!"
;
return
RET_ERROR
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录