source: CIVL/examples/cuda/pathfinder.cvl@ 63812cc

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

add cuda folder in ./examples

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

  • Property mode set to 100644
File size: 4.2 KB
Line 
1/**
2 * This file is modified from the original pathfinder_cuda.cvl
3 * program by the following way:
4 * First, GPU/GPU_BLOCK/GPU_THREAD no longer access global variables of
5 * int type, but use their value (by parameter passing) instead.
6 */
7
8#include<civlc.h>
9
10//pyramid height of 0 does nothing, of 1 is normal stencil, 2 is where
11//the algorithm even kicks in
12#define pyramid_height 2
13
14//This is how many iterations of the loop (minus 1). If less than
15//the pyramid_height, then the pyramid_height does nothing.
16#define rows 3
17
18//I think this is the minimum this can be to be useful
19//(the number of cells)
20#define cols 2
21
22
23//BLOCK_SIZE must satisfy (BLOCK_SIZE > pyramid_height*2)
24#define BLOCK_SIZE 5
25#define HALO 1
26
27int borderCols = (pyramid_height)*HALO;
28int smallBlockCol = BLOCK_SIZE - (pyramid_height) * HALO * 2;
29int blockCols = cols/smallBlockCol+((cols%smallBlockCol==0)?0:1);
30
31int result[cols];
32
33//GPU MEMORY (couldn't do it scoped because need references to memory
34//on host side)
35int gpuResult[2][cols];
36int gpuWall[rows*cols - cols];
37
38//$input int wall[rows][cols];
39
40#define IN_RANGE(x, min, max) ((x)>=(min) && (x)<=(max))
41#define CLAMP_RANGE(x, min, max) x = (x<(min)) ? min : ((x>(max)) ? max : x )
42#define MIN(a, b) ((a)<=(b) ? (a) : (b))
43
44//Not implemented yet.
45//void __syncthreads() {
46//}
47
48void GPU_THREAD(int tx, int tbx, int tIteration, int tBorderCols, int src, int dst, int* prev, int* result, int startStep){
49 int small_block_cols = BLOCK_SIZE-tIteration*HALO*2;
50 int blkX = small_block_cols*tbx-tBorderCols;
51 int blkXmax = blkX+BLOCK_SIZE-1;
52
53 // calculate the global thread coordination
54 int xidx = blkX+tx;
55
56 // effective range within this block that falls within
57 // the valid range of the input data
58 // used to rule out computation outside the boundary.
59 int validXmin = (blkX < 0) ? -blkX : 0;
60 int validXmax = (blkXmax > cols-1) ? BLOCK_SIZE-1-(blkXmax-cols+1) : BLOCK_SIZE-1;
61
62 int W = tx-1;
63 int E = tx+1;
64
65 W = (W < validXmin) ? validXmin : W;
66 E = (E > validXmax) ? validXmax : E;
67
68 int isValid = IN_RANGE(tx, validXmin, validXmax);
69 if(IN_RANGE(xidx, 0, cols-1)){
70 prev[tx] = gpuResult[src][xidx];
71 }
72
73 int computed;
74
75 for (int i=0; i<tIteration; i++){
76 computed = 0;
77 if( IN_RANGE(tx, i+1, BLOCK_SIZE-i-2) && isValid) {
78 computed = 1;
79 int left = prev[W];
80 int up = prev[tx];
81 int right = prev[E];
82 int shortest = MIN(left, up);
83 shortest = MIN(shortest, right);
84 int index = cols*(startStep+i)+xidx;
85 result[tx] = shortest + gpuWall[index];
86 }
87
88 //Break not implemented yet...
89 /* if(i==iteration-1) */
90 /* break; */
91 if(computed != 0) //Assign the computation range
92 prev[tx]= result[tx];
93 //__syncthreads(); // [Ronny] Added sync to avoid race on prev Aug. 14 201
94 }
95
96 // update the global memory
97 // after the last iteration, only threads coordinated within the
98 // small block perform the calculation and switch on ``computed''
99 if (computed != 0) {
100 gpuResult[dst][xidx]=result[tx];
101 }
102}
103
104void GPU_BLOCK(int bx, int bIteration, int bBorderCols, int src, int dst, int startStep){
105 //shared memory
106 int prev[BLOCK_SIZE];
107 int result[BLOCK_SIZE];
108 $proc thread_procs[BLOCK_SIZE];
109
110 //Launch the threads per block
111 for (int tp = 0; tp < BLOCK_SIZE; tp++) {
112 thread_procs[tp] = $spawn GPU_THREAD(tp, bx, bIteration, bBorderCols, src, dst, prev, result, startStep);
113 }
114
115 for (int tp = 0; tp < BLOCK_SIZE; tp++) {
116 $wait thread_procs[tp];
117 }
118}
119
120void GPU(int iteration, int src, int dst, int startStep, int blocks, int threads, int gBorderCols) {
121 $proc block_procs[blocks];
122
123 //Launch the blocks
124 for (int b = 0; b < blocks; b++) {
125 block_procs[b] = $spawn GPU_BLOCK(b, iteration, gBorderCols, src, dst, startStep);
126 }
127
128 for (int b = 0; b < blocks; b++) {
129 $wait block_procs[b];
130 }
131}
132
133void calc_path() {
134 int src = 1, dst = 0;
135 for (int t = 0; t < rows-1; t+=pyramid_height) {
136 int temp = src;
137 src = dst;
138 dst = temp;
139 GPU(MIN(pyramid_height, rows-t-1), src, dst,t, blockCols, BLOCK_SIZE, borderCols);
140 }
141}
142
143void main() {
144 $assert BLOCK_SIZE > pyramid_height*2;
145 calc_path();
146 $assert 1 == 1;
147}
Note: See TracBrowser for help on using the repository browser.