#NEWLINE# def test_matmul_fast( self ) : #NEWLINE# #INDENT# blocksize = 20 #NEWLINE# gridsize = 20 #NEWLINE# @ hsa . jit #NEWLINE# def matmulfast( A , B , C ) : #NEWLINE# #INDENT# x = hsa . get_global_id ( 0 ) #NEWLINE# y = hsa . get_global_id ( 1 ) #NEWLINE# tx = hsa . get_local_id ( 0 ) #NEWLINE# ty = hsa . get_local_id ( 1 ) #NEWLINE# sA = hsa . shared . array ( shape = ( blocksize , blocksize ) , dtype = float32 ) #NEWLINE# sB = hsa . shared . array ( shape = ( blocksize , blocksize ) , dtype = float32 ) #NEWLINE# if ( ( x >= C . shape [ 0 ] ) or ( y >= C . shape [ 1 ] ) ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# tmp = 0 #NEWLINE# for i in range ( gridsize ) : #NEWLINE# #INDENT# sA [ ( tx , ty ) ] = A [ ( x , ( ty + ( i * blocksize ) ) ) ] #NEWLINE# sB [ ( tx , ty ) ] = B [ ( ( tx + ( i * blocksize ) ) , y ) ] #NEWLINE# hsa . barrier ( hsa . CLK_GLOBAL_MEM_FENCE ) #NEWLINE# for j in range ( blocksize ) : #NEWLINE# #INDENT# tmp += ( sA [ ( tx , j ) ] * sB [ ( j , ty ) ] ) #NEWLINE# #UNINDENT# hsa . barrier ( hsa . CLK_GLOBAL_MEM_FENCE ) #NEWLINE# #UNINDENT# C [ ( x , y ) ] = tmp #NEWLINE# #UNINDENT# N = ( gridsize * blocksize ) #NEWLINE# A = np . random . random ( ( N , N ) ) . astype ( np . float32 ) #NEWLINE# B = np . random . random ( ( N , N ) ) . astype ( np . float32 ) #NEWLINE# C = np . zeros_like ( A ) #NEWLINE# griddim = ( B , gridsize ) #NEWLINE# blockdim = ( blocksize , blocksize ) #NEWLINE# with hsa . register ( A , B , C ) : #NEWLINE# #INDENT# ts = timer ( ) #NEWLINE# matmulfast [ ( griddim , blockdim ) ] ( A , B , C ) #NEWLINE# te = timer ( ) #NEWLINE# print ( '1st GPU time:' , ( te - ts ) ) #NEWLINE# #UNINDENT# with hsa . register ( A , B , C ) : #NEWLINE# #INDENT# ts = timer ( ) #NEWLINE# matmulfast [ ( griddim , blockdim ) ] ( A , B , C ) #NEWLINE# te = timer ( ) #NEWLINE# print ( '2nd GPU time:' , ( te - ts ) ) #NEWLINE# #UNINDENT# ts = timer ( ) #NEWLINE# ans = np . dot ( A , B ) #NEWLINE# te = timer ( ) #NEWLINE# print ( 'CPU time:' , ( te - ts ) ) #NEWLINE# np . testing . assert_allclose ( ans , C , rtol = 1e-05 )