关于矩阵乘法的问题 [英] Question about matrix multiplications

查看:103
本文介绍了关于矩阵乘法的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

我的矩阵乘法一直有问题.大多数情况下都可以使用,但是有时所得到的矩阵会包含NaN:s.下面是一个简短的测试乘法,通常可以正常工作,但有时会失败.也许我破坏了某个地方的记忆 该程序中的其他内容,但我已经在打开和关闭bug的搜索中进行了数周的探索.如果有人能看到我进行乘法的问题会很高兴.

I have been having problem with my matrix multiplications. Works most of the times, but once in a while the resulting matrix contains NaN:s. Below is a short test multiplication that usually works, but sometimes fails. Maybe I corrupt the memory somewhere else in the program, but I've been search for bugs on and off for weeks. Would be happy if some one can see any issue with the way I do the multiplications.

BLAS可能很好,但是如果有一些文档,它会很好.

The BLAS may be good, but it would nice with some documentation.

void TestCalc()
{
    int aHeight = 555;
     int aWidth = 101;
     int bHeight = 101;
     int bWidth = 8;
     int resHeight = 555;
     int resWidth = 8;

     float * aC =新的float [aHeight * aWidth];
     float * bC =新的float [bHeight * bWidth];
     float * resC =新的float [resHeight * resWidth];

     for(int i = 0; i< aHeight * aWidth; i ++){   aC [i] = 0.0f; }
     for(int i = 0; i< bHeight * bWidth; i ++){     bC [i] = 0.0f; }

     array_view< const float,2> a(aHeight,aWidth,aC);
     array_view< const float,2> b(bHeight,bWidth,bC);
     array_view< float,2> res(resHeight,resWidth,resC);
     res.discard_data();

     accelerator_view target = accelerator().default_view;
     ampblas :: transpose notrans = ampblas :: transpose :: no_trans;
   & ampblas :: gemm(target,notrans,notrans,1.0f,b,a,0.0f,res); //由于某种原因,矩阵的顺序相反.
     res.synchronize();

     for(int i = 0; i< resWidth * resHeight; i ++)
     {
         if(resC [i]!= 0)//检查NaN.
         {
             std :: cout<< " ***失败! *** \ n;
             break;
        }
    }
}

void TestCalc()
{
    int aHeight = 555;
    int aWidth = 101;
    int bHeight = 101;
    int bWidth = 8;
    int resHeight = 555;
    int resWidth = 8;

    float* aC = new float[aHeight * aWidth];
    float* bC = new float[bHeight * bWidth];
    float* resC = new float[resHeight * resWidth];

    for(int i = 0; i < aHeight * aWidth; i++) {    aC[i] = 0.0f; }
    for(int i = 0; i < bHeight * bWidth; i++) {    bC[i] = 0.0f; }

    array_view<const float, 2> a(aHeight, aWidth, aC);
    array_view<const float, 2> b(bHeight, bWidth, bC);
    array_view<float, 2> res(resHeight, resWidth, resC);
    res.discard_data();

    accelerator_view target = accelerator().default_view;
    ampblas::transpose notrans = ampblas::transpose::no_trans;
    ampblas::gemm(target, notrans, notrans, 1.0f, b, a, 0.0f, res); // For some reason the matrixes have be in reverse order.
    res.synchronize();

    for(int i = 0; i < resWidth * resHeight; i++)
    {
        if (resC[i] != 0) // Check for NaN.
        {
            std::cout << "*** Fail! ***\n";
            break;
        }
    }
}

亲切的问候

MagnusJäverberg

Magnus Jäverberg

推荐答案

Javerberg,

Hi Javerberg,

我尝试长时间循环运行您的代码(进行了修改以释放分配给堆的浮点指针),但没有遇到问题.您是否知道遇到此问题的频率如何?

I tried to run your code (with modification to free the heap allocated float pointers) repeatedly in a loop for long time but did not hit the issue. Do you have an idea how frequently you hit this issue? 

您提到了矩阵乘法的实现,希望有人对此问题进行审查.但是我不确定您是否要参考问题中发布的代码.

You mentioned about your implementation of matrix multiplication which you wanted someone to review for issues. But I am not sure if you are referring to the code that you posted in your question or otherwise.

谢谢


这篇关于关于矩阵乘法的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