/***************************************************
模块说明 :c2h测试范例
芯片类型 :Nios II
作 者 :
公 司 :杭州自由电子科技
:
http://www.freefpga.com电 话 :0571-85084089
修 改 :
日期时间 :200704
说 明 :本程序通过拷贝数据对比软件运行和C2H
加速来对比性能的提升
****************************************************/
#include <stdio.h>
#include <string.h>
#include <sys/alt_cache.h>
#include "sys/alt_alarm.h"
#define TRANSFER_LENGTH 1048576
#define ITERATIONS 100
/*********************************************
函数名:software_do_dma
功 能:整型数拷贝
输 入:
返 回:
备 注:该函数不使用C2H加速,是加速的对比函数
**********************************************/
int software_do_dma( int * __restrict__ dest_ptr, int * __restrict__ source_ptr, int length )
{
int i;
for( i = 0; i < (length / 4); i++ )
{
*dest_ptr++ = *source_ptr++;
}
return( 0 );
}
/*********************************************
函数名:c2h_do_dma
功 能:整型数拷贝
输 入:
返 回:
备 注:该函数使用C2H加速
**********************************************/
int c2h_do_dma( int * __restrict__ dest_ptr, int * __restrict__ source_ptr, int length )
{
int i;
for( i = 0; i < (length / 4); i++ )
{
*dest_ptr++ = *source_ptr++;
}
return( 0 );
}
/*********************************************
函数名:main
功 能:测试主函数
输 入:
返 回:
备 注:软件拷贝和C2H加速时间对比
**********************************************/
int main( void )
{
int i;
int *source_ptr, *dest_ptr;
int software_start_time, software_finish_time, software_total_time;
int c2h_start_time, c2h_finish_time, c2h_total_time;
// Buffers we'll be transferring to and from.
source_ptr = (int*)malloc(TRANSFER_LENGTH);
dest_ptr = (int*)malloc(TRANSFER_LENGTH);
// Fill the source buffer and erase the dest buffer
for( i = 0; i < (TRANSFER_LENGTH / 4); i++ )
{
*(source_ptr + i) = i;
*(dest_ptr + i) = 0x0;
}
printf("This simple program copies %d bytes of data from a source buffer to a destination buffer.\n", TRANSFER_LENGTH);
printf("The program performs %d iterations of the copy operation, and calculates the time spent.\n\n", ITERATIONS);
printf("software Copy beginning\n");
software_start_time = alt_nticks();
for( i = 0; i < ITERATIONS; i++ )
{
software_do_dma( dest_ptr, source_ptr, TRANSFER_LENGTH );
}
software_finish_time = alt_nticks();
if( memcmp( dest_ptr, source_ptr, TRANSFER_LENGTH ) )
{
printf("ERROR: Source and destination data do not match. Copy failed.\n");
}
else
{
printf("SUCCESS: Source and destination data match. Copy verified.\n");
}
software_total_time = ((software_finish_time - software_start_time) * 1000) /
alt_ticks_per_second();
printf("software Total time: %d ms\n", software_total_time);
printf("c2h Copy beginning\n");
c2h_start_time = alt_nticks();
for( i = 0; i < ITERATIONS; i++ )
{
c2h_do_dma( dest_ptr, source_ptr, TRANSFER_LENGTH );
}
c2h_finish_time = alt_nticks();
if( memcmp( dest_ptr, source_ptr, TRANSFER_LENGTH ) )
{
printf("ERROR: Source and destination data do not match. Copy failed.\n");
}
else
{
printf("SUCCESS: Source and destination data match. Copy verified.\n");
}
c2h_total_time = ((c2h_finish_time - c2h_start_time) * 1000) /
alt_ticks_per_second();
printf("c2h Total time: %d ms\n", c2h_total_time);
free(dest_ptr);
free(source_ptr);
return 0;
}
在数字应用开发板上实际测试结果如下:
software Copy beginning
SUCCESS: Source and destination data match. Copy verified.
software Total time: 57756 ms
c2h Copy beginning
SUCCESS: Source and destination data match. Copy verified.
c2h Total time: 10651 ms 性能提升了约6倍。