Swarm-NG  1.1
allocators.hpp
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) 2011 by Saleh Dindar and the Swarm-NG Development Team *
3  * *
4  * This program is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU General Public License as published by *
6  * the Free Software Foundation; either version 3 of the License. *
7  * *
8  * This program is distributed in the hope that it will be useful, *
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
11  * GNU General Public License for more details. *
12  * *
13  * You should have received a copy of the GNU General Public License *
14  * along with this program; if not, write to the *
15  * Free Software Foundation, Inc., *
16  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
17  ************************************************************************/
47 #pragma once
48 
52 template< class T >
54  typedef T Elem;
55  static void free(T * p) { delete[] p; }
56  static T * alloc(size_t s) { return new T[s]; }
57 
58  static void copy( T* begin, T* end, T* dst ) {
59  std::copy ( begin, end, dst );
60  }
61 
62  static T* clone (T* begin, T* end) {
63  T* p = alloc( end - begin );
64  copy( begin, end, p );
65  return p;
66  }
67 };
68 
74 template< class T >
76  typedef T Elem;
77  static void free(T * p) { cudaFree(p); }
78  static T * alloc(size_t s) {
79  void* p;
80  cudaMalloc(&p, s * sizeof(T) );
81  return (T*)p;
82  }
83 
84  static void copy( T* begin, T* end, T* dst ) {
85  cudaMemcpy( dst, begin, (end-begin)*sizeof(T), cudaMemcpyDeviceToDevice );
86  }
87 
88  static T* clone (T* begin, T* end) {
89  T* p = alloc( end - begin );
90  copy( begin, end, p );
91  return p;
92  }
93 };
94 
95 
101 template< class T >
103  typedef T Elem;
104  static void free(T * p) { cudaFreeHost(p); }
105  static T * alloc(size_t s) {
106  T* p;
107  cudaMallocHost(&p, s * sizeof(T) );
108  return p;
109  }
110 
111  static void copy( T* begin, T* end, T* dst ) {
112  cudaMemcpy( dst, begin, (end-begin)*sizeof(T), cudaMemcpyHostToHost );
113  }
114 
115  static T* clone (T* begin, T* end) {
116  T* p = alloc( end - begin );
117  copy( begin, end, p );
118  return p;
119  }
120 };
121 
128 template< class T >
129 struct MappedHostAllocator : public HostAllocator<T> {
130  static T * alloc(size_t s) {
131  T* p;
132  cudaHostAlloc(&p, s * sizeof(T), cudaHostAllocMapped );
133  return p;
134  }
135 
141  static T* getDevicePointer(T* hp){
142  T* dp;
143  cudaGetDevicePointer(&dp,hp,0);
144  return dp;
145  }
146 };
147 
148 
151 template< class A, class T>
152 void alloc_copy(A,A, T* begin, T* end, T* dst){
153  A::copy(begin,end,dst);
154 }
155 
157 template< class T>
158 void alloc_copy(DefaultAllocator<T>,DeviceAllocator<T>, T* begin, T* end, T* dst){
159  cudaMemcpy(dst, begin, (end-begin)*sizeof(T), cudaMemcpyHostToDevice);
160 }
161 
163 template< class T>
164 void alloc_copy(DeviceAllocator<T>,DefaultAllocator<T>, T* begin, T* end, T* dst){
165  cudaMemcpy(dst, begin, (end-begin)*sizeof(T), cudaMemcpyDeviceToHost);
166 }
167