Aspie96

CPU vs GPU

Feb 27th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.18 KB | None | 0 0
  1. internal static void ApplyConnectionMatrix(double[] input, int inputSkip, double[] output, int outputSkip, double[,] weights)
  2. {
  3.     int inputLength = weights.GetLength(0);
  4.     int outputLength = weights.GetLength(1);
  5.     for (int i = 0; i < outputLength; i++)
  6.     {
  7.         double outputValue = 0.0;
  8.         {
  9.             for (int j = 0; j < inputLength; j++)
  10.             {
  11.                 outputValue += weights[j, i] * input[inputSkip + j];
  12.             }
  13.         }
  14.         output[outputSkip + j] = outputValue;
  15.     }
  16. }
  17.  
  18. internal static void ApplyConnectionMatrix1(double[] input, int inputSkip, double[] output, int outputSkip, double[,] weights)
  19. {
  20.     int inputLength = weights.GetLength(0);
  21.     int outputLength = weights.GetLength(1);
  22.     Gpu.Default.For(0, outputLength, delegate (int i)
  23.     {
  24.         double outputValue = 0.0;
  25.         for (int j = 0; j < inputLength; j++)
  26.         {
  27.             outputValue += input[inputSkip + j] * weights[j, i];
  28.         }
  29.         output[outputSkip + i] = outputValue;
  30.     });
  31. }
  32.  
  33. internal static void ApplyConvolution(float[,,] input, float[,,] output, float[][,,] kernels, int kernelSide, int stride, int padding, Func<float, float> function)
  34. {
  35.     int outputDepth = output.GetLength(0);
  36.     int outputWidth = output.GetLength(1);
  37.     int outputHeight = output.GetLength(2);
  38.     int inputDepth = input.GetLength(0);
  39.     int inputWidth = input.GetLength(1);
  40.     int inputHeight = input.GetLength(2);
  41.     for (int i = 0; i < outputDepth; i++)
  42.     {
  43.         for (int j = 0; j < outputWidth; j++)
  44.         {
  45.             for (int k = 0; k < outputHeight; k++)
  46.             {
  47.                 float outputValue = 0.0F;
  48.                 for (int l = 0; l < kernelSide; l++)
  49.                 {
  50.                     for (int m = 0; m < kernelSide; m++)
  51.                     {
  52.                         int inputX = (j + l) * stride - padding;
  53.                         int inputY = (k + m) * stride - padding;
  54.                         if (0 <= inputX && inputX < inputWidth && 0 <= inputY && inputY < inputHeight)
  55.                         {
  56.                             for (int n = 0; n < inputDepth; n++)
  57.                             {
  58.                                 outputValue += kernels[i][n, l, m] * input[n, inputX, inputY];
  59.                             }
  60.                         }
  61.                     }
  62.                 }
  63.                 output[i, j, k] = function(outputValue);
  64.             }
  65.         }
  66.     }
  67. }
  68.  
  69. internal static void ApplyConvolution1(float[,,] input, float[,,] output, float[][,,] kernels, int kernelSide, int stride, int padding, Func<float, float> function)
  70. {
  71.     int outputDepth = output.GetLength(0);
  72.     int outputWidth = output.GetLength(1);
  73.     int outputHeight = output.GetLength(2);
  74.     int inputDepth = input.GetLength(0);
  75.     int inputWidth = input.GetLength(1);
  76.     int inputHeight = input.GetLength(2);
  77.     Gpu.Default.Launch(delegate
  78.     {
  79.         int w = blockIdx.x * 8 + threadIdx.x;
  80.         int x = blockIdx.y * 8 + threadIdx.y;
  81.         int y = blockIdx.z * 8 + threadIdx.z;
  82.         float[] kernel = kernels[w];
  83.         {
  84.             float outputValue = 0.0F;
  85.             for (int i = 0; i < kernelSide; i++)
  86.             {
  87.                 for (int j = 0; j < kernelSide; j++)
  88.                 {
  89.                     int inputX = (x + i) * stride - padding;
  90.                     int inputY = (y + j) * stride - padding;
  91.                     if (0 <= inputX && inputX < inputWidth && 0 <= inputY && inputY < inputHeight)
  92.                     {
  93.                         for (int l = 0; l < inputDepth; l++)
  94.                         {
  95.                             outputValue += kernel[l, i, j] * input[l, inputX, inputY];
  96.                         }
  97.                     }
  98.                 }
  99.             }
  100.             output[w, x, y] = function(outputValue);
  101.         }
  102.     }, new LaunchParam(new dim3((outputDepth + 7) / 8, (outputWidth + 7) / 8, (outputHeight + 7) / 8), new dim3(8, 8, 8)));
  103. }
Add Comment
Please, Sign In to add comment