/// <summary> /// Private Constructor /// </summary> Camera() { try { // pre-allocate and initialize the variables and data structures for brightness correction imgGPU = worker.Malloc <byte>(new byte[640 * 640]); meanImg = worker.Malloc <float>(new float[640 * 640]); addReduce = DeviceSumModuleF32.Default.Create(numPixels); correctionFactor = worker.Malloc <float>(640 * 640); float[] temp = new float[640 * 640]; for (int i = 0; i < temp.Length; i++) { temp[i] = 1; } correctionFactor.Scatter(temp); scalarOutput = worker.Malloc <float>(1); if (File.Exists("correctionFactor.dat")) { FileStream stream = new FileStream("correctionFactor.dat", FileMode.Open); byte[] buffer = new byte[640 * 640 * 4]; stream.Read(buffer, 0, (int)Math.Min(buffer.Length, stream.Length)); for (int i = 0; i < 640 * 640; i++) { temp[i] = BitConverter.ToSingle(buffer, 4 * i); } stream.Close(); correctionFactor.Scatter(temp); } // initialize CUDA parameters var blockDims = new dim3(32, 32); var gridDims = new dim3(Common.divup(640, blockDims.x), Common.divup(640, blockDims.y)); lp = new LaunchParam(gridDims, blockDims); // set up the camera parameters and events provider = new IduleProviderCsCam(0); provider.Initialize(); if (provider.IsConnected) { provider.ImageTransaction += provider_ImageTransaction; provider.Interrupt += provider_Interrupt; provider.Exception += camera_Exception; provider.WriteRegister(new NanEyeGSRegisterPayload(false, 0x05, true, 0, prescaler)); provider.WriteRegister(new NanEyeGSRegisterPayload(false, 0x06, true, 0, exposure)); ProcessingWrapper.pr[0].ReduceProcessing = true; } } catch (Exception ex) { OnError(ex.Message); } }
protected override bool Execute(Assignment assignment, ILValue <T> output) { var input = assignment.GetInput(Input).ToRValue(); var reduce = Reduction; var reductionIndices = ReductionIndices; var shape = Shape; // unit stride case and reduction is full reduction (to scalar) if (input.Layout.IsFullyUnitStride && shape.Rank == 0) { var length = input.Layout.Shape.Length; Util.EnsureTrue(length > 0L); var read = input.BufferReader.GetFlatReader1(); var write = output.Buffer.FlatWriter1; if (assignment.Context.Type == ContextType.Gpu) { var stream = assignment.Context.ToGpuContext().Stream; var numItems = (int)length; Func <int, T> sourceOp = i => read(i); Action <int, T> outputOp = (i, value) => write(i, value); // first pass, get the size of temp memory var tempStorageSize = 0; DeviceReduce.Reduce(stream, new deviceptr <byte>(), ref tempStorageSize, numItems, sourceOp, outputOp, reduce); // now allocate temp memory using (var tempMemoryRcpt = assignment.Context.Device.ToGpuDevice().MemoryRepository.Acquire <byte>(tempStorageSize)) { DeviceReduce.Reduce(stream, tempMemoryRcpt.Memory.Ptr, ref tempStorageSize, numItems, sourceOp, outputOp, reduce); } return(true); } if (assignment.Context.Type == ContextType.Cpu) { var acc = read(0L); for (var i = 1L; i < length; ++i) { acc = reduce(acc, read(i)); } write(0L, acc); return(true); } } // currently we only support matrix partial reduce, need TODO to fix this with more generic cases if (input.Layout.IsFullyUnitStride && input.Layout.Rank == 2 && reductionIndices.Length == 1) { var rows = input.Layout.Shape[0]; var cols = input.Layout.Shape[1]; var read = input.BufferReader.GetReader2(); var write = output.Buffer.FlatWriter1; if (reductionIndices[0] == 1) { if (assignment.Context.Type == ContextType.Gpu) { var stream = assignment.Context.ToGpuContext().Stream; var numSegments = (int)rows; var numItems = (int)cols; Func <int, int, T> sourceOp = (i, j) => read(i, j); Action <int, int, T> outputOp = (i, _, value) => write(i, value); // first pass, get the size of temp memory var tempStorageSize = 0; DeviceReduce.Reduce(stream, new deviceptr <byte>(), ref tempStorageSize, numSegments, numItems, sourceOp, outputOp, reduce); // now allocate temp memory // TODO: move to assigmnet, manage the temp memory using (var tempMemoryRcpt = assignment.Context.Device.ToGpuDevice().MemoryRepository.Acquire <byte>(tempStorageSize)) { DeviceReduce.Reduce(stream, tempMemoryRcpt.Memory.Ptr, ref tempStorageSize, numSegments, numItems, sourceOp, outputOp, reduce); } return(true); } if (assignment.Context.Type == ContextType.Cpu) { for (var row = 0L; row < rows; ++row) { var acc = read(row, 0L); for (var col = 1L; col < cols; ++col) { acc = reduce(acc, read(row, col)); } write(row, acc); } return(true); } } if (reductionIndices[0] == 0) { if (assignment.Context.Type == ContextType.Gpu) { var stream = assignment.Context.ToGpuContext().Stream; // This is a quick fix, it is not good performance stream.LongFor(0L, cols, col => { var acc = read(0L, col); for (var row = 1L; row < rows; ++row) { acc = reduce(acc, read(row, col)); } write(col, acc); }); return(true); } if (assignment.Context.Type == ContextType.Cpu) { for (var col = 0L; col < cols; ++col) { var acc = read(0L, col); for (var row = 1L; row < rows; ++row) { acc = reduce(acc, read(row, col)); } write(col, acc); } return(true); } } } return(false); }