Fortran

Guide To Learn

Advanced Fortran use

Accessing derived type components

Once we have a class instance declared and initialized, in most cases we’ll want to access its components, often to read their values, sometimes to modify them. After all, if we use type components to store data that’s specific to the instance, there’s no use for it unless we can access it in some way. […]

Instantiating a derived type

Defining a derived type was the first necessary step, but to actually use it, we need to instantiate one. Instantiating means nothing more than creating a new instance. It’s kind of an unwieldy word, but the more you use it, the more natural it becomes. In listing 8.2, I used a shortcut to declare and initialize a […]

Defining a derived type

Now that you see where we’re going, let’s take a few steps back and start simple and slow. The following snippet demonstrates the syntax to define a new derived type: ❶ Specifies derived type name ❷ Declares a component variable ❸ We can have as many of these as we want. ❹ Closes the type definition block The type […]

Defining, declaring, and initializing derived types

Perhaps the greatest strength of an object-oriented approach to development is that it offers a mechanism that captures a model of the real world.   –Grady Booch (1986) Let’s start with a small program that makes use of a derived type. We’ll define a Person type and assign to it a greeting subroutine, which will do nothing more […]

Recasting the tsunami simulator with derived types

The main goal of this chapter is to apply derived types toward refactoring our parallel tsunami simulator. Specifically, we’ll use them to abstract away the low-level code to allocate and manipulate data arrays, as well as to black-box much of the tedious code used for setting up the data structures. Derived types will help us […]

Exercise 2: Writing a function that returns the indices of neighbor images

If you’ve practiced using the this_image and num_images built-in functions long enough, the solution will be straightforward. For any image that calls this function, its left neighbor will be this_image() – 1, and its right neighbor this_image() + 1. (Except when this_image() is at the boundary, that is, this_image() == 1 or this_image() == num_images().) The following listing provides the solution. Listing 7.9 Calculating the index of neighbor images ❶ Two-element integer array to store the result […]

Exercise 1: Finding the array subranges on each image

We need a function that returns a start and end index for each image, given the integer size of the global array to be decomposed. For example, for an input 100 and total number of images 2, the result should be [1, 50] on image 1 and [51, 100] on image 2. When the input is divisible by num_images(), the solution is straightforward. […]

Scroll to top