/// <summary> /// Set the default function for a GPIO pin /// </summary> /// <remarks> /// Allows gpio pins to be assigned a device function. /// For example setting the I2C1 data pin to use GPIO pin 17. /// </remarks> /// <param name="pin"></param> /// <param name="value"></param> public static void SetPinFunction(int pin, DeviceFunction value) { GpioPin gpioPin = GpioController.GetDefault().OpenPin(pin); gpioPin.SetAlternateFunction((int)value); gpioPin.Dispose(); }
//这里先对二维数组进行降维(PACK方法)处理,变成一维数组,效率会提高很多,这里直接计算出A和B的列数输入进去 private static void KernelPacked(double[] a, double[] b, double[] c, int colsA, int colsB, int colsC) { //获取当前内核的块的位置 var blockRow = blockIdx.x; var blockCol = blockIdx.y; var valueC = 0.0; //获取当前块的线程的位置 var row = threadIdx.x; var col = threadIdx.y; for (var m = 0; m < DivUp(colsA, BlockSize); ++m) { var subA = __shared__.Array2D <double>(BlockSize, BlockSize); var subB = __shared__.Array2D <double>(BlockSize, BlockSize); subA[row, col] = GetMatrixElement(colsA, a, blockRow, m, row, col); subB[row, col] = GetMatrixElement(colsB, b, m, blockCol, row, col); //同步线程,等所有线程都拿到数据再开始计算 DeviceFunction.SyncThreads(); //计算对应的行和列的乘积然后求和 for (var e = 0; e < BlockSize; ++e) { valueC += subA[row, e] * subB[e, col]; //valueC = col; } //同步线程,得出结果 DeviceFunction.SyncThreads(); } SetMatrixElement(colsC, c, blockRow, blockCol, row, col, valueC); }
/// <summary> /// Execute a function on a device /// </summary> /// <param name="context">The user context.</param> /// <param name="deviceData">The device data.</param> /// <param name="function">The function to execute.</param> /// <param name="argument">The argument to the function</param> /// <returns> /// A task. /// </returns> public Task ExecuteFunction(UserContext context, DeviceImport deviceData, DeviceFunction function, object argument) { // Get the device meta for execute var meta = LifxMetaData.FromString(deviceData.MetaData); return(Handlers.ExecuteFunction(context, meta, function, argument)); }
static DeviceFunction GetDeviceFunction(Excel.Worksheet sheet, int row, int column, out bool correct) { DeviceFunction function = 0; correct = true; string deviceFunction = sheet.Cells[row, column].Text.ToString(); deviceFunction = deviceFunction.ToLower(); if (deviceFunction == "глюкометр") { function = DeviceFunction.Glucometer; } else { if (deviceFunction == "пульсометр") { function = DeviceFunction.HeartRateMonitor; } else { if (deviceFunction == "измеритель артериального давления" || deviceFunction == "тонометр") { function = DeviceFunction.BloodPressureMonitor; } else { correct = false; } } } return(function); }
private void OnMouseMove(object sender, MouseEventArgs e) { if (isMouseDown) { float2 offset = new float2((e.X - mouseLocation.x) * mouseSensitivity, (e.Y - mouseLocation.y) * mouseSensitivity); float c1 = DeviceFunction.Cos(offset.x); float s1 = DeviceFunction.Sin(offset.x); float t1 = 1 - c1; float c2 = DeviceFunction.Cos(offset.y); float s2 = DeviceFunction.Sin(offset.y); float t2 = 1 - c2; float camDist = L(camera); gpu.Launch(RotateDirection, launchParam, dirDevPtr, y, t1, c1, s1, width, height); x = RotateVec(x, y, t1, c1, s1); z = RotateVec(z, y, t1, c1, s1); camera = RotateVec(camera, y, t1, c1, s1); gpu.Launch(RotateDirection, launchParam, dirDevPtr, x, t2, c2, s2, width, height); y = RotateVec(y, x, t2, c2, s2); z = RotateVec(z, x, t2, c2, s2); camera = RotateVec(camera, x, t2, c2, s2); Gpu.Copy(dirDevMem, directions); x = D(x, L(x)); y = D(y, L(y)); z = D(z, L(z)); ScreenDivider.Panel2.Invalidate(); } mouseLocation = new int2(e.X, e.Y); }
public static float3 MinSpace(float3 a, float b) { a.x = DeviceFunction.Min(a.x, b); a.y = DeviceFunction.Min(a.y, b); a.z = DeviceFunction.Min(a.z, b); return(a); }
private void AddDevice_Click(object sender, EventArgs e) { int row = PatientsList.SelectedCells[0].RowIndex; int userCode = int.Parse(PatientsList.Rows[row].Cells[0].Value.ToString()); Patient selectedPat = _container.PatientSet.Find(userCode); AddDeviceForm newDeviceType = new AddDeviceForm(); if (newDeviceType.ShowDialog() != DialogResult.Cancel) { DeviceFunction type = newDeviceType.function; bool exist = false; foreach (Device d in selectedPat.Device) { if (d.Function == type) { exist = true; } } if (exist) { MessageBox.Show("Данный тип прибора уже закреплен за пациентом."); } else { Device newDevice = InformationSystem.CreateDevice(selectedPat, type); InformationSystem.AddDevice(newDevice, selectedPat); } } }
private void ColorPass(float[] eniromentLight, float[] color, float[] normal, float[] p3D, float[] tex, bool specular, int from) { ArrayMath.Sub(_lightPosArray, p3D, _l, 0, from, from, 3); ArrayMath.Normalize(_l, from, 3); var dotNl = ArrayMath.Dot(normal, _l, from, from, 3); ArrayMath.Mul(color, Math.Max(0f, dotNl), _Id, from, from, 3); ArrayMath.Mul(normal, dotNl * 2, _r, from, from, 3); ArrayMath.Sub(_r, _l, _r, from, from, from, 3); ArrayMath.Normalize(_r, from, 3); ArrayMath.Normalize(p3D, from, 3); var rDotP3D = -ArrayMath.Dot(_r, p3D, from, from, 3); var Is = 0f; if (specular) { var max = Math.Max(0, rDotP3D); Is = DeviceFunction.Pow(max, 50); } ArrayMath.Mul(eniromentLight, color, color, 0, from, from, 3); ArrayMath.Mul(tex, _Id, tex, from, from, from, 3); ArrayMath.Add(color, tex, color, from, from, from, 3); ArrayMath.Add(color, Is, color, from, 3); ArrayMath.Mul(color, 255, color, from, from, 3); }
private static void backward_bias_kernel(float[] biasUpdates, float[] delta, int batch, int n, int size) { var part = __shared__.Array <float>(CudaUtils.BlockSize); int i, b; int filter = blockIdx.x; int p = threadIdx.x; float sum = 0; for (b = 0; b < batch; ++b) { for (i = 0; i < size; i += CudaUtils.BlockSize) { int index = p + i + size * (filter + n * b); sum += (p + i < size) ? delta[index] : 0; } } part[p] = sum; DeviceFunction.SyncThreads(); if (p == 0) { for (i = 0; i < CudaUtils.BlockSize; ++i) { biasUpdates[filter] += part[i]; } } }
public static void Kernel(double[] positionsx, double[] positionsy, double[] velocitiesx, double[] velocitiesy, double[] accelerationsx, double[] accelerationsy, int width, int height, float mousex, float mousey, int[,] squareFish, int[] squaresStart, int[] fishInSquare, int squaresInRow, int squaresNumber, int[] bitmap) { int ind = blockIdx.x * blockDim.x + threadIdx.x; FishFunctions.AlignWithOtherFish(positionsx, positionsy, velocitiesx, velocitiesy, accelerationsx, accelerationsy, ind, squareFish, squaresStart, fishInSquare, squaresInRow, squaresNumber); FishFunctions.CohesionWithOtherFish(positionsx, positionsy, velocitiesx, velocitiesy, accelerationsx, accelerationsy, ind, squaresStart, squareFish, fishInSquare, squaresNumber, squaresInRow); FishFunctions.AvoidOtherFish(positionsx, positionsy, velocitiesx, velocitiesy, accelerationsx, accelerationsy, ind, squaresStart, squareFish, fishInSquare, squaresInRow, squaresNumber); if (mousex >= 0 && mousey >= 0) { FishFunctions.AvoidMouse(positionsx, positionsy, velocitiesx, velocitiesy, accelerationsx, accelerationsy, ind, mousex, mousey); } FishFunctions.UpdateFish(positionsx, positionsy, velocitiesx, velocitiesy, accelerationsx, accelerationsy, ind, width, height, mousex, mousey); FishFunctions.Edges(positionsx, positionsy, ind, width, height); DeviceFunction.SyncThreads(); int col = (255 << 24) + (0 << 16) + (255 << 8) + 255; int x = (int)positionsx[ind]; int y = (int)positionsy[ind]; CircleDrawing.CircleBresenham(x, y, 2, bitmap, width, height, col); }
public static float CubeDE(float3 p, float3 c, float di) { float3 o = S(p, c); float d = DeviceFunction.Max(DeviceFunction.Abs(o.x), DeviceFunction.Max(DeviceFunction.Abs(o.y), DeviceFunction.Abs(o.z))); return(d - di / 2); }
public static float3 MaxSpace(float3 a, float3 b) { a.x = DeviceFunction.Max(a.x, b.x); a.y = DeviceFunction.Max(a.y, b.y); a.z = DeviceFunction.Max(a.z, b.z); return(a); }
public static float OldSoftShadow(float3 p, float3 d, float shadowStrength, int iterations, float side, float3 seed, float3 shift, float minDist, float maxDist, float minAngle) { float k = 1; float dist = minDist; float angle = 1; float totalDist = minDist / 100; float3 marchedPoint = p; while (totalDist < maxDist) { dist = ScaledDE(marchedPoint, iterations, side, seed, shift); if (dist == 0) { dist = minDist; } marchedPoint = A(marchedPoint, M(d, dist)); totalDist += dist; angle = shadowStrength * dist / totalDist; k = DeviceFunction.Min(k, angle); if (dist < 0) { return(0); } if (k < minAngle) { return(0); } } return(k); }
public static float NewSoftShadow(float3 p, float3 d, float shadowStrength, int iterations, float side, float3 seed, float3 shift, float minDist, float maxDist, float minAngle) { float darkness = 1; float prevDist = float.MaxValue; float dist = minDist / 100; float angle = 1; float totalDist = minDist; float oldNewIntDist = 0; float legLength = 0; while (totalDist < maxDist) { dist = ScaledDE(A(p, M(d, totalDist)), iterations, side, seed, shift); oldNewIntDist = dist * dist / (2 * prevDist); legLength = DeviceFunction.Sqrt(dist * dist - oldNewIntDist * oldNewIntDist); angle = shadowStrength * legLength / DeviceFunction.Max(0, totalDist - oldNewIntDist); darkness = DeviceFunction.Min(darkness, angle); prevDist = dist; totalDist += dist; if (dist < 0) { return(0); } if (darkness < minAngle) { return(0); } } return(darkness); }
private static void Kernel(long colsA, Func <long, long, T> getA, Func <long, long, T> getB, Action <long, long, T> setC, T zero, Func <T, T, T> add, Func <T, T, T> mul) { var blockRow = blockIdx.x; var blockCol = blockIdx.y; var valueC = zero; var row = threadIdx.x; var col = threadIdx.y; for (var m = 0; m < ScalarOps.DivUp(colsA, BlockSize); ++m) { var subA = __shared__.Array2D <T>(BlockSize, BlockSize); var subB = __shared__.Array2D <T>(BlockSize, BlockSize); subA[row, col] = getA(blockRow * BlockSize + row, m * BlockSize + col); subB[row, col] = getB(m * BlockSize + row, blockCol * BlockSize + col); DeviceFunction.SyncThreads(); for (var e = 0; e < BlockSize; ++e) { valueC = add(valueC, mul(subA[row, e], subB[e, col])); } DeviceFunction.SyncThreads(); } setC(blockRow * BlockSize + row, blockCol * BlockSize + col, valueC); }
public static void Kernel(double[] result, int n, double lb, double ub, double d) { var temp = __shared__.Array <double>(BlockSize); var start = blockIdx.x * blockDim.x + threadIdx.x; var step = gridDim.x * blockDim.x; for (int yi = start; yi < n; yi += step) { double y = (yi + 0.5) * d; double intSum = 0; for (int xi = 0; xi < n; xi++) { double x = (xi + 0.5) * d; intSum += CalkaGPU(x, y); } temp[threadIdx.x] += intSum * d * d; } DeviceFunction.SyncThreads(); if (threadIdx.x == 0) { for (int i = 0; i < BlockSize; i++) { result[blockIdx.x] += temp[i]; } } }
// ReSharper disable once SuggestBaseTypeForParameter private static void KernelSequentialReduceIdleThreads <T>(deviceptr <T> array, int length, T[] result, Func <T, T, T> op) { var shared = __shared__.ExternArray <T>(); var tid = threadIdx.x; var bid = blockIdx.x; var gid = 2 * blockDim.x * bid + tid; shared[tid] = (gid < length && gid + blockDim.x < length) ? op(array[gid], array[gid + blockDim.x]) : array[gid]; DeviceFunction.SyncThreads(); for (int s = blockDim.x / 2; s > 0; s >>= 1) { if (tid < s && gid + s < length) { shared[tid] = op(shared[tid], shared[tid + s]); } DeviceFunction.SyncThreads(); } if (tid == 0) { result[bid] = shared[0]; } }
// ReSharper disable once SuggestBaseTypeForParameter private static void KernelInterleavedAccess <T>(deviceptr <T> array, int length, T[] result, Func <T, T, T> op) { var shared = __shared__.ExternArray <T>(); var tid = threadIdx.x; var bid = blockIdx.x; var gid = blockDim.x * bid + tid; if (gid < length) { shared[tid] = array[gid]; } DeviceFunction.SyncThreads(); for (var s = 1; s < blockDim.x; s *= 2) { if (tid % (2 * s) == 0 && gid + s < length) { shared[tid] = op(shared[tid], shared[tid + s]); } DeviceFunction.SyncThreads(); } if (tid == 0) { result[bid] = shared[0]; } }
private static void AleaKernelFloat2( deviceptr <Real> mSquaredDistances, deviceptr <Real> mCoordinates, int c, int n, int pitch) { // Same as KernelSharedMemory, but one thread does two element in one by using float2 reads. var shared = DeviceFunction.AddressOfArray(__shared__.ExternArray <Real>()); var coordinatesI = shared.Ptr(0); var coordinatesJ = shared.Ptr(c * blockDim.x); var bI = blockIdx.y * blockDim.x; var bJ = blockIdx.x * blockDim.x; for (int k = 0; k != c; ++k) { if (bI + threadIdx.x < n) { coordinatesI[k * blockDim.x + threadIdx.x] = mCoordinates[k * n + bI + threadIdx.x]; } if (bJ + threadIdx.x < n) { coordinatesJ[k * blockDim.x + threadIdx.x] = mCoordinates[k * n + bJ + threadIdx.x]; } } DeviceFunction.SyncThreads(); var line = threadIdx.x / (blockDim.x / 2); var tid = threadIdx.x % (blockDim.x / 2); if (bJ + tid * 2 < n) { var coordinatesJ2 = coordinatesJ.Reinterpret <Real2>(); for (int i = line; i < blockDim.x && bI + i < n; i += 2) { Real dist0 = 0; Real dist1 = 0; for (int k = 0; k != c; ++k) { var coord1 = coordinatesI[k * blockDim.x + i]; var coord2 = coordinatesJ2[(k * blockDim.x / 2) + tid]; var diff = new Real2(coord1 - coord2.x, coord1 - coord2.y); dist0 += diff.x * diff.x; dist1 += diff.y * diff.y; } mSquaredDistances[(bI + i) * pitch + (bJ + 2 * tid + 0)] = dist0; mSquaredDistances[(bI + i) * pitch + (bJ + 2 * tid + 1)] = dist1; } } }
private static void AleaKernelConstants( deviceptr <Real> mSquaredDistances, deviceptr <Real> mCoordinates, Constant <int> c, int n, int pitch) { // Same as CudaKernelOptimised2, but the number of coordinates is given as a meta-constant. // Also, we write the results as float2. var shared = DeviceFunction.AddressOfArray(__shared__.ExternArray <Real>()); var coordinatesI = shared.Ptr(0); var coordinatesJ = shared.Ptr(c.Value * blockDim.x); var bI = blockIdx.y * blockDim.x; var bJ = blockIdx.x * blockDim.x; for (int k = 0; k != c.Value; ++k) { if (bI + threadIdx.x < n) { coordinatesI[k * blockDim.x + threadIdx.x] = mCoordinates[k * n + bI + threadIdx.x]; } if (bJ + threadIdx.x < n) { coordinatesJ[k * blockDim.x + threadIdx.x] = mCoordinates[k * n + bJ + threadIdx.x]; } } DeviceFunction.SyncThreads(); var line = threadIdx.x / (blockDim.x / 2); var tid = threadIdx.x % (blockDim.x / 2); if (bJ + tid * 2 < n) { var coordinatesJ2 = coordinatesJ.Reinterpret <Real2>(); for (int i = line; i < blockDim.x && bI + i < n; i += 2) { var dist = default(Real2); for (int k = 0; k != c.Value; ++k) { var coord1 = coordinatesI[k * blockDim.x + i]; var coord2 = coordinatesJ2[(k * blockDim.x / 2) + tid]; var diff = new Real2(coord1 - coord2.x, coord1 - coord2.y); dist.x += diff.x * diff.x; dist.y += diff.y * diff.y; } var dst = mSquaredDistances.Ptr((bI + i) * pitch + bJ).Reinterpret <Real2>(); dst[tid] = dist; } } }
public static Device CreateDevice(Patient owner, DeviceFunction function) { Device newDevice = new Device(); newDevice.OwnerCode = owner.UserCode; newDevice.Function = function; return(newDevice); }
private static void AleaKernelLocalMemory( deviceptr <Real> mSquaredDistances, deviceptr <Real> mCoordinates, Constant <int> dimX, Constant <int> c, int n, int pitch) { // Same as KernelConstants, but use both local and shared memory to increase the effective shared memory. var coordinatesI = __shared__.Array <Real>(c.Value * dimX.Value); var coordinatesJ = __local__.Array <Real2>(c.Value); var bI = blockIdx.y * dimX.Value; var bJ = blockIdx.x * dimX.Value; var line = threadIdx.x / (dimX.Value / 2); var tid = threadIdx.x % (dimX.Value / 2); var isActive = bJ + tid * 2 < n; for (int k = 0; k != c.Value; ++k) { if (bI + threadIdx.x < n) { coordinatesI[k * dimX.Value + threadIdx.x] = mCoordinates[k * n + bI + threadIdx.x]; } if (isActive) { var mCoordinates2 = mCoordinates.Reinterpret <Real2>(); coordinatesJ[k] = mCoordinates2[(k * n + bJ) / 2 + tid]; } } DeviceFunction.SyncThreads(); if (isActive) { for (int i = line; i < dimX.Value && bI + i < n; i += 2) { var dist = default(Real2); for (int k = 0; k != c.Value; ++k) { var coord1 = coordinatesI[k * dimX.Value + i]; var coord2 = coordinatesJ[k]; var diff = new Real2(coord1 - coord2.x, coord1 - coord2.y); dist.x += diff.x * diff.x; dist.y += diff.y * diff.y; } var dst = mSquaredDistances.Reinterpret <Real2>(); dst[((bI + i) * pitch + bJ) / 2 + tid] = dist; } } }
public static float3 RotateX(float3 z, float t) { float3 p = z; float s = DeviceFunction.Sin(t); float c = DeviceFunction.Cos(t); p.y = c * z.y + s * z.z; p.z = c * z.z - s * z.y; return(p); }
public static float3 RotateY(float3 z, float t) { float3 p = z; float s = DeviceFunction.Sin(t); float c = DeviceFunction.Cos(t); p.x = c * z.x - s * z.z; p.z = c * z.z + s * z.x; return(p); }
public static float3 RotateZ(float3 z, float t) { float3 p = z; float s = DeviceFunction.Sin(t); float c = DeviceFunction.Cos(t); p.x = c * z.x + s * z.y; p.y = c * z.y - s * z.x; return(p); }
// Helpers! private static void ComputeRippleAtOffset(int x, int y, int width, int height, Action <byte> action) { var fx = x - width * 0.5f; var fy = y - height * 0.5f; var d = DeviceFunction.Sqrt(fx * fx + fy * fy); var v = (byte)(128f + 127f * DeviceFunction.Cos(d / 10f) / (d / 10f + 1f)); action(v); }
private void initAllDevComp() { fpc = new FunctionParameterCollection(); dev = new Device(); AllDevices = new DeviceCollection(); devevent = new DeviceEvent(); devFunc = new DeviceFunction(); devProp = new DeviceProperty(); CondColl = new ConditionsCollection(); }
public static Matrix Phansalkar(Matrix input, int radius) { var width = input.Width; var height = input.Height; var data = (float[])input; var result = (float[])new Matrix(width, height); var p = 2.5f; var q = 10; var k = 0.15; var r = 0.4; var dimension = GetKernelDimension(height); Gpu.Default.Launch(KernelBuilder(tid => { var index = tid * width; for (var x = 0; x < width; x++) { var sum = 0f; var count = 0; for (var ky = -radius; ky <= radius; ky++) { var py = tid + ky; var pindex = py * width; for (var kx = -radius; kx <= radius; kx++) { var px = x + kx; if (px >= 0 && px < width && py >= 0 && py < height) { sum += data[pindex + px] / byte.MaxValue; count++; } } } var mean = sum / count; var variance = DeviceFunction.Abs(sum / count - mean); var deviation = DeviceFunction.Sqrt(variance / count); var threshold = mean * (1f + p * DeviceFunction.Exp(-q * mean) + k * (deviation / r - 1)); result[index + x] = data[index + x] / byte.MaxValue > threshold ? byte.MaxValue : 0; } }, height), new LaunchParam(dimension, dimension)); return(new Matrix(width, height, result)); }
public static void MarchRay(deviceptr <float3> directions, deviceptr <byte> pixelValues, float3 camera, float3 light, float2 cols, float minDist, float maxDist, int maxstep, int bytes, int width, int iterations, float side, float3 seed, float3 shift, float shadowStrength, float ambientOccStrength) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int h = Index(i, j, width); float3 p = camera; int stepnum = 0; float dist = minDist + 1; float totalDist = 0; while (totalDist < maxDist && dist > minDist) { dist = ScaledDE(p, iterations, side, seed, shift); p = A(p, M(directions[h], dist)); totalDist += dist; stepnum++; if (stepnum == maxstep) { break; } } float brightness = 0; if (DeviceFunction.Abs(dist) <= minDist) { float3 off = S(light, p); float lightVectorLength = L(off); off = D(off, lightVectorLength); float shadow = 1; float diffuseCalculated = 0; float normalAngle = O(off, Normal(p, iterations, side, seed, shift, minDist)); if (normalAngle > 0) { shadow = NewSoftShadow(p, off, shadowStrength, iterations, side, seed, shift, minDist, lightVectorLength, 0.01f); diffuseCalculated = DeviceFunction.Max(cols.y * shadow * normalAngle, 0); } brightness += diffuseCalculated + cols.x / (1 + stepnum * ambientOccStrength); brightness = DeviceFunction.Min(DeviceFunction.Max(brightness, 0), 1); float col = Orbit(p, iterations, side, seed, shift); pixelValues[h * 3] = (byte)(Blue(col) * brightness * byte.MaxValue); pixelValues[h * 3 + 1] = (byte)(Green(col) * brightness * byte.MaxValue); pixelValues[h * 3 + 2] = (byte)(Red(col) * brightness * byte.MaxValue); } else { pixelValues[h * 3] = 0; pixelValues[h * 3 + 1] = 0; pixelValues[h * 3 + 2] = 0; } }
private void initAllDevComp() { fpc = new FunctionParameterCollection(); devCap = new DeviceCapabilities(); dev = new Device(); devcol = new DeviceCollection(); devEventColl = new DeviceEventCollection(); devfuncColl = new DeviceFunctionCollection(); devPropColl = new DevicePropertyCollection(); devevent = new DeviceEvent(); devFunc = new DeviceFunction(); devProp = new DeviceProperty(); }