GPU => CPU memory transfer time changes

qiazhang: <p>Hi, I want to optimize the transfer time of data from GPU to CPU. I am using RTX 3060 with a CPU of i7-10700. First I check the bandwidth of Cuda tensor to pinned-memory CPU tensor on c++ using the code in this blog (<a href="https://developer.nvidia.com/blog/how-optimize-data-transfers-cuda-cc/" rel="noopener nofollow ugc">https://developer.nvidia.com/blog/how-optimize-data-transfers-cuda-cc/</a>). It is 12.33 GB/s, which is reasonable. Then I check the transfer time of my python code shown below.</p>
<pre><code class="lang-auto">sample_number = 65
imgs_display = torch.zeros([sample_number,800,1280],dtype = torch.uint8,device='cuda')
imgData = torch.zeros(sample_number*800*1280,dtype = torch.uint8,device='cpu',pin_memory = True)
time_count = np.zeros(n_iter)
for n in range(n_iter):
    # here I hide the code for updating the imgs_display matrix based on the feedback
    torch.cuda.synchronize()
    start = time.perf_counter()
    imgData.copy_(imgs_display.flatten())
    torch.cuda.synchronize()
    time_count[n] = time.perf_counter()-start
    # here I hide the code for projecting the generated image patterns and getting the feedback signal      
</code></pre>
<p>I notice that if I comment the code for projecting images, the transfer time in each iteration is about 0.0053 s. The size of imgs_display is 66 MB and then the transfer speed is about 12.45 GB/s. matching the bandwidth measured in the benchmark. If I run the code for the first time (which means there is no warmup), the value is higher (0.02 s) at first. This high value at the beginning will disappear after I run it again.</p>
<p>Then, if I add the code for projecting images after transferring, the transfer speed is much slower. To mimic this situation, I use time.sleep() and the code is shown below:</p>
<pre><code class="lang-auto">imgs_display = torch.zeros([sample_number,800,1280],dtype = torch.uint8,device='cuda')
imgData = torch.zeros(sample_number*800*1280,dtype = torch.uint8,device='cpu',pin_memory = True)
time_count = np.zeros(n_iter)
for n in range(n_iter):
    # here I hide the code for updating the imgs_display matrix based on the feedback
    torch.cuda.synchronize()
    start = time.perf_counter()
    imgData.copy_(imgs_display.flatten())
    torch.cuda.synchronize()
    time_count[n] = time.perf_counter()-start
    time.sleep(0.02)
</code></pre>
<p>Here is the result for sleep time of 0, 0.02, 0.05 s:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/1/b18096eda4400a274c8515344b166833050c9858.png" data-download-href="https://discuss.pytorch.org/uploads/default/b18096eda4400a274c8515344b166833050c9858" title="summary"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/1/b18096eda4400a274c8515344b166833050c9858_2_659x500.png" alt="summary" data-base62-sha1="pkg0SlHMDOUgYwksL2k6QAz5ryE" width="659" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/1/b18096eda4400a274c8515344b166833050c9858_2_659x500.png, https://discuss.pytorch.org/uploads/default/original/3X/b/1/b18096eda4400a274c8515344b166833050c9858.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/b/1/b18096eda4400a274c8515344b166833050c9858.png 2x" data-dominant-color="FAFAFB"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">summary</span><span class="informations">974×738 51.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>For time.sleep longer than 0.1 s, it is always 0.02 s:</p>
<p>Given these observations, I have the following questions:</p>
<ol>
<li>From the fact the transfer speed is related to the sleep time during iteration, I assume it is caused by warmup, but which part is causing this problem? The temperature or something else?</li>
<li>In the real experiment, the kernel need to wait for the detector to get the feedback signal, and the time is not negligible (~0.1 s per iteration). Then the transfer time is almost four times larger than the one without sleep time. Is there anything I can do in order to keep the high transfer speed measured in the benchmark? I am wondering if I should find a way to keep GPU busy during the data acquisition time…</li>
</ol>
<p>Thank you in advance!</p>

ptrblck: <ol>
<li>
<p>I would guess your GPU puts itself into IDLE mode to save power when no work is needed. <code>nvidia-smi</code> will show the current power state in <code>Perf</code>. You could activate persistence mode and lock the clocks for proper profiling, but your GPU would then use of course more power without doing actual work.</p>
</li>
<li>
<p>Same as above - try to activate persistence mode.</p>
</li>
</ol>

qiazhang: <p>Thank you so much for your speedy reply!</p>
<p>It seems that persistence mode is not available on Windows… I found this answer from a forum:</p>
<blockquote>
<p>On Windows, nvidia-smi is not able to set persistence mode. Instead, you need to set your computational GPUs to TCC mode. This should be done through NVIDIA’s graphical GPU device management panel.</p>
</blockquote>
<p>It’s true that <em>nvidia-smi</em> shows <em>TCC/WDDM</em> instead of <em>Persistence Mode</em> in my case. Unfortunately, there is no TCC support on RTX GPUs. I find a useful answer from Nvidia Forum for those who are interested in (<a href="https://forums.developer.nvidia.com/t/do-all-of-the-new-rtx-gpus-support-tcc-mode/64383/9" class="inline-onebox" rel="noopener nofollow ugc">Do all of the new RTX GPUs support TCC mode? - #9 by genifycom - CUDA Setup and Installation - NVIDIA Developer Forums</a>).</p>
<blockquote>
<p>It should be noted that “TCC” is a unique feature / restriction relevant only to Windows. It’s sole feature is to allow peering in the Windows environment where that is otherwise not available because of Windows OS restrictions. GeForce cards have always been restricted in terms of peering in Windows.<br>
In Linux however, any version of Linux, TCC is irrelevant. What is relevant is hardware peer-to-peer capability enabling UVA (unified virtual addressing). Up until the 2000 series, All 900 series and 1000 series GeForce cards could peer (share memory) over pcie in Linux - which is really what this is all about.<br>
Beginning in the 2000 series of GeForce cards, peer-to-peer cannot be achieved over pcie. You could connect up to 2 cards over NVLink that had that interface (2070+). That restriction applies both to Windows, Linux, or any other OS environment, as well.</p>
</blockquote>
<p>I know now the question is not just about to pytorch, but do you have any suggestion based on this situation?</p>

ptrblck: <p>Thanks for sharing the link and answers!<br>
No, I’m not familiar enough with Windows and according to these answers TCC doesn’t seem to be available for your RTX GPU, so you might want to try out a Linux-based OS.</p>