MatlabからGPUを使用する通常の方法で、計算後にcudaDeviceReset()を使用していますか?私のGPUはCompute Capability 1.3+をサポートしていないため、Matlabの最新バージョンではGPU計算を使用できません.cudaMemGetInfo()のような単純なCuda関数を使用するためにAccelereyes Jacketに多額のお金を払いたくはありません。または私のシンプルなCudaカーネル。MatlabはCUcontextキャッシングのためにCudaにメモリリークを引き起こしますか?
私はMatlabからCudaを呼び出すときに非常に不満足な動作を見出しました。 Visual Studio 2008では、標準的なMEXインターフェイスを使用して1つのCudaクエリを実行する簡単なDLLを作成しました。デバイス上のRAMの量(リスト1)。
// cudaMemoryCheck.cpp : Defines the exported functions for the DLL application.
#include <mex.h>
#include <cuda.h>
#include <driver_types.h>
#include <cuda_runtime_api.h>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
size_t free = 0, total = 0;
cudaError_t result = cudaMemGetInfo(&free, &total);
mexPrintf("free memory in bytes %u (%u MB), total memory in bytes %u (%u MB). ", free, free/1024/1024, total, total/1024/1024);
if(total > 0)
mexPrintf("%2.2f%% free\n", (100.0*free)/total);
else
mexPrintf("\n");
// this is the critical line!
cudaDeviceReset();
}
は、私はDEFファイルを使用してでmexFunctionをエクスポートするプロジェクトのWin32 DLL(リリースモード)をコンパイルし、.mexw32するDLLファイルの拡張子を変更します。
MatlabからcudaMemoryCheckを実行すると、cudaDeviceReset()がコメントアウトされているとGPUにメモリがリークすることがあります。ここでは(リスト2)私の些細なMATLABコードです:
addpath('C:\Users\admin\Documents\Visual Studio 2008\Projects\cudaMemoryCheck\Release')
for i=1:20
clear mex
cudaMemoryCheck;
end
MATLABでこの関数を実行すると、私は次を参照してください。
free memory in bytes 57393152 (54 MB), total memory in bytes 244776960 (233 MB). 23.45% free
free memory in bytes 57393152 (54 MB), total memory in bytes 244776960 (233 MB). 23.45% free
free memory in bytes 57393152 (54 MB), total memory in bytes 244776960 (233 MB). 23.45% free
free memory in bytes 57393152 (54 MB), total memory in bytes 244776960 (233 MB). 23.45% free
free memory in bytes 57393152 (54 MB), total memory in bytes 244776960 (233 MB). 23.45% free
free memory in bytes 57393152 (54 MB), total memory in bytes 244776960 (233 MB). 23.45% free
free memory in bytes 57393152 (54 MB), total memory in bytes 244776960 (233 MB). 23.45% free
free memory in bytes 57393152 (54 MB), total memory in bytes 244776960 (233 MB). 23.45% free
free memory in bytes 57393152 (54 MB), total memory in bytes 244776960 (233 MB). 23.45% free
free memory in bytes 57393152 (54 MB), total memory in bytes 244776960 (233 MB). 23.45% free
free memory in bytes 57393152 (54 MB), total memory in bytes 244776960 (233 MB). 23.45% free
free memory in bytes 57393152 (54 MB), total memory in bytes 244776960 (233 MB). 23.45% free
free memory in bytes 57393152 (54 MB), total memory in bytes 244776960 (233 MB). 23.45% free
free memory in bytes 57393152 (54 MB), total memory in bytes 244776960 (233 MB). 23.45% free
free memory in bytes 57393152 (54 MB), total memory in bytes 244776960 (233 MB). 23.45% free
free memory in bytes 57393152 (54 MB), total memory in bytes 244776960 (233 MB). 23.45% free
free memory in bytes 57393152 (54 MB), total memory in bytes 244776960 (233 MB). 23.45% free
free memory in bytes 57393152 (54 MB), total memory in bytes 244776960 (233 MB). 23.45% free
free memory in bytes 57393152 (54 MB), total memory in bytes 244776960 (233 MB). 23.45% free
free memory in bytes 57393152 (54 MB), total memory in bytes 244776960 (233 MB). 23.45% free
cudaDeviceReset()がコメントアウトされたときにMATLABからの出力は非常に異なっている:
free memory in bytes 37019648 (35 MB), total memory in bytes 244776960 (233 MB). 15.12% free
free memory in bytes 25092096 (23 MB), total memory in bytes 244776960 (233 MB). 10.25% free
free memory in bytes 13549568 (12 MB), total memory in bytes 244776960 (233 MB). 5.54% free
free memory in bytes 12107776 (11 MB), total memory in bytes 244776960 (233 MB). 4.95% free
free memory in bytes 8568832 (8 MB), total memory in bytes 244776960 (233 MB). 3.50% free
free memory in bytes 9617408 (9 MB), total memory in bytes 244776960 (233 MB). 3.93% free
free memory in bytes 6078464 (5 MB), total memory in bytes 244776960 (233 MB). 2.48% free
free memory in bytes 8044544 (7 MB), total memory in bytes 244776960 (233 MB). 3.29% free
free memory in bytes 5816320 (5 MB), total memory in bytes 244776960 (233 MB). 2.38% free
free memory in bytes 7520256 (7 MB), total memory in bytes 244776960 (233 MB). 3.07% free
free memory in bytes 8830976 (8 MB), total memory in bytes 244776960 (233 MB). 3.61% free
free memory in bytes 5292032 (5 MB), total memory in bytes 244776960 (233 MB). 2.16% free
free memory in bytes 3407872 (3 MB), total memory in bytes 244776960 (233 MB). 1.39% free
free memory in bytes 0 (0 MB), total memory in bytes 0 (0 MB).
free memory in bytes 0 (0 MB), total memory in bytes 0 (0 MB).
free memory in bytes 0 (0 MB), total memory in bytes 0 (0 MB).
free memory in bytes 0 (0 MB), total memory in bytes 0 (0 MB).
free memory in bytes 0 (0 MB), total memory in bytes 0 (0 MB).
free memory in bytes 0 (0 MB), total memory in bytes 0 (0 MB).
free memory in bytes 0 (0 MB), total memory in bytes 0 (0 MB).
私はMEX関数がGPUにメモリを割り当てないにもかかわらず、CudaランタイムAPIはMEX関数が実行されるたびに新しいCUcontextを作成していて決してクリアしないと結論づけました私はMatlabを閉じるか、または私はcudaDeviceReset()を使用します。最終的にGPUには何も割り当てられていないにもかかわらず、メモリが不足してしまいます。
私はcudaDeviceReset()を使いたくありません。 APIは、「関数cudaDeviceReset()は、呼び出し元のスレッドの現在のデバイスのプライマリコンテキストを直ちに初期化しません」と、この関数が呼び出されたときにデバイスが他のホストスレッドからアクセスされないようにするのは呼び出し側の責任です」と呼びます。言い換えれば、cudaDeviceReset()を使用すると、他のGPU計算を警告なしで直ちに終了することができます。私はcudaDeviceReset()を頻繁に使用するという文書は見つかっていないので、やりたくありません。私はここでcudaDeviceReset()を使用することが正常で必要であることを証明するすべての回答を受け入れます。
バージョン情報:NVIDIA GPUコンピューティングツールキット4.0、Matlab 7.8.0(R2009a、32ビット)、Windows 7 Enterprise SP1(64ビット)、Nvidia Quadro NVS 420(最新NVIDIAドライバ、270.81)
また、GeForce 8400 GS、同じMatlab、Visual Studio、およびGPU Computing Toolkitを搭載したWindows XP(32ビット、SP3)でもこの問題を再現できます。 deviceQuery.exeの
出力:
deviceQuery.exe Starting...
CUDA Device Query (Runtime API) version (CUDART static linking)
Found 2 CUDA Capable device(s)
Device 0: "Quadro NVS 420"
CUDA Driver Version/Runtime Version 4.0/4.0
CUDA Capability Major/Minor version number: 1.1
Total amount of global memory: 233 MBytes (244776960 bytes)
(1) Multiprocessors x (8) CUDA Cores/MP: 8 CUDA Cores
GPU Clock Speed: 1.40 GHz
Memory Clock rate: 700.00 Mhz
Memory Bus Width: 64-bit
Max Texture Dimension Size (x,y,z) 1D=(8192), 2D=(65536,32768), 3D=(2048,2048,2048)
Max Layered Texture Size (dim) x layers 1D=(8192) x 512, 2D=(8192,8192) x 512
Total amount of constant memory: 65536 bytes
Total amount of shared memory per block: 16384 bytes
Total number of registers available per block: 8192
Warp size: 32
Maximum number of threads per block: 512
Maximum sizes of each dimension of a block: 512 x 512 x 64
Maximum sizes of each dimension of a grid: 65535 x 65535 x 1
Maximum memory pitch: 2147483647 bytes
Texture alignment: 256 bytes
Concurrent copy and execution: No with 0 copy engine(s)
Run time limit on kernels: Yes
Integrated GPU sharing Host Memory: No
Support host page-locked memory mapping: Yes
Concurrent kernel execution: No
Alignment requirement for Surfaces: Yes
Device has ECC support enabled: No
Device is using TCC driver mode: No
Device supports Unified Addressing (UVA): No
Device PCI Bus ID/PCI location ID: 3/0
Compute Mode:
< Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >
Device 1: "Quadro NVS 420"
CUDA Driver Version/Runtime Version 4.0/4.0
CUDA Capability Major/Minor version number: 1.1
Total amount of global memory: 234 MBytes (244908032 bytes)
(1) Multiprocessors x (8) CUDA Cores/MP: 8 CUDA Cores
GPU Clock Speed: 1.40 GHz
Memory Clock rate: 700.00 Mhz
Memory Bus Width: 64-bit
Max Texture Dimension Size (x,y,z) 1D=(8192), 2D=(65536,32768), 3D=(2048,2048,2048)
Max Layered Texture Size (dim) x layers 1D=(8192) x 512, 2D=(8192,8192) x 512
Total amount of constant memory: 65536 bytes
Total amount of shared memory per block: 16384 bytes
Total number of registers available per block: 8192
Warp size: 32
Maximum number of threads per block: 512
Maximum sizes of each dimension of a block: 512 x 512 x 64
Maximum sizes of each dimension of a grid: 65535 x 65535 x 1
Maximum memory pitch: 2147483647 bytes
Texture alignment: 256 bytes
Concurrent copy and execution: No with 0 copy engine(s)
Run time limit on kernels: Yes
Integrated GPU sharing Host Memory: No
Support host page-locked memory mapping: Yes
Concurrent kernel execution: No
Alignment requirement for Surfaces: Yes
Device has ECC support enabled: No
Device is using TCC driver mode: No
Device supports Unified Addressing (UVA): No
Device PCI Bus ID/PCI location ID: 4/0
Compute Mode:
< Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >
deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 4.0, CUDA Runtime Version = 4.0, NumDevs = 2, Device = Quadro NVS 420, Device = Quadro NVS 420
clear mexを呼び出さないとメモリリークがなくなりますが、なぜMatlabが開いているcuContextを保持しているのかわかりません。 DLLがアンロードされると、それらは破壊されるはずです! mexAtExitを使用しても問題は解決しません。 Matlabのプロセス自体がそれらを破壊するために終了しなければならないように見えますが、それはイライラしています。 – user244795
'version -modules'を実行しようとしましたが、 'clear mex'を呼び出した後もメモリ内にまだ残っているDLLを確認しましたか? – Edric
@エドリック:+1便利な(文書化されていない)機能、このヒントを共有してくれてありがとう..この[その他の質問](http://stackoverflow.com/questions/7012408/mex-function-not-updated- – Amro