mult.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2018 Alexis Juven
  4. * Copyright (C) 2012,2013 Inria
  5. * Copyright (C) 2009-2011,2013-2015 Université de Bordeaux
  6. * Copyright (C) 2010 Mehdi Juhoor
  7. * Copyright (C) 2010-2013,2015,2017 CNRS
  8. *
  9. * StarPU is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * StarPU is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  19. */
  20. /*
  21. * This example shows a simple implementation of a blocked matrix
  22. * multiplication. Note that this is NOT intended to be an efficient
  23. * implementation of sgemm! In this example, we show:
  24. * - how to declare dense matrices (starpu_matrix_data_register)
  25. * - how to manipulate matrices within codelets (eg. descr[0].blas.ld)
  26. * - how to use filters to partition the matrices into blocks
  27. * (starpu_data_partition and starpu_data_map_filters)
  28. * - how to unpartition data (starpu_data_unpartition) and how to stop
  29. * monitoring data (starpu_data_unregister)
  30. * - how to manipulate subsets of data (starpu_data_get_sub_data)
  31. * - how to construct an autocalibrated performance model (starpu_perfmodel)
  32. * - how to submit asynchronous tasks
  33. */
  34. #include <string.h>
  35. #include <math.h>
  36. #include <sys/types.h>
  37. #include <signal.h>
  38. #include <starpu.h>
  39. /*
  40. * That program should compute C = A * B
  41. *
  42. * A of size (z,y)
  43. * B of size (x,z)
  44. * C of size (x,y)
  45. |---------------|
  46. z | B |
  47. |---------------|
  48. z x
  49. |----| |---------------|
  50. | | | |
  51. | | | |
  52. | A | y | C |
  53. | | | |
  54. | | | |
  55. |----| |---------------|
  56. */
  57. //void gpu_mult(void **, void *);
  58. void cpu_mult(void **, void *);
  59. static struct starpu_perfmodel model =
  60. {
  61. .type = STARPU_HISTORY_BASED,
  62. .symbol = "history_perf"
  63. };
  64. static struct starpu_codelet cl =
  65. {
  66. .cpu_funcs = {cpu_mult},
  67. .cpu_funcs_name = {"cpu_mult"},
  68. //.cuda_funcs = {gpu_mult},
  69. .nbuffers = 3,
  70. .modes = {STARPU_R, STARPU_R, STARPU_W},
  71. .model = &model
  72. };
  73. void multiply_with_starpu(float *A, float *B, float *C, unsigned xdim, unsigned ydim, unsigned zdim, unsigned nslicesx, unsigned nslicesy)
  74. {
  75. starpu_data_handle_t A_handle, B_handle, C_handle;
  76. starpu_matrix_data_register(&A_handle, STARPU_MAIN_RAM, (uintptr_t)A,
  77. ydim, ydim, zdim, sizeof(float));
  78. starpu_matrix_data_register(&B_handle, STARPU_MAIN_RAM, (uintptr_t)B,
  79. zdim, zdim, xdim, sizeof(float));
  80. starpu_matrix_data_register(&C_handle, STARPU_MAIN_RAM, (uintptr_t)C,
  81. ydim, ydim, xdim, sizeof(float));
  82. struct starpu_data_filter vert =
  83. {
  84. .filter_func = starpu_matrix_filter_vertical_block,
  85. .nchildren = nslicesx
  86. };
  87. struct starpu_data_filter horiz =
  88. {
  89. .filter_func = starpu_matrix_filter_block,
  90. .nchildren = nslicesy
  91. };
  92. starpu_data_partition(B_handle, &vert);
  93. starpu_data_partition(A_handle, &horiz);
  94. starpu_data_map_filters(C_handle, 2, &vert, &horiz);
  95. unsigned taskx, tasky;
  96. for (taskx = 0; taskx < nslicesx; taskx++){
  97. for (tasky = 0; tasky < nslicesy; tasky++){
  98. struct starpu_task *task = starpu_task_create();
  99. task->cl = &cl;
  100. task->handles[0] = starpu_data_get_sub_data(A_handle, 1, tasky);
  101. task->handles[1] = starpu_data_get_sub_data(B_handle, 1, taskx);
  102. task->handles[2] = starpu_data_get_sub_data(C_handle, 2, taskx, tasky);
  103. if (starpu_task_submit(task)!=0) fprintf(stderr,"submit task error\n");
  104. }
  105. }
  106. starpu_task_wait_for_all();
  107. starpu_data_unpartition(A_handle, STARPU_MAIN_RAM);
  108. starpu_data_unpartition(B_handle, STARPU_MAIN_RAM);
  109. starpu_data_unpartition(C_handle, STARPU_MAIN_RAM);
  110. starpu_data_unregister(A_handle);
  111. starpu_data_unregister(B_handle);
  112. starpu_data_unregister(C_handle);
  113. }
  114. void init_rand(float * m, unsigned width, unsigned height)
  115. {
  116. unsigned i,j;
  117. for (j = 0 ; j < height ; j++){
  118. for (i = 0 ; i < width ; i++){
  119. m[j+i*height] = (float)(starpu_drand48());
  120. }
  121. }
  122. }
  123. void init_zero(float * m, unsigned width, unsigned height)
  124. {
  125. memset(m, 0, sizeof(float) * width * height);
  126. }
  127. double min_time(unsigned nb_test, unsigned xdim, unsigned ydim, unsigned zdim, unsigned nsclicesx, unsigned nsclicesy)
  128. {
  129. unsigned i;
  130. float * A = (float *) malloc(zdim*ydim*sizeof(float));
  131. float * B = (float *) malloc(xdim*zdim*sizeof(float));
  132. float * C = (float *) malloc(xdim*ydim*sizeof(float));
  133. double exec_times=-1;
  134. for (i = 0 ; i < nb_test ; i++){
  135. double start, stop, exec_t;
  136. init_rand(A, zdim, ydim);
  137. init_rand(B, xdim, zdim);
  138. init_zero(C, xdim, ydim);
  139. start = starpu_timing_now();
  140. multiply_with_starpu(A, B, C, xdim, ydim, zdim, nsclicesx, nsclicesy);
  141. stop = starpu_timing_now();
  142. exec_t = (stop - start)*1.e3; // Put in ns instead of us
  143. if (exec_times<0 || exec_times>exec_t) exec_times= exec_t;
  144. }
  145. free(A);
  146. free(B);
  147. free(C);
  148. return exec_times;
  149. }
  150. void display_times(unsigned start_dim, unsigned step_dim, unsigned stop_dim, unsigned nb_tests, unsigned nsclicesx, unsigned nsclicesy)
  151. {
  152. unsigned dim;
  153. for (dim = start_dim ; dim <= stop_dim ; dim += step_dim){
  154. double t = min_time(nb_tests, dim, dim, dim, nsclicesx, nsclicesy);
  155. printf("%f %f\n", dim*dim*4.*3./1024./1024, (2.*dim-1.)*dim*dim/t);
  156. }
  157. }
  158. int main(int argc, char * argv[])
  159. {
  160. if (starpu_init(NULL) != EXIT_SUCCESS){
  161. fprintf(stderr, "ERROR\n");
  162. return 77;
  163. }
  164. unsigned start_dim = 16*STRIDE;
  165. unsigned step_dim = 4*STRIDE;
  166. unsigned stop_dim = 4096;
  167. unsigned nb_tests = 10;
  168. unsigned nsclicesx = 2;
  169. unsigned nsclicesy = 2;
  170. display_times(start_dim, step_dim, stop_dim, nb_tests, nsclicesx, nsclicesy);
  171. starpu_shutdown();
  172. return 0;
  173. }