Advertisement
Guest User

MatrMul

a guest
Oct 29th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. program MatrMul
  2.  
  3.    implicit none
  4.    
  5.    Character(len = 1) :: finishProgram
  6.    integer :: n, i, j, k, maxDimension
  7.    integer :: alloc_status, dealloc_status
  8.    double precision, allocatable, dimension(:,:) :: a, b, c
  9.    real :: t1, t2
  10.  
  11.    write ( *, * ) 'Maximun Dimension (Starts at 100, counting step 100): '
  12.    read (*,*) maxDimension
  13.    
  14.    do n = 100, maxDimension, 100
  15.    
  16.       allocate(a(n,n),stat=alloc_status)
  17.       allocate(b(n,n),stat=alloc_status)
  18.       allocate(c(n,n),stat=alloc_status)
  19.      
  20.       !get random matrix a
  21.       do i = 1, n
  22.          do j = 1, n
  23.             a(i, j) = rand(0)
  24.          end do
  25.       end do
  26.    
  27.       !get random matrix b
  28.       do i = 1, n
  29.          do j = 1, n
  30.             b(i, j) = rand(0)
  31.          end do
  32.       end do
  33.    
  34.       !initilize matrix c (0 everywhere)
  35.       do i = 1, n
  36.          do j = 1, n
  37.             c(i, j) = 0
  38.          end do  
  39.       end do
  40.    
  41.    
  42.       call cpu_time ( t1 )
  43.      
  44.       do i = 1, n
  45.          do j = 1, n
  46.             do k = 1, n
  47.                c(i, j) = c(i, j) + (a(i, k) * b(k, j))
  48.             end do
  49.          end do
  50.       end do
  51.              
  52.       call cpu_time ( t2 )
  53.    
  54.    
  55.       write ( *, * ) 'dimension =', n, ' time needed:', t2 - t1, 'sec'
  56.    
  57.       deallocate(a,stat=dealloc_status)
  58.       deallocate(b,stat=dealloc_status)
  59.       deallocate(c,stat=dealloc_status)
  60.    
  61.    end do
  62.    
  63.    write ( *, * ) 'Enter any key to finish'
  64.    read (*,*) finishProgram
  65.    
  66. end program MatrMul
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement