matmul-omp.f90 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. !gfortran -fopenmp -O3 timings.f90
  2. program timings1
  3. use omp_lib
  4. implicit none
  5. integer, parameter :: ntests = 20
  6. integer :: n, nthreads
  7. real(kind=8), allocatable, dimension(:,:) :: a,b,c
  8. real(kind=8) :: t1, t2, elapsed_time
  9. integer(kind=8) :: tclock1, tclock2, clock_rate
  10. integer :: i,j,k,itest
  11. ! Specify number of threads to use:
  12. !!$ print *, "Using OpenMP, how many threads? "
  13. !!$ read *, nthreads
  14. !!nthreads=8
  15. !!$ call omp_set_num_threads(nthreads)
  16. !omp_get_num_threads()
  17. n=1200
  18. !print *, "Will multiply n by n matrices, input n: "
  19. !read *, n
  20. allocate(a(n,n), b(n,n), c(n,n))
  21. ! fill a and b with 1's just for demo purposes:
  22. a = 1.d0
  23. b = 1.d0
  24. call system_clock(tclock1) ! start wall timer
  25. call cpu_time(t1) ! start cpu timer
  26. do itest=1,ntests
  27. !$omp parallel do private(i,k)
  28. do j = 1,n
  29. do i = 1,n
  30. c(i,j) = 0.d0
  31. do k=1,n
  32. c(i,j) = c(i,j) + a(i,k)*b(k,j)
  33. enddo
  34. enddo
  35. enddo
  36. enddo
  37. call cpu_time(t2) ! end cpu timer
  38. print 10, ntests, t2-t1
  39. 10 format("Performed ",i4, " matrix multiplies: CPU time = ",f12.8, " seconds")
  40. call system_clock(tclock2, clock_rate)
  41. elapsed_time = float(tclock2 - tclock1) / float(clock_rate)
  42. print 11, elapsed_time
  43. 11 format("Elapsed time = ",f12.8, " seconds")
  44. end program timings1