Library/ManagedCuda/CudaContext.cs
/// <summary>
/// Load a ptx module from image as byte[]
/// </summary>
/// <param name="moduleImage"></param>
/// <param name="options"></param>
/// <param name="values"></param>
/// <returns></returns>
Load a ptx module from image as byte[]
Load a ptx module from image as byte []
public CUmodule LoadModulePTX(byte[] moduleImage, CUJITOption[] options, object[] values)
		{
			if (disposed) throw new ObjectDisposedException(this.ToString());
			bool noOptions = options == null || values == null;
			if (!noOptions)
				if (options.Length != values.Length)
					throw new ArgumentException("options array and values array must have same size.");
			CUResult res;
			CUmodule hcuModule = new CUmodule();
			moduleImage = AddZeroToArray(moduleImage);
			if (noOptions)
			{
				res = DriverAPINativeMethods.ModuleManagement.cuModuleLoadDataEx(ref hcuModule, moduleImage, 0, null, null);
				Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuModuleLoadDataEx", res));
			}
			else
			{
				IntPtr[] valuesIntPtr = new IntPtr[values.Length];
				GCHandle[] gcHandles = new GCHandle[values.Length];
				#region Convert OptionValues
				for (int i = 0; i < values.Length; i++)
				{
					switch (options[i])
					{
						case CUJITOption.MaxRegisters:
							valuesIntPtr[i] = (IntPtr)(Convert.ToUInt32(values[i], System.Globalization.CultureInfo.InvariantCulture));
							break;
						case CUJITOption.ThreadsPerBlock:
							valuesIntPtr[i] = (IntPtr)(Convert.ToUInt32(values[i], System.Globalization.CultureInfo.InvariantCulture));
							break;
						case CUJITOption.WallTime:
							valuesIntPtr[i] = new IntPtr();
							break;
						case CUJITOption.InfoLogBuffer:
							gcHandles[i] = GCHandle.Alloc(values[i], GCHandleType.Pinned);
							valuesIntPtr[i] = gcHandles[i].AddrOfPinnedObject();
							break;
						case CUJITOption.InfoLogBufferSizeBytes:
							valuesIntPtr[i] = (IntPtr)(Convert.ToUInt32(values[i], System.Globalization.CultureInfo.InvariantCulture));
							break;
						case CUJITOption.ErrorLogBuffer:
							gcHandles[i] = GCHandle.Alloc(values[i], GCHandleType.Pinned);
							valuesIntPtr[i] = gcHandles[i].AddrOfPinnedObject();
							break;
						case CUJITOption.ErrorLogBufferSizeBytes:
							valuesIntPtr[i] = (IntPtr)(Convert.ToUInt32(values[i], System.Globalization.CultureInfo.InvariantCulture));
							break;
						case CUJITOption.OptimizationLevel:
							valuesIntPtr[i] = (IntPtr)(Convert.ToUInt32(values[i], System.Globalization.CultureInfo.InvariantCulture));
							break;
						case CUJITOption.TargetFromContext:
							valuesIntPtr[i] = (IntPtr)(Convert.ToUInt32(values[i], System.Globalization.CultureInfo.InvariantCulture));
							break;
						case CUJITOption.Target:
							valuesIntPtr[i] = (IntPtr)(Convert.ToUInt32(values[i], System.Globalization.CultureInfo.InvariantCulture));
							break;
						case CUJITOption.FallbackStrategy:
							valuesIntPtr[i] = (IntPtr)(Convert.ToUInt32(values[i], System.Globalization.CultureInfo.InvariantCulture));
							break;
						case CUJITOption.GenerateDebugInfo:
							valuesIntPtr[i] = (IntPtr)(Convert.ToUInt32(values[i], System.Globalization.CultureInfo.InvariantCulture));
							break;
						case CUJITOption.LogVerbose:
							valuesIntPtr[i] = (IntPtr)(Convert.ToUInt32(values[i], System.Globalization.CultureInfo.InvariantCulture));
							break;
						case CUJITOption.GenerateLineInfo:
							valuesIntPtr[i] = (IntPtr)(Convert.ToUInt32(values[i], System.Globalization.CultureInfo.InvariantCulture));
							break;
						case CUJITOption.JITCacheMode:
							valuesIntPtr[i] = (IntPtr)(Convert.ToUInt32(values[i], System.Globalization.CultureInfo.InvariantCulture));
							break;
						default:
							break;
					}
				}
				#endregion
				res = DriverAPINativeMethods.ModuleManagement.cuModuleLoadDataEx(ref hcuModule, moduleImage, (uint)options.Length, options, valuesIntPtr);
				Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuModuleLoadDataEx", res));
				#region Convert back OptionValues
				for (int i = 0; i < values.Length; i++)
				{
					switch (options[i])
					{
						case CUJITOption.ErrorLogBuffer:
							gcHandles[i].Free();
							break;
						case CUJITOption.InfoLogBuffer:
							gcHandles[i].Free();
							break;
						case CUJITOption.ErrorLogBufferSizeBytes:
							values[i] = (uint)valuesIntPtr[i];
							break;
						case CUJITOption.InfoLogBufferSizeBytes:
							values[i] = (uint)valuesIntPtr[i];
							break;
						case CUJITOption.ThreadsPerBlock:
							values[i] = (uint)valuesIntPtr[i];
							break;
						case CUJITOption.WallTime:
							uint test = (uint)valuesIntPtr[i];
							byte[] bytes = BitConverter.GetBytes(test);
							values[i] = BitConverter.ToSingle(bytes, 0);
							break;
					}
				}
				#endregion
			}
			if (res != CUResult.Success)
				throw new CudaException(res);
			return hcuModule;
		}
