fmatrix.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (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 <starpu.h>
  17. #define NX 5
  18. #define NY 4
  19. #define PARTS 2
  20. void cpu_func(void *buffers[], void *cl_arg)
  21. {
  22. unsigned i, j;
  23. int *factor = cl_arg;
  24. /* length of the matrix */
  25. unsigned nx = STARPU_MATRIX_GET_NX(buffers[0]);
  26. unsigned ny = STARPU_MATRIX_GET_NY(buffers[0]);
  27. unsigned ld = STARPU_MATRIX_GET_LD(buffers[0]);
  28. /* local copy of the matrix pointer */
  29. int *val = (int *)STARPU_MATRIX_GET_PTR(buffers[0]);
  30. for(j=0; j<ny ; j++) {
  31. for(i=0; i<nx ; i++)
  32. val[(j*ld)+i] = *factor;
  33. }
  34. }
  35. int main(int argc, char **argv)
  36. {
  37. unsigned i, j, n=1;
  38. int matrix[NX*NY];
  39. fprintf(stderr,"IN Matrix: \n");
  40. for(j=0 ; j<NY ; j++) {
  41. for(i=0 ; i<NX ; i++) {
  42. matrix[(j*NX)+i] = n++;
  43. fprintf(stderr, "%2d ", matrix[(j*NX)+i]);
  44. }
  45. fprintf(stderr,"\n");
  46. }
  47. fprintf(stderr,"\n");
  48. starpu_data_handle handle;
  49. starpu_codelet cl = {
  50. .where = STARPU_CPU,
  51. .cpu_func = cpu_func,
  52. .nbuffers = 1
  53. };
  54. starpu_init(NULL);
  55. /* Declare data to StarPU */
  56. starpu_matrix_data_register(&handle, 0, (uintptr_t)matrix, NX, NX, NY, sizeof(matrix[0]));
  57. /* Partition the matrix in PARTS sub-matrices */
  58. struct starpu_data_filter f =
  59. {
  60. .filter_func = starpu_block_filter_func,
  61. .nchildren = PARTS,
  62. .get_nchildren = NULL,
  63. .get_child_ops = NULL
  64. };
  65. starpu_data_partition(handle, &f);
  66. /* Submit a task on each sub-vector */
  67. for (i=0; i<starpu_data_get_nb_children(handle); i++)
  68. {
  69. struct starpu_task *task = starpu_task_create();
  70. int factor = i;
  71. task->buffers[0].handle = starpu_data_get_sub_data(handle, 1, i);
  72. task->buffers[0].mode = STARPU_RW;
  73. task->cl = &cl;
  74. task->synchronous = 1;
  75. task->cl_arg = &factor;
  76. task->cl_arg_size = sizeof(factor);
  77. starpu_task_submit(task);
  78. }
  79. /* Unpartition the data, unregister it from StarPU and shutdown */
  80. starpu_data_unpartition(handle, 0);
  81. starpu_data_unregister(handle);
  82. starpu_shutdown();
  83. /* Print result matrix */
  84. fprintf(stderr,"OUT Matrix: \n");
  85. for(j=0 ; j<NY ; j++) {
  86. for(i=0 ; i<NX ; i++) {
  87. fprintf(stderr, "%2d ", matrix[(j*NX)+i]);
  88. }
  89. fprintf(stderr,"\n");
  90. }
  91. fprintf(stderr,"\n");
  92. return 0;
  93. }