首页 文章

Cython中C函数性能不佳

提问于
浏览
7

我有这个C函数,我可以用Python调用下面的代码 . 与运行纯C相比,性能只有一半 . 有没有办法让他们的表现达到同一水平?我用 -Ofast -march=native 标志编译了两个代码 . 我不明白我可以在哪里失去50%,因为大部分时间都应该花在C内核上 . Cython是否制作了我可以避免的内存副本?

namespace diff
{
    void diff_cpp(double* __restrict__ at, const double* __restrict__ a, const double visc,
                  const double dxidxi, const double dyidyi, const double dzidzi,
                  const int itot, const int jtot, const int ktot)
    {
        const int ii = 1;
        const int jj = itot;
        const int kk = itot*jtot;

        for (int k=1; k<ktot-1; k++)
            for (int j=1; j<jtot-1; j++)
                for (int i=1; i<itot-1; i++)
                {
                    const int ijk = i + j*jj + k*kk;
                    at[ijk] += visc * (
                            + ( (a[ijk+ii] - a[ijk   ]) 
                              - (a[ijk   ] - a[ijk-ii]) ) * dxidxi 
                            + ( (a[ijk+jj] - a[ijk   ]) 
                              - (a[ijk   ] - a[ijk-jj]) ) * dyidyi
                            + ( (a[ijk+kk] - a[ijk   ]) 
                              - (a[ijk   ] - a[ijk-kk]) ) * dzidzi
                            );
                }
    }
}

我有这个 .pyx 文件

# import both numpy and the Cython declarations for numpy
import cython
import numpy as np
cimport numpy as np

# declare the interface to the C code
cdef extern from "diff_cpp.cpp" namespace "diff":
    void diff_cpp(double* at, double* a, double visc, double dxidxi, double dyidyi, double dzidzi, int itot, int jtot, int ktot)

@cython.boundscheck(False)
@cython.wraparound(False)
def diff(np.ndarray[double, ndim=3, mode="c"] at not None,
         np.ndarray[double, ndim=3, mode="c"] a not None,
         double visc, double dxidxi, double dyidyi, double dzidzi):
    cdef int ktot, jtot, itot
    ktot, jtot, itot = at.shape[0], at.shape[1], at.shape[2]
    diff_cpp(&at[0,0,0], &a[0,0,0], visc, dxidxi, dyidyi, dzidzi, itot, jtot, ktot)
    return None

我在Python中调用此函数

import numpy as np
import diff
import time

nloop = 20;
itot = 256;
jtot = 256;
ktot = 256;
ncells = itot*jtot*ktot;

at = np.zeros((ktot, jtot, itot))

index = np.arange(ncells)
a = (index/(index+1))**2
a.shape = (ktot, jtot, itot)

# Check results
diff.diff(at, a, 0.1, 0.1, 0.1, 0.1)
print("at={0}".format(at.flatten()[itot*jtot+itot+itot//2]))

# Time the loop
start = time.perf_counter()
for i in range(nloop):
    diff.diff(at, a, 0.1, 0.1, 0.1, 0.1)
end = time.perf_counter()

print("Time/iter: {0} s ({1} iters)".format((end-start)/nloop, nloop))

这是 setup.py

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

import numpy

setup(
    cmdclass = {'build_ext': build_ext},
    ext_modules = [Extension("diff",
                             sources=["diff.pyx"],
                             language="c++",
                             extra_compile_args=["-Ofast -march=native"],
                             include_dirs=[numpy.get_include()])],
)

这里的C参考文件达到了两倍的性能:

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <stdlib.h>
#include <cstdio>
#include <ctime>
#include "math.h"

void init(double* const __restrict__ a, double* const __restrict__ at, const int ncells)
{
    for (int i=0; i<ncells; ++i)
    {
        a[i]  = pow(i,2)/pow(i+1,2);
        at[i] = 0.;
    }
}

void diff(double* const __restrict__ at, const double* const __restrict__ a, const double visc, 
          const double dxidxi, const double dyidyi, const double dzidzi, 
          const int itot, const int jtot, const int ktot)
{
    const int ii = 1;
    const int jj = itot;
    const int kk = itot*jtot;

    for (int k=1; k<ktot-1; k++)
        for (int j=1; j<jtot-1; j++)
            for (int i=1; i<itot-1; i++)
            {
                const int ijk = i + j*jj + k*kk;
                at[ijk] += visc * (
                        + ( (a[ijk+ii] - a[ijk   ]) 
                          - (a[ijk   ] - a[ijk-ii]) ) * dxidxi 
                        + ( (a[ijk+jj] - a[ijk   ]) 
                          - (a[ijk   ] - a[ijk-jj]) ) * dyidyi
                        + ( (a[ijk+kk] - a[ijk   ]) 
                          - (a[ijk   ] - a[ijk-kk]) ) * dzidzi
                        );
            }
}

int main()
{
    const int nloop = 20;
    const int itot = 256;
    const int jtot = 256;
    const int ktot = 256;
    const int ncells = itot*jtot*ktot;

    double *a  = new double[ncells];
    double *at = new double[ncells];

    init(a, at, ncells);

    // Check results
    diff(at, a, 0.1, 0.1, 0.1, 0.1, itot, jtot, ktot); 
    printf("at=%.20f\n",at[itot*jtot+itot+itot/2]);

    // Time performance 
    std::clock_t start = std::clock(); 

    for (int i=0; i<nloop; ++i)
        diff(at, a, 0.1, 0.1, 0.1, 0.1, itot, jtot, ktot); 

    double duration = (std::clock() - start ) / (double)CLOCKS_PER_SEC;

    printf("time/iter = %f s (%i iters)\n",duration/(double)nloop, nloop);

    return 0;
}

1 回答

  • 4

    这里的问题不是在运行期间发生的事情,而是在编译期间发生了哪种优化 .

    进行哪种优化取决于编译器(甚至版本),并且无法保证可以完成的每项优化都将完成 .

    实际上,cython速度较慢有两个不同的原因,这取决于你是使用g还是clang:

    由于cython构建中的flag -fwrapv

    • g无法优化

    • clang无法首先进行优化(请继续阅读以了解会发生什么) .


    First issue (g++) :与纯c程序的标志相比,Cython编译了不同的标志,因此无法进行一些优化 .

    如果查看设置日志,您会看到:

    x86_64-linux-gnu-gcc ... -O2 ..-fwrapv .. -c diff.cpp ... -Ofast -march=native
    

    正如你所说, -Ofast 将赢得 -O2 ,因为它是最后一次 . 但问题是 -fwrapv ,这似乎阻止了一些优化,因为有符号整数溢出不能被认为是UB并且不再用于优化 .

    所以你有以下选择:

    • -fno-wrapv 添加到 extra_compile_flags ,缺点是,所有文件现在都使用已更改的标志进行编译,这可能是不需要的 .

    • 从cpp构建一个只有你喜欢的标志的库并将它链接到你的cython模块 . 这个解决方案有一些开销,但具有健壮的优势:正如你指出的不同的编译器不同的cython-flags可能是问题所在 - 所以第一个解决方案可能太脆弱了 .

    • 不确定您是否可以禁用默认标志,但也许文档中有一些信息 .


    Second issue (clang++) 在测试cpp程序中内联 .

    当我用我很老的5.4版g编译你的cpp程序时:

    g++ test.cpp -o test -Ofast -march=native -fwrapv
    

    与没有 -fwrapv 的编译相比,它几乎变慢了3倍 . 然而,这是优化器的一个弱点:当内联时,应该看到,没有符号整数溢出是可能的(所有维度都是 256 ),因此标志 -fwrapv 不应该有任何影响 .

    我的旧版本 clang++ -version(3.8)似乎在这里做得更好:上面的标志我看不到任何性能下降 . 我需要通过 -fno-inline 禁用内联成为一个较慢的代码,但即使没有 -fwrapv 它也会更慢,即:

    clang++ test.cpp -o test -Ofast -march=native -fno-inline
    

    所以有一个系统的偏见支持你的c程序:优化器可以在内联后优化已知值的代码 - 这是cython无法做到的 .

    所以我们可以看到:clang无法使用任意大小优化 function diff 但是能够针对size = 256优化它 . 但是,Cython只能使用 diff 的非优化版本 . 这就是为什么 -fno-wrapv 没有积极影响的原因 .

    我从中获取:不允许在cpp-tester中内联感兴趣的函数(例如在自己的目标文件中编译它)以确保与cython的基础,否则会看到为此特别优化的程序的性能一个输入 .


    注意:有趣的是,如果所有 intunsigned int 替换,那么 -fwrapv 自然不起任何作用,但 unsigned int 的版本与 int -version一样慢 -fwrapv ,这是合乎逻辑的,因为那里没有未定义的行为可供利用 .

相关问题