cudafreelist.C
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef RUTZ_CUDAFREELIST_CC_DEFINED
00035 #define RUTZ_CUDAFREELIST_CC_DEFINED
00036
00037 #include "CUDA/cudafreelist.H"
00038 #include "CUDA/CudaDevices.H"
00039 #include "Util/Assert.H"
00040 #include <map>
00041
00042 rutz::cuda_free_list_base::cuda_free_list_base(std::size_t size_check) :
00043 m_size_check(size_check)
00044 {
00045 for(int d=0;d<MAX_CUDA_DEVICES;d++)
00046 {
00047 m_node_list[d] = 0;
00048 m_num_allocations[d] = 0;
00049 }
00050 }
00051
00052 void* rutz::cuda_free_list_base::allocate(std::size_t bytes, int dev)
00053 {
00054 ASSERT(bytes == m_size_check);
00055
00056 int index = find_index_from_device(dev);
00057
00058 if (m_node_list[index] == 0)
00059 {
00060 ++m_num_allocations[index];
00061 void *d;
00062 CudaDevices::malloc(&d,bytes,dev);
00063 return d;
00064 }
00065 node* n = m_node_list[index];
00066 m_node_list[index] = m_node_list[index]->next;
00067 void *mem = n->mem;
00068
00069 delete n;
00070 return mem;
00071 }
00072
00073 void rutz::cuda_free_list_base::deallocate(void* space, int dev)
00074 {
00075 int index = find_index_from_device(dev);
00076
00077 node* n = new node();
00078 n->mem = space;
00079 n->next = m_node_list[index];
00080 m_node_list[index] = n;
00081 }
00082
00083 int rutz::cuda_free_list_base::get_num_nodes(int dev)
00084 {
00085 int index = find_index_from_device(dev);
00086 node *n = m_node_list[index];
00087 int nodes=0;
00088 while(n != NULL)
00089 {
00090 n = n->next;
00091 nodes++;
00092 }
00093 return nodes;
00094 }
00095
00096 int rutz::cuda_free_list_base::get_index_from_device(int dev)
00097 {
00098 if( devices.find(dev) == devices.end ())
00099 {
00100 return -1;
00101 }
00102 else
00103 {
00104 return devices[dev];
00105 }
00106 }
00107
00108 int rutz::cuda_free_list_base::find_index_from_device(int dev)
00109 {
00110 int index = get_index_from_device(dev);
00111 if(index == -1)
00112 {
00113 index = devices.size();
00114 devices.insert(std::pair<int,int>(dev,index));
00115 }
00116 return index;
00117 }
00118
00119 void rutz::cuda_free_list_base::release_free_nodes()
00120 {
00121 std::map<int,int>::iterator it;
00122 for ( it=devices.begin() ; it != devices.end(); it++ )
00123 {
00124
00125 int dev = (*it).first;
00126 int index = (*it).second;
00127 while (m_node_list[index] != 0)
00128 {
00129 void* mem = m_node_list[index]->mem;
00130 node* n = m_node_list[index];
00131 m_node_list[index] = m_node_list[index]->next;
00132 delete n;
00133 CudaDevices::free(mem,dev);
00134 --m_num_allocations[index];
00135 }
00136 }
00137 }
00138
00139 #endif // !RUTZ_CUDAFREELIST_CC_DEFINED