有时,一个人的代码必须简单地执行,而诸如美观、“聪明”或对单一语言解决方案的承诺之类的原则则完全不适用。
在 tprc 我做了一个演讲(这里是幻灯片)关于如何做到这一点
可以针对生物信息学应用程序完成,但我认为有必要使用一个更简单的示例来说明最大化 perl 性能的潜在场所
程序员在数据密集型应用程序中工作时可以随意使用。
所以这是一个玩具问题来说明这些选项。给定一个非常大的双精度浮点数组使用以下函数将它们变换:cos(sin(sqrt(x)))。
该函数有 3 个嵌套的浮点运算。这是一个评估成本高昂的函数,尤其是在必须计算大量值的情况下。我们可以合理生成
使用以下代码快速获取 perl 中的数组值(以及我们将要检查的解决方案的一些副本):
my $num_of_elements = 50_000_000;my @array0 = map { rand } 1 .. $num_of_elements; ## generate random numbersmy @array1 = @array0; ## copy the arraymy @array2 = @array0; ## another copymy @array3 = @array0; ## yet another copymy @rray4 = @array0; ## the last? copymy $array_in_pdl = pdl(@array0); ## convert the array to a pdl ndarraymy $array_in_pdl_copy = $array_in_pdl->copy; ## copy the pdl ndarray
登录后复制
可能的解决方案包括以下:
在 perl 中使用 for 循环进行就地修改。
for my $elem (@array0) { $elem = cos( sin( sqrt($elem) ) );}
登录后复制
使用内联 c 代码遍历数组并在 c 中就地转换。 。有效地使用 c 进行就地映射。在 c 中访问 perl 数组(c 中的 av*)的元素尤其如此
如果使用 perl 5.36 及更高版本,则性能更高,因为该版本的 perl 中引入了优化的获取函数。
void map_in_c(av *array) { int len = av_len(array) + 1; for (int i = 0; i < len; i++) { sv **elem = av_fetch_simple(array, i, 0); // perl 5.36 and above if (elem != null) { double value = svnv(*elem); value = cos(sin(sqrt(value))); // modify the value sv_setnv(*elem, value); } }}
登录后复制
使用内联 c 代码来转换数组,但将转换分解为 3 个连续的 c for 循环。 这是一个真正关于权衡的实验:现代 x86 处理器有一个专门的,
向量化平方根指令,因此编译器也许可以弄清楚如何使用它来加速至少一部分计算。另一方面,我们将降低算术强度
每个循环并访问相同的数据值两次,因此可能会为这些重复的数据访问付出代价。
void map_in_c_sequential(av *array) { int len = av_len(array) + 1; for (int i = 0; i < len; i++) { sv **elem = av_fetch_simple(array, i, 0); // perl 5.36 and above if (elem != null) { double value = svnv(*elem); value = sqrt(value); // modify the value sv_setnv(*elem, value); } } for (int i = 0; i < len; i++) { sv **elem = av_fetch_simple(array, i, 0); // perl 5.36 and above double value = svnv(*elem); value = sin(value); // modify the value sv_setnv(*elem, value); } for (int i = 0; i < len; i++) { sv **elem = av_fetch_simple(array, i, 0); // perl 5.36 and above double value = svnv(*elem); value = cos(value); // modify the value sv_setnv(*elem, value); }}
登录后复制
使用 openmp 并行化 c 函数循环。 在上一篇文章中,我们讨论了如何从 perl 中控制 openmp 环境并编译 openmp 感知的 inline::c 代码
由 perl 使用,所以让我们将这些知识付诸实践!在程序的 perl 方面,我们将这样做:
use v5.38;use alien::openmp;use openmp::environment;use inline ( c => 'data', with => qw/alien::openmp/,);my $env = openmp::environment->new();my $threads_or_workers = 8; ## or any other value## modify number of threads and make c aware of the change$env->omp_num_threads($threads_or_workers);_set_num_threads_from_env();## modify runtime schedule and make c aware of the change$env->omp_schedule("guided,1"); ## modify runtime schedule_set_openmp_schedule_from_env();
登录后复制
在程序的 c 部分,我们将执行此操作(已经讨论了 openmp 环境的辅助函数
之前,因此这里不再重复)。
#include void map_in_c_using_omp(av *array) { int len = av_len(array) + 1;#pragma omp parallel {#pragma omp for schedule(runtime) nowait for (int i = 0; i < len; i++) { sv **elem = av_fetch_simple(array, i, 0); // perl 5.36 and above if (elem != null) { double value = svnv(*elem); value = cos(sin(sqrt(value))); // modify the value sv_setnv(*elem, value); } } }}
登录后复制
perl 数据语言 (pdl) 可以拯救你。 pdl 模块集是另一种加速操作的方法,可以将程序员从 c 语言中解救出来。它还能在给定正确指令的情况下自动并行化,所以为什么不使用它呢?
use pdl;## set the minimum size problem for autothreading in pdlset_autopthread_size(0);my $threads_or_workers = 8; ## or any other value## pdl## use pdl to modify the array - multi threadedset_autopthread_targ($threads_or_workers);$array_in_pdl->inplace->sqrt;$array_in_pdl->inplace->sin;$array_in_pdl->inplace->cos;## use pdl to modify the array - single threadset_autopthread_targ(0);$array_in_pdl_copy->inplace->sqrt;$array_in_pdl_copy->inplace->sin;$array_in_pdl_copy->inplace->cos;
登录后复制
使用8个线程我们得到这样的东西
inplace benchmarksinplace in perl took 2.85 secondsinplace in perl/mapcseq took 1.62 secondsinplace in perl/mapc took 1.54 secondsinplace in perl/c/omp took 0.24 secondspdl benchmarksinplace in pdl - st took 0.94 secondsinplace in pdl - mt took 0.17 seconds
登录后复制
使用16个线程我们得到了这个!
Starting the benchmark for 50000000 elements using 16 threads/workersInplace benchmarksInplace in Perl took 3.00 secondsInplace in Perl/mapCseq took 1.72 secondsInplace in Perl/mapC took 1.62 secondsInplace in Perl/C/OMP took 0.13 secondsPDL benchmarksInplace in PDL - ST took 0.99 secondsInplace in PDL - MT took 0.10 seconds
登录后复制
一些观察:
openmp 和 pdl 的多线程 (mt) 会响应工作线程的数量,而解决方案则不会。因此,这些基准测试中纯 perl 和内联非 openmp 解决方案的时序给出了性能自然变化的想法用 c 语言编写地图版本的代码,性能提高了约 180%(对比 perl 和 perl/mapc)。在单线程中使用 pdl 性能提高了 285-300%(对比 pdl – st 和 perl 计时)。重复内存访问是要付出代价的(对比 perl/mapc 与 perl/mapcseq)openmp 和多线程 pdl 操作提供了类似的性能(尽管 pdl 在这些示例中显得更快)。代码运行速度快了 23-30 倍。总之,在 perl 中,有原生(pdl 模块)和外来(c/openmp)解决方案来加速数据密集型操作,那么为什么不广泛而明智地使用它们来提高 perl 程序的性能呢?
以上就是追求性能第一部分:内联 C、OpenMP 和 Perl 数据语言 (PDL)的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2459789.html