fmatrix.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011 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 = 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. for(i=0; i<nx ; i++)
  33. val[(j*ld)+i] = *factor;
  34. }
  35. }
  36. int main(int argc, char **argv)
  37. {
  38. unsigned i, j, n=1;
  39. int matrix[NX*NY];
  40. FPRINTF(stderr,"IN Matrix: \n");
  41. for(j=0 ; j<NY ; j++) {
  42. for(i=0 ; i<NX ; i++) {
  43. matrix[(j*NX)+i] = n++;
  44. FPRINTF(stderr, "%2d ", matrix[(j*NX)+i]);
  45. }
  46. FPRINTF(stderr,"\n");
  47. }
  48. FPRINTF(stderr,"\n");
  49. starpu_data_handle handle;
  50. starpu_codelet cl = {
  51. .where = STARPU_CPU,
  52. .cpu_func = cpu_func,
  53. .nbuffers = 1
  54. };
  55. starpu_init(NULL);
  56. /* Declare data to StarPU */
  57. starpu_matrix_data_register(&handle, 0, (uintptr_t)matrix, NX, NX, NY, sizeof(matrix[0]));
  58. /* Partition the matrix in PARTS sub-matrices */
  59. struct starpu_data_filter f =
  60. {
  61. .filter_func = starpu_block_filter_func,
  62. .nchildren = PARTS,
  63. .get_nchildren = NULL,
  64. .get_child_ops = NULL
  65. };
  66. starpu_data_partition(handle, &f);
  67. /* Submit a task on each sub-vector */
  68. for (i=0; i<starpu_data_get_nb_children(handle); i++)
  69. {
  70. struct starpu_task *task = starpu_task_create();
  71. int factor = i;
  72. task->buffers[0].handle = starpu_data_get_sub_data(handle, 1, i);
  73. task->buffers[0].mode = STARPU_RW;
  74. task->cl = &cl;
  75. task->synchronous = 1;
  76. task->cl_arg = &factor;
  77. task->cl_arg_size = sizeof(factor);
  78. starpu_task_submit(task);
  79. }
  80. /* Unpartition the data, unregister it from StarPU and shutdown */
  81. starpu_data_unpartition(handle, 0);
  82. starpu_data_unregister(handle);
  83. starpu_shutdown();
  84. /* Print result matrix */
  85. FPRINTF(stderr,"OUT Matrix: \n");
  86. for(j=0 ; j<NY ; j++) {
  87. for(i=0 ; i<NX ; i++) {
  88. FPRINTF(stderr, "%2d ", matrix[(j*NX)+i]);
  89. }
  90. FPRINTF(stderr,"\n");
  91. }
  92. FPRINTF(stderr,"\n");
  93. return 0;
  94. }