source: CIVL/examples/omp/dotProduct1.cvl@ 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: 1.2 KB
Line 
1/* CIVL model of dotproduct1.c.
2 * How to verify: civl verify dotproduct1.cvl
3 *
4 * Limitations: bound is placed on number of threads; each thread always
5 * executes iterations in increasing order; assumes a round-robin distribution
6 * of iterations to threads
7 */
8#include <civlc.h>
9#include <stdio.h>
10#include <assert.h>
11#define THREAD_MAX 4
12/* Does thread t own iteration i in loop with n iterations? */
13#define CIVL_owns(t, n, i) ((i)%(n)==(t))
14
15
16void main() {
17 int i, n;
18 float a[8], b[8], sum;
19
20 n=8;
21 for (i=0; i < n; i++)
22 a[i] = b[i] = i * 1.0;
23 sum = 0.0;
24
25 /* Translation of omp for loop: */
26 {
27 int nthreads = 1+$choose_int(THREAD_MAX);
28 $proc threads[nthreads];
29 void loop(int tid) {
30 float _sum = 0.0;
31
32 for (int i=0; i<n; i++) {
33 if (CIVL_owns(tid, nthreads, i)) {
34 _sum = _sum + (a[i] * b[i]);
35 printf("loop %d\n", i);
36 }
37 }
38 sum += _sum;
39 }
40
41 printf("nthreads = %d\n", nthreads); // for debugging
42 for (int tid=0; tid<nthreads; tid++)
43 threads[tid] = $spawn loop(tid);
44 for (int tid=0; tid<nthreads; tid++)
45 $wait(threads[tid]);
46 } /* End of omp for loop */
47
48 printf(" Sum = %f\n", sum);
49 assert(sum == 140.0); // for checking correctness
50}
Note: See TracBrowser for help on using the repository browser.