2

现在我需要使用 cuda 技术分配所有可用内存。我用 Tesla C2050、Quadro 600 和 GeForce GTX 560 Ti 做到这一点:首先,我在设备上分配 0 字节的全局内存。第二步是通过 cudaMemGetInfo 函数定义设备的可用内存并分配可用内存。它适用于上面列出的设备。但这种机制不适用于 GeForce GTX 690。

有人可以帮助我,我可以使用什么机制在 GeForce GTX 690 设备上分配内存或该操作的任何范例?

它看起来像这样:

cudaSetDevice(deviceIndex);

int (*reservedMemory);

cudaMalloc(&reservedMemory, 0);

size_t freeMemory, totalMemory;

cudaMemGetInfo(&freeMemory, &totalMemory);

cudaMalloc(&reservedMemory, freeMemory);

在 GeForce GTX 690 上,现有的两个流式多处理器之一在 2147483648 字节的内存上运行,但我只能分配 1341915136 字节的可用全局内存,即 2050109440 字节。在 Quadro 600 上,一个现有的流式多处理器在 1073414144 字节的内存上运行,我可以分配所有可用的 859803648 字节的空闲全局内存,等于 859803648 字节。

以 Quadro 600 为例(显示编译、链接和执行过程):

D:\Gdmt> nvcc -arch=compute_20 -code=sm_21 -c ./Gdmt.cu -o ./Gdmt.obj
Gdmt.cu
tmpxft_00000bb4_00000000-3_Gdmt.cudafe1.gpu
tmpxft_00000bb4_00000000-8_Gdmt.cudafe2.gpu
Gdmt.cu
tmpxft_00000bb4_00000000-3_Gdmt.cudafe1.cpp
tmpxft_00000bb4_00000000-14_Gdmt.ii

D:\Gdmt> nvcc ./Gdmt.obj -o ./Gdmt.exe

D:\Gdmt> nvcc -arch=compute_20 -code=sm_21 -c ./Gdmt_additional.cu -o ./Gdmt_add
itional.obj
Gdmt_additional.cu
tmpxft_00000858_00000000-3_Gdmt_additional.cudafe1.gpu
tmpxft_00000858_00000000-8_Gdmt_additional.cudafe2.gpu
Gdmt_additional.cu
tmpxft_00000858_00000000-3_Gdmt_additional.cudafe1.cpp
tmpxft_00000858_00000000-14_Gdmt_additional.ii

D:\Gdmt> nvcc ./Gdmt_additional.obj -o ./Gdmt_additional.exe

D:\Gdmt> Gdmt.exe
Total amount of memory: 1073414144 Bytes;
Memory to reserve: 859803648 Bytes;
Memory reserved: 859803648 Bytes;
^C
D:\Gdmt> Gdmt_additional.exe
Allocation is succeeded on 890830848 bytes of reserved memory.
^C
D:\Gdmt>

以 GeForce GTX 690 为例(显示编译、链接和执行过程):

J:\Gdmt> nvcc -arch=compute_30 -code=sm_30 -c ./Gdmt.cu -o ./Gdmt.obj
Gdmt.cu
tmpxft_000011f0_00000000-5_Gdmt.cudafe1.gpu
tmpxft_000011f0_00000000-10_Gdmt.cudafe2.gpu
Gdmt.cu
tmpxft_000011f0_00000000-5_Gdmt.cudafe1.cpp
tmpxft_000011f0_00000000-15_Gdmt.ii

J:\Gdmt> nvcc ./Gdmt.obj -o ./Gdmt.exe

J:\Gdmt> nvcc -arch=compute_30 -code=sm_30 -c ./Gdmt_additional.cu -o ./Gdmt_add
itional.obj
Gdmt_additional.cu
tmpxft_00001164_00000000-5_Gdmt_additional.cudafe1.gpu
tmpxft_00001164_00000000-10_Gdmt_additional.cudafe2.gpu
Gdmt_additional.cu
tmpxft_00001164_00000000-5_Gdmt_additional.cudafe1.cpp
tmpxft_00001164_00000000-15_Gdmt_additional.ii

J:\Gdmt> nvcc ./Gdmt_additional.obj -o ./Gdmt_additional.exe

J:\Gdmt> Gdmt.exe
Total amount of memory: 2147483648 Bytes;
Memory to reserve: 2050109440 Bytes;
Warning, memory allocation process is not succeeded!
^C
J:\Gdmt> Gdmt_additional.exe
Allocation is succeeded on 1341915136 bytes of reserved memory.
^C

示例存档并位于:

(z7 存档 - 78.5 KB ~ 80,434 字节) https://docs.google.com/file/d/0BzZ5q0v8n-qTTDctVDV5Mnh2ODA/edit (zip 存档 - 163 KB ~ 167,457 字节) https://docs.google.com/file /d/0BzZ5q0v8n-qTT2xoV3NXSzhQMDQ/编辑

该主题是在“The GeForce Lounge”和“CUDA Programming and Performance”上发布的同名主题的克隆。

4

2 回答 2

1

我可以重新运行您的示例并得出相同的结果。

我试图从另一边解决这个问题,并尝试分配大小减小的块。

int (*reservedMemory);
size_t const NBlockSize = 1300 *1024*1024; 
size_t freeMemory, totalMemory;
cudaError_t nErr = cudaSuccess;
size_t nTotalAlloc=0;
while( nErr == cudaSuccess )
{
    cudaMemGetInfo(&freeMemory, &totalMemory);
    std::cout << "===========================================================" << std::endl;
    std::cout << "Free/Total(kB): " << freeMemory/1024 << "/" << totalMemory/1024 << std::endl;

    size_t nAllocSize = NBlockSize;
    while( nAllocSize > freeMemory )
        nAllocSize /= 2;

    nErr = cudaMalloc(&reservedMemory, nAllocSize );
    if( nErr == cudaSuccess )
        nTotalAlloc += nAllocSize;
    std::cout << "AllocSize(kB): " << nAllocSize/1024 << ", error: " << cudaGetErrorString(nErr) << std::endl;

}
std::cout << "TotalAlloc/Total (kB): " << nTotalAlloc/1024 << "/" << totalMemory/1024 << std::endl;

