* Copyright (c) 2025 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 <thread>
#include <iostream>
#include "utils.h"
#include "acl/acl.h"
#include "callback_utils.h"
#include "callback_sample.h"
using namespace std;
extern void LongOP(uint32_t coreDim, void *stream, uint32_t* x);
aclrtContext CallBackSpace::CallBackSample::context_ = nullptr;
aclrtStream CallBackSpace::CallBackSample::stream_ = nullptr;
int32_t CallBackSpace::CallBackSample::deviceId_ = 0;
CallBackSpace::CallBackSample::CallBackSample() = default;
CallBackSpace::CallBackSample::~CallBackSample()
{
(void)Destroy();
}
int CallBackSpace::CallBackSample::Init()
{
CHECK_ERROR(aclInit(nullptr));
CHECK_ERROR(aclrtSetDevice(deviceId_));
CHECK_ERROR(aclrtCreateContext(&context_, deviceId_));
CHECK_ERROR(aclrtCreateStream(&stream_));
CHECK_ERROR(aclrtSetStreamFailureMode(stream_, ACL_STOP_ON_FAILURE));
}
void CallBackSpace::CallBackSample::ThreadFunc(void *arg)
{
const int waitTime = 100;
aclrtSetCurrentContext(context_);
while (CallbackUtils::IsLoopFlag(arg)) {
aclrtProcessReport(waitTime);
}
INFO_LOG("Thread exit");
}
void CallBackSpace::CallBackSample::CallBackBeforeLaunchFunc(void *arg)
{
thread::id tid = std::this_thread::
get_id();
string tidStr = CallbackUtils::GetThreadId(tid);
INFO_LOG("Callback thread id is %s", tidStr.c_str());
int *data = static_cast<int*>(arg);
INFO_LOG("This callback before task, result: user data is: %d.", *data);
}
void CallBackSpace::CallBackSample::CallBackFunc(void *arg)
{
thread::id tid = std::this_thread::get_id();
string tidStr = CallbackUtils::GetThreadId(tid);
INFO_LOG("Callback thread id is %s", tidStr.c_str());
int *data = static_cast<int*>(arg);
INFO_LOG("This callback after task and loop five times, result: user data is: %d.", *data);
}
int CallBackSpace::CallBackSample::Callback()
{
uint32_t num = 0;
int blockDim = 1;
bool isLoop = true;
size_t size = sizeof(uint32_t);
uint32_t *numDevice = nullptr;
const int count = 5;
thread::id tid;
CHECK_ERROR(aclrtMalloc((void **)&numDevice, size, ACL_MEM_MALLOC_HUGE_FIRST));
CHECK_ERROR(aclrtMemcpy(numDevice, size, &num, size, ACL_MEMCPY_HOST_TO_DEVICE));
thread td(ThreadFunc, &isLoop);
tid = std::this_thread::get_id();
string mainThreadId = CallbackUtils::GetThreadId(tid);
INFO_LOG("The main thread id is %s", mainThreadId.c_str());
tid = td.get_id();
string createdThreadId = CallbackUtils::GetThreadId(tid);
INFO_LOG("The created thread id is %s", createdThreadId.c_str());
int *userData = new int(520);
uint64_t tidInt = std::stoull(createdThreadId);
CHECK_ERROR(aclrtSubscribeReport(static_cast<uint64_t>(tidInt), stream_));
CHECK_ERROR(aclrtLaunchCallback(CallBackBeforeLaunchFunc, userData, ACL_CALLBACK_BLOCK, stream_));
LongOP(blockDim, stream_, numDevice);
INFO_LOG("After begin a task, launch five callback.");
for (int i = 0; i < count; i++) {
CHECK_ERROR(aclrtLaunchCallback(CallBackFunc, userData, ACL_CALLBACK_BLOCK, stream_));
}
CHECK_ERROR(aclrtSynchronizeStream(stream_));
CHECK_ERROR(aclrtMemcpy(&num, size, numDevice, size, ACL_MEMCPY_DEVICE_TO_HOST));
INFO_LOG("After assigning the task, the current int is: %d.", num);
isLoop = false;
td.join();
CHECK_ERROR(aclrtUnSubscribeReport(static_cast<uint64_t>(tidInt), stream_));
CHECK_ERROR(aclrtFree(numDevice));
delete userData;
}
int CallBackSpace::CallBackSample::Destroy()
{
CHECK_ERROR(aclrtDestroyStreamForce(stream_));
CHECK_ERROR(aclrtDestroyContext(context_));
CHECK_ERROR(aclrtResetDeviceForce(deviceId_));
CHECK_ERROR(aclFinalize());
}