在 OpenCL 中运行一些测试代码(使用 Cloo C#)时,我开始从 OpenCL 中获取这些 OutOfResource 错误,有时 Unity 在出现异常之前就完全崩溃了。我基本上是用不同数量的全局/本地工作项一遍又一遍地重新调用内核函数来检查时间。我将参数保持不变,并以 2x2x2 全局和 2x2x2 局部开始调用内核,并向上迭代,仅检查有效大小。它偶尔可以正常工作,但大多数时候它会完成大约 30 或 40 次 Execute() 调用,然后在下一次 Execute() 调用时崩溃。
注意:执行是指计算机上的 OpenCL.dll。由于本机代码,我假设 Unity 返回的堆栈跟踪为 NULL。
任何人都知道可能导致这种情况的原因是什么?
注意:这个版本的 Cloo 是来自 GitHub 的 Cloo-Unity,我在 Unity 中使用它。当我收到错误时调用的等效 OpenCL 函数是 clEnqueueNDRangeKernel(),但在 Cloo 中它被称为 Execute()。
代码示例:
//Setup inputs one time...
foreach (var input in p_inputs)
{
inputs.Add(input.Function, input);
profiles.Add(input.Function, new RunProfile(input.Function, input.Weight));
input.Input.Prepare(package[input.Function]);
}
//Profile...
DateTime start;
int g_state = 0;
int l_state = 0;
long[] g = new long[3] { 2, 2, 2 };
long[] l = new long[3] { 2, 2, 2 };
while(g[0] * g[1] * g[2] < Device.MaxWorkGroupSize)
{
l[0] = 2; l[1] = 2; l[2] = 2; l_state = 0; //Reset locals
bool proceed = true;
while(proceed)
{
proceed = (l[0] != g[0] || l[1] != g[1] || l[2] != g[2]);
if (CLUtilities.ValidateExecutionParameters(Device, g, l))
{
Debug.Log("Profiling Start: " + g.ToEnumeratedString() + " / " + l.ToEnumeratedString());
foreach (var profile in profiles)
{
start = DateTime.Now;
//Exception here when on (g=6x4x4, l=6x4x4)
package.Execute(package[profile.Key], g, l);
package.Commands.Flush();
package.Commands.Finish();
float time = (float)(DateTime.Now - start).TotalMilliseconds;
profile.Value.AddRun(g, l, time);
}
Debug.Log("Profiling Ending: " + g.ToEnumeratedString() + " / " + l.ToEnumeratedString());
}
l[l_state] += 2;
l_state = (l_state == 2) ? 0 : l_state + 1;
}
g[g_state] += 2;
g_state = (g_state == 2) ? 0 : g_state + 1;
}