本文整理汇总了C#中CudaDeviceVariable.CopyToDevice方法的典型用法代码示例。如果您正苦于以下问题:C# CudaDeviceVariable.CopyToDevice方法的具体用法?C# CudaDeviceVariable.CopyToDevice怎么用?C# CudaDeviceVariable.CopyToDevice使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CudaDeviceVariable
的用法示例。
在下文中一共展示了CudaDeviceVariable.CopyToDevice方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Backward
public void Backward(CudnnPoolingDescriptor pooling, CudnnTensorDescriptor srcTensor, float[] srcData, CudnnTensorDescriptor srcDiffTensor, float[] srcDiffData,
CudnnTensorDescriptor destTensor, float[] destData, CudnnTensorDescriptor destDiffTensor, float[] destDiffData)
{
Contract.Requires(pooling != null);
Contract.Requires(srcTensor != null);
Contract.Requires(srcData != null);
Contract.Requires(destTensor != null);
Contract.Requires(destData != null);
Contract.Requires(srcDiffTensor != null);
Contract.Requires(srcDiffData != null);
Contract.Requires(destDiffTensor != null);
Contract.Requires(destDiffData != null);
ThrowIfNotInitialized();
CheckIfCompatible(CudnnType.Float, srcTensor, srcDiffTensor, destTensor, destDiffTensor);
using (var srcDataGpu = new CudaDeviceVariable<float>(srcData.Length))
using (var srcDiffDataGpu = new CudaDeviceVariable<float>(srcDiffData.Length))
using (var destDataGpu = new CudaDeviceVariable<float>(destData.Length))
using (var destDiffDataGpu = new CudaDeviceVariable<float>(destDiffData.Length))
{
srcDataGpu.CopyToDevice(srcData);
srcDiffDataGpu.CopyToDevice(srcDiffData);
destDataGpu.CopyToDevice(destData);
Invoke(() => CudnnNativeMethods.cudnnPoolingBackward(handle, pooling.Handle,
srcTensor.Handle, srcDataGpu.DevicePointer, srcDiffTensor.Handle, srcDiffDataGpu.DevicePointer,
destTensor.Handle, destDataGpu.DevicePointer, destDiffTensor.Handle, destDiffDataGpu.DevicePointer));
destDiffDataGpu.CopyToHost(destDiffData);
}
}
示例2: Test
static void Test(byte[] ptxFile)
{
const int size = 16;
var context = new CudaContext();
var kernel = context.LoadKernelPTX(ptxFile, "kernel");
var memory = context.AllocateMemory(4 * size);
var gpuMemory = new CudaDeviceVariable<int>(memory);
var cpuMemory = new int[size];
for (var i = 0; i < size; i++)
cpuMemory[i] = i - 2;
gpuMemory.CopyToDevice(cpuMemory);
kernel.BlockDimensions = 4;
kernel.GridDimensions = 4;
kernel.Run(memory);
gpuMemory.CopyToHost(cpuMemory);
for (var i = 0; i < size; i++)
Console.WriteLine("{0} = {1}", i, cpuMemory[i]);
}
示例3: BackwardBias
public void BackwardBias(CudnnTensorDescriptor srcTensor, double[] srcData, CudnnTensorDescriptor destTensor, double[] destData, CudnnAccumulateResult accumulate)
{
Contract.Requires(srcTensor != null);
Contract.Requires(srcData != null);
Contract.Requires(destTensor != null);
Contract.Requires(destData != null);
ThrowIfNotInitialized();
CheckIfCompatible(CudnnType.Double, srcTensor, destTensor);
using (var srcDataGpu = new CudaDeviceVariable<double>(srcData.Length))
using (var destDataGpu = new CudaDeviceVariable<double>(destData.Length))
{
srcDataGpu.CopyToDevice(srcData);
Invoke(() => CudnnNativeMethods.cudnnConvolutionBackwardBias(handle, srcTensor.Handle, srcDataGpu.DevicePointer, destTensor.Handle, destDataGpu.DevicePointer, accumulate));
destDataGpu.CopyToHost(destData);
}
}
示例4: Run
public void Run(DistanceOperation operation,
CudaDeviceVariable<float> A, int sizeA,
CudaDeviceVariable<float> B, int sizeB,
CudaDeviceVariable<float> result, int sizeRes)
{
if (!ValidateAtRun(operation))
return;
switch (operation)
{
case DistanceOperation.DotProd:
//ZXC m_dotKernel.Run(result.DevicePointer, 0, A.DevicePointer, B.DevicePointer, sizeA, 0);
m_dotKernel.Run(result.DevicePointer, A.DevicePointer, B.DevicePointer, sizeA);
break;
case DistanceOperation.CosDist:
//ZXC m_cosKernel.Run(result.DevicePointer, 0, A.DevicePointer, B.DevicePointer, sizeA, 0);
m_cosKernel.Run(result.DevicePointer, A.DevicePointer, B.DevicePointer, sizeA);
break;
case DistanceOperation.EuclidDist:
float res = RunReturn(operation, A, sizeA, B, sizeB);
result.CopyToDevice(res);
break;
case DistanceOperation.EuclidDistSquared:
m_combineVecsKernel.SetupExecution(sizeA);
m_combineVecsKernel.Run(A.DevicePointer, B.DevicePointer, m_temp, (int)MyJoin.MyJoinOperation.Subtraction, sizeA);
//ZXC m_dotKernel.Run(result.DevicePointer, 0, m_temp, m_temp, m_temp.Count, 0);
m_dotKernel.Run(result.DevicePointer, m_temp, m_temp);
break;
case DistanceOperation.HammingDist:
m_combineVecsKernel.SetupExecution(sizeA);
m_combineVecsKernel.Run(A.DevicePointer, B.DevicePointer, m_temp, (int)MyJoin.MyJoinOperation.Equal, sizeA);
//ZXC m_reduceSumKernel.Run(result.DevicePointer, m_temp, m_temp.Count, 0, 0, 1, /*distributed = false*/0); // reduction to a single number
m_reduceSumKernel.Run(result.DevicePointer, m_temp);
float fDist = 0; // to transform number of matches to a number of differences
result.CopyToHost(ref fDist);
fDist = m_temp.Count - fDist;
result.CopyToDevice(fDist);
break;
case DistanceOperation.HammingSim:
m_combineVecsKernel.SetupExecution(sizeA);
m_combineVecsKernel.Run(A.DevicePointer, B.DevicePointer, m_temp, (int)MyJoin.MyJoinOperation.Equal, sizeA);
//ZXC m_reduceSumKernel.Run(result.DevicePointer, m_temp, m_temp.Count, 0, 0, 1, /*distributed = false*/0); // reduction to a single number
m_reduceSumKernel.Run(result.DevicePointer, m_temp);
// take the single number (number of different bits) and convert it to Hamming Similarity:
// a number in range <0,1> that says how much the vectors are similar
float fSim = 0;
result.CopyToHost(ref fSim);
fSim = fSim / m_temp.Count;
result.CopyToDevice(fSim);
break;
}
}
示例5: Main
static void Main(string[] args)
{
int SIGNAL_SIZE = 50;
int FILTER_KERNEL_SIZE = 11;
Console.WriteLine("[simpleCUFFT] is starting...");
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "simpleCUFFT.simpleCUFFTKernel.ptx";
CudaContext ctx = new CudaContext(0);
CudaKernel ComplexPointwiseMulAndScale;
string[] liste = assembly.GetManifestResourceNames();
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
{
ComplexPointwiseMulAndScale = ctx.LoadKernelPTX(stream, "ComplexPointwiseMulAndScale");
}
// Allocate host memory for the signal
cuFloatComplex[] h_signal = new cuFloatComplex[SIGNAL_SIZE]; //we use cuFloatComplex for complex multiplaction in reference host code...
Random rand = new Random(0);
// Initialize the memory for the signal
for (int i = 0; i < SIGNAL_SIZE; ++i)
{
h_signal[i].real = (float)rand.NextDouble();
h_signal[i].imag = 0;
}
// Allocate host memory for the filter
cuFloatComplex[] h_filter_kernel = new cuFloatComplex[FILTER_KERNEL_SIZE];
// Initialize the memory for the filter
for (int i = 0; i < FILTER_KERNEL_SIZE; ++i)
{
h_filter_kernel[i].real = (float)rand.NextDouble();
h_filter_kernel[i].imag = 0;
}
// Pad signal and filter kernel
cuFloatComplex[] h_padded_signal = null;
cuFloatComplex[] h_padded_filter_kernel = null;
int new_size = PadData(h_signal, ref h_padded_signal, SIGNAL_SIZE,
h_filter_kernel, ref h_padded_filter_kernel, FILTER_KERNEL_SIZE);
int mem_size = (int)cuFloatComplex.SizeOf * new_size;
// Allocate device memory for signal
CudaDeviceVariable<cuFloatComplex> d_signal = new CudaDeviceVariable<cuFloatComplex>(new_size);
// Copy host memory to device
d_signal.CopyToDevice(h_padded_signal);
// Allocate device memory for filter kernel
CudaDeviceVariable<cuFloatComplex> d_filter_kernel = new CudaDeviceVariable<cuFloatComplex>(new_size);
// Copy host memory to device
d_filter_kernel.CopyToDevice(h_padded_filter_kernel);
// CUFFT plan simple API
CudaFFTPlan1D plan = new CudaFFTPlan1D(new_size, cufftType.C2C, 1);
// Transform signal and kernel
Console.WriteLine("Transforming signal cufftExecC2C");
plan.Exec(d_signal.DevicePointer, TransformDirection.Forward);
plan.Exec(d_filter_kernel.DevicePointer, TransformDirection.Forward);
// Multiply the coefficients together and normalize the result
Console.WriteLine("Launching ComplexPointwiseMulAndScale<<< >>>");
ComplexPointwiseMulAndScale.BlockDimensions = 256;
ComplexPointwiseMulAndScale.GridDimensions = 32;
ComplexPointwiseMulAndScale.Run(d_signal.DevicePointer, d_filter_kernel.DevicePointer, new_size, 1.0f / new_size);
// Transform signal back
Console.WriteLine("Transforming signal back cufftExecC2C");
plan.Exec(d_signal.DevicePointer, TransformDirection.Inverse);
// Copy device memory to host
cuFloatComplex[] h_convolved_signal = d_signal;
// Allocate host memory for the convolution result
cuFloatComplex[] h_convolved_signal_ref = new cuFloatComplex[SIGNAL_SIZE];
// Convolve on the host
Convolve(h_signal, SIGNAL_SIZE,
h_filter_kernel, FILTER_KERNEL_SIZE,
h_convolved_signal_ref);
// check result
bool bTestResult = sdkCompareL2fe(h_convolved_signal_ref, h_convolved_signal, 1e-5f);
//Destroy CUFFT context
plan.Dispose();
// cleanup memory
d_filter_kernel.Dispose();
d_signal.Dispose();
ctx.Dispose();
if (bTestResult)
{
Console.WriteLine("Test Passed");
//.........这里部分代码省略.........
示例6: Backward
public void Backward(CudnnSoftmaxAlgorithm algorithm, CudnnSoftmaxMode mode,
CudnnTensorDescriptor srcTensor, double[] srcData, CudnnTensorDescriptor srcDiffTensor, double[] srcDiffData,
CudnnTensorDescriptor destDiffTensor, double[] destDiffData)
{
Contract.Requires(srcTensor != null);
Contract.Requires(srcData != null);
Contract.Requires(srcDiffTensor != null);
Contract.Requires(srcDiffData != null);
Contract.Requires(destDiffTensor != null);
Contract.Requires(destDiffData != null);
ThrowIfNotInitialized();
CheckIfCompatible(CudnnType.Double, srcTensor, srcDiffTensor, destDiffTensor);
using (var srcDataGpu = new CudaDeviceVariable<double>(srcData.Length))
using (var srcDiffDataGpu = new CudaDeviceVariable<double>(srcDiffData.Length))
using (var destDiffDataGpu = new CudaDeviceVariable<double>(destDiffData.Length))
{
srcDataGpu.CopyToDevice(srcData);
srcDiffDataGpu.CopyToDevice(srcDiffData);
Invoke(() => CudnnNativeMethods.cudnnSoftmaxBackward(handle, algorithm, mode,
srcTensor.Handle, srcDataGpu.DevicePointer, srcDiffTensor.Handle, srcDiffDataGpu.DevicePointer,
destDiffTensor.Handle, destDiffDataGpu.DevicePointer));
destDiffDataGpu.CopyToHost(destDiffData);
}
}
示例7: Forward
public void Forward(CudnnTensorDescriptor srcTensor, float[] srcData, CudnnFilterDescriptor filter, float[] filterData, CudnnConvolutionDescriptor convolution, CudnnTensorDescriptor destTensor, float[] destData, CudnnAccumulateResult accumulate)
{
Contract.Requires(srcTensor != null);
Contract.Requires(srcData != null);
Contract.Requires(filter != null);
Contract.Requires(filterData != null);
Contract.Requires(convolution != null);
Contract.Requires(destTensor != null);
Contract.Requires(destData != null);
ThrowIfNotInitialized();
CheckIfCompatible(CudnnType.Float, srcTensor, destTensor, filter);
using (var srcDataGpu = new CudaDeviceVariable<float>(srcData.Length))
using (var filterDataGpu = new CudaDeviceVariable<float>(filterData.Length))
using (var destDataGpu = new CudaDeviceVariable<float>(destData.Length))
{
srcDataGpu.CopyToDevice(srcData);
filterDataGpu.CopyToDevice(filterData);
Invoke(() => CudnnNativeMethods.cudnnConvolutionForward(handle, srcTensor.Handle, srcDataGpu.DevicePointer, filter.Handle, filterDataGpu.DevicePointer, convolution.Handle, destTensor.Handle, destDataGpu.DevicePointer, accumulate));
destDataGpu.CopyToHost(destData);
}
}
示例8: BackwardFilter
public void BackwardFilter(CudnnTensorDescriptor srcTensor, double[] srcData, CudnnTensorDescriptor diffTensor, double[] diffData, CudnnConvolutionDescriptor convolution, CudnnFilterDescriptor gradient, double[] gradientData, CudnnAccumulateResult accumulate)
{
Contract.Requires(srcTensor != null);
Contract.Requires(srcData != null);
Contract.Requires(diffTensor != null);
Contract.Requires(diffData != null);
Contract.Requires(convolution != null);
Contract.Requires(gradient != null);
Contract.Requires(gradientData != null);
ThrowIfNotInitialized();
CheckIfCompatible(CudnnType.Double, srcTensor, diffTensor, gradient);
using (var srcDataGpu = new CudaDeviceVariable<double>(srcData.Length))
using (var diffDataGpu = new CudaDeviceVariable<double>(diffData.Length))
using (var gradientDataGpu = new CudaDeviceVariable<double>(gradientData.Length))
{
srcDataGpu.CopyToDevice(srcData);
diffDataGpu.CopyToDevice(diffData);
Invoke(() => CudnnNativeMethods.cudnnConvolutionBackwardFilter(handle, srcTensor.Handle, srcDataGpu.DevicePointer, diffTensor.Handle, diffDataGpu.DevicePointer, convolution.Handle, gradient.Handle, gradientDataGpu.DevicePointer, accumulate));
gradientDataGpu.CopyToHost(gradientData);
}
}