oacc-host.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /* OpenACC Runtime Library: acc_device_host.
  2. Copyright (C) 2013-2022 Free Software Foundation, Inc.
  3. Contributed by Mentor Embedded.
  4. This file is part of the GNU Offloading and Multi Processing Library
  5. (libgomp).
  6. Libgomp is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3, or (at your option)
  9. any later version.
  10. Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. more details.
  14. Under Section 7 of GPL version 3, you are granted additional
  15. permissions described in the GCC Runtime Library Exception, version
  16. 3.1, as published by the Free Software Foundation.
  17. You should have received a copy of the GNU General Public License and
  18. a copy of the GCC Runtime Library Exception along with this program;
  19. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. <http://www.gnu.org/licenses/>. */
  21. #include "libgomp.h"
  22. #include "oacc-int.h"
  23. #include "gomp-constants.h"
  24. #include <stdbool.h>
  25. #include <stddef.h>
  26. static struct gomp_device_descr host_dispatch;
  27. static const char *
  28. host_get_name (void)
  29. {
  30. return host_dispatch.name;
  31. }
  32. static unsigned int
  33. host_get_caps (void)
  34. {
  35. return host_dispatch.capabilities;
  36. }
  37. static int
  38. host_get_type (void)
  39. {
  40. return host_dispatch.type;
  41. }
  42. static int
  43. host_get_num_devices (void)
  44. {
  45. return 1;
  46. }
  47. static bool
  48. host_init_device (int n __attribute__ ((unused)))
  49. {
  50. return true;
  51. }
  52. static bool
  53. host_fini_device (int n __attribute__ ((unused)))
  54. {
  55. return true;
  56. }
  57. static unsigned
  58. host_version (void)
  59. {
  60. return GOMP_VERSION;
  61. }
  62. static int
  63. host_load_image (int n __attribute__ ((unused)),
  64. unsigned v __attribute__ ((unused)),
  65. const void *t __attribute__ ((unused)),
  66. struct addr_pair **r __attribute__ ((unused)))
  67. {
  68. return 0;
  69. }
  70. static bool
  71. host_unload_image (int n __attribute__ ((unused)),
  72. unsigned v __attribute__ ((unused)),
  73. const void *t __attribute__ ((unused)))
  74. {
  75. return true;
  76. }
  77. static void *
  78. host_alloc (int n __attribute__ ((unused)), size_t s)
  79. {
  80. return gomp_malloc (s);
  81. }
  82. static bool
  83. host_free (int n __attribute__ ((unused)), void *p)
  84. {
  85. free (p);
  86. return true;
  87. }
  88. static bool
  89. host_dev2host (int n __attribute__ ((unused)),
  90. void *h __attribute__ ((unused)),
  91. const void *d __attribute__ ((unused)),
  92. size_t s __attribute__ ((unused)))
  93. {
  94. return true;
  95. }
  96. static bool
  97. host_host2dev (int n __attribute__ ((unused)),
  98. void *d __attribute__ ((unused)),
  99. const void *h __attribute__ ((unused)),
  100. size_t s __attribute__ ((unused)))
  101. {
  102. return true;
  103. }
  104. static void
  105. host_run (int n __attribute__ ((unused)), void *fn_ptr, void *vars,
  106. void **args __attribute__((unused)))
  107. {
  108. void (*fn)(void *) = (void (*)(void *)) fn_ptr;
  109. fn (vars);
  110. }
  111. static void
  112. host_openacc_exec (void (*fn) (void *),
  113. size_t mapnum __attribute__ ((unused)),
  114. void **hostaddrs,
  115. void **devaddrs __attribute__ ((unused)),
  116. unsigned *dims __attribute__ ((unused)),
  117. void *targ_mem_desc __attribute__ ((unused)))
  118. {
  119. fn (hostaddrs);
  120. }
  121. static void
  122. host_openacc_async_exec (void (*fn) (void *),
  123. size_t mapnum __attribute__ ((unused)),
  124. void **hostaddrs,
  125. void **devaddrs __attribute__ ((unused)),
  126. unsigned *dims __attribute__ ((unused)),
  127. void *targ_mem_desc __attribute__ ((unused)),
  128. struct goacc_asyncqueue *aq __attribute__ ((unused)))
  129. {
  130. fn (hostaddrs);
  131. }
  132. static int
  133. host_openacc_async_test (struct goacc_asyncqueue *aq __attribute__ ((unused)))
  134. {
  135. return 1;
  136. }
  137. static bool
  138. host_openacc_async_synchronize (struct goacc_asyncqueue *aq
  139. __attribute__ ((unused)))
  140. {
  141. return true;
  142. }
  143. static bool
  144. host_openacc_async_serialize (struct goacc_asyncqueue *aq1
  145. __attribute__ ((unused)),
  146. struct goacc_asyncqueue *aq2
  147. __attribute__ ((unused)))
  148. {
  149. return true;
  150. }
  151. static bool
  152. host_openacc_async_host2dev (int ord __attribute__ ((unused)),
  153. void *dst __attribute__ ((unused)),
  154. const void *src __attribute__ ((unused)),
  155. size_t n __attribute__ ((unused)),
  156. struct goacc_asyncqueue *aq
  157. __attribute__ ((unused)))
  158. {
  159. return true;
  160. }
  161. static bool
  162. host_openacc_async_dev2host (int ord __attribute__ ((unused)),
  163. void *dst __attribute__ ((unused)),
  164. const void *src __attribute__ ((unused)),
  165. size_t n __attribute__ ((unused)),
  166. struct goacc_asyncqueue *aq
  167. __attribute__ ((unused)))
  168. {
  169. return true;
  170. }
  171. static void
  172. host_openacc_async_queue_callback (struct goacc_asyncqueue *aq
  173. __attribute__ ((unused)),
  174. void (*callback_fn)(void *)
  175. __attribute__ ((unused)),
  176. void *userptr __attribute__ ((unused)))
  177. {
  178. }
  179. static struct goacc_asyncqueue *
  180. host_openacc_async_construct (int device __attribute__((unused)))
  181. {
  182. /* Non-NULL 0xffff... value as opaque dummy. */
  183. return (struct goacc_asyncqueue *) -1;
  184. }
  185. static bool
  186. host_openacc_async_destruct (struct goacc_asyncqueue *aq
  187. __attribute__ ((unused)))
  188. {
  189. return true;
  190. }
  191. static union goacc_property_value
  192. host_openacc_get_property (int n, enum goacc_property prop)
  193. {
  194. union goacc_property_value nullval = { .val = 0 };
  195. if (n >= host_get_num_devices ())
  196. return nullval;
  197. switch (prop)
  198. {
  199. case GOACC_PROPERTY_NAME:
  200. return (union goacc_property_value) { .ptr = "GOMP" };
  201. case GOACC_PROPERTY_VENDOR:
  202. return (union goacc_property_value) { .ptr = "GNU" };
  203. case GOACC_PROPERTY_DRIVER:
  204. return (union goacc_property_value) { .ptr = VERSION };
  205. case GOACC_PROPERTY_MEMORY:
  206. case GOACC_PROPERTY_FREE_MEMORY:
  207. default:
  208. return nullval;
  209. }
  210. }
  211. static void *
  212. host_openacc_create_thread_data (int ord __attribute__ ((unused)))
  213. {
  214. return NULL;
  215. }
  216. static void
  217. host_openacc_destroy_thread_data (void *tls_data __attribute__ ((unused)))
  218. {
  219. }
  220. static struct gomp_device_descr host_dispatch =
  221. {
  222. .name = "host",
  223. .capabilities = (GOMP_OFFLOAD_CAP_SHARED_MEM
  224. | GOMP_OFFLOAD_CAP_NATIVE_EXEC
  225. | GOMP_OFFLOAD_CAP_OPENACC_200),
  226. .target_id = 0,
  227. .type = OFFLOAD_TARGET_TYPE_HOST,
  228. .get_name_func = host_get_name,
  229. .get_caps_func = host_get_caps,
  230. .get_type_func = host_get_type,
  231. .get_num_devices_func = host_get_num_devices,
  232. .init_device_func = host_init_device,
  233. .fini_device_func = host_fini_device,
  234. .version_func = host_version,
  235. .load_image_func = host_load_image,
  236. .unload_image_func = host_unload_image,
  237. .alloc_func = host_alloc,
  238. .free_func = host_free,
  239. .dev2host_func = host_dev2host,
  240. .host2dev_func = host_host2dev,
  241. .run_func = host_run,
  242. .mem_map = { NULL },
  243. /* .lock initialized in goacc_host_init. */
  244. .state = GOMP_DEVICE_UNINITIALIZED,
  245. .openacc = {
  246. .exec_func = host_openacc_exec,
  247. .create_thread_data_func = host_openacc_create_thread_data,
  248. .destroy_thread_data_func = host_openacc_destroy_thread_data,
  249. .async = {
  250. .construct_func = host_openacc_async_construct,
  251. .destruct_func = host_openacc_async_destruct,
  252. .test_func = host_openacc_async_test,
  253. .synchronize_func = host_openacc_async_synchronize,
  254. .serialize_func = host_openacc_async_serialize,
  255. .queue_callback_func = host_openacc_async_queue_callback,
  256. .exec_func = host_openacc_async_exec,
  257. .dev2host_func = host_openacc_async_dev2host,
  258. .host2dev_func = host_openacc_async_host2dev,
  259. },
  260. .get_property_func = host_openacc_get_property,
  261. .cuda = {
  262. .get_current_device_func = NULL,
  263. .get_current_context_func = NULL,
  264. .get_stream_func = NULL,
  265. .set_stream_func = NULL,
  266. }
  267. }
  268. };
  269. /* Initialize and register this device type. */
  270. void
  271. goacc_host_init (void)
  272. {
  273. gomp_mutex_init (&host_dispatch.lock);
  274. goacc_register (&host_dispatch);
  275. }