COMPASS  5.0.0
End-to-end AO simulation tool using GPU acceleration
carma_ipcs.h
1 // -----------------------------------------------------------------------------
2 // This file is part of COMPASS <https://anr-compass.github.io/compass/>
3 //
4 // Copyright (C) 2011-2019 COMPASS Team <https://github.com/ANR-COMPASS>
5 // All rights reserved.
6 // Distributed under GNU - LGPL
7 //
8 // COMPASS is free software: you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License as published by the Free
10 // Software Foundation, either version 3 of the License, or any later version.
11 //
12 // COMPASS: End-to-end AO simulation tool using GPU acceleration
13 // The COMPASS platform was designed to meet the need of high-performance for
14 // the simulation of AO systems.
15 //
16 // The final product includes a software package for simulating all the
17 // critical subcomponents of AO, particularly in the context of the ELT and a
18 // real-time core based on several control approaches, with performances
19 // consistent with its integration into an instrument. Taking advantage of the
20 // specific hardware architecture of the GPU, the COMPASS tool allows to
21 // achieve adequate execution speeds to conduct large simulation campaigns
22 // called to the ELT.
23 //
24 // The COMPASS platform can be used to carry a wide variety of simulations to
25 // both testspecific components of AO of the E-ELT (such as wavefront analysis
26 // device with a pyramid or elongated Laser star), and various systems
27 // configurations such as multi-conjugate AO.
28 //
29 // COMPASS is distributed in the hope that it will be useful, but WITHOUT ANY
30 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
31 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
32 // details.
33 //
34 // You should have received a copy of the GNU Lesser General Public License
35 // along with COMPASS. If not, see <https://www.gnu.org/licenses/lgpl-3.0.txt>.
36 // -----------------------------------------------------------------------------
37 
46 
47 #ifndef CARMA_IPCS_H
48 #define CARMA_IPCS_H
49 // cuda
50 #include <cuda.h>
51 // shms
52 #include <fcntl.h> /* For O_* constants */
53 #include <semaphore.h>
54 #include <sys/mman.h>
55 #include <sys/stat.h> /* For mode constants */
56 // malloc
57 #include <stdlib.h>
58 // getpid
59 #include <errno.h>
60 #include <limits.h>
61 #include <stdio.h>
62 #include <string.h>
63 #include <sys/types.h>
64 #include <unistd.h>
65 
66 #include <iterator>
67 #include <map>
68 
69 #define STAMP(fmt, args...) \
70  fprintf(stderr, "[%s@%d]:" fmt, __FUNCTION__, __LINE__, ##args)
71 
72 // todo: CIPCS_CHECK macro
73 
74 // sizeof(CUipcMemHandle) == sizeof(CUipcEventHandle)
75 // interessant !!
76 // possibilité d'unifier le tout et de déclarer moins de struct
77 // a creuseer
78 
79 typedef struct sh_cu_dptr_st {
80  char name[NAME_MAX + 1];
81  pid_t owner;
82  CUipcMemHandle handle;
83  sem_t var_mutex;
84 } sh_dptr;
85 
86 typedef struct sh_cu_event_st {
87  char name[NAME_MAX + 1];
88  pid_t owner;
89  CUipcEventHandle handle;
90  sem_t var_mutex;
91 } sh_event;
92 
93 typedef struct sh_buffer_st {
94  char name[NAME_MAX + 1];
95  bool isBoard;
96  size_t size;
97  size_t data_size;
98  unsigned int nb_proc;
99  void *p_shm;
100  sem_t mutex;
101  sem_t wait_pub;
102 } sh_buffer;
103 
104 typedef struct sh_barrier_st {
105  char name[NAME_MAX + 1];
106  bool valid;
107  unsigned int nb_proc;
108  unsigned int waiters_cnt;
109  unsigned int val;
110  sem_t b_sem;
111  sem_t var_mutex;
112 } sh_barrier;
113 
114 class CarmaIPCS {
115  private:
116  /*
117  maps used to control the shms
118  */
119  std::map<unsigned int, sh_dptr *> dptrs;
120  std::map<unsigned int, sh_event *> events;
121  std::map<unsigned int, sh_barrier *> barriers;
122  std::map<unsigned int, sh_buffer *> buffers;
123 
124  /*
125  General purpose methods
126  */
127  void *create_shm(const char *name, size_t size);
128  void *get_shm(const char *name);
129  void free_shm(const char *name, void *p_shm, size_t size);
130  void close_shm(void *p_shm, size_t size);
131  void complete_clean();
132 
133  /*
134  Transfer via CPU memory private methods
135  */
136  sh_buffer *get_elem_tshm(unsigned int id);
137  int write_gpu(void *dst, CUdeviceptr src, size_t bsize);
138  int read_gpu(CUdeviceptr dst, void *src, size_t bsize);
139 
140  public:
141  /*
142  general purpose methods
143  */
146 
147  /*
148  Cuda handles methods
149  */
150  // register a device pointer, id must be a non nul argument
151  int register_cudptr(unsigned int id, CUdeviceptr dptr);
152  // register an event, id must be a non nul argument
153  int register_cuevent(unsigned int id, CUevent event);
154 
155  // get a memory handle
156  int get_memHandle(unsigned int id, CUipcMemHandle *phandle);
157  // get a event handle
158  int get_eventHandle(unsigned int id, CUipcEventHandle *phandle);
159 
160  // free a memory handle shared mem space
161  void free_memHandle(unsigned int id);
162  // free a event handle shared event space
163  void free_eventHandle(unsigned int id);
164 
165  /*
166  Transfer via CPU memory methods
167  */
168  // allocation of the shm for memory tranfers
169  int alloc_transfer_shm(unsigned int id, size_t bsize, bool isBoard = false);
170  // return size in bytes of the transfer shm in bsize
171  int get_size_transfer_shm(unsigned int id, size_t *bsize);
172  // return actual data size used in bytes of the transfer shm in bsize
173  int get_datasize_transfer_shm(unsigned int id, size_t *bsize);
174  // map the shm buffer to the cuda device
175  int map_transfer_shm(unsigned int id);
176  // write to a transfer shm referenced by id
177  int write_transfer_shm(unsigned int id, const void *src, size_t bsize,
178  bool gpuBuffer = false);
179  // reads from a transfer shm referenced by id
180  int read_transfer_shm(unsigned int id, void *dst, size_t bsize,
181  bool gpuBuffer = false);
182  // map the shm buffer to the cuda device
183  int unmap_transfer_shm(unsigned int id);
184  // free transfer shm ref by id
185  void free_transfer_shm(unsigned int id);
186 
187  /*
188  Barrier methods
189  */
190  // initialize a barrier with value the number of process who will call
191  // wait_barrier
192  int init_barrier(unsigned int id, unsigned int value);
193  // wait for the other process subscribed to the barrier
194  int wait_barrier(unsigned int id);
195  // free the barrier structure, all blocked process will be unlocked<
196  void free_barrier(unsigned int id);
197 };
198 
199 #endif // CARMA_IPCS_H
sh_cu_dptr_st
Definition: carma_ipcs.h:79
sh_barrier_st::name
char name[NAME_MAX+1]
Definition: carma_ipcs.h:105
sh_barrier_st
Definition: carma_ipcs.h:104
sh_barrier_st::var_mutex
sem_t var_mutex
Definition: carma_ipcs.h:111
sh_cu_dptr_st::var_mutex
sem_t var_mutex
Definition: carma_ipcs.h:83
sh_cu_dptr_st::handle
CUipcMemHandle handle
Definition: carma_ipcs.h:82
CarmaIPCS::free_eventHandle
void free_eventHandle(unsigned int id)
CarmaIPCS::register_cuevent
int register_cuevent(unsigned int id, CUevent event)
sh_barrier_st::b_sem
sem_t b_sem
Definition: carma_ipcs.h:110
sh_buffer_st
Definition: carma_ipcs.h:93
CarmaIPCS
Definition: carma_ipcs.h:114
CarmaIPCS::get_memHandle
int get_memHandle(unsigned int id, CUipcMemHandle *phandle)
sh_barrier_st::val
unsigned int val
Definition: carma_ipcs.h:109
CarmaIPCS::get_datasize_transfer_shm
int get_datasize_transfer_shm(unsigned int id, size_t *bsize)
sh_cu_dptr_st::owner
pid_t owner
Definition: carma_ipcs.h:81
sh_barrier_st::valid
bool valid
Definition: carma_ipcs.h:106
sh_cu_event_st::owner
pid_t owner
Definition: carma_ipcs.h:88
CarmaIPCS::read_transfer_shm
int read_transfer_shm(unsigned int id, void *dst, size_t bsize, bool gpuBuffer=false)
CarmaIPCS::unmap_transfer_shm
int unmap_transfer_shm(unsigned int id)
CarmaIPCS::wait_barrier
int wait_barrier(unsigned int id)
CarmaIPCS::get_size_transfer_shm
int get_size_transfer_shm(unsigned int id, size_t *bsize)
CarmaIPCS::alloc_transfer_shm
int alloc_transfer_shm(unsigned int id, size_t bsize, bool isBoard=false)
CarmaIPCS::free_transfer_shm
void free_transfer_shm(unsigned int id)
sh_buffer_st::data_size
size_t data_size
Definition: carma_ipcs.h:97
CarmaIPCS::free_barrier
void free_barrier(unsigned int id)
sh_buffer_st::size
size_t size
Definition: carma_ipcs.h:96
sh_buffer_st::nb_proc
unsigned int nb_proc
Definition: carma_ipcs.h:98
CarmaIPCS::map_transfer_shm
int map_transfer_shm(unsigned int id)
sh_buffer_st::wait_pub
sem_t wait_pub
Definition: carma_ipcs.h:101
sh_cu_dptr_st::name
char name[NAME_MAX+1]
Definition: carma_ipcs.h:80
sh_buffer_st::isBoard
bool isBoard
Definition: carma_ipcs.h:95
sh_cu_event_st::var_mutex
sem_t var_mutex
Definition: carma_ipcs.h:90
CarmaIPCS::init_barrier
int init_barrier(unsigned int id, unsigned int value)
sh_barrier_st::nb_proc
unsigned int nb_proc
Definition: carma_ipcs.h:107
sh_cu_event_st::name
char name[NAME_MAX+1]
Definition: carma_ipcs.h:87
sh_barrier_st::waiters_cnt
unsigned int waiters_cnt
Definition: carma_ipcs.h:108
CarmaIPCS::register_cudptr
int register_cudptr(unsigned int id, CUdeviceptr dptr)
CarmaIPCS::free_memHandle
void free_memHandle(unsigned int id)
CarmaIPCS::CarmaIPCS
CarmaIPCS()
sh_cu_event_st::handle
CUipcEventHandle handle
Definition: carma_ipcs.h:89
CarmaIPCS::get_eventHandle
int get_eventHandle(unsigned int id, CUipcEventHandle *phandle)
sh_cu_event_st
Definition: carma_ipcs.h:86
CarmaIPCS::write_transfer_shm
int write_transfer_shm(unsigned int id, const void *src, size_t bsize, bool gpuBuffer=false)
sh_buffer_st::name
char name[NAME_MAX+1]
Definition: carma_ipcs.h:94
sh_buffer_st::p_shm
void * p_shm
Definition: carma_ipcs.h:99
sh_buffer_st::mutex
sem_t mutex
Definition: carma_ipcs.h:100
CarmaIPCS::~CarmaIPCS
~CarmaIPCS()