
172 CHAPTER 6. SHARED-MEMORY: GPUS
hm = ( int ∗ ) malloc ( msize ) ;
// as a t e s t , f i l l matrix with random 1 s and 0 s
int i , j ;
for ( i = 0 ; i < n ; i ++) {
hm[ n∗ i+i ] = 0 ;
for ( j = 0 ; j < n ; j ++) {
i f ( j != i ) hm[ i ∗n+j ] = rand ( ) % 2 ;
}
}
// more o f the u s ual i n i t i a l i z a t i o n s
cudaMalloc ( ( void ∗∗ )&dm, msize ) ;
// copy host matrix to d e v i c e matrix
cudaMemcpy (dm, hm, msize , cudaMemcpyHostToDevice ) ;
htot = 0 ;
// s e t up d e v i c e t o t a l and i n i t i a l i z e i t
cudaMalloc ( ( void ∗∗ )&dtot , s i ze o f ( int ) ) ;
cudaMemcpy ( dtot ,&htot , s i ze o f ( int ) ,
cudaMemcpyHostToDevice ) ;
// OK,