mlr.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2015 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013 CNRS
  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. */
  24. #include <stdio.h>
  25. #include <stdint.h>
  26. #include <starpu.h>
  27. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  28. /* When the task is done, task->callback_func(task->callback_arg) is called. Any
  29. * callback function must have the prototype void (*)(void *).
  30. * NB: Callback are NOT allowed to perform potentially blocking operations */
  31. void callback_func(void *callback_arg)
  32. {
  33. }
  34. /* Every implementation of a codelet must have this prototype, the first
  35. * argument (buffers) describes the buffers/streams that are managed by the
  36. * DSM; the second arguments references read-only data that is passed as an
  37. * argument of the codelet (task->cl_arg). Here, "buffers" is unused as there
  38. * are no data input/output managed by the DSM (cl.nbuffers = 0) */
  39. struct params
  40. {
  41. int m;
  42. int n;
  43. int k;
  44. };
  45. void cpu_func(void *buffers[], void *cl_arg)
  46. {
  47. struct params *params = (struct params *) cl_arg;
  48. for(int i=0; i< params->m * params->m * params->n; i++)
  49. printf("**i");
  50. for(int i=0; i< params->n * params->n * params->k; i++)
  51. printf("*i");
  52. }
  53. int main(int argc, char **argv)
  54. {
  55. struct starpu_codelet cl;
  56. struct starpu_task *task;
  57. struct params params = {1, 2, 3};
  58. int ret;
  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. ret = starpu_init(NULL);
  63. if (ret == -ENODEV)
  64. return 77;
  65. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  66. /* create a new task that is non-blocking by default : the task is not
  67. * submitted to the scheduler until the starpu_task_submit function is
  68. * called */
  69. task = starpu_task_create();
  70. starpu_codelet_init(&cl);
  71. /* this codelet may only be executed on a CPU, and its cpu
  72. * implementation is function "cpu_func" */
  73. cl.cpu_funcs[0] = cpu_func;
  74. cl.cpu_funcs_name[0] = "test_codelet";
  75. /* the codelet does not manipulate any data that is managed
  76. * by our DSM */
  77. cl.nbuffers = 0;
  78. cl.name="test";
  79. /* the task uses codelet "cl" */
  80. task->cl = &cl;
  81. /* It is possible to pass buffers that are not managed by the DSM to the
  82. * kernels: the second argument of the "cpu_func" function is a pointer to a
  83. * buffer that contains information for the codelet (cl_arg stands for
  84. * codelet argument). In the case of accelerators, it is possible that
  85. * the codelet is given a pointer to a copy of that buffer: this buffer
  86. * is read-only so that any modification is not passed to other copies
  87. * of the buffer. For this reason, a buffer passed as a codelet
  88. * argument (cl_arg) is NOT a valid synchronization medium! */
  89. task->cl_arg = &params;
  90. task->cl_arg_size = sizeof(params);
  91. /* submit the task to StarPU */
  92. ret = starpu_task_submit(task);
  93. if (ret == -ENODEV) goto enodev;
  94. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  95. /* terminate StarPU: statistics and other debug outputs are not
  96. * guaranteed to be generated unless this function is called. Once it
  97. * is called, it is not possible to submit tasks anymore, and the user
  98. * is responsible for making sure all tasks have already been executed:
  99. * calling starpu_shutdown() before the termination of all the tasks
  100. * results in an undefined behaviour */
  101. starpu_shutdown();
  102. return 0;
  103. enodev:
  104. starpu_shutdown();
  105. return 77;
  106. }