分析细纹织物内联代码

2024-05-13 20:27:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我在python脚本中使用scipy.weave作为性能关键部分。如果可能的话,使用这些并行化的p代码。在某些情况下,我会遇到瓶颈,这可能是由于错误的共享。我该如何分析这些内联代码,即对Linux平台上合适的工具有什么建议?在

请看下面一个错误的向量加法实现,这很容易导致错误的共享。在

from scipy.weave import inline
import numpy as np
import time

N = 1000
a = np.random.rand(N)
b = np.random.rand(N)
c = np.random.rand(N)

cpus = 4
weave_options = {'headers'           : ['<omp.h>'],
                 'extra_compile_args': ['-fopenmp -O3'],
                 'extra_link_args'   : ['-lgomp'],
                 'compiler'          : 'gcc'}

code = \
r"""
omp_set_num_threads(cpus);
#pragma omp parallel
{
   #pragma omp for schedule(dynamic)
      for ( int i=0; i<N; i++ ){
         c[i] = a[i]+b[i];
      }
}
"""

now = time.time()
inline(code,['a','b','c','N','cpus'],**weave_options)
print "TOOK {0:.4f}".format(time.time()-now)
print "SUCCESS" if np.all(np.equal(a,a)) else "FAIL"

编辑:

可以使用

valgrind --tool=callgrind --simulate-cache=yes python ***.py

而且kcachegrind ./callgrind.out.****至少能得到一点 印象。但结果往往会变得一团糟 包装代码。在


Tags: 代码importtime错误npargsinlinerandom