public static int[] GetSupportedArchs() { int num = 0; nvrtcResult res = NVRTCNativeMethods.nvrtcGetNumSupportedArchs(ref num); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nvrtcGetNumSupportedArchs", res)); if (res != nvrtcResult.Success) { throw new NVRTCException(res); } if (num <= 0) { return(null); } int[] archs = new int[num]; res = NVRTCNativeMethods.nvrtcGetSupportedArchs(archs); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nvrtcGetSupportedArchs", res)); if (res != nvrtcResult.Success) { throw new NVRTCException(res); } return(archs); }
static void CheckResult(nvrtcResult result) { if (result != nvrtcResult.NVRTC_SUCCESS) { throw new CudaException(result.ToString()); } }
private static string GetErrorMessageFromCUResult(nvrtcResult error) { string message = string.Empty; switch (error) { case nvrtcResult.Success: message = "No Error."; break; case nvrtcResult.ErrorOutOfMemory: message = "Error out of memory."; break; case nvrtcResult.ErrorProgramCreationFailure: message = "Program creation failure."; break; case nvrtcResult.ErrorInvalidInput: message = "Invalid Input."; break; case nvrtcResult.ErrorInvalidProgram: message = "Invalid program."; break; case nvrtcResult.ErrorInvalidOption: message = "Invalid option."; break; case nvrtcResult.ErrorCompilation: message = "Compilation error."; break; case nvrtcResult.ErrorBuiltinOperationFailure: message = "Builtin operation failure."; break; case nvrtcResult.NoLoweredNamesBeforeCompilation: message = "No lowered names before compilation error."; break; case nvrtcResult.NoNameExpressionsAfterCompilation: message = "No name expressions after compilation error."; break; case nvrtcResult.ExpressionNotValid: message = "Expression not valid error."; break; case nvrtcResult.InternalError: message = "Internal error."; break; default: break; } return(error.ToString() + ": " + message); }
/// <summary/> public void AddNameExpression(string nameExpression) { res = NVRTCNativeMethods.nvrtcAddNameExpression(_program, nameExpression); Debug.Write(""); //Line(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nvrtcAddNameExpression", res)); if (res != nvrtcResult.Success) { throw new NVRTCException(res); } }
/// <summary> /// Creates a runtime compiler instance. /// </summary> /// <param name="src">CUDA program source.</param> /// <param name="name">CUDA program name.<para/> /// name can be NULL; "default_program" is used when name is NULL.</param> /// <param name="includeNames">Sources of the headers.</param> /// <param name="headers">Name of each header by which they can be included in the CUDA program source.</param> public CudaRuntimeCompiler(string src, string name, string[] headers, string[] includeNames) { int headerCount = 0; IntPtr[] headersPtr = null; IntPtr[] includeNamesPtr = null; try { if (headers != null && includeNames != null) { if (headers.Length != includeNames.Length) throw new ArgumentException("headers and includeNames must have same length."); if (headers == null) throw new ArgumentNullException("headers can't be NULL if includeNames is not NULL"); if (includeNames == null) throw new ArgumentNullException("includeNames can't be NULL if headers is not NULL"); headerCount = headers.Length; headersPtr = new IntPtr[headerCount]; includeNamesPtr = new IntPtr[headerCount]; for (int i = 0; i < headerCount; i++) { headersPtr[i] = Marshal.StringToHGlobalAnsi(headers[i]); includeNamesPtr[i] = Marshal.StringToHGlobalAnsi(includeNames[i]); } } _program = new nvrtcProgram(); res = NVRTCNativeMethods.nvrtcCreateProgram(ref _program, src, name, headerCount, headersPtr, includeNamesPtr); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nvrtcCreateProgram", res)); } finally { if (headersPtr != null) for (int i = 0; i < headersPtr.Length; i++) { Marshal.FreeHGlobal(headersPtr[i]); } if (includeNamesPtr != null) for (int i = 0; i < includeNamesPtr.Length; i++) { Marshal.FreeHGlobal(includeNamesPtr[i]); } } if (res != nvrtcResult.Success) throw new NVRTCException(res); }
/// <summary/> public static Version GetVersion() { int major = 0; int minor = 0; nvrtcResult res = NVRTCNativeMethods.nvrtcVersion(ref major, ref minor); Debug.Write(""); //Line(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nvrtcVersion", res)); if (res != nvrtcResult.Success) { throw new NVRTCException(res); } return(new Version(major, minor)); }
/// <summary> /// For IDisposable /// </summary> /// <param name="fDisposing"></param> protected virtual void Dispose(bool fDisposing) { if (fDisposing && !disposed) { //Ignore if failing res = NVRTCNativeMethods.nvrtcDestroyProgram(ref _program); Debug.Write(""); //Line(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nvrtcDestroyProgram", res)); disposed = true; } if (!fDisposing && !disposed) { Debug.Write("");//Line(String.Format("ManagedCUDA not-disposed warning: {0}", this.GetType())); } }
/// <summary/> public string GetLoweredName(string nameExpression) { IntPtr ret = new IntPtr(); res = NVRTCNativeMethods.nvrtcGetLoweredName(_program, nameExpression, ref ret); Debug.Write(""); //Line(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nvrtcGetLoweredName", res)); if (res != nvrtcResult.Success) { throw new NVRTCException(res); } //ret ptr is freed when _program is destroyed! return(Marshal.PtrToStringAnsi(ret)); }
private static string GetErrorMessageFromCUResult(nvrtcResult error) { string message = string.Empty; switch (error) { case nvrtcResult.Success: message = "No Error."; break; case nvrtcResult.ErrorOutOfMemory: message = "Error out of memory."; break; case nvrtcResult.ErrorProgramCreationFailure: message = "Program creation failure."; break; case nvrtcResult.ErrorInvalidInput: message = "Invalid Input."; break; case nvrtcResult.ErrorInvalidProgram: message = "Invalid program."; break; case nvrtcResult.ErrorInvalidOption: message = "Invalid option."; break; case nvrtcResult.ErrorCompilation: message = "Compilation error."; break; case nvrtcResult.ErrorBuiltinOperationFailure: message = "Builtin operation failure."; break; default: break; } return(error.ToString() + ": " + message); }
/// <summary/> public byte[] GetNVVM() { SizeT nvvmSize = new SizeT(); res = NVRTCNativeMethods.nvrtcGetNVVMSize(_program, ref nvvmSize); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nvrtcGetNVVMSize", res)); if (res != nvrtcResult.Success) { throw new NVRTCException(res); } byte[] nvvmCode = new byte[nvvmSize]; res = NVRTCNativeMethods.nvrtcGetNVVM(_program, nvvmCode); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nvrtcGetNVVM", res)); if (res != nvrtcResult.Success) { throw new NVRTCException(res); } return(nvvmCode); }
/// <summary/> public byte[] GetPTX() { SizeT ptxSize = new SizeT(); res = NVRTCNativeMethods.nvrtcGetPTXSize(_program, ref ptxSize); Debug.Write(""); //Line(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nvrtcGetPTXSize", res)); if (res != nvrtcResult.Success) { throw new NVRTCException(res); } byte[] ptxCode = new byte[ptxSize]; res = NVRTCNativeMethods.nvrtcGetPTX(_program, ptxCode); Debug.Write(""); //Line(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nvrtcGetPTX", res)); if (res != nvrtcResult.Success) { throw new NVRTCException(res); } return(ptxCode); }
/// <summary/> public byte[] GetCubin() { SizeT cubinSize = new SizeT(); res = NVRTCNativeMethods.nvrtcGetCUBINSize(_program, ref cubinSize); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nvrtcGetCUBINSize", res)); if (res != nvrtcResult.Success) { throw new NVRTCException(res); } byte[] cubinCode = new byte[cubinSize]; res = NVRTCNativeMethods.nvrtcGetCUBIN(_program, cubinCode); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nvrtcGetCUBIN", res)); if (res != nvrtcResult.Success) { throw new NVRTCException(res); } return(cubinCode); }
/// <summary/> public byte[] GetLog() { SizeT logSize = new SizeT(); res = NVRTCNativeMethods.nvrtcGetProgramLogSize(_program, ref logSize); Debug.Write(""); //Line(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nvrtcGetProgramLogSize", res)); if (res != nvrtcResult.Success) { throw new NVRTCException(res); } byte[] logCode = new byte[logSize]; res = NVRTCNativeMethods.nvrtcGetProgramLog(_program, logCode); Debug.Write(""); //Line(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nvrtcGetProgramLog", res)); if (res != nvrtcResult.Success) { throw new NVRTCException(res); } return(logCode); }
public string Compile(string name, string src) { IntPtr program = CreateProgram(src, name); int numOptions = optionList.Count; nvrtcResult result = NVRTC.API.nvrtcCompileProgram( program, numOptions, optionList.ToArray() ); if (result != nvrtcResult.NVRTC_SUCCESS) { log = GetLog(program); return(null); } log = GetLog(program); string ptx = GetPTX(program); NVRTC.DestroyProgram(program); return(ptx); }
/// <summary/> public void Compile(string[] options) { int optionCount = 0; IntPtr[] optionsPtr = null; try { if (options != null) { optionCount = options.Length; optionsPtr = new IntPtr[optionCount]; for (int i = 0; i < optionCount; i++) { optionsPtr[i] = Marshal.StringToHGlobalAnsi(options[i]); } } res = NVRTCNativeMethods.nvrtcCompileProgram(_program, optionCount, optionsPtr); Debug.Write(""); //Line(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nvrtcCompileProgram", res)); } finally { if (optionsPtr != null) { for (int i = 0; i < optionsPtr.Length; i++) { Marshal.FreeHGlobal(optionsPtr[i]); } } } if (res != nvrtcResult.Success) { throw new NVRTCException(res); } }
/// <summary> /// /// </summary> /// <param name="error"></param> public NVRTCException(nvrtcResult error) : base(GetErrorMessageFromCUResult(error)) { this._NVRTCError = error; }
/// <summary> /// Creates a runtime compiler instance. /// </summary> /// <param name="src">CUDA program source.</param> /// <param name="name">CUDA program name.<para/> /// name can be NULL; "default_program" is used when name is NULL.</param> /// <param name="includeNames">Sources of the headers.</param> /// <param name="headers">Name of each header by which they can be included in the CUDA program source.</param> public CudaRuntimeCompiler(string src, string name, string[] headers, string[] includeNames) { int headerCount = 0; IntPtr[] headersPtr = null; IntPtr[] includeNamesPtr = null; try { if (headers != null && includeNames != null) { if (headers.Length != includeNames.Length) { throw new ArgumentException("headers and includeNames must have same length."); } if (headers == null) { throw new ArgumentNullException("headers can't be NULL if includeNames is not NULL"); } if (includeNames == null) { throw new ArgumentNullException("includeNames can't be NULL if headers is not NULL"); } headerCount = headers.Length; headersPtr = new IntPtr[headerCount]; includeNamesPtr = new IntPtr[headerCount]; for (int i = 0; i < headerCount; i++) { headersPtr[i] = Marshal.StringToHGlobalAnsi(headers[i]); includeNamesPtr[i] = Marshal.StringToHGlobalAnsi(includeNames[i]); } } _program = new nvrtcProgram(); res = NVRTCNativeMethods.nvrtcCreateProgram(ref _program, src, name, headerCount, headersPtr, includeNamesPtr); Debug.Write(""); //Line(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nvrtcCreateProgram", res)); } finally { if (headersPtr != null) { for (int i = 0; i < headersPtr.Length; i++) { Marshal.FreeHGlobal(headersPtr[i]); } } if (includeNamesPtr != null) { for (int i = 0; i < includeNamesPtr.Length; i++) { Marshal.FreeHGlobal(includeNamesPtr[i]); } } } if (res != nvrtcResult.Success) { throw new NVRTCException(res); } }
/// <summary/> public void AddNameExpression(string nameExpression) { res = NVRTCNativeMethods.nvrtcAddNameExpression(_program, nameExpression); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nvrtcAddNameExpression", res)); if (res != nvrtcResult.Success) throw new NVRTCException(res); }
/// <summary> /// /// </summary> /// <param name="error"></param> /// <param name="message"></param> /// <param name="exception"></param> public NVRTCException(nvrtcResult error, string message, Exception exception) : base(message, exception) { this._NVRTCError = error; }
/// <summary/> public byte[] GetPTX() { SizeT ptxSize = new SizeT(); res = NVRTCNativeMethods.nvrtcGetPTXSize(_program, ref ptxSize); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nvrtcGetPTXSize", res)); if (res != nvrtcResult.Success) throw new NVRTCException(res); byte[] ptxCode = new byte[ptxSize]; res = NVRTCNativeMethods.nvrtcGetPTX(_program, ptxCode); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nvrtcGetPTX", res)); if (res != nvrtcResult.Success) throw new NVRTCException(res); return ptxCode; }
/// <summary> /// For IDisposable /// </summary> /// <param name="fDisposing"></param> protected virtual void Dispose(bool fDisposing) { if (fDisposing && !disposed) { //Ignore if failing res = NVRTCNativeMethods.nvrtcDestroyProgram(ref _program); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nvrtcDestroyProgram", res)); disposed = true; } if (!fDisposing && !disposed) Debug.WriteLine(String.Format("ManagedCUDA not-disposed warning: {0}", this.GetType())); }
/// <summary/> public string GetLoweredName(string nameExpression) { IntPtr ret = new IntPtr(); res = NVRTCNativeMethods.nvrtcGetLoweredName(_program, nameExpression, ref ret); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nvrtcGetLoweredName", res)); if (res != nvrtcResult.Success) throw new NVRTCException(res); //ret ptr is freed when _program is destroyed! return Marshal.PtrToStringAnsi(ret); }
/// <summary/> public byte[] GetLog() { SizeT logSize = new SizeT(); res = NVRTCNativeMethods.nvrtcGetProgramLogSize(_program, ref logSize); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nvrtcGetProgramLogSize", res)); if (res != nvrtcResult.Success) throw new NVRTCException(res); byte[] logCode = new byte[logSize]; res = NVRTCNativeMethods.nvrtcGetProgramLog(_program, logCode); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nvrtcGetProgramLog", res)); if (res != nvrtcResult.Success) throw new NVRTCException(res); return logCode; }
/// <summary/> public void Compile(string[] options) { int optionCount = 0; IntPtr[] optionsPtr = null; try { if (options != null) { optionCount = options.Length; optionsPtr = new IntPtr[optionCount]; for (int i = 0; i < optionCount; i++) { optionsPtr[i] = Marshal.StringToHGlobalAnsi(options[i]); } } res = NVRTCNativeMethods.nvrtcCompileProgram(_program, optionCount, optionsPtr); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nvrtcCompileProgram", res)); } finally { if (optionsPtr != null) for (int i = 0; i < optionsPtr.Length; i++) { Marshal.FreeHGlobal(optionsPtr[i]); } } if (res != nvrtcResult.Success) throw new NVRTCException(res); }
internal static extern IntPtr nvrtcGetErrorStringInternal(nvrtcResult result);
private static string GetErrorMessageFromCUResult(nvrtcResult error) { string message = string.Empty; switch (error) { case nvrtcResult.Success: message = "No Error."; break; case nvrtcResult.ErrorOutOfMemory: message = "Error out of memory."; break; case nvrtcResult.ErrorProgramCreationFailure: message = "Program creation failure."; break; case nvrtcResult.ErrorInvalidInput: message = "Invalid Input."; break; case nvrtcResult.ErrorInvalidProgram: message = "Invalid program."; break; case nvrtcResult.ErrorInvalidOption: message = "Invalid option."; break; case nvrtcResult.ErrorCompilation: message = "Compilation error."; break; case nvrtcResult.ErrorBuiltinOperationFailure: message = "Builtin operation failure."; break; case nvrtcResult.NoLoweredNamesBeforeCompilation: message = "No lowered names before compilation error."; break; case nvrtcResult.NoNameExpressionsAfterCompilation: message = "No name expressions after compilation error."; break; case nvrtcResult.ExpressionNotValid: message = "Expression not valid error."; break; case nvrtcResult.InternalError: message = "Internal error."; break; default: break; } return error.ToString() + ": " + message; }
/// <summary> /// helper function that stringifies the given #nvrtcResult code, e.g., NVRTC_SUCCESS to /// "NVRTC_SUCCESS". For unrecognized enumeration values, it returns "NVRTC_ERROR unknown" /// </summary> /// <param name="result">CUDA Runtime Compiler API result code.</param> /// <returns>Message string for the given nvrtcResult code.</returns> public static string nvrtcGetErrorString(nvrtcResult result) { IntPtr ptr = nvrtcGetErrorStringInternal(result); return Marshal.PtrToStringAnsi(ptr); }
public static extern IntPtr nvrtcGetErrorString(nvrtcResult result);
private static string GetErrorMessageFromCUResult(nvrtcResult error) { string message = string.Empty; switch (error) { case nvrtcResult.Success: message = "No Error."; break; case nvrtcResult.ErrorOutOfMemory: message = "Error out of memory."; break; case nvrtcResult.ErrorProgramCreationFailure: message = "Program creation failure."; break; case nvrtcResult.ErrorInvalidInput: message = "Invalid Input."; break; case nvrtcResult.ErrorInvalidProgram: message = "Invalid program."; break; case nvrtcResult.ErrorInvalidOption: message = "Invalid option."; break; case nvrtcResult.ErrorCompilation: message = "Compilation error."; break; case nvrtcResult.ErrorBuiltinOperationFailure: message = "Builtin operation failure."; break; default: break; } return error.ToString() + ": " + message; }
/// <summary> /// helper function that stringifies the given #nvrtcResult code, e.g., NVRTC_SUCCESS to /// "NVRTC_SUCCESS". For unrecognized enumeration values, it returns "NVRTC_ERROR unknown" /// </summary> /// <param name="result">CUDA Runtime Compiler API result code.</param> /// <returns>Message string for the given nvrtcResult code.</returns> public static string nvrtcGetErrorString(nvrtcResult result) { IntPtr ptr = nvrtcGetErrorStringInternal(result); return(Marshal.PtrToStringAnsi(ptr)); }
// ----- C# Interface /// <summary> /// nvrtcGetErrorString is a helper function that returns a string describing the given nvrtcResult code, /// e.g., NVRTC_SUCCESS to "NVRTC_SUCCESS". For unrecognized enumeration values, it returns "NVRTC_ERROR unknown". /// </summary> /// <param name="result">CUDA Runtime Compilation API result code.</param> /// <returns>Message string for the given nvrtcResult code.</returns> public static string GetErrorString(nvrtcResult result) { IntPtr ptr = API.nvrtcGetErrorString(result); return(Marshal.PtrToStringAnsi(ptr)); }