程序性能调优..
- 格式:doc
- 大小:409.00 KB
- 文档页数:23
实验题目:程序性能调优 实验要求:本次实验,要求针对每个函数、每个人均至少写出3种优化版本、并根据driver报告的结果进行性能分析
实验目的: 理解编译器,学习程序优化,从优化程序代码和程序执行速度两方面着手。
实验环境:WIN7 64位、ubuntu ,VMware workstation,
实验内容及操作步骤: 将下载下来的 kernels.c 中的 rotate、smooth 函数进行优化。 本实验的实验原理是通过循环展开、cache 友好、替换变量等手段来实现程序优化。
实验过程及分析: 由于优化代码较长,就不进行截图。 1. Naive_rotate 1)原代码 char naive_rotate_descr[] = "naive_rotate: Naive baseline implementation"; void naive_rotate(int dim, pixel *src, pixel *dst) { int i, j; for (i = 0; i < dim; i++) for (j = 0; j < dim; j++) dst[RIDX(dim-1-j, i, dim)] = src[RIDX(i, j, dim)]; } 2)分析 :这段代码的作用就是将所有的像素进行行列调位、导致整幅图画进行了 90 度旋转。P 从 defs.h 中可以找到 #define RIDX(i,j,n) ((i)*(n)+(j)) 。这段代码本来很短,但是从 cache 友好性来分析,这个代码的效率机会很低, 所以按照 cache 的大小, 应在存储的时候进行 32 个像素依次存储 (列 存储)。做到 cache 友好这样就可以可以大幅度提高效率。
#include #include #include "defs.h" team_t team = { "201308060228", /* 队名 */ "201308060228", /* 序号 */ "747660816@qq.com", /* 邮箱 */ "", /* Second member full name (leave blank if none) */ "" /* Second member email addr (leave blank if none) */
};
/* * naive_rotate - The naive baseline version of rotate */ char naive_rotate_descr[] = "naive_rotate: Naive baseline implementation"; void naive_rotate(int dim, pixel *src, pixel *dst) { int i, j; for (i = 0; i < dim; i++) for (j = 0; j < dim; j++) dst[RIDX(dim-1-j, i, dim)] = src[RIDX(i, j, dim)]; } /* * rotate - Your current working version of rotate * IMPORTANT: This is the version you will be graded on */ char rotate_descr[] = "rotate: Current working version,using pointer rather than computing address";
void rotate(int dim, pixel *src, pixel *dst) { int i; int j; int tmp1=dim*dim; int tmp2=dim *31; int tmp3=tmp1-dim; int tmp4=tmp1+32; int tmp5=dim+31; dst+=tmp3;
for(i=0; i< dim; i+=32) { for(j=0;j { *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; dst++;src+=dim; *dst=*src; src++; src-=tmp2; dst-=tmp5; } src+=tmp2; dst+=tmp4; } }
/********************************************************************* * register_rotate_functions - Register all of your different versions * of the rotate kernel with the driver by calling the * add_rotate_function() for each test function. When you run the * driver program, it will test and report the performance of each * registered test function. *********************************************************************/ char rotate_descr_v1[] = "rotate_v1: version1 break into 4*4 blocks"; void rotate_v1(int dim, pixel *src, pixel *dst) { int i, j,ii,jj; for(ii=0; ii < dim; ii+=4) for(jj=0; jj < dim; jj+=4) for(i=ii; i < ii+4; i++) for(j=jj; j < jj+4; j++)
dst[RIDX(dim-1-j,i,dim)] = src[RIDX(i,j,dim)];
}
char rotate_descr_v2[] = "rotate_v2: version2 break into 32*32 blocks"; void rotate_v2(int dim, pixel *src, pixel *dst) { int i, j,ii,jj; for(ii=0; ii < dim; ii+=32) for(jj=0; jj < dim; jj+=32) for(i=ii; i < ii+32; i++) for(j=jj; j < jj+32; j++)
dst[RIDX(dim-1-j,i,dim)] = src[RIDX(i,j,dim)];
}
char rotate_descr_v3[] = "rotate_v3: version3 break into 4*1 blocks with 4 parallel paths";