mem_mgr.txx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. #include <omp.h>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <cassert>
  5. #include <pvfmm/profile.hpp>
  6. namespace pvfmm {
  7. #ifdef PVFMM_MEMDEBUG
  8. template <class ValueType> inline ConstIterator<ValueType>::ConstIterator(const ValueType* base_, difference_type len_, bool dynamic_alloc) {
  9. this->base = (char*)base_;
  10. this->len = len_ * (Long)sizeof(ValueType);
  11. this->offset = 0;
  12. PVFMM_ASSERT_MSG((uintptr_t)(this->base + this->offset) % alignof(ValueType) == 0, "invalid alignment during pointer type conversion.");
  13. if (dynamic_alloc) {
  14. MemoryManager::MemHead& mh = *&MemoryManager::GetMemHead((char*)this->base);
  15. MemoryManager::CheckMemHead(mh);
  16. alloc_ctr = mh.alloc_ctr;
  17. mem_head = &mh;
  18. } else
  19. mem_head = NULL;
  20. }
  21. template <class ValueType> inline static void IteratorAssertChecks(Long offset, Long len, void* mem_head, Long alloc_ctr) {
  22. PVFMM_ASSERT_MSG(offset >= 0 && offset + (Long)sizeof(ValueType) <= len, "access to pointer [B" << (offset < 0 ? "" : "+") << offset << ",B" << (offset + (Long)sizeof(ValueType) < 0 ? "" : "+") << offset + (Long)sizeof(ValueType) << ") is outside of the range [B,B+" << len << ").");
  23. if (mem_head) {
  24. MemoryManager::MemHead& mh = *(MemoryManager::MemHead*)(mem_head);
  25. PVFMM_ASSERT_MSG(mh.alloc_ctr == alloc_ctr, "invalid memory address or corrupted memory.");
  26. }
  27. }
  28. template <class ValueType> inline typename ConstIterator<ValueType>::reference ConstIterator<ValueType>::operator*() const {
  29. IteratorAssertChecks<ValueType>(this->offset, this->len, this->mem_head, this->alloc_ctr);
  30. return *(ValueType*)(base + offset);
  31. }
  32. template <class ValueType> inline const typename ConstIterator<ValueType>::value_type* ConstIterator<ValueType>::operator->() const {
  33. IteratorAssertChecks<ValueType>(this->offset, this->len, this->mem_head, this->alloc_ctr);
  34. return (ValueType*)(base + offset);
  35. }
  36. template <class ValueType> inline typename ConstIterator<ValueType>::reference ConstIterator<ValueType>::operator[](difference_type j) const {
  37. IteratorAssertChecks<ValueType>(this->offset + j * (Long)sizeof(ValueType), this->len, this->mem_head, this->alloc_ctr);
  38. return *(ValueType*)(base + offset + j * (Long)sizeof(ValueType));
  39. }
  40. template <class ValueType> inline typename Iterator<ValueType>::reference Iterator<ValueType>::operator*() const {
  41. IteratorAssertChecks<ValueType>(this->offset, this->len, this->mem_head, this->alloc_ctr);
  42. return *(ValueType*)(this->base + this->offset);
  43. }
  44. template <class ValueType> inline typename Iterator<ValueType>::value_type* Iterator<ValueType>::operator->() const {
  45. IteratorAssertChecks<ValueType>(this->offset, this->len, this->mem_head, this->alloc_ctr);
  46. return (ValueType*)(this->base + this->offset);
  47. }
  48. template <class ValueType> inline typename Iterator<ValueType>::reference Iterator<ValueType>::operator[](difference_type j) const {
  49. IteratorAssertChecks<ValueType>(this->offset + j * (Long)sizeof(ValueType), this->len, this->mem_head, this->alloc_ctr);
  50. return *(ValueType*)(this->base + this->offset + j * (Long)sizeof(ValueType));
  51. }
  52. template <class ValueType, Long DIM> inline StaticArray<ValueType, DIM>::StaticArray() {
  53. arr = aligned_new<ValueType>(DIM);
  54. Iterator<ValueType>::operator=(arr);
  55. }
  56. template <class ValueType, Long DIM> inline StaticArray<ValueType, DIM>::~StaticArray() { aligned_delete<ValueType>(arr); }
  57. template <class ValueType, Long DIM> inline StaticArray<ValueType, DIM>::StaticArray(const StaticArray& I) {
  58. arr = aligned_new<ValueType>(DIM);
  59. Iterator<ValueType>::operator=(arr);
  60. for (Long i = 0; i < DIM; i++) (*this)[i] = I[i];
  61. }
  62. template <class ValueType, Long DIM> inline StaticArray<ValueType, DIM>& StaticArray<ValueType, DIM>::operator=(const StaticArray& I) {
  63. for (Long i = 0; i < DIM; i++) (*this)[i] = I[i];
  64. return *this;
  65. }
  66. #endif
  67. template <class T> inline uintptr_t TypeTraits<T>::ID() { return (uintptr_t) & ID; }
  68. template <class T> inline bool TypeTraits<T>::IsPOD() { return false; }
  69. #define PVFMMDefinePOD(type) \
  70. template <> bool inline TypeTraits<type>::IsPOD() { \
  71. return true; \
  72. };
  73. PVFMMDefinePOD(char);
  74. PVFMMDefinePOD(float);
  75. PVFMMDefinePOD(double);
  76. PVFMMDefinePOD(int);
  77. PVFMMDefinePOD(long long);
  78. PVFMMDefinePOD(unsigned long);
  79. PVFMMDefinePOD(char*);
  80. PVFMMDefinePOD(float*);
  81. PVFMMDefinePOD(double*);
  82. #undef PVFMMDefinePOD
  83. inline MemoryManager::MemoryManager(Long N) {
  84. buff_size = N;
  85. { // Allocate buff
  86. assert(PVFMM_MEM_ALIGN <= 0x8000);
  87. Long alignment = PVFMM_MEM_ALIGN - 1;
  88. char* base_ptr = (char*)::malloc(N + 2 + alignment);
  89. PVFMM_ASSERT_MSG(base_ptr, "memory allocation failed.");
  90. buff = (char*)((uintptr_t)(base_ptr + 2 + alignment) & ~(uintptr_t)alignment);
  91. ((uint16_t*)buff)[-1] = (uint16_t)(buff - base_ptr);
  92. }
  93. { // Initialize to init_mem_val
  94. #ifdef PVFMM_MEMDEBUG
  95. #pragma omp parallel for
  96. for (Long i = 0; i < buff_size; i++) {
  97. buff[i] = init_mem_val;
  98. }
  99. #endif
  100. }
  101. n_dummy_indx = new_node();
  102. Long n_indx = new_node();
  103. MemNode& n_dummy = node_buff[n_dummy_indx - 1];
  104. MemNode& n = node_buff[n_indx - 1];
  105. n_dummy.size = 0;
  106. n_dummy.free = false;
  107. n_dummy.prev = 0;
  108. n_dummy.next = n_indx;
  109. n_dummy.mem_ptr = &buff[0];
  110. assert(n_indx);
  111. n.size = N;
  112. n.free = true;
  113. n.prev = n_dummy_indx;
  114. n.next = 0;
  115. n.mem_ptr = &buff[0];
  116. n.it = free_map.insert(std::make_pair(N, n_indx));
  117. omp_init_lock(&omp_lock);
  118. }
  119. inline MemoryManager::~MemoryManager() {
  120. Check();
  121. MemNode* n_dummy = &node_buff[n_dummy_indx - 1];
  122. MemNode* n = &node_buff[n_dummy->next - 1];
  123. if (!n->free || n->size != buff_size || node_stack.size() != node_buff.size() - 2 || !system_malloc.empty()) {
  124. PVFMM_WARN("memory leak detected.");
  125. }
  126. omp_destroy_lock(&omp_lock);
  127. { // free buff
  128. assert(buff);
  129. ::free(buff - ((uint16_t*)buff)[-1]);
  130. }
  131. }
  132. inline MemoryManager::MemHead& MemoryManager::GetMemHead(char* I) {
  133. PVFMM_ASSERT_MSG(I != NULL, "NULL pointer exception.");
  134. static uintptr_t alignment = PVFMM_MEM_ALIGN - 1;
  135. static uintptr_t header_size = (uintptr_t)(sizeof(MemHead) + alignment) & ~(uintptr_t)alignment;
  136. return *(MemHead*)(((char*)I) - header_size);
  137. }
  138. inline void MemoryManager::CheckMemHead(const MemHead& mem_head) { // Verify header check_sum
  139. #ifdef PVFMM_MEMDEBUG
  140. Long check_sum = 0;
  141. const unsigned char* base_ = (const unsigned char*)&mem_head;
  142. for (Integer i = 0; i < sizeof(MemHead); i++) {
  143. check_sum += base_[i];
  144. }
  145. check_sum -= mem_head.check_sum;
  146. check_sum = check_sum & ((1UL << (8 * sizeof(mem_head.check_sum))) - 1);
  147. PVFMM_ASSERT_MSG(check_sum == mem_head.check_sum, "invalid memory address or corrupted memory.");
  148. #endif
  149. }
  150. inline Iterator<char> MemoryManager::malloc(const Long n_elem, const Long type_size, const uintptr_t type_id) const {
  151. if (!n_elem) return NULL;
  152. static uintptr_t alignment = PVFMM_MEM_ALIGN - 1;
  153. static uintptr_t header_size = (uintptr_t)(sizeof(MemHead) + alignment) & ~(uintptr_t)alignment;
  154. Long size = n_elem * type_size + header_size;
  155. size = (uintptr_t)(size + alignment) & ~(uintptr_t)alignment;
  156. char* base = NULL;
  157. omp_set_lock(&omp_lock);
  158. static Long alloc_ctr = 0;
  159. alloc_ctr++;
  160. Long head_alloc_ctr = alloc_ctr;
  161. std::multimap<Long, Long>::iterator it = free_map.lower_bound(size);
  162. Long n_indx = (it != free_map.end() ? it->second : 0);
  163. if (n_indx) { // Allocate from buff
  164. Long n_free_indx = (it->first > size ? new_node() : 0);
  165. MemNode& n = node_buff[n_indx - 1];
  166. assert(n.size == it->first);
  167. assert(n.it == it);
  168. assert(n.free);
  169. if (n_free_indx) { // Create a node for the remaining free part.
  170. MemNode& n_free = node_buff[n_free_indx - 1];
  171. n_free = n;
  172. n_free.size -= size;
  173. n_free.mem_ptr = (char*)n_free.mem_ptr + size;
  174. { // Insert n_free to the link list
  175. n_free.prev = n_indx;
  176. if (n_free.next) {
  177. Long n_next_indx = n_free.next;
  178. MemNode& n_next = node_buff[n_next_indx - 1];
  179. n_next.prev = n_free_indx;
  180. }
  181. n.next = n_free_indx;
  182. }
  183. assert(n_free.free); // Insert n_free to free map
  184. n_free.it = free_map.insert(std::make_pair(n_free.size, n_free_indx));
  185. n.size = size; // Update n
  186. }
  187. n.free = false;
  188. free_map.erase(it);
  189. base = n.mem_ptr;
  190. }
  191. omp_unset_lock(&omp_lock);
  192. if (!base) { // Use system malloc
  193. Long end_padding = 8; // to check for out-of-bound writes
  194. char* p = (char*)::malloc(size + 2 + alignment + end_padding);
  195. PVFMM_ASSERT_MSG(p, "memory allocation failed.");
  196. #ifdef PVFMM_MEMDEBUG
  197. { // system_malloc.insert(p)
  198. omp_set_lock(&omp_lock);
  199. system_malloc.insert(p);
  200. omp_unset_lock(&omp_lock);
  201. }
  202. { // set p[*] to init_mem_val
  203. #pragma omp parallel for
  204. for (Long i = 0; i < size + 2 + alignment + end_padding; i++) p[i] = init_mem_val;
  205. }
  206. #endif
  207. { // base <-- align(p)
  208. base = (char*)((uintptr_t)(p + 2 + alignment) & ~(uintptr_t)alignment);
  209. ((uint16_t*)base)[-1] = (uint16_t)(base - p);
  210. }
  211. }
  212. { // Check out-of-bounds write
  213. #ifdef PVFMM_MEMDEBUG
  214. if (n_indx) {
  215. #pragma omp parallel for
  216. for (Long i = 0; i < size; i++) PVFMM_ASSERT_MSG(base[i] == init_mem_val, "memory corruption detected.");
  217. }
  218. #endif
  219. }
  220. MemHead& mem_head = *(MemHead*)base;
  221. { // Set mem_head
  222. #ifdef PVFMM_MEMDEBUG
  223. for (Integer i = 0; i < sizeof(MemHead); i++) base[i] = init_mem_val;
  224. #endif
  225. mem_head.n_indx = n_indx;
  226. mem_head.n_elem = n_elem;
  227. mem_head.type_size = type_size;
  228. mem_head.alloc_ctr = head_alloc_ctr;
  229. mem_head.type_id = type_id;
  230. }
  231. { // Set header check_sum
  232. #ifdef PVFMM_MEMDEBUG
  233. Long check_sum = 0;
  234. unsigned char* base_ = (unsigned char*)base;
  235. mem_head.check_sum = 0;
  236. for (Integer i = 0; i < sizeof(MemHead); i++) check_sum += base_[i];
  237. check_sum = check_sum & ((1UL << (8 * sizeof(mem_head.check_sum))) - 1);
  238. mem_head.check_sum = check_sum;
  239. #endif
  240. }
  241. Profile::Add_MEM(n_elem * type_size);
  242. #ifdef PVFMM_MEMDEBUG
  243. return Iterator<char>(base + header_size, n_elem * type_size, true);
  244. #else
  245. return base + header_size;
  246. #endif
  247. }
  248. inline void MemoryManager::free(Iterator<char> p) const {
  249. if (p == NULL) return;
  250. static uintptr_t alignment = PVFMM_MEM_ALIGN - 1;
  251. static uintptr_t header_size = (uintptr_t)(sizeof(MemHead) + alignment) & ~(uintptr_t)alignment;
  252. MemHead& mem_head = GetMemHead(&p[0]);
  253. Long n_indx = mem_head.n_indx;
  254. Long n_elem = mem_head.n_elem;
  255. Long type_size = mem_head.type_size;
  256. char* base = (char*)&mem_head;
  257. { // Verify header check_sum; set array to init_mem_val
  258. #ifdef PVFMM_MEMDEBUG
  259. CheckMemHead(mem_head);
  260. Long size = mem_head.n_elem * mem_head.type_size;
  261. #pragma omp parallel for
  262. for (Long i = 0; i < size; i++) p[i] = init_mem_val;
  263. for (Integer i = 0; i < sizeof(MemHead); i++) base[i] = init_mem_val;
  264. #endif
  265. }
  266. if (n_indx == 0) { // Use system free
  267. assert(base < &buff[0] || base >= &buff[buff_size]);
  268. char* p_;
  269. { // p_ <-- unalign(base)
  270. p_ = (char*)((uintptr_t)base - ((uint16_t*)base)[-1]);
  271. }
  272. #ifdef PVFMM_MEMDEBUG
  273. { // Check out-of-bounds write
  274. base[-1] = init_mem_val;
  275. base[-2] = init_mem_val;
  276. Long size = n_elem * type_size + header_size;
  277. size = (uintptr_t)(size + alignment) & ~(uintptr_t)alignment;
  278. Long end_padding = 8; // to check for out-of-bound writes
  279. #pragma omp parallel for
  280. for (Long i = 0; i < size + 2 + alignment + end_padding; i++) {
  281. PVFMM_ASSERT_MSG(p_[i] == init_mem_val, "memory corruption detected.");
  282. }
  283. }
  284. { // system_malloc.erase(p_)
  285. omp_set_lock(&omp_lock);
  286. PVFMM_ASSERT_MSG(system_malloc.erase(p_) == 1, "double free or corruption.");
  287. omp_unset_lock(&omp_lock);
  288. }
  289. #endif
  290. ::free(p_);
  291. } else {
  292. assert(n_indx <= node_buff.size());
  293. omp_set_lock(&omp_lock);
  294. MemNode& n = node_buff[n_indx - 1];
  295. assert(!n.free && n.size > 0 && n.mem_ptr == base);
  296. if (n.prev != 0 && node_buff[n.prev - 1].free) {
  297. Long n_prev_indx = n.prev;
  298. MemNode& n_prev = node_buff[n_prev_indx - 1];
  299. n.size += n_prev.size;
  300. n.mem_ptr = n_prev.mem_ptr;
  301. n.prev = n_prev.prev;
  302. free_map.erase(n_prev.it);
  303. delete_node(n_prev_indx);
  304. if (n.prev) {
  305. node_buff[n.prev - 1].next = n_indx;
  306. }
  307. }
  308. if (n.next != 0 && node_buff[n.next - 1].free) {
  309. Long n_next_indx = n.next;
  310. MemNode& n_next = node_buff[n_next_indx - 1];
  311. n.size += n_next.size;
  312. n.next = n_next.next;
  313. free_map.erase(n_next.it);
  314. delete_node(n_next_indx);
  315. if (n.next) {
  316. node_buff[n.next - 1].prev = n_indx;
  317. }
  318. }
  319. n.free = true; // Insert n to free_map
  320. n.it = free_map.insert(std::make_pair(n.size, n_indx));
  321. omp_unset_lock(&omp_lock);
  322. }
  323. Profile::Add_MEM(-n_elem * type_size);
  324. }
  325. inline void MemoryManager::print() const {
  326. if (!buff_size) return;
  327. omp_set_lock(&omp_lock);
  328. Long size = 0;
  329. Long largest_size = 0;
  330. MemNode* n = &node_buff[n_dummy_indx - 1];
  331. std::cout << "\n|";
  332. while (n->next) {
  333. n = &node_buff[n->next - 1];
  334. if (n->free) {
  335. std::cout << ' ';
  336. largest_size = std::max(largest_size, n->size);
  337. } else {
  338. std::cout << '#';
  339. size += n->size;
  340. }
  341. }
  342. std::cout << "| allocated=" << round(size * 1000.0 / buff_size) / 10 << "%";
  343. std::cout << " largest_free=" << round(largest_size * 1000.0 / buff_size) / 10 << "%\n";
  344. omp_unset_lock(&omp_lock);
  345. }
  346. inline void MemoryManager::test() {
  347. Long M = 2000000000;
  348. { // With memory manager
  349. Long N = M * sizeof(double) * 1.1;
  350. double tt;
  351. Iterator<double> tmp;
  352. std::cout << "With memory manager: ";
  353. MemoryManager memgr(N);
  354. for (Integer j = 0; j < 3; j++) {
  355. tmp = (Iterator<double>)memgr.malloc(M * sizeof(double));
  356. PVFMM_ASSERT(tmp != NULL);
  357. tt = omp_get_wtime();
  358. #pragma omp parallel for
  359. for (Long i = 0; i < M; i += 64) tmp[i] = (double)i;
  360. tt = omp_get_wtime() - tt;
  361. std::cout << tt << ' ';
  362. memgr.free((Iterator<char>)tmp);
  363. }
  364. std::cout << '\n';
  365. }
  366. { // Without memory manager
  367. double tt;
  368. double* tmp;
  369. std::cout << "Without memory manager: ";
  370. for (Integer j = 0; j < 3; j++) {
  371. tmp = (double*)::malloc(M * sizeof(double));
  372. PVFMM_ASSERT(tmp != NULL);
  373. tt = omp_get_wtime();
  374. #pragma omp parallel for
  375. for (Long i = 0; i < M; i += 64) tmp[i] = (double)i;
  376. tt = omp_get_wtime() - tt;
  377. std::cout << tt << ' ';
  378. ::free(tmp);
  379. }
  380. std::cout << '\n';
  381. }
  382. }
  383. inline void MemoryManager::Check() const {
  384. #ifdef PVFMM_MEMDEBUG
  385. // print();
  386. omp_set_lock(&omp_lock);
  387. MemNode* curr_node = &node_buff[n_dummy_indx - 1];
  388. while (curr_node->next) {
  389. curr_node = &node_buff[curr_node->next - 1];
  390. if (curr_node->free) {
  391. char* base = curr_node->mem_ptr;
  392. #pragma omp parallel for
  393. for (Long i = 0; i < curr_node->size; i++) {
  394. PVFMM_ASSERT_MSG(base[i] == init_mem_val, "memory corruption detected.");
  395. }
  396. }
  397. }
  398. omp_unset_lock(&omp_lock);
  399. #endif
  400. }
  401. inline Long MemoryManager::new_node() const {
  402. if (node_stack.empty()) {
  403. node_buff.resize(node_buff.size() + 1);
  404. node_stack.push(node_buff.size());
  405. }
  406. Long indx = node_stack.top();
  407. node_stack.pop();
  408. assert(indx);
  409. return indx;
  410. }
  411. inline void MemoryManager::delete_node(Long indx) const {
  412. assert(indx);
  413. assert(indx <= node_buff.size());
  414. MemNode& n = node_buff[indx - 1];
  415. n.free = false;
  416. n.size = 0;
  417. n.prev = 0;
  418. n.next = 0;
  419. n.mem_ptr = NULL;
  420. node_stack.push(indx);
  421. }
  422. template <class ValueType> inline Iterator<ValueType> aligned_new(Long n_elem, const MemoryManager* mem_mgr) {
  423. if (!n_elem) return NULL;
  424. static MemoryManager def_mem_mgr(0);
  425. if (!mem_mgr) mem_mgr = &def_mem_mgr;
  426. Iterator<ValueType> A = (Iterator<ValueType>)mem_mgr->malloc(n_elem, sizeof(ValueType));
  427. PVFMM_ASSERT_MSG(A != NULL, "memory allocation failed.");
  428. if (!TypeTraits<ValueType>::IsPOD()) { // Call constructors
  429. // printf("%s\n", __PRETTY_FUNCTION__);
  430. #pragma omp parallel for schedule(static)
  431. for (Long i = 0; i < n_elem; i++) {
  432. ValueType* Ai = new (&A[i]) ValueType();
  433. assert(Ai == (&A[i]));
  434. }
  435. } else {
  436. #ifdef PVFMM_MEMDEBUG
  437. static Long random_init_val = 1;
  438. Iterator<char> A_ = (Iterator<char>)A;
  439. #pragma omp parallel for schedule(static)
  440. for (Long i = 0; i < n_elem * sizeof(ValueType); i++) {
  441. A_[i] = random_init_val + i;
  442. }
  443. random_init_val += n_elem * sizeof(ValueType);
  444. #endif
  445. }
  446. return A;
  447. }
  448. template <class ValueType> inline void aligned_delete(Iterator<ValueType> A, const MemoryManager* mem_mgr) {
  449. if (A == NULL) return;
  450. if (!TypeTraits<ValueType>::IsPOD()) { // Call destructors
  451. // printf("%s\n", __PRETTY_FUNCTION__);
  452. MemoryManager::MemHead& mem_head = MemoryManager::GetMemHead((char*)&A[0]);
  453. #ifdef PVFMM_MEMDEBUG
  454. MemoryManager::CheckMemHead(mem_head);
  455. // PVFMM_ASSERT_MSG(mem_head.n_elem==1 || mem_head.type_id==TypeTraits<ValueType>::ID(), "pointer to aligned_delete has different type than what was used in aligned_new.");
  456. #endif
  457. Long n_elem = mem_head.n_elem;
  458. for (Long i = 0; i < n_elem; i++) {
  459. A[i].~ValueType();
  460. }
  461. } else {
  462. #ifdef PVFMM_MEMDEBUG
  463. MemoryManager::MemHead& mem_head = MemoryManager::GetMemHead((char*)&A[0]);
  464. MemoryManager::CheckMemHead(mem_head);
  465. // PVFMM_ASSERT_MSG(mem_head.type_id==TypeTraits<ValueType>::ID(), "pointer to aligned_delete has different type than what was used in aligned_new.");
  466. Long size = mem_head.n_elem * mem_head.type_size;
  467. Iterator<char> A_ = (Iterator<char>)A;
  468. #pragma omp parallel for
  469. for (Long i = 0; i < size; i++) {
  470. A_[i] = 0;
  471. }
  472. #endif
  473. }
  474. static MemoryManager def_mem_mgr(0);
  475. if (!mem_mgr) mem_mgr = &def_mem_mgr;
  476. mem_mgr->free((Iterator<char>)A);
  477. }
  478. template <class ValueType> inline Iterator<ValueType> memcopy(Iterator<ValueType> destination, ConstIterator<ValueType> source, Long num) {
  479. if (destination != source && num) {
  480. #ifdef PVFMM_MEMDEBUG
  481. destination[num - 1];
  482. source[num - 1];
  483. #endif
  484. if (TypeTraits<ValueType>::IsPOD()) {
  485. memcpy(&destination[0], &source[0], num * sizeof(ValueType));
  486. } else {
  487. for (Long i = 0; i < num; i++) destination[i] = source[i];
  488. }
  489. }
  490. return destination;
  491. }
  492. template <class ValueType> inline Iterator<ValueType> memset(Iterator<ValueType> ptr, int value, Long num) {
  493. if (num) {
  494. #ifdef PVFMM_MEMDEBUG
  495. ptr[0];
  496. ptr[num - 1];
  497. #endif
  498. ::memset(&ptr[0], value, num * sizeof(ValueType));
  499. }
  500. return ptr;
  501. }
  502. } // end namespace