【问题标题】:Why does complex Matlab gpuArray take twice as much memory than it should?为什么复杂的 Matlab gpuArray 占用的内存是应有的两倍?
【发布时间】:2014-05-15 15:24:57
【问题描述】:

我注意到大型复杂数组在 GPU 上占用的内存是 CPU 上的两倍。

这是一个最小的例子:

%-- First Try: Complex Single
gpu = gpuDevice(1);
m1 = gpu.FreeMemory;
test = complex(single(zeros(600000/8,1000)));  % 600 MByte complex single
whos('test')
test = gpuArray(test);
fprintf(' Used memory on GPU: %e\n', m1-gpu.FreeMemory);

现在我对一个不复杂的两倍大数组做同样的事情:

%-- Second Try:, Single
gpu = gpuDevice(1);
m1 = gpu.FreeMemory;
test = single(zeros(600000/4,1000));  % 600MB MByte real single
whos('test')
test = gpuArray(test);
fprintf(' Used memory on GPU: %e\n', m1-gpu.FreeMemory);

输出是:

 Name          Size                  Bytes  Class     Attributes    
 test      75000x1000            600000000  single    complex   
 Used memory on GPU: 1.200095e+09

 Name           Size                  Bytes  Class     Attributes   
 test      150000x1000            600000000  single                  
 Used memory on GPU: 6.000476e+08

在 CPU 上,两个数组都是 600MB - 在 GPU 上,复数数组使用 1.2 GByte。 我使用 Matlab 2013a 在两个显卡上进行了测试:GeForce GTX 680 和 Tesla K20。

我怎样才能避免这种情况?这是 Matlab 中的错误吗?

【问题讨论】:

  • 我无法在 R2012b + GeForce GT 650 上重现这一点。两个 gpu 阵列在 GPU 上都使用 6.000476e+08。
  • 如果您执行zeros(100,100,'single','gpuArray') 之类的操作,这对您有什么用?另请注意,根据gpuArray 文档,complex 不受支持,因此它可能会做一些奇怪的事情,例如在将其放入 gpuArray 时将其转换为 double
  • @MZimmerman6:如果我这样做test = complex(zeros(600000/8,1000,'single','gpuArray'));,GPU 需要 900MB!?如果你看heregpuArray 确实支持复数。
  • 该链接基本上向我表明,GPU 上的复数需要小心处理。如果您有可用的 MATLAB,我可能建议的唯一方法是运行早期版本的 MATLAB,并查看是否得到类似的结果。看看你是否能像@H.Muster 那样得到 2012b,看看它是否有效。
  • 我认为它与复数有关,当然您需要为复数分配两倍的内存,一个用于实部,另一个用于虚部。而对于实数,您只需要为实数部分分配内存。

标签: matlab gpu


【解决方案1】:

这是在MATLAB central 上回答的。总结 MathWorks 开发人员 Edric Ellis 的回答:

  • gpu.FreeMemory 可能无法准确衡量可用 GPU 内存,因为 MATLAB 在使用完内存后不会立即释放内存。 gpu.AvailableMemory 是对可用内存的更准确度量。

  • 向/从 GPU 传输复杂数据仍然需要 2 倍的内存,因为在 GPU 上完成了格式转换。具体来说,CPU 主机内存中的复数数组以实部/虚部分为 2 个独立向量的方式存储,而 GPU 设备上的复数数组以交错格式存储。

在 R2017a 上进行测试,我确认:

  • gpu.FreeMemory 切换到gpu.AvailableMemory 确实解决了引发原始问题的报告内存使用情况的差异。

  • 使用 8 GB 的 GPU 内存,正在复制...

    • 6 GB 实数数组:成功
    • 6 GB 复杂数组:“设备内存不足”错误
    • 6 个单独的 1 GB 复杂数组:成功

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-22
    • 2011-06-25
    • 2019-01-02
    • 1970-01-01
    • 2012-10-30
    • 2012-03-03
    • 1970-01-01
    相关资源
    最近更新 更多