To tell the images what to work on, we first need to know more about the images themselves, specifically how many there are and who they are. Fortran provides the functions this_image and num_images to do exactly this. Here’s how we write the image number and total number of images to screen:
program hello_images
print *, 'I am image', this_image(), 'of', num_images()
end program hello_images
Let’s store this into a file, hello_images.f 90, compile it, and run it:
caf hello_images.f90 -o hello_images cafrun -n 4 hello_images I am image 1 of 4 I am image 2 of 4 I am image 3 of 4 I am image 4 of 4
Based on this, you probably get the idea how these functions work, but let’s go over the details:
this_image–A built-in function that, when called without arguments, returns an integer index of the current image. Fortran array indices start from 1 (unlike C or Python, which start from 0), andthis_imagefollows the same convention. For now, this is all you need to know aboutthis_image. We’ll explore a bit more advanced use in chapter 12.num_images–A built-in function that takes no arguments and returns an integer total number of images. The result of this function will always match the argument number of images passed tocafrun-n.
Both this_image and num_images are automatically available in any Fortran program–they don’t need to be imported from a module. In case you have some experience with parallel programming with MPI, these functions are the analogs of the mpi_comm_rank and mpi_comm_size subroutines.