
Computational Model Computational Methods for Oil Recovery PASI: Scientific Computing in the Americas The Challenge of Massive Parallelism Luis M. de la Cruz Salas Instituto de Geof´ısica Universidad Nacional Aut´onomade M´exico January 2011 Valpara´ıso,Chile Comp EOR LMCS 1 / 47 Computational Model Table of contents 1 Computational Model OOP & Generic Programming TUNA CUDA & Seldon Final Remarks Comp EOR LMCS 2 / 47 Modular Programming Module: contains a set of procedures along the data they work on (F90, C++, ::: ). Object{Oriented Programming Construction of user defined Abstract Data Types (ADT), called classes of objects. Behaviour and data are encapsulated in classes (C++, Java, F90+). Generic Programming We focus on the implementation of models defined by a set of requirements bundled into a concept (C++). E. g. the study of linear algebra algorithms produce Matrix and Vector concepts. Computational Model Structured Programming Subroutines, Functions, Procedures (C, F77, ::: ) Comp EOR LMCS 3 / 47 Object{Oriented Programming Construction of user defined Abstract Data Types (ADT), called classes of objects. Behaviour and data are encapsulated in classes (C++, Java, F90+). Generic Programming We focus on the implementation of models defined by a set of requirements bundled into a concept (C++). E. g. the study of linear algebra algorithms produce Matrix and Vector concepts. Computational Model Structured Programming Subroutines, Functions, Procedures (C, F77, ::: ) Modular Programming Module: contains a set of procedures along the data they work on (F90, C++, ::: ). Comp EOR LMCS 3 / 47 Generic Programming We focus on the implementation of models defined by a set of requirements bundled into a concept (C++). E. g. the study of linear algebra algorithms produce Matrix and Vector concepts. Computational Model Structured Programming Subroutines, Functions, Procedures (C, F77, ::: ) Modular Programming Module: contains a set of procedures along the data they work on (F90, C++, ::: ). Object{Oriented Programming Construction of user defined Abstract Data Types (ADT), called classes of objects. Behaviour and data are encapsulated in classes (C++, Java, F90+). Comp EOR LMCS 3 / 47 Computational Model Structured Programming Subroutines, Functions, Procedures (C, F77, ::: ) Modular Programming Module: contains a set of procedures along the data they work on (F90, C++, ::: ). Object{Oriented Programming Construction of user defined Abstract Data Types (ADT), called classes of objects. Behaviour and data are encapsulated in classes (C++, Java, F90+). Generic Programming We focus on the implementation of models defined by a set of requirements bundled into a concept (C++). E. g. the study of linear algebra algorithms produce Matrix and Vector concepts. Comp EOR LMCS 3 / 47 Computational Model OOP & Generic Programming Table of contents 1 Computational Model OOP & Generic Programming TUNA CUDA & Seldon Final Remarks Comp EOR LMCS 4 / 47 Computational Model OOP & Generic Programming Generic Programming (GP)I To developing efficient and reusable software libraries [6]. Main idea: many algorithms can be abstracted away from the particular data structures on which they operate. For example: The algorithm accumulate(), which successively applies a binary operator to each element of a container. Typical use would be to sum the elements of a container, using the addition operator. accumulate() algorithm in C++: t e m p l a t e < class InputIterator , c l a s s T > double x[10]; T accumulate(InputIterator first , v e c t o r <double> y ( 1 0 ) ; InputIterator last , l i s t <complex<double> > z ( 1 0 ) ; T i n i t ) f a=accumulate(x, x+10, 0.0); for (; first != last; ++first) b=accumulate(y.begin() ,y.end() ,0.0); i n i t = i n i t + ∗ f i r s t ; c=accumulate(z.begin() ,z.end() ,0.0); return init; g Comp EOR LMCS 5 / 47 Computational Model OOP & Generic Programming Generic Programming (GP)II The algorithm can be used with any container that exports the InputIterator interface. An InputIterator can be de-referenced. An InputIterator can be incremented. GP process (lifting): Identify useful and efficient algorithms. Focus on finding commonality among similar implementations of the same algorithm, and then find its generic representation. Derive a set of (minimal) requirements that allow these algorithms to run and run efficiently. Construct a framework based on classifications of requeriments. Lifting process provide us with a suitable abstractions so that a single, generic algorithm can cover many concrete implementations. Comp EOR LMCS 6 / 47 Computational Model OOP & Generic Programming Generic Programming (GP) III Concepts : describe a set of abstractions, each of which meets all of the requirements. Graph algorithms will produce Graph concepts. Linear algebra algorithms will produce Matrix and Vector concepts. Model : is an abstraction that meets all requirements. Summary: GP process give us a better understanding of the problem domain. GP is the creation of a systematic organization of abstract and efficient software components. GP is to program with concepts. GP is not about languages features. Standard Template Library (STL) uses generic programming. THRUST use many concepts from STL. Comp EOR LMCS 7 / 47 Computational Model OOP & Generic Programming Classes and Objects A C++ Implementation while ( t <= Tmax) f IMPES Algorithm pressure. calcCoefficients (); Solver ::TDMA1D( pressure ); 1: while (t < Tmax) do pressure .update(); 2: Calc. coeff. of pressure equation. 3: Solve the pressure equation implicitly. saturation. calcCoefficients (); 4: Calc. coeff. of saturation equation. Solver :: solExplicit(saturation); 5: Solve the saturation eq. explicitly. saturation .update(); 6: t t + ∆t 7: end while t += dt ; g An object has a name, attributes and behaviour. A class is a construct that is used as a blueprint to create objects. An object of a given class is called an instance of the class. Comp EOR LMCS 8 / 47 Computational Model OOP & Generic Programming Class declaration class Vector f class Matrix f p r i v a t e : p r i v a t e : i n t s i z e ; int rows, cols; double ∗ data ; Vector ∗ d v e c t o r s ; p u b l i c : p u b l i c : Vector ( ) ; Matrix(int , int); Vector(int ); Matrix(const Matrix&); Vector(const Vector&); int getCols() const; int getDimension() const; int getRows() const void setDimension(int ); void setCols(int); void resize(int); void setRows(); double& operator()(int i) const g ; f return data[i ]; g ; g ; Vector x(10), y(10), z(10); Matrix A(10,10), B(10,10); Vector& operator+(const Vector&, z = x + y ; const Vector); A = B ∗ z ; Comp EOR LMCS 9 / 47 Computational Model OOP & Generic Programming Templates Classes Functions template<typename T> class Vector f t e m p l a t e <typename T> p r i v a t e : inline T const& max (T const& a, i n t s i z e ; T c o n s t& b ) T ∗ data ; f p u b l i c : r e t u r n a < b ? b : a ; Vector ( ) ; g Vector(int ); Vector(const Vector&); i n t main ( ) f int getDimension() const; i n t i = 4 2 ; void setDimension(int ); ::max(7,i); //max<i n t >(7 , i ) void resize(int); double f1 = 3.4; T& operator()(int i) const double f 2 = −6.7; f return data[i ]; g ; ::max(f1,f2); //max<double >(f1 , f 2 ) //... string s1 = "mathematics"; g ; string s2 = "math"; //... ::max(s1,s2); //max<s t r i n g >(s1 , s2 ) Vector<i n t > a r r a y i ( 1 0 ) ; r e t u r n 0 ; Vector<double> a r r a y d ( 1 0 0 ) ; g Substitution of type parameters during compile time: template instantation. C++ templates supports: functions, classes and member functions. Comp EOR LMCS. 10 / 47 Computational Model OOP & Generic Programming Dynamic polymorphism In C++ this is about the use of virtual functions. Virtual functions can introduce an overhead in run time: class Matrix f public: virtual double operator()(int i, int j); g ; class SymmetricMatrix : public Matrix f public: virtual double operator()(int i, int j); g ; class UpperTriangularMatrix : public Matrix f public: virtual double operator()(int i, int j); g ; //... double sum(Matrix &A) f double suma; for(int j = 0; j < A.cols(); ++j) for(int i = 0; i < A.cols(); ++i) suma += A(i ,j); return suma; g //... SymmetricMatrix A(N,N); double suma = sum(A); Comp EOR LMCS 11 / 47 Computational Model OOP & Generic Programming Engines class Symmetric f //... g ; class UpperTriangular f //... g ; template<c l a s s T engine> class Matrix f p r i v a t e : T engine engine; g ; template<c l a s s T engine> double sum(Matrix<T engine> &A) f //... g Matrix<Symmetric> A(N,N ) ; double suma = sum(A); Matrix is a template class with the T engine parameter. Symmetric and UpperTriangular are two different engines that hide the particular implementation for each kind of matrix. All engines must have the same set of operations. Comp EOR LMCS 12 / 47 Computational Model OOP & Generic Programming Curiosly Recurring Template Pattern (CRTP) [3] Also known as Barton & Nackman trick [2]. template<c l a s s T l e a f t y p e > class Matrix f p u b l i c : T leaftype& asLeaf() f return static c a s t <T l e a f t y p e &>(∗ t h i s ) ; g double operator()(int i, int j) f return asLeaf()(i ,j); g g ; class SymmetricMatrix : public Matrix<SymmetricMatrix > f //... g ; class UpperTriangularMatrix : public Matrix<UpperTriangularMatrix > f //... g ; template<c l a s s T l e a f t y p e > double sum(Matrix<T l e a f t y p e >&A); SymmetricMatrix A; double suma = sum(A); The base class takes as parameter the type of the derived class.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages54 Page
-
File Size-