Swarm-NG  1.1
coalescedstructarray.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  ************************************************************************/
18 
23 #pragma once
24 
25 
28 #ifdef __CUDACC__
29 #define GENERIC inline __device__ __host__
30 #define GPUAPI inline __device__
31 #else
32 #define GENERIC inline
33 #define GPUAPI inline
34 #endif
35 
36 
37 namespace swarm {
38 
91 template<class Item, typename _Scalar = typename Item::scalar_t, int _CHUNK_SIZE = Item::CHUNK_SIZE >
93 public:
94  static const int CHUNK_SIZE = _CHUNK_SIZE;
96  typedef Item* PItem;
97  typedef _Scalar scalar_t;
98 
99 private:
100  PItem _array;
101  size_t _block_count;
102 
103 public:
122  :_array(array),_block_count(block_count){}
123 
135  GENERIC Item& operator[] ( const int & i ) {
136  size_t block_idx = i / CHUNK_SIZE;
137  size_t idx = i % CHUNK_SIZE;
138  scalar_t * blockaddr = (scalar_t*) (get() + block_idx);
139  return * (Item *) ( blockaddr + idx );
140  }
141 
146  GENERIC int block_count()const{
147  return _block_count ;
148  }
152  GENERIC int size()const{
153  return _block_count * CHUNK_SIZE;
154  }
155 
159  GENERIC Item * get() {
160  return _array;
161  }
162 
167  GENERIC Item * begin() {
168  return get();
169  }
170 
175  GENERIC Item * end() {
176  return get() + _block_count;
177  }
178 };
179 
181 template<int W>
183  typedef double scalar_t;
184  double _value[W];
185  GENERIC double& value(){ return _value[0]; }
186 };
187 
188 }