This C++ program generates an image of the Mandelbrot set using an iterative algorithm.

In summary, this program generates a Mandelbrot set image and saves the pixel data to a file, allowing users to visualize the intricate patterns of the Mandelbrot set.

CPU Info

Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Byte Order:                      Little Endian
Address sizes:                   46 bits physical, 48 bits virtual
CPU(s):                          24
On-line CPU(s) list:             0-23
Thread(s) per core:              1
Core(s) per socket:              16
Socket(s):                       1
NUMA node(s):                    1
Vendor ID:                       GenuineIntel
CPU family:                      6
Model:                           151
Model name:                      12th Gen Intel(R) Core(TM) i9-12900K
Stepping:                        2
CPU MHz:                         1085.455
CPU max MHz:                     5200.0000
CPU min MHz:                     800.0000
BogoMIPS:                        6374.40
Virtualization:                  VT-x
L1d cache:                       384 KiB
L1i cache:                       256 KiB
L2 cache:                        10 MiB
NUMA node0 CPU(s):               0-23
Vulnerability Itlb multihit:     Not affected
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Mmio stale data:   Not affected
Vulnerability Retbleed:          Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Not affected
Flags:                           

fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi
mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon
pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq
dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2
x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch
cpuid_fault epb invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority
ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap
clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves split_lock_detect avx_vnni
dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req hfi umip pku ospke
waitpkg gfni vaes vpclmulqdq tme rdpid movdiri movdir64b fsrm md_clear serialize pconfig
arch_lbr ibt flush_l1d arch_capabilities

The CPU has 24 cores (16 physical and 8 with hyperthreading), allowing us to execute multiple portion of code concurrently. Furthermore, its high frequency of 5200.0000 MHz enables us to complete computations quickly.

Analyzing with the Intel Compiler

To understand how the code performs when executed without any optimization we have compiled with icc -diag-disable=10441 mandelbrot.cpp:

<aside> ⏳ Time elapsed: 13.08 seconds.

</aside>

We utilized the -diag-disable=10441 flag to suppress the annoying message regarding the compiler's deprecation.

We started analyzing the vanilla code for searching for hotspots to parallelize. We found a loop inside another loop in the main function, this could be a hotspot because has quadratic complexity.

More precisely, in the following part of the code:

for (int pos = 0; pos < HEIGHT * WIDTH; pos++)
    {
        image[pos] = 0;

        const int row = pos / WIDTH;
        const int col = pos % WIDTH;
        const complex<double> c(col * STEP + MIN_X, row * STEP + MIN_Y);

        // z = z^2 + c
        complex<double> z(0, 0);
        for (int i = 1; i <= ITERATIONS; i++)
        {
            z = pow(z, 2) + c;

            // If it is convergent
            if (abs(z) >= 2)
            {
                image[pos] = i;
                break;
            }
        }
    }

Optimizing using optimization flags

Once identified the “critical part” of the code, we start analyzing how time varies with different optimization flags

After experimenting with various flags for the instruction set, we found that using -xSSE3 could significantly improve performance. This flag changes the instruction set level, resulting in enhanced performance.

By compiling with specific compiler flags, such as icc -diag-disable=10441 -fast -xSSE3 mandelbrot.cpp, we already achieved a boost in performance. Specifically, the required time has been reduced to:

<aside> ⌛ Time elapsed: 4.69 seconds.

</aside>

We choose to use the following flags: