|
|
|
from myutils.ConfigManager import myCongif
|
|
|
|
if myCongif.get_data("model_platform") == "acl":
|
|
|
|
import acl
|
|
|
|
|
|
|
|
SUCCESS = 0 # 成功状态值
|
|
|
|
FAILED = 1 # 失败状态值
|
|
|
|
'''
|
|
|
|
#acl.rt.get_device_count() 获取可用Device数量
|
|
|
|
# 需先调用acl.rt.get_run_mode接口获取软件栈的运行模式。
|
|
|
|
#
|
|
|
|
# 当查询结果为ACL_HOST = 1,则数据传输时涉及申请Host上的内存。
|
|
|
|
# 当查询结果为ACL_DEVICE = 0 ,则数据传输时不涉及申请Host上的内存,仅需申请Device上的内存。
|
|
|
|
'''
|
|
|
|
class ACLModeManger:
|
|
|
|
def __init__(self,):
|
|
|
|
self.acl_ok = False
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
#初始化acl相关资源--一个进程内只能调用一次acl.init接口
|
|
|
|
@staticmethod
|
|
|
|
def init_acl(device_id):
|
|
|
|
# '''acl初始化函数'''
|
|
|
|
ret = acl.init() # 0-成功,其它失败
|
|
|
|
if ret:
|
|
|
|
raise RuntimeError(ret)
|
|
|
|
ret = acl.rt.set_device(device_id) # 指定当前进程或线程中用于运算的Device。可以进程或线程中指定。*多设备时可以放线程*
|
|
|
|
# 在某一进程中指定Device,该进程内的多个线程可共用此Device显式创建Context(acl.rt.create_context接口)。
|
|
|
|
if ret:
|
|
|
|
raise RuntimeError(ret)
|
|
|
|
print('ACL init Device Successfully')
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
#去初始化
|
|
|
|
@staticmethod
|
|
|
|
def del_acl(device_id):
|
|
|
|
'''Device去初始化'''
|
|
|
|
ret = acl.rt.reset_device(device_id) # 释放Device
|
|
|
|
if ret:
|
|
|
|
raise RuntimeError(ret)
|
|
|
|
|
|
|
|
ret = acl.finalize() # 去初始化 0-成功,其它失败 --官方文档不建议放析构函数中执行
|
|
|
|
if ret:
|
|
|
|
raise RuntimeError(ret)
|
|
|
|
print('ACL finalize Successfully')
|
|
|
|
return True
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def th_inti_acl(device_id):
|
|
|
|
# 线程申请context
|
|
|
|
context, ret = acl.rt.create_context(device_id) # 显式创建一个Context
|
|
|
|
if ret:
|
|
|
|
raise RuntimeError(ret)
|
|
|
|
print('Init TH-Context Successfully')
|
|
|
|
return context
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def th_del_acl(context):
|
|
|
|
#线程释放context
|
|
|
|
ret = acl.rt.destroy_context(context) # 释放 Context
|
|
|
|
if ret:
|
|
|
|
raise RuntimeError(ret)
|
|
|
|
print('Deinit TH-Context Successfully')
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def pro_init_acl(device_id):
|
|
|
|
'''
|
|
|
|
独立进程初始化acl资源
|
|
|
|
:param device_id:
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
# '''acl初始化函数'''
|
|
|
|
ret = acl.init() # 0-成功,其它失败
|
|
|
|
if ret:
|
|
|
|
raise RuntimeError(ret)
|
|
|
|
ret = acl.rt.set_device(device_id) # 指定当前进程或线程中用于运算的Device。可以进程或线程中指定。*多设备时可以放线程*
|
|
|
|
# 在某一进程中指定Device,该进程内的多个线程可共用此Device显式创建Context(acl.rt.create_context接口)。
|
|
|
|
if ret:
|
|
|
|
raise RuntimeError(ret)
|
|
|
|
print('ACL init Device Successfully')
|
|
|
|
#显示创建context
|
|
|
|
context, ret = acl.rt.create_context(device_id) # 显式创建一个Context
|
|
|
|
if ret:
|
|
|
|
raise RuntimeError(ret)
|
|
|
|
print('Init TH-Context Successfully')
|
|
|
|
return context
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def pro_del_acl(device_id,context):
|
|
|
|
'''
|
|
|
|
独立进程反初始化acl资源
|
|
|
|
:param device_id:
|
|
|
|
:param context:
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
# 释放context
|
|
|
|
if context:
|
|
|
|
ret = acl.rt.destroy_context(context) # 释放 Context
|
|
|
|
if ret:
|
|
|
|
raise RuntimeError(ret)
|
|
|
|
#acl去初始化
|
|
|
|
ret = acl.rt.reset_device(device_id) # 释放Device
|
|
|
|
if ret:
|
|
|
|
raise RuntimeError(ret)
|
|
|
|
|
|
|
|
ret = acl.finalize() # 去初始化 0-成功,其它失败 --官方文档不建议放析构函数中执行
|
|
|
|
if ret:
|
|
|
|
raise RuntimeError(ret)
|
|
|
|
print('ACL finalize Successfully')
|
|
|
|
return True
|