OpenMP로 다음 코드의 실행 속도를 높이려고합니다. 이 코드는 만델 브로를 계산하여 캔버스로 출력합니다.OpenMP로 코드 실행 속도가 느림
이 코드는 단일 스레드로 작동하지만 OpenMP를 사용하여 빠르게 처리하려고합니다. 개인 변수와 공유 변수를 여러 가지 조합으로 시도했지만 지금까지 아무 것도 작동하지 않는 것 같습니다. 코드는 OpenMP가 없으면 항상 느리게 실행됩니다 (50 000 반복 - 2 초 느림).
나는 우분투 16.04를 사용하고 있고 GCC로 컴파일 중이다.
는void calculate_mandelbrot(GLubyte *canvas, GLubyte *color_buffer, uint32_t w, uint32_t h, mandelbrot_f x0, mandelbrot_f x1, mandelbrot_f y0, mandelbrot_f y1, uint32_t max_iter) {
mandelbrot_f dx = (x1 - x0)/w;
mandelbrot_f dy = (y1 - y0)/h;
uint16_t esc_time;
int i, j;
mandelbrot_f x, y;
//timer start
clock_t begin = clock();
#pragma omp parallel for private(i,j,x,y, esc_time) shared(canvas, color_buffer)
for(i = 0; i < w; ++i) {
x = x0 + i * dx;
for(j = 0; j < h; ++j) {
y = y1 - j * dy;
esc_time = escape_time(x, y, max_iter);
canvas[ GET_R(i, j, w) ] = color_buffer[esc_time * 3];
canvas[ GET_G(i, j, w) ] = color_buffer[esc_time * 3 + 1];
canvas[ GET_B(i, j, w) ] = color_buffer[esc_time * 3 + 2];
}
}
//time calculation
clock_t end = clock();
double time_spent = (double)(end - begin)/CLOCKS_PER_SEC;
printf("%f\n",time_spent);
}
코드 사용
escape_time 기능 :
inline uint16_t escape_time(mandelbrot_f x0, mandelbrot_f y0, uint32_t max_iter) {
mandelbrot_f x = 0.0;
mandelbrot_f y = 0.0;
mandelbrot_f xtemp;
uint16_t iteration = 0;
while((x*x + y*y < 4) && (iteration < max_iter)) {
xtemp = x*x - y*y + x0;
y = 2*x*y + y0;
x = xtemp;
iteration++;
}
return iteration;
}
주석에 암시 같은 코드는이 저장소로부터 https://github.com/hortont424/mandelbrot
가능한 [OpenMP 시간 및 클록()은 두 가지 다른 결과를 계산합니다] (http://stackoverflow.com/questions/10673732/openmp-time-and-clock-calculates-two-different-results) –