virtual_allocate.h

本文介绍了Windows应用程序中消息处理函数WndProc及对话框过程函数About的声明方式,并展示了资源标识符的定义方法,包括退出命令IDM_EXIT、测试命令IDM_TEST及关于对话框命令IDM_ABOUT。

  name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-5572165936844014&dt=1194442938015&lmt=1194190197&format=336x280_as&output=html&correlator=1194442937843&url=file%3A%2F%2F%2FC%3A%2FDocuments%2520and%2520Settings%2Flhh1%2F%E6%A1%8C%E9%9D%A2%2FCLanguage.htm&color_bg=FFFFFF&color_text=000000&color_link=000000&color_url=FFFFFF&color_border=FFFFFF&ad_type=text&ga_vid=583001034.1194442938&ga_sid=1194442938&ga_hid=1942779085&flash=9&u_h=768&u_w=1024&u_ah=740&u_aw=1024&u_cd=32&u_tz=480&u_java=true" frameborder="0" width="336" scrolling="no" height="280" allowtransparency="allowtransparency"> #define IDM_EXIT           100
#define IDM_TEST           200
#define IDM_ABOUT          301

LRESULT CALLBACK WndProc  (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About    (HWND, UINT, WPARAM, LPARAM);

/* * Vhost-user RDMA device : init and packets forwarding * * Copyright (C) 2025 KylinSoft Inc. and/or its affiliates. All rights reserved. * * Author: Xiong Weimin <xiongweimin@kylinos.cn> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. */ #include <unistd.h> #include <sys/uio.h> #include <stdlib.h> #include <rte_ethdev.h> #include <rte_spinlock.h> #include <rte_malloc.h> #include "vhost_rdma.h" #include "vhost_rdma_ib.h" #include "vhost_rdma_log.h" #include "vhost_rdma_pkt.h" void free_rd_atomic_resource(__rte_unused struct vhost_rdma_qp *qp, struct vhost_rdma_resp_res *res) { if (res->type == VHOST_ATOMIC_MASK) { rte_pktmbuf_free(res->atomic.mbuf); } else if (res->type == VHOST_READ_MASK) { if (res->read.mr) vhost_rdma_drop_ref(res->read.mr, qp->dev, mr); } res->type = 0; } void free_rd_atomic_resources(struct vhost_rdma_qp *qp) { if (qp->resp.resources) { for (int i = 0; i < qp->attr.max_dest_rd_atomic; i++) { struct vhost_rdma_resp_res *res = &qp->resp.resources[i]; free_rd_atomic_resource(qp, res); } rte_free(qp->resp.resources); qp->resp.resources = NULL; } } void vhost_rdma_queue_cleanup(struct vhost_rdma_qp *qp, struct vhost_rdma_queue* queue) { rte_intr_callback_unregister(&queue->intr_handle, queue->cb, qp); rte_free(queue->data); } void vhost_rdma_mr_cleanup(void* arg) { struct vhost_rdma_mr *mr = arg; mr->type = VHOST_MR_TYPE_NONE; } void vhost_rdma_qp_cleanup(void* arg) { struct vhost_rdma_qp *qp = arg; if (qp->scq) vhost_rdma_drop_ref(qp->scq, qp->dev, cq); if (qp->rcq) vhost_rdma_drop_ref(qp->rcq, qp->dev, cq); if (qp->pd) vhost_rdma_drop_ref(qp->pd, qp->dev, pd); if (qp->resp.mr) { vhost_rdma_drop_ref(qp->resp.mr, qp->dev, mr); qp->resp.mr = NULL; } free_rd_atomic_resources(qp); } void vhost_rdma_init_ib(struct vhost_rdma_device *dev) { uint32_t qpn; dev->attr.max_qps = 64; dev->attr.max_cqs = 64; dev->attr.max_mr_size = -1ull; dev->attr.page_size_cap = 0xfffff000; dev->attr.max_qp_wr = 1024; dev->attr.device_cap_flags = VIRTIO_IB_DEVICE_RC_RNR_NAK_GEN; dev->attr.max_send_sge = 32; dev->attr.max_recv_sge = 32; dev->attr.max_sge_rd = 32; dev->attr.max_cqe = 1024; dev->attr.max_mr = 0x00001000; dev->attr.max_mw = 0; dev->attr.max_pd = 0x7ffc; dev->attr.max_qp_rd_atom = 128; dev->attr.max_qp_init_rd_atom = 128; dev->attr.max_ah = 100; dev->attr.max_fast_reg_page_list_len = 512; dev->attr.local_ca_ack_delay = 15; /* rdma device config */ dev->rdma_config.phys_port_cnt = 1; dev->rdma_config.device_cap_flag = dev->attr.device_cap_flags; dev->rdma_config.hw_ver = 1; dev->rdma_config.local_ca_ack_delay = dev->attr.local_ca_ack_delay; dev->rdma_config.max_ah = dev->attr.max_ah; dev->rdma_config.max_cq = dev->attr.max_cqs; dev->rdma_config.max_cqe = dev->attr.max_cqe; dev->rdma_config.max_fast_reg_page_list_len = dev->attr.max_fast_reg_page_list_len; dev->rdma_config.max_mcast_grp = 8192UL; dev->rdma_config.max_mcast_qp_attach = 56UL; dev->rdma_config.max_mr = dev->attr.max_mr; dev->rdma_config.max_mr_size = dev->attr.max_mr_size; dev->rdma_config.max_mw = dev->attr.max_mw; dev->rdma_config.max_pd = dev->attr.max_pd; dev->rdma_config.max_pi_fast_reg_page_list_len = dev->attr.max_fast_reg_page_list_len / 2; dev->rdma_config.max_pkeys = 1; dev->rdma_config.max_qp = dev->attr.max_qps; dev->rdma_config.max_qp_init_rd_atom = dev->attr.max_qp_init_rd_atom; dev->rdma_config.max_qp_rd_atom = dev->attr.max_qp_rd_atom; dev->rdma_config.max_qp_wr = dev->attr.max_qp_wr; dev->rdma_config.max_recv_sge = dev->attr.max_recv_sge; dev->rdma_config.max_res_rd_atom = dev->rdma_config.max_qp_rd_atom * dev->rdma_config.max_qp; dev->rdma_config.max_send_sge = dev->attr.max_send_sge; dev->rdma_config.max_sge_rd = dev->attr.max_sge_rd; dev->rdma_config.max_total_mcast_qp_attach = dev->rdma_config.max_mcast_grp * dev->rdma_config.max_mcast_qp_attach; dev->rdma_config.page_size_cap = dev->attr.page_size_cap; dev->rdma_config.phys_port_cnt = 1; dev->rdma_config.sys_image_guid = 1; dev->rdma_config.vendor_id = 0x1af4; dev->rdma_config.vendor_part_id = 0x0042; dev->max_inline_data = dev->attr.max_send_sge * sizeof(struct vhost_user_rdma_sge); dev->mtu_cap = ib_mtu_enum_to_int(DEFAULT_IB_MTU); dev->port_attr.bad_pkey_cntr = 0; dev->port_attr.qkey_viol_cntr = 0; for (int i = 0; i < VHOST_MAX_GID_TBL_LEN; i++) { dev->gid_tbl[i].type = VHOST_RDMA_GID_TYPE_ILLIGAL; } dev->cq_vqs = &dev->rdma_vqs[1]; dev->qp_vqs = &dev->rdma_vqs[1 + dev->attr.max_cqs]; vhost_rdma_pool_init(&dev->pd_pool, "pd_pool", dev->attr.max_pd, sizeof(struct vhost_rdma_pd), false, NULL); vhost_rdma_pool_init(&dev->mr_pool, "mr_pool", dev->attr.max_mr, sizeof(struct vhost_rdma_mr), false, vhost_rdma_mr_cleanup); vhost_rdma_pool_init(&dev->cq_pool, "cq_pool", dev->attr.max_cqs, sizeof(struct vhost_rdma_cq), true, NULL); vhost_rdma_pool_init(&dev->qp_pool, "qp_pool", dev->attr.max_qps, sizeof(struct vhost_rdma_qp), false, vhost_rdma_qp_cleanup); vhost_rdma_pool_init(&dev->ah_pool, "ah_pool", dev->attr.max_ah, sizeof(struct vhost_rdma_av), false, NULL); dev->qp_gsi = vhost_rdma_pool_alloc(&dev->qp_pool, &qpn); vhost_rdma_add_ref(dev->qp_gsi); assert(qpn == 1); } void vhost_rdma_destroy_ib(struct vhost_rdma_device *dev) { struct vhost_rdma_mr *mr; struct vhost_rdma_pd *pd; struct vhost_rdma_cq *cq; struct vhost_rdma_qp *qp; struct vhost_rdma_av *av; uint32_t i = 0; for (i = 0; i < dev->attr.max_mr; i++) { mr = vhost_rdma_pool_get(&dev->mr_pool, i); if (mr) vhost_rdma_pool_free(&dev->mr_pool, i); } for (i = 0; i < dev->attr.max_pd; i++) { pd = vhost_rdma_pool_get(&dev->pd_pool, i); if (pd) vhost_rdma_pool_free(&dev->pd_pool, i); } for (i = 0; i < dev->attr.max_cqs; i++) { cq = vhost_rdma_pool_get(&dev->cq_pool, i); if (cq) vhost_rdma_pool_free(&dev->cq_pool, i); } for (i = 0; i < dev->attr.max_qps; i++) { qp = vhost_rdma_pool_get(&dev->qp_pool, i); if (qp) { vhost_rdma_queue_cleanup(qp, &qp->sq.queue); vhost_rdma_queue_cleanup(qp, &qp->rq.queue); vhost_rdma_pool_free(&dev->qp_pool, i); } } for (i = 0; i < dev->attr.max_ah; i++) { av = vhost_rdma_pool_get(&dev->ah_pool, i); if (av) vhost_rdma_pool_free(&dev->ah_pool, i); } vhost_rdma_pool_destroy(&dev->mr_pool); vhost_rdma_pool_destroy(&dev->pd_pool); vhost_rdma_pool_destroy(&dev->cq_pool); vhost_rdma_pool_destroy(&dev->qp_pool); vhost_rdma_pool_destroy(&dev->ah_pool); } void vhost_rdma_handle_ctrl_vq(void* arg) { } int vhost_rdma_task_scheduler(void *arg) { return 0; } 这段 也改一下
10-10
改写以下代码,将数据输入改为socket接收,模型推理输出改为socket发送:/** @brief Source file of vendor ai net sample code. @file ai_net_with_mblob.c @ingroup ai_net_sample @note Nothing. Copyright Novatek Microelectronics Corp. 2021. All rights reserved. */ /*-----------------------------------------------------------------------------*/ /* Including Files */ /*-----------------------------------------------------------------------------*/ #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include "hdal.h" #include "hd_debug.h" #include "vendor_ai.h" #include "vendor_ai_cpu/vendor_ai_cpu.h" #include <sys/time.h> #if defined(_NVT_NVR_SDK_) #include <comm/nvtmem_if.h> #include <sys/ioctl.h> #endif #if defined(_BSP_NA51068_) || defined(_BSP_NA51090_) #include "vendor_common.h" #endif // platform dependent #if defined(__LINUX) #include <signal.h> #include <pthread.h> //for pthread API #define MAIN(argc, argv) int main(int argc, char** argv) #define GETCHAR() getchar() #else #include <FreeRTOS_POSIX.h> #include <FreeRTOS_POSIX/signal.h> #include <FreeRTOS_POSIX/pthread.h> //for pthread API #include <kwrap/util.h> //for sleep API #define sleep(x) vos_util_delay_ms(1000*(x)) #define msleep(x) vos_util_delay_ms(x) #define usleep(x) vos_util_delay_us(x) #include <kwrap/examsys.h> //for MAIN(), GETCHAR() API #define MAIN(argc, argv) EXAMFUNC_ENTRY(ai3_net_with_mblob, argc, argv) #define GETCHAR() NVT_EXAMSYS_GETCHAR() #endif #define DEBUG_MENU 1 #define DUMP_POSTPROC_INFO 1 #define DBG_OUT_DUMP 0 // debug mode, dump output iobuf #define FLOAT_IN 0 #define FLOAT_OUT 0 /////////////////////////////////////////////////////////////////////////////// #define NET_PATH_ID UINT32 #define VENDOR_AI_CFG 0x000f0000 //vendor ai config #define AI_RGB_BUFSIZE(w, h) (ALIGN_CEIL_4((w) * HD_VIDEO_PXLFMT_BPP(HD_VIDEO_PXLFMT_RGB888_PLANAR) / 8) * (h)) #define MULTI_BATCH_IN 0 #define CONFIG_INTERNAL_BUFFER 0 #define BATCH_SIZE 64 UINT32 g_proc_num=10; UINT32 dump_time_ut=0; UINT32 dump_time_proc=0; UINT32 dump_output=0; static UINT MPROCESS_FOR_AIISP =0; CHAR dump_path[256]; /////////////////////////////////////////////////////////////////////////////// /*-----------------------------------------------------------------------------*/ /* Type Definitions */ /*-----------------------------------------------------------------------------*/ typedef struct _MEM_PARM { UINTPTR pa; UINTPTR va; UINT32 size; UINTPTR blk; } MEM_PARM; /*-----------------------------------------------------------------------------*/ /* Global Functions */ /*-----------------------------------------------------------------------------*/ static HD_RESULT mem_alloc(MEM_PARM *mem_parm, CHAR* name, UINT32 size) { HD_RESULT ret = HD_OK; UINTPTR pa = 0; void *va = NULL; //alloc private pool ret = hd_common_mem_alloc(name, &pa, (void**)&va, size, DDR_ID0); if (ret!= HD_OK) { return ret; } mem_parm->pa = pa; mem_parm->va = (UINTPTR)va; mem_parm->size = size; mem_parm->blk = (UINTPTR)-1; return HD_OK; } static HD_RESULT mem_free(MEM_PARM *mem_parm) { HD_RESULT ret = HD_OK; //free private pool ret = hd_common_mem_free(mem_parm->pa, (void *)mem_parm->va); if (ret!= HD_OK) { return ret; } mem_parm->pa = 0; mem_parm->va = 0; mem_parm->size = 0; mem_parm->blk = (UINT32)-1; return HD_OK; } static INT32 mem_load(MEM_PARM *mem_parm, const CHAR *filename) { FILE *fd; INT32 size = 0; fd = fopen(filename, "rb"); if (!fd) { printf("cannot read %s\r\n", filename); size = -1; goto exit; } fseek(fd, 0, SEEK_END); size = ftell(fd); fseek(fd, 0, SEEK_SET); // check "ai_in_buf" enough or not if (mem_parm->size < (UINT32)size) { printf("ERROR: ai_in_buf(%u) is not enough, input file(%u)\r\n", mem_parm->size, (UINT32)size); size = -1; goto exit; } if (size < 0) { printf("getting %s size failed\r\n", filename); goto exit; } else if ((INT32)fread((VOID *)mem_parm->va, 1, size, fd) != size) { printf("read size < %d\r\n", size); size = -1; goto exit; } mem_parm->size = size; // we use cpu to read memory, which needs to deal cache flush. if(hd_common_mem_flush_cache((VOID *)mem_parm->va, mem_parm->size) != HD_OK) { printf("flush cache failed.\r\n"); } exit: if (fd) { fclose(fd); } return size; } /*-----------------------------------------------------------------------------*/ /* Input Functions */ /*-----------------------------------------------------------------------------*/ /////////////////////////////////////////////////////////////////////////////// typedef struct _NET_IN_CONFIG { CHAR input_filename[256]; UINT32 w; UINT32 h; UINT32 c; UINT32 b; UINT32 bitdepth; UINT32 loff; UINT32 fmt; UINT32 is_comb_img; // 1: image image (or feature-in) is a combination image (or feature-in). } NET_IN_CONFIG; typedef struct _NET_IN { NET_IN_CONFIG in_cfg; MEM_PARM input_mem; UINT32 in_id; VENDOR_AI3_BUF src_img; } NET_IN; static NET_IN *g_in = NULL; static UINT32 _calc_ai_buf_size(UINT32 loff, UINT32 h, UINT32 c, UINT32 b, UINT32 bitdepth, UINT32 fmt) { UINT size = 0; switch (fmt) { case HD_VIDEO_PXLFMT_YUV420: { size = loff * h * 3 / 2; } break; case HD_VIDEO_PXLFMT_RGB888_PLANAR: { size = AI_RGB_BUFSIZE(loff, h); } break; case HD_VIDEO_PXLFMT_BGR888_PLANAR: { size = AI_RGB_BUFSIZE(loff, h); } break; default: // feature-in { size = loff * h * c * bitdepth/8; } break; } if (!size) { printf("ERROR!! ai_buf size = 0\n"); } return size; } static HD_RESULT _load_buf(MEM_PARM *mem_parm, CHAR *filename, VENDOR_AI3_BUF *p_buf, UINT32 w, UINT32 h, UINT32 c, UINT32 b, UINT32 bitdepth, UINT32 loff, UINT32 fmt) { INT32 file_len; file_len = mem_load(mem_parm, filename); if (file_len < 0) { printf("load buf(%s) fail\r\n", filename); return HD_ERR_NG; } printf("load buf(%s) ok\r\n", filename); p_buf->width = w; p_buf->height = h; p_buf->channel = c; p_buf->batch_num = b; p_buf->line_ofs = loff; p_buf->fmt = fmt; p_buf->pa = mem_parm->pa; p_buf->va = mem_parm->va; p_buf->sign = MAKEFOURCC('A','B','U','F'); p_buf->size = _calc_ai_buf_size(loff, h, c, b, bitdepth, fmt); if (p_buf->size == 0) { printf("load buf(%s) fail, p_buf->size = 0\r\n", filename); return HD_ERR_NG; } return HD_OK; } static HD_RESULT input_init(void) { HD_RESULT ret = HD_OK; int i; for (i = 0; i < 16; i++) { NET_IN* p_net = g_in + i; p_net->in_id = i; } return ret; } static HD_RESULT input_uninit(void) { HD_RESULT ret = HD_OK; return ret; } static HD_RESULT input_set_config(NET_PATH_ID in_path, NET_IN_CONFIG* p_in_cfg) { HD_RESULT ret = HD_OK; NET_IN* p_net = g_in + in_path; UINT32 in_id = p_net->in_id; memcpy((void*)&p_net->in_cfg, (void*)p_in_cfg, sizeof(NET_IN_CONFIG)); printf("in_path(%u) in_id(%u) set in_cfg: file(%s), buf=(%u,%u,%u,%u,%u,%u,%08x)\r\n", in_path, in_id, p_net->in_cfg.input_filename, p_net->in_cfg.w, p_net->in_cfg.h, p_net->in_cfg.c, p_net->in_cfg.b, p_net->in_cfg.bitdepth, p_net->in_cfg.loff, p_net->in_cfg.fmt); return ret; } static HD_RESULT input_open(NET_PATH_ID in_path) { HD_RESULT ret = HD_OK; NET_IN* p_net = g_in + in_path; UINT32 in_id = p_net->in_id; UINT32 buf_size = 0; CHAR mem_name[23] ; snprintf(mem_name, 23, "ai_in_buf %u", in_id); // calculate in buf size buf_size = _calc_ai_buf_size(p_net->in_cfg.loff, p_net->in_cfg.h, p_net->in_cfg.c, p_net->in_cfg.b, p_net->in_cfg.bitdepth, p_net->in_cfg.fmt); if (buf_size == 0) { printf("in_path(%u) in_id(%u) size == 0 \r\n", in_path, in_id); return HD_ERR_FAIL; } // allocate in buf ret = mem_alloc(&p_net->input_mem, mem_name, buf_size * p_net->in_cfg.b); if (ret != HD_OK) { printf("in_path(%u) in_id(%u) alloc ai_in_buf fail\r\n", in_path, in_id); return HD_ERR_FAIL; } printf("alloc_in_buf: pa = 0x%lx, va = 0x%lx, size = %u\n", p_net->input_mem.pa, p_net->input_mem.va, p_net->input_mem.size); // load in buf ret = _load_buf(&p_net->input_mem, p_net->in_cfg.input_filename, &p_net->src_img, p_net->in_cfg.w, p_net->in_cfg.h, p_net->in_cfg.c, p_net->in_cfg.b, p_net->in_cfg.bitdepth, p_net->in_cfg.loff, p_net->in_cfg.fmt); if (ret != HD_OK) { printf("in_path(%u) in_id(%u) input_open fail=%d\n", in_path, in_id, ret); } return ret; } static HD_RESULT input_close(NET_PATH_ID in_path) { HD_RESULT ret = HD_OK; NET_IN* p_net = g_in + in_path; mem_free(&p_net->input_mem); return ret; } static HD_RESULT input_start(NET_PATH_ID in_path) { HD_RESULT ret = HD_OK; return ret; } static HD_RESULT input_stop(NET_PATH_ID in_path) { HD_RESULT ret = HD_OK; return ret; } static HD_RESULT input_pull_buf(NET_PATH_ID in_path, VENDOR_AI3_BUF *p_in, INT32 wait_ms) { HD_RESULT ret = HD_OK; NET_IN* p_net = g_in + in_path; memcpy((void*)p_in, (void*)&(p_net->src_img), sizeof(VENDOR_AI3_BUF)); return ret; } /////////////////////////////////////////////////////////////////////////////// /*-----------------------------------------------------------------------------*/ /* Network Functions */ /*-----------------------------------------------------------------------------*/ typedef struct _NET_PROC_CONFIG { CHAR model_filename[256]; INT32 binsize; void *p_share_model; CHAR label_filename[256]; } NET_PROC_CONFIG; typedef struct _NET_PROC { NET_PROC_CONFIG net_cfg; MEM_PARM proc_mem; UINT32 proc_id; //CHAR out_class_labels[MAX_CLASS_NUM * VENDOR_AIS_LBL_LEN]; MEM_PARM rslt_mem; MEM_PARM io_mem; MEM_PARM intl_mem; MEM_PARM *out_mem; VENDOR_AI3_NET_INFO net_info; } NET_PROC; static NET_PROC *g_net = NULL; static INT32 _getsize_model(char* filename) { FILE *bin_fd; UINT32 bin_size = 0; bin_fd = fopen(filename, "rb"); if (!bin_fd) { printf("get bin(%s) size fail\n", filename); return (-1); } fseek(bin_fd, 0, SEEK_END); bin_size = ftell(bin_fd); fseek(bin_fd, 0, SEEK_SET); fclose(bin_fd); return bin_size; } static UINT32 _load_model(CHAR *filename, UINTPTR va) { FILE *fd; UINT32 file_size = 0, read_size = 0; const UINTPTR model_addr = va; //DBG_DUMP("model addr = %#lx\r\n", model_addr); fd = fopen(filename, "rb"); if (!fd) { printf("load model(%s) fail\r\n", filename); return 0; } fseek ( fd, 0, SEEK_END ); file_size = ALIGN_CEIL_4( ftell(fd) ); fseek ( fd, 0, SEEK_SET ); read_size = fread ((void *)model_addr, 1, file_size, fd); if (read_size != file_size) { printf("size mismatch, real = %d, idea = %d\r\n", (int)read_size, (int)file_size); } fclose(fd); printf("load model(%s) ok\r\n", filename); return read_size; } /* static HD_RESULT _load_label(UINTPTR addr, UINT32 line_len, const CHAR *filename) { FILE *fd; CHAR *p_line = (CHAR *)addr; fd = fopen(filename, "r"); if (!fd) { printf("load label(%s) fail\r\n", filename); return HD_ERR_NG; } while (fgets(p_line, line_len, fd) != NULL) { p_line[strlen(p_line) - 1] = '\0'; // remove newline character p_line += line_len; } if (fd) { fclose(fd); } printf("load label(%s) ok\r\n", filename); return HD_OK; } */ static HD_RESULT network_init(void) { HD_RESULT ret = HD_OK; { VENDOR_AI3_DEV_CFG dev_cfg = {0}; ret = vendor_ai3_dev_init(&dev_cfg); if (ret != HD_OK) { printf("vendor_ai3_dev_init fail=%d\n", ret); return ret; } } // dump AI3 version { VENDOR_AI3_VER ai3_ver = {0}; ret = vendor_ai3_dev_get(VENDOR_AI3_CFG_VER, &ai3_ver); if (ret != HD_OK) { printf("vendor_ai3_dev_get(CFG_VER) fail=%d\n", ret); return ret; } printf("vendor_ai version = %s\r\n", ai3_ver.vendor_ai_impl_version); printf("kflow_ai version = %s\r\n", ai3_ver.kflow_ai_impl_version); printf("kdrv_ai version = %s\r\n", ai3_ver.kdrv_ai_impl_version); } return ret; } static HD_RESULT network_uninit(void) { HD_RESULT ret = HD_OK; ret = vendor_ai3_dev_uninit(); if (ret != HD_OK) { printf("vendor_ai3_dev_uninit fail=%d\n", ret); } return ret; } static INT32 network_mem_config(NET_PATH_ID net_path, HD_COMMON_MEM_INIT_CONFIG* p_mem_cfg, void* p_cfg) { NET_PROC* p_net = g_net + net_path; NET_PROC_CONFIG* p_proc_cfg = (NET_PROC_CONFIG*)p_cfg; #if defined(_NVT_NVR_SDK_) int sys_fd; struct nvtmem_hdal_base sys_hdal; uintptr_t hdal_start_addr0, hdal_start_addr1; sys_fd = open("/dev/nvtmem0", O_RDWR); if (sys_fd < 0) { printf("Error: cannot open /dev/nvtmem0 device.\n"); exit(0); } if (ioctl(sys_fd, NVTMEM_GET_DTS_HDAL_BASE, &sys_hdal) < 0) { printf("PCIE_SYS_IOC_HDALBASE! \n"); close(sys_fd); exit(0); } close(sys_fd); /* init ddr0 user_blk */ hdal_start_addr0 = sys_hdal.base[0]; p_mem_cfg->pool_info[0].start_addr = hdal_start_addr0; p_mem_cfg->pool_info[0].blk_cnt = 1; p_mem_cfg->pool_info[0].blk_size = 200 * 1024 * 1024; p_mem_cfg->pool_info[0].type = HD_COMMON_MEM_USER_BLK; p_mem_cfg->pool_info[0].ddr_id = sys_hdal.ddr_id[0]; printf("create ddr%d: hdal_memory(%#lx, %ldKB), usr_blk(%#lx, %dKB)\n", p_mem_cfg->pool_info[0].ddr_id, hdal_start_addr0, sys_hdal.size[0] / 1024, p_mem_cfg->pool_info[0].start_addr, p_mem_cfg->pool_info[0].blk_size * p_mem_cfg->pool_info[0].blk_cnt / 1024); /* init ddr1 user_blk, if ddr1 is exist */ if (sys_hdal.size[1] != 0) { hdal_start_addr1 = sys_hdal.base[1]; p_mem_cfg->pool_info[1].start_addr = hdal_start_addr1; p_mem_cfg->pool_info[1].blk_cnt = 1; p_mem_cfg->pool_info[1].blk_size = 200 * 1024 * 1024; p_mem_cfg->pool_info[1].type = HD_COMMON_MEM_USER_BLK; p_mem_cfg->pool_info[1].ddr_id = sys_hdal.ddr_id[1]; printf("create ddr%d: hdal_memory(%#lx, %ldKB) usr_blk(%#lx, %dKB)\n", p_mem_cfg->pool_info[1].ddr_id, hdal_start_addr1, sys_hdal.size[1] / 1024, p_mem_cfg->pool_info[1].start_addr, p_mem_cfg->pool_info[1].blk_size * p_mem_cfg->pool_info[1].blk_cnt / 1024); } else { printf("create ddr1: hdal_memory(%#lx, %ldKB) is not exist\n", sys_hdal.base[1], sys_hdal.size[1] / 1024); } usleep(30000); // wait for printf completely #endif memcpy((void*)&p_net->net_cfg, (void*)p_proc_cfg, sizeof(NET_PROC_CONFIG)); if (strlen(p_net->net_cfg.model_filename) == 0) { printf("net_path(%u) input model is null\r\n", net_path); return HD_ERR_NG; } p_net->net_cfg.binsize = _getsize_model(p_net->net_cfg.model_filename); if (p_net->net_cfg.binsize <= 0) { printf("net_path(%u) input model is not exist?\r\n", net_path); return HD_ERR_NG; } printf("net_path(%u) set net_mem_cfg: model-file(%s), binsize=%d\r\n", net_path, p_net->net_cfg.model_filename, p_net->net_cfg.binsize); printf("net_path(%u) set net_mem_cfg: label-file(%s)\r\n", net_path, p_net->net_cfg.label_filename); return HD_OK; } static HD_RESULT network_alloc_io_buf(NET_PATH_ID net_path, UINT32 req_size) { HD_RESULT ret = HD_OK; NET_PROC* p_net = g_net + net_path; CHAR mem_name[23] ; snprintf(mem_name, 23, "ai_io_buf %u", net_path); ret = mem_alloc(&p_net->io_mem, mem_name, req_size); if (ret != HD_OK) { printf("net_path(%u) alloc ai_io_buf fail\r\n", net_path); return HD_ERR_FAIL; } printf("alloc_io_buf: work buf, pa = %#lx, va = %#lx, size = %u\r\n", p_net->io_mem.pa, p_net->io_mem.va, p_net->io_mem.size); return ret; } static HD_RESULT network_free_io_buf(NET_PATH_ID net_path) { HD_RESULT ret = HD_OK; NET_PROC* p_net = g_net + net_path; if (p_net->io_mem.pa && p_net->io_mem.va) { mem_free(&p_net->io_mem); } return ret; } static HD_RESULT network_alloc_intl_buf(NET_PATH_ID net_path, UINT32 req_size) { HD_RESULT ret = HD_OK; NET_PROC* p_net = g_net + net_path; CHAR mem_name[23] ; snprintf(mem_name, 23, "ai_ronly_buf %u", net_path); ret = mem_alloc(&p_net->intl_mem, mem_name, req_size); if (ret != HD_OK) { printf("net_path(%u) alloc ai_ronly_buf fail\r\n", net_path); return HD_ERR_FAIL; } printf("alloc_intl_buf: internal buf, pa = %#lx, va = %#lx, size = %u\r\n", p_net->intl_mem.pa, p_net->intl_mem.va, p_net->intl_mem.size); return ret; } static HD_RESULT network_free_intl_buf(NET_PATH_ID net_path) { HD_RESULT ret = HD_OK; NET_PROC* p_net = g_net + net_path; if (p_net->intl_mem.pa && p_net->intl_mem.va) { mem_free(&p_net->intl_mem); } return ret; } static HD_RESULT network_open(NET_PATH_ID net_path) { HD_RESULT ret = HD_OK; NET_PROC* p_net = g_net + net_path; UINT32 loadsize = 0; CHAR mem_name[23] ; snprintf(mem_name, 23, "model.bin %u", net_path); if (strlen(p_net->net_cfg.model_filename) == 0) { printf("net_path(%u) input model is null\r\n", net_path); return 0; } ret = mem_alloc(&p_net->proc_mem, mem_name, p_net->net_cfg.binsize); if (ret != HD_OK) { printf("net_path(%u) mem_alloc model.bin fail=%d\n", net_path, ret); return HD_ERR_FAIL; } //load file loadsize = _load_model(p_net->net_cfg.model_filename, p_net->proc_mem.va); if (loadsize <= 0) { printf("net_path(%u) input model load fail: %s\r\n", net_path, p_net->net_cfg.model_filename); return 0; } /* // load label ret = _load_label((UINTPTR)p_net->out_class_labels, VENDOR_AIS_LBL_LEN, p_net->net_cfg.label_filename); if (ret != HD_OK) { printf("proc_id(%u) load_label fail=%d\n", proc_id, ret); return HD_ERR_FAIL; } */ { VENDOR_AI3_MODEL_INFO model_info = {0}; model_info.model_buf.pa = p_net->proc_mem.pa; model_info.model_buf.va = p_net->proc_mem.va; model_info.model_buf.size = p_net->proc_mem.size; #if DBG_OUT_DUMP model_info.ctrl = CTRL_BUF_DEBUG | CTRL_JOB_DEBUG | CTRL_JOB_DUMPOUT; #endif #if FLOAT_IN model_info.ctrl = model_info.ctrl | CTRL_BUF_FLOATIN ; #endif #if FLOAT_OUT model_info.ctrl = model_info.ctrl | CTRL_BUF_FLOATOUT ; #endif printf("net_path(%u) vendor_ai3_dev_get(MODEL_INFO) \n", net_path); ret = vendor_ai3_dev_get(VENDOR_AI3_CFG_MODEL_INFO, &model_info); if (ret != HD_OK) { printf("net_path(%u) vendor_ai3_dev_get(MODEL_INFO) fail=%d\n", net_path, ret); return HD_ERR_FAIL; } printf("model_info get => workbuf size = %d, ronlybuf size = %d\r\n", model_info.proc_mem.buf[AI3_PROC_BUF_WORKBUF].size, model_info.proc_mem.buf[AI3_PROC_BUF_RONLYBUF].size); // alloc WORKBUF/RONLYBUF ret = network_alloc_intl_buf(net_path, model_info.proc_mem.buf[AI3_PROC_BUF_RONLYBUF].size); if (ret != HD_OK) { printf("net_path(%u) alloc ronlybuf fail=%d\n", net_path, ret); return HD_ERR_FAIL; } ret = network_alloc_io_buf(net_path, model_info.proc_mem.buf[AI3_PROC_BUF_WORKBUF].size); if (ret != HD_OK) { printf("net_path(%u) alloc workbuf fail=%d\n", net_path, ret); return HD_ERR_FAIL; } } // call open() { VENDOR_AI3_PROC_CFG proc_cfg = {0}; proc_cfg.model_buf.pa = p_net->proc_mem.pa; proc_cfg.model_buf.va = p_net->proc_mem.va; proc_cfg.model_buf.size = p_net->proc_mem.size; proc_cfg.proc_mem.buf[AI3_PROC_BUF_RONLYBUF].pa = p_net->intl_mem.pa; proc_cfg.proc_mem.buf[AI3_PROC_BUF_RONLYBUF].va = p_net->intl_mem.va; proc_cfg.proc_mem.buf[AI3_PROC_BUF_RONLYBUF].size = p_net->intl_mem.size; proc_cfg.proc_mem.buf[AI3_PROC_BUF_WORKBUF].pa = p_net->io_mem.pa; proc_cfg.proc_mem.buf[AI3_PROC_BUF_WORKBUF].va = p_net->io_mem.va; proc_cfg.proc_mem.buf[AI3_PROC_BUF_WORKBUF].size = p_net->io_mem.size; proc_cfg.plugin[AI3_PLUGIN_CPU] = vendor_ai_cpu1_get_engine(); #if DBG_OUT_DUMP proc_cfg.ctrl = CTRL_BUF_DEBUG | CTRL_JOB_DEBUG | CTRL_JOB_DUMPOUT; #endif #if FLOAT_IN proc_cfg.ctrl = proc_cfg.ctrl | CTRL_BUF_FLOATIN ; #endif #if FLOAT_OUT proc_cfg.ctrl = proc_cfg.ctrl | CTRL_BUF_FLOATOUT ; #endif ret = vendor_ai3_net_open(&p_net->proc_id, &proc_cfg, &p_net->net_info); if (ret != HD_OK) { printf("net_path(%u) vendor_ai3_net_open() fail=%d\n", net_path, ret); return HD_ERR_FAIL; } else { printf("net_path(%u) open success => get proc_id(%u)\r\n", net_path, p_net->proc_id); } } return ret; } static HD_RESULT network_close(NET_PATH_ID net_path) { HD_RESULT ret = HD_OK; NET_PROC* p_net = g_net + net_path; UINT32 proc_id = p_net->proc_id; UINT32 i ; // close ret = vendor_ai3_net_close(proc_id); if (ret != HD_OK) { printf("net_path(%u), proc_id(%u) vendor_ai3_net_close fail=%d\n", net_path, proc_id, ret); return HD_ERR_FAIL; } if ((ret = network_free_intl_buf(net_path)) != HD_OK) return ret; if ((ret = network_free_io_buf(net_path)) != HD_OK) return ret; mem_free(&p_net->proc_mem); for (i = 0 ; i < p_net->net_info.out_buf_cnt; i++){ if(p_net->out_mem && p_net->out_mem[i].va) mem_free(&p_net->out_mem[i]); } if(p_net->out_mem) free(p_net->out_mem) ; return ret; } #if DUMP_POSTPROC_INFO static HD_RESULT network_dump_ai_buf(VENDOR_AI3_BUF *p_outbuf) { HD_RESULT ret = HD_OK; printf(" sign(0x%x) pa(0x%lx) va(0x%lx) sz(%u) w(%u) h(%u) ch(%u) batch_num(%u)\n", p_outbuf->sign, p_outbuf->pa, p_outbuf->va, p_outbuf->size, p_outbuf->width, p_outbuf->height, p_outbuf->channel, p_outbuf->batch_num); printf(" l_ofs(%llu) c_ofs(%llu) b_ofs(%llu) t_ofs(%llu) layout(%s) name(%s) scale_ratio(%.6f)\n", p_outbuf->line_ofs, p_outbuf->channel_ofs, p_outbuf->batch_ofs, p_outbuf->time_ofs, p_outbuf->layout, p_outbuf->name, p_outbuf->scale_ratio); // parsing pixel format switch (AI_PXLFMT_TYPE(p_outbuf->fmt)) { case HD_VIDEO_PXLFMT_AI_UINT8: case HD_VIDEO_PXLFMT_AI_SINT8: case HD_VIDEO_PXLFMT_AI_UINT16: case HD_VIDEO_PXLFMT_AI_SINT16: case HD_VIDEO_PXLFMT_AI_UINT32: case HD_VIDEO_PXLFMT_AI_SINT32: case HD_VIDEO_PXLFMT_AI_FLOAT32: { INT8 bitdepth = HD_VIDEO_PXLFMT_BITS(p_outbuf->fmt); INT8 int_bits = HD_VIDEO_PXLFMT_INT(p_outbuf->fmt); INT8 frac_bits = HD_VIDEO_PXLFMT_FRAC(p_outbuf->fmt); printf(" fmt(0x%x) bits(%u) int(%u) frac(%u)\n", p_outbuf->fmt, bitdepth, int_bits, frac_bits); } break; default: switch ((UINT32)p_outbuf->fmt) { case HD_VIDEO_PXLFMT_BGR888_PLANAR: { printf(" fmt(0x%x), BGR888_PLANAR\n", p_outbuf->fmt); } break; case HD_VIDEO_PXLFMT_YUV420: { printf(" fmt(0x%x), YUV420\n", p_outbuf->fmt); } break; case HD_VIDEO_PXLFMT_Y8: { printf(" fmt(0x%x), Y8 only\n", p_outbuf->fmt); } break; case HD_VIDEO_PXLFMT_UV: { printf(" fmt(0x%x), UV only\n", p_outbuf->fmt); } break; case 0: { printf(" fmt(0x%x), AI BUF\n", p_outbuf->fmt); } break; default: printf("unknown pxlfmt(0x%x)\n", p_outbuf->fmt); break; } } printf("\n"); return ret; } /* static INT32 mem_save(MEM_PARM *mem_parm, const CHAR *filename) { FILE *fd; UINT32 size = 0; fd = fopen(filename, "wb"); if (!fd) { printf("ERR: cannot open %s for write!\r\n", filename); return -1; } size = (INT32)fwrite((VOID *)mem_parm->va, 1, mem_parm->size, fd); if (size != mem_parm->size) { printf("ERR: write %s with size %ld < wanted %ld?\r\n", filename, size, mem_parm->size); } else { printf("write %s with %ld bytes.\r\n", filename, mem_parm->size); } if (fd) { fclose(fd); } return size; } */ #endif /////////////////////////////////////////////////////////////////////////////// typedef struct _VIDEO_LIVEVIEW { // (1) input NET_IN_CONFIG net_in_cfg; NET_PATH_ID in_path; // (2) network NET_PROC_CONFIG net_proc_cfg; NET_PATH_ID net_path; pthread_t proc_thread_id; UINT32 proc_start; UINT32 proc_exit; UINT32 proc_oneshot; UINT32 input_blob_num; } VIDEO_LIVEVIEW; static HD_RESULT init_module(void) { HD_RESULT ret; if ((ret = input_init()) != HD_OK) return ret; if ((ret = network_init()) != HD_OK) return ret; return HD_OK; } static HD_RESULT open_module(VIDEO_LIVEVIEW *p_stream) { HD_RESULT ret; //if ((ret = input_open(p_stream->in_path)) != HD_OK) // return ret; if ((ret = network_open(p_stream->net_path)) != HD_OK) return ret; return HD_OK; } static HD_RESULT close_module(VIDEO_LIVEVIEW *p_stream) { HD_RESULT ret; //if ((ret = input_close(p_stream->in_path)) != HD_OK) // return ret; if ((ret = network_close(p_stream->net_path)) != HD_OK) return ret; return HD_OK; } static HD_RESULT exit_module(void) { HD_RESULT ret; if ((ret = input_uninit()) != HD_OK) return ret; if ((ret = network_uninit()) != HD_OK) return ret; return HD_OK; } static HD_RESULT perf_begin(void) { vendor_ai3_dev_perf_begin(VENDOR_AI3_PERF_ID_TIME_UT); return HD_OK; } static HD_RESULT perf_end(void) { UINT32 i; VENDOR_AI3_PERF_TIME_UT perf_time_ut = {0}; vendor_ai3_dev_perf_end(VENDOR_AI3_PERF_ID_TIME_UT, &perf_time_ut); printf("\r\n ************* util-per-proc() *************\r\n"); for (i=0; i<perf_time_ut.core_count; i++) { printf("%8s: time(us) = %6d, util(%%) = %6.2f\r\n", perf_time_ut.core[i].name, perf_time_ut.core[i].time, ((float)perf_time_ut.core[i].util)/100); } return HD_OK; } /////////////////////////////////////////////////////////////////////////////// static VOID *network_user_thread(VOID *arg); static HD_RESULT set_buf_by_in_path_list(VIDEO_LIVEVIEW *p_stream) { HD_RESULT ret = HD_OK; NET_PATH_ID net_path = p_stream->net_path; NET_PROC* p_net = g_net + net_path; VENDOR_AI3_NET_INFO net_info = p_net->net_info; VENDOR_AI3_BUF in_buf = {0}; VENDOR_AI3_BUF tmp_buf = {0}; UINT32 proc_id = p_net->proc_id; UINT32 i = 0, idx = 0, k = 0; UINT32 in_buf_cnt = 0; UINT32 *in_path_list = NULL; /* get in path list */ in_path_list = p_net->net_info.in_path_list; in_buf_cnt = net_info.in_buf_cnt; for (i = 0; i < in_buf_cnt; i++) { /* get in buf (by in path list) */ ret = vendor_ai3_net_get(proc_id, in_path_list[i], &tmp_buf); if (HD_OK != ret) { printf("net_path(%u), proc_id(%u) get in buf fail, i(%d), in_path(0x%x)\n",net_path, proc_id, i, in_path_list[i]); goto exit; } if (dump_output) { // dump in buf printf("dump_in_buf: path_id: 0x%x\n", in_path_list[i]); ret = network_dump_ai_buf(&tmp_buf); if (HD_OK != ret) { printf("net_path(%u), proc_id(%u) dump in buf fail !!\n", net_path, proc_id); goto exit; }} } for (i = 0; i < p_stream->input_blob_num; i++) { NET_IN* p_in = g_in + i; /* load input bin */ ret = input_pull_buf((p_stream->in_path + i), &in_buf, 0); if (HD_OK != ret) { printf("in_path(%u) pull input fail !!\n", (p_stream->in_path + i)); goto exit; } if (p_in->in_cfg.is_comb_img == 0) { for(k =1 ; k < in_buf.batch_num; k++) { UINTPTR dst_va = in_buf.va + (k * in_buf.size); memcpy((VOID*)dst_va , (VOID*)in_buf.va, in_buf.size); hd_common_mem_flush_cache((VOID *)dst_va, in_buf.size); } } //printf(" path_%d(0x%x) pa(0x%lx) va(0x%lx) size(%u)\n", idx, in_path_list[idx], in_buf.pa, in_buf.va, in_buf.size); ret = vendor_ai3_net_set(proc_id, in_path_list[idx], &in_buf); if (HD_OK != ret) { printf("proc_id(%u)push input fail !! i(%u)\n", proc_id, i); goto exit; } idx++; } exit: return ret; } static HD_RESULT set_buf_by_out_path_list(NET_PATH_ID net_path) { HD_RESULT ret = HD_OK; NET_PROC* p_net = g_net + net_path; UINT32 proc_id = p_net->proc_id; UINT32 i; VENDOR_AI3_BUF ai_buf = {0}; /* get out buf */ for (i = 0; i < p_net->net_info.out_buf_cnt; i++) { // get out buf (by out path list) ret = vendor_ai3_net_get(proc_id, p_net->net_info.out_path_list[i], &ai_buf); if (HD_OK != ret) { printf("proc_id(%u) get out buf fail, i(%d), out_path(0x%x)\n", proc_id, i, p_net->net_info.out_path_list[i]); goto exit; } if (ai_buf.size > p_net->out_mem[i].size){ printf("output size %u < ai_buf.size %u\r\n", p_net->out_mem[i].size, ai_buf.size); goto exit; } ai_buf.va = p_net->out_mem[i].va ; ai_buf.pa = p_net->out_mem[i].pa ; ai_buf.size = p_net->out_mem[i].size ; #if FLOAT_OUT ai_buf.fmt = HD_VIDEO_PXLFMT_AI_FLOAT32; #endif ret = vendor_ai3_net_set(proc_id, p_net->net_info.out_path_list[i], &ai_buf); if (HD_OK != ret) { printf("proc_id(%u)set output buf fail !! (%u)\n", proc_id, i); goto exit; } } exit: return ret; } #if (1) static uintptr_t get_post_buf(uint32_t size) { uintptr_t buf = (uintptr_t)malloc(size); return buf; } static VOID release_post_buf(VOID *ptr) { if (ptr) { free(ptr); } return; } #endif #if(1) static HD_RESULT network_dump_out_buf(NET_PATH_ID net_path, VENDOR_AI3_BUF *p_outbuf, UINT32 id) { HD_RESULT ret = HD_OK; //NET_PROC *p_net = g_net + net_path; //INT32 i; //AI_NET_ACCURACY_PARM parm = {0}; //AI_NET_SHAPE shape = {p_outbuf->batch_num, p_outbuf->channel, 1, 1, 1}; //INT32 size = shape.num * shape.channels * shape.height * shape.width; UINT32 length = p_outbuf->batch_num * p_outbuf->channel * p_outbuf->height * p_outbuf->width; FLOAT *p_outbuf_float = (FLOAT *)get_post_buf(length * sizeof(FLOAT)); ret = vendor_ai_cpu_util_fixed2float((VOID *)p_outbuf->va, p_outbuf->fmt, p_outbuf_float, p_outbuf->scale_ratio, length, p_outbuf->zero_point); printf("id:%d size:%d name:%s\n", id, length, p_outbuf->name); #if (1) // save output float bin CHAR out_float_path[256] = {0}; sprintf(out_float_path, "%s/float_%s.bin", dump_path, p_outbuf->name); printf("out_float_path: %s\n", out_float_path); FILE* fp_out = NULL; if ((fp_out = fopen(out_float_path, "wb+")) == NULL) { printf("fopen fail\n"); } else { fwrite((VOID *)p_outbuf_float, sizeof(FLOAT), length, fp_out); //fwrite((VOID *)layer_buffer[i].va, sizeof(INT16), length, fp_out); } if(fp_out){ fclose(fp_out); } // save output fixed bin CHAR out_fixed_path[256] = {0}; sprintf(out_fixed_path, "%s/fixed_%s.bin", dump_path, p_outbuf->name); printf("out_fixed_path: %s\n", out_fixed_path); FILE* fp_out1 = NULL; if ((fp_out1 = fopen(out_fixed_path, "wb+")) == NULL) { printf("fopen fail\n"); } else { //fwrite((VOID *)p_outbuf_float, sizeof(FLOAT), length, fp_out1); fwrite((VOID *)p_outbuf->va, sizeof(INT16), length, fp_out1); } if(fp_out1){ fclose(fp_out1); } #endif /* INT32 *p_idx = (INT32 *)get_post_buf(p_outbuf->channel * sizeof(INT32)); AI_NET_OUTPUT_CLASSS *p_classes = (AI_NET_OUTPUT_CLASSS *)get_post_buf(p_outbuf->batch_num * TOP_N * sizeof(AI_NET_OUTPUT_CLASSS)); parm.in_addr = (uintptr_t)p_outbuf_float; parm.classes = p_classes; parm.shape = shape; parm.top_n = TOP_N; parm.class_idx = p_idx; ret = ai_net_accuracy_process(&parm); printf("Classification Results:\r\n"); for (i = 0; i < parm.top_n; i++) { printf("%d. no=%d, label=%s, score=%f\r\n", i + 1, parm.classes[i].no, &p_net->out_class_labels[parm.classes[i].no * LABEL_LEN], parm.classes[i].score); } */ release_post_buf(p_outbuf_float); //release_post_buf(p_idx); //release_post_buf(p_classes); return ret; } #endif static HD_RESULT get_buf_by_out_path_list(NET_PATH_ID net_path) { HD_RESULT ret = HD_OK; NET_PROC* p_net = g_net + net_path; UINT32 proc_id = p_net->proc_id; UINT32 i; VENDOR_AI3_BUF ai_buf = {0}; printf("########### p_net->net_info.out_buf_cnt %d \r\n",p_net->net_info.out_buf_cnt); /* get out buf */ for (i = 0; i < p_net->net_info.out_buf_cnt; i++) { // get out buf (by out path list) ret = vendor_ai3_net_get(proc_id, p_net->net_info.out_path_list[i], &ai_buf); if (HD_OK != ret) { printf("net_path(%u), proc_id(%u) get out buf fail, i(%d), out_path(0x%x)\n", net_path, proc_id, i, p_net->net_info.out_path_list[i]); goto exit; } #if DUMP_POSTPROC_INFO { //CHAR dump_path[23]; // dump out buf if (hd_common_mem_flush_cache((VOID *)ai_buf.va, ai_buf.size) != HD_OK) { printf("flush cache failed.\r\n"); } printf("dump_out_buf: path_id: 0x%x\n", p_net->net_info.out_path_list[i]); ret = network_dump_ai_buf(&ai_buf); if (HD_OK != ret) { printf("net_path(%u) dump out buf fail !!\n", net_path); goto exit; } //snprintf(dump_path, 23, "%s.bin", ai_buf.name); //printf(" DUMP out_buf (%u): %s va = %lx\n", i, dump_path,(ULONG)p_net->out_mem[i].va); //mem_save(&p_net->out_mem[i], dump_path); ret = network_dump_out_buf(net_path, &ai_buf, i); } #endif } exit: return ret; } static HD_RESULT allocate_buf_by_out_path_list(NET_PATH_ID net_path) { HD_RESULT ret = HD_OK; NET_PROC* p_net = g_net + net_path; UINT32 proc_id = p_net->proc_id; UINT32 i; VENDOR_AI3_BUF ai_buf = {0}; CHAR mem_name[23] ; /* get out path list */ p_net->out_mem = (MEM_PARM *)malloc(sizeof(MEM_PARM) * p_net->net_info.out_buf_cnt); memset(p_net->out_mem, 0, sizeof(MEM_PARM) * p_net->net_info.out_buf_cnt); /* get out buf */ for (i = 0; i < p_net->net_info.out_buf_cnt; i++) { // get out buf (by out path list) ret = vendor_ai3_net_get(proc_id, p_net->net_info.out_path_list[i], &ai_buf); if (HD_OK != ret) { printf("proc_id(%u) get out buf fail, i(%d), out_path(0x%x)\n", proc_id, i, p_net->net_info.out_path_list[i]); goto exit; } // // allocate in buf #if FLOAT_OUT ai_buf.size = ai_buf.width * ai_buf.height * ai_buf.channel * ai_buf.batch_num *sizeof(float) ; #endif snprintf(mem_name, 23, "output_buf %u", i); ret = mem_alloc(&p_net->out_mem[i], mem_name, ai_buf.size); if (ret != HD_OK) { printf("proc_id(%u) alloc ai_in_buf fail\r\n", proc_id); goto exit; } printf("alloc_outbuf: pa = 0x%lx, va = 0x%lx, size = %u\n", p_net->out_mem[i].pa, p_net->out_mem[i].va, p_net->out_mem[i].size); } return ret; exit: for (i = 0 ; i < p_net->net_info.out_buf_cnt; i++){ if(p_net->out_mem && p_net->out_mem[i].va) mem_free(&p_net->out_mem[i]); } if(p_net->out_mem) free(p_net->out_mem) ; return ret; } static HD_RESULT network_user_start(VIDEO_LIVEVIEW *p_stream) { HD_RESULT ret = HD_OK; NET_PROC* p_net = g_net + p_stream->net_path; UINT32 proc_id = p_net->proc_id; p_stream->proc_start = 0; p_stream->proc_exit = 0; p_stream->proc_oneshot = 0; ret = vendor_ai3_net_start(proc_id); if (HD_OK != ret) { printf("net_path(%u), proc_id(%u) vendor_ai3_net_start fail !!\n", p_stream->net_path, proc_id); } ret = pthread_create(&p_stream->proc_thread_id, NULL, network_user_thread, (VOID*)(p_stream)); if (ret < 0) { return HD_ERR_FAIL; } p_stream->proc_start = 1; p_stream->proc_exit = 0; p_stream->proc_oneshot = 0; return ret; } static HD_RESULT network_user_oneshot(VIDEO_LIVEVIEW *p_stream) { HD_RESULT ret = HD_OK; p_stream->proc_oneshot = 1; return ret; } static HD_RESULT network_user_stop(VIDEO_LIVEVIEW *p_stream) { HD_RESULT ret = HD_OK; NET_PROC* p_net = g_net + p_stream->net_path; UINT32 proc_id = p_net->proc_id; p_stream->proc_exit = 1; if (p_stream->proc_thread_id) { pthread_join(p_stream->proc_thread_id, NULL); } //stop: should be call after last time proc ret = vendor_ai3_net_stop(proc_id); if (HD_OK != ret) { printf("net_path(%u), proc_id(%u) vendor_ai3_net_stop fail !!\n", p_stream->net_path, proc_id); } return ret; } static VOID *network_user_thread(VOID *arg) { HD_RESULT ret = HD_OK; VIDEO_LIVEVIEW *p_stream = (VIDEO_LIVEVIEW*)arg; NET_PROC* p_net = g_net + p_stream->net_path; UINT32 proc_id = p_net->proc_id; static struct timeval tstart, tend; static UINT64 cur_time = 0; static UINT64 all_time = 0; static float mean_time = 0; UINT32 count=0; printf("\r\n"); while (p_stream->proc_start == 0) sleep(1); printf("\r\n"); while (p_stream->proc_exit == 0) { if (p_stream->proc_oneshot) { p_stream->proc_oneshot = 0; for (count=0;count<g_proc_num;count++) { // set buf by in_path_list ret = set_buf_by_in_path_list(p_stream); if (HD_OK != ret) { printf("net_path(%u), proc_id(%u) set in_buf fail(%d) !!\n", p_stream->net_path, proc_id, ret); goto skip; } ret = set_buf_by_out_path_list(p_stream->net_path); if (HD_OK != ret) { printf("net_path(%u), proc_id(%u) set in_buf fail(%d) !!\n", p_stream->net_path, proc_id, ret); goto skip; } // do net proc gettimeofday(&tstart, NULL); if(dump_time_ut){ perf_begin(); } ret = vendor_ai3_net_proc(proc_id); if(dump_time_ut){ perf_end(); } gettimeofday(&tend, NULL); if (HD_OK != ret) { printf("net_path(%u), proc_id(%u) proc fail !!\n", p_stream->net_path, proc_id); goto skip; } //count++; cur_time = (UINT64)(tend.tv_sec - tstart.tv_sec) * 1000000 + (tend.tv_usec - tstart.tv_usec); if(count>0){ all_time+=cur_time; mean_time=all_time/(float)(count); if (dump_time_proc){ printf("count %d cur time(us): %lld all time(us): %lld mean time(us): %f \r\n",count,cur_time,all_time,mean_time); } } // printf("net_path(%u), proc_id(%u) oneshot done!\n", p_stream->net_path, proc_id); // get buf by out_path_list if (dump_output) { /* code */ ret = get_buf_by_out_path_list(p_stream->net_path); if (HD_OK != ret) { printf("net_path(%u), proc_id(%u) get out_buf fail(%d) !!\n", p_stream->net_path, proc_id, ret); goto skip; } } } printf("count %d cur time(us): %lld all time(us): %lld mean time(us): %f \r\n",count,cur_time,all_time,mean_time); } usleep(100); } skip: return 0; } /*-----------------------------------------------------------------------------*/ /* Interface Functions */ /*-----------------------------------------------------------------------------*/ MAIN(argc, argv) { VIDEO_LIVEVIEW stream[1] = {0}; HD_COMMON_MEM_INIT_CONFIG mem_cfg = {0}; HD_RESULT ret; INT key; UINT32 j; INT32 idx; #if FLOAT_IN stream[0].input_blob_num = 1; //net_in NET_IN_CONFIG in_cfg = { .input_filename = "/mnt/sd/jpg/float32.bin", .w = 74, .h = 1, .c = 40, .b = 1, .bitdepth = 32, .loff = 296, .fmt = HD_VIDEO_PXLFMT_AI_FLOAT32, .is_comb_img = 1, }; NET_IN_CONFIG in_cfg2 = { .input_filename = "NULL", .w = 0, .h = 0, .c = 0, .b = 0, .bitdepth = 0, .loff = 0, .fmt = 0, .is_comb_img = 0, }; //net proc NET_PROC_CONFIG net_cfg = { .model_filename = "/mnt/sd/para/nvt_model_float32.bin", .label_filename = "/mnt/sd/accuracy/labels.txt" }; #elif MULTI_BATCH_IN /* multi-blob with multi-batch */ stream[0].input_blob_num = 2; //net_in NET_IN_CONFIG in_cfg = { .input_filename = "/mnt/sd/jpg/mblob_mbatch_0.yuv", .w = 12, .h = 12, .c = 2, .b = 8, .bitdepth = 8, .loff = 12, .fmt = HD_VIDEO_PXLFMT_YUV420, .is_comb_img = 1, }; NET_IN_CONFIG in_cfg2 = { .input_filename = "/mnt/sd/jpg/mblob_mbatch_1.bin", .w = 1, .h = 1, .c = 5, .b = 8, .bitdepth = 16, .loff = 2, .fmt = 0xA2101000, .is_comb_img = 1, }; //net proc NET_PROC_CONFIG net_cfg = { .model_filename = "/mnt/sd/para/nvt_model_mblob_mbatch.bin", .label_filename = "/mnt/sd/accuracy/labels.txt" }; #else stream[0].input_blob_num = 3; //net_in NET_IN_CONFIG in_cfg1 = { .input_filename = "/mnt/sd/jpg/mblob.bin", .w = 224, .h = 224, .c = 2, .b = 1, .bitdepth = 8, .loff = 224, .fmt = HD_VIDEO_PXLFMT_YUV420, .is_comb_img = 0, }; NET_IN_CONFIG in_cfg2 = { .input_filename = "/mnt/sd/jpg/mblob.bin", .w = 224, .h = 224, .c = 2, .b = 1, .bitdepth = 8, .loff = 224, .fmt = HD_VIDEO_PXLFMT_YUV420, .is_comb_img = 0, }; NET_IN_CONFIG in_cfg3 = { .input_filename = "/mnt/sd/jpg/mblob.bin", .w = 224, .h = 224, .c = 2, .b = 1, .bitdepth = 8, .loff = 224, .fmt = HD_VIDEO_PXLFMT_YUV420, .is_comb_img = 0, }; //net proc NET_PROC_CONFIG net_cfg = { .model_filename = "/mnt/sd/para/nvt_model_mblob.bin", .label_filename = "/mnt/sd/accuracy/labels.txt" }; #endif // End of #if MULTI_BATCH_IN if(argc < 4){ printf("usage : ai3_net_with_mblob (proc_num) (model_path) (input_blob_num)\n"); printf("usage : (MPROCESS_FOR_AIISP)\n"); printf("usage : (dump_time_UT)\n"); printf("usage : (dump_time_proc)\n"); printf("usage : (dump_output)\n"); printf("usage : (dump_path)\n"); printf("usage : (input_data_path1) (input_w) (input_h) (input_c) (input_b) (input_bitdepth) (input_loff) (input_fmt_10) (input_is_comb_img)\n"); printf("usage : (input_data_path2) (input_w) (input_h) (input_c) (input_b) (input_bitdepth) (input_loff) (input_fmt_10) (input_is_comb_img)\n"); printf("usage : (input_data_path2) (input_w) (input_h) (input_c) (input_b) (input_bitdepth) (input_loff) (input_fmt_10) (input_is_comb_img)\n"); return -1; } printf("\r\n\r\n"); idx = 1; UINT32 proc_num = 0; UINT32 input_blob_num = 0; { if (argc > idx) { sscanf(argv[idx++], "%d", &proc_num); printf("proc_num: %d\n", proc_num); g_proc_num=proc_num; } if (argc > idx) { sscanf(argv[idx++], "%s", net_cfg.model_filename); printf("net_cfg.model_filename: %s\n", net_cfg.model_filename); } if (argc > idx) { sscanf(argv[idx++], "%d", &input_blob_num); printf("input_blob_num: %d\n", input_blob_num); stream[0].input_blob_num=input_blob_num; } if (argc > idx) { sscanf(argv[idx++], "%d", &MPROCESS_FOR_AIISP); printf("MPROCESS_FOR_AIISP : %d\n", MPROCESS_FOR_AIISP); } if (argc > idx) { sscanf(argv[idx++], "%d", &dump_time_ut); printf("dump_time_ut: %d\n", dump_time_ut); } if (argc > idx) { sscanf(argv[idx++], "%d", &dump_time_proc); printf("dump_time_proc: %d\n", dump_time_proc); } if (argc > idx) { sscanf(argv[idx++], "%d", &dump_output); printf("dump_output: %d\n", dump_output); } if (argc > idx) { sscanf(argv[idx++], "%s", dump_path); printf("dump_path: %s\n", dump_path); } if (argc > idx) { sscanf(argv[idx++], "%s", in_cfg1.input_filename); printf("in_cfg1.input_filename: %s\n", in_cfg1.input_filename); } if (argc > idx) { sscanf(argv[idx++], "%d", &(in_cfg1.w)); printf("(in_cfg1.w): %d\n", (in_cfg1.w)); } if (argc > idx) { sscanf(argv[idx++], "%d", &(in_cfg1.h)); printf("(in_cfg1.h): %d\n", (in_cfg1.h)); } if (argc > idx) { sscanf(argv[idx++], "%d", &(in_cfg1.c)); printf("(in_cfg1.c): %d\n", (in_cfg1.c)); } if (argc > idx) { sscanf(argv[idx++], "%d", &(in_cfg1.b)); printf("(in_cfg1.b): %d\n", (in_cfg1.b)); } if (argc > idx) { sscanf(argv[idx++], "%d", &(in_cfg1.bitdepth)); printf("(in_cfg1.bitdepth): %d\n", (in_cfg1.bitdepth)); } if (argc > idx) { sscanf(argv[idx++], "%d", &(in_cfg1.loff)); printf("(in_cfg1.loff): %d\n", (in_cfg1.loff)); } if (argc > idx) { sscanf(argv[idx++], "%d", &(in_cfg1.fmt)); printf("(in_cfg1.fmt): %d\n", (in_cfg1.fmt)); } if (argc > idx) { sscanf(argv[idx++], "%d", &(in_cfg1.is_comb_img)); printf("(in_cfg1.is_comb_img): %d\n", (in_cfg1.is_comb_img)); } if (argc > idx) { sscanf(argv[idx++], "%s", in_cfg2.input_filename); printf("in_cfg2.input_filename: %s\n", in_cfg2.input_filename); } if (argc > idx) { sscanf(argv[idx++], "%d", &(in_cfg2.w)); printf("(in_cfg2.w): %d\n", (in_cfg2.w)); } if (argc > idx) { sscanf(argv[idx++], "%d", &(in_cfg2.h)); printf("(in_cfg2.h): %d\n", (in_cfg2.h)); } if (argc > idx) { sscanf(argv[idx++], "%d", &(in_cfg2.c)); printf("(in_cfg2.c): %d\n", (in_cfg2.c)); } if (argc > idx) { sscanf(argv[idx++], "%d", &(in_cfg2.b)); printf("(in_cfg2.b): %d\n", (in_cfg2.b)); } if (argc > idx) { sscanf(argv[idx++], "%d", &(in_cfg2.bitdepth)); printf("(in_cfg2.bitdepth): %d\n", (in_cfg2.bitdepth)); } if (argc > idx) { sscanf(argv[idx++], "%d", &(in_cfg2.loff)); printf("(in_cfg2.loff): %d\n", (in_cfg2.loff)); } if (argc > idx) { sscanf(argv[idx++], "%d", &(in_cfg2.fmt)); printf("(in_cfg2.fmt): %d\n", (in_cfg2.fmt)); } if (argc > idx) { sscanf(argv[idx++], "%d", &(in_cfg2.is_comb_img)); printf("(in_cfg2.is_comb_img): %d\n", (in_cfg2.is_comb_img)); } // cfg3 if (argc > idx) { sscanf(argv[idx++], "%s", in_cfg3.input_filename); printf("in_cfg3.input_filename: %s\n", in_cfg3.input_filename); } if (argc > idx) { sscanf(argv[idx++], "%d", &(in_cfg3.w)); printf("(in_cfg3.w): %d\n", (in_cfg3.w)); } if (argc > idx) { sscanf(argv[idx++], "%d", &(in_cfg3.h)); printf("(in_cfg3.h): %d\n", (in_cfg3.h)); } if (argc > idx) { sscanf(argv[idx++], "%d", &(in_cfg3.c)); printf("(in_cfg3.c): %d\n", (in_cfg3.c)); } if (argc > idx) { sscanf(argv[idx++], "%d", &(in_cfg3.b)); printf("(in_cfg3.b): %d\n", (in_cfg3.b)); } if (argc > idx) { sscanf(argv[idx++], "%d", &(in_cfg3.bitdepth)); printf("(in_cfg3.bitdepth): %d\n", (in_cfg3.bitdepth)); } if (argc > idx) { sscanf(argv[idx++], "%d", &(in_cfg3.loff)); printf("(in_cfg3.loff): %d\n", (in_cfg3.loff)); } if (argc > idx) { sscanf(argv[idx++], "%d", &(in_cfg3.fmt)); printf("(in_cfg3.fmt): %d\n", (in_cfg3.fmt)); } if (argc > idx) { sscanf(argv[idx++], "%d", &(in_cfg3.is_comb_img)); printf("(in_cfg3.is_comb_img): %d\n", (in_cfg3.is_comb_img)); } } // malloc for g_in & g_net g_in = (NET_IN *)malloc(sizeof(NET_IN)*16); g_net = (NET_PROC *)malloc(sizeof(NET_PROC)*16); if ((g_in == NULL) || (g_net == NULL)) { printf("fail to malloc g_in/g_net\n"); goto exit; } stream[0].in_path = 0; stream[0].net_path = 0; // init hdal if(MPROCESS_FOR_AIISP){ ret = hd_common_init(1); // multi-process for aiisp }else{ ret = hd_common_init(0); } //this is no longer need in 690 /* #if defined(_BSP_NS02201_) || defined(_BSP_NS02302_) // set project config for AI hd_common_sysconfig(0, (1<<16), 0, VENDOR_AI_CFG); //enable AI engine #endif */ // init mem { // config common pool network_mem_config(stream[0].net_path, &mem_cfg, &net_cfg); } #if defined(_BSP_NS02201_) || defined(_BSP_NS02302_) if(MPROCESS_FOR_AIISP){ ret = hd_common_mem_init(NULL); // multi-process for aiisp }else{ ret = hd_common_mem_init(&mem_cfg); } if (HD_OK != ret) { printf("hd_common_mem_init err: %d\r\n", ret); goto exit; } #endif // init all modules ret = init_module(); if (ret != HD_OK) { printf("init fail=%d\n", ret); goto exit; } // set open config for (j=0; j < stream[0].input_blob_num; j++) { if (j == 0) { ret = input_set_config((stream[0].in_path + j), &in_cfg1); } else if (j == 1) { ret = input_set_config((stream[0].in_path + j), &in_cfg2); } else if (j == 2) { ret = input_set_config((stream[0].in_path + j), &in_cfg3); } if (HD_OK != ret) { printf("in_path(%u) input_set_config fail=%d\n", (stream[0].in_path + j), ret); goto exit; } } // open video_liveview modules for(j=0; j < stream[0].input_blob_num; j++) { ret = input_open((stream[0].in_path + j)); if (ret != HD_OK) { printf("in_path(%u) input open fail=%d\n", (stream[0].in_path + j), ret); goto exit; } } ret = open_module(&stream[0]); if (ret != HD_OK) { printf("open fail=%d\n", ret); goto exit; } // start input_start(stream[0].in_path); network_user_start(&stream[0]); allocate_buf_by_out_path_list(stream[0].net_path); do { printf("Enter q to exit, r to run once\n"); key = GETCHAR(); if (key == 'r') { // run once network_user_oneshot(&stream[0]); continue; } if (key == 'q' || key == 0x3) { break; } } while(1); // stop input_stop(stream[0].in_path); network_user_stop(&stream[0]); exit: // close video_liveview modules ret = close_module(&stream[0]); if (ret != HD_OK) { printf("close fail=%d\n", ret); } for(j=0; j < stream[0].input_blob_num; j++) { ret = input_close((stream[0].in_path + j)); if (ret != HD_OK) { printf("in_path(%u) input close fail=%d\n", (stream[0].in_path + j), ret); goto exit; } } // uninit all modules ret = exit_module(); if (ret != HD_OK) { printf("exit fail=%d\n", ret); } #if defined(_BSP_NS02201_) || defined(_BSP_NS02302_) // uninit memory ret = hd_common_mem_uninit(); if (ret != HD_OK) { printf("mem fail=%d\n", ret); } #endif // uninit hdal ret = hd_common_uninit(); if (ret != HD_OK) { printf("common fail=%d\n", ret); } // free g_in & g_net if (g_in) free(g_in); if (g_net) free(g_net); return ret; }
最新发布
11-04
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值