XGC1
scratch.hpp
Go to the documentation of this file.
1 #ifndef SCRATCH_HPP
2 #define SCRATCH_HPP
3 
4 #include <mutex>
5 #include <iostream>
6 #include "space_settings.hpp"
7 
8 class ScratchLock;
9 
10 class Scratch {
11 public:
12  Scratch(size_t n_double_host, size_t n_double_device);
14 
15  KOKKOS_INLINE_FUNCTION void pop_host(size_t n) {
16  //std::lock_guard<std::mutex> lock(mutex_);
17  host_offset_ -= n;
18  }
19 
20  KOKKOS_INLINE_FUNCTION void pop_device(size_t n) {
21  //std::lock_guard<std::mutex> lock(mutex_);
22  device_offset_ -= n;
23  }
24 
25  void report_peak_usage() const;
26 
27 private:
28  friend class ScratchLock;
29 
31  View<double*, CLayout, HostType> host_pool_;
32  View<double*, CLayout, DeviceType> device_pool_;
33 
34  size_t host_offset_;
36 
39 
40  size_t host_peak_;
41  size_t device_peak_;
42 
43  //std::mutex mutex_;
44 };
45 
46 extern std::unique_ptr<Scratch> GLOBAL_SCRATCH;
47 
48 class ScratchLock {
49 public:
50  enum Layout{
51  KokkosDefaultLayout = true
52  };
53 
54  template<typename Device> View<double*, CLayout, Device> get_view(const std::string& label, int dim1);
55  template<typename Device> View<double**, CLayout, Device> get_view(const std::string& label, int dim1, int dim2);
56  template<typename Device> View<double***, CLayout, Device> get_view(const std::string& label, int dim1, int dim2, int dim3);
57  template<typename Device> View<double****, CLayout, Device> get_view(const std::string& label, int dim1, int dim2, int dim3, int dim4);
58  template<typename Device> View<double*****, CLayout, Device> get_view(const std::string& label, int dim1, int dim2, int dim3, int dim4, int dim5);
59  template<typename Device> View<double******, CLayout, Device> get_view(const std::string& label, int dim1, int dim2, int dim3, int dim4, int dim5, int dim6);
60 
61  template<typename Device> View<double*, Device> get_view(const std::string& label, int dim1, Layout kdl_true);
62  template<typename Device> View<double**, Device> get_view(const std::string& label, int dim1, int dim2, Layout kdl_true);
63  template<typename Device> View<double***, Device> get_view(const std::string& label, int dim1, int dim2, int dim3, Layout kdl_true);
64  template<typename Device> View<double****, Device> get_view(const std::string& label, int dim1, int dim2, int dim3, int dim4, Layout kdl_true);
65  template<typename Device> View<double*****, Device> get_view(const std::string& label, int dim1, int dim2, int dim3, int dim4, int dim5, Layout kdl_true);
66  template<typename Device> View<double******, Device> get_view(const std::string& label, int dim1, int dim2, int dim3, int dim4, int dim5, int dim6, Layout kdl_true);
67 
68  // Constructor
69  ScratchLock(Scratch* scratch)
70  : scratch_(scratch), is_used_(false) {}
71 
72  // Default Constructor
73  KOKKOS_INLINE_FUNCTION ScratchLock()
74  : scratch_(nullptr), is_used_(false) {}
75 
76  // Destructor
77  KOKKOS_INLINE_FUNCTION ~ScratchLock() {
78  if(is_used_){
79  if (used_scratch_) {
80  if (is_device_){
81  //printf("Releasing %s scratch range: %lu-%lu\n", is_device_ ? "device" : "host", allocation_offset_, scratch_->device_offset_);
83  assert(scratch_->device_offset_ == allocation_offset_ && "FILO order violated: device memory was not released in reverse allocation order.");
84  }else{
85  //printf("Releasing %s scratch range: %lu-%lu\n", is_device_ ? "device" : "host", allocation_offset_, scratch_->host_offset_);
87  assert(scratch_->host_offset_ == allocation_offset_ && "FILO order violated: host memory was not released in reverse allocation order.");
88  }
89  }
90  if (is_device_){
91  //printf("Releasing %s tracked range: %lu-%lu\n", is_device_ ? "device" : "host", scratch_->device_unlimited_offset_-allocation_size_, scratch_->device_unlimited_offset_);
93  }else{
94  //printf("Releasing %s tracked range: %lu-%lu\n", is_device_ ? "device" : "host", scratch_->host_unlimited_offset_-allocation_size_, scratch_->host_unlimited_offset_);
96  }
97  }
98  }
99 
100  // Copy constructor: don't transfer ownership. Only original has ownership!
101  KOKKOS_INLINE_FUNCTION ScratchLock(const ScratchLock& other)
102  : scratch_(other.scratch_),
105  is_device_(other.is_device_),
107  is_used_(false) {}
108 
109  // Copy assignment: don't transfer ownership. Only original has ownership!
111  scratch_ = other.scratch_;
114  is_device_ = other.is_device_;
116  is_used_ = false;
117  return *this;
118  }
119 
120 
121 private:
127  bool is_used_;
128 
129  template<typename Device> double* get_ptr(size_t view_size);
130 };
131 
132 #endif
Definition: scratch.hpp:48
size_t allocation_offset_
Definition: scratch.hpp:124
Scratch * scratch_
Definition: scratch.hpp:122
double * get_ptr(size_t view_size)
Definition: scratch.cpp:26
bool used_scratch_
Definition: scratch.hpp:126
KOKKOS_INLINE_FUNCTION ScratchLock(const ScratchLock &other)
Definition: scratch.hpp:101
bool is_device_
Definition: scratch.hpp:125
KOKKOS_INLINE_FUNCTION ~ScratchLock()
Definition: scratch.hpp:77
Layout
Definition: scratch.hpp:50
@ KokkosDefaultLayout
Definition: scratch.hpp:51
size_t allocation_size_
Definition: scratch.hpp:123
KOKKOS_INLINE_FUNCTION ScratchLock()
Definition: scratch.hpp:73
View< double *, CLayout, Device > get_view(const std::string &label, int dim1)
Definition: scratch.cpp:73
ScratchLock(Scratch *scratch)
Definition: scratch.hpp:69
ScratchLock & operator=(const ScratchLock &other)
Definition: scratch.hpp:110
bool is_used_
Definition: scratch.hpp:127
Definition: scratch.hpp:10
bool is_initialized
Definition: scratch.hpp:30
View< double *, CLayout, HostType > host_pool_
Definition: scratch.hpp:31
size_t device_offset_
Definition: scratch.hpp:35
KOKKOS_INLINE_FUNCTION void pop_device(size_t n)
Definition: scratch.hpp:20
size_t host_offset_
Definition: scratch.hpp:34
size_t host_unlimited_offset_
Definition: scratch.hpp:37
size_t device_peak_
Definition: scratch.hpp:41
View< double *, CLayout, DeviceType > device_pool_
Definition: scratch.hpp:32
void report_peak_usage() const
Definition: scratch.cpp:14
Scratch()
Definition: scratch.hpp:13
KOKKOS_INLINE_FUNCTION void pop_host(size_t n)
Definition: scratch.hpp:15
size_t device_unlimited_offset_
Definition: scratch.hpp:38
size_t host_peak_
Definition: scratch.hpp:40
logical false
Definition: module.F90:102
std::unique_ptr< Scratch > GLOBAL_SCRATCH
Definition: scratch.cpp:208