variable_size.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2017 Université de Bordeaux
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <config.h>
  17. #include <starpu.h>
  18. #include "../helper.h"
  19. /*
  20. * This is a dumb test for variable size
  21. * We defined a dumb interface for data whose size increase over kernel execution
  22. */
  23. #define FULLSIZE (5*1024*1024)
  24. #define INCREASE 0.80
  25. #ifdef STARPU_QUICK_CHECK
  26. #define N 5
  27. #define LIMIT "60"
  28. #else
  29. #define N 20
  30. #define LIMIT "1000"
  31. #endif
  32. /* Define the interface */
  33. #if !defined(STARPU_HAVE_SETENV)
  34. #warning setenv is not defined. Skipping test
  35. int main(int argc, char **argv)
  36. {
  37. return STARPU_TEST_SKIPPED;
  38. }
  39. #else
  40. struct variable_size_interface
  41. {
  42. enum starpu_data_interface_id id;
  43. uintptr_t ptr;
  44. size_t size;
  45. /* Coordinates of the represented object, to model growth */
  46. unsigned x, y;
  47. };
  48. static struct starpu_data_interface_ops starpu_interface_variable_size_ops;
  49. static void register_variable_size(starpu_data_handle_t handle, unsigned home_node, void *data_interface)
  50. {
  51. struct variable_size_interface *variable_size_interface = data_interface;
  52. unsigned node;
  53. for (node = 0; node < STARPU_MAXNODES; node++)
  54. {
  55. struct variable_size_interface *local_interface =
  56. starpu_data_get_interface_on_node(handle, node);
  57. if (node == home_node)
  58. local_interface->ptr = variable_size_interface->ptr;
  59. local_interface->id = variable_size_interface->id;
  60. local_interface->size = variable_size_interface->size;
  61. local_interface->x = variable_size_interface->x;
  62. local_interface->y = variable_size_interface->y;
  63. }
  64. }
  65. void variable_size_data_register(starpu_data_handle_t *handleptr, unsigned x, unsigned y)
  66. {
  67. if (starpu_interface_variable_size_ops.interfaceid == STARPU_UNKNOWN_INTERFACE_ID)
  68. {
  69. starpu_interface_variable_size_ops.interfaceid = starpu_data_interface_get_next_id();
  70. }
  71. struct variable_size_interface interface =
  72. {
  73. .id = starpu_interface_variable_size_ops.interfaceid,
  74. .x = x,
  75. .y = y,
  76. };
  77. /* Simulate that tiles close to the diagonal are more dense */
  78. interface.size = FULLSIZE * (starpu_lrand48() % 1024 + 1024) / 2048. * (N-sqrt(abs(x-y)*N)) / N;
  79. /* Round to page size */
  80. interface.size -= interface.size & (4096-1);
  81. starpu_data_register(handleptr, -1, &interface, &starpu_interface_variable_size_ops);
  82. }
  83. static size_t variable_size_get_size(starpu_data_handle_t handle STARPU_ATTRIBUTE_UNUSED)
  84. {
  85. struct variable_size_interface *interface =
  86. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  87. return interface->size;
  88. }
  89. static uint32_t variable_size_footprint(starpu_data_handle_t handle)
  90. {
  91. return starpu_hash_crc32c_be(variable_size_get_size(handle), 0);
  92. }
  93. static int variable_size_compare(void *data_interface_a, void *data_interface_b)
  94. {
  95. struct variable_size_interface *variable_a = data_interface_a;
  96. struct variable_size_interface *variable_b = data_interface_b;
  97. /* Two variables are considered compatible if they have the same size */
  98. return (variable_a->size == variable_b->size);
  99. }
  100. static void display_variable_size(starpu_data_handle_t handle, FILE *f)
  101. {
  102. struct variable_size_interface *variable_interface =
  103. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  104. fprintf(f, "%lu\t", (unsigned long) variable_interface->size);
  105. }
  106. static starpu_ssize_t describe_variable_size(void *data_interface, char *buf, size_t size)
  107. {
  108. struct variable_size_interface *variable_interface = data_interface;
  109. return snprintf(buf, size, "vv%lu\t", (unsigned long) variable_interface->size);
  110. }
  111. /* returns the size of the allocated area */
  112. static starpu_ssize_t allocate_variable_size_on_node(void *data_interface,
  113. unsigned dst_node)
  114. {
  115. struct variable_size_interface *variable_interface = data_interface;
  116. variable_interface->ptr = starpu_malloc_on_node_flags(dst_node, variable_interface->size, STARPU_MALLOC_PINNED | STARPU_MALLOC_COUNT | STARPU_MEMORY_OVERFLOW);
  117. STARPU_ASSERT(variable_interface->ptr);
  118. return 0;
  119. }
  120. static void free_variable_size_on_node(void *data_interface,
  121. unsigned node)
  122. {
  123. struct variable_size_interface *variable_interface = data_interface;
  124. starpu_free_on_node(node, variable_interface->ptr, variable_interface->size);
  125. }
  126. static int variable_size_copy(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, void *async_data)
  127. {
  128. struct variable_size_interface *src = src_interface;
  129. struct variable_size_interface *dst = dst_interface;
  130. return starpu_interface_copy(src->ptr, 0, src_node,
  131. dst->ptr, 0, dst_node,
  132. src->size, async_data);
  133. }
  134. static const struct starpu_data_copy_methods variable_size_copy_data_methods =
  135. {
  136. .any_to_any = variable_size_copy,
  137. };
  138. static struct starpu_data_interface_ops starpu_interface_variable_size_ops =
  139. {
  140. .register_data_handle = register_variable_size,
  141. .allocate_data_on_node = allocate_variable_size_on_node,
  142. .free_data_on_node = free_variable_size_on_node,
  143. .copy_methods = &variable_size_copy_data_methods,
  144. .get_size = variable_size_get_size,
  145. .footprint = variable_size_footprint,
  146. .compare = variable_size_compare,
  147. .interfaceid = STARPU_UNKNOWN_INTERFACE_ID,
  148. .interface_size = sizeof(struct variable_size_interface),
  149. .display = display_variable_size,
  150. .pack_data = NULL,
  151. .unpack_data = NULL,
  152. .describe = describe_variable_size,
  153. };
  154. static void kernel(void *descr[], void *cl_arg)
  155. {
  156. struct variable_size_interface *variable_interface = descr[0];
  157. unsigned workerid = starpu_worker_get_id_check();
  158. uintptr_t old = variable_interface->ptr;
  159. unsigned dst_node = starpu_worker_get_memory_node(workerid);
  160. /* Simulate that tiles close to the diagonal fill up faster */
  161. size_t increase = (FULLSIZE - variable_interface->size) * (starpu_lrand48() % 1024 + 1024) / 2048. * INCREASE;
  162. /* Round to page size */
  163. increase -= increase & (4096-1);
  164. variable_interface->ptr = starpu_malloc_on_node_flags(dst_node, variable_interface->size + increase, STARPU_MALLOC_PINNED | STARPU_MALLOC_COUNT | STARPU_MEMORY_OVERFLOW);
  165. STARPU_ASSERT(variable_interface->ptr);
  166. /* fprintf(stderr,"increase from %lu by %lu\n", variable_interface->size, increase); */
  167. starpu_free_on_node_flags(dst_node, old, variable_interface->size, STARPU_MALLOC_PINNED | STARPU_MALLOC_COUNT | STARPU_MEMORY_OVERFLOW);
  168. variable_interface->size += increase;
  169. starpu_sleep(0.010);
  170. }
  171. static double cost_function(struct starpu_task *t, struct starpu_perfmodel_arch *a, unsigned i)
  172. {
  173. return 10000;
  174. }
  175. static struct starpu_perfmodel perf_model =
  176. {
  177. .type = STARPU_PER_ARCH,
  178. .arch_cost_function = cost_function,
  179. };
  180. static struct starpu_codelet cl =
  181. {
  182. .cpu_funcs = {kernel},
  183. /* dynamic size doesn't work on MIC */
  184. /*.cpu_funcs_name = {"kernel"},*/
  185. .nbuffers = 1,
  186. .modes = {STARPU_RW},
  187. .model = &perf_model,
  188. .flags = STARPU_CODELET_SIMGRID_EXECUTE,
  189. };
  190. static void nop(void *descr[], void *cl_arg)
  191. {
  192. }
  193. static double nop_cost_function(struct starpu_task *t, struct starpu_perfmodel_arch *a, unsigned i)
  194. {
  195. return 0.001;
  196. }
  197. static struct starpu_perfmodel nop_perf_model =
  198. {
  199. .type = STARPU_PER_ARCH,
  200. .arch_cost_function = nop_cost_function,
  201. };
  202. static struct starpu_codelet cl_init =
  203. {
  204. .cpu_funcs = {nop},
  205. /* dynamic size doesn't work on MIC */
  206. /*.cpu_funcs_name = {"kernel"},*/
  207. .nbuffers = 1,
  208. .modes = {STARPU_W},
  209. .model = &nop_perf_model,
  210. };
  211. int main(int argc, char **argv)
  212. {
  213. int ret;
  214. int i;
  215. int x, y;
  216. starpu_data_handle_t handles[N][N];
  217. setenv("STARPU_LIMIT_CPU_MEM", LIMIT, 1);
  218. setenv("STARPU_DISK_SWAP", "/tmp/starpu-variable_size", 0);
  219. setenv("STARPU_DISK_SWAP_SIZE", "100000", 1);
  220. #ifdef STARPU_LINUX_SYS
  221. setenv("STARPU_DISK_SWAP_BACKEND", "unistd_o_direct", 0);
  222. #else
  223. setenv("STARPU_DISK_SWAP_BACKEND", "unistd", 0);
  224. #endif
  225. ret = starpu_init(NULL);
  226. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  227. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  228. for (x = 0; x < N; x++)
  229. for (y = 0; y < N; y++)
  230. {
  231. variable_size_data_register(&handles[x][y], x, y);
  232. ret = starpu_task_insert(&cl_init, STARPU_W, handles[x][y], 0);
  233. if (ret == ENODEV)
  234. goto enodev;
  235. #ifdef STARPU_SIMGRID
  236. starpu_sleep(0.0005);
  237. #endif
  238. }
  239. starpu_task_wait_for_all();
  240. /* Cholesky-like accesses */
  241. for (i = 0; i < 100; i++)
  242. for (x = i; x < N; x++)
  243. for (y = x; y < N; y++)
  244. starpu_task_insert(&cl, STARPU_RW, handles[x][y], STARPU_PRIORITY, (2*N-x-y), 0);
  245. starpu_task_wait_for_all();
  246. #if 0
  247. /* Look at the values */
  248. for (x = 0; x < N; x++)
  249. for (y = 0; y < N; y++)
  250. {
  251. starpu_data_acquire(handles[x][y], STARPU_R);
  252. starpu_data_release(handles[x][y]);
  253. }
  254. #endif
  255. for (x = 0; x < N; x++)
  256. for (y = 0; y < N; y++)
  257. starpu_data_unregister(handles[x][y]);
  258. starpu_shutdown();
  259. return EXIT_SUCCESS;
  260. enodev:
  261. for (x = 0; x < N; x++)
  262. for (y = 0; y < N; y++)
  263. starpu_data_unregister(handles[x][y]);
  264. fprintf(stderr, "WARNING: No one can execute this task\n");
  265. /* yes, we do not perform the computation but we did detect that no one
  266. * could perform the kernel, so this is not an error from StarPU */
  267. starpu_shutdown();
  268. return STARPU_TEST_SKIPPED;
  269. }
  270. #endif