private static int[] Process(Template tempate, float[] series, float[][] clusters, float error) { var clustersHeight = clusters.Length; var clustersWidth = clusters[0].Length; var clusters2d = new float[clustersHeight, clustersWidth]; for (int i = 0; i < clustersHeight; i++) { for (int j = 0; j < clustersWidth; j++) { clusters2d[i, j] = clusters[i][j]; } } using (var context = new Context()) { using (var accelerator = new CPUAccelerator(context)) { var paintKernel = accelerator.LoadAutoGroupedStreamKernel <Index, Template, ArrayView <float>, ArrayView2D <float>, ArrayView <int>, float>(PaintKernel); using (var seriesBuffer = accelerator.Allocate <float>(series.Count())) using (var clustersBuffer = accelerator.Allocate <float>(clustersHeight, clustersWidth)) using (var buffer = accelerator.Allocate <int>(series.Count())) { seriesBuffer.CopyFrom(series, 0, 0, series.Count()); clustersBuffer.CopyFrom(clusters2d, new Index2(0, 0), new Index2(0, 0), new Index2(clustersHeight, clustersWidth)); paintKernel(clustersHeight, tempate, seriesBuffer, clustersBuffer, buffer, error); accelerator.Synchronize(); var data = buffer.GetAsArray(); return(data); } } } }
public static int[] Process(IList <Template> tempate, IList <float[][]> clusterCollection, float[] series, float error) { var templatesCount = clusterCollection.Count; var clustersCount = clusterCollection.Max(x => x.Length); var pointsInClusters = clusterCollection[0][0].Length; float[,,] clsts = new float[templatesCount, clustersCount, pointsInClusters]; for (int k = 0; k < templatesCount; k++) { for (int i = 0; i < clustersCount; i++) { if (i >= clusterCollection[k].Length) { break; } for (int j = 0; j < pointsInClusters; j++) { clsts[k, i, j] = clusterCollection[k][i][j]; } } } using (var context = new Context()) { using (var accelerator = new CPUAccelerator(context)) { var paintKernel = accelerator.LoadAutoGroupedStreamKernel <Index2, ArrayView <Template>, ArrayView <float>, ArrayView3D <float>, ArrayView <int>, float>(PaintKernel2d); using (var seriesBuffer = accelerator.Allocate <float>(series.Count())) using (var templatesBuffer = accelerator.Allocate <Template>(templatesCount)) using (var clustersBuffer = accelerator.Allocate <float>(templatesCount, clustersCount, pointsInClusters)) using (var buffer = accelerator.Allocate <int>(series.Count())) { seriesBuffer.CopyFrom(series, 0, 0, series.Count()); clustersBuffer.CopyFrom(clsts, new Index3(0, 0, 0), new Index3(0, 0, 0), new Index3(templatesCount, clustersCount, pointsInClusters)); paintKernel(new Index2(templatesCount, clustersCount), templatesBuffer, seriesBuffer, clustersBuffer, buffer, error); accelerator.Synchronize(); var data = buffer.GetAsArray(); return(data); } } } }
/// <summary> /// Uses the CPU accelerator to allocate pinned chunks of memory in CPU host memory. /// </summary> /// <param name="accelerator">The current accelerator.</param> /// <param name="dataSize">The number of elements to copy.</param> static void PerformPinnedCopyToCPUAccelerator(Accelerator accelerator, int dataSize) { using (var cpuAccl = new CPUAccelerator(accelerator.Context)) { // All buffers allocated through the CPUAccelerator class are automatically pinned // in memory to enable async memory transfers via AcceleratorStreams using (var pinnedCPUBuffer = cpuAccl.Allocate <int>(dataSize)) { var stream = accelerator.DefaultStream; // Allocate buffer on this device using (var bufferOnGPU = accelerator.Allocate <int>(pinnedCPUBuffer.Length)) { // Use an accelerator stream to perform an async copy operation. // Note that you should use the CopyTo function from the associated GPU // buffer to perform the copy operation using the associated accelerator stream. bufferOnGPU.CopyTo(stream, pinnedCPUBuffer, 0); // // Perform other operations... // // Wait for the copy operation to finish stream.Synchronize(); } } } }
/// <summary> /// Demonstrates the use of array views. Operations on array views are /// supported on all accelerators. /// </summary> static void Main() { // Create main context using (var context = new Context()) { // We perform all operations in CPU memory here using (var accelerator = new CPUAccelerator(context)) { using (var buffer = accelerator.Allocate <int>(1024)) { // Retrieve a view to the whole buffer. ArrayView <int> bufferView = buffer.View; // Note that accessing an array view which points to memory // that is not accessible in the current context triggers // an invalid access exception. // For instance, array views that point to CUDA memory are // inaccessible from the CPU by default (and vice-versa). // We can ignore this restriction in the current context since we // perform all operations in CPU memory. // Perform some unsafe operations on array views. UnsafeAccess(bufferView); // SubView access SubViewAccess(bufferView); // VariableView access VariableViewAccess(bufferView); // Perform some unsafe operations on variable views. UnsafeVariableViewAccess(bufferView); } } } }