cpu_mult.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2018 Alexis Juven
  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 <stdint.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <starpu.h>
  20. /*
  21. * The codelet is passed 3 matrices, the "descr" union-type field gives a
  22. * description of the layout of those 3 matrices in the local memory (ie. RAM
  23. * in the case of CPU, GPU frame buffer in the case of GPU etc.). Since we have
  24. * registered data with the "matrix" data interface, we use the matrix macros.
  25. */
  26. void cpu_mult(void *descr[], void *cl_arg)
  27. {
  28. int stride;
  29. float *subA, *subB, *subC;
  30. stride = *((int *)cl_arg);
  31. /* .blas.ptr gives a pointer to the first element of the local copy */
  32. subA = (float *)STARPU_MATRIX_GET_PTR(descr[0]);
  33. subB = (float *)STARPU_MATRIX_GET_PTR(descr[1]);
  34. subC = (float *)STARPU_MATRIX_GET_PTR(descr[2]);
  35. /* .blas.nx is the number of rows (consecutive elements) and .blas.ny
  36. * is the number of lines that are separated by .blas.ld elements (ld
  37. * stands for leading dimension).
  38. * NB: in case some filters were used, the leading dimension is not
  39. * guaranteed to be the same in main memory (on the original matrix)
  40. * and on the accelerator! */
  41. const uint32_t nxC = STARPU_MATRIX_GET_NX(descr[2]);
  42. const uint32_t nyC = STARPU_MATRIX_GET_NY(descr[2]);
  43. const uint32_t nyA = STARPU_MATRIX_GET_NY(descr[0]);
  44. const uint32_t ldA = STARPU_MATRIX_GET_LD(descr[0]);
  45. const uint32_t ldB = STARPU_MATRIX_GET_LD(descr[1]);
  46. const uint32_t ldC = STARPU_MATRIX_GET_LD(descr[2]);
  47. /* we assume a FORTRAN-ordering! */
  48. int i,j,k,ii,jj,kk;
  49. for (i = 0; i < nyC*nxC; i++) subC[i] = 0;
  50. //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()));
  51. for (i=0;i<nyC;i+=stride)
  52. {
  53. for (k=0;k<nyA;k+=stride)
  54. {
  55. for (j=0;j<nxC;j+=stride)
  56. {
  57. for (ii = i; ii < i+stride; ii+=2)
  58. {
  59. float *sC0=subC+ii*ldC+j;
  60. float *sC1=subC+ii*ldC+ldC+j;
  61. for (kk = k; kk < k+stride; kk+=4)
  62. {
  63. float alpha00=subB[kk + ii*ldB];
  64. float alpha01=subB[kk+1+ii*ldB];
  65. float alpha10=subB[kk+ ii*ldB+ldB];
  66. float alpha11=subB[kk+1+ii*ldB+ldB];
  67. float alpha02=subB[kk+2+ii*ldB];
  68. float alpha03=subB[kk+3+ii*ldB];
  69. float alpha12=subB[kk+2+ ii*ldB+ldB];
  70. float alpha13=subB[kk+3+ii*ldB+ldB];
  71. float *sA0=subA+kk*ldA+j;
  72. float *sA1=subA+kk*ldA+ldA+j;
  73. float *sA2=subA+kk*ldA+2*ldA+j;
  74. float *sA3=subA+kk*ldA+3*ldA+j;
  75. for (jj = 0; jj < stride; jj+=1)
  76. {
  77. sC0[jj] += alpha00*sA0[jj]+alpha01*sA1[jj]+alpha02*sA2[jj]+alpha03*sA3[jj];
  78. sC1[jj] += alpha10*sA0[jj]+alpha11*sA1[jj]+alpha12*sA2[jj]+alpha13*sA3[jj];
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }
  85. //fprintf(stderr,"inside cpu_mult %dx%dx%d\n",nyC,nyA,nxC);
  86. }
  87. char* CPU = "cpu_mult";
  88. char* GPU = "gpu_mult";
  89. extern char *starpu_find_function(char *name, char *device)
  90. {
  91. if (!strcmp(device,"gpu")) return GPU;
  92. return CPU;
  93. }