CUDA
C/C++    Fortran   

Quick Reference

Table of contents

Basic data types and arithmetic

There is one generic array container object while the underlying data may be one of various basic types:

  • f32 real single-precision (real)
  • c32 complex single-precision (complex)
  • f64 real double-precision (double precision*)
  • c64 complex double-precision (double complex*)
  • b8 Boolean
    * Requires NVIDIA GPU with Compute Capability of 1.3 or higher You can generate matrices out on the device. The default underlying datatype is f32 (float) unless otherwise specified. Some examples:
     type(array) res
     res = randu(3, 3)     ! Uniformly distributed random matrix
     res = randn(3, 3)     ! Normally distributed  random matrix
     res = constant(1,5, 5, f32) ! Single precision matrix of all ones
     res = constant(0,4, 4, f64)! Double precision matrix of all zeros
     res = identity(2, 2)  ! Identity Matrix
    
    You can also initialize value from a host array, just by assigning it to array variables
     type(array) arr
     real, dimension(2, 2) :: a
     a(1,1) = 1
     a(2,2) = 1
     ! a is now identity matrix of width 2
     arr = a         ! Data is now on device
    
    You can get back data to host, just by assigning array variables to host variables
     type(array) arr
     real, dimension(:,:), allocatable :: b
     arr = randu(5,5) ! Generate matrix on device
     b = log(arr)     ! Return log(arr) to host
    

Basic matrix operations

Matrix operations allow you to process your data and extract the information needed. The can be broadly classified into the following

  • Statistical operations: sum, min, max, mean, var, stdev
  • Manipulation operations: sort, transpose
  • Extraction operations: lower, upper, diag
  • Concatenation operations: join, tile

Statistical operations

 type(array) A, sm, mn, mx
 ! Generate random system
 A  = randu(5,5)
 sm = sum(A) ! Get sum of elements value
 mn = min(A) ! Get minimum of elements value
 mx = max(A) ! Get maximum of elements value

Manipulation operations

 type(array) A, T, L, U
 A = randu(4, 5)  ! Random matrix
 T = transpose(A) ! Matrix transpose
 L = lower(A)     ! Extract lower matrix
 U = upper(A)     ! Extract upper matrix

Linear algebra

  • Matrix operations available: Multiplication, transpose, inverse, power
  • Matrix decompositions available: lu, qr, cholesky, singular value, eigen
  • Solving linear systems: solve The decompositions have a general form as follows Here is an example to perform inplace and out of place lu decomposition
 type(arr) A, L, U, p
 A = randu(5,5)
 call lu(L,U,p,A) ! Results are stored in L, U, p
 call lu(A)       ! L is the same as lower triangle of A, U the upper triangle of A

Some more code snippets

 type(array) A, B, X0, X, Y
 type(array) S, V, U
 A = randu(5,5)              ! Generavte random matrix
 X0 = randu(5,1)             ! Generate random matrix
 call singular(S, U, V, A)   ! singular value decomposition
 B = matmul(A, X0)           ! Generate a random system
 X = solve(A, B)             ! Solve the system
 Y = matmul(inverse(A), B)   ! Solve using inverse
 print(max(X0 - X))           ! Display max error
 print(max(X0 - Y))           ! Display max error

Array Indexing

Indexing up to four dimensions is supported with the following limitations

  • The first and second dimension indices have to be type(array)
  • The third and fourth dimension indices have to be integer scalars

The following functions are provided to help with array indexing

  • get - Used to extract a sub matrix
  • set - Used to change the values of a sub matrix
  • idx - Used to convert integer scalars and vectors for indexing
  • seq - Used to generate sequences for indexing

Code snippet to showcase indexing support

 3D indexing
 A1 = randu(3,3,2)
 A2 = constant(1,3,3,2)
 I1 = (/ 1, 3 /)
 I2 = (/ 2, 3 /)
 tmp = get(A1, idx(I1),  seq(1,3,2), 1)  ! Get rows 1 and 3 for columns 1 and 3, tile 1
 call set(A2, tmp, idx(I2), seq(1,2), 2) ! Set rows 2 and 3 for columns 1 and 2, tile 2 with tmp
 call print(A1, "A1")
 call print(tmp, "tmp")
 call print(A2, "A2")