Geeks3D Forums

Tech Forums => 3D-Tech News Around The Web => Topic started by: JeGX on October 08, 2018, 08:17:33 PM

Title: VUDA: CUDA in Vulkan
Post by: JeGX on October 08, 2018, 08:17:33 PM
VUDA is a header-only lib based on Vulkan that provides a CUDA Runtime API interface for writing GPU-accelerated applications.


Code: [Select]
#include <vuda.hpp>
int main(void)
  // allocate memory on the device
  const int N = 5000;
  int a[N], b[N], c[N];
  for(int i=0; i<N; ++i)
    a[i] = -i;
    b[i] = i*i;
  int *dev_a, *dev_b, *dev_c;
  vuda::malloc((void**)&dev_a, N * sizeof(int));
  vuda::malloc((void**)&dev_b, N * sizeof(int));
  vuda::malloc((void**)&dev_c, N * sizeof(int));
  // copy the arrays a and b to the device
  vuda::memcpy(dev_a, a, N * sizeof(int), vuda::memcpyHostToDevice);
  vuda::memcpy(dev_b, b, N * sizeof(int), vuda::memcpyHostToDevice);
  // run kernel (vulkan shader module)
  const int stream_id = 0;
  const int blocks = 128;
  const int threads = 128;
  vuda::kernelLaunch("add.spv", "main", blocks, threads, stream_id, dev_a, dev_b, dev_c, N);
  // copy result to host
  vuda::memcpy(c, dev_c, N * sizeof(int), vuda::memcpyDeviceToHost);

  // do something useful with the result in array c ...

  // free memory on device