为什么按照下面代码,我C代码创建的线程和python是同一个线程,应该如何修改才能使C代码线程和python线程独立开来
C代码如下:
void ep_int_hook_register( unsigned int bus_no,ep_read_dma_callback hook)
{
g_ep_irq_read_callback_info.bus_no = bus_no;
g_ep_irq_read_callback_info.hook[bus_no] = hook;
printf("ep_read_dma_callback.hook[%d] = 0x%x\n",\
bus_no,(unsigned int)(g_ep_irq_read_callback_info.hook[bus_no]));
}
int module_init(void)
{
int ret = 0;
usr_board_info_show();
/*pcie 配置寄存器和数据通道初始化*/
ret = pcie_reg_base_addr_init();
if(ret < 0)
{
printf("Pcie Cfg Reg Bar Init Failed...\n");
return ret;
}
ep_read_irq_pthead_init();
return ret;
}
int ep_read_irq_pthead_init(void)
{
int ret;
ret = pcie_thread_create_detached(NULL,ep_read_irq_thread,NULL);
if(ret < 0)
{
printf("ep read irq thread create Fail ! \n");
return -1;
}
return 0;
}
void* ep_read_irq_thread(void* param)
{
unsigned int data_len = 0;
unsigned int channel = 0;
unsigned int value = 0;
int ret = 0;
while(1)
{
sem_wait(&g_irq_sem[READ_IRQ_TYPE]); //等待接收读中断
g_ep_w_irq_num++;
pcie_reg_write(PCI_EP_W_IRQ_NUM_REG,g_ep_w_irq_num);
data_len = pcie_reg_read(EP_WRITE_LEN_TO_RC_REG);
if(0x2 == g_ep_w_irq_type)
{
/*调用注册的函数*/
if(g_ep_irq_read_callback_info.hook[0] != NULL)
{
ret = read(gEpDevFd,g_ep_dma_addr_info.r_vir_addr[g_ep_dma_addr_info.r_flag],data_len);
if(ret < 0)
{
printf("read error ret:%d.\n",ret);
return NULL;
}
g_ep_irq_read_callback_info.hook[0](0,channel, g_ep_dma_addr_info.r_vir_addr[g_ep_dma_addr_info.r_flag],data_len);
}
}
else if(0x1 == g_ep_w_irq_type)
{
printf("recv pcie pkg\n");
ret = read(gEpDevFd,g_ep_dma_addr_info.r_vir_addr[g_ep_dma_addr_info.r_flag],data_len);
if(ret < 0)
{
printf("read error ret:%d.\n",ret);
return NULL;
}
ret = deal_pcie_pkg(g_ep_dma_addr_info.r_vir_addr[g_ep_dma_addr_info.r_flag], data_len);
if(ret < 0)
{
printf("deal pcie pkg fail ret = %d\n", ret);
}
}
if(g_ep_dma_addr_info.r_flag == 0)
{
/*等待回调函数结束再切换地址*/
g_ep_dma_addr_info.r_flag = 1;
}
else
{
/*等待回调函数结束再切换地址*/
g_ep_dma_addr_info.r_flag = 0;
}
pcie_reg_write(PCI_R_ADDR_FLAG,g_ep_dma_addr_info.r_flag);
value = g_ep_dma_addr_info.r_addr[g_ep_dma_addr_info.r_flag] & 0xffffffff;
pcie_reg_write(RC_READ_LOW_ADDR_CFG_REG,value);
value = (g_ep_dma_addr_info.r_addr[g_ep_dma_addr_info.r_flag] >> 32) & 0xffffffff;
pcie_reg_write(RC_READ_HIGH_ADDR_CFG_REG,value);
}
}
int pcie_thread_create_detached(pthread_t *thread,void *(*thread_start)(void*),void *arg)
{
int err;
pthread_t thread_id;
pthread_attr_t attr;
err = pthread_attr_init (&attr);
if (err != 0)
{
printf(" pthread_attr_init() fail ");
return -1;
}
err = pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
if (err != 0)
{
printf("pthread_attr_setdetachstate"
"(PTHREAD_CREATE_DETACHED) failed");
return -1;
}
err = pthread_create(&thread_id,&attr, thread_start,(void*)arg);
if (err != 0)
{
printf(" pthread_create() fail ");
return -1;
}
if(thread != NULL)
{
*thread = thread_id;
}
(void) pthread_attr_destroy(&attr);
return 0;
}
python代码如下:
# coding:utf-8
# 运行双模型代码 20250416
import numpy as np
import onnxruntime as ort
import queue
from ctypes import *
import time
import multiprocessing
import os
from onnx_model import Predictor, npuPredictor
ort.set_default_logger_severity(3)
from util import post_pro, vis_image
import ctypes
from utils.acllite.acllite_model import AclLiteModel
from utils.acllite.acllite_resource import AclLiteResource
import random
import traceback
receive_queue = multiprocessing.Queue(maxsize=500) # 该队列存接收数据包
def ep_read_callback_test(irq_type,channel,addr,rcvlen):
print('*******************************')
print('tgC_test:recv data')
start = time.time()
data_size = rcvlen
data_buffer = ctypes.create_string_buffer(data_size)
ctypes.memmove(data_buffer,ctypes.c_void_p(addr),data_size)
head = np.frombuffer(data_buffer[:2], dtype=np.uint16) #头,data_buffer切片单位为1字节
if head[0] == 32382:
data = data_buffer[28:-4]
try:
receive_queue.put(data, block=False)
except queue.Full:
x = receive_queue.get()
print('receive_queue is full, size ', receive_queue.qsize())
else:
print('recv_data time', time.time()-start)
print('receive_queue input size ', receive_queue.qsize())
print('tgC_test:recv OK')
print('*******************************')
PCIEREADFUNC = CFUNCTYPE(None, c_uint, c_uint, c_ulonglong,c_uint)
pResutFunc = PCIEREADFUNC(ep_read_callback_test)
lib = cdll.LoadLibrary('/usr/lib/libpetEpApi.so')
def after_model(power_data, output, CLASSES, start_frequency, end_frequency, end_time, sum_sec, width, height, time_every_point, rbw, modelType):
results = b''
print('output:', np.array(output).shape)
if len(output) > 0:
output = output[output[:, 0] >= 0]
output = output[output[:, 1] >= 0]
output = output[output[:, 2] <= width]
output = output[output[:, 3] <= height]
clss = output[:, 5].tolist()
clss = [CLASSES[int(i)] for i in clss]
output[:, 4] = output[:, 4] * 100
bboxs = output[:, :5].astype(np.float32).astype(np.int32).tolist()
results, count = post_pro(power_data, bboxs, clss, start_frequency, end_frequency, end_time, sum_sec, width, height, time_every_point)
# print('len results', len(results))
if len(results)==0:
# 为了数据回环,造一个数据出来
maxa = np.argmax(power_data)
h, w = power_data.shape
xx = maxa - int(maxa/w)*w
results = end_time+sum_sec + np.array([1,(start_frequency+xx*rbw/1000000)*100*1000,(start_frequency+xx*(rbw+1)/1000000)*100*1000,rbw/10,0,101]).astype(np.uint32).tobytes()
results = results + np.array([1,2,3,4,1,0]).astype(np.uint16).tobytes()
results = results + np.array([(start_frequency+xx*rbw/1000000)*100*1000,10*100*1000,rbw/10,10,20,30]).astype(np.uint32).tobytes()
head = np.array([32382,0,0,0]).astype(np.uint16).tobytes()
datalen = np.array([len(results)+36+4]).astype(np.uint32).tobytes()
others = np.array([1,2,3,230,0,0,1,1]).astype(np.uint16).tobytes()
print('modelType:',modelType)
modelType = np.array([int(modelType)]).astype(np.uint32).tobytes()
sgnNum = np.array([0]).astype(np.uint32).tobytes() #用于回环,信号数先设为1?
tail = np.array([0,3338]).astype(np.uint16).tobytes()
real_send_data = head + datalen + others + modelType + sgnNum + results + tail
print('before send')
iRet = lib.usrPcieWriteData(real_send_data, len(real_send_data))
print('end send')
else:
head = np.array([32382,0,0,0]).astype(np.uint16).tobytes()
datalen = np.array([len(results)+36+4]).astype(np.uint32).tobytes()
others = np.array([1,2,3,230,0,0,1,1]).astype(np.uint16).tobytes()
print('modelType:',modelType)
modelType = np.array([int(modelType)]).astype(np.uint32).tobytes()
sgnNum = np.array([count]).astype(np.uint32).tobytes()
tail = np.array([0,3338]).astype(np.uint16).tobytes()
real_send_data = head + datalen + others + modelType + sgnNum + results + tail
print('before send')
iRet = lib.usrPcieWriteData(real_send_data, len(real_send_data))
print('end send')
# print('after time:', time.time()-start)
# print('+++++++++ end ++++++++++')
def infer_model(CLASSES1, CLASSES2, args):
try:
if args['device'] == 'npu':
model_path1 = os.path.join(args['model1_dir'], 'yolox_416_832.om')
model_path2 = os.path.join(args['model2_dir'], 'yolox_416_832.om')
predictor1 = npuPredictor(model_path1, cls_names=CLASSES1, test_conf=args['confidence'], nmsthre=args['nms'])
predictor2 = npuPredictor(model_path2, cls_names=CLASSES2, test_conf=args['confidence'], nmsthre=args['nms'])
print('model_path1: ', model_path1)
print('model_path2: ', model_path2)
else:
model_path1 = args['model1_dir'] + '/yolox.onnx'
model_path2 = args['model2_dir'] + '/yolox.onnx'
# print('model_path: ', model_path)
model1 = ort.InferenceSession(model_path1)
model2 = ort.InferenceSession(model_path2)
predictor1 = Predictor(model=model1, cls_names=CLASSES1, test_conf=args['confidence'], nmsthre=args['nms'], decoder=False,
device='cpu')
predictor2 = Predictor(model=model2, cls_names=CLASSES2, test_conf=args['confidence'], nmsthre=args['nms'], decoder=False,
device='cpu')
except Exception as e:
traceback.print_exc()
while True:
try:
start = time.time()
data = receive_queue.get()
end_time = data[:16]
sum_sec = data[16:20] #以秒为单位的时间,测试时只有这个有真实时间数据
# print('sum_sec', np.frombuffer(sum_sec, dtype=np.uint32)[0])
start_frequency = np.frombuffer(data[20:24], dtype=np.uint32)[0]
rbw = np.frombuffer(data[24:28], dtype=np.uint32)[0] #单位hz
rtw = np.frombuffer(data[28:32], dtype=np.uint32)[0]
frameLen = np.frombuffer(data[32:36], dtype=np.uint32)[0]
frameNum = np.frombuffer(data[36:40], dtype=np.uint32)[0]
# print('rbw',rbw) #1000
# print('rtw',rtw) #20
# print('frameLen', frameLen)
# print('frameNum', frameNum)
real_data = data[48:frameLen*frameNum*2*2+48]
power_data_orign = np.frombuffer(real_data, dtype=np.uint16)
# print(power_data.shape)
power_data_orign = power_data_orign.reshape(2,-1,frameLen) #临时
# print(power_data.shape)
# power_data = power_data_orign[0]
power_data = power_data_orign[0].copy()
power_data[np.isnan(power_data)] = 0
power_data[np.isinf(power_data)] = 0
power_data = power_data*0.25 #因为转换前单位是0.25db
end_frequency = (start_frequency + frameLen*rbw/1000)/1000 #单位是Mhz
# print('end_frequency',end_frequency) #200.82
start_frequency = start_frequency/1000 #单位是Mhz
# print('start_frequency',start_frequency) #
time_every_point = (rtw*10)/(10**6) #单位是毫秒
# print('time_every_point',time_every_point)
# print('power_data.shape', power_data.shape)
# print('power_data.max', power_data.max())
# print('power_data.max', power_data.min())
height, width = power_data.shape
pmin = power_data.min() + 5
power_data_min_max = power_data - pmin
power_data_min_max[power_data_min_max<0] = 0
# 专用模型
output1 = predictor1.inference(power_data_min_max)
after_model(power_data, output1, CLASSES1, start_frequency, end_frequency, end_time, sum_sec, width, height, time_every_point,rbw, 0)
# 通用模型
output2 = predictor2.inference(power_data_min_max)
after_model(power_data, output2, CLASSES2, start_frequency, end_frequency, end_time, sum_sec, width, height, time_every_point,rbw, 10)
print('*********************************infer_time:', time.time()-start)
except Exception as e:
traceback.print_exc()
def heartbeat():
try:
end_time = np.array([1,2,3,4,1,0]).astype(np.uint16).tobytes() + np.array([1]).astype(np.uint32).tobytes()
sum_sec = np.array([0]).astype(np.uint32).tobytes()
results = end_time+sum_sec + np.array([1,1,2,1,0,101]).astype(np.uint32).tobytes()
results = results + np.array([1,2,3,4,1,0]).astype(np.uint16).tobytes()
results = results + np.array([1,10*100*1000,1,10,20,30]).astype(np.uint32).tobytes()
head = np.array([32382,0,0,0]).astype(np.uint16).tobytes()
datalen = np.array([len(results)+36+4]).astype(np.uint32).tobytes()
others = np.array([1,2,3,224,0,0,1,1]).astype(np.uint16).tobytes()
modelType = np.array([0]).astype(np.uint32).tobytes()
sgnNum = np.array([0]).astype(np.uint32).tobytes()
tail = np.array([0,3338]).astype(np.uint16).tobytes()
real_send_data = head + datalen + others + modelType + sgnNum + results + tail
# print('heart send')
print('-----heartbeat-----')
iRet = lib.usrPcieWriteData(real_send_data, len(real_send_data))
# print('heart end send')
except Exception as e:
traceback.print_exc()
if __name__ == "__main__":
try:
iRet = 0
iRet = lib.module_init()
print("start register ")
lib.ep_int_hook_register(0,pResutFunc)
except Exception as e:
traceback.print_exc()
try:
acl_resource = AclLiteResource()
acl_resource.init()
args = {'model1_dir':'./models/model_22cls','model2_dir':'./models/model_dingpin', 'confidence':0.2, 'device':'npu', 'nms':0.15}
with open(os.path.join(args['model1_dir'], 'new_classes.txt'), 'r') as fp:
CLASSES1 = [line.strip('\n') for line in fp.readlines()]
CLASSES1 = [int(cls) for cls in CLASSES1]
with open(os.path.join(args['model2_dir'], 'new_classes.txt'), 'r') as fp:
CLASSES2 = [line.strip('\n') for line in fp.readlines()]
CLASSES2 = [int(cls) for cls in CLASSES2]
heartbeat()
infer_model(CLASSES1, CLASSES2, args)
except Exception as e:
traceback.print_exc()
最新发布