简体   繁体   English

OpenMP 不产生额外的线程

[英]OpenMP not spawning additional threads

I'm new to OpenMP and cannot for the life of me utilise multiple threads.我是 OpenMP 的新手,无法终生使用多线程。 I have my environment variable set.我设置了环境变量。 Here is a snippet of code which simply should just iterate through the mandelbrot set:这是一段代码,它应该只是遍历 mandelbrot 集:

#include <omp.h>
#include <limits>

#define WIDTH 10000
#define HEIGHT 10000
#define INFINITY 2.0f
#define ITERATIONS 1000

using namespace std;


int main()
{

    #pragma omp parallel for
    for (size_t py = 0; py < HEIGHT; py++) {
        for (size_t px = 0; px < WIDTH; px++) {

            float x0 = -2.5f + (px * (1.0f - -2.5f) / WIDTH);
            float y0 = 1.0f + (py * (-1.0f - 1.0f) / HEIGHT);

            unsigned short iteration;
            float x = 0.0f;
            float y = 0.0f;
            for (iteration = 0; iteration < ITERATIONS; iteration++) {
                float xn = x * x - y * y + x0;
                y = 2 * x * y + y0;
                x = xn;
                if (x * x + y * y > INFINITY) {
                    break;
                }
            }
        }
    }
}

Whenever I run this, it never spawns additional threads.每当我运行它时,它都不会产生额外的线程。 I feel I'm doing something horribly wrong.我觉得我做错了什么。 Any help would be appreciated, thanks.任何帮助将不胜感激,谢谢。

I needed at add the flag -fopenmp to my compiler arguments. Now works properly.我需要将标志-fopenmp添加到我的编译器 arguments。现在可以正常工作了。

Just to add to the missing -fopenmp flag, OpenMP supports different work scheduling modes and the simple equal work distribution may not reach high efficiency on the run-time minimization for unbalanced workloads like Mandelbrot-Set generation.补充一下缺少的-fopenmp标志,OpenMP 支持不同的工作调度模式,对于像 Mandelbrot-Set 生成这样的不平衡工作负载,简单的平均工作分配可能无法在运行时最小化上达到高效率。

#pragma omp parallel for schedule(dynamic)
for(...){}

can be faster for unknown work-intensity per pixel with speedup depending on the unbalance between pixels.对于每个像素未知的工作强度,加速取决于像素之间的不平衡,可以更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM