C++ Multithreading not achieving expected performance boost
20:24 31 Oct 2026
#include 
#include 
#include 
#include 
#include 
#include 
#include 

constexpr float MAX_TEMP = 400.0f;
constexpr unsigned int CELL_SIZE = 1024;
constexpr float ln10 = 2.302f;
constexpr float w = 4.0f * ln10 / (CELL_SIZE * CELL_SIZE * 0.25f);
constexpr unsigned int STEPX = 1;
constexpr unsigned int STEPY = 1;
constexpr unsigned int INVSQRSTEPX = 1.0f / (STEPX * STEPX);
constexpr unsigned int INVSQRSTEPY = 1.0f / (STEPY * STEPY);
constexpr float k = 1.0f;
constexpr float dt = 0.01f;
constexpr float MAX_TIME = 100.0f;
constexpr unsigned int MAX_STEPS = MAX_TIME / dt;
constexpr unsigned int MAX_ITERATIONS = 100;

using Matrix = std::array, CELL_SIZE>;

float timeDeriv(int x, int y, Matrix& u)
{
    float uxy = u[y][x];
    float ux1y = CELL_SIZE - 1 != x ? u[y][x+1] : u[y][x-1];
    float ux_1y = x != 0 ? u[y][x-1] : u[y][x+1];
    float uxy1 = CELL_SIZE - 1 != y ? u[y+1][x] : u[y-1][x];
    float uxy_1 = y != 0 ? u[y-1][x] : u[y+1][x];

    float uxx = INVSQRSTEPX * (ux1y - 2.0f*uxy + ux_1y);
    float uyy = INVSQRSTEPY * (uxy1 - 2.0f*uxy + uxy_1);

    return k * (uxx + uyy);
}

float initialTemp(float x, float y)
{
    return MAX_TEMP * std::exp(w * (-(x-CELL_SIZE*0.5)*(x-CELL_SIZE*0.5) - (y-CELL_SIZE*0.5)*(y-CELL_SIZE*0.5)));
}

int main()
{
    Matrix* heatMap = new Matrix;
    Matrix* u = new Matrix;
    for(int i = 0; i < CELL_SIZE; i++)
    {
        for(int j = 0; j < CELL_SIZE; j++)
        {
            (*heatMap)[i][j] = initialTemp(j, i);
            (*u)[i][j] = 0.0f;
        }
    }
    Matrix* next = u;
    Matrix* curr = heatMap;
    
    unsigned int threadCount = 16;
    unsigned int matrixSize = CELL_SIZE / 4;
    
    
    std::barrier sync(threadCount, [&](){std::swap(curr, next);});
    auto worker = [&](unsigned int id)
    {
        for(int step = 0 ; step < MAX_STEPS; step++)
        {
            for(int i = 0; i < matrixSize; i++)
            {
                for(int j = 0; j < matrixSize; j++)
                {
                    int c = static_cast(id / 4);
                    int jp = (id - c * 4) * matrixSize  + j;
                    int ip = c * matrixSize + i;
                    (*next)[ip][jp] = (*curr)[ip][jp] + timeDeriv(jp, ip, *curr) * dt;
                }
            }

            sync.arrive_and_wait();
        }
    };
    std::vector threads;
    threads.reserve(threadCount - 1);
    auto start = std::chrono::steady_clock::now();
    for(int i = 1; i < threadCount; i++)
        threads.emplace_back(std::thread(worker, i));

    worker(0);

    for(auto& t : threads)
        t.join();
    
    auto end = std::chrono::steady_clock::now();
    float time = std::chrono::duration(end-start).count();
    std::cout << "took: " << time << "ms\n";
}

I've been experimenting with c++'s multithreading, and I compiled this program using:
g++ -O3 -fno-tree-vectorize -fno-tree-dce

So I can test out performance gains from pure scalar multithreading without any SIMD instructions. Running this, I get an execution time of 3 seconds for 16 threads, 6 seconds for 4 threads and modifying the code a bit for one threaded execution:

auto start = std::chrono::steady_clock::now();
    for(int step = 0 ; step < MAX_STEPS; step++)
    {
        for(int i = 0; i < CELL_SIZE; i++)
        {
            for(int j = 0; j < CELL_SIZE; j++)
            {
                (*next)[i][j] = (*curr)[i][j] + timeDeriv(j, i, *curr) * dt;
            }
        }
        std::swap(curr, next);
    }    
    auto end = std::chrono::steady_clock::now();
    float time = std::chrono::duration(end-start).count();

While keeping everything else the same, I get 12 seconds. From the 4 thread program, its a 2x speedup, so that means that I'm not compute bound in this program, because, from my understand at least, I should've gotten a 4x speedup (or at least close to that). But if it were the case that I'm memory bound, getting 16 threads shouldn't change anything. But also the amount of memory these 2 arrays take is 1024*1024**4**2 which should fit into the L3 cache on my CPU (according to my research its 16 MB).

So my question basically is, what's really the problem here from the above things I've mentioned? How come 4 threads give me a 2x speed up but 16 threads give a 4x speed up? If it were the case that I'm memory bound shouldn't the speed up stay the same with more threads? Also I can't really understand if memory should be the problem because, from my calculations at least, the two arrays should fit the L3 cache, making it really fast to access this memory.

I've tried profiling with AMD's uProf, but either window's hyper V or my laptop's hardware wont allow me to do a more in depth profiling other than L1 cache misses/hits and a few other metrics that I couldn't connect to this problem.

I'm on a lenovo idea pad slim 3 with a Ryzen 7 8840HS, DDR5 double channel RAM.

c++ arrays c multithreading memory