Last active
January 4, 2018 04:12
-
-
Save urasandesu/a1415ac49963daa38cd19a41e410907f to your computer and use it in GitHub Desktop.
This is the sample checking kernel progress for Alea GPU
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To make this, I reference the following native CUDA codes:
https://stackoverflow.com/questions/20345702/how-can-i-check-the-progress-of-matrix-multiplication
https://stackoverflow.com/questions/33455396/cuda-mapped-memory-device-host-writes-are-not-visible-on-host