ForTrilinos Tutorial

Similar documents
ForTrilinos: Bringing Trilinos to Object- Oriented Fortran Parallel Applica9ons

Heterogeneous Scalable Framework for Multiphase Flows

MaanavaN.Com CS1203 OBJECT ORIENTED PROGRAMMING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 1 of 7. Date: 1998/05/31 To: WG5 From: J3/interop Subject: Interoperability syntax (Part 1) References: J3/98-132r1, J3/98-139

Research Article Emulating Multiple Inheritance in Fortran 2003/2008

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

What are the characteristics of Object Oriented programming language?

Fast Introduction to Object Oriented Programming and C++

Derived Data Types in Fortran. Front Matter. Table of Contents. Introduction. Other Topics. Bruno Abreu Calfa. Last Update: September 29, / 10

C++ (Non for C Programmer) (BT307) 40 Hours

VALLIAMMAI ENGINEERING COLLEGE

Short Notes of CS201

Fortran Bill Long, Cray Inc. 21-May Cray Proprietary

CS201 - Introduction to Programming Glossary By

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

Instantiation of Template class

Object Oriented Programming with c++ Question Bank

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

Python, C, C++, and Fortran Relationship Status: It s Not That Complicated. Philip Semanchuk

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Modern Fortran OO Features

Object Oriented Programming. Solved MCQs - Part 2

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

RAJIV GANDHI COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF INFORMATION TECHNOLOGY OBJECT ORIENTED PROGRAMMING QUESTION BANK UNIT I 2 MARKS

M.C.A DEGREE EXAMINATION,NOVEMBER/DECEMBER 2010 Second Semester MC 9222-OBJECT ORIENTED PROGRAMMING (Regulation 2009)

STRUCTURING OF PROGRAM

Lecture Notes on Programming Languages

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

TS Further Interoperability of Fortran with C WG5/N1917

The new features of Fortran 2003

C++ Programming: Polymorphism

C++ Important Questions with Answers

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

COMP322 - Introduction to C++

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Chapter 5 Object-Oriented Programming

The Design Process. General Development Issues. C/C++ and OO Rules of Thumb. Home

C++\CLI. Jim Fawcett CSE687-OnLine Object Oriented Design Summer 2017

Hierarchical inheritance: Contains one base class and multiple derived classes of the same base class.

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Interfacing With Other Programming Languages Using Cython

Welcome. Modern Fortran (F77 to F90 and beyond) Virtual tutorial starts at BST

Oops known as object-oriented programming language system is the main feature of C# which further support the major features of oops including:

Evaluation Issues in Generic Programming with Inheritance and Templates in C++

CMSC 330: Organization of Programming Languages

C++ Coding Standards. 101 Rules, Guidelines, and Best Practices. Herb Sutter Andrei Alexandrescu. Boston. 'Y.'YAddison-Wesley

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Polymorphism. Zimmer CSCI 330

Compaq Interview Questions And Answers

B.C.A 2017 OBJECT ORIENTED PROGRAMMING USING C++ BCA303T MODULE SPECIFICATION SHEET

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

September 10,

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2015 EOM, HYEONSANG ALL RIGHTS RESERVED


