OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gcc-4.2.2/] [libgomp/] [testsuite/] [libgomp.c/] [omp_matvec.c] - Blame information for rev 862

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 38 julius
/******************************************************************************
2
* OpenMP Example - Matrix-vector multiplication - C/C++ Version
3
* FILE: omp_matvec.c
4
* DESCRIPTION:
5
*   This example multiplies all row i elements of matrix A with vector
6
*   element b(i) and stores the summed products in vector c(i).  A total is
7
*   maintained for the entire matrix.  Performed by using the OpenMP loop
8
*   work-sharing construct.  The update of the shared global total is
9
*   serialized by using the OpenMP critical directive.
10
* SOURCE: Blaise Barney  5/99
11
* LAST REVISED:
12
******************************************************************************/
13
 
14
#include <omp.h>
15
#include <stdio.h>
16
#define SIZE 10
17
 
18
 
19
main ()
20
{
21
 
22
float A[SIZE][SIZE], b[SIZE], c[SIZE], total;
23
int i, j, tid;
24
 
25
/* Initializations */
26
total = 0.0;
27
for (i=0; i < SIZE; i++)
28
  {
29
  for (j=0; j < SIZE; j++)
30
    A[i][j] = (j+1) * 1.0;
31
  b[i] = 1.0 * (i+1);
32
  c[i] = 0.0;
33
  }
34
printf("\nStarting values of matrix A and vector b:\n");
35
for (i=0; i < SIZE; i++)
36
  {
37
  printf("  A[%d]= ",i);
38
  for (j=0; j < SIZE; j++)
39
    printf("%.1f ",A[i][j]);
40
  printf("  b[%d]= %.1f\n",i,b[i]);
41
  }
42
printf("\nResults by thread/row:\n");
43
 
44
/* Create a team of threads and scope variables */
45
#pragma omp parallel shared(A,b,c,total) private(tid,i)
46
  {
47
  tid = omp_get_thread_num();
48
 
49
/* Loop work-sharing construct - distribute rows of matrix */
50
#pragma omp for private(j)
51
  for (i=0; i < SIZE; i++)
52
    {
53
    for (j=0; j < SIZE; j++)
54
      c[i] += (A[i][j] * b[i]);
55
 
56
    /* Update and display of running total must be serialized */
57
    #pragma omp critical
58
      {
59
      total = total + c[i];
60
      printf("  thread %d did row %d\t c[%d]=%.2f\t",tid,i,i,c[i]);
61
      printf("Running total= %.2f\n",total);
62
      }
63
 
64
    }   /* end of parallel i loop */
65
 
66
  } /* end of parallel construct */
67
 
68
printf("\nMatrix-vector total - sum of all c[] = %.2f\n\n",total);
69
 
70
  return 0;
71
}
72
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.