#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include "acl_wrapper_basic.h"

// 定义内存分配策略常量,对应ACL中的aclrtMemMallocPolicy枚举值
#define ACL_WRAPPER_MEM_MALLOC_HUGE_FIRST      0
#define ACL_WRAPPER_MEM_MALLOC_HUGE_ONLY       1
#define ACL_WRAPPER_MEM_MALLOC_NORMAL_ONLY     2
#define ACL_WRAPPER_MEM_MALLOC_HUGE_FIRST_P2P  3
#define ACL_WRAPPER_MEM_MALLOC_HUGE_ONLY_P2P   4
#define ACL_WRAPPER_MEM_MALLOC_NORMAL_ONLY_P2P 5
#define ACL_WRAPPER_MEM_MALLOC_HUGE1G_ONLY     6
#define ACL_WRAPPER_MEM_MALLOC_HUGE1G_ONLY_P2P 7

int main() {
    printf("Testing basic ACL wrapper layer\n");

    // 初始化ACL
    int ret = acl_wrapper_init(NULL);
    if (ret != 0) {
        printf("Failed to initialize ACL, ret=%d\n", ret);
        return -1;
    }
    printf("ACL initialized successfully\n");

    // 获取设备数量
    uint32_t device_count = 0;
    ret = acl_wrapper_get_device_count(&device_count);
    if (ret != 0) {
        printf("Failed to get device count, ret=%d\n", ret);
        acl_wrapper_finalize();
        return -1;
    }
    printf("Device count: %u\n", device_count);

    if (device_count > 0) {
        // 设置设备ID
        ret = acl_wrapper_set_device(0);
        if (ret != 0) {
            printf("Failed to set device, ret=%d\n", ret);
            acl_wrapper_finalize();
            return -1;
        }
        printf("Device set successfully\n");

        // 分配主机内存
        void *host_buffer = acl_wrapper_rt_malloc_host(1024 * sizeof(float));
        if (host_buffer == NULL) {
            printf("Failed to allocate host memory\n");
        } else {
            printf("Successfully allocated host memory\n");
            
            // 分配设备内存
            void *device_buffer = NULL;
            uint64_t buffer_size = 1024 * sizeof(float);
            ret = acl_wrapper_rt_malloc(&device_buffer, buffer_size, ACL_WRAPPER_MEM_MALLOC_NORMAL_ONLY);
            if (ret != 0 || device_buffer == NULL) {
                printf("Failed to allocate device memory, ret=%d\n", ret);
            } else {
                printf("Successfully allocated device memory, size=%lu bytes\n", (unsigned long)buffer_size);
                
                // 内存填充
                ret = acl_wrapper_rt_memset(device_buffer, buffer_size, 0, buffer_size);
                if (ret != 0) {
                    printf("Failed to memset device memory, ret=%d\n", ret);
                } else {
                    printf("Device memory filled successfully\n");
                }
                
                // 释放设备内存
                acl_wrapper_rt_free(device_buffer);
            }
            
            // 释放主机内存
            acl_wrapper_rt_free_host(host_buffer);
        }
    }

    // 清理ACL资源
    acl_wrapper_finalize();
    printf("ACL resources cleaned up\n");

    printf("Basic ACL wrapper test completed\n");
    return 0;
}