source: CIVL/examples/omp/fig3.10-mxv-omp.cvl@ 8190175

main test-branch
Last change on this file since 8190175 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: 1.8 KB
Line 
1/* fig3.10-mxv-omp.cvl: CIVL model of fig3.10-mxv-omp.c
2 * Translated by Stephen Siegel.
3 */
4#include <civlc.h>
5#include <stdio.h>
6#include <stdlib.h>
7#define THREAD_MAX 4
8/* Does thread t own iteration i in loop with n iterations? */
9#define CIVL_owns(t, n, i) ((i)%(n)==(t))
10
11void mxv(int m, int n, double * restrict a,
12 double * restrict b, double * restrict c);
13
14void perror(char *s) {
15 printf("%s\n", s);
16 exit(1);
17}
18
19int main(int argc, char *argv[])
20{
21 double *a,*b,*c;
22 int i, j, m, n;
23
24 printf("Please give m and n: ");
25 scanf("%d %d",&m,&n);
26 printf("\n");
27
28 if ( (a=(double *)malloc(m*sizeof(double))) == NULL )
29 perror("memory allocation for a");
30 if ( (b=(double *)malloc(m*n*sizeof(double))) == NULL )
31 perror("memory allocation for b");
32 if ( (c=(double *)malloc(n*sizeof(double))) == NULL )
33 perror("memory allocation for c");
34
35 printf("Initializing matrix B and vector c\n");
36 for (j=0; j<n; j++)
37 c[j] = 2.0;
38 for (i=0; i<m; i++)
39 for (j=0; j<n; j++)
40 b[i*n+j] = i;
41
42 printf("Executing mxv function for m = %d n = %d\n",m,n);
43 (void) mxv(m, n, a, b, c);
44
45 free(a);free(b);free(c);
46 return(0);
47}
48
49void mxv(int m, int n, double * restrict a, double * restrict b,
50 double * restrict c)
51{
52
53 // #pragma omp parallel for default(none)
54 // shared(m,n,a,b,c) private(i,j)
55 {
56 int _nthreads = 1+$choose_int(THREAD_MAX);
57 $proc _threads[_nthreads];
58 void loop(int _tid) {
59 int i, j;
60
61 for (i=0; i<m; i++) {
62 if (CIVL_owns(_tid, _nthreads, i)) {
63 a[i] = 0.0;
64 for (j=0; j<n; j++)
65 a[i] += b[i*n+j]*c[j];
66 }
67 }
68 }
69
70 printf("nthreads = %d\n", _nthreads); // for debugging
71 for (int tid=0; tid<_nthreads; tid++)
72 _threads[tid] = $spawn loop(tid);
73 for (int tid=0; tid<_nthreads; tid++)
74 $wait(_threads[tid]);
75 } /* End of omp for loop */
76
77}
Note: See TracBrowser for help on using the repository browser.