檢查活動協調器資源支援

此範例示範應用程式 如何使用IsActivityCoordinatorResourceSupported ,在建立原則之前偵測對資源的支援。 您可以使用 IsActivityCoordinatorResourceSupported 來檢查執行時間所支援 的任何 ACTIVITY_COORDINATOR_RESOURCE。

檢查 NPU 支援範例

下列範例示範如何使用資源的新功能檢查 API 來檢查 NPU 支援。 此 API 可讓應用程式在執行時間檢查支援的資源類型,並在目前系統上支援 NPU 資源時傳回 true 。 本範例會建立活動協調器原則,並在支援時將 NPU 資源條件設定為 ACTIVITY_COORDINATOR_CONDITION_GOOD

#include <Windows.h>
#include <ActivityCoordinator.h>
#include <wil/resource.h>
#include <apiquery2.h>

// Declare RAII wrappers for the Activity Coordinator policy and subscription.
// These behave like traditional smart pointers and will call their associated
// API cleanup functions when they go out of scope.
using unique_policy = wil::unique_any<
          ACTIVITY_COORDINATOR_POLICY,
          decltype(&DestroyActivityCoordinatorPolicy),
          DestroyActivityCoordinatorPolicy>;

bool
CanUseNpuPolicy()
{
    // Check that the Activity Coordinator feature check API is implemented
    // before calling.
    if (IsApiSetImplemented("ext-ms-win-resourcemanager-activitycoordinator-l1-1-1")) {
        if (IsActivityCoordinatorResourceSupported(ACTIVITY_COORDINATOR_RESOURCE_NPU)) {
            return true;
        }
    }

    return false;
}

int
__cdecl
wmain()
{
    unique_policy policy;

    // Activity Coordinator support for NPUs does not indicate their actual
    // presence on the system. Applications must still detect and target desired
    // hardware using their API or framework of choice.
    //
    // When using system resources not supported by Activity Coordinator, it is
    // recommended that policies always include USER_IDLE in the GOOD condition.
    // This will help minimize the potential for interference with a user's
    // foreground applications utilizing the same resource.
    //
    // The GOOD policy template covers this scenario in addition to other
    // resources that most applications are likely to affect even when targeting
    // dedicated hardware.
    RETURN_IF_FAILED(CreateActivityCoordinatorPolicy(
        ACTIVITY_COORDINATOR_POLICY_TEMPLATE_GOOD,
        &policy));

    if (CanUseNpuPolicy()) {
        RETURN_IF_FAILED(SetActivityCoordinatorPolicyResourceCondition(
            policy.get(),
            ACTIVITY_COORDINATOR_RESOURCE_NPU,
            ACTIVITY_COORDINATOR_CONDITION_GOOD));
    }

    // Proceed to use Activity Coordinator to run opportunistic work. See the
    // example project in the documentation for further details.

    return S_OK;
}

另請參閱