fmatrix.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011, 2012 Centre National de la Recherche Scientifique
  4. *
  5. * StarPU 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. * StarPU 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 <starpu.h>
  17. #define NX 5
  18. #define NY 4
  19. #define PARTS 2
  20. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  21. void cpu_func(void *buffers[], void *cl_arg)
  22. {
  23. unsigned i, j;
  24. int *factor = (int *) cl_arg;
  25. /* length of the matrix */
  26. unsigned nx = STARPU_MATRIX_GET_NX(buffers[0]);
  27. unsigned ny = STARPU_MATRIX_GET_NY(buffers[0]);
  28. unsigned ld = STARPU_MATRIX_GET_LD(buffers[0]);
  29. /* local copy of the matrix pointer */
  30. int *val = (int *)STARPU_MATRIX_GET_PTR(buffers[0]);
  31. for(j=0; j<ny ; j++)
  32. {
  33. for(i=0; i<nx ; i++)
  34. val[(j*ld)+i] = *factor;
  35. }
  36. }
  37. int main(int argc, char **argv)
  38. {
  39. unsigned i, j, n=1;
  40. int matrix[NX*NY];
  41. int ret;
  42. FPRINTF(stderr,"IN Matrix: \n");
  43. for(j=0 ; j<NY ; j++)
  44. {
  45. for(i=0 ; i<NX ; i++)
  46. {
  47. matrix[(j*NX)+i] = n++;
  48. FPRINTF(stderr, "%2d ", matrix[(j*NX)+i]);
  49. }
  50. FPRINTF(stderr,"\n");
  51. }
  52. FPRINTF(stderr,"\n");
  53. starpu_data_handle_t handle;
  54. struct starpu_codelet cl =
  55. {
  56. .where = STARPU_CPU,
  57. .cpu_funcs = {cpu_func, NULL},
  58. .nbuffers = 1,
  59. .modes = {STARPU_RW}
  60. };
  61. ret = starpu_init(NULL);
  62. if (ret == -ENODEV)
  63. return 77;
  64. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  65. /* Declare data to StarPU */
  66. starpu_matrix_data_register(&handle, 0, (uintptr_t)matrix, NX, NX, NY, sizeof(matrix[0]));
  67. /* Partition the matrix in PARTS sub-matrices */
  68. struct starpu_data_filter f =
  69. {
  70. .filter_func = starpu_block_filter_func,
  71. .nchildren = PARTS
  72. };
  73. starpu_data_partition(handle, &f);
  74. /* Submit a task on each sub-vector */
  75. for (i=0; i<starpu_data_get_nb_children(handle); i++)
  76. {
  77. struct starpu_task *task = starpu_task_create();
  78. int factor = i;
  79. task->handles[0] = starpu_data_get_sub_data(handle, 1, i);
  80. task->cl = &cl;
  81. task->synchronous = 1;
  82. task->cl_arg = &factor;
  83. task->cl_arg_size = sizeof(factor);
  84. ret = starpu_task_submit(task);
  85. if (ret == -ENODEV) goto enodev;
  86. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  87. }
  88. /* Unpartition the data, unregister it from StarPU and shutdown */
  89. starpu_data_unpartition(handle, 0);
  90. starpu_data_unregister(handle);
  91. starpu_shutdown();
  92. /* Print result matrix */
  93. FPRINTF(stderr,"OUT Matrix: \n");
  94. for(j=0 ; j<NY ; j++)
  95. {
  96. for(i=0 ; i<NX ; i++)
  97. {
  98. FPRINTF(stderr, "%2d ", matrix[(j*NX)+i]);
  99. }
  100. FPRINTF(stderr,"\n");
  101. }
  102. FPRINTF(stderr,"\n");
  103. return 0;
  104. enodev:
  105. starpu_shutdown();
  106. return 77;
  107. }