I'm new to OpenMP and cannot for the life of me utilise multiple threads. I have my environment variable set. Here is a snippet of code which simply should just iterate through the mandelbrot set:
#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.
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.
#pragma omp parallel for schedule(dynamic)
for(...){}
can be faster for unknown work-intensity per pixel with speedup depending on the unbalance between pixels.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.