2015-07-03 Richard Biener <rguenther@suse.de>
[official-gcc.git] / liboffloadmic / runtime / offload_host.h
blob2212dec51494510adeda3374785bddd418e31f23
1 /*
2 Copyright (c) 2014 Intel Corporation. All Rights Reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 * Neither the name of Intel Corporation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 /*! \file
32 \brief The parts of the runtime library used only on the host
35 #ifndef OFFLOAD_HOST_H_INCLUDED
36 #define OFFLOAD_HOST_H_INCLUDED
38 #ifndef TARGET_WINNT
39 #include <unistd.h>
40 #endif // TARGET_WINNT
41 #include "offload_common.h"
42 #include "offload_util.h"
43 #include "offload_engine.h"
44 #include "offload_env.h"
45 #include "offload_orsl.h"
46 #include "coi/coi_client.h"
48 // MIC engines.
49 extern Engine* mic_engines;
50 extern uint32_t mic_engines_total;
52 //! The target image is packed as follows.
53 /*! 1. 8 bytes containing the size of the target binary */
54 /*! 2. a null-terminated string which is the binary name */
55 /*! 3. <size> number of bytes that are the contents of the image */
56 /*! The address of symbol __offload_target_image
57 is the address of this structure. */
58 struct Image {
59 int64_t size; //!< Size in bytes of the target binary name and contents
60 char data[]; //!< The name and contents of the target image
63 // The offload descriptor.
64 class OffloadDescriptor
66 public:
67 OffloadDescriptor(
68 int index,
69 _Offload_status *status,
70 bool is_mandatory,
71 bool is_openmp,
72 OffloadHostTimerData * timer_data
73 ) :
74 m_device(mic_engines[index % mic_engines_total]),
75 m_is_mandatory(is_mandatory),
76 m_is_openmp(is_openmp),
77 m_inout_buf(0),
78 m_func_desc(0),
79 m_func_desc_size(0),
80 m_in_deps(0),
81 m_in_deps_total(0),
82 m_out_deps(0),
83 m_out_deps_total(0),
84 m_vars(0),
85 m_vars_extra(0),
86 m_status(status),
87 m_timer_data(timer_data)
90 ~OffloadDescriptor()
92 if (m_in_deps != 0) {
93 free(m_in_deps);
95 if (m_out_deps != 0) {
96 free(m_out_deps);
98 if (m_func_desc != 0) {
99 free(m_func_desc);
101 if (m_vars != 0) {
102 free(m_vars);
103 free(m_vars_extra);
107 bool offload(const char *name, bool is_empty,
108 VarDesc *vars, VarDesc2 *vars2, int vars_total,
109 const void **waits, int num_waits, const void **signal,
110 int entry_id, const void *stack_addr);
111 bool offload_finish();
113 bool is_signaled();
115 OffloadHostTimerData* get_timer_data() const {
116 return m_timer_data;
119 private:
120 bool wait_dependencies(const void **waits, int num_waits);
121 bool setup_descriptors(VarDesc *vars, VarDesc2 *vars2, int vars_total,
122 int entry_id, const void *stack_addr);
123 bool setup_misc_data(const char *name);
124 bool send_pointer_data(bool is_async);
125 bool send_noncontiguous_pointer_data(
126 int i,
127 PtrData* src_buf,
128 PtrData* dst_buf,
129 COIEVENT *event);
130 bool recieve_noncontiguous_pointer_data(
131 int i,
132 char* src_data,
133 COIBUFFER dst_buf,
134 COIEVENT *event);
136 bool gather_copyin_data();
138 bool compute();
140 bool receive_pointer_data(bool is_async);
141 bool scatter_copyout_data();
143 void cleanup();
145 bool find_ptr_data(PtrData* &ptr_data, void *base, int64_t disp,
146 int64_t length, bool error_does_not_exist = true);
147 bool alloc_ptr_data(PtrData* &ptr_data, void *base, int64_t disp,
148 int64_t length, int64_t alloc_disp, int align);
149 bool init_static_ptr_data(PtrData *ptr_data);
150 bool init_mic_address(PtrData *ptr_data);
151 bool offload_stack_memory_manager(const void * stack_begin, int routine_id,
152 int buf_size, int align, bool *is_new);
153 bool nullify_target_stack(COIBUFFER targ_buf, uint64_t size);
155 bool gen_var_descs_for_pointer_array(int i);
157 void report_coi_error(error_types msg, COIRESULT res);
158 _Offload_result translate_coi_error(COIRESULT res) const;
160 private:
161 typedef std::list<COIBUFFER> BufferList;
163 // extra data associated with each variable descriptor
164 struct VarExtra {
165 PtrData* src_data;
166 PtrData* dst_data;
167 AutoData* auto_data;
168 int64_t cpu_disp;
169 int64_t cpu_offset;
170 CeanReadRanges *read_rng_src;
171 CeanReadRanges *read_rng_dst;
172 int64_t ptr_arr_offset;
173 bool is_arr_ptr_el;
176 template<typename T> class ReadArrElements {
177 public:
178 ReadArrElements():
179 ranges(NULL),
180 el_size(sizeof(T)),
181 offset(0),
182 count(0),
183 is_empty(true),
184 base(NULL)
187 bool read_next(bool flag)
189 if (flag != 0) {
190 if (is_empty) {
191 if (ranges) {
192 if (!get_next_range(ranges, &offset)) {
193 // ranges are over
194 return false;
197 // all contiguous elements are over
198 else if (count != 0) {
199 return false;
202 length_cur = size;
204 else {
205 offset += el_size;
207 val = (T)get_el_value(base, offset, el_size);
208 length_cur -= el_size;
209 count++;
210 is_empty = length_cur == 0;
212 return true;
214 public:
215 CeanReadRanges * ranges;
216 T val;
217 int el_size;
218 int64_t size,
219 offset,
220 length_cur;
221 bool is_empty;
222 int count;
223 char *base;
226 // ptr_data for persistent auto objects
227 PtrData* m_stack_ptr_data;
228 PtrDataList m_destroy_stack;
230 // Engine
231 Engine& m_device;
233 // if true offload is mandatory
234 bool m_is_mandatory;
236 // if true offload has openmp origin
237 const bool m_is_openmp;
239 // The Marshaller for the inputs of the offloaded region.
240 Marshaller m_in;
242 // The Marshaller for the outputs of the offloaded region.
243 Marshaller m_out;
245 // List of buffers that are passed to dispatch call
246 BufferList m_compute_buffers;
248 // List of buffers that need to be destroyed at the end of offload
249 BufferList m_destroy_buffers;
251 // Variable descriptors
252 VarDesc* m_vars;
253 VarExtra* m_vars_extra;
254 int m_vars_total;
256 // Pointer to a user-specified status variable
257 _Offload_status *m_status;
259 // Function descriptor
260 FunctionDescriptor* m_func_desc;
261 uint32_t m_func_desc_size;
263 // Buffer for transferring copyin/copyout data
264 COIBUFFER m_inout_buf;
266 // Dependencies
267 COIEVENT *m_in_deps;
268 uint32_t m_in_deps_total;
269 COIEVENT *m_out_deps;
270 uint32_t m_out_deps_total;
272 // Timer data
273 OffloadHostTimerData *m_timer_data;
275 // copyin/copyout data length
276 uint64_t m_in_datalen;
277 uint64_t m_out_datalen;
279 // a boolean value calculated in setup_descriptors. If true we need to do
280 // a run function on the target. Otherwise it may be optimized away.
281 bool m_need_runfunction;
284 // Initialization types for MIC
285 enum OffloadInitType {
286 c_init_on_start, // all devices before entering main
287 c_init_on_offload, // single device before starting the first offload
288 c_init_on_offload_all // all devices before starting the first offload
291 // Initializes library and registers specified offload image.
292 extern "C" void __offload_register_image(const void* image);
293 extern "C" void __offload_unregister_image(const void* image);
295 // Initializes offload runtime library.
296 extern int __offload_init_library(void);
298 // thread data for associating pipelines with threads
299 extern pthread_key_t mic_thread_key;
301 // Environment variables for devices
302 extern MicEnvVar mic_env_vars;
304 // CPU frequency
305 extern uint64_t cpu_frequency;
307 // LD_LIBRARY_PATH for MIC libraries
308 extern char* mic_library_path;
310 // stack size for target
311 extern uint32_t mic_stack_size;
313 // Preallocated memory size for buffers on MIC
314 extern uint64_t mic_buffer_size;
316 // Setting controlling inout proxy
317 extern bool mic_proxy_io;
318 extern char* mic_proxy_fs_root;
320 // Threshold for creating buffers with large pages
321 extern uint64_t __offload_use_2mb_buffers;
323 // offload initialization type
324 extern OffloadInitType __offload_init_type;
326 // Device number to offload to when device is not explicitly specified.
327 extern int __omp_device_num;
329 // target executable
330 extern TargetImage* __target_exe;
332 // IDB support
334 // Called by the offload runtime after initialization of offload infrastructure
335 // has been completed.
336 extern "C" void __dbg_target_so_loaded();
338 // Called by the offload runtime when the offload infrastructure is about to be
339 // shut down, currently at application exit.
340 extern "C" void __dbg_target_so_unloaded();
342 // Null-terminated string containing path to the process image of the hosting
343 // application (offload_main)
344 #define MAX_TARGET_NAME 512
345 extern "C" char __dbg_target_exe_name[MAX_TARGET_NAME];
347 // Integer specifying the process id
348 extern "C" pid_t __dbg_target_so_pid;
350 // Integer specifying the 0-based device number
351 extern "C" int __dbg_target_id;
353 // Set to non-zero by the host-side debugger to enable offload debugging
354 // support
355 extern "C" int __dbg_is_attached;
357 // Major version of the debugger support API
358 extern "C" const int __dbg_api_major_version;
360 // Minor version of the debugger support API
361 extern "C" const int __dbg_api_minor_version;
363 #endif // OFFLOAD_HOST_H_INCLUDED