Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Fortran Parallel Programming

Tamiris_Crepalde
Beginner
639 Views

Hello!

I'm trying to do a task from a class of my university that consists to make a parallel program on fortran and I'm using visual studio. But always I put on the code 'include "mpif.h"' it returns a error message saying that is not possible open the mpif.h. I have read that the visual includes the MPI library. Someone can help me in this?

0 Kudos
6 Replies
jimdempseyatthecove
Honored Contributor III
639 Views

Tamiris,

In Fortran, the headers are generally module files (.mod files). To "include" (incorporate) a module into a Fortran program/subroutine/function, insert:

    USE MPI

after the program/subroutine/function statement, but before "implicit none" and/or data declarations.

the mpi??.h files would be used by C/C++ programs.

Also,

Due to mpif.h not being located, indicates that the MPI library is either not installed, or the INCLUDE path has not been properly setup.

Are you sure you have been instructed to make a parallel program using MPI, or is it to be with OpenMP, or not specified by instructor?

Jim Dempsey

0 Kudos
TimP
Honored Contributor III
639 Views

mpich (Argonne MPI) for Windows was supported at one time by Microsoft and continues to be available to work with ifort and visual studio.  You would need to install and set paths for that separately, if you don't have the Intel cluster studio version of ifort (which includes an MPI similar to mpich).

As Jim hinted, MPI has moved toward 'USE MPI....' method for header files, although mpif.h was the original Fortran interface.

ifort has OpenMP parallel capability built in.

0 Kudos
FortranFan
Honored Contributor II
639 Views

@Tamiris Crepalde,

Given your original post indicating this is for a task in a class at your university thereby implying you are student, I suggest you study standard facilities in Fortran with COARRAYs and DO CONCURRENT and related topics in Intel Fortran online reference guide:

https://software.intel.com/en-us/fortran-compiler-18.0-developer-guide-and-reference-using-coarrays

https://software.intel.com/en-us/fortran-compiler-18.0-developer-guide-and-reference-do-concurrent

Here's a Hello World! type of example you can try as your first exercise.  Please note the license terms I embedded in the example (I normally don't do this), so please you have to include the copyright information if you use this code as-is in your class and then it will be up to your professor as to whether it gets accepted toward your class work requirements!

module m
! File: m.f90
!
! BSD License:
! Copyright 2017 FortranFan
!
! Redistribution and use in source and binary forms, with or without modification,
! are permitted provided that the following conditions are met:
!
! 1. Redistributions of source code must retain the above copyright notice, this
!    list of conditions and the following disclaimer.
!
! 2. Redistributions in binary form must reproduce the above copyright notice,
!    this list of conditions and the following disclaimer in the documentation
!    and/or other materials provided with the distribution.
!
! 3. Neither the name of the copyright holder nor the names of its contributors
!    may be used to endorse or promote products derived from this software without 
!    specific prior written permission.
!
! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
! ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
! WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
! IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
! INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
! BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
! DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
! OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
! NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
! EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
!    
   implicit none
   
   private
   
   type, public :: foo_t
      private
      integer :: m_foo = 0
      integer :: m_bar = 0
   contains
      procedure, pass(this), public :: init
      procedure, pass(this), public :: calc
      procedure, pass(this), public :: Message => get_Message
   end type foo_t

contains

   pure elemental subroutine init( this, Key )
      class(foo_t), intent(inout) :: this
      integer, intent(in)         :: Key
      this%m_foo = Key
      return
   end subroutine init

   pure elemental subroutine calc( this )
      class(foo_t), intent(inout) :: this
      this%m_bar = this%m_foo + 42
      return
   end subroutine calc

   pure elemental function get_Message( this ) result( Message )
      class(foo_t), intent(in) :: this
      ! function result
      character(len=50) :: Message
      write( Message, fmt=* ) this%m_bar
      Message = "Hello World!" // new_line("") // "The answer to everything is " // trim(Message)
      return
   end function get_Message

end module
program p
! File: p.f90
!
! BSD License:
! Copyright 2017 FortranFan
!
! Redistribution and use in source and binary forms, with or without modification,
! are permitted provided that the following conditions are met:
!
! 1. Redistributions of source code must retain the above copyright notice, this
!    list of conditions and the following disclaimer.
!
! 2. Redistributions in binary form must reproduce the above copyright notice,
!    this list of conditions and the following disclaimer in the documentation
!    and/or other materials provided with the distribution.
!
! 3. Neither the name of the copyright holder nor the names of its contributors
!    may be used to endorse or promote products derived from this software without 
!    specific prior written permission.
!
! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
! ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
! WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
! IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
! INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
! BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
! DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
! OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
! NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
! EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
!    

   use, intrinsic :: iso_fortran_env, only : output_unit
   use m, only : foo_t

   implicit none

   type(foo_t), save :: foo
  • character(len=50), save :: Message
  • integer :: Idx if ( this_image() == 1 ) then do Idx = 1, num_images() critical call foo[Idx]%init( Idx ) end critical end do sync all do concurrent ( Idx=1:num_images() ) call foo[Idx]%calc() end do sync all do concurrent ( Idx=1:num_images() ) Message[Idx] = foo[Idx]%Message() end do sync all write( output_unit, "(t1,g0,t10,g0)" ) "Image", "Message" do Idx = 1, num_images() write( output_unit, "(t1,g0,t10,g0)" ) Idx, trim( Message[Idx] ) end do end if stop end program
  • Compilation, linking, and execution instructions:

    xxx>ifort -c /Qcoarray:shared /standard-semantics m.f90
    Intel(R) Visual Fortran Intel(R) 64 Compiler for applications running on Intel(R
    ) 64, Version 18.0.0.124 Build 20170811
    Copyright (C) 1985-2017 Intel Corporation.  All rights reserved.
    
    
    xxx>ifort -c /Qcoarray:shared /standard-semantics p.f90
    Intel(R) Visual Fortran Intel(R) 64 Compiler for applications running on Intel(R
    ) 64, Version 18.0.0.124 Build 20170811
    Copyright (C) 1985-2017 Intel Corporation.  All rights reserved.
    
    
    xxx>link m.obj p.obj /subsystem:console -out:p.exe
    Microsoft (R) Incremental Linker Version 14.11.25508.2
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    

    Upon execution, you should see output along the following lines showing parallel execution:

    Image    Message
    1        Hello World!
    The answer to everything is  43
    2        Hello World!
    The answer to everything is  44
    3        Hello World!
    The answer to everything is  45
    4        Hello World!
    The answer to everything is  46
    5        Hello World!
    The answer to everything is  47
    6        Hello World!
    The answer to everything is  48
    7        Hello World!
    The answer to everything is  49
    8        Hello World!
    The answer to everything is  50
    

     

    0 Kudos
    jimdempseyatthecove
    Honored Contributor III
    639 Views

    FF,

    We all know the answer to everything is 42 (Hitchhiker's Guide to the Galaxy)

    Jim Dempsey

    0 Kudos
    FortranFan
    Honored Contributor II
    639 Views

    jimdempseyatthecove wrote:

    FF,

    We all know the answer to everything is 42 ..

    Indeed, glad you noticed - hopefully OP will too and fix the code to restore the "equilibrium" in the galaxy ..

    -))

    0 Kudos
    gib
    New Contributor II
    639 Views

    43-50 are the answers in parallel universes.

    0 Kudos
    Reply