COMPASS  5.4.4
End-to-end AO simulation tool using GPU acceleration
carma_timer.h
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------
2 // This file is part of COMPASS <https://anr-compass.github.io/compass/>
3 //
4 // Copyright (C) 2011-2023 COMPASS Team <https://github.com/ANR-COMPASS>
5 // All rights reserved.
6 
7 // -----------------------------------------------------------------------------
8 
16 
17 
18 #ifndef CARMA_TIMER_H_
19 #define CARMA_TIMER_H_
20 
21 #include <carma_context.h>
22 #include <carma_obj.h>
23 
27 class CarmaTimer {
28  protected:
29  cudaEvent_t start_event, stop_event;
30  cudaStream_t stream = 0;
31  double total_time;
32 
33  public:
35  carma_safe_call(cudaEventCreate(&start_event));
36  carma_safe_call(cudaEventCreate(&stop_event));
37  total_time = 0.0;
38  }
39 
41  cudaEventDestroy(start_event);
42  cudaEventDestroy(stop_event);
43  }
44 
45  void start() { carma_safe_call(cudaEventRecord(start_event, stream)); }
46 
47  void reset() { total_time = 0.0; }
48 
50  void stop() {
51  float gpuTime;
52  carma_safe_call(cudaEventRecord(stop_event, stream));
53  carma_safe_call(cudaEventSynchronize(stop_event));
54  carma_safe_call(cudaEventElapsedTime(&gpuTime, start_event, stop_event));
55  total_time += (double)1e-3 * gpuTime;
56  }
57 
58  void set_stream(cudaStream_t newStream) { stream = newStream; }
60  double elapsed() { return total_time; }
61 };
62 
63 void get_clock_count(long long int *clock_counter);
64 void get_clock_count(double *, long long int *clock_counter, double gpu_freq);
65 class CarmaClock {
66  public:
68  double gpu_freq;
69  long cc;
70  long long int *clock_counter;
71 
72  CarmaClock(CarmaContext *context, int i) {
73  cudaDeviceProp cdp;
74  cudaGetDeviceProperties(&cdp, context->get_active_device());
75  gpu_freq = cdp.clockRate * 1000;
76  long dims[2] = {1, i};
77  time_buffer = new CarmaObj<double>(context, dims);
78  cc = 0;
79  cudaMalloc(&clock_counter, sizeof(long long int));
80  }
81 
83  delete time_buffer;
84  cudaFree(clock_counter);
85  }
86 
88 
89  void toc() {
91  cc++;
92  if (cc >= time_buffer->get_nb_elements()) cc = 0;
93  }
94 };
95 #endif // CARMA_TIMER_H_
void get_clock_count(long long int *clock_counter)
#define carma_safe_call(err)
Definition: carma_utils.h:108
double gpu_freq
Definition: carma_timer.h:68
void toc()
Definition: carma_timer.h:89
CarmaObj< double > * time_buffer
Definition: carma_timer.h:67
CarmaClock(CarmaContext *context, int i)
Definition: carma_timer.h:72
long long int * clock_counter
Definition: carma_timer.h:70
void tic()
Definition: carma_timer.h:87
this class provides the context in which CarmaObj are created
Definition: carma_context.h:79
int get_active_device()
T_data * get_data_at(int index)
Definition: carma_obj.h:209
int get_nb_elements()
Definition: carma_obj.h:219
a simple timer for CUDA kernel.
Definition: carma_timer.h:27
cudaEvent_t start_event
Definition: carma_timer.h:29
void start()
Definition: carma_timer.h:45
void set_stream(cudaStream_t newStream)
Definition: carma_timer.h:58
cudaStream_t stream
Definition: carma_timer.h:30
cudaEvent_t stop_event
Definition: carma_timer.h:29
double elapsed()
Definition: carma_timer.h:60
void reset()
Definition: carma_timer.h:47
double total_time
Definition: carma_timer.h:31
void stop()
Definition: carma_timer.h:50