public CUmodule LoadModulePTX ( byte [ ] moduleImage , CUJITOption [ ] options , object [ ] values ) { if ( disposed ) throw new ObjectDisposedException ( this . ToString ( ) ) ; bool noOptions = options == null || values == null ; if ( ! noOptions ) if ( options . Length != values . Length ) throw new ArgumentException ( " " ) ; CUResult res ; CUmodule hcuModule = new CUmodule ( ) ; moduleImage = AddZeroToArray ( moduleImage ) ; if ( noOptions ) { res = DriverAPINativeMethods . ModuleManagement . cuModuleLoadDataEx ( ref hcuModule , moduleImage , 0 , null , null ) ; Debug . WriteLine ( String . Format ( " " , DateTime . Now , " " , res ) ) ; } else { IntPtr [ ] valuesIntPtr = new IntPtr [ values . Length ] ; GCHandle [ ] gcHandles = new GCHandle [ values . Length ] ; region  Convert OptionValues for ( int i = 0 ; i < values . Length ; i ++ ) { switch ( options [ i ] ) { case CUJITOption . MaxRegisters : valuesIntPtr [ i ] = ( IntPtr ) ( Convert . ToUInt32 ( values [ i ] , System . Globalization . CultureInfo . InvariantCulture ) ) ; break ; case CUJITOption . ThreadsPerBlock : valuesIntPtr [ i ] = ( IntPtr ) ( Convert . ToUInt32 ( values [ i ] , System . Globalization . CultureInfo . InvariantCulture ) ) ; break ; case CUJITOption . WallTime : valuesIntPtr [ i ] = new IntPtr ( ) ; break ; case CUJITOption . InfoLogBuffer : gcHandles [ i ] = GCHandle . Alloc ( values [ i ] , GCHandleType . Pinned ) ; valuesIntPtr [ i ] = gcHandles [ i ] . AddrOfPinnedObject ( ) ; break ; case CUJITOption . InfoLogBufferSizeBytes : valuesIntPtr [ i ] = ( IntPtr ) ( Convert . ToUInt32 ( values [ i ] , System . Globalization . CultureInfo . InvariantCulture ) ) ; break ; case CUJITOption . ErrorLogBuffer : gcHandles [ i ] = GCHandle . Alloc ( values [ i ] , GCHandleType . Pinned ) ; valuesIntPtr [ i ] = gcHandles [ i ] . AddrOfPinnedObject ( ) ; break ; case CUJITOption . ErrorLogBufferSizeBytes : valuesIntPtr [ i ] = ( IntPtr ) ( Convert . ToUInt32 ( values [ i ] , System . Globalization . CultureInfo . InvariantCulture ) ) ; break ; case CUJITOption . OptimizationLevel : valuesIntPtr [ i ] = ( IntPtr ) ( Convert . ToUInt32 ( values [ i ] , System . Globalization . CultureInfo . InvariantCulture ) ) ; break ; case CUJITOption . TargetFromContext : valuesIntPtr [ i ] = ( IntPtr ) ( Convert . ToUInt32 ( values [ i ] , System . Globalization . CultureInfo . InvariantCulture ) ) ; break ; case CUJITOption . Target : valuesIntPtr [ i ] = ( IntPtr ) ( Convert . ToUInt32 ( values [ i ] , System . Globalization . CultureInfo . InvariantCulture ) ) ; break ; case CUJITOption . FallbackStrategy : valuesIntPtr [ i ] = ( IntPtr ) ( Convert . ToUInt32 ( values [ i ] , System . Globalization . CultureInfo . InvariantCulture ) ) ; break ; case CUJITOption . GenerateDebugInfo : valuesIntPtr [ i ] = ( IntPtr ) ( Convert . ToUInt32 ( values [ i ] , System . Globalization . CultureInfo . InvariantCulture ) ) ; break ; case CUJITOption . LogVerbose : valuesIntPtr [ i ] = ( IntPtr ) ( Convert . ToUInt32 ( values [ i ] , System . Globalization . CultureInfo . InvariantCulture ) ) ; break ; case CUJITOption . GenerateLineInfo : valuesIntPtr [ i ] = ( IntPtr ) ( Convert . ToUInt32 ( values [ i ] , System . Globalization . CultureInfo . InvariantCulture ) ) ; break ; case CUJITOption . JITCacheMode : valuesIntPtr [ i ] = ( IntPtr ) ( Convert . ToUInt32 ( values [ i ] , System . Globalization . CultureInfo . InvariantCulture ) ) ; break ; default : break ; } } endregion res = DriverAPINativeMethods . ModuleManagement . cuModuleLoadDataEx ( ref hcuModule , moduleImage , ( uint ) options . Length , options , valuesIntPtr ) ; Debug . WriteLine ( String . Format ( " " , DateTime . Now , " " , res ) ) ; region  Convert back OptionValues for ( int i = 0 ; i < values . Length ; i ++ ) { switch ( options [ i ] ) { case CUJITOption . ErrorLogBuffer : gcHandles [ i ] . Free ( ) ; break ; case CUJITOption . InfoLogBuffer : gcHandles [ i ] . Free ( ) ; break ; case CUJITOption . ErrorLogBufferSizeBytes : values [ i ] = ( uint ) valuesIntPtr [ i ] ; break ; case CUJITOption . InfoLogBufferSizeBytes : values [ i ] = ( uint ) valuesIntPtr [ i ] ; break ; case CUJITOption . ThreadsPerBlock : values [ i ] = ( uint ) valuesIntPtr [ i ] ; break ; case CUJITOption . WallTime : uint test = ( uint ) valuesIntPtr [ i ] ; byte [ ] bytes = BitConverter . GetBytes ( test ) ; values [ i ] = BitConverter . ToSingle ( bytes , 0 ) ; break ; } } endregion } if ( res != CUResult . Success ) throw new CudaException ( res ) ; return hcuModule ; }
Load a ptx module from image as byte[]
Load a ptx module from image as byte []
//Append zero to make image always zero terminated:
None
moduleImage
byte[]
None
options
CUJITOption[]
None
values
object[]
None