@@ -57,6 +57,18 @@ typedef struct {
5757} Cache ;
5858typedef void (* MatMul )(const F * A , const F * B , F * C , size_t n , Cache * cache );
5959
60+ void cl_buf_init (Cache * cache , size_t mat_sizeof ) {
61+ cache -> buf_a = clCreateBuffer (cache -> common .context , CL_MEM_READ_ONLY , mat_sizeof , NULL , NULL );
62+ cache -> buf_b = clCreateBuffer (cache -> common .context , CL_MEM_READ_ONLY , mat_sizeof , NULL , NULL );
63+ cache -> buf_c = clCreateBuffer (cache -> common .context , CL_MEM_WRITE_ONLY , mat_sizeof , NULL , NULL );
64+ }
65+
66+ void cl_buf_deinit (Cache * cache ) {
67+ clReleaseMemObject (cache -> buf_a );
68+ clReleaseMemObject (cache -> buf_b );
69+ clReleaseMemObject (cache -> buf_c );
70+ }
71+
6072/* No, this was not created for debugging, my code is flawless from the first try. */
6173void mat_print (const F * A , size_t n ) {
6274 size_t i , j ;
@@ -574,56 +586,62 @@ int main(int argc, char **argv) {
574586 max_runtime = 1.0 ;
575587 }
576588
589+ common_init (& (cache .common ), NULL );
590+
577591 /* Unit test 2x2. */
578592 {
579- /*const F A[] = {*/
580- /*1.0, 2.0,*/
581- /*3.0, 4.0*/
582- /*};*/
583- /*const F B[] = {*/
584- /*5.0, 6.0,*/
585- /*7.0, 8.0*/
586- /*};*/
587- /*enum N { n = 2 };*/
588- /*F C[n*n];*/
589- /*const F C_ref[] = {*/
590- /*19.0, 22.0,*/
591- /*43.0, 50.0*/
592- /*};*/
593- /*for (f = 0; f < sizeof(mat_mul_funcs)/sizeof(mat_mul_funcs[0]); ++f) {*/
594- /*mat_zero(C, n);*/
595- /*mat_mul_funcs[f](A, B, C, n, &cache);*/
596- /*mat_assert_eq(C, C_ref, n);*/
597- /*}*/
593+ const F A [] = {
594+ 1.0 , 2.0 ,
595+ 3.0 , 4.0
596+ };
597+ const F B [] = {
598+ 5.0 , 6.0 ,
599+ 7.0 , 8.0
600+ };
601+ enum N { n = 2 };
602+ F C [n * n ];
603+ const F C_ref [] = {
604+ 19.0 , 22.0 ,
605+ 43.0 , 50.0
606+ };
607+ cl_buf_init (& cache , n * n * sizeof (F ));
608+ for (f = 0 ; f < sizeof (mat_mul_funcs )/sizeof (mat_mul_funcs [0 ]); ++ f ) {
609+ mat_zero (C , n );
610+ mat_mul_funcs [f ](A , B , C , n , & cache );
611+ mat_assert_eq (C , C_ref , n );
612+ }
613+ cl_buf_deinit (& cache );
598614 }
599615
600616 /* Unit test 4x4. */
601617 {
602- /*const F A[] = {*/
603- /*1.0, 2.0, 3.0, 4.0,*/
604- /*5.0, 6.0, 7.0, 8.0,*/
605- /*9.0, 10.0, 11.0, 12.0,*/
606- /*13.0, 14.0, 15.0, 16.0,*/
607- /*};*/
608- /*const F B[] = {*/
609- /*17.0, 18.0, 19.0, 20.0,*/
610- /*21.0, 22.0, 23.0, 24.0,*/
611- /*25.0, 26.0, 27.0, 28.0,*/
612- /*29.0, 30.0, 31.0, 32.0,*/
613- /*};*/
614- /*const F C_ref[] = {*/
615- /*250.0, 260.0, 270.0, 280.0,*/
616- /*618.0, 644.0, 670.0, 696.0,*/
617- /*986.0, 1028.0, 1070.0, 1112.0,*/
618- /*1354.0, 1412.0, 1470.0, 1528.0,*/
619- /*};*/
620- /*enum N { n = 4 };*/
621- /*F C[n*n];*/
622- /*for (f = 0; f < NELEMS(mat_mul_funcs); ++f) {*/
623- /*mat_zero(C, n);*/
624- /*mat_mul_funcs[f](A, B, C, n, &cache);*/
625- /*mat_assert_eq(C, C_ref, n);*/
626- /*}*/
618+ const F A [] = {
619+ 1.0 , 2.0 , 3.0 , 4.0 ,
620+ 5.0 , 6.0 , 7.0 , 8.0 ,
621+ 9.0 , 10.0 , 11.0 , 12.0 ,
622+ 13.0 , 14.0 , 15.0 , 16.0 ,
623+ };
624+ const F B [] = {
625+ 17.0 , 18.0 , 19.0 , 20.0 ,
626+ 21.0 , 22.0 , 23.0 , 24.0 ,
627+ 25.0 , 26.0 , 27.0 , 28.0 ,
628+ 29.0 , 30.0 , 31.0 , 32.0 ,
629+ };
630+ const F C_ref [] = {
631+ 250.0 , 260.0 , 270.0 , 280.0 ,
632+ 618.0 , 644.0 , 670.0 , 696.0 ,
633+ 986.0 , 1028.0 , 1070.0 , 1112.0 ,
634+ 1354.0 , 1412.0 , 1470.0 , 1528.0 ,
635+ };
636+ enum N { n = 4 };
637+ F C [n * n ];
638+ cl_buf_init (& cache , n * n * sizeof (F ));
639+ for (f = 0 ; f < NELEMS (mat_mul_funcs ); ++ f ) {
640+ mat_zero (C , n );
641+ mat_mul_funcs [f ](A , B , C , n , & cache );
642+ mat_assert_eq (C , C_ref , n );
643+ }
644+ cl_buf_deinit (& cache );
627645 }
628646
629647 /* Benchmarks. */
@@ -634,7 +652,6 @@ int main(int argc, char **argv) {
634652 size_t n = 2 ;
635653
636654 puts ("#matmul" );
637- common_init (& (cache .common ), NULL );
638655 done = 0 ;
639656 while (1 ) {
640657 printf ("%zu " , (size_t )log2 (n ));
@@ -652,11 +669,7 @@ int main(int argc, char **argv) {
652669 mat_rand (A , n );
653670 mat_rand (B , n );
654671
655- /* CL setup. */
656- cache .buf_a = clCreateBuffer (cache .common .context , CL_MEM_READ_ONLY , mat_sizeof , NULL , NULL );
657- cache .buf_b = clCreateBuffer (cache .common .context , CL_MEM_READ_ONLY , mat_sizeof , NULL , NULL );
658- cache .buf_c = clCreateBuffer (cache .common .context , CL_MEM_WRITE_ONLY , mat_sizeof , NULL , NULL );
659-
672+ cl_buf_init (& cache , mat_sizeof );
660673 first = 1 ;
661674 for (f = 0 ; f < NELEMS (mat_mul_funcs ); ++ f ) {
662675 if (func_done [f ]) {
@@ -687,16 +700,13 @@ int main(int argc, char **argv) {
687700 break ;
688701 n *= 2 ;
689702
690- /* CPU teardown . */
703+ /* CPU deinit . */
691704 free (A );
692705 free (B );
693706 free (C );
694707 free (C_ref );
695708
696- /* GPU teardown. */
697- clReleaseMemObject (cache .buf_a );
698- clReleaseMemObject (cache .buf_b );
699- clReleaseMemObject (cache .buf_c );
709+ cl_buf_deinit (& cache );
700710 }
701711 common_deinit (& cache .common );
702712 }
0 commit comments