Aging_MouthReplace / dlibs /docs /dlib /cuda /cuda_data_ptr.cpp.html
AshanGimhana's picture
Upload folder using huggingface_hub
9375c9a verified
raw
history blame
15.1 kB
<html><!-- Created using the cpp_pretty_printer from the dlib C++ library. See http://dlib.net for updates. --><head><title>dlib C++ Library - cuda_data_ptr.cpp</title></head><body bgcolor='white'><pre>
<font color='#009900'>// Copyright (C) 2017 Davis E. King ([email protected])
</font><font color='#009900'>// License: Boost Software License See LICENSE.txt for the full license.
</font><font color='#0000FF'>#ifndef</font> DLIB_DNN_CuDA_DATA_PTR_CPP_
<font color='#0000FF'>#define</font> DLIB_DNN_CuDA_DATA_PTR_CPP_
<font color='#0000FF'>#ifdef</font> DLIB_USE_CUDA
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='cuda_data_ptr.h.html'>cuda_data_ptr.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='cuda_utils.h.html'>cuda_utils.h</a>"
<font color='#0000FF'>namespace</font> dlib
<b>{</b>
<font color='#0000FF'>namespace</font> cuda
<b>{</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
weak_cuda_data_void_ptr::
<b><a name='weak_cuda_data_void_ptr'></a>weak_cuda_data_void_ptr</b><font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> cuda_data_void_ptr<font color='#5555FF'>&amp;</font> ptr
<font face='Lucida Console'>)</font> : num<font face='Lucida Console'>(</font>ptr.num<font face='Lucida Console'>)</font>, pdata<font face='Lucida Console'>(</font>ptr.pdata<font face='Lucida Console'>)</font>
<b>{</b>
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
cuda_data_void_ptr weak_cuda_data_void_ptr::
<b><a name='lock'></a>lock</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
<b>{</b>
<font color='#0000FF'>auto</font> ptr <font color='#5555FF'>=</font> pdata.<font color='#BB00BB'>lock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>ptr<font face='Lucida Console'>)</font>
<b>{</b>
cuda_data_void_ptr temp;
temp.pdata <font color='#5555FF'>=</font> ptr;
temp.num <font color='#5555FF'>=</font> num;
<font color='#0000FF'>return</font> temp;
<b>}</b>
<font color='#0000FF'>else</font>
<b>{</b>
<font color='#0000FF'>return</font> <font color='#BB00BB'>cuda_data_void_ptr</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<b>}</b>
<b>}</b>
<font color='#009900'>// -----------------------------------------------------------------------------------
</font> <font color='#009900'>// -----------------------------------------------------------------------------------
</font>
cuda_data_void_ptr::
<b><a name='cuda_data_void_ptr'></a>cuda_data_void_ptr</b><font face='Lucida Console'>(</font>
<font color='#0000FF'><u>size_t</u></font> n
<font face='Lucida Console'>)</font> : num<font face='Lucida Console'>(</font>n<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>n <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
<font color='#0000FF'>return</font>;
<font color='#0000FF'><u>void</u></font><font color='#5555FF'>*</font> data <font color='#5555FF'>=</font> nullptr;
<font color='#BB00BB'>CHECK_CUDA</font><font face='Lucida Console'>(</font><font color='#BB00BB'>cudaMalloc</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>data, n<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
pdata.<font color='#BB00BB'>reset</font><font face='Lucida Console'>(</font>data, []<font face='Lucida Console'>(</font><font color='#0000FF'><u>void</u></font><font color='#5555FF'>*</font> ptr<font face='Lucida Console'>)</font><b>{</b>
<font color='#0000FF'>auto</font> err <font color='#5555FF'>=</font> <font color='#BB00BB'>cudaFree</font><font face='Lucida Console'>(</font>ptr<font face='Lucida Console'>)</font>;
<font color='#0000FF'>if</font><font face='Lucida Console'>(</font>err<font color='#5555FF'>!</font><font color='#5555FF'>=</font>cudaSuccess<font face='Lucida Console'>)</font>
std::cerr <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>cudaFree() failed. Reason: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>cudaGetErrorString</font><font face='Lucida Console'>(</font>err<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> std::endl;
<b>}</b><font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// ------------------------------------------------------------------------------------
</font>
<font color='#0000FF'><u>void</u></font> <b><a name='memcpy'></a>memcpy</b><font face='Lucida Console'>(</font>
<font color='#0000FF'><u>void</u></font><font color='#5555FF'>*</font> dest,
<font color='#0000FF'>const</font> cuda_data_void_ptr<font color='#5555FF'>&amp;</font> src,
<font color='#0000FF'>const</font> <font color='#0000FF'><u>size_t</u></font> num
<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font>num <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> src.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>src.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
<b>{</b>
<font color='#BB00BB'>CHECK_CUDA</font><font face='Lucida Console'>(</font><font color='#BB00BB'>cudaMemcpy</font><font face='Lucida Console'>(</font>dest, src.<font color='#BB00BB'>data</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, num, cudaMemcpyDefault<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<b>}</b>
<b>}</b>
<font color='#009900'>// ------------------------------------------------------------------------------------
</font>
<font color='#0000FF'><u>void</u></font> <b><a name='memcpy'></a>memcpy</b><font face='Lucida Console'>(</font>
<font color='#0000FF'><u>void</u></font><font color='#5555FF'>*</font> dest,
<font color='#0000FF'>const</font> cuda_data_void_ptr<font color='#5555FF'>&amp;</font> src
<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#BB00BB'>memcpy</font><font face='Lucida Console'>(</font>dest, src, src.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// ------------------------------------------------------------------------------------
</font>
<font color='#0000FF'><u>void</u></font> <b><a name='memcpy'></a>memcpy</b><font face='Lucida Console'>(</font>
cuda_data_void_ptr dest,
<font color='#0000FF'>const</font> <font color='#0000FF'><u>void</u></font><font color='#5555FF'>*</font> src,
<font color='#0000FF'>const</font> <font color='#0000FF'><u>size_t</u></font> num
<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font>num <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> dest.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>dest.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
<b>{</b>
<font color='#BB00BB'>CHECK_CUDA</font><font face='Lucida Console'>(</font><font color='#BB00BB'>cudaMemcpy</font><font face='Lucida Console'>(</font>dest.<font color='#BB00BB'>data</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, src, num, cudaMemcpyDefault<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<b>}</b>
<b>}</b>
<font color='#009900'>// ------------------------------------------------------------------------------------
</font>
<font color='#0000FF'><u>void</u></font> <b><a name='memcpy'></a>memcpy</b><font face='Lucida Console'>(</font>
cuda_data_void_ptr dest,
<font color='#0000FF'>const</font> <font color='#0000FF'><u>void</u></font><font color='#5555FF'>*</font> src
<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#BB00BB'>memcpy</font><font face='Lucida Console'>(</font>dest,src,dest.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// ------------------------------------------------------------------------------------
</font>
<font color='#0000FF'>class</font> <b><a name='cudnn_device_buffer'></a>cudnn_device_buffer</b>
<b>{</b>
<font color='#0000FF'>public</font>:
<font color='#009900'>// not copyable
</font> <b><a name='cudnn_device_buffer'></a>cudnn_device_buffer</b><font face='Lucida Console'>(</font><font color='#0000FF'>const</font> cudnn_device_buffer<font color='#5555FF'>&amp;</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#0000FF'>delete</font>;
cudnn_device_buffer<font color='#5555FF'>&amp;</font> <b><a name='operator'></a>operator</b><font color='#5555FF'>=</font><font face='Lucida Console'>(</font><font color='#0000FF'>const</font> cudnn_device_buffer<font color='#5555FF'>&amp;</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#0000FF'>delete</font>;
<b><a name='cudnn_device_buffer'></a>cudnn_device_buffer</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
<b>{</b>
buffers.<font color='#BB00BB'>resize</font><font face='Lucida Console'>(</font><font color='#979000'>16</font><font face='Lucida Console'>)</font>;
<b>}</b>
~<b><a name='cudnn_device_buffer'></a>cudnn_device_buffer</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
<b>{</b>
<b>}</b>
cuda_data_void_ptr <b><a name='get'></a>get</b> <font face='Lucida Console'>(</font>
<font color='#0000FF'><u>size_t</u></font> size
<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#0000FF'><u>int</u></font> new_device_id;
<font color='#BB00BB'>CHECK_CUDA</font><font face='Lucida Console'>(</font><font color='#BB00BB'>cudaGetDevice</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>new_device_id<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// make room for more devices if needed
</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>new_device_id <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font><font face='Lucida Console'>)</font>buffers.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
buffers.<font color='#BB00BB'>resize</font><font face='Lucida Console'>(</font>new_device_id<font color='#5555FF'>+</font><font color='#979000'>16</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// If we don't have a buffer already for this device then make one, or if it's too
</font> <font color='#009900'>// small, make a bigger one.
</font> cuda_data_void_ptr buff <font color='#5555FF'>=</font> buffers[new_device_id].<font color='#BB00BB'>lock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#5555FF'>!</font>buff <font color='#5555FF'>|</font><font color='#5555FF'>|</font> buff.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font> size<font face='Lucida Console'>)</font>
<b>{</b>
buff <font color='#5555FF'>=</font> <font color='#BB00BB'>cuda_data_void_ptr</font><font face='Lucida Console'>(</font>size<font face='Lucida Console'>)</font>;
buffers[new_device_id] <font color='#5555FF'>=</font> buff;
<b>}</b>
<font color='#009900'>// Finally, return the buffer for the current device
</font> <font color='#0000FF'>return</font> buff;
<b>}</b>
<font color='#0000FF'>private</font>:
std::vector<font color='#5555FF'>&lt;</font>weak_cuda_data_void_ptr<font color='#5555FF'>&gt;</font> buffers;
<b>}</b>;
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
cuda_data_void_ptr <b><a name='device_global_buffer'></a>device_global_buffer</b><font face='Lucida Console'>(</font><font color='#0000FF'><u>size_t</u></font> size<font face='Lucida Console'>)</font>
<b>{</b>
thread_local cudnn_device_buffer buffer;
<font color='#0000FF'>return</font> buffer.<font color='#BB00BB'>get</font><font face='Lucida Console'>(</font>size<font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// ------------------------------------------------------------------------------------
</font>
<b>}</b>
<b>}</b>
<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_USE_CUDA
</font>
<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_DNN_CuDA_DATA_PTR_CPP_
</font>
</pre></body></html>