/src/MxNet/RTC/CudaModule.cs

https://github.com/tech-quantum/MxNet.Sharp · C# · 81 lines · 61 code · 5 blank · 15 comment · 2 complexity · 1d5475e86d09548d8b54a8ed6f9d02ca MD5 · raw file

  1. /*****************************************************************************
  2. Copyright 2018 The MxNet.Sharp Authors. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ******************************************************************************/
  13. using MxNet.Interop;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Text.RegularExpressions;
  17. using System.Linq;
  18. namespace MxNet.RTC
  19. {
  20. public class CudaModule : IDisposable
  21. {
  22. public IntPtr Handle;
  23. private Dictionary<string, DType> cudaDtypeMap = new Dictionary<string, DType>()
  24. {
  25. {"float", DType.Float32 },
  26. {"double", DType.Float64 },
  27. {"__half", DType.Float16 },
  28. {"uint8_t", DType.UInt8 },
  29. {"int", DType.Int32 },
  30. {"int32_t", DType.Int32 },
  31. {"int8_t", DType.Int8 },
  32. {"char", DType.Int8 },
  33. {"int64_t", DType.Int64 },
  34. };
  35. public CudaModule(string source, string[] options = null, string[] exports = null)
  36. {
  37. NativeMethods.MXRtcCudaModuleCreate(source, options.Length, options, exports.Length, exports, out Handle);
  38. }
  39. public void Dispose()
  40. {
  41. if (Handle != null)
  42. NativeMethods.MXRtcCudaModuleFree(Handle);
  43. }
  44. public CudaKernel GetKernel(string name, string signature)
  45. {
  46. var pattern = new Regex(@"""^\s*(const)?\s*([\w_]+)\s*(\*)?\s*([\w_]+)?\s*$""");
  47. var args = Regex.Replace(signature, @"\s+", " ").Split(',');
  48. List<bool> is_const = new List<bool>();
  49. List<bool> is_ndarray = new List<bool>();
  50. List<int> dtypes = new List<int>();
  51. foreach (var arg in args)
  52. {
  53. var match = pattern.Match(arg);
  54. if (!match.Success || match.Groups[1].Value == "const")
  55. throw new Exception($"Invalid function prototype \"{ arg }\". Must be in the " +
  56. "form of \"(const) type (*) (name)\"");
  57. is_const.Add(match.Groups[0].Success);
  58. string dtype = match.Groups[1].Value;
  59. is_ndarray.Add(match.Groups[2].Success);
  60. if (!cudaDtypeMap.ContainsKey(dtype))
  61. throw new Exception($"Unsupported kernel argument type {arg}");
  62. dtypes.Add(cudaDtypeMap[dtype].Index);
  63. }
  64. NativeMethods.MXRtcCudaKernelCreate(Handle, name, dtypes.Count, is_ndarray.ToArray(), is_const.ToArray(), dtypes.ToArray(), out var kernel_handle);
  65. return new CudaKernel(kernel_handle, name, is_ndarray.ToArray(), dtypes.Select(x=>(DType.GetType(x))).ToArray());
  66. }
  67. }
  68. }