* Copyright (c) 2026 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
#include <iostream>
#include "utils.h"
#include "acl/acl.h"
#include <unistd.h>
using namespace std;
void SimulateWork(aclrtStream stream, int milliseconds)
{
usleep(milliseconds * 1000);
INFO_LOG("Work completed after %d milliseconds", milliseconds);
}
int main()
{
int32_t deviceId = 0;
aclrtStream streamA = nullptr;
aclrtStream streamB = nullptr;
aclrtContext context;
aclrtCntNotify cntNotify = nullptr;
uint32_t notifyId = 0;
CHECK_ERROR(aclInit(nullptr));
CHECK_ERROR(aclrtSetDevice(deviceId));
CHECK_ERROR(aclrtCreateContext(&context, deviceId));
CHECK_ERROR(aclrtCreateStream(&streamA));
CHECK_ERROR(aclrtCreateStream(&streamB));
INFO_LOG("Begin CntNotify sample between StreamA and StreamB");
CHECK_ERROR(aclrtCntNotifyCreate(&cntNotify, 0));
INFO_LOG("Create CntNotify successfully");
CHECK_ERROR(aclrtCntNotifyGetId(cntNotify, ¬ifyId));
INFO_LOG("Get CntNotify ID: %u", notifyId);
INFO_LOG("StreamA: Start working...");
SimulateWork(streamA, 500);
aclrtCntNotifyRecordInfo recordInfo = {};
recordInfo.mode = ACL_RT_CNT_NOTIFY_RECORD_SET_VALUE_MODE;
recordInfo.value = 1;
CHECK_ERROR(aclrtCntNotifyRecord(cntNotify, streamA, &recordInfo));
INFO_LOG("StreamA: Record CntNotify successfully");
INFO_LOG("StreamB: Waiting for CntNotify from StreamA...");
aclrtCntNotifyWaitInfo waitInfo = {};
waitInfo.mode = ACL_RT_CNT_NOTIFY_WAIT_EQUAL_MODE;
waitInfo.value = 1;
waitInfo.timeout = 2000;
waitInfo.isClear = 0;
CHECK_ERROR(aclrtCntNotifyWaitWithTimeout(cntNotify, streamB, &waitInfo));
INFO_LOG("StreamB: Wait CntNotify with timeout successfully");
INFO_LOG("StreamB: Start working after receiving CntNotify...");
SimulateWork(streamB, 300);
CHECK_ERROR(aclrtCntNotifyReset(cntNotify, streamA));
INFO_LOG("Reset CntNotify successfully");
CHECK_ERROR(aclrtSynchronizeStream(streamA));
CHECK_ERROR(aclrtSynchronizeStream(streamB));
CHECK_ERROR(aclrtCntNotifyDestroy(cntNotify));
INFO_LOG("Destroy CntNotify successfully");
CHECK_ERROR(aclrtDestroyStreamForce(streamA));
CHECK_ERROR(aclrtDestroyStreamForce(streamB));
CHECK_ERROR(aclrtDestroyContext(context));
CHECK_ERROR(aclrtResetDeviceForce(deviceId));
aclFinalize();
INFO_LOG("Resource cleanup completed.");
}