如何使用openmp为数学函数“ exp”生成simd代码?

时间:2018-11-13 11:58:28

标签: openmp simd

我有一个简单的c代码,如下所示

void calculate_exp(float *out, float *in, int size) {
    for(int i = 0; i < size; i++) {
        out[i] = exp(in[i]);
    }
}

我想使用open-mp simd优化它。我是Open-mp的新手,并使用过一些杂注,例如“ omp simd”,“ omp simd safelen”等。但是我无法生成simd代码。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:3)

您可以使用以下四个替代方法之一对exp函数进行矢量化处理。 请注意,我使用expf(浮点数)而不是exp(这是double函数)。 此Godbolt link表明这些函数是矢量化的:在编译器生成的代码中搜索call _ZGVdN8v___expf_finite

#include<math.h>

int exp_vect_a(float* x, float* y, int N) {
    /* Inform the compiler that N is a multiple of 8, this leads to shorter code */
    N = N & 0xFFFFFFF8;    
    x = (float*)__builtin_assume_aligned(x, 32); /* gcc 8.2 doesn't need aligned x and y  to generate `nice` code */
    y = (float*)__builtin_assume_aligned(y, 32); /* with gcc 7.3 it improves the generated code                   */
    #pragma omp simd             
    for(int i=0; i<N; i++) y[i] = expf(x[i]);
    return 0; 
}


int exp_vect_b(float* restrict x, float* restrict y, int N) {
    N = N & 0xFFFFFFF8;
    x = (float*)__builtin_assume_aligned(x, 32); /* gcc 8.2 doesn't need aligned x and y  to generate `nice` code */
    y = (float*)__builtin_assume_aligned(y, 32); /* with gcc 7.3 it improves the generated code                   */
    for(int i=0; i<N; i++) y[i] = expf(x[i]);
    return 0; 
}

/* This also vectorizes, but it doesn't lead to `nice` code */
int exp_vect_c(float* restrict x, float* restrict y, int N) {
    for(int i=0; i<N; i++) y[i] = expf(x[i]);
    return 0; 
}

/* This also vectorizes, but it doesn't lead to `nice` code */
int exp_vect_d(float* x, float* y, int N) {
    #pragma omp simd             
    for(int i=0; i<N; i++) y[i] = expf(x[i]);
    return 0; 
}

请注意,Peter Cordes' comment在这里非常相关: 函数_ZGVdN8v___expf_finite的结果可能与expf略有不同 因为它的重点是速度,而不是特殊情况,例如输入 无限,次正规或非数字。 而且,精度是4ulp最大相对误差, 这可能比标准expf函数的精度稍差。 因此,您需要优化级别-Ofast(这样可以降低代码的准确性) 而不是-O3来使用gcc对代码进行向量化。

有关更多详细信息,请参见this libmvec page

以下测试代码可以在gcc 7.3上编译并成功运行:

#include <math.h>
#include <stdio.h>
/* gcc expv.c -m64 -Ofast -std=c99 -march=skylake -fopenmp -lm */

int exp_vect_d(float* x, float* y, int N) {
    #pragma omp simd             
    for(int i=0; i<N; i++) y[i] = expf(x[i]);
    return 0; 
}

int main(){
    float x[32];
    float y[32];
    int i;
    int N = 32;

    for(i = 0; i < N; i++) x[i] = i/100.0f;
    x[10]=-89.0f;            /* exp(-89.0f)=2.227e-39 which is a subnormal number */
    x[11]=-1000.0f;          /* output: 0.0                                   */
    x[12]=1000.0f;           /* output: Inf.                                  */
    x[13]=0.0f/0.0f;         /* input: NaN: Not a number                      */
    x[14]=1e20f*1e20f;       /* input: Infinity                               */
    x[15]=-1e20f*1e20f;      /* input: -Infinity                              */
    x[16]=2.3025850929940f;  /* exp(2.3025850929940f)=10.0...                 */
    exp_vect_d(x, y, N);
    for(i = 0; i < N; i++) printf("x=%11.8e,  y=%11.8e\n", x[i], y[i]);
    return 0;
}
相关问题