gpt4 book ai didi

cuda - openacc 与 openmp 和 mpi 的区别?

转载 作者:行者123 更新时间:2023-12-01 16:30:33 27 4
gpt4 key购买 nike

我想知道 openacc 和 openmp 之间的主要区别是什么。MPI、cuda 和 opencl 怎么样?我了解openmp和mpi之间的区别,特别是关于共享和分布式内存的部分它们中的任何一个都允许混合 GPU-CPU 处理设置吗?

最佳答案

OpenMP 和 OpenACC 支持基于指令的并行编程。

OpenMP 支持在共享内存计算平台(例如多核 CPU)上进行并行编程。它非常容易使用,因为它足以告诉编译器一些关于如何提取并行性的指令(代码注释或编译指示),从而触发输入源代码的并行版本的合成。

带有编译指示的 OpenMP“Hello World”程序示例如下

#include <omp.h>
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[])
{
int nthreads, tid;

/* Fork a team of threads giving them their own copies of variables */
#pragma omp parallel private(nthreads, tid)

{
/* Obtain thread number */
tid = omp_get_thread_num();
printf("Hello World from thread = %d\n", tid);

/* Only master thread does this */
if (tid == 0)
{
nthreads = omp_get_num_threads();
printf("Number of threads = %d\n", nthreads);
}

} /* All threads join master thread and disband */

}

上述代码来源为OpenMP Exercise从那里你会发现许多其他的例子。在这个“Hello World”示例中,主线程将输出涉及的线程数,而每个线程将打印 Hello World from thread = xxx

OpenACC 是编译器指令的集合,用于指定由附加加速器(如 GPU)加速的 C/C++ 或 Fortran 代码部分。它遵循与 OpenMP 几乎相同的理念,并且能够创建高级主机+加速器程序,同样无需管理加速器编程语言。例如,OpenACC 可以让您简单地加速现有的 C/C++ 代码,而无需学习 CUDA(当然会有一些性能损失)。

典型的 OpenACC 代码类似于以下内容

#pragma acc kernels loop gang(32), vector(16)
for (int j=1; j<n-1; j++)
{
#pragma acc loop gang(16), vector(32)
for (int i=1; i<m-1; i++)
{
Anew[j][i] = 0.25f * (A[j][i+1] + A[j-1][i]);
...
}
}

以上源码摘自博客An OpenACC Example (Part 1) ,您可以在其中找到一些更有用的 Material 来了解 OpenMP 和 OpenACC 之间的区别。

其他来源如下

How does the OpenACC API relate to the OpenMP API? .

OpenACC and OpenMP directives

Shane Cook,CUDA 编程,Morgan Kaufmann(第 10 章)

由于其本质,OpenACC 支持混合 CPU+GPU 编程。您还可以混合使用 OpenMP 和 OpenACC 指令。例如,在 4-GPU 系统中,您可以创建 4 个 CPU 线程,将计算工作卸载到 4 个可用 GPU。肖恩·库克(Shane Cook)的书中对此进行了描述。但是,应该提到的是,OpenMP 4.0 还预见了将工作卸载到附加加速器的指令,请参阅

OpenMP Technical Report 1 on Directives for Attached Accelerators

关于cuda - openacc 与 openmp 和 mpi 的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19494786/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com