hello_world_top.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. /*
  18. * This examples demonstrates how to construct and submit a task to StarPU and
  19. * more precisely:
  20. * - how to allocate a new task structure (starpu_task_create)
  21. * - how to describe a multi-versionned computational kernel (ie. a codelet)
  22. * - how to pass an argument to the codelet (task->cl_arg)
  23. * - how to declare a callback function that is called once the task has been
  24. * executed
  25. * - how to specify if starpu_task_submit is a blocking or non-blocking
  26. * operation (task->synchronous)
  27. */
  28. #include <stdio.h>
  29. #include <stdint.h>
  30. #include <starpu.h>
  31. #include <stdlib.h>
  32. #include <time.h>
  33. /* Example of enum param */
  34. char* names[] = {"Paul", "Jean", "Jaques", "Alain", "Brian"};
  35. int names_len = 5;
  36. int name_selected=2; //must be between 0 and names_len-1
  37. /* Exemple of int param */
  38. int number_of_addition = 30;
  39. /* Exemple of bool param */
  40. int stop_after_5_task = 0;
  41. /* When the task is done, task->callback_func(task->callback_arg) is called. Any
  42. * callback function must have the prototype void (*)(void *).
  43. * NB: Callback are NOT allowed to perform potentially blocking operations */
  44. void callback_func(void *callback_arg)
  45. {
  46. printf("Callback function got argument %p\n", callback_arg);
  47. }
  48. /* Every implementation of a codelet must have this prototype, the first
  49. * argument (buffers) describes the buffers/streams that are managed by the
  50. * DSM; the second arguments references read-only data that is passed as an
  51. * argument of the codelet (task->cl_arg). Here, "buffers" is unused as there
  52. * are no data input/output managed by the DSM (cl.nbuffers = 0) */
  53. struct params
  54. {
  55. int i;
  56. float f;
  57. };
  58. void cpu_func(void *buffers[], void *cl_arg)
  59. {
  60. struct params *params = (struct params *) cl_arg;
  61. //loosing time for top example...
  62. int sum = 0;
  63. int i = 0;
  64. while(i<number_of_addition*1000000)
  65. {
  66. sum+=rand();
  67. i++;
  68. }
  69. printf("Hello %s (params = {%i, %f} ) sum=%d\n",
  70. names[name_selected],
  71. params->i,
  72. params->f,
  73. sum);
  74. }
  75. void callback_name_changed(struct starpu_top_param* param)
  76. {
  77. char* message = (char *) malloc(256);
  78. sprintf(message, "Name have been changed to %s", names[name_selected]);
  79. starpu_top_debug_log(message);
  80. }
  81. void callback_number_addition_changed(struct starpu_top_param* param)
  82. {
  83. char* message = (char *) malloc(256);
  84. sprintf(message, "Number of addition is now %d", number_of_addition);
  85. starpu_top_debug_log(message);
  86. }
  87. struct starpu_codelet cl =
  88. {
  89. /* this codelet may only be executed on a CPU, and its cpu
  90. * implementation is function "cpu_func" */
  91. .where = STARPU_CPU,
  92. .cpu_funcs = {cpu_func, NULL},
  93. /* the codelet does not manipulate any data that is managed
  94. * by our DSM */
  95. .nbuffers = 0
  96. };
  97. int main(int argc, char **argv)
  98. {
  99. int ret;
  100. srand ( time(NULL) );
  101. /* initialize StarPU : passing a NULL argument means that we use
  102. * default configuration for the scheduling policies and the number of
  103. * processors/accelerators */
  104. ret = starpu_init(NULL);
  105. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  106. /*init starpu_top*/
  107. struct starpu_top_data * loop_count =
  108. starpu_top_add_data_integer("Loop count", 0,124,1);
  109. struct starpu_top_data * remain_count =
  110. starpu_top_add_data_integer("Remaining loop", 0,124,1);
  111. struct starpu_top_data * midle_reach =
  112. starpu_top_add_data_boolean("Midle reached", 1);
  113. struct starpu_top_param* name =
  114. starpu_top_register_parameter_enum("Your name : ",
  115. &name_selected,
  116. names,
  117. names_len,
  118. callback_name_changed);
  119. struct starpu_top_param * number_of_addition_param =
  120. starpu_top_register_parameter_integer("Number of Millions of addition",
  121. &number_of_addition,
  122. 0,
  123. 50,
  124. callback_number_addition_changed);
  125. STARPU_ASSERT(number_of_addition_param != NULL);
  126. struct starpu_top_param * stop5_param =
  127. starpu_top_register_parameter_boolean("Stop after 5 task ?",
  128. &stop_after_5_task,
  129. NULL);
  130. STARPU_ASSERT(stop5_param != NULL);
  131. //all parameters are initialized, we can connect to UI
  132. starpu_top_init_and_wait("Serveur de test HelloWorld");
  133. //set "default value"
  134. starpu_top_update_data_boolean(midle_reach, 0);
  135. /* create a new task that is non-blocking by default : the task is not
  136. * submitted to the scheduler until the starpu_task_submit function is
  137. * called */
  138. /*
  139. * For this simple example, we make 124 iter
  140. */
  141. struct starpu_task *task[124];
  142. int i;
  143. for(i=0; i<124; i++)
  144. {
  145. starpu_top_update_data_integer(loop_count, i);
  146. starpu_top_update_data_integer(remain_count, 124-i);
  147. if(i==62)
  148. {
  149. starpu_top_update_data_boolean(midle_reach, 1);
  150. }
  151. if(i==25)
  152. {
  153. //changing name
  154. name_selected = 1;
  155. starpu_top_update_parameter(name);
  156. }
  157. if(i>4 && stop_after_5_task)
  158. {
  159. break;
  160. }
  161. task[i]=starpu_task_create();
  162. /* the task uses codelet "cl" */
  163. task[i]->cl = &cl;
  164. /* It is possible to pass buffers that are not managed by the DSM to the
  165. * kernels: the second argument of the "cpu_func" function is a pointer to a
  166. * buffer that contains information for the codelet (cl_arg stands for
  167. * codelet argument). In the case of accelerators, it is possible that
  168. * the codelet is given a pointer to a copy of that buffer: this buffer
  169. * is read-only so that any modification is not passed to other copies
  170. * of the buffer. For this reason, a buffer passed as a codelet
  171. * argument (cl_arg) is NOT a valid synchronization medium! */
  172. struct params params = { i, 2.0f };
  173. task[i]->cl_arg = &params;
  174. task[i]->cl_arg_size = sizeof(params);
  175. /* once the task has been executed, callback_func(0x42)
  176. * will be called on a CPU */
  177. task[i]->callback_func = callback_func;
  178. task[i]->callback_arg = (void*) (uintptr_t) 0x42;
  179. /* starpu_task_submit will be a blocking call */
  180. task[i]->synchronous = 1;
  181. /* submit the task to StarPU */
  182. if(number_of_addition==42)
  183. starpu_top_debug_lock("debug stop point because of 42 !");
  184. ret = starpu_task_submit(task[i]);
  185. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  186. }
  187. /* terminate StarPU: statistics and other debug outputs are not
  188. * guaranteed to be generated unless this function is called. Once it
  189. * is called, it is not possible to submit tasks anymore, and the user
  190. * is responsible for making sure all tasks have already been executed:
  191. * calling starpu_shutdown() before the termination of all the tasks
  192. * results in an undefined behaviour */
  193. starpu_shutdown();
  194. return 0;
  195. }