You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.3 KiB

3 years ago
3 years ago
3 years ago
  1. // Patric Zhao: patric.zhao@intel.com
  2. #include <CL/sycl.hpp>
  3. #include <iostream>
  4. using namespace sycl;
  5. constexpr int N = 10;
  6. int main() {
  7. queue my_gpu_queue( gpu_selector{} );
  8. std::cout << "Selected GPU device: " <<
  9. my_gpu_queue.get_device().get_info<info::device::name>() << "\n";
  10. int *host_mem = malloc_host<int>(N, my_gpu_queue);
  11. int *device_mem = malloc_device<int>(N, my_gpu_queue);
  12. // Init CPU data
  13. for(int i = 0; i < N; i++) {
  14. host_mem[i] = i;
  15. }
  16. // Copy from host(CPU) to device(GPU)
  17. my_gpu_queue.memcpy(device_mem, host_mem, N * sizeof(int)).wait();
  18. // submit the content to the queue for execution
  19. my_gpu_queue.submit([&](handler& h) {
  20. // Parallel Computation
  21. h.parallel_for(range{N}, [=](id<1> item) {
  22. device_mem[item] *= 2;
  23. });
  24. });
  25. // wait the computation done
  26. my_gpu_queue.wait();
  27. // Copy back from GPU to CPU
  28. my_gpu_queue.memcpy(host_mem, device_mem, N * sizeof(int)).wait();
  29. printf("\nData Result\n");
  30. for(int i = 0; i < N; i++) {
  31. printf("%d, ", host_mem[i]);
  32. }
  33. printf("\nTask Done!\n");
  34. free(host_mem, my_gpu_queue);
  35. free(device_mem, my_gpu_queue);
  36. return 0;
  37. }