hello_world.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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. /*
  17. * This examples demonstrates how to construct and submit a task to StarPU and
  18. * more precisely:
  19. * - how to allocate a new task structure (starpu_task_create)
  20. * - how to describe a multi-versionned computational kernel (ie. a codelet)
  21. * - how to pass an argument to the codelet (task->cl_arg)
  22. * - how to declare a callback function that is called once the task has been
  23. * executed
  24. * - how to specify if starpu_task_submit is a blocking or non-blocking
  25. * operation (task->synchronous)
  26. */
  27. #include <stdio.h>
  28. #include <stdint.h>
  29. #include <starpu.h>
  30. /* When the task is done, task->callback_func(task->callback_arg) is called. Any
  31. * callback function must have the prototype void (*)(void *).
  32. * NB: Callback are NOT allowed to perform potentially blocking operations */
  33. void callback_func(void *callback_arg)
  34. {
  35. printf("Callback function got argument %p\n", callback_arg);
  36. }
  37. /* Every implementation of a codelet must have this prototype, the first
  38. * argument (buffers) describes the buffers/streams that are managed by the
  39. * DSM; the second arguments references a read-only buffer that is passed as an
  40. * argument of the codelet (task->cl_arg). Here, "buffers" is unused as there
  41. * are no data input/output managed by the DSM (cl.nbuffers = 0) */
  42. void cpu_func(void *buffers[], void *func_arg)
  43. {
  44. float *array = func_arg;
  45. printf("Hello world (array = {%f, %f} )\n", array[0], array[1]);
  46. }
  47. starpu_codelet cl =
  48. {
  49. /* this codelet may only be executed on a CPU, and its cpu
  50. * implementation is function "cpu_func" */
  51. .where = STARPU_CPU,
  52. .cpu_func = cpu_func,
  53. /* the codelet does not manipulate any data that is managed
  54. * by our DSM */
  55. .nbuffers = 0
  56. };
  57. int main(int argc, char **argv)
  58. {
  59. /* initialize StarPU : passing a NULL argument means that we use
  60. * default configuration for the scheduling policies and the number of
  61. * processors/accelerators */
  62. starpu_init(NULL);
  63. /* create a new task that is non-blocking by default : the task is not
  64. * submitted to the scheduler until the starpu_task_submit function is
  65. * called */
  66. struct starpu_task *task = starpu_task_create();
  67. /* the task uses codelet "cl" */
  68. task->cl = &cl;
  69. /* It is possible to use buffers that are not managed by the DSM to the
  70. * kernels: the second argument of the "cpu_func" function is a pointer to a
  71. * buffer that contains information for the codelet (cl_arg stands for
  72. * codelet argument). In the case of accelerators, it is possible that
  73. * the codelet is given a pointer to a copy of that buffer: this buffer
  74. * is read-only so that any modification is not passed to other copies
  75. * of the buffer. For this reason, a buffer passed as a codelet
  76. * argument (cl_arg) is NOT a valid synchronization medium! */
  77. float array[2] = {1.0f, -1.0f};
  78. task->cl_arg = &array;
  79. task->cl_arg_size = 2*sizeof(float);
  80. /* once the task has been executed, callback_func(0x42)
  81. * will be called on a CPU */
  82. task->callback_func = callback_func;
  83. task->callback_arg = (void*) (uintptr_t) 0x42;
  84. /* starpu_task_submit will be a blocking call */
  85. task->synchronous = 1;
  86. /* submit the task to StarPU */
  87. starpu_task_submit(task);
  88. /* terminate StarPU: statistics and other debug outputs are not
  89. * guaranteed to be generated unless this function is called. Once it
  90. * is called, it is not possible to submit tasks anymore, and the user
  91. * is responsible for making sure all tasks have already been executed:
  92. * calling starpu_shutdown() before the termination of all the tasks
  93. * results in an undefined behaviour */
  94. starpu_shutdown();
  95. return 0;
  96. }