本文整理汇总了C#中MyMemoryBlock.GetDevicePtr方法的典型用法代码示例。如果您正苦于以下问题:C# MyMemoryBlock.GetDevicePtr方法的具体用法?C# MyMemoryBlock.GetDevicePtr怎么用?C# MyMemoryBlock.GetDevicePtr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MyMemoryBlock
的用法示例。
在下文中一共展示了MyMemoryBlock.GetDevicePtr方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Bind
public virtual void Bind(MyMemoryBlock<float> firstInput, MyMemoryBlock<float> secondInput, MyMemoryBlock<float> output)
{
int nrInputs = secondInput.Count / m_inputSize;
var vecs = nrInputs > 1
// Concatenate pointers to the individual vectors
? Enumerable.Range(0, nrInputs).Select(i => secondInput.GetDevicePtr(m_owner) + i * m_inputSize * sizeof(float))
// Use only a singe pointer
: Enumerable.Repeat(secondInput.GetDevicePtr(m_owner), 1);
Bind(firstInput.GetDevicePtr(m_owner), vecs, output.GetDevicePtr(m_owner));
}
示例2: Bind
public virtual void Bind(MyMemoryBlock<float> inputs, MyMemoryBlock<float> output)
{
int nrInputs = inputs.Count / m_inputSize;
CUdeviceptr start = inputs.GetDevicePtr(m_owner);
CUdeviceptr[] arr = GetTempArray(nrInputs); //-1 to skip the first +1 to include output
for (int i = 0; i < nrInputs - 1; ++i)
{
arr[i] = start + (i + 1) * m_inputSize * sizeof(float);
}
arr[nrInputs - 1] = output.GetDevicePtr(m_owner);
Bind(start, arr);
}
示例3: UnbindMultiple
public virtual void UnbindMultiple(MyMemoryBlock<float> firstInput, MyMemoryBlock<float> otherInputs, MyMemoryBlock<float> output)
{
int nrInputs = otherInputs.Count / m_inputSize;
CUdeviceptr firstPtr = firstInput.GetDevicePtr(m_owner);
CUdeviceptr start = otherInputs.GetDevicePtr(m_owner);
CUdeviceptr[] arr = GetTempArray(nrInputs + 1);//+1 for output
for (int i = 0; i <= nrInputs; ++i)
{
arr[i] = start + i * m_inputSize * sizeof(float);
}
arr[nrInputs] = output.GetDevicePtr(m_owner);
Unbind(firstPtr, arr);
}
示例4: OrthonormalizeVectors
/// <summary>
/// Transforms all the vectors stored in <paramref name="vectors"/> to be pair-wise orthonormal using a modified version of the Gram-Schmidt algorithm.
/// </summary>
/// <param name="vectors">The vectors to orthonormalize.</param>
/// <param name="temp">A vector of temporal space.</param>
/// <param name="xDim">The length of each vector.</param>
/// <param name="yDim">The number of vectors.</param>
/// <param name="dotKernel">The kernel to compute a dot product.</param>
/// <param name="multKernel">The kernel to compute vector combinations.</param>
public static void OrthonormalizeVectors(MyMemoryBlock<float> vectors, MyMemoryBlock<float> temp, int xDim, int yDim, MyProductKernel<float> dotKernel, MyCudaKernel multKernel, int GPU)
{
int count = xDim * yDim;
Debug.Assert(vectors != null && temp != null, "Missing data!");
Debug.Assert(dotKernel != null && multKernel != null, "Missing a kernel!");
Debug.Assert(xDim > 0 && yDim > 0, "Negative matrix dimensions!");
Debug.Assert(vectors.Count >= count, "Too little vectors to orthonormalize!");
Debug.Assert(temp.Count >= xDim, "Too little temp space!");
multKernel.SetupExecution(xDim);
for (int i = 0; i < count; i += xDim)
{
var curr = vectors.GetDevicePtr(GPU, i);
// Normalize the current vector
{
//ZXC dotKernel.Run(temp, 0, curr, curr, xDim, /* distributed: */ 0);
dotKernel.Run(temp, curr, curr, xDim);
temp.SafeCopyToDevice(0, 1);
if (temp.Host[0] < 0.0000001f)
continue;
temp.Host[0] = (float)(1 / Math.Sqrt(temp.Host[0]));
temp.SafeCopyToDevice(0, 1);
multKernel.Run(curr, temp, curr, (int)MyJoin.MyJoinOperation.Multiplication, xDim, 1);
}
// Make all the remaining vectors orthogonal to the current one
for (int j = i + xDim; j < count; j += xDim)
{
var next = vectors.GetDevicePtr(GPU, j);
// Compute and subtract the projection onto the current vector
//ZXC dotKernel.Run(temp, xDim, curr, next, xDim, /* distributed: */ 0);
dotKernel.outOffset = xDim;
dotKernel.Run(temp, curr, next, xDim);
multKernel.Run(curr, temp, temp, (int)MyJoin.MyJoinOperation.Multiplication, xDim, 1);
multKernel.Run(next, temp, next, (int)MyJoin.MyJoinOperation.Subtraction, xDim, xDim);
}
}
}
示例5: NormalizeLeadingDim
/// <summary>
/// Normalizes vectors along the leading dimension.
/// </summary>
public static void NormalizeLeadingDim(
MyMemoryBlock<float> vectors, MyMemoryBlock<float> temp,
int leadingDim, int otherDim,
MyProductKernel<float> dotKernel, MyCudaKernel multKernel, int GPU)
{
var count = leadingDim * otherDim;
Debug.Assert(vectors != null && temp != null, "Missing data!");
Debug.Assert(dotKernel != null && multKernel != null, "Missing kernels.");
Debug.Assert(leadingDim > 0 && otherDim > 0, "Negative matrix dimensions!");
Debug.Assert(vectors.Count >= count, "Too little vectors to orthonormalize!");
Debug.Assert(temp.Count >= Math.Max(leadingDim, otherDim), "Too little temp space!");
multKernel.SetupExecution(leadingDim);
for (int i = 0; i < otherDim; i++)
{
var seg = vectors.GetDevicePtr(GPU, i * leadingDim);
//dotKernel.Run(temp, i, seg, seg, leadingDim, /* distributed: */ 0);
dotKernel.outOffset = i;
dotKernel.Run(temp, seg, seg, leadingDim);
}
temp.SafeCopyToHost(0, otherDim);
for (int i = 0; i < otherDim; i++)
{
if (temp.Host[i] < 0.0000001f)
temp.Host[i] = 0;
else
temp.Host[i] = (float)(1 / Math.Sqrt(temp.Host[i]));
}
temp.SafeCopyToDevice(0, otherDim);
for (int i = 0; i < otherDim; i++)
{
var seg = vectors.GetDevicePtr(GPU, i * leadingDim);
var len = temp.GetDevicePtr(GPU, i);
multKernel.Run(seg, len, seg, (int)MyJoin.MyJoinOperation.Multiplication, leadingDim, 1);
}
}
示例6: GenerateTransformMatrix
//.........这里部分代码省略.........
GenerateRandomNormalVectors(unmanagedVectors.Host, random, largerDim, smallerDim, normalize: false);
unmanagedVectors.SafeCopyToDevice();
// Orthonormalize along the larger dimension
OrthonormalizeVectors(unmanagedVectors, temp, largerDim, smallerDim, dotKernel, multKernel, GPU);
if (xDim > yDim)
{
// xDim is leading and is normalized
// We need to transpose to get the correct dims
transposeKernel.Run(unmanagedVectors, unmanagedVectors, xDim, yDim);
if (axisToNormalize == AxisToNormalizeEnum.yDim)
NormalizeLeadingDim(unmanagedVectors, temp, yDim, xDim, dotKernel, multKernel, GPU);
}
else
{
// yDim is leading and is normalized
// The matrix is in correct position
if (axisToNormalize == AxisToNormalizeEnum.xDim)
{
// TODO: generate the matrix with transposed dims?
// TODO: SMELLY VERSION:
transposeKernel.Run(unmanagedVectors, unmanagedVectors, yDim, xDim);
NormalizeLeadingDim(unmanagedVectors, temp, xDim, yDim, dotKernel, multKernel, GPU);
transposeKernel.Run(unmanagedVectors, unmanagedVectors, xDim, yDim);
}
}
break;
case VectorGenerationMode.AverageBaseVectors:
int longerDim = Math.Max(xDim, yDim);
int shorterDim = Math.Min(xDim, yDim);
GenerateTransformMatrix(
unmanagedBaseVectors, null, temp,
random, longerDim, longerDim,
dotKernel, multKernel, transposeKernel, GPU,
VectorGenerationMode.Orthonormalize);
if (shorterDim == longerDim)
break;
float it = 0f;
float step = longerDim / (float)shorterDim;
int beg, end = 0;
for (int i = 0; i < shorterDim; i++)
{
beg = end;
it += step;
end = (int)it;
var vect = unmanagedVectors.GetDevicePtr(GPU, i * longerDim);
for (int j = beg; j < end; j++)
{
var baseVect = unmanagedBaseVectors.GetDevicePtr(GPU, j * longerDim);
multKernel.Run(baseVect, vect, vect, (int)MyJoin.MyJoinOperation.Addition, longerDim,
longerDim);
}
}
if (xDim > yDim)
{
// xDim is leading and is not normalized
// We need to transpose to get the correct dims
if (axisToNormalize == AxisToNormalizeEnum.xDim)
{
NormalizeLeadingDim(unmanagedVectors, temp, xDim, yDim, dotKernel, multKernel, GPU);
transposeKernel.Run(unmanagedVectors, unmanagedVectors, xDim, yDim);
}
else
{
transposeKernel.Run(unmanagedVectors, unmanagedVectors, xDim, yDim);
NormalizeLeadingDim(unmanagedVectors, temp, yDim, xDim, dotKernel, multKernel, GPU);
}
}
else
{
// yDim is leading and is not normalized
// The matrix is in correct position
if (axisToNormalize == AxisToNormalizeEnum.yDim)
NormalizeLeadingDim(unmanagedVectors, temp, yDim, xDim, dotKernel, multKernel, GPU);
else
{
// TODO: SMELLY VERSION:
transposeKernel.Run(unmanagedVectors, unmanagedVectors, yDim, xDim);
NormalizeLeadingDim(unmanagedVectors, temp, xDim, yDim, dotKernel, multKernel, GPU);
transposeKernel.Run(unmanagedVectors, unmanagedVectors, xDim, yDim);
}
}
break;
}
}
示例7: Unbind
public virtual void Unbind(MyMemoryBlock<float> firstInput, MyMemoryBlock<float> secondInput, MyMemoryBlock<float> output)
{
Unbind(firstInput.GetDevicePtr(m_owner), secondInput.GetDevicePtr(m_owner), output.GetDevicePtr(m_owner));
}