我有一个函数计算mandelbrot
设置我尝试使用openMP
并行化它。
我在#pragma omp parallel for private
for
static void calculer (Image * im, int nb_iter, double x_min, double x_max, double y_min, double y_max) {
/* Discretisation de l'ensemble */
double pasx = (x_max - x_min) / im -> nb_col;
double pasy = (y_max - y_min) / im -> nb_lig;
double cy = y_min;
double new_zx;
unsigned int l,c;
// Calcul
#pragma omp parallel for private ( pasx, pasy, im,nb_iter,x_min,x_max,y_min, y_max)
for (l = 0; l < im->nb_lig; l++) {
double cx = x_min;
#pragma omp parallel for private (cx)
for (c = 0; c < im->nb_col; c++) {
double zx = 0.0;
double zy = 0.0;
unsigned int n = 0;
while ( ( zx*zx + zy*zy < 4.0 ) && ( n < nb_iter ) ) {
new_zx = zx*zx - zy*zy + cx;
zy = 2.0*zx*zy + cy;
zx = new_zx;
++n;
}
im->pixels[l*im->nb_col + c] = n%256;
cx += pasx;
}
cy += pasy;
}
}
使用gcc mandelbrot.c -fopenmp -o exe
进行编译时,我得到segmentation fault
。可能的原因是什么?
编辑:执行vingrid后我收到此消息
Process terminating with default action of signal 11 (SIGSEGV)
==10689== Access not within mapped region at address 0x0
==10689== at 0x40105F: calculer._omp_fn.0 (in /home/haddad/Documents/TPOpenMP/TP_OpenMP/TP_Mandelbrot/exe3)
==10689== by 0x4E39EE9: ??? (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
==10689== by 0x5047E99: start_thread (pthread_create.c:308)
==10689== by 0x535038C: clone (clone.S:112)
==10689== If you believe this happened as a result of a stack
==10689== overflow in your program's main thread (unlikely but
==10689== possible), you can try to increase the size of the
==10689== main thread stack using the --main-stacksize= flag.
==10689== The main thread stack size used in this run was 8388608.
答案 0 :(得分:6)
我继续清理你的代码甚至从中制作了一张图片。
#include <stdlib.h>
#include <stdio.h>
struct Image {
unsigned nb_lig;
unsigned nb_col;
unsigned *pixels;
};
void calculer (struct Image * im, unsigned nb_iter, double x_min, double x_max, double y_min, double y_max) {
double pasx = (x_max - x_min) / im -> nb_col;
double pasy = (y_max - y_min) / im -> nb_lig;
unsigned l,c;
#pragma omp parallel for private (c)
for (l = 0; l < im->nb_lig; l++) {
for (c = 0; c < im->nb_col; c++) {
double zx = 0.0, zy = 0.0, new_zx;
double cx = x_min + c*pasx, cy = y_min + l*pasy;
unsigned n = 0;
for(n=0; (zx*zx + zy*zy < 4.0 ) && ( n < nb_iter ); n++ ) {
new_zx = zx*zx - zy*zy + cx;
zy = 2.0*zx*zy + cy;
zx = new_zx;
}
if(n == nb_iter) n = 0;
im->pixels[l*im->nb_col + c] = n;
}
}
}
void draw_image(struct Image *im) {
const char charset[] = ".,c8M@jawrpogOQEPGJ";
unsigned l,c;
for (l = 0; l < im->nb_lig; l++) {
for (c = 0; c < im->nb_col; c++) {
unsigned n = im->pixels[l*im->nb_col + c];
char p = n > 0 ? charset[n % (sizeof(charset)-1)] : ' ';
putchar(p);
if(c+1 == im->nb_col) puts("");
}
}
puts("");
}
int main(void) {
struct Image im;
im.nb_lig = 40;
im.nb_col = 80;
im.pixels = malloc(sizeof *im.pixels * im.nb_lig*im.nb_col);
unsigned nb_iter = 256;
calculer(&im, nb_iter, -2.5, 1.5, -2.0, 2.0);
draw_image(&im);
return 0;
}
输出
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,ccccccccccccccccccccccccc,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,ccccccccccccccccccccccccccccccccccc,,,,,,,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,ccccccccccccccccccccccccccccccccccccccccccc,,,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,ccccccccccccccccccccccccccccccccccccccccccccccc,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,ccccccccccccccccccccccccccccccccccccccccccccccccccccc,,, ,,,,,,,,,,,,,,,,,,,,,,ccccccccccccccccccccccccccccccccccccccccccccccccccccccccc, ,,,,,,,,,,,,,,,,,,,,ccccccccccccccc88888888888cccccccccccccccccccccccccccccccccc ,,,,,,,,,,,,,,,,,,,ccccccccc8888888888888888888888888ccccccccccccccccccccccccccc ,,,,,,,,,,,,,,,,,ccccccc8888888888888888MMMMM@.w@@MMM8888ccccccccccccccccccccccc ,,,,,,,,,,,,,,,,ccccc8888888888888888MMMMMM@@jaoro @MMMM8888cccccccccccccccccccc ,,,,,,,,,,,,,,,ccc8888888888888888MMMMMMM@@@jwrG@owj@@MMMM88888ccccccccccccccccc ,,,,,,,,,,,,,,cc8888888888888888MMMMMMM@@jjawQ Jwj@@@@MM888888ccccccccccccccc ,,,,,,,,,,,,,cc88888888888888MMMMMM@@jawwwwrpQ OprwjjjJ@MM88888cccccccccccccc ,,,,,,,,,,,,cc8888888888888MMMM@@@@jjagM Pa ,gQEPE@M888888ccccccccccccc ,,,,,,,,,,,,c88888888888MM@@@@@@@jjjwQg@ ,aj@M888888cccccccccccc ,,,,,,,,,,,c8888888MMM@@agaaaaaaaaawo, Gr.@MM888888ccccccccccc ,,,,,,,,,,,888MMMMMM@@@japP,gOPOorro@ EwjMM8888888cccccccccc ,,,,,,,,,,,8MMMMMM@@@@jawoJ EP ga@MMM888888cccccccccc ,,,,,,,,,,,MMMMMjjjjawgOQ8 Q wj@MMM888888cccccccccc ,,,,,,,,,,, gwaj@MMM888888cccccccccc ,,,,,,,,,,,MMMMMjjjjawgOQ8 Q wj@MMM888888cccccccccc ,,,,,,,,,,,8MMMMMM@@@@jawoJ EP ga@MMM888888cccccccccc ,,,,,,,,,,,888MMMMMM@@@japP,gOPOorro@ EwjMM8888888cccccccccc ,,,,,,,,,,,c8888888MMM@@agaaaaaaaaawo, Gr.@MM888888ccccccccccc ,,,,,,,,,,,,c88888888888MM@@@@@@@jjjwQg@ ,aj@M888888cccccccccccc ,,,,,,,,,,,,cc8888888888888MMMM@@@@jjagM Pa ,gQEPE@M888888ccccccccccccc ,,,,,,,,,,,,,cc88888888888888MMMMMM@@jawwwwrpQ OprwjjjJ@MM88888cccccccccccccc ,,,,,,,,,,,,,,cc8888888888888888MMMMMMM@@jjawQ Jwj@@@@MM888888ccccccccccccccc ,,,,,,,,,,,,,,,ccc8888888888888888MMMMMMM@@@jwrG@owj@@MMMM88888ccccccccccccccccc ,,,,,,,,,,,,,,,,ccccc8888888888888888MMMMMM@@jaoro @MMMM8888cccccccccccccccccccc ,,,,,,,,,,,,,,,,,ccccccc8888888888888888MMMMM@.w@@MMM8888ccccccccccccccccccccccc ,,,,,,,,,,,,,,,,,,,ccccccccc8888888888888888888888888ccccccccccccccccccccccccccc ,,,,,,,,,,,,,,,,,,,,ccccccccccccccc88888888888cccccccccccccccccccccccccccccccccc ,,,,,,,,,,,,,,,,,,,,,,ccccccccccccccccccccccccccccccccccccccccccccccccccccccccc, ,,,,,,,,,,,,,,,,,,,,,,,,ccccccccccccccccccccccccccccccccccccccccccccccccccccc,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,ccccccccccccccccccccccccccccccccccccccccccccccc,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,ccccccccccccccccccccccccccccccccccccccccccc,,,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,ccccccccccccccccccccccccccccccccccc,,,,,,,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,ccccccccccccccccccccccccc,,,,,,,,,,,,,,,,,