dgemm.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #define _GNU_SOURCE
  2. #include <sched.h>
  3. #include <stdio.h>
  4. #include <float.h>
  5. #include <mkl.h>
  6. #include <morse.h>
  7. #include <starpurm.h>
  8. #include <hwloc.h>
  9. static int rm_cpu_type_id = -1;
  10. static int rm_nb_cpu_units = 0;
  11. static void test1();
  12. static void init_rm_infos(void);
  13. static const int nb_random_tests = 10;
  14. static void test1()
  15. {
  16. int i;
  17. }
  18. static void init_rm_infos(void)
  19. {
  20. int cpu_type = starpurm_get_device_type_id("cpu");
  21. int nb_cpu_units = starpurm_get_nb_devices_by_type(cpu_type);
  22. if (nb_cpu_units < 1)
  23. {
  24. /* No CPU unit available. */
  25. exit(77);
  26. }
  27. rm_cpu_type_id = cpu_type;
  28. rm_nb_cpu_units = nb_cpu_units;
  29. }
  30. static void disp_selected_cpuset(void)
  31. {
  32. hwloc_cpuset_t selected_cpuset = starpurm_get_selected_cpuset();
  33. int strl = hwloc_bitmap_snprintf(NULL, 0, selected_cpuset);
  34. char str[strl+1];
  35. hwloc_bitmap_snprintf(str, strl+1, selected_cpuset);
  36. printf("selected cpuset = %s\n", str);
  37. }
  38. int main( int argc, char const *argv[])
  39. {
  40. starpurm_initialize();
  41. init_rm_infos();
  42. printf("using default units\n");
  43. disp_selected_cpuset();
  44. test1();
  45. starpurm_shutdown();
  46. #if 0
  47. if(argc < 6 || argc > 6)
  48. {
  49. fprintf(stderr, "Usage: ./test_dgemm M N K TRANS_A TRANS_B\n" );
  50. return 1;
  51. }
  52. // Local variables
  53. int i, j;
  54. int m, n, k;
  55. const char *transA_input = NULL;
  56. const char *transB_input = NULL;
  57. enum DDSS_TRANS transA = Trans;
  58. enum DDSS_TRANS transB = Trans;
  59. double alpha;
  60. double beta;
  61. double error;
  62. double max_error;
  63. double count_error;
  64. double *A;
  65. double *B;
  66. double *C;
  67. double *C_test;
  68. struct timeval start, end;
  69. double flops;
  70. double flops_ddss;
  71. double flops_ref;
  72. int ret;
  73. m = atoi( argv[1] );
  74. n = atoi( argv[2] );
  75. k = atoi( argv[3] );
  76. if ( strlen( argv[4] ) != 1 )
  77. {
  78. fprintf(stderr,"Illegal value of TRANS_A, TRANS_A can be T or N\n");
  79. return 1;
  80. }
  81. transA_input = argv[4];
  82. if ( strlen( argv[5] ) != 1 )
  83. {
  84. fprintf(stderr,"Illegal value of TRANS_B, TRANS_B can be T or N\n");
  85. return 1;
  86. }
  87. transB_input = argv[5];
  88. // Set seed
  89. srand(time(NULL));
  90. max_error = 1.0;
  91. count_error = 0.0;
  92. // Checking inputs
  93. if ( m < 0 )
  94. {
  95. fprintf(stderr, "Illegal value of M, M must be >= 0\n");
  96. return 1;
  97. }
  98. if ( n < 0 )
  99. {
  100. fprintf(stderr, "Illegal value of N, N must be >= 0\n");
  101. return 1;
  102. }
  103. if ( k < 0 )
  104. {
  105. fprintf(stderr, "Illegal value of K, K must be >= 0\n");
  106. return 1;
  107. }
  108. if ( transA_input[0] == 'T' )
  109. {
  110. transA = Trans;
  111. }
  112. else if ( transA_input[0] == 'N' )
  113. {
  114. transA = NoTrans;
  115. }
  116. else
  117. {
  118. fprintf(stderr, "Illegal value of TRANS_A, TRANS_A can be T or N\n");
  119. return 1;
  120. }
  121. if ( transB_input[0] == 'T' )
  122. {
  123. transB = Trans;
  124. }
  125. else if ( transB_input[0] == 'N' )
  126. {
  127. transB = NoTrans;
  128. }
  129. else
  130. {
  131. fprintf(stderr, "Illegal value of TRANS_B, TRANS_B can be T or N\n");
  132. return 1;
  133. }
  134. // Matrices allocation
  135. A = ( double * ) malloc( sizeof( double ) * m * k );
  136. B = ( double * ) malloc( sizeof( double ) * k * n );
  137. C = ( double * ) malloc( sizeof( double ) * m * n );
  138. C_test = ( double * ) malloc( sizeof( double ) * m * n );
  139. // Alpha and beta initialization
  140. alpha = ( double ) rand() / (double) rand() + DBL_MIN;
  141. beta = ( double ) rand() / (double) rand() + DBL_MIN;
  142. // Matrix A, B, C and C_test initialization
  143. for ( i = 0; i < m; i++ )
  144. {
  145. for ( j = 0; j < n; j++ )
  146. {
  147. A[ i * n + j ] = ( double ) rand() / (double) rand()
  148. + DBL_MIN;
  149. B[ i * n + j ] = ( double ) rand() / (double) rand()
  150. + DBL_MIN;
  151. C[ i * n + j ] = 0.0;
  152. C_test[ i * n + j ] = 0.0;
  153. }
  154. }
  155. /* Test case */
  156. {
  157. /* pocl_starpu_init */
  158. {
  159. hwloc_topology_init(&topology);
  160. hwloc_topology_load(topology);
  161. starpurm_initialize();
  162. starpurm_set_drs_enable(NULL);
  163. }
  164. /* pocl_starpu_submit_task */
  165. {
  166. /* GLIBC cpu_mask as supplied by POCL */
  167. cpu_set_t cpu_mask;
  168. CPU_ZERO(&cpu_mask);
  169. CPU_SET (0, &cpu_mask);
  170. CPU_SET (1, &cpu_mask);
  171. CPU_SET (2, &cpu_mask);
  172. CPU_SET (3, &cpu_mask);
  173. /* Convert GLIBC cpu_mask into HWLOC cpuset */
  174. hwloc_cpuset_t hwloc_cpuset = hwloc_bitmap_alloc();
  175. int status = hwloc_cpuset_from_glibc_sched_affinity(topology, hwloc_cpuset, &cpu_mask, sizeof(cpu_set_t));
  176. assert(status == 0);
  177. /* Reset any unit previously allocated to StarPU */
  178. starpurm_withdraw_all_cpus_from_starpu(NULL);
  179. /* Enforce new cpu mask */
  180. starpurm_assign_cpu_mask_to_starpu(NULL, hwloc_cpuset);
  181. /* task function */
  182. {
  183. int TRANS_A = transA==NoTrans?MorseNoTrans:MorseTrans;
  184. int TRANS_B = transB==NoTrans?MorseNoTrans:MorseTrans;
  185. int M = m;
  186. int N = n;
  187. int K = k;
  188. double ALPHA = alpha;
  189. int LDA = k;
  190. int LDB = n;
  191. double BETA = beta;
  192. int LDC = n;
  193. MORSE_Init(4, 0);
  194. int res = MORSE_dgemm(TRANS_A, TRANS_B, M, N, K,
  195. ALPHA, A, LDA, B, LDB,
  196. BETA, C, LDC);
  197. MORSE_Finalize();
  198. }
  199. /* Withdraw all CPU units from StarPU */
  200. starpurm_withdraw_all_cpus_from_starpu(NULL);
  201. hwloc_bitmap_free(hwloc_cpuset);
  202. }
  203. /* pocl_starpu_shutdown() */
  204. {
  205. starpurm_shutdown();
  206. }
  207. }
  208. #if 0
  209. /* Check */
  210. cblas_dgemm( CblasColMajor,
  211. ( CBLAS_TRANSPOSE ) transA,
  212. ( CBLAS_TRANSPOSE ) transB,
  213. m, n, k,
  214. alpha, A, k,
  215. B, n,
  216. beta, C_test, n );
  217. // Error computation
  218. for ( i = 0; i < m; i++ )
  219. {
  220. for ( j = 0; j < n; j++ )
  221. {
  222. error = abs( C[ i * n + j ] - C_test[ i * n + j ] );
  223. if ( max_error > error )
  224. max_error = error;
  225. count_error += error;
  226. }
  227. }
  228. fprintf(stdout, "Max. error = %1.2f\n", max_error );
  229. fprintf(stdout, "Av. error = %1.2f\n", count_error / ( m * n ) );
  230. #endif
  231. #endif
  232. return 0;
  233. }