c++ - 双CPU内存分配性能

标签 c++ multithreading performance memory-management cpu

当我在具有 2 个 CPU 的特定服务器上使用多线程代码时,我遇到了问题。服务器在 Windows 7 x64 上运行,配备 Bi-Xeon E5-2697Wv2 12 核 2.7 GHz; 64 Gb 内存(8X8 Gb 1866 MHz);主板超微X9DAI。我的可执行文件是使用 Visual Studio MSVC 2013 生成的,并使用 OpenMP 进行多线程处理。

现在的问题是我使用 1 个线程比使用 24 个线程有更好的性能...这个问题只在这台计算机上可见,当我连接一个分析器 (CodeXL) 时,我得到以下结果:

  • 1 个线程:~3% 的执行时间在 malloc/free(~3/~2) 内
  • 24 个线程:~64% 的执行时间在 malloc/free 中(~33%/~31%)

代码很复杂,我不能发布一个例子,但基本上它是一个蒙特卡洛代码,有少量的动态分配(初始化阶段创建所有需要的数据),它仍然只是一个动态分配在一个事件的开始存储事件数据。该代码不包含任何互斥锁,每个线程都在没有任何通信的情况下工作,除了在计算开始和结束时。

我在服务器和双 CPU 架构方面的知识非常有限,我想知道我是否可以做些什么来避免这个问题(BIOS 选项?),我猜是有一个 Controller 可以选择哪个 CPU RAM使用过并且此操作会减慢...

感谢阅读。

编辑: 我写了一个小基准来评估 malloc/free 的性能下降,这里是代码:

#include <omp.h>
#include <afx.h>
#include <vector>
#include <fstream>
#include <iostream>
#include <chrono>

// malloc allocation size tab
int allocSize[] =
{
    4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072
};

int main()
{
    // number max of thread
    int nbThreadMax = omp_get_max_threads();
    // malloc/free iteration per bench
    unsigned int nbIteration = 1000000;

    // Empty res tab
    std::vector<double> emptyRes(16, 0.);
    // Duration per thread
    std::vector<std::vector<double>> avgDuration(nbThreadMax, emptyRes);

    int nbThread = 1;
    unsigned int idxt = 0;
    while (nbThread <= nbThreadMax)
    {
        // Current bench result
        std::vector<std::vector<double>> threadResult(nbThread, emptyRes);

        std::cout << "Thread : " << nbThread << std::endl;

        // Create parrallel region
        #pragma omp parallel num_threads(nbThread)
        {
            int nt = omp_get_thread_num();

            for (unsigned int i = 0; i < 16; ++i)
            {
                int allocationSize = allocSize[i];

                std::chrono::time_point<std::chrono::system_clock> start, end;
                start = std::chrono::system_clock::now();
                for (unsigned int j = 0; j < nbIteration; ++j)
                {
                    void* pData = malloc(allocationSize);
                    free(pData);
                }
                end = std::chrono::system_clock::now();

                threadResult[nt][i] += std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() / 1000.;
            }
        }

        // Sum
        for (unsigned int i = 0; i < 16; ++i)
        {
            for (unsigned int j = 0; j <= idxt; ++j)
            {
                avgDuration[idxt][i] += threadResult[j][i];
            }
            // /!\  Normalize for one thread /!\
            avgDuration[idxt][i] /= nbThread;
        }

        ++idxt;
        // Increase thread number (X2)
        if (nbThread >= nbThreadMax)
            break;
        if (nbThread * 2 > nbThreadMax)
            nbThread = nbThreadMax;
        else
            nbThread = nbThread * 2;
    }

    // Write results
    {
        std::ofstream ofs("resultats.csv");
        ofs << "NbThread;";
        for (unsigned int i = 0; i < 16; ++i)
        {
            ofs << allocSize[i] << ";";
        }
        ofs << std::endl;

        int nbThread = 1;
        for (unsigned int n = 0; n < idxt; ++n)
        {
            ofs << nbThread << ";";
            for (unsigned int i = 0; i < 16; ++i)
            {
                ofs << avgDuration[n][i] << ";";
            }
            ofs << std::endl;
            nbThread = nbThread * 2;
        }

        ofs.close();
    }
}

这是在我的服务器上获得的结果: malloc/free duration /thread malloc/free performance factor /thread

这种结果表明存在问题还是正常的性能下降?

最佳答案

BIOS 选项太奇特了。最简单的解决方案是略微偏离标准 C 方法并使用 native Windows 方法。

第一个测试是将 malloc/free 替换为 HeapAlloc。这里的好处是 HeapAlloc 可以支持多个堆,并且使用 HEAP_NO_SERIALIZE,每个堆都可以是单线程的。这意味着您必须在同一个线程上调用HeapFree。您可以在工作线程上调用HeapAlloc,将结果存储在分配的内存块中,与主线程连接(此处为内存屏障),然后在主线程上收集所有工作线程的数据并调用HeapFree 来自主线程。由于工作线程不再存在,因此没有序列化风险。

第二个改进(如果需要)是检查 NUMA 支持。最好将线程固定到 CPU 并从连接到该特定 CPU 的 4xGB 分配内存。但这要复杂得多。

关于c++ - 双CPU内存分配性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39955951/

相关文章:

c++ - 极其频繁地调用 std::nth_element() 函数

mysql - 我应该将我的表分成多个表吗?

c++ - 延迟渲染 : Use depth buffer from Gbuffer in default framebuffer

c++ - 为什么将树存储为连续的内存块?

c++ - 用 std::set 替换 std::map 并按索引搜索

java - 从另一个同步方法调用同步方法,两者都在不同的对象上

java - 如何使用可运行程序在单例中执行工作?

c++ - 使用多个参数值调用模板函数

c# - 手动使 C# 线程超时

mysql - 哪个更快 : a lookup on a large denormalized table or a join between three smaller tables?