variable_size.c 10 KB

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