当分辨率高于 320 像素时,Mandelbrot 代码不令人兴奋?

Mandelbrot code not exciting when resolution is higher than 320 pixels?

我正在学习 C 并尝试新事物来测试我能做什么。我已经编写了生成具有给定分辨率 (RES) 的 Mandelbrot 集的代码,该分辨率在 .h 文件中为 #define RES。对于小于 321 的分辨率,这有效并产生良好的输出。出于某种原因,当 RES > 321 时代码不再执行。

我 运行正在使用 GCC 并使用 Gnuplot 绘制输出。我曾尝试使用调试器进行调试,但是对于 RES > 321main 函数不再获得 运行?我在 main() 的第一行添加了一个打印来查看,但没有得到 运行。生成可执行文件并且程序编译没有错误?

#include <stdio.h>
#include <math.h>

#define MAX_DEPTH 100
#define RES       321

typedef struct complex_t {
    double re;
    double im;
} complex;

void init_complex_grid(complex complex_grid[RES][RES], double left, double right, double top, double bottom);
int converge(complex a);
complex add_complex(complex a, complex b);
complex square_complex(complex a);
double mag_complex(complex a);
void output_grid(unsigned int grid[RES][RES]);

int main(void) {
    // printf("HERE\n");
    int i, j;
    unsigned int convergence_grid[RES][RES];
    complex complex_grid[RES][RES];
    init_complex_grid(complex_grid, -2.5, 1, 1, -1);
    for (i = 0; i < RES; i++) {
        for (j = 0; j < RES; j++) {
            convergence_grid[i][j] = converge(complex_grid[i][j]);
        }
    }
    output_grid(convergence_grid);
    return 0;
}

void init_complex_grid(complex complex_grid[RES][RES], 
                       double left, double right, 
                       double top, double bottom) {
    int i, j;
    double restep = (top - bottom) / RES;
    double imstep = (right - left) / RES;
    for (i = 0; i < RES; i++) {
        for (j = 0; j < RES; j++) {
            complex_grid[i][j].re = left + j * imstep;
            complex_grid[i][j].im = bottom + i * restep;
        }
    }
}

int converge(complex a) {
    complex z = { 0, 0 };
    int cnt = 0;
    while (cnt <= MAX_DEPTH && mag_complex(z) <= 2) {
        z = add_complex(square_complex(z), a);
        cnt++;
    }   
    return cnt;
}

complex add_complex(complex a, complex b) {
    complex added = { a.re + b.re, a.im + b.im };
    return added;
}

complex square_complex(complex a) {
    complex b;
    b.re = a.re * a.re - a.im * a.im;
    b.im = 2 * a.re * b.im;
    return b;
}

double mag_complex(complex a) {
    return sqrt(a.re * a.re + a.im * a.im);
}

void output_grid(unsigned int grid[RES][RES]) {
    FILE *f = fopen("mandelbrot.dat", "w");
    int i, j;
    for (i = 0; i < RES; i++) {
        for (j = 0; j < RES; j++) {
            fprintf(f, "%d ", grid[i][j]);
        }
        fprintf(f, "\n");
    }
    fclose(f);
    printf("\nFILE CLOSED\n");
}

我还添加了 printf("\nFILE CLOSED\n"); 行,这样我就知道输出已写入文件,但是 运行 也没有 RES > 321.

您在 main() 函数中使用自动存储定义了太多数据:要么将大型数组设为全局、静态,要么从堆中分配它们。

这里有一个您可以尝试的简单修复方法:

int main(void) {
    int i, j;
    static unsigned int convergence_grid[RES][RES];
    static complex complex_grid[RES][RES];
    init_complex_grid(complex_grid, -2.5, 1, 1, -1);
    for (i = 0; i < RES; i++) {
        for (j = 0; j < RES; j++) {
            convergence_grid[i][j] = converge(complex_grid[i][j]);
        }
    }
    output_grid(convergence_grid);
    return 0;
}

这是使用堆分配的替代方法:

int main(void) {
    int i, j;
    unsigned int (*convergence_grid)[RES] = calloc(sizeof(*convergence_grid), RES);
    complex (*complex_grid)[RES] = calloc(sizeof(*complex_grid), RES);
    if (!convergence_grid || !complex_grid) {
        fprintf(stderr, "cannot allocate arrays\n");
        return 1;
    }
    init_complex_grid(complex_grid, -2.5, 1, 1, -1);
    for (i = 0; i < RES; i++) {
        for (j = 0; j < RES; j++) {
            convergence_grid[i][j] = converge(complex_grid[i][j]);
        }
    }
    output_grid(convergence_grid);
    free(complex_grid);
    free(convergence_grid);
    return 0;
}