gpt4 book ai didi

c - 优化 C 中的 2 个循环

转载 作者:太空狗 更新时间:2023-10-29 15:26:00 25 4
gpt4 key购买 nike

我有这个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.

我愿意:

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

-使用 OpenMP 向量化循环

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

谢谢。

最佳答案

你正在计算 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 函数调用。

关于c - 优化 C 中的 2 个循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36948169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com