60 likes | 184 Views
Fortran Arrays. CSCI 317 Mike Heroux. Array Notation. Interface is similar to Java/C/C++. Except: Indices are enclosed with (…) not […]. Fortran: myarray ( i ) C/C++ : myarray [ i ] Note: MYARRAY( i ), myarray ( i ) and MyArRaY ( i ) are all the same array in Fortran.
E N D
Fortran Arrays CSCI 317 Mike Heroux CSCI 317 Mike Heroux
Array Notation • Interface is similar to Java/C/C++. • Except: • Indices are enclosed with (…) not […]. • Fortran: myarray(i) • C/C++ : myarray[i] • Note: MYARRAY(i), myarray(i) and MyArRaY(i) are all the same array in Fortran. • Default index base is 1 not 0. • First Fortran element: myarray(1) • Address of myarray(1) == Address of myarray[0] (Fortran vs. C/C++) • Base can be changed: • real myarray(0:n-1) makes myarray(0) the first element. CSCI 317 Mike Heroux
2D Array • real A(3,4) • “Column Major”: • A(1,1) next to A(2,1), next to A(3,1) … • 1D in storage: • A(3,1) stored next to A(1,2) CSCI 317 Mike Heroux
Subarrays • Leading dimension: • Also called stride. • Distance between two adjacent row elements. • Example: LD = 3. • Subarray: • Described by: • Address of first element: A(2,2) • LD: 3 • Number of columns: 2 CSCI 317 Mike Heroux
Simple OMP GEMM #include <omp.h> void dgemmfkernel_(int * m, int * n, int * k, double * alpha, double * a, int * lda, double * b, int * ldb, double * beta, double * c, int * ldc); void dgemmckernel_(int * M, int * N, int * K, double *A, int * LDA, double *B, int* LDB, double *C, int * LDC) { int m = *M; int n = *N; int k = *K; intlda = *LDA; intldb = *LDB; intldc = *LDC; double alpha = 1.0; double beta = 0.0; intmlocal; inti,j,l; intnum_threads, chunksize; CSCI 317 Mike Heroux
Simple OMP GEMM (2) #pragma omp parallel num_threads = omp_get_num_threads(); /*chunksize = (m+num_threads-1)/num_threads;*/ chunksize = m/num_threads; #pragma omp parallel for for (i=0; i<m; i+=chunksize) { /*mlocal = (m-i*chunksize>chunksize) ? chunksize : m - i*chunksize;*/ mlocal = chunksize; dgemmfkernel_(&mlocal, N, K, &alpha, A+i, LDA, B, LDB, &beta, C+i, LDC); } return; } CSCI 317 Mike Heroux