在C中优化2个循环

时间:2016-04-29 22:06:08

标签: c loops openmp vectorization

我有这个c代码来优化

#include <math.h>

void baseline (int n, float a[n][n],float b[n], float c[n]) 
{
    int i, j;

for (j=0; j<n; j++)
        for (i=0; i<n; i++)
        a[j][i] = expl (b[i] + c[j]);
}

所以我试图取出一个指数因为:exp(a + b)= exp(a)* exp(b)

for (j=0; j<n; j++)
{
    ej=exp(c[j]);
    for (i=0; i<n; i++)
    a[i][j] = exp (b[i]) *ej;
}

我有一个工具可以提供有关我的计划的信息以及更好地优化它的建议

Section 1.1: Source loop ending at line 9
=========================================

Composition and unrolling
-------------------------
It is composed of the loop 5
and is not unrolled or unrolled with no peel/tail loop.

Section 1.1.1: Binary loop #5
=============================

The loop is defined in /home/haddad/Documents/Sujet1/kernel.c:8-9
In the binary file, the address of the loop is: 400a08
Warnings:
Detected a function call instruction:
Ignoring called function instructions


1% of peak computational performance is used (0.20 out of 16.00 FLOP per cycle (GFLOPS @ 1GHz))

Obsolete instructions
---------------------
Detected X87 INSTRUCTIONS.
x87 is the legacy x86 extension to process FP values. This instruction set is much less efficient than SSE or AVX. In particular, it does not support vectorization (x87 units not being vector units).
A recent compiler should never generate x87 code as soon as you don't use any 80 bits FP elements or complex divides in your code.

If you don't need 80 bits precision, use only single or double precision elements in your code and then, if necessary, tune your compiler to make it generate only SSE or AVX instructions. 
If complex divides are used, use fcx-limited-range that is included in ffast-math (see manual for safe usage).


Code clean check
----------------
Detected a slowdown caused by scalar integer instructions (typically used for address computation).
By removing them, you can lower the cost of an iteration from 5.00 to 2.00 cycles (2.50x speedup).

Vectorization status
--------------------
Your loop is not vectorized (all SSE/AVX instructions are used in scalar mode).
Only 12% of vector length is used.


Vectorization
-------------
Your loop is processing FP elements but is NOT OR PARTIALLY VECTORIZED and could benefit from full vectorization.
By fully vectorizing your loop, you can lower the cost of an iteration from 5.00 to 0.75 cycles (6.67x speedup).
Since your execution units are vector units, only a fully vectorized loop can use their full power.

Two propositions:
 - Try another compiler or update/tune your current one:
 - Remove inter-iterations dependences from your loop and make it unit-stride.
  * If your arrays have 2 or more dimensions, check whether elements are accessed contiguously and, otherwise, try to permute loops accordingly:
C storage order is row-major: for(i) for(j) a[j][i] = b[j][i]; (slow, non stride 1) => for(i) for(j) a[i][j] = b[i][j]; (fast, stride 1)
  * If your loop streams arrays of structures (AoS), try to use structures of arrays instead (SoA):
for(i) a[i].x = b[i].x; (slow, non stride 1) => for(i) a.x[i] = b.x[i]; (fast, stride 1)


Bottlenecks
-----------
Front-end is a bottleneck.
The store unit is a bottleneck.

Try to reduce the number of stores.
For example, provide more information to your compiler:
 - hardcode the bounds of the corresponding 'for' loop,
 -  use the 'restrict' C99 keyword


Complex instructions
--------------------
Detected COMPLEX INSTRUCTIONS.

These instructions generate more than one micro-operation and only one of them can be decoded during a cycle and the extra micro-operations increase pressure on execution units.
CALL: 1 occurrences
FSTP: 1 occurrences

 - Pass to your compiler a micro-architecture specialization option:
  * use march=native.


Type of elements and instruction set
------------------------------------
1 SSE or AVX instructions are processing arithmetic or math operations on single precision FP elements in scalar mode (one at a time).


Matching between your loop (in the source code) and the binary loop
-------------------------------------------------------------------
The binary loop is composed of 1 FP arithmetical operations:
 - 1: addition or subtraction
The binary loop is loading 12 bytes (3 single precision FP elements).
The binary loop is storing 18 bytes (4 single precision FP elements).


Arithmetic intensity
--------------------
Arithmetic intensity is 0.03 FP operations per loaded or stored byte.


Unroll opportunity
------------------
Loop is data access bound.

我想:

- 如果可能的话,使用唯一的而不是2

- 使用OpenMP

监控循环

- 并将指数更改为另一种更便宜的表达式。

感谢。

1 个答案:

答案 0 :(得分:6)

您正在计算exp(b[i]) n次而不是一次:

float exp_b[n];
for (j=0; j<n; j++)
{
    exp_b[j] = exp(b[j]);
}

for (j=0; j<n; j++)
{
    ej=exp(c[j]);
    for (i=0; i<n; i++)
        a[i][j] = exp_b[i] *ej;
}

此解决方案仅调用exp函数2 * n次,而您的调用时间为n * n次,因此您可以保存(n - 2) * n exp个函数调用。