openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
limpet_data.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // openCARP is an open cardiac electrophysiology simulator.
3 //
4 // Copyright (C) 2020 openCARP project
5 //
6 // This program is licensed under the openCARP Academic Public License (APL)
7 // v1.0: You can use and redistribute it and/or modify it in non-commercial
8 // academic environments under the terms of APL as published by the openCARP
9 // project v1.0, or (at your option) any later version. Commercial use requires
10 // a commercial license (info@opencarp.org).
11 //
12 // This program is distributed without any warranty; see the openCARP APL for
13 // more details.
14 //
15 // You should have received a copy of the openCARP APL along with this program
16 // and can find it online: http://www.opencarp.org/license
17 // ----------------------------------------------------------------------------
18 
19 #ifndef LIMPET_DATA_H
20 #define LIMPET_DATA_H
21 
22 #include "target.h"
23 #ifdef HAS_ROCM_MODEL
24 #include <hip/hip_runtime.h>
25 #endif
26 #ifdef HAS_CUDA_MODEL
27 #include <cuda_runtime.h>
28 #endif
29 #include <iostream>
30 
31 namespace limpet {
32 template<typename T>
33 class LimpetData {
34  public:
35  LimpetData(Target target, std::size_t n) : _target(target), _size(n) {
36  this->_data = allocate_on_target<T>(this->_target, this->_size);
37  this->_count = new std::size_t(0);
38  };
39 
40  LimpetData() : _target(Target::UNKNOWN), _size(0), _data(nullptr) {}
41 
43  if (*this->_count == 0) {
44  deallocate_on_target<T>(this->_target, this->_data);
45  }
46  else {
47  (*this->_count)--;
48  }
49  }
50 
51 #if defined HAS_ROCM_MODEL || HAS_CUDA_MODEL
52  __device__ __host__
53 #endif
54  T *data() const {
55  return this->_data;
56  }
57 
59  if (this != &other) {
60  this->_data = other._data;
61  this->_size = other._size;
62  this->_target = other._target;
63  this->_count = other._count;
64  (*this->_count)++;
65  }
66  return *this;
67  }
68 
69  T operator*() {
70  return *this->_data;
71  }
72  T* operator&() {
73  return this->_data;
74  }
75 
76  private:
77  Target _target;
78  std::size_t _size;
79  T *_data = nullptr;
80  std::size_t *_count = nullptr;
81  bool _copied;
82 };
83 }
84 
85 #endif // LIMPET_DATA_H
LimpetData< T > & operator=(const LimpetData< T > &other)
Definition: limpet_data.h:58
special value to handle unknown targets
Definition: target.h:47
LimpetData(Target target, std::size_t n)
Definition: limpet_data.h:35
Defines valid targets for an ionic model to run on and an allocator for allocating memory on a specif...
Target
enum that represents different targets to run ionic models on.
Definition: target.h:45
T * data() const
Definition: limpet_data.h:54