hello-world.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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_submit_task 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, call task->callback_func(task->callback_arg) */
  31. void callback_func(void *callback_arg)
  32. {
  33. printf("Callback function got argument %p\n", callback_arg);
  34. }
  35. /* Every implementation of a codelet must have this prototype, the first
  36. * argument (buffers) describes the buffers/streams that are managed by the
  37. * DSM; the second arguments references a read-only buffer that is passed as an
  38. * argument of the codelet (task->cl_arg). Here, "buffers" is unused as there
  39. * are no data input/output managed by the DSM (cl.nbuffers = 0) */
  40. void cpu_func(starpu_data_interface_t *buffers, void *func_arg)
  41. {
  42. float *array = func_arg;
  43. printf("Hello world (array = {%f, %f} )\n", array[0], array[1]);
  44. }
  45. starpu_codelet cl =
  46. {
  47. /* this codelet may only be executed on a CPU, and its cpu
  48. * implementation is function "cpu_func" */
  49. .where = CORE,
  50. .core_func = cpu_func,
  51. /* the codelet does not manipulate any data that is managed
  52. * by our DSM */
  53. .nbuffers = 0
  54. };
  55. int main(int argc, char **argv)
  56. {
  57. /* initialize StarPU : passing a NULL argument means that we use
  58. * default configuration for the scheduling policies and the number of
  59. * processors/accelerators */
  60. starpu_init(NULL);
  61. /* create a new task that is non-blocking by default : the task is not
  62. * submitted to the scheduler until the starpu_submit_task function is
  63. * called */
  64. struct starpu_task *task = starpu_task_create();
  65. /* the task implements codelet "cl" */
  66. task->cl = &cl;
  67. /* It is possible to use buffers that are not managed by the DSM to the
  68. * kernels: the second argument of the "cpu_func" function is a pointer to a
  69. * buffer that contains information for the codelet (cl_arg stands for
  70. * codelet argument). In the case of accelerators, it is possible that
  71. * the codelet is given a pointer to a copy of that buffer: this buffer
  72. * is read-only so that any modification is not passed to other copies
  73. * of the buffer. For this reason, a buffer passed as a codelet
  74. * argument (cl_arg) is NOT a valid synchronization medium ! */
  75. float array[2] = {1.0f, -1.0f};
  76. task->cl_arg = &array;
  77. task->cl_arg_size = 2*sizeof(float);
  78. /* once the task has been executed, callback_func(0x42)
  79. * will be called on a CPU */
  80. task->callback_func = callback_func;
  81. task->callback_arg = (void*) (uintptr_t) 0x42;
  82. /* starpu_submit_task will be a blocking call */
  83. task->synchronous = 1;
  84. /* submit the task to StarPU */
  85. starpu_submit_task(task);
  86. /* terminate StarPU: statistics and other debug outputs are not
  87. * guaranteed to be generated unless this function is called. Once it
  88. * is called, it is not possible to submit tasks anymore, and the user
  89. * is responsible for making sure all tasks have already been executed:
  90. * calling starpu_shutdown() before the termination of all the tasks
  91. * results in an undefined behaviour */
  92. starpu_shutdown();
  93. return 0;
  94. }