fmatrix.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 = (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. FPRINTF(stderr,"IN Matrix: \n");
  42. for(j=0 ; j<NY ; j++)
  43. {
  44. for(i=0 ; i<NX ; i++)
  45. {
  46. matrix[(j*NX)+i] = n++;
  47. FPRINTF(stderr, "%2d ", matrix[(j*NX)+i]);
  48. }
  49. FPRINTF(stderr,"\n");
  50. }
  51. FPRINTF(stderr,"\n");
  52. starpu_data_handle_t handle;
  53. struct starpu_codelet cl =
  54. {
  55. .where = STARPU_CPU,
  56. .cpu_funcs = {cpu_func, NULL},
  57. .nbuffers = 1
  58. };
  59. starpu_init(NULL);
  60. /* Declare data to StarPU */
  61. starpu_matrix_data_register(&handle, 0, (uintptr_t)matrix, NX, NX, NY, sizeof(matrix[0]));
  62. /* Partition the matrix in PARTS sub-matrices */
  63. struct starpu_data_filter f =
  64. {
  65. .filter_func = starpu_block_filter_func,
  66. .nchildren = PARTS
  67. };
  68. starpu_data_partition(handle, &f);
  69. /* Submit a task on each sub-vector */
  70. for (i=0; i<starpu_data_get_nb_children(handle); i++)
  71. {
  72. struct starpu_task *task = starpu_task_create();
  73. int factor = i;
  74. task->buffers[0].handle = starpu_data_get_sub_data(handle, 1, i);
  75. task->buffers[0].mode = STARPU_RW;
  76. task->cl = &cl;
  77. task->synchronous = 1;
  78. task->cl_arg = &factor;
  79. task->cl_arg_size = sizeof(factor);
  80. starpu_task_submit(task);
  81. starpu_task_destroy(task);
  82. }
  83. /* Unpartition the data, unregister it from StarPU and shutdown */
  84. starpu_data_unpartition(handle, 0);
  85. starpu_data_unregister(handle);
  86. starpu_shutdown();
  87. /* Print result matrix */
  88. FPRINTF(stderr,"OUT Matrix: \n");
  89. for(j=0 ; j<NY ; j++)
  90. {
  91. for(i=0 ; i<NX ; i++)
  92. {
  93. FPRINTF(stderr, "%2d ", matrix[(j*NX)+i]);
  94. }
  95. FPRINTF(stderr,"\n");
  96. }
  97. FPRINTF(stderr,"\n");
  98. return 0;
  99. }