#include <iostream>
#include <vector>
#include <getopt.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/wait.h>
#include <dlfcn.h>
#include <cstdio>
#include <string>
#include <iomanip>
#include <string.h>
#include <list>
#include <algorithm>
#include <stdarg.h>
#include "acl/acl.h"
#include "aclnn/aclnn_base.h"
#include "acl/acl_base.h"
#include "acl/acl_rt.h"
#include "sec_common.h"
#include "utils.h"

#include "../fuzzlib/secodeFuzz.h"
#include "aclnnop/aclnn_cdist.h"

using namespace std;


static const char* GetNonContigSceneName(int scene) {
    switch(scene) {
        case 0: return "CONTINUOUS";
        case 1: return "BROADCAST";
        case 2: return "SLICE";
        case 3: return "ASSTRIDED";
        case 4: return "STORAGE_PADDED";
        case 5: return "TRANSPOSE";
        default: return "UNKNOWN";
    }
}

static std::string ShapeToStr(const std::vector<int64_t>& s) {
    std::string r = "[";
    for (size_t k = 0; k < s.size(); k++) {
        if (k) r += ",";
        r += std::to_string(s[k]);
    }
    return r + "]";
}
int aclnnCdist_Normal(int fuzz_num, aclrtContext context)
{
    auto ret = aclrtSetCurrentContext(context);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCdist aclrtSetCurrentContext failed. ERROR: %d\n", ret); return ret);
    aclrtStream stream;
    ret = aclrtCreateStream(&stream);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCdist aclrtCreateStream failed. ERROR: %d\n", ret); return ret);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
    WriteDTLog("aclnnCdist", "aclnnCdist multithread op fuzz start \n");
    const long long kSingleInputSizeMax = 1073741824LL;
    const long long kTotalInputSizeMax = 2147483648LL;

    // dtype enums
    int dtype0Enum[] = { 0, 1, 27 };
    int dtype0Num = sizeof(dtype0Enum) / sizeof(dtype0Enum[0]);

    aclrtSetOpExecuteTimeOut(1091);
    DT_Set_TimeOut_Second(3000);
    int random_int = aclnn_multithread_RANDOM_INT(1, 99999999);
    DT_FUZZ_START(random_int, fuzz_num, "aclnnCdist", 0)
    {
        int i = 0;
        int shapeDimMax = 16;
        int aclnnRet = -1;  // aclnn 实际调用返回值(成功判定依据)
        void* x1DeviceAddr = nullptr;
        aclTensor* x1 = nullptr;
        void* x2DeviceAddr = nullptr;
        aclTensor* x2 = nullptr;
        void* outDeviceAddr = nullptr;
        aclTensor* out = nullptr;
        uint64_t workspaceSize = 0;
        aclOpExecutor* executor;
        void* workspaceAddr = nullptr;

        LOG_PRINT("aclnnCdist_multithread %d\n", fuzzi);
        printf("[FUZZ_LOG] plog_file: aclnnCdist_multithread_fuzz_plog_case_%d.log\n", fuzzi);
        aclnnRet = -1;
        try
        {
        // DT mutation data
            int sizeBucket = *(int *)DTSetGetNumberRange(&g_Element[i++], 1, 0, 3, "i");
            int dim_num0 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 2, 8, "i");
            {
                const long long sizeTargets[4] = {1000LL, 1048576LL, 4194304LL, 8388608LL};
                int nTensors = 3;
                long long target = sizeTargets[sizeBucket] / nTensors;
                if (target < 1) { target = 1; }
                int nd = (dim_num0 > 0) ? dim_num0 : 1;
                long long perDim = (long long)pow((double)target, 1.0 / nd);
                if (perDim < 1) { perDim = 1; }
                if (perDim > MAX_SHAPE_SIZE) { perDim = MAX_SHAPE_SIZE; }
                shapeDimMax = (int)perDim;
            }
            int shape1 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, shapeDimMax, "i");
            int shape2 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, shapeDimMax, "i");
            int shape3 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, shapeDimMax, "i");
            int shape4 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, shapeDimMax, "i");
            int shape5 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, shapeDimMax, "i");
            int shape6 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, shapeDimMax, "i");
            int shape7 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, shapeDimMax, "i");
            int shape8 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, shapeDimMax, "i");
            auto dtype0 = (aclDataType)*(int *)DT_SetGetNumberEnumV3(i++, aclDataType::ACL_FLOAT, dtype0Enum, dtype0Num);
            float p = *(float *)DTSetGetNumberRangeFloat(&g_Element[i++], 1.0f, -100.0f, 100.0f, "i");
            int64_t compute_mode = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 1, -100, 100, "i");
            int dim_num1 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 0, 8, "i");
            std::vector<int64_t> x1Shape = {};
            if (dim_num0 == 1) {
                x1Shape = {shape1};
            } else if (dim_num0 == 2) {
                x1Shape = {shape1, shape2};
            } else if (dim_num0 == 3) {
                x1Shape = {shape1, shape2, shape3};
            } else if (dim_num0 == 4) {
                x1Shape = {shape1, shape2, shape3, shape4};
            } else if (dim_num0 == 5) {
                x1Shape = {shape1, shape2, shape3, shape4, shape5};
            } else if (dim_num0 == 6) {
                x1Shape = {shape1, shape2, shape3, shape4, shape5, shape6};
            } else if (dim_num0 == 7) {
                x1Shape = {shape1, shape2, shape3, shape4, shape5, shape6, shape7};
            } else if (dim_num0 == 8) {
                x1Shape = {shape1, shape2, shape3, shape4, shape5, shape6, shape7, shape8};
            }
            std::vector<int64_t> x2Shape = x1Shape;
            std::vector<int64_t> outShape = x1Shape;
            if (outShape.size() >= 2 && x2Shape.size() >= 2) {
                outShape[outShape.size() - 1] = x2Shape[x2Shape.size() - 2];
            }
            long long shape_size = GetShapeSize(outShape);
            if (shape_size > kSingleInputSizeMax) { printf("shape_size too big\n"); goto ERROR; }
            NonContigParams ncX1;
            ncX1.scene = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 0, 5, "i");
            ncX1.viewShape = x1Shape;
            ncX1.sliceStep = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 2, 2, 5, "i");
            ncX1.sliceAxis = 0;
            ncX1.offset = 0;
            ncX1.pos1 = *(int *)DTSetGetNumberRange(&g_Element[i++], 1, 1, 4, "i");
            ncX1.pos2 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, 4, "i");
            ncX1.strides.resize(x1Shape.size());
            for (size_t j = 0; j < x1Shape.size(); j++) {
                ncX1.strides[j] = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 2, 1, 20, "i");
            }
            CalcNonContiguousParams(ncX1);
            long long x1StorageSize = GetShapeSize(ncX1.storageShape);
            printf("[NON_CONTIG] ncX1 scene=%s(%d), originalShape=[", GetNonContigSceneName(ncX1.scene), ncX1.scene);
            for (int _i = 0; _i < (int)x1Shape.size(); _i++) printf("%lld%s", (long long)x1Shape[_i], _i < (int)x1Shape.size() - 1 ? "," : "");
            printf("], viewShape=[");
            for (int _i = 0; _i < (int)ncX1.viewShape.size(); _i++) printf("%lld%s", (long long)ncX1.viewShape[_i], _i < (int)ncX1.viewShape.size() - 1 ? "," : "");
            printf("], viewStrides=[");
            for (int _i = 0; _i < (int)ncX1.strides.size(); _i++) printf("%lld%s", (long long)ncX1.strides[_i], _i < (int)ncX1.strides.size() - 1 ? "," : "");
            printf("], storageShape=[");
            for (int _i = 0; _i < (int)ncX1.storageShape.size(); _i++) printf("%lld%s", (long long)ncX1.storageShape[_i], _i < (int)ncX1.storageShape.size() - 1 ? "," : "");
            printf("], offset=%lld\n", (long long)ncX1.offset);
            if (dtype0 == aclDataType::ACL_FLOAT) {
                std::vector<float> x1HostData(GetShapeSize(x1Shape), 1);
                ret = CreateAclTensorByScene(x1HostData, ncX1, &x1DeviceAddr, dtype0, ACL_FORMAT_ND, &x1);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (dtype0 == aclDataType::ACL_FLOAT16) {
                std::vector<int16_t> x1HostData(GetShapeSize(x1Shape), 1);
                ret = CreateAclTensorByScene(x1HostData, ncX1, &x1DeviceAddr, dtype0, ACL_FORMAT_ND, &x1);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (dtype0 == aclDataType::ACL_BF16) {
                std::vector<int16_t> x1HostData(GetShapeSize(x1Shape), 1);
                ret = CreateAclTensorByScene(x1HostData, ncX1, &x1DeviceAddr, dtype0, ACL_FORMAT_ND, &x1);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            }
            NonContigParams ncX2;
            ncX2.scene = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 0, 5, "i");
            ncX2.viewShape = x1Shape;
            ncX2.sliceStep = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 2, 2, 5, "i");
            ncX2.sliceAxis = 0;
            ncX2.offset = 0;
            ncX2.pos1 = *(int *)DTSetGetNumberRange(&g_Element[i++], 1, 1, 4, "i");
            ncX2.pos2 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, 4, "i");
            ncX2.strides.resize(x1Shape.size());
            for (size_t j = 0; j < x1Shape.size(); j++) {
                ncX2.strides[j] = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 2, 1, 20, "i");
            }
            CalcNonContiguousParams(ncX2);
            long long x2StorageSize = GetShapeSize(ncX2.storageShape);
            printf("[NON_CONTIG] ncX2 scene=%s(%d), originalShape=[", GetNonContigSceneName(ncX2.scene), ncX2.scene);
            for (int _i = 0; _i < (int)x1Shape.size(); _i++) printf("%lld%s", (long long)x1Shape[_i], _i < (int)x1Shape.size() - 1 ? "," : "");
            printf("], viewShape=[");
            for (int _i = 0; _i < (int)ncX2.viewShape.size(); _i++) printf("%lld%s", (long long)ncX2.viewShape[_i], _i < (int)ncX2.viewShape.size() - 1 ? "," : "");
            printf("], viewStrides=[");
            for (int _i = 0; _i < (int)ncX2.strides.size(); _i++) printf("%lld%s", (long long)ncX2.strides[_i], _i < (int)ncX2.strides.size() - 1 ? "," : "");
            printf("], storageShape=[");
            for (int _i = 0; _i < (int)ncX2.storageShape.size(); _i++) printf("%lld%s", (long long)ncX2.storageShape[_i], _i < (int)ncX2.storageShape.size() - 1 ? "," : "");
            printf("], offset=%lld\n", (long long)ncX2.offset);
            if (dtype0 == aclDataType::ACL_FLOAT) {
                std::vector<float> x2HostData(GetShapeSize(x1Shape), 1);
                ret = CreateAclTensorByScene(x2HostData, ncX2, &x2DeviceAddr, dtype0, ACL_FORMAT_ND, &x2);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (dtype0 == aclDataType::ACL_FLOAT16) {
                std::vector<int16_t> x2HostData(GetShapeSize(x1Shape), 1);
                ret = CreateAclTensorByScene(x2HostData, ncX2, &x2DeviceAddr, dtype0, ACL_FORMAT_ND, &x2);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (dtype0 == aclDataType::ACL_BF16) {
                std::vector<int16_t> x2HostData(GetShapeSize(x1Shape), 1);
                ret = CreateAclTensorByScene(x2HostData, ncX2, &x2DeviceAddr, dtype0, ACL_FORMAT_ND, &x2);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            }
            NonContigParams ncOut;
            ncOut.scene = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 0, 5, "i");
            ncOut.viewShape = outShape;
            ncOut.sliceStep = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 2, 2, 5, "i");
            ncOut.sliceAxis = 0;
            ncOut.offset = 0;
            ncOut.pos1 = *(int *)DTSetGetNumberRange(&g_Element[i++], 1, 1, 4, "i");
            ncOut.pos2 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, 4, "i");
            ncOut.strides.resize(outShape.size());
            for (size_t j = 0; j < outShape.size(); j++) {
                ncOut.strides[j] = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 2, 1, 20, "i");
            }
            CalcNonContiguousParams(ncOut);
            long long outStorageSize = GetShapeSize(ncOut.storageShape);
            printf("[NON_CONTIG] ncOut scene=%s(%d), originalShape=[", GetNonContigSceneName(ncOut.scene), ncOut.scene);
            for (int _i = 0; _i < (int)outShape.size(); _i++) printf("%lld%s", (long long)outShape[_i], _i < (int)outShape.size() - 1 ? "," : "");
            printf("], viewShape=[");
            for (int _i = 0; _i < (int)ncOut.viewShape.size(); _i++) printf("%lld%s", (long long)ncOut.viewShape[_i], _i < (int)ncOut.viewShape.size() - 1 ? "," : "");
            printf("], viewStrides=[");
            for (int _i = 0; _i < (int)ncOut.strides.size(); _i++) printf("%lld%s", (long long)ncOut.strides[_i], _i < (int)ncOut.strides.size() - 1 ? "," : "");
            printf("], storageShape=[");
            for (int _i = 0; _i < (int)ncOut.storageShape.size(); _i++) printf("%lld%s", (long long)ncOut.storageShape[_i], _i < (int)ncOut.storageShape.size() - 1 ? "," : "");
            printf("], offset=%lld\n", (long long)ncOut.offset);
            if (dtype0 == aclDataType::ACL_FLOAT) {
                std::vector<float> outHostData(GetShapeSize(outShape), 1);
                ret = CreateAclTensorByScene(outHostData, ncOut, &outDeviceAddr, dtype0, ACL_FORMAT_ND, &out);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (dtype0 == aclDataType::ACL_FLOAT16) {
                std::vector<int16_t> outHostData(GetShapeSize(outShape), 1);
                ret = CreateAclTensorByScene(outHostData, ncOut, &outDeviceAddr, dtype0, ACL_FORMAT_ND, &out);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (dtype0 == aclDataType::ACL_BF16) {
                std::vector<int16_t> outHostData(GetShapeSize(outShape), 1);
                ret = CreateAclTensorByScene(outHostData, ncOut, &outDeviceAddr, dtype0, ACL_FORMAT_ND, &out);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            }

            ret = aclnnCdistGetWorkspaceSize(x1, x2, p, compute_mode, out, &workspaceSize, &executor);
printf("[FUZZ_PARAM] x1: dtype=%d, shape=%s; x2: dtype=%d, shape=%s; out: dtype=%d, shape=%s; p=%f; compute_mode=%lld\n", (int)dtype0, ShapeToStr(x1Shape).c_str(), (int)dtype0, ShapeToStr(x2Shape).c_str(), (int)dtype0, ShapeToStr(outShape).c_str(), (double)p, (long long)compute_mode);
            CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCdistGetWorkspaceSize failed. ERROR: %d\n", ret); goto ERROR);
            if (workspaceSize > 0) {
                ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
                CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); goto ERROR);
            }
            ret = aclnnCdist(workspaceAddr, workspaceSize, executor, stream);
            aclnnRet = (int)ret;
            if (ret == ACL_SUCCESS) { printf("aclnnCdist execute success :%d\n", (int)ret); }
            else { printf("aclnnCdist execute failed :%d\n", (int)ret); goto ERROR; }
            ret = aclrtSynchronizeStream(stream);
            CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); goto ERROR);
            LOG_PRINT("aclnnCdist execute success : %d\n", ret);
        }
        catch (std::exception& e)
        {
            printf("catch exception %s\n", e.what());
            printf("[FUZZ_RESULT] case_%d: faild, ret=%d\n", fuzzi, (int)ret);
        }

