OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
memory_array.hpp
1/*
2 * memory_stride.hpp
3 *
4 * Created on: Aug 17, 2014
5 * Author: Pietro Incardona
6 */
7
8#ifndef MEMORY_ARRAY_HPP_
9#define MEMORY_ARRAY_HPP_
10
11#include "memory/memory.hpp"
12
13#include "util/cuda_util.hpp"
14
25template<typename T>
27{
28
29#if defined(__GNUG__) || defined(__clang__)
30
32 T __attribute__((aligned(16))) * ptr;
33
34#else
35
37 T * ptr;
38
39#endif
40
42 size_t sz;
43
51 T get(mem_id i)
52 {
53 return ptr[i];
54 }
55
56
57 public:
58
60 typedef T value_type;
61
71 void initialize(void * ptr, size_t sz, bool init)
72 {
73 this->ptr = static_cast<T *>(ptr);
74
75
76 // Initialize the constructors
77
78 if (init == false)
79 new (ptr)T[sz];
80
81 this->sz = sz;
82 }
83
85 size_t size()
86 {
87 return sz;
88 }
89
95 void set_pointer(void * ptr_)
96 {
97 ptr = static_cast<T *>(ptr_);
98 }
99
101 __device__ __host__ void * get_pointer() const
102 {
103 return ptr;
104 }
105
111 template<typename Tbind> void bind_ref(const memory_array<Tbind> & ref)
112 {
113 ptr = static_cast<T *>(ref.get_pointer());
114 }
115
123 __host__ __device__ T & operator[](mem_id i)
124 {
125 return ptr[i];
126 }
127
135 __host__ __device__ const T & operator[](mem_id i) const
136 {
137 return ptr[i];
138 }
139
146 {
147 size_t sz_tmp = sz;
148 sz = obj.sz;
149 obj.sz = sz_tmp;
150
151 T * ptr_tmp = ptr;
152 ptr = obj.ptr;
153 obj.ptr = ptr_tmp;
154 }
155
161 void deinit()
162 {
163 // Call the destructor of every objects
164 for (size_t i = 0 ; i < sz ; i++)
165 {
166 (&ptr[i])->~T();
167 }
168 }
169
172 :ptr(NULL),sz(0)
173 {};
174
182 memory_array(void * ptr, size_t sz, bool init)
183 {
184 initialize(ptr,sz,init);
185 }
186
193 {
194 };
195};
196
197
198#endif
This class give a representation to a chunk or memory.
T * ptr
Internal pointer.
size_t sz
number of elements
void set_pointer(void *ptr_)
Set the internal pointer to the indicated chunk of memory.
~memory_array()
Destructor.
memory_array(void *ptr, size_t sz, bool init)
Memory array constructor.
void initialize(void *ptr, size_t sz, bool init)
Initialize the memory array.
__host__ __device__ const T & operator[](mem_id i) const
Access element an element of the array.
memory_array()
Default constructor.
size_t size()
return the size of the array
void deinit()
Deinitialize the memory.
__host__ __device__ T & operator[](mem_id i)
Access element an element of the array.
__device__ __host__ void * get_pointer() const
Return the pointer.
void bind_ref(const memory_array< Tbind > &ref)
Set from another memory array.
T get(mem_id i)
Get the i element.
T value_type
type returned by this structure
void swap(memory_array< T > &obj)
swap the two objects memory