Skip to content

Instantly share code, notes, and snippets.

@urasandesu
Last active January 4, 2018 04:12
Show Gist options
  • Save urasandesu/a1415ac49963daa38cd19a41e410907f to your computer and use it in GitHub Desktop.
Save urasandesu/a1415ac49963daa38cd19a41e410907f to your computer and use it in GitHub Desktop.
This is the sample checking kernel progress for Alea GPU
using Alea;
using Alea.CSharp;
using System;
using System.Threading;
namespace AleaCheckKernelProgressSample
{
class Program
{
static void Main(string[] args)
{
unsafe
{
var gpu = Gpu.Default; // to avoid the 'cudaErrorInitializationError = 3' error that indicates the CUDA driver and runtime could not be initialized
const int Incs = 100;
int* d_data, h_data;
var rc = default(CUDAInterop.cudaError_enum);
if ((rc = CUDAInterop.cuMemHostAlloc((IntPtr*)&h_data, (IntPtr)sizeof(int), 2)) != CUDAInterop.cudaError_enum.CUDA_SUCCESS)
throw new InvalidOperationException($"'cudaHostAlloc' is failed. CUDA Error Code = { (int)rc }({ rc })");
if ((rc = CUDAInterop.cuMemHostGetDevicePointer((IntPtr*)&d_data, (IntPtr)h_data, 0)) != CUDAInterop.cudaError_enum.CUDA_SUCCESS)
throw new InvalidOperationException($"'cudaHostGetDevicePointer' is failed. CUDA Error Code = { (int)rc }({ rc })");
*h_data = 0;
var e = gpu.CreateEvent();
Console.WriteLine("kernel starting");
var d_dataPtr = new deviceptr<int>((IntPtr)d_data);
gpu.Launch(d_dataPtr_ =>
{
for (var i = 0; i < Incs; i++)
{
DeviceFunction.AtomicAdd(d_dataPtr_, 1);
DeviceFunction.ThreadFenceSystem();
var time = DeviceFunction.Clock64();
while ((DeviceFunction.Clock64() - time) < 100000000) { }
}
}, new LaunchParam(1, 1), d_dataPtr);
var value = 0;
do
{
e.Record();
CUDAInterop.cuEventQuery(e.Handle);
Thread.Sleep(100); // if there are too many requests, value reflection stops
var value1 = *h_data;
if (value1 > value)
{
Console.WriteLine("h_data = {0}", value1);
value = value1;
}
} while (value < (Incs - 1));
}
}
}
}
@urasandesu
Copy link
Author

To make this, I reference the following native CUDA codes:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment