source: CIVL/examples/omp/matProduct1_orphan.c

main
Last change on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

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

  • Property mode set to 100644
File size: 2.9 KB
RevLine 
[e3d4779]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#ifdef _CIVL
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 */
23#else
24#define NRA 8 /* number of rows in matrix A */
25#define NCA 8 /* number of columns in matrix A */
26#define NCB 8 /* number of columns in matrix B */
27#endif
28
29double a[NRA][NCA], /* matrix A to be multiplied */
30 b[NCA][NCB], /* matrix B to be multiplied */
31 c[NRA][NCB]; /* result matrix C */
32
33int chunk;
34
35void matProd(int tid){
36 int i,j,k;
37 #pragma omp for schedule (static, chunk)
38 for (i=0; i<NRA; i++)
39 {
40 printf("Thread=%d did row=%d\n",tid,i);
41 for(j=0; j<NCB; j++)
42 for (k=0; k<NCA; k++)
43 c[i][j] += a[i][k] * b[k][j];
44 }
45}
46
47void foo(int tid){
48 matProd(tid);
49}
50
51int main (int argc, char *argv[])
52{
53int tid, nthreads, i, j, k;
54
55chunk = 10; /* set loop iteration chunk size */
56
57/*** Spawn a parallel region explicitly scoping all variables ***/
58#pragma omp parallel shared(a,b,c,nthreads,chunk) private(tid,i,j,k)
59 {
60 tid = omp_get_thread_num();
61 if (tid == 0)
62 {
63 nthreads = omp_get_num_threads();
64 printf("Starting matrix multiple example with %d threads\n",nthreads);
65 printf("Initializing matrices...\n");
66 }
67 /*** Initialize matrices ***/
68 #pragma omp for schedule (static, chunk)
69 for (i=0; i<NRA; i++)
70 for (j=0; j<NCA; j++)
71 a[i][j]= i+j;
72 #pragma omp for schedule (static, chunk)
73 for (i=0; i<NCA; i++)
74 for (j=0; j<NCB; j++)
75 b[i][j]= i*j;
76 #pragma omp for schedule (static, chunk)
77 for (i=0; i<NRA; i++)
78 for (j=0; j<NCB; j++)
79 c[i][j]= 0;
80
81 /*** Do matrix multiply sharing iterations on outer loop ***/
82 /*** Display who does which iterations for demonstration purposes ***/
83 printf("Thread %d starting matrix multiply...\n",tid);
84 foo(tid);
85 } /*** End of parallel region ***/
86
87/*** Print results ***/
88printf("******************************************************\n");
89printf("Result Matrix:\n");
90for (i=0; i<NRA; i++)
91 {
92 for (j=0; j<NCB; j++)
93 printf("%6.2f ", c[i][j]);
94 printf("\n");
95 }
96printf("******************************************************\n");
97printf ("Done.\n");
98
99}
Note: See TracBrowser for help on using the repository browser.