ERROR:
        ret = aclrtSynchronizeStream(stream);
        if (aclnnRet == -1) { printf("[FUZZ_RESULT] case_%d: skip, ret=-1\n", fuzzi); }
        else { printf("[FUZZ_RESULT] case_%d: %s, ret=%d\n", fuzzi, (aclnnRet == 0) ? "success" : "faild", aclnnRet); }
        printf("end \n");
        CheckAndLog(ret);
        pid_t pid = getpid();
        std::string cmd = "grep -rinE 'ai(vec|core) error' /root/ascend/log/debug/plog/ ";
        printf("system return value %d\n", CheckResult(pid));
        if (x1) { aclDestroyTensor(x1); }
        if (x2) { aclDestroyTensor(x2); }
        if (out) { aclDestroyTensor(out); }
        if (x1DeviceAddr) { aclrtFree(x1DeviceAddr); }
        if (x2DeviceAddr) { aclrtFree(x2DeviceAddr); }
        if (outDeviceAddr) { aclrtFree(outDeviceAddr); }
        if (workspaceSize > 0 && workspaceAddr) { aclrtFree(workspaceAddr); }
    }
    DT_FUZZ_END()
    WriteDTLog("aclnnCdist", "aclnnCdist multithread op fuzz total number:%u, para error number %u, execute error number %u \n", fuzz_num, aclnn_INVALID_Para, aclnn_Inner_Error + Runtime_Error);
    WriteDTLog("aclnnCdist", "aclnnCdist multithread op fuzz end \n");
    aclrtDestroyStream(stream);
    return 0;
}

extern "C" int aclnnCdist_fuzz(int argc, char *argv[], aclrtContext context);
int aclnnCdist_fuzz(int argc, char *argv[], aclrtContext context)
{
    int fuzz_num = MAXFUZZNUMBER;
    if (argc < 1) {
        cout << "[ERROR] input arg num must be bigger than 1 \n" << endl;
        return -1;
    } else if (argc > 1) {
        fuzz_num = atoi(argv[2]);
    }
    if (argc == 4) { deviceId = atoi(argv[3]); }
    printf("fuzz_num %d \n", fuzz_num);
    printf("aclnnCdist_non_contig :%d \n", aclnnCdist_Normal(fuzz_num, context));
    return 0;
}