Daily bump.
[official-gcc.git] / libgomp / testsuite / libgomp.c / omp_matvec.c
blob44547b6ebb580da352318f76103bca48cd9ec592
1 /******************************************************************************
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 ******************************************************************************/
14 #include <omp.h>
15 #include <stdio.h>
16 #define SIZE 10
19 int
20 main ()
23 float A[SIZE][SIZE], b[SIZE], c[SIZE], total;
24 int i, j, tid;
26 /* Initializations */
27 total = 0.0;
28 for (i=0; i < SIZE; i++)
30 for (j=0; j < SIZE; j++)
31 A[i][j] = (j+1) * 1.0;
32 b[i] = 1.0 * (i+1);
33 c[i] = 0.0;
35 printf("\nStarting values of matrix A and vector b:\n");
36 for (i=0; i < SIZE; i++)
38 printf(" A[%d]= ",i);
39 for (j=0; j < SIZE; j++)
40 printf("%.1f ",A[i][j]);
41 printf(" b[%d]= %.1f\n",i,b[i]);
43 printf("\nResults by thread/row:\n");
45 /* Create a team of threads and scope variables */
46 #pragma omp parallel shared(A,b,c,total) private(tid,i)
48 tid = omp_get_thread_num();
50 /* Loop work-sharing construct - distribute rows of matrix */
51 #pragma omp for private(j)
52 for (i=0; i < SIZE; i++)
54 for (j=0; j < SIZE; j++)
55 c[i] += (A[i][j] * b[i]);
57 /* Update and display of running total must be serialized */
58 #pragma omp critical
60 total = total + c[i];
61 printf(" thread %d did row %d\t c[%d]=%.2f\t",tid,i,i,c[i]);
62 printf("Running total= %.2f\n",total);
65 } /* end of parallel i loop */
67 } /* end of parallel construct */
69 printf("\nMatrix-vector total - sum of all c[] = %.2f\n\n",total);
71 return 0;