| [f2097b0] | 1 | /******************************************************************************
|
|---|
| 2 | * FILE: omp_mm.c
|
|---|
| 3 | * DESCRIPTION:
|
|---|
| 4 | * OpenMp Example - Matrix Multiply - C Version
|
|---|
| 5 | * Demonstrates a matrix multiply using OpenMP. Threads share row iterations
|
|---|
| 6 | * according to a predefined chunk size.
|
|---|
| 7 | * AUTHOR: Blaise Barney
|
|---|
| 8 | * LAST REVISED: 06/28/05
|
|---|
| 9 | ******************************************************************************/
|
|---|
| 10 | /**
|
|---|
| 11 | * This program performs the multiplication of two matrix's.
|
|---|
| 12 | * Online source:
|
|---|
| 13 | * https://computing.llnl.gov/tutorials/openMP/samples/C/omp_mm.c
|
|---|
| 14 | **/
|
|---|
| 15 | #include <omp.h>
|
|---|
| 16 | #include <stdio.h>
|
|---|
| 17 | #include <stdlib.h>
|
|---|
| 18 |
|
|---|
| [6d6ae23] | 19 | #ifdef _CIVL
|
|---|
| [20ac35f] | 20 | $input int NRA=5; /* number of rows in matrix A */
|
|---|
| 21 | $input int NCA=5; /* number of columns in matrix A */
|
|---|
| 22 | $input int NCB=5; /* number of columns in matrix B */
|
|---|
| [6d6ae23] | 23 | #else
|
|---|
| 24 | #define NRA 8 /* number of rows in matrix A */
|
|---|
| [dccd621] | 25 | #define NCA 8 /* number of columns in matrix A */
|
|---|
| 26 | #define NCB 8 /* number of columns in matrix B */
|
|---|
| [6d6ae23] | 27 | #endif
|
|---|
| [f2097b0] | 28 |
|
|---|
| 29 | int main (int argc, char *argv[])
|
|---|
| 30 | {
|
|---|
| 31 | int tid, nthreads, i, j, k, chunk;
|
|---|
| 32 | double a[NRA][NCA], /* matrix A to be multiplied */
|
|---|
| 33 | b[NCA][NCB], /* matrix B to be multiplied */
|
|---|
| 34 | c[NRA][NCB]; /* result matrix C */
|
|---|
| 35 |
|
|---|
| 36 | chunk = 10; /* set loop iteration chunk size */
|
|---|
| 37 |
|
|---|
| 38 | /*** Spawn a parallel region explicitly scoping all variables ***/
|
|---|
| [e80ab56] | 39 | #pragma omp parallel shared(a,b,c,chunk) private(tid,i,j,k,nthreads)
|
|---|
| [f2097b0] | 40 | {
|
|---|
| 41 | tid = omp_get_thread_num();
|
|---|
| 42 | if (tid == 0)
|
|---|
| 43 | {
|
|---|
| 44 | nthreads = omp_get_num_threads();
|
|---|
| 45 | printf("Starting matrix multiple example with %d threads\n",nthreads);
|
|---|
| 46 | printf("Initializing matrices...\n");
|
|---|
| 47 | }
|
|---|
| 48 | /*** Initialize matrices ***/
|
|---|
| 49 | #pragma omp for schedule (static, chunk)
|
|---|
| 50 | for (i=0; i<NRA; i++)
|
|---|
| 51 | for (j=0; j<NCA; j++)
|
|---|
| 52 | a[i][j]= i+j;
|
|---|
| 53 | #pragma omp for schedule (static, chunk)
|
|---|
| 54 | for (i=0; i<NCA; i++)
|
|---|
| 55 | for (j=0; j<NCB; j++)
|
|---|
| 56 | b[i][j]= i*j;
|
|---|
| 57 | #pragma omp for schedule (static, chunk)
|
|---|
| 58 | for (i=0; i<NRA; i++)
|
|---|
| 59 | for (j=0; j<NCB; j++)
|
|---|
| 60 | c[i][j]= 0;
|
|---|
| 61 |
|
|---|
| 62 | /*** Do matrix multiply sharing iterations on outer loop ***/
|
|---|
| 63 | /*** Display who does which iterations for demonstration purposes ***/
|
|---|
| 64 | printf("Thread %d starting matrix multiply...\n",tid);
|
|---|
| 65 | #pragma omp for schedule (static, chunk)
|
|---|
| 66 | for (i=0; i<NRA; i++)
|
|---|
| 67 | {
|
|---|
| 68 | printf("Thread=%d did row=%d\n",tid,i);
|
|---|
| 69 | for(j=0; j<NCB; j++)
|
|---|
| 70 | for (k=0; k<NCA; k++)
|
|---|
| 71 | c[i][j] += a[i][k] * b[k][j];
|
|---|
| 72 | }
|
|---|
| 73 | } /*** End of parallel region ***/
|
|---|
| 74 |
|
|---|
| 75 | /*** Print results ***/
|
|---|
| 76 | printf("******************************************************\n");
|
|---|
| 77 | printf("Result Matrix:\n");
|
|---|
| 78 | for (i=0; i<NRA; i++)
|
|---|
| 79 | {
|
|---|
| 80 | for (j=0; j<NCB; j++)
|
|---|
| 81 | printf("%6.2f ", c[i][j]);
|
|---|
| 82 | printf("\n");
|
|---|
| 83 | }
|
|---|
| 84 | printf("******************************************************\n");
|
|---|
| 85 | printf ("Done.\n");
|
|---|
| 86 |
|
|---|
| 87 | }
|
|---|