hello-world.c 3.5 KB

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