source: CIVL/examples/translation/cuda/sum.cvl@ be4355b

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since be4355b was 5f0a06f, checked in by Manchun Zheng <zmanchun@…>, 12 years ago

updated makefile and README for cuda examples.

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

  • Property mode set to 100644
File size: 1.6 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 int num_in_barrier =0;
18 int barrier_size = 0;
19 int in_barrier[nt];
20
21 #include "cuda.cvh"
22
23 void gpuThread(int tid){
24 int i;
25 int bdim = NTHREADS;
26
27 shared[tid] = in[bid * bdim + tid];
28
29 __syncthreads(in_barrier, tid);
30 if(tid < bdim/2) {
31 shared[tid] += shared[bdim/2 + tid];
32 }
33 __syncthreads(in_barrier, tid);
34 if(tid == 0) {
35 for (i = 1; i != (bdim/2) + (bdim%2); ++i) {
36 shared[0] += shared[i];
37 }
38 out[bid] = shared[0];
39 }
40 }
41
42 $proc threads[nt];
43 __sync_init(in_barrier, nt);
44 for(int i = 0; i < nt; i++) {
45 threads[i] = $spawn gpuThread(i);
46 }
47 for(int i = 0; i < nt; i++) {
48 $wait(threads[i]);
49 }
50 }
51
52 $proc blocks[nb];
53 for(int i = 0; i < nb; i++) {
54 blocks[i] = $spawn gpuBlock(i);
55 }
56 for(int i = 0; i < nb; i++) {
57 $wait(blocks[i]);
58 }
59}
60
61void main() {
62 int i, *dev_out, host[N];
63
64 printf("INPUT: ");
65 for(i = 0; i != N; ++i) {
66 host[i] = (21*i + 29) % 100;
67 printf(" %d ", host[i]);
68 }
69 printf("\n");
70
71 dev_out = (int *) malloc(NBLOCKS * sizeof(int));
72
73 gpu(NBLOCKS, NTHREADS, host, dev_out);
74 gpu(1, NBLOCKS, dev_out, dev_out);
75 printf("OUTPUT: %d\n", *dev_out);
76 free(dev_out);
77}
Note: See TracBrowser for help on using the repository browser.