(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

Cross-compiling C++ to JavaScript. Challenges in porting the join.me common library to HTML5

Introduce C# as Object Oriented programming language. Explain, tokens,

Object Oriented Paradigm

Table of Contents Preface Bare Necessities... 17

Borland 105, 278, 361, 1135 Bounded array Branch instruction 7 break statement 170 BTree 873 Building a project 117 Built in data types 126

Programming, numerics and optimization

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

Concepts of Object Oriented Programming

Inheritance, Polymorphism and the Object Memory Model

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli

Absolute C++ Walter Savitch

Object-Oriented Design

Wrapping a complex C++ library for Eiffel. FINAL REPORT July 1 st, 2005

1 Motivating Questions. 2 An Illustrating Example. 3 Software Quality. 4 An Example of OOP

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

The Foundation of C++: The C Subset An Overview of C p. 3 The Origins and History of C p. 4 C Is a Middle-Level Language p. 5 C Is a Structured

Data Abstraction. Hwansoo Han

Hydro-dynamics in CUDA Performance optimizations in MOHID

Interfacing Chapel with traditional HPC programming languages

QUIZ Friends class Y;

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

CGS 2405 Advanced Programming with C++ Course Justification

Object-Oriented Programming

Introduction to C++ Systems Programming

Fundamentals of Programming Languages

Evolution of Fortran. Presented by: Tauqeer Ahmad. Seminar on Languages for Scientific Computing

Programming in C and C++

CIS 190: C/C++ Programming. Classes in C++

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Fortran classes and data visibility

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

Polymorphism. Contents. Assignment to Derived Class Object. Assignment to Base Class Object

Introduction to Design Patterns

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

CSE 452: Programming Languages. Previous Lecture. From ADTs to OOP. Data Abstraction and Object-Orientation

CS250 Final Review Questions

Transcription:

ForTrilinos Tutorial Damian Rouson, Nicole Lemaster Sandia National Laboratories Karla Morris, Xiaofeng Xu City University of New York Salvatore Filippone University of Rome Jim Xia IBM Sponsors: DOE/OASCR, ONR November 2, 2010 SAND2010-7764C Unlimited Release Sandia is a multiprogram laboratory operated by Sandia Corporation, a Lockheed Martin Company, for the United States Department of Energy under contract DE-AC04-94AL85000.

Outline Motivation and objectives Methodology Shadow object interface (Gray et al. 1999) ForTrilinos application software stack Application prototype Simple main Prototypical PDE solver

Motivation Fortran Apps Currently Use Custom Wrappers for Trilinos High Order Method Modeling Environment (HOMME) atmospheric dynamics solver Parallel Ocean Program (POP) Glimmer ice sheet model Multiphase Flow with Interphase exchanges (MFIX)

Customized Procedural Interface Advantages Gets the job done Does not required extensive lines of additional code Disadvantages Requires flattening the data (reduced to intrinsic types & 1D arrays thereof) Requires flattening the functions that act on the data Requires hardwiring assumptions into receiving code Increases software complexity Low portability, reusability & maintainability

Interface Construction Approach Specific Language Pairing Simplified Wrapper and Interface Generator (SWIG): Parses C/C++ code to create bindings in scripting language Interfaces C/C++ code with high level languages No Fortran interoperability Scientific Interface Definition Language (SIDL) Babel Developer must define application programming interfaces (API) for given source code Generates code stubs for code interoperability No extensible derived types in Fortran 2003

Chasm: Interface Construction Approach (cont.) Parses C/C++ and Fortran source code to generate its XML description Uses XML stylesheet transformation (XSLT) to create binding code No Fortran 2003 support Open Fortran Parser: Parses Fortran 2003 code and automatically generates bindings for Fortran and C No C++ support

Objectives To increase the adoption of Trilinos throughout DOE research communities that principally write Fortran, e.g. climate & combustion researchers. To maintain the OOP philosophy of the Trilinos project while using idioms that feel natural to Fortran programmers.

Outline Motivation and objectives Methodology Shadow object interface (Gray et al. 1999) ForTrilinos application software stack Application prototype Sample main Prototypical PDE solver

Gray s Shadow Object Interface Interface OO C++ and object based Fortran 95 Flat interface exports real object behavior Shadow object is a logical interface, can be treated as a native object Real Object Flat Physical Interface Shadow Object Language A Language B

Gray s Shadow Object Interface (cont.) Constructing a physical interface requires: Unmangling procedure names Limit C++ physical interfaces to extern "C"{}-bracketed C++ procedures with lower case names and Fortran external procedure Flattening interfaces with interoperable built-in data types Pass only matching intrinsic data types C++ arguments are passed as instances of an opaque pointer class template Ensuring code initialization C/C++ extern const and static objects Fortran save attribute objects

ForTrilinos/CTrilinos Shadow Object Interface No type-system ambiguity No case restrictions on C++ and the external procedures on Fortran No required passing by reference No OOP limitation

Features of Fortran 2003 Object Oriented Support Inheritance Polymorphism (static and dynamic) Encapsulation & information hiding Interfaces (abstract derived types) C interoperability Enumerators

Application Software Stack Application Object-Oriented Interface Extensible Objects ForTrilinos Procedural Bindings C Headers Shadow interopable Fortran/C structures CTrilinos Extern C Bindings Trilinos Flattened C++ data structures & procedures Distributed C++ objects

Sample Fortran Main program main! use iso_c_binding, only : c_double! use Fepetra_MpiComm, only : epetra_mpicomm! use FEpetra_Map, only : epetra_map! use FEpetra_Vector, only : epetra_vector! implicit none! type(epetra_mpicomm) :: comm! type(epetra_map) :: map! type(epetra_vector) :: b!! MPI startup lines ommitted! comm = epetra_mpicomm()! map = epetra_map(numglobalelements=64,indexbase=1,comm=comm)! b = epetra_vector(map)! call b%putscalar(2.0_c_double)! print *, "L2 norm of b = ",b%norm2()!! MPI shutdown lines ommitted! end program!

Procedural Bindings Trilinos Epetra_MultiVector object!int Random();! Ctrilinos exports: C wrapper prototype int Epetra_MultiVector_Random(CT_Epetra_MultiVector_ID_t selfid);! Corresponding Fortran interface block interface! integer(c_int) function Epetra_MultiVector_Random(selfID) &! bind(c,name='epetra_multivector_random')! import :: c_int,ft_epetra_multivector_id_t! type(ft_epetra_multivector_id_t), intent(in), value :: selfid! end function! end interface!

Procedural Bindings (cont.) ForTrilinos derived type definition!type, bind(c) :: FT_Epetra_MultiVector_ID_t!!! private!!! integer(fortrilinos_table_id_t) :: table!!! integer(c_int) :: index!!! integer(ft_boolean_t) :: is_const!!end type! CTrilinos corresponding C struct typedef struct {!!CTrilinos_Table_ID_t table;/*table holding object reference*/!!int index;!!!!! /*Array index of the object*/!!boolean is_const; /*Whether object was declared const*/! }CT_Epetra_MultiVector_ID_t;petra_MultiVector_ID_t;!

Procedural Bindings module ForTrilinos_enums! use iso_c_binding,only : c_int! enum,bind(c) enumerator ::! FT_Epetra_SerialComm_ID, &! FT_Epetra_Comm_ID, &! FT_Epetra_MpiComm_ID! end enum! integer(kind(c_int)),parameter :: ForTrilinos_Type_ID_t= c_int! integer(kind(c_int)),parameter :: FT_boolean_t = c_int! integer(ft_boolean_t),parameter :: FT_FALSE = 0! integer(ft_boolean_t),parameter :: FT_TRUE = 1! type,bind(c) :: ForTrilinos_Object_ID_t! integer(fortrilinos_type_id_t) :: table! Object data type! integer(c_int) :: index! Array index! integer(ft_boolean_t) :: is_const! Const status! end type!! Additional structurally equivalent types! end module!

CTrilinos Capabilities not supported by C: Object method invocation Inheritance Polymorphism Overloaded function names and operators Default argument values Class and function templates String and bool types Exception handling Safe type-casting

CTrilinos Invoking object methods from C: C++ Trilinos code!!epetra_multivector mv(...);!!!mv.random(); C wrapper possible code STOP EXTREMELY DANGEROUS MANEUVER!!Code in STOP C using EXTREMELY raw pointers DANGEROUS.! MANEUVER STOP EXTREMELY DANGEROUS MANEUVER Ctrilinos wrapper!!epetra_multivector_random(mv_id);!

CTrilinos Object Construction: Type-specific struct ID!!typedef struct {!!! CTrilinos_Table_ID_t table;!!! int index!!! boolean is_const;!!} CTrilinos_Universal_ID_t; table: indentifies table holding reference to the object index: entry in table for specific object is_const: states if the object is constant or not Object Destruction: Epetra_MultiVector_Destroy(&mv_id)!

Inheritance Hierarchy for Epetra_RowMatrix Class implements aggregates aggregates implements

CTrilinos Function Overloading and Inheritance: C++ Trilinos code!!epetra_crsmatrix *A = new Epetra_CrsMatrix(...);!!!Epetra_JadMatrix *B = new Epetra_JadMatrix(...);!!!A->TwoRowMatrixOp(B);! C wrapper possible code!ct_epetra_crsmatrix_id_t A;!!CT_Epetra_JadMatrix_ID_t B;!!A = Epetra_CrsMatrix_Create(...);!!B = Epetra_JadMatrix_Create(...);!!Epetra_RowMatrix_TwoRowMatrixOp_Crs_Jad(A,B);!

Union type definition: CTrilinos typedef union{!!!!ctrilinos_universal_id_t universal;!!!!ct_epetra_crsmatrix_id_t Epetra_CrsMatrix;!!!!CT_Epetra_BLAS_ID_t Epetra_BLAS;!!!!CT_Epetra_CompObject_ID_t Epetra_CompObject;!!!!CT_Epetra_DistObject_ID_t Epetra_DistObject;!!!!CT_Epetra_Object_ID_t Epetra_Object;!!!!CT_Epetra_Operator_ID_t Epetra_Operator;!!!!CT_Epetra_RowMatrix_ID_t EPetra_RowMatrix;!!!!CT_Epetra_SrcDistObject_ID_t Epetra_SrcDistObject;! } CT_Epetra_CrsMatrix_ID_Flex_t;!

CTrilinos Function Overloading and Inheritance: CTrilinos wrapper! CT_Epetra_CrsMatrix_ID_Flex_t A;! CT_Epetra_JadMatrix_ID_Flex_t B;! A.Epetra_CrsMatrix = Epetra_CrsMatrix_Create(...);! B.Epetra_JadMatrix = Epetra_JadMatrix_Create(...);! Epetra_RowMatrix_TwoRowMatrixOp! (A.Epetra_RowMatrix,B.Epetra_RowMatrix);!

ForTrilinos Re-introduces OOP capabilities Information hiding Encapsulation Inheritance Static and dynamic polymorphism Fortran 2003 support allows Abstract derived types User-defined assignments and operators

ForTrilinos Object construction/destruction in Fortran 2003: program main!!use field_module,only : field!!implicit none!!type(field) :: u!!u = field()! end!

Reference Counting

ForTrilinos Inheritance and Polymorphism:!

Inheritance: ForTrilinos module FEpetra_Vector! use FEpetra_MultiVector, only: FEpetra_MultiVector! private! Hide everything by default! public :: Epetra_Vector! Expose type/constructors/methods! implicit none! type,extends(epetra_multivector) :: Epetra_Vector! private! type(ft_epetra_vector_id_t) :: vector_id! contains! procedure :: ctrilinos_delete =>ctrilinos_delete_epetravector! procedure :: get_epetravector_id! procedure,nopass :: alias_epetravector_id! procedure :: generalize!! ( other type bound procedures )!

ForTrilinos Inheritance:! interface Epetra_Vector! constructors! module procedure from_id, from_scratch! end interface! contains! type(epetra_vector) function from_id(id)! type(ft_epetra_vector_id_t),intent(in) :: id! from_id%vector_id = id! from_id%epetra_multivector=epetra_multivector( from_struct%alias_epetramultivector_id(from_struct%generalize ()))! call from_id%register_self! end function!!...! &!

ForTrilinos Inheritance:! type(epetra_vector) function from_scratch(...)! type(ft_epetra_vector_id_t) :: from_scratch_id! from_scratch_id = Epetra_Vector_Create(...)! C wrapper! from_scratch = from_id(from_scratch_id)! end function! subroutine ctrilinos_delete_epetravector(this)! class(epetra_vector),intent(inout) :: this! call Epetra_Vector_Destroy(this%vector_id)! end subroutine!!...! end module!

ForTrilinos Inheritance:! program main!! use FEpetra_Vector, only : Epetra_Vector!! use iso_c_binding, only : c_double!! type(epetra_vector) :: A! real(c_double), allocatable :: Anorm! A=Epetra_Vector(...)! Anorm=A%Norm2()! end main!

ForTrilinos Polymorphism: type(epetra_crsmatrix) :: A! type(epetra_jadmatrix) :: B! A=Epetra_CrsMatrix(...)! B=Epetra_JadMatrix(...)! call A%TwoRowMatrixOp(B)!

Object-Oriented Fortran Interface module FEpetra_Vector! use forepetra! Procedural bindings!!...! private! Hide everything by default! public :: Epetra_Vector! Expose type/constructors/methods! implicit none! type,extends(epetra_multivector) :: Epetra_Vector! private! type(ft_epetra_vector_id_t) :: vector_id! contains! procedure :: ReplaceGlobalValues! procedure :: ExtractCopy_EpetraVector! generic :: ExtractCopy => ExtractCopy_EpetraVector! procedure :: get_element_epetravector! generic :: get_element => get_element_epetravector! end type! interface Epetra_MultiVector!! Specific constructor names! end interface! contains! Method impelementations!

Outline Motivation and objectives Methodology Shadow object interface (Gray et al. 1999) ForTrilinos application software stack Application prototype Simple main Prototypical PDE solver

ForTrilinos Application Prototype Burgers Equation: u t + uu x =νu xx u = u(x,t) ν : velocity field : diffusion coefficient Abstract Calculus Pattern: du_dt = nu*u%xx() - u*u%x()! Synchronization Asynchronous, purely functional operators and methods.

Burgers Solver Architecture

Burgers Equation Solver program main! #include "ForTrilinos_config.h"! #ifdef HAVE_MPI! use mpi! use FEpetra_MpiComm,only:Epetra_MpiComm! #else! use FEpetra_SerialComm,only:Epetra_SerialComm! #endif! use ForTrilinos_utils, only : valid_kind_parameters! use iso_c_binding, only : c_int,c_double! use field_module, only:initial_field! use periodic_2nd_order_module, only : periodic_2nd_order! use initializer,only : u_initial,zero! implicit none!!...!

Burgers Equation Solver (cont.) #ifdef HAVE_MPI! type(epetra_mpicomm) :: comm! #else! type(epetra_serialcomm) :: comm! #endif! type(periodic_2nd_order) :: u,half_uu,u_half! procedure(initial_field),pointer :: initial!!...! #ifdef HAVE_MPI! call MPI_INIT(ierr)! comm = Epetra_MpiComm(MPI_COMM_WORLD)! #else! comm = Epetra_SerialComm()! #endif!

Burgers Equation Solver (cont.) initial => u_initial! u = periodic_2nd_order(initial,grid_resolution,comm)! initial => zero! half_uu = periodic_2nd_order(initial,grid_resolution,comm)! u_half = periodic_2nd_order(initial,grid_resolution,comm)! do tstep=1,1000!2nd-order Runge-Kutta:! dt = u%runge_kutta_2nd_step(nu,grid_resolution)! half_uu = u*u*half! u_half = u + (u%xx()*nu - half_uu%x())*dt*half! 1st substep! half_uu = u_half*u_half*half! u = u + (u_half%xx()*nu - half_uu%x())*dt t = t + dt! end do!! 2nd substep!

Burgers Equation Solver (cont.) call half_uu%force_finalize! call u_half%force_finalize! call u%force_finalize! call comm%force_finalize! #ifdef HAVE_MPI! call MPI_FINALIZE(rc)! #endif! end program!

Sample Operator function multiple(lhs,rhs)! class(periodic_2nd_order),intent(in) :: lhs! real(c_double),intent(in) :: rhs! class(field),allocatable :: multiple! type(periodic_2nd_order),allocatable ::local_multiple! type(error) :: ierr! allocate(periodic_2nd_order::local_multiple)! local_multiple%f=epetra_vector(map,.true.)! call local_multiple%f%scale(rhs,lhs%f,ierr)!!...! call move_alloc(local_multiple,multiple)! end function!

ForTrilinos Application Prototype Burgers equation solver performance