source: CIVL/mods/dev.civl.abc/examples/omp/matProduct1.c

main
Last change on this file was aad342c, checked in by Stephen Siegel <siegel@…>, 3 years ago

Performing huge refactor to incorporate ABC, GMC, and SARL into CIVL repo and use Java modules.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5664 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 2.6 KB
Line 
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
23int main (int argc, char *argv[])
24{
25int tid, nthreads, i, j, k, chunk;
26double 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
30chunk = 10; /* set loop iteration chunk size */
31
32/*** Spawn a parallel region explicitly scoping all variables ***/
33#pragma omp parallel shared(a,b,c,nthreads,chunk) private(tid,i,j,k)
34 {
35 tid = omp_get_thread_num();
36 if (tid == 0)
37 {
38 nthreads = omp_get_num_threads();
39 printf("Starting matrix multiple example with %d threads\n",nthreads);
40 printf("Initializing matrices...\n");
41 }
42 /*** Initialize matrices ***/
43 #pragma omp for schedule (static, chunk)
44 for (i=0; i<NRA; i++)
45 for (j=0; j<NCA; j++)
46 a[i][j]= i+j;
47 #pragma omp for schedule (static, chunk)
48 for (i=0; i<NCA; i++)
49 for (j=0; j<NCB; j++)
50 b[i][j]= i*j;
51 #pragma omp for schedule (static, chunk)
52 for (i=0; i<NRA; i++)
53 for (j=0; j<NCB; j++)
54 c[i][j]= 0;
55
56 /*** Do matrix multiply sharing iterations on outer loop ***/
57 /*** Display who does which iterations for demonstration purposes ***/
58 printf("Thread %d starting matrix multiply...\n",tid);
59 #pragma omp for schedule (static, chunk)
60 for (i=0; i<NRA; i++)
61 {
62 printf("Thread=%d did row=%d\n",tid,i);
63 for(j=0; j<NCB; j++)
64 for (k=0; k<NCA; k++)
65 c[i][j] += a[i][k] * b[k][j];
66 }
67 } /*** End of parallel region ***/
68
69/*** Print results ***/
70printf("******************************************************\n");
71printf("Result Matrix:\n");
72for (i=0; i<NRA; i++)
73 {
74 for (j=0; j<NCB; j++)
75 printf("%6.2f ", c[i][j]);
76 printf("\n");
77 }
78printf("******************************************************\n");
79printf ("Done.\n");
80
81}
Note: See TracBrowser for help on using the repository browser.