source: CIVL/examples/cuda/sum.cvl@ e0fc189

1.23 2.0 main test-branch
Last change on this file since e0fc189 was e3151da, checked in by Ziqing Luo <ziqing@…>, 11 years ago

re-organized example directory

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@1763 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 1.7 KB
Line 
1/**
2* This is an example from the paper "Formal Semantics of Heterogeneous CUDA-C:
3* A Modular Approach with Applications" by Chris Hathhorn et al.
4*/
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <civlc.h>
9
10#define N 8
11#define NBLOCKS 4
12#define NTHREADS (N/NBLOCKS)
13
14void gpu(int nb, int nt, int *in, int *out){
15 void gpuBlock(int bid){
16 int shared[];
17 $gbarrier gbarrier = $gbarrier_create($here, NTHREADS);
18
19 void gpuThread(int tid){
20 int i;
21 int bdim = NTHREADS;
22 $barrier barrier = $barrier_create($here, gbarrier, tid);
23
24 shared[tid] = in[bid * bdim + tid];
25 //synchronize threads
26 $barrier_call(barrier);
27 if(tid < bdim/2) {
28 shared[tid] += shared[bdim/2 + tid];
29 }
30 //synchronize threads
31 $barrier_call(barrier);
32 if(tid == 0) {
33 for (i = 1; i != (bdim/2) + (bdim%2); ++i) {
34 shared[0] += shared[i];
35 }
36 out[bid] = shared[0];
37 }
38 $barrier_destroy(barrier);
39 }
40
41 $proc threads[nt];
42 for(int i = 0; i < nt; i++) {
43 threads[i] = $spawn gpuThread(i);
44 }
45 for(int i = 0; i < nt; i++) {
46 $wait(threads[i]);
47 }
48 $gbarrier_destroy(gbarrier);
49 }
50
51 $proc blocks[nb];
52 for(int i = 0; i < nb; i++) {
53 blocks[i] = $spawn gpuBlock(i);
54 }
55 for(int i = 0; i < nb; i++) {
56 $wait(blocks[i]);
57 }
58}
59
60void main() {
61 int i, *dev_out, host[N];
62
63 printf("INPUT: ");
64 for(i = 0; i != N; ++i) {
65 host[i] = (21*i + 29) % 100;
66 printf(" %d ", host[i]);
67 }
68 printf("\n");
69
70 dev_out = (int *) malloc(NBLOCKS * sizeof(int));
71
72 gpu(NBLOCKS, NTHREADS, host, dev_out);
73 gpu(1, NBLOCKS, dev_out, dev_out);
74 printf("OUTPUT: %d\n", *dev_out);
75 free(dev_out);
76}
Note: See TracBrowser for help on using the repository browser.