Getting started on blueice—Example
last update: 1/4/2007

Matrix-matrix multiplication for OpenMP

This is an OpenMP program that performs matrix-matrix multiplication. Since most of the program consists of loops (three to initialize the arrays and another to do the calculation), we judge whether these loops could be performed in parallel. If so, we insert OpenMP "do parallel" directives above the loops. We have declared the loop indices to be private, so that they are not overwritten by the parallel threads.

program matmul_omp
 
   implicit none
   integer, parameter :: nx = 1024, &
                         nm = nx, &
                         ny = nx

   integer, dimension(nx, nm) :: a
   integer, dimension(nm, ny) :: b
   integer, dimension(nx, ny) :: m
   
   integer :: i,j,n
 
!--Initialize the Matrix arrays

!$omp parallel do default(shared) private(n, i)
   do n=1, nm
   do i=1, nx
      a(i,n) = i+n
   end do
   end do
 
!$omp parallel do default(shared) private(n, j)
   do j=1, ny
   do n=1, nm
      b(n,j) = j-n
   end do
   end do
 
!$omp parallel do default(shared) private(i, j)
   do j=1, ny
   do i=1, nx
      m(i,j) = 0
   end do
   end do
 
!--Matrix-Matrix Multiplication

!$omp parallel do default(shared) private(i, j, n)
   do j=1,ny
   do n=1,nm
   do i=1,nx
      m(i,j) = m(i,j) + a(i,n)*b(n,j)
   end do
   end do
   end do

end program matmul_omp

If you have questions about this document, please contact SCD Customer Support. You can also reach us by telephone 24 hours a day, seven days a week at 303-497-1278. Additional contact methods: consult1@ucar.edu and during business hours in NCAR Mesa Lab Suite 39.

© Copyright 2006. University Corporation for Atmospheric Research (UCAR). All Rights Reserved.

Address of this page: http://www.cisl.ucar.edu/docs/blueice/examples/matmul_omp.f.html