molen 发表于 2004-12-23 03:40:10

提高运算速度的有限元编程技巧

我们编写的有限元计算程序,解决具体问题往往需要大量运算时间。如果说计算一分种和计算两分钟相差不大,那么计算半天时间和计算五天时间就差别很大了。
在许多论文里看到,以下这些技巧,综合起来可以使原本五天完成的任务加快到一天内完成,并且实现起来也很容易,且一般不破坏程序的可读性。因此对于编程者来说是有必要了解的。
Real time programming approaches:
1)  avoid floating point numbers as much as possible:

x*y takes almost ten time longer if a and y are floating point numbers than if they are integers. If a parameter has float value, we can use scale-up integer to represent it as much as possible.

2)  avoid function calls as much as possible

calling a function takes extra computing resources because stack pop and push of the function parameters and returns. Short functions can be implemented using predefined macros. If function is necessary, we can reduce the parameter list at the cost of less safety when using more global variables.

3)  define a union for 2D array

union in C defines alias of data type. We define a union of a 2D array and a 1D array with the same size.

union TackUnion
{   
int array_2D;
int array_1D;
}TackName;

This is very useful in reducing computing time when clearing the 2D array.
for (int m=0;m<Width*Height;m++)
{  array_1D = 0;}

Another way is to get the address of a 2D array, which is an integer multiplication and summation.

4)  Use time efficient operator:

Less efficient            Time efficient
m+1    m++
2*x                           x<<2
5*x    x<<2+x
a*x*x+b*x+c  (a*x+b)*x+c
x=x+y    x+=y

5)  define register integer variables as array index

We can use register integers as array index; however we can not declare all integers as register integers because of the very limited number of CPU registers.

6)  the unrolling technique (矩阵运算的循环展开技术, 参考:    http://www.simwe.com/forum/viewthread.php?tid=35643

MaXPowerPLANe 发表于 2004-12-23 09:17:35

Re:提高运算速度的有限元编程技巧

对cpu运算的认识有一定境界!顶!

handong1994 发表于 2004-12-23 14:31:54

Re:提高运算速度的有限元编程技巧

几个小技巧:
1. 能用乘的时候不要用除。例如:a/100.0,可采用0.01*a。
2. 能用加的时候不用用乘。例如:a*2.0,可采用a+a。
3. 像switch to和if,else if,...,else语句编程时,计算次数多的应排在判断的前面。
4. 如斑竹所说,多采用迭代,而不要展开计算,例:x*x*x+x*x+x+1,改为((x+1)*x+1)*x+1,进行计算。
5. C和C++语言中多采用指针。
个人愚见,请多指教。

行星 发表于 2012-1-3 09:36:58

非常好,可惜刚看见!顶!
页: [1]
查看完整版本: 提高运算速度的有限元编程技巧