test_strassen.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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 "strassen.h"
  17. #include <sys/time.h>
  18. unsigned dim = 4096;
  19. unsigned reclevel = 4;
  20. unsigned norandom = 0;
  21. sem_t sem;
  22. float *A;
  23. float *B;
  24. float *C;
  25. starpu_data_handle A_state;
  26. starpu_data_handle B_state;
  27. starpu_data_handle C_state;
  28. struct timeval start;
  29. struct timeval end;
  30. /* to compute MFlop/s */
  31. uint64_t flop_cublas = 0;
  32. uint64_t flop_atlas = 0;
  33. /* to compute MB/s (load/store) */
  34. uint64_t ls_cublas = 0;
  35. uint64_t ls_atlas = 0;
  36. /*
  37. * Strassen complexity : n = 2^k matrices, stops at 2^r : recursion = (k-r) levels
  38. * m = n / 2^rec
  39. * M(k) = 7^(k-r) 8^r = 7^rec (m^3)
  40. * A(k) = 4^r (2^r + 5) 7^(k-r) - 6 x 4^k = (m^2)(m+5)*7^rec - 6n^2
  41. *
  42. * 4n^2.807
  43. */
  44. double strassen_complexity(unsigned n, unsigned rec)
  45. {
  46. double mult, add;
  47. double m = (1.0*n)/(pow(2.0, (double)rec));
  48. add = ((m*m)*(m+5)*(pow(7.0, (double)rec)) - 6.0*n*n);
  49. mult = (m*m*m)*(pow(7.0, (double)rec));
  50. //printf("%e adds %e mult\n", add, mult);
  51. return (add+mult);
  52. }
  53. /*
  54. * That program should compute C = A * B
  55. *
  56. * A of size (z,y)
  57. * B of size (x,z)
  58. * C of size (x,y)
  59. */
  60. void terminate(void *arg __attribute__ ((unused)))
  61. {
  62. gettimeofday(&end, NULL);
  63. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  64. //uint64_t total_flop = flop_cublas + flop_atlas;
  65. double total_flop = strassen_complexity(dim, reclevel);//4.0*pow((double)dim, 2.807);
  66. fprintf(stderr, "Computation took (ms):\n");
  67. printf("%2.2f\n", timing/1000);
  68. fprintf(stderr, " GFlop : total (%2.2f)\n", (double)total_flop/1000000000.0f);
  69. fprintf(stderr, " GFlop/s : %2.2f\n", (double)total_flop / (double)timing/1000);
  70. sem_post(&sem);
  71. }
  72. void parse_args(int argc, char **argv)
  73. {
  74. int i;
  75. for (i = 1; i < argc; i++) {
  76. if (strcmp(argv[i], "-size") == 0) {
  77. char *argptr;
  78. dim = strtol(argv[++i], &argptr, 10);
  79. }
  80. if (strcmp(argv[i], "-rec") == 0) {
  81. char *argptr;
  82. reclevel = strtol(argv[++i], &argptr, 10);
  83. }
  84. if (strcmp(argv[i], "-no-random") == 0) {
  85. norandom = 1;
  86. }
  87. }
  88. }
  89. void init_problem(void)
  90. {
  91. unsigned i,j;
  92. #ifdef USE_FXT
  93. fxt_register_thread(0);
  94. #endif
  95. A = malloc(dim*dim*sizeof(float));
  96. B = malloc(dim*dim*sizeof(float));
  97. C = malloc(dim*dim*sizeof(float));
  98. /* fill the A and B matrices */
  99. if (norandom) {
  100. for (i=0; i < dim; i++) {
  101. for (j=0; j < dim; j++) {
  102. A[i+j*dim] = (float)(i);
  103. }
  104. }
  105. for (i=0; i < dim; i++) {
  106. for (j=0; j < dim; j++) {
  107. B[i+j*dim] = (float)(j);
  108. }
  109. }
  110. }
  111. else {
  112. srand(2008);
  113. for (j=0; j < dim; j++) {
  114. for (i=0; i < dim; i++) {
  115. A[i+j*dim] = (float)(starpu_drand48());
  116. }
  117. }
  118. for (j=0; j < dim; j++) {
  119. for (i=0; i < dim; i++) {
  120. B[i+j*dim] = (float)(starpu_drand48());
  121. }
  122. }
  123. }
  124. for (j=0; j < dim; j++) {
  125. for (i=0; i < dim; i++) {
  126. C[i+j*dim] = (float)(0);
  127. }
  128. }
  129. starpu_register_blas_data(&A_state, 0, (uintptr_t)A,
  130. dim, dim, dim, sizeof(float));
  131. starpu_register_blas_data(&B_state, 0, (uintptr_t)B,
  132. dim, dim, dim, sizeof(float));
  133. starpu_register_blas_data(&C_state, 0, (uintptr_t)C,
  134. dim, dim, dim, sizeof(float));
  135. gettimeofday(&start, NULL);
  136. strassen(A_state, B_state, C_state, terminate, NULL, reclevel);
  137. }
  138. int main(__attribute__ ((unused)) int argc,
  139. __attribute__ ((unused)) char **argv)
  140. {
  141. parse_args(argc, argv);
  142. /* start the runtime */
  143. starpu_init(NULL);
  144. starpu_helper_init_cublas();
  145. sem_init(&sem, 0, 0U);
  146. init_problem();
  147. sem_wait(&sem);
  148. sem_destroy(&sem);
  149. starpu_helper_shutdown_cublas();
  150. starpu_shutdown();
  151. return 0;
  152. }