cpu_mult.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2018 Alexis Juven
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <stdint.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <starpu.h>
  21. /*
  22. * The codelet is passed 3 matrices, the "descr" union-type field gives a
  23. * description of the layout of those 3 matrices in the local memory (ie. RAM
  24. * in the case of CPU, GPU frame buffer in the case of GPU etc.). Since we have
  25. * registered data with the "matrix" data interface, we use the matrix macros.
  26. */
  27. void cpu_mult(void *descr[], void *cl_arg)
  28. {
  29. int stride;
  30. float *subA, *subB, *subC;
  31. stride = *((int *)cl_arg);
  32. /* .blas.ptr gives a pointer to the first element of the local copy */
  33. subA = (float *)STARPU_MATRIX_GET_PTR(descr[0]);
  34. subB = (float *)STARPU_MATRIX_GET_PTR(descr[1]);
  35. subC = (float *)STARPU_MATRIX_GET_PTR(descr[2]);
  36. /* .blas.nx is the number of rows (consecutive elements) and .blas.ny
  37. * is the number of lines that are separated by .blas.ld elements (ld
  38. * stands for leading dimension).
  39. * NB: in case some filters were used, the leading dimension is not
  40. * guaranteed to be the same in main memory (on the original matrix)
  41. * and on the accelerator! */
  42. const uint32_t nxC = STARPU_MATRIX_GET_NX(descr[2]);
  43. const uint32_t nyC = STARPU_MATRIX_GET_NY(descr[2]);
  44. const uint32_t nyA = STARPU_MATRIX_GET_NY(descr[0]);
  45. const uint32_t ldA = STARPU_MATRIX_GET_LD(descr[0]);
  46. const uint32_t ldB = STARPU_MATRIX_GET_LD(descr[1]);
  47. const uint32_t ldC = STARPU_MATRIX_GET_LD(descr[2]);
  48. /* we assume a FORTRAN-ordering! */
  49. int i,j,k,ii,jj,kk;
  50. for (i = 0; i < nyC*nxC; i++) subC[i] = 0;
  51. //fprintf(stderr,"inside cpu_mult %dx%dx%d %d/%d on %d\n",nyC,nyA,nxC,starpu_worker_get_id(),STARPU_NMAXWORKERS,starpu_worker_get_devid(starpu_worker_get_id()));
  52. for (i=0;i<nyC;i+=stride)
  53. {
  54. for (k=0;k<nyA;k+=stride)
  55. {
  56. for (j=0;j<nxC;j+=stride)
  57. {
  58. for (ii = i; ii < i+stride; ii+=2)
  59. {
  60. float *sC0=subC+ii*ldC+j;
  61. float *sC1=subC+ii*ldC+ldC+j;
  62. for (kk = k; kk < k+stride; kk+=4)
  63. {
  64. float alpha00=subB[kk + ii*ldB];
  65. float alpha01=subB[kk+1+ii*ldB];
  66. float alpha10=subB[kk+ ii*ldB+ldB];
  67. float alpha11=subB[kk+1+ii*ldB+ldB];
  68. float alpha02=subB[kk+2+ii*ldB];
  69. float alpha03=subB[kk+3+ii*ldB];
  70. float alpha12=subB[kk+2+ ii*ldB+ldB];
  71. float alpha13=subB[kk+3+ii*ldB+ldB];
  72. float *sA0=subA+kk*ldA+j;
  73. float *sA1=subA+kk*ldA+ldA+j;
  74. float *sA2=subA+kk*ldA+2*ldA+j;
  75. float *sA3=subA+kk*ldA+3*ldA+j;
  76. for (jj = 0; jj < stride; jj+=1)
  77. {
  78. sC0[jj] += alpha00*sA0[jj]+alpha01*sA1[jj]+alpha02*sA2[jj]+alpha03*sA3[jj];
  79. sC1[jj] += alpha10*sA0[jj]+alpha11*sA1[jj]+alpha12*sA2[jj]+alpha13*sA3[jj];
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86. //fprintf(stderr,"inside cpu_mult %dx%dx%d\n",nyC,nyA,nxC);
  87. }
  88. char* CPU = "cpu_mult";
  89. char* GPU = "";
  90. extern char *starpu_find_function(char *name, char *device)
  91. {
  92. if (!strcmp(device,"gpu")) return GPU;
  93. return CPU;
  94. }