/// <summary> /// Constructs a new CPU runtime. /// </summary> /// <param name="context">The ILGPU context.</param> /// <param name="numThreads">The number of threads for paralllel processing.</param> /// <param name="warpSize">The number of threads per warp.</param> /// <param name="flags">The compile-unit flags.</param> public CPUAccelerator( Context context, int numThreads, int warpSize, CompileUnitFlags flags) : this(context, numThreads, warpSize, ThreadPriority.Normal, flags) { }
/// <summary> /// Constructs a new CPU runtime. /// </summary> /// <param name="context">The ILGPU context.</param> /// <param name="numThreads">The number of threads for paralllel processing.</param> /// <param name="threadPriority">The thread priority of the execution threads.</param> /// <param name="flags">The compile-unit flags.</param> public CPUAccelerator( Context context, int numThreads, ThreadPriority threadPriority, CompileUnitFlags flags) : this(context, numThreads, 1, threadPriority, flags) { }
/// <summary> /// Constructs a new CPU runtime. /// </summary> /// <param name="context">The ILGPU context.</param> /// <param name="numThreads">The number of threads for paralllel processing.</param> /// <param name="warpSize">The number of threads per warp.</param> /// <param name="threadPriority">The thread priority of the execution threads.</param> /// <param name="flags">The compile-unit flags.</param> public CPUAccelerator( Context context, int numThreads, int warpSize, ThreadPriority threadPriority, CompileUnitFlags flags) : base(context, AcceleratorType.CPU) { if (numThreads < 1) { throw new ArgumentOutOfRangeException(nameof(numThreads)); } if (!CPURuntimeWarpContext.IsValidWarpSize(warpSize) || numThreads < warpSize || (numThreads % warpSize) != 0) { throw new ArgumentOutOfRangeException(nameof(warpSize)); } // Setup assembly and module builder for dynamic code generation var assemblyName = new AssemblyName(nameof(CPUAccelerator)); assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run); moduleBuilder = assemblyBuilder.DefineDynamicModule(nameof(CPUAccelerator)); NumThreads = numThreads; WarpSize = warpSize; threads = new Thread[numThreads]; finishedEvent = new Barrier(numThreads + 1); // Every thread requires a custom warp context. warpContexts = new CPURuntimeWarpContext[numThreads]; // The maximum number of thread groups that can be handled in parallel is // equal to the number of available threads in the worst case. groupContexts = new CPURuntimeGroupContext[numThreads]; for (int i = 0; i < numThreads; ++i) { warpContexts[i] = new CPURuntimeWarpContext(this); groupContexts[i] = new CPURuntimeGroupContext(this); var thread = threads[i] = new Thread(ExecuteThread) { IsBackground = true, Priority = threadPriority, }; thread.Name = "ILGPUExecutionThread" + i; thread.Start(i); } DefaultStream = CreateStream(); Name = nameof(CPUAccelerator); MemorySize = long.MaxValue; MaxGridSize = new Index3(int.MaxValue, int.MaxValue, int.MaxValue); MaxNumThreadsPerGroup = NumThreads; MaxSharedMemoryPerGroup = int.MaxValue; MaxConstantMemory = int.MaxValue; NumMultiprocessors = 1; MaxNumThreadsPerMultiprocessor = NumThreads; Bind(); InitBackend(CreateBackend(), flags); }
/// <summary> /// Creates a new compile unit that targets the given backend. /// </summary> /// <param name="backend">The target backend.</param> /// <param name="unitFlags">The compile-unit flags.</param> /// <param name="name">The name of the compile unit.</param> /// <returns>The created compile unit.</returns> public CompileUnit CreateCompileUnit(Backend backend, CompileUnitFlags unitFlags, string name) { if (backend == null) { throw new ArgumentNullException(nameof(backend)); } if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentNullException(nameof(name)); } return(new CompileUnit(this, name, backend, deviceFunctions, deviceTypes, unitFlags)); }
/// <summary> /// Verifies a static-field store operation. /// </summary> /// <param name="compilationContext">The current compilation context.</param> /// <param name="flags">The current compile unit flags.</param> /// <param name="field">The static field to store to.</param> public static void VerifyStaticFieldStore( CompilationContext compilationContext, CompileUnitFlags flags, FieldInfo field) { Debug.Assert(compilationContext != null, "Invalid compilation context"); Debug.Assert(field != null || !field.IsStatic, "Invalid field"); if ((flags & CompileUnitFlags.IgnoreStaticFieldStores) != CompileUnitFlags.IgnoreStaticFieldStores) { throw compilationContext.GetNotSupportedException( ErrorMessages.NotSupportedStoreToStaticField, field); } }
/// <summary> /// Constructs a new Cuda accelerator. /// </summary> /// <param name="context">The ILGPU context.</param> /// <param name="deviceId">The target device id.</param> /// <param name="acceleratorFlags">The accelerator flags.</param> /// <param name="flags">The compile-unit flags.</param> public CudaAccelerator( Context context, int deviceId, CudaAcceleratorFlags acceleratorFlags, CompileUnitFlags flags) : base(context, AcceleratorType.Cuda) { CudaException.ThrowIfFailed( CurrentAPI.CreateContext(out contextPtr, acceleratorFlags, deviceId)); DeviceId = deviceId; SetupAccelerator(); InitBackend(CreateBackend(), flags); }
/// <summary> /// Verifies a static-field load operation. /// </summary> /// <param name="compilationContext">The current compilation context.</param> /// <param name="flags">The current compile unit flags.</param> /// <param name="field">The static field to load.</param> public static void VerifyStaticFieldLoad( CompilationContext compilationContext, CompileUnitFlags flags, FieldInfo field) { Debug.Assert(compilationContext != null, "Invalid compilation context"); Debug.Assert(field != null || !field.IsStatic, "Invalid field"); if ((field.Attributes & FieldAttributes.InitOnly) != FieldAttributes.InitOnly && (flags & CompileUnitFlags.InlineMutableStaticFieldValues) != CompileUnitFlags.InlineMutableStaticFieldValues) { throw compilationContext.GetNotSupportedException( ErrorMessages.NotSupportedLoadOfStaticField, field); } }
/// <summary> /// Constructs a new compile unit using the provided method. /// </summary> /// <param name="context">The parent context.</param> /// <param name="name">The name of the unit.</param> /// <param name="backend">The final backend.</param> /// <param name="deviceFunctions">The device functions to use.</param> /// <param name="deviceTypes">The device types to use.</param> /// <param name="flags">The compile-unit flags.</param> internal CompileUnit( Context context, string name, Backend backend, IReadOnlyList <IDeviceFunctions> deviceFunctions, IReadOnlyList <IDeviceTypes> deviceTypes, CompileUnitFlags flags) { Debug.Assert(context != null && backend != null && !string.IsNullOrWhiteSpace(name)); Name = name; Flags = flags; Context = context; Backend = backend; LLVMModule = ModuleCreateWithNameInContext(name, context.LLVMContext); typeMapping.Add(typeof(object), new MappedType( typeof(object), StructCreateNamed(LLVMContext, "Object"), 0, null)); this.deviceFunctions = new List <IDeviceFunctions>(deviceFunctions.Count + 1); for (int i = 0, e = deviceFunctions.Count; i < e; ++i) { this.deviceFunctions.Add(deviceFunctions[i]); } backend.TargetUnit(this); this.deviceTypes = new List <IDeviceTypes>(deviceTypes.Count + 1); for (int i = 0, e = deviceTypes.Count; i < e; ++i) { this.deviceTypes.Add(deviceTypes[i]); } CodeGenFunctionPassManager = CreateFunctionPassManagerForModule(LLVMModule); #if DEBUG AddVerifierPass(CodeGenFunctionPassManager); #endif AddPromoteMemoryToRegisterPass(CodeGenFunctionPassManager); AddCFGSimplificationPass(CodeGenFunctionPassManager); AddScalarReplAggregatesPassSSA(CodeGenFunctionPassManager); abiSpecification = backend.CreateABISpecification(this); CompilationContext = new CompilationContext(this); }
/// <summary> /// Constructs a new Cuda accelerator. /// </summary> /// <param name="context">The ILGPU context.</param> /// <param name="d3d11Device">A pointer to a valid D3D11 device.</param> /// <param name="acceleratorFlags">The accelerator flags.</param> /// <param name="flags">The compile-unit flags.</param> public CudaAccelerator( Context context, IntPtr d3d11Device, CudaAcceleratorFlags acceleratorFlags, CompileUnitFlags flags) : base(context, AcceleratorType.Cuda) { if (d3d11Device == IntPtr.Zero) { throw new ArgumentNullException(nameof(d3d11Device)); } CudaException.ThrowIfFailed( CurrentAPI.CreateContextD3D11(out contextPtr, out int deviceId, acceleratorFlags, d3d11Device)); DeviceId = deviceId; SetupAccelerator(); InitBackend(CreateBackend(), flags); }
/// <summary> /// Creates the specified accelerator using the provided accelerator id. /// </summary> /// <param name="context">The ILGPU context.</param> /// <param name="acceleratorId">The specified accelerator id.</param> /// <param name="flags">The compile-unit flags.</param> /// <returns>The created accelerator.</returns> public static Accelerator Create( Context context, AcceleratorId acceleratorId, CompileUnitFlags flags) { if (context == null) { throw new ArgumentNullException(nameof(context)); } switch (acceleratorId.AcceleratorType) { case AcceleratorType.CPU: return(new CPU.CPUAccelerator(context, flags)); case AcceleratorType.Cuda: return(new Cuda.CudaAccelerator(context, acceleratorId.DeviceId, flags)); default: throw new ArgumentException(RuntimeErrorMessages.NotSupportedTargetAccelerator, nameof(acceleratorId)); } }
/// <summary> /// Returns true iff the given flags are set. /// </summary> /// <param name="flags">The flags to check.</param> /// <returns>True, iff the given flags are set.</returns> public bool HasFlags(CompileUnitFlags flags) { return((Flags & flags) == flags); }
/// <summary> /// Constructs a new Cuda accelerator. /// </summary> /// <param name="context">The ILGPU context.</param> /// <param name="deviceId">The target device id.</param> /// <param name="flags">The compile-unit flags.</param> public CudaAccelerator(Context context, int deviceId, CompileUnitFlags flags) : this(context, deviceId, CudaAcceleratorFlags.ScheduleAuto, flags) { }
/// <summary> /// Creates a new compile unit that targets the given backend. /// </summary> /// <param name="backend">The target backend.</param> /// <param name="unitFlags">The compile-unit flags.</param> /// <returns>The created compile unit.</returns> public CompileUnit CreateCompileUnit(Backend backend, CompileUnitFlags unitFlags) { return(CreateCompileUnit(backend, unitFlags, $"ILGPUUnit{compileUnitCounter++}")); }
/// <summary> /// Initializes the associated backend. /// </summary> /// <param name="compilationBackend">The associated backend.</param> /// <param name="flags">The compile-unit flags.</param> protected void InitBackend(Backend compilationBackend, CompileUnitFlags flags) { backend = compilationBackend; compileUnit = Context.CreateCompileUnit(backend, flags); }
/// <summary> /// Constructs a new Cuda accelerator. /// </summary> /// <param name="context">The ILGPU context.</param> /// <param name="d3d11Device">A pointer to a valid D3D11 device.</param> /// <param name="flags">The compile-unit flags.</param> public CudaAccelerator(Context context, IntPtr d3d11Device, CompileUnitFlags flags) : this(context, d3d11Device, CudaAcceleratorFlags.ScheduleAuto) { }
/// <summary> /// Constructs a new CPU runtime. /// </summary> /// <param name="context">The ILGPU context.</param> /// <param name="flags">The compile-unit flags.</param> public CPUAccelerator(Context context, CompileUnitFlags flags) : this(context, Environment.ProcessorCount, flags) { }
/// <summary> /// Constructs a new Cuda accelerator targeting the default device. /// </summary> /// <param name="context">The ILGPU context.</param> /// <param name="flags">The compile-unit flags.</param> public CudaAccelerator(Context context, CompileUnitFlags flags) : this(context, 0, flags) { }