1 Source code transformation using Rubus - Plos

8 downloads 0 Views 77KB Size Report
public static void main( String [ ] args ) { int n ... for ( int index = 0; index < inA . length ; index++) { ... public static float [] multiplyVector( float [ ] inA , float [ ] inB ) {.
1 1.1

Source code transformation using Rubus Source code:

public c l a s s V e c t o r M u l t i p l i c a t i o n { public s t a t i c void main ( S t r i n g [ ] a r g s ) { int n = 1 2 8 ; f l o a t inA [ ] = new f l o a t [ n ] ; f l o a t inB [ ] = new f l o a t [ n ] ; // a s s i g n random v a l u e s . . f o r ( int i n d e x = 0 ; i n d e x < inA . l e n g t h ; i n d e x++) { inA [ i n d e x ] = ( f l o a t ) Math . random ( ) ∗ 1 0 0 ; inB [ i n d e x ] = ( f l o a t ) Math . random ( ) ∗ 1 0 0 ; ; } f l o a t r e s u l t [ ] = m u l t i p l y V e c t o r ( inA , inB ) ; f o r ( int i n d e x = 0 ; i n d e x < r e s u l t . l e n g t h ; i n d e x++) { System . out . p r i n t l n ( r e s u l t [ i n d e x ] ) ; } } @Transform ( l o o p s ={” i ” } ) public s t a t i c f l o a t [ ] m u l t i p l y V e c t o r ( f l o a t [ ] inA , f l o a t [ ] inB ) { int n = inA . l e n g t h ; f l o a t [ ] r e s u l t = new f l o a t [ n ] ; f o r ( int i = 0 ; i < n ; i ++) { r e s u l t [ i ] = inA [ i ] ∗ inB [ i ] ; } return r e s u l t ; } } Listing 1: Vector Multiplication Complete Source Code

1.2

Transformed code using Rubus: public class VectorMultiplication { private s t a t i c f i n a l String kernel 1856089607 = ” k e r n e l void kernel 1856089607 ( const int limit0 , g l o b a l f l o a t ∗ v3 2891 , g l o b a l f l o a t ∗ v0 2891 , i n t v4 INT , g l o b a l f l o a t ∗ v1 2891 ) { i n t dim0 = g e t g l o b a l i d ( 0 ) ; v4 INT += 1 ∗ dim0 ; 1

i f ( v4 INT >= l i m i t 0 ) r e t u r n ; { g l o b a l f l o a t ∗ t0 = v3 2891 ; i n t t 1 = v4 INT ; g l o b a l f l o a t ∗ t2 = v0 2891 ; i n t t 3 = v4 INT ; f l o a t t4 = t2 [ t3 ] ; g l o b a l f l o a t ∗ t5 = v1 2891 ; i n t t 6 = v4 INT ; f l o a t t7 = t5 [ t6 ] ; f l o a t t8 = t4 ∗ t7 ; t0 [ t1 ] = t8 ; } { v4 INT += 1 ; } }”; }”; p r i v a t e s t a t i c CLContext c o n t e x t = JavaCL . createBestContext () ; public { int i float float

s t a t i c v o i d main ( S t r i n g [ ] paramArrayOfString ) = 128; [ ] a r r a y O f F l o a t 1 = new f l o a t [ i ] ; [ ] a r r a y O f F l o a t 2 = new f l o a t [ i ] ;

f o r ( i n t j = 0 ; j < a r r a y O f F l o a t 1 . l e n g t h ; j ++) { a r r a y O f F l o a t 1 [ j ] = ( ( f l o a t ) Math . random ( ) ∗ 1 0 0 . 0F) ; a r r a y O f F l o a t 2 [ j ] = ( ( f l o a t ) Math . random ( ) ∗ 1 0 0 . 0F) ; } f l o a t [ ] arrayOfFloat3 = multiplyVector ( arrayOfFloat1 , arrayOfFloat2 ) ; f o r ( i n t k = 0 ; k < a r r a y O f F l o a t 3 . l e n g t h ; k++) System . out . p r i n t l n ( a r r a y O f F l o a t 3 [ k ] ) ; } public s t a t i c f l o a t [ ] multiplyVector ( f l o a t [ ] paramArrayOfFloat1 , f l o a t [ ] paramArrayOfFloat2 ) 2

{ i n t i = paramArrayOfFloat1 . l e n g t h ; f l o a t [ ] a r r a y O f F l o a t = new f l o a t [ i ] ; i n t j = 0 ; kernel 1856089607 ( i , arrayOfFloat , paramArrayOfFloat1 , j , paramArrayOfFloat2 ) ; return arrayOfFloat ; } p u b l i c s t a t i c v o i d k e r n e l 1 8 5 6 0 8 9 6 0 7 ( i n t paramInt1 , f l o a t [ ] paramArrayOfFloat1 , f l o a t [ ] paramArrayOfFloat2 , i n t paramInt2 , f l o a t [ ] paramArrayOfFloat3 ) { CLDevice . Q u e u e P r o p e r t i e s [ ] a r r a y O f Q u e u e P r o p e r t i e s = { CLDevice . Q u e u e P r o p e r t i e s . P r o f i l i n g E n a b l e } ; CLQueue localCLQueue = c o n t e x t . c r e a t e D e f a u l t Q u e u e ( arrayOfQueueProperties ) ; String [ ] arrayOfString = { kernel 1856089607 }; CLKernel [ ] arrayOfCLKernel = c o n t e x t . creat ePro gram ( arrayOfString ) . createKernels () ; CLKernel l o c a l C L K e r n e l = arrayOfCLKernel [ 0 ] ; CLBuffer l o c a l C L B u f f e r 1 = c o n t e x t . c r e a t e B u f f e r (CLMem. Usage . InputOutput , P o i n t e r . p o i n t e r T o F l o a t s ( paramArrayOfFloat1 ) , t r u e ) ; CLBuffer l o c a l C L B u f f e r 2 = c o n t e x t . c r e a t e B u f f e r (CLMem. Usage . Input , P o i n t e r . p o i n t e r T o F l o a t s ( paramArrayOfFloat2 ) , t r u e ) ; i n t i = paramInt2 ; CLBuffer l o c a l C L B u f f e r 3 = c o n t e x t . c r e a t e B u f f e r (CLMem. Usage . Input , P o i n t e r . p o i n t e r T o F l o a t s ( paramArrayOfFloat3 ) , t r u e ) ; l o c a l C L K e r n e l . s e t A r g s ( new Object [ ] { paramInt1 , localCLBuffer1 , localCLBuffer2 , i , localCLBuffer3 }) ; i n t j = paramInt1 − paramInt2 / 1 ; CLEvent [ ] arrayOfCLEvent = { n u l l } ; CLEvent localCLEvent = l o c a l C L K e r n e l . enqueueNDRange ( localCLQueue , new i n t [ ] { j } , arrayOfCLEvent ) ; localCLQueue . f i n i s h ( ) ; l o c a l C L B u f f e r 1 . r e a d ( localCLQueue , new CLEvent [ ] { localCLEvent } ) . g e t F l o a t s ( paramArrayOfFloat1 ) ; } } Listing 2: Vector Multiplication Transformed Code

3