hello_world_top.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 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 <starpu_top.h>
  32. #include <stdlib.h>
  33. #include <time.h>
  34. /* Example of enum param */
  35. char* names[] = {"Paul", "Jean", "Jaques", "Alain", "Brian"};
  36. int names_len = 5;
  37. int name_selected=2; //must be between 0 and names_len-1
  38. /* Exemple of int param */
  39. int number_of_addition = 30;
  40. /* Exemple of bool param */
  41. int stop_after_5_task = 0;
  42. /* When the task is done, task->callback_func(task->callback_arg) is called. Any
  43. * callback function must have the prototype void (*)(void *).
  44. * NB: Callback are NOT allowed to perform potentially blocking operations */
  45. void callback_func(void *callback_arg)
  46. {
  47. printf("Callback function got argument %p\n", callback_arg);
  48. }
  49. /* Every implementation of a codelet must have this prototype, the first
  50. * argument (buffers) describes the buffers/streams that are managed by the
  51. * DSM; the second arguments references read-only data that is passed as an
  52. * argument of the codelet (task->cl_arg). Here, "buffers" is unused as there
  53. * are no data input/output managed by the DSM (cl.nbuffers = 0) */
  54. struct params {
  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(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(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. 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_func = cpu_func,
  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. srand ( time(NULL) );
  100. /* initialize StarPU : passing a NULL argument means that we use
  101. * default configuration for the scheduling policies and the number of
  102. * processors/accelerators */
  103. starpu_init(NULL);
  104. /*init starpu_top*/
  105. starpu_top_data * loop_count =
  106. starpu_top_add_data_integer("Loop count", 0,124,1);
  107. starpu_top_data * remain_count =
  108. starpu_top_add_data_integer("Remaining loop", 0,124,1);
  109. starpu_top_data * midle_reach =
  110. starpu_top_add_data_boolean("Midle reached", 1);
  111. starpu_top_param* name =
  112. starpu_top_register_parameter_enum("Your name : ",
  113. &name_selected,
  114. names,
  115. names_len,
  116. callback_name_changed);
  117. starpu_top_param * number_of_addition_param =
  118. starpu_top_register_parameter_integer("Number of Millions of addition",
  119. &number_of_addition,
  120. 0,
  121. 50,
  122. callback_number_addition_changed);
  123. STARPU_ASSERT(number_of_addition_param != NULL);
  124. starpu_top_param * stop5_param =
  125. starpu_top_register_parameter_boolean("Stop after 5 task ?",
  126. &stop_after_5_task,
  127. NULL);
  128. STARPU_ASSERT(stop5_param != NULL);
  129. //all parameters are initialized, we can connect to UI
  130. starpu_top_init_and_wait("Serveur de test HelloWorld");
  131. //set "default value"
  132. starpu_top_update_data_boolean(midle_reach, 0);
  133. /* create a new task that is non-blocking by default : the task is not
  134. * submitted to the scheduler until the starpu_task_submit function is
  135. * called */
  136. /*
  137. * For this simple example, we make 124 iter
  138. */
  139. struct starpu_task *task[124];
  140. int i;
  141. for(i=0; i<124; i++)
  142. {
  143. starpu_top_update_data_integer(loop_count, i);
  144. starpu_top_update_data_integer(remain_count, 124-i);
  145. if(i==62)
  146. {
  147. starpu_top_update_data_boolean(midle_reach, 1);
  148. }
  149. if(i==25)
  150. {
  151. //changing name
  152. name_selected = 1;
  153. starpu_top_update_parameter(name);
  154. }
  155. if(i>4 && stop_after_5_task)
  156. {
  157. break;
  158. }
  159. task[i]=starpu_task_create();
  160. /* the task uses codelet "cl" */
  161. task[i]->cl = &cl;
  162. /* It is possible to pass buffers that are not managed by the DSM to the
  163. * kernels: the second argument of the "cpu_func" function is a pointer to a
  164. * buffer that contains information for the codelet (cl_arg stands for
  165. * codelet argument). In the case of accelerators, it is possible that
  166. * the codelet is given a pointer to a copy of that buffer: this buffer
  167. * is read-only so that any modification is not passed to other copies
  168. * of the buffer. For this reason, a buffer passed as a codelet
  169. * argument (cl_arg) is NOT a valid synchronization medium! */
  170. struct params params = { i, 2.0f };
  171. task[i]->cl_arg = &params;
  172. task[i]->cl_arg_size = sizeof(params);
  173. /* once the task has been executed, callback_func(0x42)
  174. * will be called on a CPU */
  175. task[i]->callback_func = callback_func;
  176. task[i]->callback_arg = (void*) (uintptr_t) 0x42;
  177. /* starpu_task_submit will be a blocking call */
  178. task[i]->synchronous = 1;
  179. /* submit the task to StarPU */
  180. if(number_of_addition==42)
  181. starpu_top_debug_lock("debug stop point because of 42 !");
  182. starpu_task_submit(task[i]);
  183. }
  184. /* terminate StarPU: statistics and other debug outputs are not
  185. * guaranteed to be generated unless this function is called. Once it
  186. * is called, it is not possible to submit tasks anymore, and the user
  187. * is responsible for making sure all tasks have already been executed:
  188. * calling starpu_shutdown() before the termination of all the tasks
  189. * results in an undefined behaviour */
  190. starpu_shutdown();
  191. return 0;
  192. }