没有线程本地副本的 OpenMP 减少数组

如何解决没有线程本地副本的 OpenMP 减少数组

我希望 OpenMP 将一个 数组缩减为一个较小的动态数组。例如,其中

large[] = {1,1,2,3,3};

// OpenMP reduce length-3 sublists of large,to produce:

small[] = {3,6,9};

我的要求与 this question 类似,但我还有一些重要的附加限制:

  • 必须支持 OpenMP 3.1(所以我不能像 this answer 那样使用 OpenMP 4.5 的数组缩减)
  • 不能有 small 的线程私有副本(如 this answer),因为 small 可以和 large 一样大,因此线程私有副本可能导致堆栈溢出
  • large 数组必须按顺序迭代,以获得良好的缓存(因为它可能非常大

其他一些细节:

  • large 的相加元素不一定相邻
  • 这种减少发生在一个函数中。 small(输出)数组由用户预先分配,并通过引用传递。理想情况下,该函数不应分配 small
  • 的新本地副本
  • smalllarge 都具有 2 次幂的长度(例如 248、{{1} } ...)。 16 的每个元素都是从相同数量的 small 元素(2 的另一个幂)减少的。

为了清楚起见,以下是串行伪代码示例:

large

这是一个示例实现,由于它使用了 OpenMP 4.5 的数组缩减,因此不合格。此外,它不合需要地使用了 void myfunc(double* small,double* large,int lLen,// other args) { for (int i=0; i<lLen; i++) { int sInd = // determined by other args small[sInd] += large[i]; } } 的本地副本(根据使用 small 的要求)。

reduction

我希望我可以在 OpenMP 3.1 中实现同样的事情,甚至可能通过自己管理数组元素减少来避免 void myfunc(double* small,int sLen,// other args) { double smallLocal[sLen]; int i,sInd; #pragma omp parallel private (i) for for (i=0; i<sLen; s++) smallLocal[i] = 0; #pragma omp parallel private (i,sInd) reduction (+:smallLocal) for for (i=0; i<largeLen; i++) { sInd = // determined by other args smallLocal[sInd] += large[i]; } #pragma omp parallel private (i) for for (i=0; i<sLen; s++) small[i] = smallLocal[i]; } 的本地副本。我该怎么做?

解决方法

您可以使用 OpenMPs atomic update 构造,它已经存在于 OpenMP 3.1 中:

void myfunc(double* small,double* large,int lLen,// other args) {

    #pragma omp parallel for
    for (int i=0; i<lLen; i++) {
    
        int sInd = // determined by other args
        #pragma omp atomic update
        small[sInd] += large[i];
    }
}

这应该比使用锁更快。

,

这里有一个使用锁的不太令人满意的解决方案,它需要一个与 locks 一样大的本地 small 数组(可能需要 malloc'd)。

void myfunc(double* small,int sLen,// other args) {

    int i,sInd;

    // clear small
    #pragma omp parallel private (i) for
    for (i=0; i<sLen; s++)
        small[i] = 0;

    // initialise locks
#ifdef _OPENMP
    omp_lock_t locks[sLen];
    #pragma omp parallel private (i) for
    for (i=0; i<sLen; s++)
        omp_init_lock(&locks[i]);
#endif 
    
    // main loop
    #pragma omp parallel private (i,sInd) for
    for (i=0; i<largeLen; i++) {

        sInd = // determined by other args
        
        // update small with locks
#ifdef _OPENMP
        omp_set_lock(&locks[sInd]);
        small[sInd] += large[i];
        omp_unset_lock(&locks[sInd]);
#else 
        small[sInd] += large[i];
#endif
    }

    // destroy locks
#ifdef _OPENMP
    #pragma omp parallel private (i) for
    for (i=0; i<sLen; s++)
        omp_destroy_lock(&locks[i]);
#endif 
}

正如预期的那样,这比使用 OpenMP 4.5 的数组缩减要慢:

  • 对于 sLen = 8lLen = 1048576,使用锁的速度 26 倍
  • 对于 sLen = 32768lLen = 1048576,使用锁的速度 1.5 倍

需要说明的是,OpenMP 4.5 的数组缩减只兼容本地栈数组,不兼容动态内存。这将缩减数组的大小限制为 ~1 MiB,或 2^17 双倍 (lLen = 131072)。相比之下,这个锁解决方案可以处理任何大小,因为 locks 可以被 malloc'd。

,

你可以做这种在科学代码中很常见的事情:

#pragma omp parallel for
for(int i = 0; i < N/3; i++)
{
    for (int j = 0; j < 3; j++)
    {
        small[i] += large[3*i + j];
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res