我试图了解 OMP 如何处理不同的for循环声明。我有:
int main()
{
int i, A[10000]={...};
double ave = 0.0;
#pragma omp parallel for reduction(+:ave)
for(i=0;i<10000;i++){
ave += A[i];
}
ave /= 10000;
printf("Average value = %0.4f\n",ave);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
其中{...}是从 1 到 10000 的数字。此代码打印正确的值。#pragma omp parallel for reduction(+:ave)如果我使用is#pragma omp parallel for private(ave)的结果代替。我想我明白什么是,但想知道它是否可以替代以及如何替代。printf0.0000reduction(oper:list)private
通过 OpenMP 4,您可以使用一组环境变量,例如OMP_PROC_BIND或OMP_PLACES。
这些变量是在编译时还是运行时使用的?假设我必须做一些改变这些值的实验,我是否必须每次都重新编译程序才能应用它们?或者只是重新运行它?
注意:我很难对此进行测试,因为目前我无法重新编译我的测试程序。
我在并行 for 循环内创建大量对象,以执行同一任务的细微变化。构造时,对象分配一些向量。循环看起来像这样:
#pragma omp parallel for schedule(dynamic)
for (long long unsigned i=0; i<nparam; i++) {
Foo x;
x.do_task();
x.save_results();
}
Run Code Online (Sandbox Code Playgroud)
它导致计算机内存不足。Foo这是因为在循环的所有迭代完成之前每个对象都不会被破坏。这可能是个问题,因为如果我要进行 100,000 次迭代,并且每个对象使用具有 10,000 个双精度的向量,则需要 8 GB(对吗?),这很大。
另一方面,如果每个Foo对象在循环迭代后被破坏,我必须在某处寻找内存泄漏。
我刚刚从 OpenMP 学习了KMP_AFFINITY。谁能告诉我为什么它的名字以K开头?许多变量都始于KMP_英特尔 OpenMP 实现。它代表内核吗?
我已经在 vscode 中正确安装并运行了 c/c++,我的 gcc 版本是 8.2.0 并且我已经安装了 MinGw
我使用 VS code 来运行我的 C 程序
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
int main(int argc, char* argv[])
{
int nthreads, tid;
{
tid = omp_get_thread_num();
printf("welcome to GFG from thread = %d\n", tid);
if (tid == 0){
nthreads = omp_get_num_threads();
printf("number of threads = %d\n", nthreads);
}
}
}
Run Code Online (Sandbox Code Playgroud)
但这没有用,因为
[Running] cd "c:\cexam\" && gcc openmp_helloword.c -o openmp_helloword && "c:\cexam\"openmp_helloword
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\WinstonLi\AppData\Local\Temp\ccAAWXl3.o:openmp_helloword.c:(.text+0xf): undefined reference to `omp_get_thread_num'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\WinstonLi\AppData\Local\Temp\ccAAWXl3.o:openmp_helloword.c:(.text+0x33): undefined reference to …Run Code Online (Sandbox Code Playgroud) 我想在循环parallel外部的区域中使用 OpenMP 缩减for。根据OpenMP 参考parallel,可以在区域中使用归约子句,因此不需要for循环 or 。sections
reduction (min:...)但是,当在某个区域使用 OpenMP 时parallel,我得到的结果不正确。但是,如果我对 a 使用完全相同的结构reduction (max:...),则结果是正确的。如果我使用循环进行最小减少(#pragma omp for reduction (min:...)),结果是正确的,但我认为这没有必要。这是我的代码:
#include <omp.h>
#include <iostream>
#include <limits>
#include <algorithm>
int main(){
auto minVar { std::numeric_limits<int>::max() };
auto maxVar { std::numeric_limits<int>::min() };
auto minVarLoop { std::numeric_limits<int>::max() };
#pragma omp parallel
{
int threadNo { omp_get_thread_num() };
#pragma omp reduction (min:minVar)
minVar = std::min(minVar, threadNo);
// minVar = minVar …Run Code Online (Sandbox Code Playgroud) 我不知道这是否在任何地方都有记录,如果有,我希望引用它,但是我在使用 OpenMP 时发现了一些意外的行为。我下面有一个简单的程序来说明这个问题。在这里,我将以点的形式讲述我期望该程序执行的操作:
正如您将看到的,线程之间共享的计数器对于第二个线程没有正确更改。但是,如果我将计数器转换为整数引用,我会得到预期的结果。这是一个简单的代码示例:
#include <mutex>
#include <thread>
#include <chrono>
#include <iostream>
#include <omp.h>
using namespace std;
using std::this_thread::sleep_for;
using std::chrono::milliseconds;
const int sleep_amount = 2000;
int main() {
int counter = 0; // if I comment this and uncomment the 2 lines below, I get the expected results
/* int c = 0; */
/* int &counter = c; */
omp_lock_t mut;
omp_init_lock(&mut);
int counter_1, counter_2;
#pragma omp parallel
#pragma omp …Run Code Online (Sandbox Code Playgroud) 我的任务是编写一个通过 OpenMP 卸载到 GPU 的程序。目前我使用 Intel oneAPI DPC++ 编译器编译我的代码icpxv2022.1.0 编译代码,目标是在后端使用 NVIDIA Tesla V100。请在下面找到我的相关部分Makefile:
MKLROOT = /lustre/system/local/apps/intel/oneapi/2022.2.0/mkl/latest
CXX = icpx
INC =-I"${MKLROOT}/include"
CXXFLAGS =-qopenmp -fopenmp-targets=spir64 ${INC} --gcc-toolchain=/lustre/system/local/apps/gcc9/9.3.0
LDFLAGS =-qopenmp -fopenmp-targets=spir64 -fsycl -L${MKLROOT}/lib/intel64
LDLIBS =-lmkl_sycl -lmkl_intel_lp64 -lmkl_sequential -lmkl_core -lsycl -lOpenCL -lstdc++ -lpthread -lm -ldl
${EXE}: ${OBJ}
${CXX} ${CXXFLAGS} $^ ${LDFLAGS} ${LDLIBS} -o $@
Run Code Online (Sandbox Code Playgroud)
该代码编译时没有错误和警告,但我不完全确定它在运行时确实使用了 GPU。
我在使用 OpenMP 并行化代码时遇到问题。我附上了可以重现我的问题的最简单的代码。
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n = 10;
int size = 1;
vector<double> vec(1, double(1.0));
double sum = 0.0;
#pragma omp parallel for private(vec) reduction(+: sum)
for (int i = 0; i != n; ++i)
{
/* in real case, complex operations applied on vec here */
sum += vec[0];
}
cout << "sum: " << sum << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我使用带标志 -fopenmp 的 g++ 进行编译,g++ 的错误消息提示“分段错误(核心已转储)”。我想知道代码有什么问题。
请注意,应将其设置为私有,因为在实际代码中,for 循环中vec …
是否有任何工具或应用程序将openmp转换为pthreads程序?请告诉我.