| 1 | /*************************************************************************
|
|---|
| 2 | This program is part of the
|
|---|
| 3 | OpenMP Source Code Repository
|
|---|
| 4 |
|
|---|
| 5 | http://www.pcg.ull.es/ompscr/
|
|---|
| 6 | e-mail: ompscr@etsii.ull.es
|
|---|
| 7 |
|
|---|
| 8 | This program is free software; you can redistribute it and/or modify
|
|---|
| 9 | it under the terms of the GNU General Public License as published by
|
|---|
| 10 | the Free Software Foundation; either version 2 of the License, or
|
|---|
| 11 | (at your option) any later version.
|
|---|
| 12 |
|
|---|
| 13 | This program is distributed in the hope that it will be useful,
|
|---|
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 16 | GNU General Public License for more details.
|
|---|
| 17 |
|
|---|
| 18 | You should have received a copy of the GNU General Public License
|
|---|
| 19 | (LICENSE file) along with this program; if not, write to
|
|---|
| 20 | the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
|---|
| 21 | Boston, MA 02111-1307 USA
|
|---|
| 22 |
|
|---|
| 23 | FILE: c_lu.c
|
|---|
| 24 | VERSION: 1.0
|
|---|
| 25 | DATE:
|
|---|
| 26 | AUTHOR: Arturo González-Escribano
|
|---|
| 27 | COMMENTS TO: arturo@infor.uva.es
|
|---|
| 28 | DESCRIPTION:
|
|---|
| 29 | LU reduction of a 2D dense matrix
|
|---|
| 30 |
|
|---|
| 31 | COMMENTS:
|
|---|
| 32 | REFERENCES:
|
|---|
| 33 | BASIC PRAGMAS: parallel-for (stride scheduling)
|
|---|
| 34 | USAGE: ./c_lu.par <size>
|
|---|
| 35 | INPUT: The matrix has fixed innitial values:
|
|---|
| 36 | M[i][j] = 1.0 iff (i==j)
|
|---|
| 37 | M[i][j] = 1.0 + (i*numColums)+j iff (i!=j)
|
|---|
| 38 |
|
|---|
| 39 | OUTPUT: Compile with -DDEBUG to see final matrix values
|
|---|
| 40 | FILE FORMATS:
|
|---|
| 41 | RESTRICTIONS:
|
|---|
| 42 | REVISION HISTORY:
|
|---|
| 43 | **************************************************************************/
|
|---|
| 44 | #include <omp.h>
|
|---|
| 45 |
|
|---|
| 46 | #include<stdio.h>
|
|---|
| 47 | #include<stdlib.h>
|
|---|
| 48 | //#include<OmpSCR.h>
|
|---|
| 49 |
|
|---|
| 50 | #define SIZE 10
|
|---|
| 51 |
|
|---|
| 52 | /* PROTOYPES */
|
|---|
| 53 | void lu(int, int);
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 | /* MAIN: PROCESS PARAMETERS */
|
|---|
| 57 | int main(int argc, char *argv[]) {
|
|---|
| 58 | int nthreads, size;
|
|---|
| 59 | //char *argNames[1] = { "size" };
|
|---|
| 60 | //char *defaultValues[1] = { "500" };
|
|---|
| 61 | //char *timerNames[1] = { "EXE_TIME" };
|
|---|
| 62 |
|
|---|
| 63 | nthreads = 1; // omp_get_num_threads();
|
|---|
| 64 | //OSCR_init( nthreads,
|
|---|
| 65 | // "LU reduction of a dense matrix.",
|
|---|
| 66 | // NULL,
|
|---|
| 67 | // 1,
|
|---|
| 68 | // argNames,
|
|---|
| 69 | // defaultValues,
|
|---|
| 70 | // 1,
|
|---|
| 71 | // 1,
|
|---|
| 72 | // timerNames,
|
|---|
| 73 | // argc,
|
|---|
| 74 | // argv );
|
|---|
| 75 |
|
|---|
| 76 | /* 1. GET PARAMETERS */
|
|---|
| 77 | size = SIZE; //OSCR_getarg_int(1);
|
|---|
| 78 |
|
|---|
| 79 | /* 2. CALL COMPUTATION */
|
|---|
| 80 | lu(nthreads, size);
|
|---|
| 81 |
|
|---|
| 82 | /* 3. REPORT */
|
|---|
| 83 | //OSCR_report();
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 | return 0;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 | /*
|
|---|
| 91 | * LU FORWARD REDUCTION
|
|---|
| 92 | */
|
|---|
| 93 | void lu(int nthreads, int size) {
|
|---|
| 94 | /* DECLARE MATRIX AND ANCILLARY DATA STRUCTURES */
|
|---|
| 95 | double **M;
|
|---|
| 96 | double **L;
|
|---|
| 97 |
|
|---|
| 98 | /* VARIABLES */
|
|---|
| 99 | int i,j,k;
|
|---|
| 100 |
|
|---|
| 101 | /* 0. ALLOCATE MATRICES MEMORY */
|
|---|
| 102 | M = (double **)calloc(size, sizeof(double *));
|
|---|
| 103 | L = (double **)calloc(size, sizeof(double *));
|
|---|
| 104 | for (i=0; i<size; i++) {
|
|---|
| 105 | M[i] = (double *)calloc(size, sizeof(double));
|
|---|
| 106 | L[i] = (double *)calloc(size, sizeof(double));
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /* 1. INITIALIZE MATRIX */
|
|---|
| 110 | for (i=0; i<size; i++) {
|
|---|
| 111 | for (j=0; j<size; j++) {
|
|---|
| 112 | if (i==j) M[i][j]=1.0;
|
|---|
| 113 | else M[i][j]=1.0+(i*size)+j;
|
|---|
| 114 | L[i][j]=0.0;
|
|---|
| 115 | }
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | /* 3. START TIMER */
|
|---|
| 119 | //OSCR_timer_start(0);
|
|---|
| 120 |
|
|---|
| 121 | /* 4. ITERATIONS LOOP */
|
|---|
| 122 | for(k=0; k<size-1; k++) {
|
|---|
| 123 |
|
|---|
| 124 | /* 4.1. PROCESS ROWS IN PARALLEL, DISTRIBUTE WITH nthreads STRIDE */
|
|---|
| 125 | #pragma omp parallel for default(none) shared(M,L,size,k) private(i,j) schedule(static,1)
|
|---|
| 126 | for (i=k+1; i<size; i++) {
|
|---|
| 127 | /* 4.1.1. COMPUTE L COLUMN */
|
|---|
| 128 | L[i][k] = M[i][k] / M[k][k];
|
|---|
| 129 |
|
|---|
| 130 | /* 4.1.2. COMPUTE M ROW ELEMENTS */
|
|---|
| 131 | for (j=k+1; j<size; j++)
|
|---|
| 132 | M[i][j] = M[i][j] - L[i][k]*M[k][j];
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | /* 4.2. END ITERATIONS LOOP */
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 | /* 5. STOP TIMER */
|
|---|
| 140 | //OSCR_timer_stop(0);
|
|---|
| 141 |
|
|---|
| 142 | /* 6. WRITE MATRIX (DEBUG) */
|
|---|
| 143 | #ifdef DEBUG
|
|---|
| 144 | #include "debug_ML.c"
|
|---|
| 145 | #endif
|
|---|
| 146 |
|
|---|
| 147 | /* 7. END */
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|