---
title: 函数：reset_device_force
description: "| 产品 | 是否支持 |"
url: https://www.hiascend.com/document/detail/zh/canncommercial/latest/API/runtimeapi/aclpythondevg_01_1024.html
sourcePath: /source/zh/canncommercial/900/API/runtimeapi/aclpythondevg_01_1024.html
indexId: 1888402c6793755ed30304c5ff52ee588d7b7c9fcf9b238434844dd96f3e832371
---
# 函数：reset_device_force

#### 产品支持情况

| 产品 | 是否支持 |
| --- | --- |
| Atlas 350 加速卡 | √ |
| Atlas A3 训练系列产品 / Atlas A3 推理系列产品 | √ |
| Atlas A2 训练系列产品 / Atlas A2 推理系列产品 | √ |
| Atlas 训练系列产品 | √ |
| Atlas 推理系列产品 | √ |
| Atlas 200I/500 A2 推理产品 | √ |


#### 功能说明

复位当前运算的Device，释放Device上的资源。释放的资源包括默认Context、默认Stream以及默认Context下创建的所有Stream。若默认Context或默认Stream下的任务还未完成，系统会等待任务完成后再释放。

acl.rt.reset_device_force接口可与acl.rt.set_device接口配对使用，也可不与aclrtSetDevice接口配对使用，若不配对使用，一个进程中，针对同一个Device，调用一次或多次acl.rt.set_device接口后，仅需调用一次本接口可释放Device上的资源。

```
# 与acl.rt.set_device接口配对使用：
acl.rt.set_device(1) -> acl.rt.reset_device_force(1) -> acl.rt.set_device(1) -> acl.rt.reset_device_force(1)
# 与acl.rt.set_device接口不配对使用：
acl.rt.set_device(1) -> acl.rt.set_device(1) -> acl.rt.reset_device_force(1)
```


#### 函数原型

- C函数原型
  1 aclError aclrtResetDeviceForce(int32_t deviceId)

- python函数
  1 ret=acl.rt.reset_device_force(device_id)


#### 参数说明

| 参数名 | 说明 |
| --- | --- |
| device\_id | int，Device设备号。 |


#### 返回值说明

| 返回值 | 说明 |
| --- | --- |
| ret | int，错误码，返回0表示成功，返回其它值表示失败。 |


#### 约束说明

- 多线程场景下，针对同一个Device，如果每个线程中都调用  acl.rt.set_device
接口、acl.rt.reset_device_force接口，如下所示，线程2中的acl.rt.reset_device_force接口会返回报错，因为线程1中acl.rt.reset_device_force接口已经释放了Device 1的资源：

```
时间线 ------------------------------------------------------------------------------>
线程1：acl.rt.set_device(1)  acl.rt.reset_device_force(1)
线程2：acl.rt.set_device(1)                                acl.rt.reset_device_force(1)
```


  多线程场景下，正确方式是应在线程执行的最后，调用一次acl.rt.reset_device_force释放Device资源，如下所示：

```
时间线 ------------------------------------------------------------------------------>
线程1：acl.rt.set_device(1)
线程2：acl.rt.set_device(1)                                acl.rt.reset_device_force(1)
```


-   acl.rt.reset_device
接口与acl.rt.reset_device_force接口可以混用，但混用时，若两个Reset接口的调用次数、调用顺序不对，接口会返回报错。

```
# 混用时的正确方式：
# 两个Reset接口都分别与Set接口配对使用，且acl.rt.reset_device_force接口在acl.rt.reset_device接口之后
acl.rt.set_device(1) -> acl.rt.reset_device(1) -> acl.rt.set_device(1) -> acl.rt.reset_device_force(1)
acl.rt.set_device(1) -> acl.rt.set_device(1) -> acl.rt.reset_device(1) -> acl.rt.reset_device_force(1)
# 混用时的错误方式：
# acl.rt.reset_device接口内部涉及引用计数的实现，当acl.rt.reset_device接口每被调用一次，则该引用计数减1，
# 当引用计数减到0时，会真正释放Device上的资源，此时再调用acl.rt.reset_device或acl.rt.reset_device_force接口都会报错
acl.rt.set_device(1) -> acl.rt.set_device(1) -> acl.rt.reset_device(1) -> acl.rt.reset_device(1) -> acl.rt.reset_device_force(1)
acl.rt.set_device(1) -> acl.rt.set_device(1) -> acl.rt.reset_device(1) -> acl.rt.reset_device_force(1) -> acl.rt.reset_device_force(1)
# acl.rt.reset_device_force接口在acl.rt.reset_device接口之后，否则接口返回报错
acl.rt.set_device(1) -> acl.rt.set_device(1) -> acl.rt.reset_device_force(1) -> acl.rt.reset_device(1)
```
