cudafreelist.C

00001 /** @file rutz/freelist.cc memory allocation via a free-list pool */
00002 
00003 ///////////////////////////////////////////////////////////////////////
00004 //
00005 // Copyright (c) 2001-2004 California Institute of Technology
00006 // Copyright (c) 2004-2007 University of Southern California
00007 // Rob Peters <rjpeters at usc dot edu>
00008 //
00009 // created: Fri Jul 20 08:00:31 2001
00010 // commit: $Id: cudafreelist.C 12962 2010-03-06 02:13:53Z irock $
00011 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/CUDA/cudafreelist.C $
00012 //
00013 // --------------------------------------------------------------------
00014 //
00015 // This file is part of GroovX.
00016 //   [http://ilab.usc.edu/rjpeters/groovx/]
00017 //
00018 // GroovX is free software; you can redistribute it and/or modify it
00019 // under the terms of the GNU General Public License as published by
00020 // the Free Software Foundation; either version 2 of the License, or
00021 // (at your option) any later version.
00022 //
00023 // GroovX is distributed in the hope that it will be useful, but
00024 // WITHOUT ANY WARRANTY; without even the implied warranty of
00025 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00026 // General Public License for more details.
00027 //
00028 // You should have received a copy of the GNU General Public License
00029 // along with GroovX; if not, write to the Free Software Foundation,
00030 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
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   // Check if the device has been used
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   // Free the node
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       // Key is device number, value is the index
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
Generated on Sun May 8 08:40:36 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3