offload_engine.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. Copyright (c) 2014 Intel Corporation. All Rights Reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of Intel Corporation nor the names of its
  12. contributors may be used to endorse or promote products derived
  13. from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifndef OFFLOAD_ENGINE_H_INCLUDED
  27. #define OFFLOAD_ENGINE_H_INCLUDED
  28. #include <limits.h>
  29. #include <list>
  30. #include <set>
  31. #include <map>
  32. #include "offload_common.h"
  33. #include "coi/coi_client.h"
  34. // Address range
  35. class MemRange {
  36. public:
  37. MemRange() : m_start(0), m_length(0) {}
  38. MemRange(const void *addr, uint64_t len) : m_start(addr), m_length(len) {}
  39. const void* start() const {
  40. return m_start;
  41. }
  42. const void* end() const {
  43. return static_cast<const char*>(m_start) + m_length;
  44. }
  45. uint64_t length() const {
  46. return m_length;
  47. }
  48. // returns true if given range overlaps with another one
  49. bool overlaps(const MemRange &o) const {
  50. // Two address ranges A[start, end) and B[start,end) overlap
  51. // if A.start < B.end and A.end > B.start.
  52. return start() < o.end() && end() > o.start();
  53. }
  54. // returns true if given range contains the other range
  55. bool contains(const MemRange &o) const {
  56. return start() <= o.start() && o.end() <= end();
  57. }
  58. private:
  59. const void* m_start;
  60. uint64_t m_length;
  61. };
  62. // Data associated with a pointer variable
  63. class PtrData {
  64. public:
  65. PtrData(const void *addr, uint64_t len) :
  66. cpu_addr(addr, len), cpu_buf(0),
  67. mic_addr(0), alloc_disp(0), mic_buf(0), mic_offset(0),
  68. ref_count(0), is_static(false)
  69. {}
  70. //
  71. // Copy constructor
  72. //
  73. PtrData(const PtrData& ptr):
  74. cpu_addr(ptr.cpu_addr), cpu_buf(ptr.cpu_buf),
  75. mic_addr(ptr.mic_addr), alloc_disp(ptr.alloc_disp),
  76. mic_buf(ptr.mic_buf), mic_offset(ptr.mic_offset),
  77. ref_count(ptr.ref_count), is_static(ptr.is_static)
  78. {}
  79. bool operator<(const PtrData &o) const {
  80. // Variables are sorted by the CPU start address.
  81. // Overlapping memory ranges are considered equal.
  82. return (cpu_addr.start() < o.cpu_addr.start()) &&
  83. !cpu_addr.overlaps(o.cpu_addr);
  84. }
  85. long add_reference() {
  86. if (is_static) {
  87. return LONG_MAX;
  88. }
  89. #ifndef TARGET_WINNT
  90. return __sync_fetch_and_add(&ref_count, 1);
  91. #else // TARGET_WINNT
  92. return _InterlockedIncrement(&ref_count) - 1;
  93. #endif // TARGET_WINNT
  94. }
  95. long remove_reference() {
  96. if (is_static) {
  97. return LONG_MAX;
  98. }
  99. #ifndef TARGET_WINNT
  100. return __sync_sub_and_fetch(&ref_count, 1);
  101. #else // TARGET_WINNT
  102. return _InterlockedDecrement(&ref_count);
  103. #endif // TARGET_WINNT
  104. }
  105. long get_reference() const {
  106. if (is_static) {
  107. return LONG_MAX;
  108. }
  109. return ref_count;
  110. }
  111. public:
  112. // CPU address range
  113. const MemRange cpu_addr;
  114. // CPU and MIC buffers
  115. COIBUFFER cpu_buf;
  116. COIBUFFER mic_buf;
  117. // placeholder for buffer address on mic
  118. uint64_t mic_addr;
  119. uint64_t alloc_disp;
  120. // additional offset to pointer data on MIC for improving bandwidth for
  121. // data which is not 4K aligned
  122. uint32_t mic_offset;
  123. // if true buffers are created from static memory
  124. bool is_static;
  125. mutex_t alloc_ptr_data_lock;
  126. private:
  127. // reference count for the entry
  128. long ref_count;
  129. };
  130. typedef std::list<PtrData*> PtrDataList;
  131. // Data associated with automatic variable
  132. class AutoData {
  133. public:
  134. AutoData(const void *addr, uint64_t len) :
  135. cpu_addr(addr, len), ref_count(0)
  136. {}
  137. bool operator<(const AutoData &o) const {
  138. // Variables are sorted by the CPU start address.
  139. // Overlapping memory ranges are considered equal.
  140. return (cpu_addr.start() < o.cpu_addr.start()) &&
  141. !cpu_addr.overlaps(o.cpu_addr);
  142. }
  143. long add_reference() {
  144. #ifndef TARGET_WINNT
  145. return __sync_fetch_and_add(&ref_count, 1);
  146. #else // TARGET_WINNT
  147. return _InterlockedIncrement(&ref_count) - 1;
  148. #endif // TARGET_WINNT
  149. }
  150. long remove_reference() {
  151. #ifndef TARGET_WINNT
  152. return __sync_sub_and_fetch(&ref_count, 1);
  153. #else // TARGET_WINNT
  154. return _InterlockedDecrement(&ref_count);
  155. #endif // TARGET_WINNT
  156. }
  157. long get_reference() const {
  158. return ref_count;
  159. }
  160. public:
  161. // CPU address range
  162. const MemRange cpu_addr;
  163. private:
  164. // reference count for the entry
  165. long ref_count;
  166. };
  167. // Set of autimatic variables
  168. typedef std::set<AutoData> AutoSet;
  169. // Target image data
  170. struct TargetImage
  171. {
  172. TargetImage(const char *_name, const void *_data, uint64_t _size,
  173. const char *_origin, uint64_t _offset) :
  174. name(_name), data(_data), size(_size),
  175. origin(_origin), offset(_offset)
  176. {}
  177. // library name
  178. const char* name;
  179. // contents and size
  180. const void* data;
  181. uint64_t size;
  182. // file of origin and offset within that file
  183. const char* origin;
  184. uint64_t offset;
  185. };
  186. typedef std::list<TargetImage> TargetImageList;
  187. // Data associated with persistent auto objects
  188. struct PersistData
  189. {
  190. PersistData(const void *addr, uint64_t routine_num, uint64_t size) :
  191. stack_cpu_addr(addr), routine_id(routine_num)
  192. {
  193. stack_ptr_data = new PtrData(0, size);
  194. }
  195. // 1-st key value - begining of the stack at CPU
  196. const void * stack_cpu_addr;
  197. // 2-nd key value - identifier of routine invocation at CPU
  198. uint64_t routine_id;
  199. // corresponded PtrData; only stack_ptr_data->mic_buf is used
  200. PtrData * stack_ptr_data;
  201. // used to get offset of the variable in stack buffer
  202. char * cpu_stack_addr;
  203. };
  204. typedef std::list<PersistData> PersistDataList;
  205. // class representing a single engine
  206. struct Engine {
  207. friend void __offload_init_library_once(void);
  208. friend void __offload_fini_library(void);
  209. #define check_result(res, tag, ...) \
  210. { \
  211. if (res == COI_PROCESS_DIED) { \
  212. fini_process(true); \
  213. exit(1); \
  214. } \
  215. if (res != COI_SUCCESS) { \
  216. __liboffload_error_support(tag, __VA_ARGS__); \
  217. exit(1); \
  218. } \
  219. }
  220. int get_logical_index() const {
  221. return m_index;
  222. }
  223. int get_physical_index() const {
  224. return m_physical_index;
  225. }
  226. const COIPROCESS& get_process() const {
  227. return m_process;
  228. }
  229. // initialize device
  230. void init(void);
  231. // add new library
  232. void add_lib(const TargetImage &lib)
  233. {
  234. m_lock.lock();
  235. m_ready = false;
  236. m_images.push_back(lib);
  237. m_lock.unlock();
  238. }
  239. COIRESULT compute(
  240. const std::list<COIBUFFER> &buffers,
  241. const void* data,
  242. uint16_t data_size,
  243. void* ret,
  244. uint16_t ret_size,
  245. uint32_t num_deps,
  246. const COIEVENT* deps,
  247. COIEVENT* event
  248. );
  249. #ifdef MYO_SUPPORT
  250. // temporary workaround for blocking behavior for myoiLibInit/Fini calls
  251. void init_myo(COIEVENT *event) {
  252. COIRESULT res;
  253. res = COI::PipelineRunFunction(get_pipeline(),
  254. m_funcs[c_func_myo_init],
  255. 0, 0, 0, 0, 0, 0, 0, 0, 0,
  256. event);
  257. check_result(res, c_pipeline_run_func, m_index, res);
  258. }
  259. void fini_myo(COIEVENT *event) {
  260. COIRESULT res;
  261. res = COI::PipelineRunFunction(get_pipeline(),
  262. m_funcs[c_func_myo_fini],
  263. 0, 0, 0, 0, 0, 0, 0, 0, 0,
  264. event);
  265. check_result(res, c_pipeline_run_func, m_index, res);
  266. }
  267. #endif // MYO_SUPPORT
  268. //
  269. // Memory association table
  270. //
  271. PtrData* find_ptr_data(const void *ptr) {
  272. m_ptr_lock.lock();
  273. PtrSet::iterator res = m_ptr_set.find(PtrData(ptr, 0));
  274. m_ptr_lock.unlock();
  275. if (res == m_ptr_set.end()) {
  276. return 0;
  277. }
  278. return const_cast<PtrData*>(res.operator->());
  279. }
  280. PtrData* insert_ptr_data(const void *ptr, uint64_t len, bool &is_new) {
  281. m_ptr_lock.lock();
  282. std::pair<PtrSet::iterator, bool> res =
  283. m_ptr_set.insert(PtrData(ptr, len));
  284. PtrData* ptr_data = const_cast<PtrData*>(res.first.operator->());
  285. m_ptr_lock.unlock();
  286. is_new = res.second;
  287. if (is_new) {
  288. // It's necessary to lock as soon as possible.
  289. // unlock must be done at call site of insert_ptr_data at
  290. // branch for is_new
  291. ptr_data->alloc_ptr_data_lock.lock();
  292. }
  293. return ptr_data;
  294. }
  295. void remove_ptr_data(const void *ptr) {
  296. m_ptr_lock.lock();
  297. m_ptr_set.erase(PtrData(ptr, 0));
  298. m_ptr_lock.unlock();
  299. }
  300. //
  301. // Automatic variables
  302. //
  303. AutoData* find_auto_data(const void *ptr) {
  304. AutoSet &auto_vars = get_auto_vars();
  305. AutoSet::iterator res = auto_vars.find(AutoData(ptr, 0));
  306. if (res == auto_vars.end()) {
  307. return 0;
  308. }
  309. return const_cast<AutoData*>(res.operator->());
  310. }
  311. AutoData* insert_auto_data(const void *ptr, uint64_t len) {
  312. AutoSet &auto_vars = get_auto_vars();
  313. std::pair<AutoSet::iterator, bool> res =
  314. auto_vars.insert(AutoData(ptr, len));
  315. return const_cast<AutoData*>(res.first.operator->());
  316. }
  317. void remove_auto_data(const void *ptr) {
  318. get_auto_vars().erase(AutoData(ptr, 0));
  319. }
  320. //
  321. // Signals
  322. //
  323. void add_signal(const void *signal, OffloadDescriptor *desc) {
  324. m_signal_lock.lock();
  325. m_signal_map[signal] = desc;
  326. m_signal_lock.unlock();
  327. }
  328. OffloadDescriptor* find_signal(const void *signal, bool remove) {
  329. OffloadDescriptor *desc = 0;
  330. m_signal_lock.lock();
  331. {
  332. SignalMap::iterator it = m_signal_map.find(signal);
  333. if (it != m_signal_map.end()) {
  334. desc = it->second;
  335. if (remove) {
  336. m_signal_map.erase(it);
  337. }
  338. }
  339. }
  340. m_signal_lock.unlock();
  341. return desc;
  342. }
  343. // stop device process
  344. void fini_process(bool verbose);
  345. // list of stacks active at the engine
  346. PersistDataList m_persist_list;
  347. private:
  348. Engine() : m_index(-1), m_physical_index(-1), m_process(0), m_ready(false),
  349. m_proc_number(0)
  350. {}
  351. ~Engine() {
  352. if (m_process != 0) {
  353. fini_process(false);
  354. }
  355. }
  356. // set indexes
  357. void set_indexes(int logical_index, int physical_index) {
  358. m_index = logical_index;
  359. m_physical_index = physical_index;
  360. }
  361. // start process on device
  362. void init_process();
  363. void load_libraries(void);
  364. void init_ptr_data(void);
  365. // performs library intialization on the device side
  366. pid_t init_device(void);
  367. private:
  368. // get pipeline associated with a calling thread
  369. COIPIPELINE get_pipeline(void);
  370. // get automatic vars set associated with the calling thread
  371. AutoSet& get_auto_vars(void);
  372. // destructor for thread data
  373. static void destroy_thread_data(void *data);
  374. private:
  375. typedef std::set<PtrData> PtrSet;
  376. typedef std::map<const void*, OffloadDescriptor*> SignalMap;
  377. // device indexes
  378. int m_index;
  379. int m_physical_index;
  380. // number of COI pipes created for the engine
  381. long m_proc_number;
  382. // process handle
  383. COIPROCESS m_process;
  384. // If false, device either has not been initialized or new libraries
  385. // have been added.
  386. bool m_ready;
  387. mutex_t m_lock;
  388. // List of libraries to be loaded
  389. TargetImageList m_images;
  390. // var table
  391. PtrSet m_ptr_set;
  392. mutex_t m_ptr_lock;
  393. // signals
  394. SignalMap m_signal_map;
  395. mutex_t m_signal_lock;
  396. // constants for accessing device function handles
  397. enum {
  398. c_func_compute = 0,
  399. #ifdef MYO_SUPPORT
  400. c_func_myo_init,
  401. c_func_myo_fini,
  402. #endif // MYO_SUPPORT
  403. c_func_init,
  404. c_func_var_table_size,
  405. c_func_var_table_copy,
  406. c_funcs_total
  407. };
  408. static const char* m_func_names[c_funcs_total];
  409. // device function handles
  410. COIFUNCTION m_funcs[c_funcs_total];
  411. // int -> name mapping for device signals
  412. static const int c_signal_max = 32;
  413. static const char* c_signal_names[c_signal_max];
  414. };
  415. #endif // OFFLOAD_ENGINE_H_INCLUDED