private static IEnumerable <Index3D> SpiralColumns(Index3D lowerBounds, Index3D upperBounds) { Contracts.Requires.That(lowerBounds.X <= upperBounds.X); Contracts.Requires.That(lowerBounds.Y <= upperBounds.Y); Contracts.Requires.That(lowerBounds.Z <= upperBounds.Z); Contracts.Requires.That(MiddleIndex.Get(lowerBounds, upperBounds, RoundUp) == Index3D.Zero); foreach (var index in Spiral(lowerBounds.ProjectDownYAxis(), upperBounds.ProjectDownYAxis())) { yield return(new Index3D(index.X, 0, index.Y)); int yUp = 1; int yDown = -1; bool continueLoop = true; while (continueLoop) { continueLoop = false; if (yUp <= upperBounds.Y) { yield return(new Index3D(index.X, yUp, index.Y)); yUp++; continueLoop = true; } if (yDown >= lowerBounds.Y) { yield return(new Index3D(index.X, yDown, index.Y)); yDown--; continueLoop = true; } } } }
/// <summary> /// Init grid information. /// </summary> private void InitGridInfo() { // Resolve max grid size MaxGridSize = new Index3D( CurrentAPI.GetDeviceAttribute( DeviceAttributeKind.CU_DEVICE_ATTRIBUTE_MAX_GRID_DIM_X, DeviceId), CurrentAPI.GetDeviceAttribute( DeviceAttributeKind.CU_DEVICE_ATTRIBUTE_MAX_GRID_DIM_Y, DeviceId), CurrentAPI.GetDeviceAttribute( DeviceAttributeKind.CU_DEVICE_ATTRIBUTE_MAX_GRID_DIM_Z, DeviceId)); // Resolve max group size MaxGroupSize = new Index3D( CurrentAPI.GetDeviceAttribute( DeviceAttributeKind.CU_DEVICE_ATTRIBUTE_MAX_BLOCK_DIM_X, DeviceId), CurrentAPI.GetDeviceAttribute( DeviceAttributeKind.CU_DEVICE_ATTRIBUTE_MAX_BLOCK_DIM_Y, DeviceId), CurrentAPI.GetDeviceAttribute( DeviceAttributeKind.CU_DEVICE_ATTRIBUTE_MAX_BLOCK_DIM_Z, DeviceId)); // Resolve max threads per group MaxNumThreadsPerGroup = CurrentAPI.GetDeviceAttribute( DeviceAttributeKind.CU_DEVICE_ATTRIBUTE_MAX_THREADS_PER_BLOCK, DeviceId); }
/// <summary> /// Initializes a new instance of the <see cref="Array3D{TValue}"/> class. /// </summary> /// <param name="dimensions">The dimensions of the array.</param> public Array3D(Index3D dimensions) : this(new T[dimensions.X, dimensions.Y, dimensions.Z]) { Contracts.Requires.That(dimensions.IsAllPositiveOrZero()); }
/// <summary> /// Initializes a new instance of the <see cref="ReadOnlyCompositeArray3D{TValue}" /> class. /// </summary> /// <param name="arrays">The indexable arrays to make the composite indexable out of.</param> /// <param name="originOffset">The origin offset for indexing into the composite.</param> public ReadOnlyCompositeArray3D(IBoundedReadOnlyIndexable <Index3D, T>[,,] arrays, Index3D originOffset) { Contracts.Requires.That(arrays != null); Contracts.Requires.That(CompositeArray.AreAllSameDimensionsAndZeroBounded(arrays)); this.arrays = arrays; IBoundedReadOnlyIndexable <Index3D, T> singleArray = this.arrays[0, 0, 0]; this.singleArrayLengthX = singleArray.GetLength(Axis3D.X); this.singleArrayLengthY = singleArray.GetLength(Axis3D.Y); this.singleArrayLengthZ = singleArray.GetLength(Axis3D.Z); this.originOffset = originOffset; this.dimensions = singleArray.Dimensions * this.arrays.GetDimensions(); this.lowerBounds = -this.originOffset; this.upperBounds = this.lowerBounds + this.dimensions; }
/// <inheritdoc /> protected override DynamicArray3D <T> Create(Index3D dimensions) => new DynamicArray3D <T>(dimensions);
public static void SetValue <T>(this T[,,] array, T value, Index3D index) => array[index.X, index.Y, index.Z] = value;
/// <inheritdoc /> public bool IsIndexValid(Index3D index) { IReadOnlyIndexableContracts.IsIndexValid(this, index); return(this.IsIndexInBounds(index)); }
/// <inheritdoc /> public void SetNode(Index3D index, int depth, T value) { throw new NotImplementedException(); }
/// <summary> /// Initializes a new instance of the <see cref="SectorData" /> class. /// </summary> /// <param name="index">The index.</param> public SectorData(Index3D index) : base("Sector {0}".Inject(index)) { SectorIndex = index; }
/// <inheritdoc /> public void SetNode(Index3D index, int depth, T value) => IIndexableTreeContracts.SetNode(this, index, depth);
/// <inheritdoc /> public TreeNode <T> GetNode(Index3D index) { throw new NotImplementedException(); }
/// <summary> /// Determines whether the specified index is within the current bounds. /// </summary> /// <param name="bounds">The bounds to check against.</param> /// <param name="index">The index to check.</param> /// <returns> /// <c>true</c> if the index is within the current bounds; otherwise, <c>false</c>. /// </returns> public static bool IsIndexInCurrentBounds(this IDynamicIndexingBounds <Index3D> bounds, Index3D index) { Contracts.Requires.That(bounds != null); return(index.IsIn(bounds.CurrentLowerBounds, bounds.CurrentUpperBounds)); }
public int3 GetBlockAtIndex3D(Index3D id) { int3 coords = new int3(id.x, id.y, id.z); return(blocks[Utils.to1D(coords)]); }
public static Vector3 ToVector(this Index3D index) => new Vector3(index.X, index.Y, index.Z);
public VoxelGridChunkResourcesFactory(IRasterChunkConfig <Index3D> config) { Contracts.Requires.That(config != null); this.dimensions = config.Bounds.Dimensions; }
/// <inheritdoc /> public IEnumerator <IVoxelProjection <TVoxel, TSurfaceData> > GetEnumerator() { ReusableVoxelProjection <TVoxel, TSurfaceData> projection = new ReusableVoxelProjection <TVoxel, TSurfaceData>( new TSurfaceData(), this.voxels, this.stageIndexOrigin); for (int iY = this.start.Y; iY < this.end.Y; iY++) { for (int iZ = this.start.Z; iZ < this.end.Z; iZ++) { for (int iX = this.start.X; iX < this.end.X; iX++) { Index3D negativeEndOfEdge = new Index3D(iX, iY, iZ); Index3D positiveEndOfEdge; // x-axis positiveEndOfEdge = new Index3D(iX + 1, iY, iZ); switch (this.determiner.DetermineContour(this.voxels[negativeEndOfEdge], this.voxels[positiveEndOfEdge])) { case ContourFacingDirection.TowardsNegative: projection.SetupProjectionTowardsNegativeXAxis(positiveEndOfEdge); yield return(projection); break; case ContourFacingDirection.TowardsPositive: projection.SetupProjectionTowardsPositiveXAxis(negativeEndOfEdge); yield return(projection); break; case ContourFacingDirection.Both: projection.SetupProjectionTowardsNegativeXAxis(positiveEndOfEdge); yield return(projection); projection.SetupProjectionTowardsPositiveXAxis(negativeEndOfEdge); yield return(projection); break; } // z-axis positiveEndOfEdge = new Index3D(iX, iY, iZ + 1); switch (this.determiner.DetermineContour(this.voxels[negativeEndOfEdge], this.voxels[positiveEndOfEdge])) { case ContourFacingDirection.TowardsNegative: projection.SetupProjectionTowardsNegativeZAxis(positiveEndOfEdge); yield return(projection); break; case ContourFacingDirection.TowardsPositive: projection.SetupProjectionTowardsPositiveZAxis(negativeEndOfEdge); yield return(projection); break; case ContourFacingDirection.Both: projection.SetupProjectionTowardsNegativeZAxis(positiveEndOfEdge); yield return(projection); projection.SetupProjectionTowardsPositiveZAxis(negativeEndOfEdge); yield return(projection); break; } // y-axis positiveEndOfEdge = new Index3D(iX, iY + 1, iZ); switch (this.determiner.DetermineContour(this.voxels[negativeEndOfEdge], this.voxels[positiveEndOfEdge])) { case ContourFacingDirection.TowardsNegative: projection.SetupProjectionTowardsNegativeYAxis(positiveEndOfEdge); yield return(projection); break; case ContourFacingDirection.TowardsPositive: projection.SetupProjectionTowardsPositiveYAxis(negativeEndOfEdge); yield return(projection); break; case ContourFacingDirection.Both: projection.SetupProjectionTowardsNegativeYAxis(positiveEndOfEdge); yield return(projection); projection.SetupProjectionTowardsPositiveYAxis(negativeEndOfEdge); yield return(projection); break; } } } } }
public static Index2D ProjectDownYAxis(this Index3D index) => new Index2D(index.X, index.Z);
/// <summary> /// Initializes a new instance of the <see cref="SectorItemData" /> class. /// </summary> /// <param name="index">The index.</param> public SectorItemData(Index3D index) : base("Sector {0}".Inject(index)) { SectorIndex = index; base.Topography = Topography.OpenSpace; }
/// <inheritdoc /> public bool TrySetValue(Index3D index, T value) => IndexableUtilities.TrySetValue(this, index, value);
/// <summary> /// Initializes a new instance of the <see cref="DynamicArray3D{TValue}"/> class. /// </summary> /// <param name="dimensions">The dimensions of the array.</param> public DynamicArray3D(Index3D dimensions) { Contracts.Requires.That(dimensions.IsAllPositive()); this.array = new T[dimensions.X, dimensions.Y, dimensions.Z]; }
/// <inheritdoc /> public bool IsIndexValid(Index3D index) { throw new NotImplementedException(); }
/// <inheritdoc /> public override bool IsIndexValid(Index3D index) { IReadOnlyIndexableContracts.IsIndexValid(this, index); return(index.IsAllPositiveOrZero()); }
/// <inheritdoc /> public bool TrySetValue(Index3D index, T value) { throw new NotImplementedException(); }
/// <summary> /// Entry point for a single processing thread. /// </summary> /// <param name="arg">The absolute thread index.</param> private void ExecuteThread(object arg) { // Get the current thread information int absoluteThreadIndex = (int)arg; int threadIdx = absoluteThreadIndex % MaxNumThreadsPerMultiprocessor; var processorIdx = absoluteThreadIndex / MaxNumThreadsPerMultiprocessor; var processorBarrier = processorBarriers[processorIdx]; bool isMainThread = threadIdx == 0; // Setup a new thread context for this thread and initialize the lane index int laneIdx = threadIdx % WarpSize; var threadContext = new CPURuntimeThreadContext(laneIdx) { LinearGroupIndex = threadIdx }; threadContext.MakeCurrent(); // Setup the current warp context as it always stays the same int warpIdx = threadIdx / WarpSize; bool isMainWarpThread = threadIdx == 0; var warpContext = warpContexts[processorIdx, warpIdx]; warpContext.MakeCurrent(); // Setup the current group context as it always stays the same var groupContext = groupContexts[processorIdx]; groupContext.MakeCurrent(); CPUAcceleratorTask task = null; for (; ;) { // Get a new task to execute lock (taskSynchronizationObject) { while ((currentTask == null | currentTask == task) & running) { Monitor.Wait(taskSynchronizationObject); } if (!running) { break; } task = currentTask; } Debug.Assert(task != null, "Invalid task"); // Setup the current group index threadContext.GroupIndex = Index3D.ReconstructIndex( threadIdx, task.GroupDim); // Wait for all threads of all multiprocessors to arrive here Thread.MemoryBarrier(); processorBarrier.SignalAndWait(); try { // If we are an active group thread int groupSize = task.GroupDim.Size; if (threadIdx < groupSize) { try { var launcher = task.KernelExecutionDelegate; // Split the grid into different chunks that will be processed // by the available multiprocessors int linearGridDim = task.GridDim.Size; int gridChunkSize = IntrinsicMath.DivRoundUp( linearGridDim, NumMultiprocessors); int gridOffset = gridChunkSize * processorIdx; int linearUserDim = task.TotalUserDim.Size; for ( int i = gridOffset, e = gridOffset + gridChunkSize; i < e; ++i) { groupContext.BeginThreadProcessing(); try { // Setup the current grid index threadContext.GridIndex = Index3D.ReconstructIndex( i, task.GridDim); // Invoke the actual kernel launcher int globalIndex = i * groupSize + threadIdx; if (globalIndex < linearUserDim) { launcher(task, globalIndex); } } finally { groupContext.EndThreadProcessing(); } } } finally { // This thread has already finished processing groupContext.FinishThreadProcessing(); warpContext.FinishThreadProcessing(); } } } finally { // Wait for all threads of all multiprocessors to arrive here processorBarrier.SignalAndWait(); // If we reach this point and we are the main thread, notify the // parent accelerator instance if (isMainThread) { finishedEventPerMultiprocessor.SignalAndWait(); } } } }
public static T GetValue <T>(this T[,,] array, Index3D index) => array[index.X, index.Y, index.Z];
/// <summary> /// Initializes a new instance of the <see cref="ReadOnlyConstantArray3D{TValue}"/> class. /// </summary> /// <param name="dimensions">The dimensions of the indexable.</param> /// <param name="value">The value the indexable will always return.</param> public ReadOnlyConstantArray3D(Index3D dimensions, T value) : base(dimensions, value) { }
public ChunkConglomerator(Func <Index3D, Task <IDisposableValue <TChunk> > > pinChunk, Index3D dimensionsInChunks) { Contracts.Requires.That(pinChunk != null); Contracts.Requires.That(dimensionsInChunks.IsAllPositive()); this.pinChunk = pinChunk; this.dimensionsInChunks = dimensionsInChunks; this.numberOfChunks = this.dimensionsInChunks.MultiplyCoordinates(); }
/// <summary> /// Initializes a new instance of the <see cref="ConstantArray3D{TValue}"/> class. /// </summary> /// <param name="dimensions">The dimensions of the indexable.</param> /// <param name="value">The value the indexable will always return.</param> /// <param name="setThrowsException"> /// True if attempting to set the value at an index should throw an exception, /// false to just ignore the attempt. /// </param> public ConstantArray3D(Index3D dimensions, T value, bool setThrowsException = true) : base(dimensions, value, setThrowsException) { }
/// <inheritdoc /> public bool TryGetValue(Index3D index, out T value) => ReadOnlyIndexableUtilities.TryGetValue(this, index, out value);
public IndexingBounds3D(Index3D dimensions) : this(Index3D.Zero, dimensions) { }
/// <summary> /// Initializes a new instance of the <see cref="SystemItemData" /> class. /// </summary> /// <param name="systemName">Name of the system.</param> /// <param name="sectorIndex">Index of the sector.</param> /// <param name="topography">The topography.</param> public SystemItemData(string systemName, Index3D sectorIndex, Topography topography) : base(systemName) { SectorIndex = sectorIndex; base.Topography = topography; Subscribe(); }
/// <inheritdoc /> public new T this[Index3D index] { get { return(base[index]); } set { IIndexableContracts.IndexerSet(this, index); } }
/// <summary> /// Converts the index to a corresponding <see cref="Vector3"/>. /// </summary> /// <param name="index">The source index.</param> /// <returns>The converted <see cref="Vector3"/>.</returns> public static Vector3 ToVector(this Index3D index) { return(new Vector3(index.X, index.Y, index.Z)); }
public TmpCopyInfo(Index3D offset1, Index3D offset2, Size3D size) { this.Offset1 = offset1; this.Offset2 = offset2; this.Size = size; }