| 108 | ); |
| 109 | |
| 110 | int main() |
| 111 | { |
| 112 | using compute::dim; |
| 113 | using compute::uint_; |
| 114 | |
| 115 | // fizz-buzz up to 100 |
| 116 | size_t n = 100; |
| 117 | |
| 118 | // get the default device |
| 119 | compute::device device = compute::system::default_device(); |
| 120 | compute::context ctx(device); |
| 121 | compute::command_queue queue(ctx, device); |
| 122 | |
| 123 | // compile the fizz-buzz program |
| 124 | compute::program fizz_buzz_program = |
| 125 | compute::program::create_with_source(fizz_buzz_source, ctx); |
| 126 | fizz_buzz_program.build(); |
| 127 | |
| 128 | // create a vector for the output string and computing offsets |
| 129 | compute::vector<char> output(ctx); |
| 130 | compute::vector<uint_> offsets(n, ctx); |
| 131 | |
| 132 | // run the allocate kernel to calculate string lengths |
| 133 | compute::kernel allocate_kernel(fizz_buzz_program, "fizz_buzz_allocate_strings"); |
| 134 | allocate_kernel.set_arg(0, offsets); |
| 135 | queue.enqueue_nd_range_kernel(allocate_kernel, dim(0), dim(n), dim(1)); |
| 136 | |
| 137 | // allocate space for the output string |
| 138 | output.resize( |
| 139 | compute::accumulate(offsets.begin(), offsets.end(), 0, queue) |
| 140 | ); |
| 141 | |
| 142 | // scan string lengths for each number to calculate the output offsets |
| 143 | compute::exclusive_scan( |
| 144 | offsets.begin(), offsets.end(), offsets.begin(), queue |
| 145 | ); |
| 146 | |
| 147 | // run the copy kernel to fill the output buffer |
| 148 | compute::kernel copy_kernel(fizz_buzz_program, "fizz_buzz_copy_strings"); |
| 149 | copy_kernel.set_arg(0, offsets); |
| 150 | copy_kernel.set_arg(1, output); |
| 151 | queue.enqueue_nd_range_kernel(copy_kernel, dim(0), dim(n), dim(1)); |
| 152 | |
| 153 | // copy the string to the host and print it to stdout |
| 154 | std::string str; |
| 155 | str.resize(output.size()); |
| 156 | compute::copy(output.begin(), output.end(), str.begin(), queue); |
| 157 | std::cout << str; |
| 158 | |
| 159 | return 0; |
| 160 | } |
nothing calls this directly
no test coverage detected