00001 /** @file rutz/freelist.h 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 07:54:29 2001 00010 // commit: $Id: cudafreelist.H 12962 2010-03-06 02:13:53Z irock $ 00011 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/CUDA/cudafreelist.H $ 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_H_DEFINED 00035 #define RUTZ_FREELIST_H_DEFINED 00036 00037 #include <cstddef> 00038 #include <map> 00039 #include "CUDA/cudadefs.h" 00040 00041 namespace rutz 00042 { 00043 class cuda_free_list_base; 00044 template <class T> class cuda_free_list; 00045 } 00046 00047 00048 /// Un-typesafe base class for maintaining a free-list memory pool. 00049 class rutz::cuda_free_list_base 00050 { 00051 private: 00052 /// Free-node class for free-list memory pools. 00053 struct node 00054 { 00055 node* next; 00056 void *mem; 00057 }; 00058 00059 node* m_node_list[MAX_CUDA_DEVICES]; 00060 std::size_t m_num_allocations[MAX_CUDA_DEVICES]; 00061 std::map<int,int> devices; 00062 const std::size_t m_size_check; 00063 // Return -1 if device not found 00064 int get_index_from_device(int dev); 00065 // Add device if not found 00066 int find_index_from_device(int dev); 00067 cuda_free_list_base(const cuda_free_list_base&); 00068 cuda_free_list_base& operator=(const cuda_free_list_base&); 00069 00070 public: 00071 /// Construct an (empty) free list. 00072 /** All objects from this list must be of size \a size_check. */ 00073 cuda_free_list_base(std::size_t size_check); 00074 00075 /// Allocate space for a new object. 00076 /** If there are chunks available in the free list, one of those is 00077 returned; otherwise new memory is allocated with malloc() or 00078 equivalent. */ 00079 void* allocate(std::size_t bytes, int dev); 00080 00081 /// Return an object to the free list. 00082 void deallocate(void* space, int dev); 00083 00084 /// Release all nodes currently on the free list (e.g. to conserve memory). 00085 void release_free_nodes(); 00086 00087 // Get the number of nodes currently on this list 00088 int get_num_nodes(int dev); 00089 00090 std::map<int,int>::const_iterator getDevicesBegin() { return devices.begin(); } 00091 std::map<int,int>::const_iterator getDevicesEnd() { return devices.end(); } 00092 00093 /// Query the chunk size that this freelist is for. 00094 std::size_t alloc_size() const { return m_size_check; } 00095 00096 /// Query how many allocations have been made. 00097 std::size_t num_allocations(int dev) { int i= get_index_from_device(dev); return (i<0) ? std::size_t(0) : m_num_allocations[i]; } 00098 }; 00099 00100 /// Typesafe wrapper of free_list_base for maintaining free-list memory pools. 00101 template <class T> 00102 class rutz::cuda_free_list 00103 { 00104 private: 00105 rutz::cuda_free_list_base m_base; 00106 00107 public: 00108 /// Construct an (empty) free list. 00109 /** All objects allocated from this list must be of size sizeof(T). */ 00110 cuda_free_list() : m_base(sizeof(T)) {} 00111 00112 void* allocate(std::size_t bytes, int dev) 00113 { 00114 return m_base.allocate(bytes,dev); 00115 } 00116 00117 void deallocate(void* space, int dev) 00118 { 00119 m_base.deallocate(space,dev); 00120 } 00121 00122 /// Release all nodes currently on the free list (e.g. to conserve memory). 00123 void release_free_nodes() { m_base.release_free_nodes(); } 00124 00125 // Get the number of nodes currently on this list 00126 int get_num_nodes(int dev) { return m_base.get_num_nodes(dev);} 00127 00128 }; 00129 00130 static const char __attribute__((used)) vcid_rutz_cudafreelist_h[] = "$Id: cudafreelist.H 12962 2010-03-06 02:13:53Z irock $ $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/CUDA/cudafreelist.H $"; 00131 #endif // !RUTZ_CUDAFREELIST_H_DEFINED