# Coarray Reading file

**URL:** <https://fortran-lang.discourse.group/t/coarray-reading-file/6403>\
**Category:** Help\
**Created:** [August 27, 2023, 7:06am UTC](https://fortran-lang.discourse.group/t/coarray-reading-file/6403 "2023-08-27T07:06:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![apoorv01](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@apoorv01](https://fortran-lang.discourse.group/u/apoorv01)\
**Post date:** [August 27, 2023, 7:06am UTC](https://fortran-lang.discourse.group/t/coarray-reading-file/6403/1 "2023-08-27T07:06:45Z")

</div>

Hi,

I am trying to read a file in coarray program (opencoarrays), I am using the condition that it be read when image==1. But it seems that CAFRUN automatically runs all the images available. Is there any way that coarray reads only from the image 1 and do rest of the calculations, by not repeating it for other images.

Any advice, help and documentation would be helpful

Attached is the source code and dummy file, I am testing. Can we use coarray to read and write in serial mode and do all the numerical operation in parallel mode (something similar to openmp)

Thanks and Regards

Apurva

[Fourth.f90](https://fortran-lang.discourse.group/uploads/short-url/pOqpxPEHXTh7VQQpEEMzuwfqQN7.f90) (568 Bytes)  
[xy.txt](https://fortran-lang.discourse.group/uploads/short-url/ysbshRpJxabAOi2nNkA2I7du0Aw.txt) (4 Bytes)

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [August 27, 2023, 10:52am UTC](https://fortran-lang.discourse.group/t/coarray-reading-file/6403/2 "2023-08-27T10:52:26Z")

</div>

For small files it’s easier for people to read and respond if they are pasted in the message. Below are `fourth.f90` and `xy.txt`.

```auto
program t1
        use, intrinsic :: iso_fortran_env
         implicit none
        integer :: n[*],pro,q,me
        !n=0
        !write(*,*)"Called Main Program"
        
        me = this_image()
        pro = num_images()
        write(*,*)"image : ",this_image()
        if ( me == 1) then
                open(unit=input_unit,file="xy.txt",action="read")
                read(input_unit,*)n
                do q = 2,pro
                        n[q]=n[1]
                end do
         end if
       
         write(*,*)"n :",n
         sync all 

end program t1

```

` 10`

---

<div class="post-metadata">

**Author:** ![mEm](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@mEm](https://fortran-lang.discourse.group/u/mEm)\
**Post date:** [August 27, 2023, 1:41pm UTC](https://fortran-lang.discourse.group/t/coarray-reading-file/6403/3 "2023-08-27T13:41:29Z")

</div>

Hi @apoorv01 ,

> [@apoorv01](#):
>
> But it seems that CAFRUN automatically runs all the images available. Is there any way that coarray reads only from the image 1 and do rest of the calculations, by not repeating it for other images.

It’s a bit unclear what you mean with these statements.  
Maybe, a clarification would further help readers understanding your problem and possibly provide ad-hoc solutions for them.

In the meantime, I tried your code, and by anticipating that I might have not caught what you actually want to achieve with it, I think there are some tiny logic flaws, specially with the loop

```auto
do q = 2,pro
   n[q]=n[1]
end do

```

[Here](https://coarrays.sourceforge.io/doc.html) and [here](https://fortranwiki.org/fortran/show/Coarrays) some references where you can read more about coarrays and how they work in practice. For sure, other more experianced members here can guide you towards more insightful places.

In the meantime, I give you a modified version of your code based on what I thought you are trying to attempt:

```auto
program test
   use, intrinsic :: iso_fortran_env
   implicit none
   integer :: n[*], nimgs, i_, me, iu = 100
   
   me = this_image()
   nimgs = num_images()
   
   write(*, *) ' Hello from image : ', me, ' of ', nimgs
   sync all

   ! Read file only from image 1
   if (me == 1) then
      write(*, *) ' Image ', me, ' reading file...'
      open(unit=iu, file='xy.txt', form='formatted', action = 'read')
      read(iu, *) n[1] !<--- NOTE: equivalent "read(iu, *) n"
      close(iu)
      write(*, *) ' Image ', me, ' reading file... ok.'
   endif
   sync all !<-- in the meantime, the others wait for image 1 to finish its work.

   n[me] = n[1] + me !<-- Broadcast read value to all other images (1-myself included)

  ! print results
   do i_ = 1, nimgs
      if (me == i_) write(*, *) ' Image ', me, ' : n[me] = ', n[me]
      sync all
   enddo
end program

```

Hope this helps.

Regards.

---

<div class="post-metadata">

**Author:** ![apoorv01](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@apoorv01](https://fortran-lang.discourse.group/u/apoorv01)\
**Post date:** [August 27, 2023, 3:28pm UTC](https://fortran-lang.discourse.group/t/coarray-reading-file/6403/4 "2023-08-27T15:28:48Z")

</div>

@mEm, Thank you for your suggestions. This has made me more clear about the input file handling. Let me try the real file I want to read and store data for my program.  
Best Regards  
Apurva

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [August 27, 2023, 4:53pm UTC](https://fortran-lang.discourse.group/t/coarray-reading-file/6403/5 "2023-08-27T16:53:03Z")

</div>

> [@apoorv01](#):
>
> … Can we use coarray to read and write in serial mode and do all the numerical operation in parallel mode …

1. Refer also to Modern Fortran Explained introducing Fortran 2018 for current facilities including with coarray `TEAMS`
2. Also, evaluate standard facilities with more than one Fortran processor to cross-check standard semantics and to be ultra mindful of compiler deficiences,
3. With a Fortran 2018 and newer Fortran processors, consider coarray intrinsics such as `CO_BROADCAST` for many common tasks. This can be helpful because an author can **allow the processor to do the synchronization** , as necessary.

```fortran
   integer :: n[*], i

   ! Simulate file READ
   if ( this_image() == 1 ) n = 42

   call co_broadcast( n, 1 )

   ! check values
   if ( this_image() == 1 ) then
      print "(g0,t10,g0)", "Image", "n"
      do i = 1, num_images()
         print "(g0,t10,g0)", i, n[i]
      end do
   end if

end

```

```auto
C:\temp>ifort /free /standard-semantics /Qcoarray=shared /Qcoarray-num-images=8 p.f
Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on Intel(R) 64, Version 2021.10.0 Build 20230609_000000
Copyright (C) 1985-2023 Intel Corporation. All rights reserved.

Microsoft (R) Incremental Linker Version 14.36.32537.0
Copyright (C) Microsoft Corporation. All rights reserved.

-out:p.exe
-subsystem:console
p.obj

C:\temp>p.exe
Image n
1 42
2 42
3 42
4 42
5 42
6 42
7 42
8 42

```

```auto

```

---

<div class="post-metadata">

**Author:** ![Audun](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@Audun](https://fortran-lang.discourse.group/u/Audun)\
**Post date:** [September 9, 2023, 7:44am UTC](https://fortran-lang.discourse.group/t/coarray-reading-file/6403/6 "2023-09-09T07:44:42Z")

</div>

To read a multi record file into a coarray variable with allocatable components you would have to allocate the coarray variable separately on all images, before you can use co\_broadcast?

---

<div class="post-metadata">

**Author:** ![apoorv01](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@apoorv01](https://fortran-lang.discourse.group/u/apoorv01)\
**Post date:** [September 9, 2023, 10:25am UTC](https://fortran-lang.discourse.group/t/coarray-reading-file/6403/7 "2023-09-09T10:25:11Z")

</div>

@FortranFan …, Thank you for the reply. I will refer the book mentioned
