| [004075f] | 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 |
|
|---|
| 19 | #define NRA 62 /* number of rows in matrix A */
|
|---|
| 20 | #define NCA 15 /* number of columns in matrix A */
|
|---|
| 21 | #define NCB 7 /* number of columns in matrix B */
|
|---|
| 22 |
|
|---|
| 23 | int main (int argc, char *argv[])
|
|---|
| 24 | {
|
|---|
| 25 | int tid, nthreads, i, j, k, chunk;
|
|---|
| 26 | double a[NRA][NCA], /* matrix A to be multiplied */
|
|---|
| 27 | b[NCA][NCB], /* matrix B to be multiplied */
|
|---|
| 28 | c[NRA][NCB]; /* result matrix C */
|
|---|
| 29 |
|
|---|
| 30 | chunk = 10; /* set loop iteration chunk size */
|
|---|
| 31 |
|
|---|
| 32 | /*** Spawn a parallel region explicitly scoping all variables ***/
|
|---|
| 33 | {
|
|---|
| 34 | tid = omp_get_thread_num();
|
|---|
| 35 | if (tid == 0)
|
|---|
| 36 | {
|
|---|
| 37 | nthreads = omp_get_num_threads();
|
|---|
| 38 | printf("Starting matrix multiple example with %d threads\n",nthreads);
|
|---|
| 39 | printf("Initializing matrices...\n");
|
|---|
| 40 | }
|
|---|
| 41 | /*** Initialize matrices ***/
|
|---|
| 42 | for (i=0; i<NRA; i++)
|
|---|
| 43 | for (j=0; j<NCA; j++)
|
|---|
| 44 | a[i][j]= i+j;
|
|---|
| 45 | for (i=0; i<NCA; i++)
|
|---|
| 46 | for (j=0; j<NCB; j++)
|
|---|
| 47 | b[i][j]= i*j;
|
|---|
| 48 | for (i=0; i<NRA; i++)
|
|---|
| 49 | for (j=0; j<NCB; j++)
|
|---|
| 50 | c[i][j]= 0;
|
|---|
| 51 |
|
|---|
| 52 | /*** Do matrix multiply sharing iterations on outer loop ***/
|
|---|
| 53 | /*** Display who does which iterations for demonstration purposes ***/
|
|---|
| 54 | printf("Thread %d starting matrix multiply...\n",tid);
|
|---|
| 55 | for (i=0; i<NRA; i++)
|
|---|
| 56 | {
|
|---|
| 57 | printf("Thread=%d did row=%d\n",tid,i);
|
|---|
| 58 | for(j=0; j<NCB; j++)
|
|---|
| 59 | for (k=0; k<NCA; k++)
|
|---|
| 60 | c[i][j] += a[i][k] * b[k][j];
|
|---|
| 61 | }
|
|---|
| 62 | } /*** End of parallel region ***/
|
|---|
| 63 |
|
|---|
| 64 | /*** Print results ***/
|
|---|
| 65 | printf("******************************************************\n");
|
|---|
| 66 | printf("Result Matrix:\n");
|
|---|
| 67 | for (i=0; i<NRA; i++)
|
|---|
| 68 | {
|
|---|
| 69 | for (j=0; j<NCB; j++)
|
|---|
| 70 | printf("%6.2f ", c[i][j]);
|
|---|
| 71 | printf("\n");
|
|---|
| 72 | }
|
|---|
| 73 | printf("******************************************************\n");
|
|---|
| 74 | printf ("Done.\n");
|
|---|
| 75 |
|
|---|
| 76 | }
|
|---|