283
|
1 #include <stdio.h>
|
|
2
|
|
3 #define SIZE_TEXT (sizeof(text)-1)
|
|
4 #define SIZE_END (sizeof(end)-1)
|
|
5
|
|
6 __device__ char text[] =
|
|
7 "__ bottles of beer on the wall, __ bottles of beer!\n"
|
|
8 "Take one down, and pass it around, ## bottles of beer on the wall!\n\n";
|
|
9
|
|
10 __device__ char end[] =
|
|
11 "01 bottle of beer on the wall, 01 bottle of beer.\n"
|
|
12 "Take one down and pass it around, no more bottles of beer on the wall.\n"
|
|
13 "\n"
|
|
14 "No more bottles of beer on the wall, no more bottles of beer.\n"
|
|
15 "Go to the store and buy some more, 99 bottles of beer on the wall.";
|
|
16
|
|
17
|
|
18 __global__
|
|
19 void bottle99(char *addr){
|
|
20 int x = threadIdx.x;
|
|
21 addr += x * SIZE_TEXT;
|
|
22 int bottle = 99 - x;
|
|
23 if (bottle == 1) {
|
|
24 for (int i=0; i<SIZE_END; i++) {
|
|
25 addr[i] = end[i];
|
|
26 }
|
|
27 addr[SIZE_END] = '\0';
|
|
28 } else {
|
|
29 char c1 = (bottle/10) + '0';
|
|
30 char c2 = (bottle%10) + '0';
|
|
31
|
|
32 char d1 = ((bottle-1)/10) + '0';
|
|
33 char d2 = ((bottle-1)%10) + '0';
|
|
34
|
|
35 for (int i=0; i<SIZE_TEXT; i++) {
|
|
36 int c = text[i];
|
|
37 if (c == '_') {
|
|
38 addr[i] = c1;
|
|
39 addr[i+1] = c2;
|
|
40 i++;
|
|
41 } else if (c == '#') {
|
|
42
|
|
43 addr[i] = d1;
|
|
44 addr[i+1] = d2;
|
|
45 i++;
|
|
46 } else {
|
|
47
|
|
48 addr[i] = text[i];
|
|
49 }
|
|
50 }
|
|
51 }
|
|
52 }
|
|
53
|
|
54 int main()
|
|
55 {
|
|
56 char *buffer;
|
|
57 char *d_buffer;
|
|
58
|
|
59 int size = SIZE_TEXT * 98 + SIZE_END + 1;
|
|
60
|
|
61 buffer = new char[size];
|
|
62 cudaMalloc((void**)&d_buffer, size);
|
|
63
|
|
64 bottle99<<<1, 99>>>(d_buffer);
|
|
65
|
|
66 cudaMemcpy(buffer, d_buffer, size, cudaMemcpyDeviceToHost);
|
|
67 cudaFree(d_buffer);
|
|
68
|
|
69 puts(buffer);
|
|
70 free(buffer);
|
|
71 }
|
|
72 |