【问题标题】:CuBlas Matrix Addition using axpy使用 axpy 的 CuBlas 矩阵加法
【发布时间】:2017-09-05 18:27:05
【问题描述】:

我正在尝试使用 Alea CuBlas axpy 进行矩阵加法,但它似乎只添加了第一行

let matrixAddition (a:float[,]) (b: float[,]) =
     use mA = gpu.AllocateDevice(a)
     use mB = gpu.AllocateDevice(b)
     blas.Axpy(a.Length,1.,mA.Ptr,1,mB.Ptr,1)
     Gpu.Copy2DToHost(mB)

【问题讨论】:

    标签: aleagpu


    【解决方案1】:

    JokingBear 的代码和 redb 的代码有一个重要的区别。

    在这行有问题的代码

    blas.Axpy(a.Length,1.,mA.Ptr,1,mB.Ptr,1)
    

    a 具有 float[,] 类型,长度将是该矩阵 a 中元素的数量。

    但是,更正后的代码使用了这个

    blas.Axpy(deviceA.Length, 1f, deviceA.Ptr, 1, deviceB.Ptr, 1);
    

    deviceA 不再是 float[,] 而是 DeviceMemory2D 对象。

    DeviceMemory2D.Length(float[,]).Length 惊人地大(我的硬件上的 3x3 矩阵为 384),因为 GPU 上的分配似乎由于某些未知原因占用了更多空间。

    JokingBear 的代码只对第一行求和的关键原因是 (float[,]).Length 对于 GPU 内存上更长的数据结构来说太短了。跟alea的版本没有关系。

    【讨论】:

      【解决方案2】:

      我拿了你的例子,它运行良好。

      代码:

              var gpu = Gpu.Default;
              var blas = Blas.Get(Gpu.Default);
      
              var hostA = new float[,]
              {
                  {1, 2, 3},
                  {4, 5, 6},
                  {7, 8, 9},
              };
      
              var hostB = new float[,]
              {
                  {10, 20, 30},
                  {40, 50, 60},
                  {70, 80, 90},
              };
      
              PrintArray(hostA);
              PrintArray(hostB);
      
              var deviceA = gpu.AllocateDevice(hostA);
              var deviceB = gpu.AllocateDevice(hostB);
      
              blas.Axpy(deviceA.Length, 1f, deviceA.Ptr, 1, deviceB.Ptr, 1);
      
              var hostC = Gpu.Copy2DToHost(deviceB);
      
              PrintArray(hostC);
      

      打印助手:

          private static void PrintArray(float[,] array)
          {
              for (var i = 0; i < array.GetLength(0); i++)
              {
                  for (var k = 0; k < array.GetLength(1); k++)
                  {
                      Console.Write("{0} ", array[i, k]);
                  }
      
                  Console.WriteLine();
              }
      
              Console.WriteLine(new string('-', 10));
          }
      

      这是我得到的:

      两个问题: - 您使用的是什么版本的 AleaGpu? - 您使用的是哪个版本的 CUDA 工具包?

      我对我的示例进行了编码:Alea 3.0.4-beta2,我有 CudaToolkit 8.0

      只是为了确保我尝试在 F# 中编写您的示例。 (我的 F# 不是很流利)

      代码:

      let gpu = Gpu.Default;
      let blas = Blas.Get(Gpu.Default);
      
      let hostA: float[,] = array2D [[  1.0;  2.0;  3.0 ]; [  4.0;  5.0;  6.0 ]; [  7.0;  8.0;  9.0 ]]
      let hostB: float[,] = array2D [[ 10.0; 20.0; 30.0 ]; [ 40.0; 50.0; 60.0 ]; [ 70.0; 80.0; 90.0 ]]
      
      PrintArray(hostA)
      PrintArray(hostB)
      
      use deviceA = gpu.AllocateDevice(hostA);
      use deviceB = gpu.AllocateDevice(hostB);
      
      blas.Axpy(deviceA.Length, 1.0, deviceA.Ptr, 1, deviceB.Ptr, 1);
      
      let hostC = Gpu.Copy2DToHost(deviceB);
      
      PrintArray(hostC)
      

      打印助手:

      let PrintArray(array: float[,]): unit =
          for i in 0 .. array.GetLength(0) - 1 do
              for k in 0 .. array.GetLength(1) - 1 do
                  Console.Write("{0} ", array.[i, k]);
              Console.WriteLine();
      
          Console.WriteLine(new string('-', 10));
      

      【讨论】:

      • 我正在使用 alea 3.0.3 和 cuda 工具包 8.0
      • 您可以尝试更新到最新版本吗?如果这不起作用,您可以发布一个带有实际“数字”数据的示例,以便我自己测试吗?
      猜你喜欢
      • 2011-07-23
      • 2011-07-29
      • 2013-01-22
      • 1970-01-01
      • 2013-09-02
      • 2020-03-09
      • 2011-11-30
      • 1970-01-01
      • 2018-02-12
      相关资源
      最近更新 更多