程序从大小为 NBlockSize 的块开始,如果 freeMemory 减少,也减少 nAllocSize。查看下面的输出,在分配与 freeMemory 相关的块时,cudaMalloc 的行为似乎有些不可预测。在某一时刻,它设法分配了超过 98% 的可用内存,在另一时刻,它未能从 1GB 可用内存中分配 800MB。

最有趣的运行是起始块大小为 700MB 的运行。在最后一次成功的循环中,它设法从 1428 个中分配 1400kB,并在下一次运行中分配 20kB 中的 10 个失败。

根据起始大小,程序在最佳运行时设法分配了除 8kB 之外的所有可用空间,在最坏的情况下剩余超过 1 GB。

D:\Buildx64\Test\GMDT\Debug>Gdmt.exe
NBlockSize(MB): 1000
===========================================================
Free/Total(kB): 1797120/2097152
AllocSize(kB): 1024000, percentage of freememory: 0.569801, error: no error
===========================================================
Free/Total(kB): 773120/2097152
AllocSize(kB): 512000, percentage of freememory: 0.662252, error: no error
===========================================================
Free/Total(kB): 261120/2097152
AllocSize(kB): 256000, percentage of freememory: 0.980392, error: no error
===========================================================
Free/Total(kB): 5128/2097152
AllocSize(kB): 4000, percentage of freememory: 0.780031, error: no error
===========================================================
Free/Total(kB): 1032/2097152
AllocSize(kB): 1000, percentage of freememory: 0.968992, error: no error
===========================================================
Free/Total(kB): 8/2097152
AllocSize(kB): 7, percentage of freememory: 0.976563, error: out of memory
TotalAlloc/Total (kB): 1797000/2097152


D:\Buildx64\Test\GMDT\Debug>Gdmt.exe
NBlockSize(MB): 1200
===========================================================
Free/Total(kB): 1796864/2097152
AllocSize(kB): 1228800, percentage of freememory: 0.683858, error: no error
===========================================================
Free/Total(kB): 568072/2097152
AllocSize(kB): 307200, percentage of freememory: 0.540777, error: no error
===========================================================
Free/Total(kB): 260872/2097152
AllocSize(kB): 153600, percentage of freememory: 0.588795, error: no error
===========================================================
Free/Total(kB): 107272/2097152
AllocSize(kB): 76800, percentage of freememory: 0.715937, error: no error
===========================================================
Free/Total(kB): 30472/2097152
AllocSize(kB): 19200, percentage of freememory: 0.630087, error: no error
===========================================================
Free/Total(kB): 11272/2097152
AllocSize(kB): 9600, percentage of freememory: 0.851668, error: no error
===========================================================
Free/Total(kB): 1672/2097152
AllocSize(kB): 1200, percentage of freememory: 0.717703, error: no error
===========================================================
Free/Total(kB): 392/2097152
AllocSize(kB): 300, percentage of freememory: 0.765306, error: out of memory
TotalAlloc/Total (kB): 1796400/2097152

D:\Buildx64\Test\GMDT\Debug>Gdmt.exe
NBlockSize(MB): 800
===========================================================
Free/Total(kB): 1844448/2097152
AllocSize(kB): 819200, percentage of freememory: 0.444144, error: no error
===========================================================
Free/Total(kB): 1025248/2097152
AllocSize(kB): 819200, percentage of freememory: 0.799026, error: out of memory
TotalAlloc/Total (kB): 819200/2097152

D:\Buildx64\Test\GMDT\Debug>Gdmt.exe
NBlockSize(MB): 700
===========================================================
Free/Total(kB): 1835528/2097152
AllocSize(kB): 716800, percentage of freememory: 0.390514, error: no error
===========================================================
Free/Total(kB): 1118740/2097152
AllocSize(kB): 716800, percentage of freememory: 0.640721, error: no error
===========================================================
Free/Total(kB): 401940/2097152
AllocSize(kB): 358400, percentage of freememory: 0.891675, error: no error
===========================================================
Free/Total(kB): 43540/2097152
AllocSize(kB): 22400, percentage of freememory: 0.514469, error: no error
===========================================================
Free/Total(kB): 21140/2097152
AllocSize(kB): 11200, percentage of freememory: 0.529801, error: no error
===========================================================
Free/Total(kB): 9876/2097152
AllocSize(kB): 5600, percentage of freememory: 0.567031, error: no error
===========================================================
Free/Total(kB): 4244/2097152
AllocSize(kB): 2800, percentage of freememory: 0.659755, error: no error
===========================================================
Free/Total(kB): 1428/2097152
AllocSize(kB): 1400, percentage of freememory: 0.980392, error: no error
===========================================================
Free/Total(kB): 20/2097152
AllocSize(kB): 10, percentage of freememory: 0.546875, error: out of memory
TotalAlloc/Total (kB): 1835400/2097152
于 2013-02-07T16:22:53.553 回答
0

我最近想起了 cuda 中的“页面锁定”机制。我对其进行了测试,并没有得到令人满意的性能结果(使用这种机制的计算速度要慢十倍,然后是带有 GeForce GTX 690 的 Windows 的内存保留功能非常有限的版本)。我只是以为将数据复制到设备以供以后计算和写回会自动完成,但实际上并没有涉及设备的内存。

于 2013-03-18T09:41:27.633 回答