|
| 1 | +/** |
| 2 | + * SYCL FOR CUDA : Vector Addition Example |
| 3 | + * |
| 4 | + * Copyright 2020 Codeplay Software Ltd. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + * |
| 18 | + * @File: vector_addition.cpp |
| 19 | + */ |
| 20 | + |
| 21 | +#include <algorithm> |
| 22 | +#include <iostream> |
| 23 | +#include <vector> |
| 24 | + |
| 25 | +#include <CL/sycl.hpp> |
| 26 | + |
| 27 | +class CUDASelector : public sycl::device_selector { |
| 28 | +public: |
| 29 | + int operator()(const sycl::device &device) const override { |
| 30 | + if(device.get_platform().get_backend() == sycl::backend::cuda){ |
| 31 | + std::cout << " CUDA device found " << std::endl; |
| 32 | + return 1; |
| 33 | + } else{ |
| 34 | + return -1; |
| 35 | + } |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +int main(int argc, char *argv[]) { |
| 40 | + constexpr const size_t n = 100000; |
| 41 | + |
| 42 | + // Create a sycl queue with our CUDASelector |
| 43 | + sycl::queue myQueue{CUDASelector()}; |
| 44 | + |
| 45 | + // Host input vectors |
| 46 | + double *h_a; |
| 47 | + double *h_b; |
| 48 | + // Host output vector |
| 49 | + double *h_c; |
| 50 | + |
| 51 | + // Device input vectors |
| 52 | + double *d_a; |
| 53 | + double *d_b; |
| 54 | + // Device output vector |
| 55 | + double *d_c; |
| 56 | + |
| 57 | + // Size, in bytes, of each vector |
| 58 | + size_t bytes = n * sizeof(double); |
| 59 | + |
| 60 | + // Allocate memory for each vector on host |
| 61 | + h_a = (double *)malloc(bytes); |
| 62 | + h_b = (double *)malloc(bytes); |
| 63 | + h_c = (double *)malloc(bytes); |
| 64 | + |
| 65 | + // Allocate memory for each vector on GPU |
| 66 | + d_a = sycl::malloc_device<double>(n, myQueue); |
| 67 | + d_b = sycl::malloc_device<double>(n, myQueue); |
| 68 | + d_c = sycl::malloc_device<double>(n, myQueue); |
| 69 | + |
| 70 | + // Initialize vectors on host |
| 71 | + for (int i = 0; i < n; i++) { |
| 72 | + h_a[i] = sin(i) * sin(i); |
| 73 | + h_b[i] = cos(i) * cos(i); |
| 74 | + } |
| 75 | + |
| 76 | + myQueue.memcpy(d_a, h_a, bytes).wait(); |
| 77 | + myQueue.memcpy(d_b, h_b, bytes).wait(); |
| 78 | + |
| 79 | + // Command Group creation |
| 80 | + auto cg = [&](sycl::handler &h) { |
| 81 | + h.parallel_for(sycl::range(n), |
| 82 | + [=](sycl::id<1> i) { |
| 83 | + d_c[i] = d_a[i] + d_b[i]; |
| 84 | + }); |
| 85 | + }; |
| 86 | + |
| 87 | + // Run the kernel defined above |
| 88 | + myQueue.submit(cg).wait(); |
| 89 | + |
| 90 | + // Copy the result back to host |
| 91 | + myQueue.memcpy(h_c, d_c, bytes).wait(); |
| 92 | + |
| 93 | + double sum = 0.0f; |
| 94 | + for (int i = 0; i < n; i++) { |
| 95 | + sum += h_c[i]; |
| 96 | + } |
| 97 | + std::cout << "Sum is : " << sum << std::endl; |
| 98 | + |
| 99 | + return 0; |
| 100 | +} |
0 commit comments