source: CIVL/examples/omp/matProduct1.c@ bb03188

main test-branch
Last change on this file since bb03188 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.8 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#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
29int main (int argc, char *argv[])
30{
31int tid, nthreads, i, j, k, chunk;
32double 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
36chunk = 10; /* set loop iteration chunk size */
37
38/*** Spawn a parallel region explicitly scoping all variables ***/
39#pragma omp parallel shared(a,b,c,chunk) private(tid,i,j,k,nthreads)
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 ***/
76printf("******************************************************\n");
77printf("Result Matrix:\n");
78for (i=0; i<NRA; i++)
79 {
80 for (j=0; j<NCB; j++)
81 printf("%6.2f ", c[i][j]);
82 printf("\n");
83 }
84printf("******************************************************\n");
85printf ("Done.\n");
86
87}
Note: See TracBrowser for help on using the repository browser.