fmatrix.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-2013 Inria
  4. * Copyright (C) 2010-2013,2015-2017 CNRS
  5. * Copyright (C) 2011,2013-2016 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. /*
  19. * This examplifies how to use partitioning filters. We here just split a 2D
  20. * matrix into 2D slices (along the X axis), and run a dumb kernel on them.
  21. */
  22. #include <starpu.h>
  23. #define NX 5
  24. #define NY 4
  25. #define PARTS 2
  26. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  27. void cpu_func(void *buffers[], void *cl_arg)
  28. {
  29. unsigned i, j;
  30. int *factor = (int *) cl_arg;
  31. /* length of the matrix */
  32. unsigned nx = STARPU_MATRIX_GET_NX(buffers[0]);
  33. unsigned ny = STARPU_MATRIX_GET_NY(buffers[0]);
  34. unsigned ld = STARPU_MATRIX_GET_LD(buffers[0]);
  35. /* local copy of the matrix pointer */
  36. int *val = (int *)STARPU_MATRIX_GET_PTR(buffers[0]);
  37. for(j=0; j<ny ; j++)
  38. {
  39. for(i=0; i<nx ; i++)
  40. val[(j*ld)+i] *= *factor;
  41. }
  42. }
  43. int main(void)
  44. {
  45. unsigned j;
  46. int n=1;
  47. int matrix[NX*NY];
  48. int ret, i;
  49. int factor = 12;
  50. FPRINTF(stderr,"IN Matrix: \n");
  51. for(j=0 ; j<NY ; j++)
  52. {
  53. for(i=0 ; i<NX ; i++)
  54. {
  55. matrix[(j*NX)+i] = n++;
  56. FPRINTF(stderr, "%4d ", matrix[(j*NX)+i]);
  57. }
  58. FPRINTF(stderr,"\n");
  59. }
  60. FPRINTF(stderr,"\n");
  61. starpu_data_handle_t handle;
  62. struct starpu_codelet cl =
  63. {
  64. .cpu_funcs = {cpu_func},
  65. .cpu_funcs_name = {"cpu_func"},
  66. .nbuffers = 1,
  67. .modes = {STARPU_RW},
  68. .name = "matrix_scal"
  69. };
  70. ret = starpu_init(NULL);
  71. if (ret == -ENODEV)
  72. return 77;
  73. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  74. /* Declare data to StarPU */
  75. starpu_matrix_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)matrix, NX, NX, NY, sizeof(matrix[0]));
  76. /* Partition the matrix in PARTS sub-matrices */
  77. struct starpu_data_filter f =
  78. {
  79. .filter_func = starpu_matrix_filter_block,
  80. .nchildren = PARTS
  81. };
  82. starpu_data_partition(handle, &f);
  83. /* Submit a task on each sub-vector */
  84. for (i=0; i<starpu_data_get_nb_children(handle); i++)
  85. {
  86. struct starpu_task *task = starpu_task_create();
  87. task->handles[0] = starpu_data_get_sub_data(handle, 1, i);
  88. task->cl = &cl;
  89. task->synchronous = 1;
  90. task->cl_arg = &factor;
  91. task->cl_arg_size = sizeof(factor);
  92. ret = starpu_task_submit(task);
  93. if (ret == -ENODEV) goto enodev;
  94. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  95. }
  96. /* Unpartition the data, unregister it from StarPU and shutdown */
  97. starpu_data_unpartition(handle, STARPU_MAIN_RAM);
  98. starpu_data_unregister(handle);
  99. starpu_shutdown();
  100. /* Print result matrix */
  101. n=1;
  102. FPRINTF(stderr,"OUT Matrix: \n");
  103. for(j=0 ; j<NY ; j++)
  104. {
  105. for(i=0 ; i<NX ; i++)
  106. {
  107. FPRINTF(stderr, "%4d ", matrix[(j*NX)+i]);
  108. if (matrix[(j*NX)+i] != (int) n*12)
  109. {
  110. FPRINTF(stderr, "Incorrect result %4d != %4d", matrix[(j*NX)+i], n*12);
  111. ret=1;
  112. }
  113. n++;
  114. }
  115. FPRINTF(stderr,"\n");
  116. }
  117. FPRINTF(stderr,"\n");
  118. return ret;
  119. enodev:
  120. starpu_shutdown();
  121. return 77;
  122. }