/**
 * 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.
 */

/* Generated By CANNBot */

/*!
 * \file tan_tiling.cpp
 * \brief tan tiling implementation
 */

#include "log/log.h"
#include "util/math_util.h"
#include "util/platform_util.h"
#include "op_host/tiling_base_util.h"
#include "op_host/math_tiling_templates_registry.h"
#include "../../op_kernel/arch35/tan_tiling_data.h"
#include "../../op_kernel/arch35/tan_tiling_key.h"

namespace optiling {


constexpr uint32_t WS_SYS_SIZE = 0U;
constexpr int32_t THREAD_NUM = 512;
constexpr int32_t MIN_ELEMENTS_PER_THREAD = 4;

struct TanCompileInfo {};

static ge::graphStatus GetPlatformInfo(gert::TilingContext* context, int64_t& coreNum)
{
    fe::PlatFormInfos* platformInfoPtr = context->GetPlatformInfo();
    OP_CHECK_NULL_WITH_CONTEXT(context, platformInfoPtr);
    auto ascendcPlatform = platform_ascendc::PlatformAscendC(platformInfoPtr);
    coreNum = ascendcPlatform.GetCoreNumAiv();
    OP_CHECK_IF(coreNum == 0, OP_LOGE(context, "coreNum is 0"), return ge::GRAPH_FAILED);
    return ge::GRAPH_SUCCESS;
}

static ge::graphStatus TanTilingFunc(gert::TilingContext* context)
{
    // 1. 动态获取平台核数
    int64_t coreNum = 0;
    OP_CHECK_IF(GetPlatformInfo(context, coreNum) != ge::GRAPH_SUCCESS,
        OP_LOGE(context, "GetPlatformInfo error"), return ge::GRAPH_FAILED);

    // 2. 获取输入 shape 和 dtype
    auto inputShape = context->GetInputShape(0);
    OP_CHECK_NULL_WITH_CONTEXT(context, inputShape);
    const gert::Shape shape = inputShape->GetShape();
    int64_t totalElements = 1;
    for (size_t i = 0; i < shape.GetDimNum(); i++) {
        totalElements *= shape.GetDim(i);
    }

    auto inputDesc = context->GetInputDesc(0);
    OP_CHECK_NULL_WITH_CONTEXT(context, inputDesc);
    auto dataType = inputDesc->GetDataType();

    // 3. 计算核切分
    int64_t perCoreElements = THREAD_NUM * MIN_ELEMENTS_PER_THREAD;
    int32_t needCoreNum = static_cast<int32_t>((totalElements + perCoreElements - 1) / perCoreElements);
    if (needCoreNum > static_cast<int32_t>(coreNum)) {
        needCoreNum = static_cast<int32_t>(coreNum);
    }

    // 4. 设置 tiling 数据
    TanTilingData* tiling = context->GetTilingData<TanTilingData>();
    OP_CHECK_NULL_WITH_CONTEXT(context, tiling);
    tiling->totalElements = totalElements;
    tiling->perCoreElements = perCoreElements;
    tiling->needCoreNum = needCoreNum;

    // 5. 设置 dtype 标识
    int32_t dataTypeId = 0;
    if (dataType == ge::DT_FLOAT16) {
        dataTypeId = 0;
    } else if (dataType == ge::DT_FLOAT) {
        dataTypeId = 1;
    } else if (dataType == ge::DT_BF16) {
        dataTypeId = 2;
    } else if (dataType == ge::DT_INT32) {
        dataTypeId = 3;
    } else {
        OP_LOGE(context, "unsupported dtype for Tan");
        return ge::GRAPH_FAILED;
    }
    tiling->dataType = dataTypeId;

    // 6. 设置 blockDim 和 tiling key
    context->SetBlockDim(needCoreNum);
    context->SetLocalMemorySize(128 * 1024);  // 128KB

    uint64_t tilingKey = 0;
    if (dataTypeId == 0) {
        tilingKey = GET_TPL_TILING_KEY(TAN_TPL_SCH_MODE_0);
    } else if (dataTypeId == 1) {
        tilingKey = GET_TPL_TILING_KEY(TAN_TPL_SCH_MODE_1);
    } else if (dataTypeId == 2) {
        tilingKey = GET_TPL_TILING_KEY(TAN_TPL_SCH_MODE_2);
    } else if (dataTypeId == 3) {
        tilingKey = GET_TPL_TILING_KEY(TAN_TPL_SCH_MODE_3);
    }
    context->SetTilingKey(tilingKey);

    // 7. 设置 workspace
    size_t* currentWorkspace = context->GetWorkspaceSizes(1);
    OP_CHECK_NULL_WITH_CONTEXT(context, currentWorkspace);
    currentWorkspace[0] = WS_SYS_SIZE;

    return ge::GRAPH_SUCCESS;
}

static ge::graphStatus TilingParseForTan([[maybe_unused]] gert::TilingParseContext* context)
{
    return ge::GRAPH_SUCCESS;
}

IMPL_OP_OPTILING(Tan).Tiling(TanTilingFunc).TilingParse<TanCompileInfo>(TilingParseForTan);

} // namespace optiling