【问题标题】:Efficient Matrix decomposition into square submatrices in C++在 C++ 中将矩阵高效分解为方子矩阵
【发布时间】:2011-06-29 01:36:50
【问题描述】:

我通过使用一维数据类型并将其包装成行和列,在 C++ 中实现了一个矩阵数据类型。现在,我希望有可能从此时开始创建方形/块状子矩阵,并且我想在内存中进行。

问题是我希望这些子矩阵中的一些可以转移到 GPU 内存中,并且可以在那里并行处理它们。例如,这对矩阵乘法很有用。由于这些子矩阵在主内存中没有对齐,如果不创建单独的副本,将它们作为一个单元复制到设备内存看起来是不可能的?我希望将此直接 GPU 子矩阵复制映射到 CPU 原始矩阵以进行更新和提高效率。我事先不知道确切的分区。

有人知道我怎样才能实现它吗?

提醒一下,矩阵需要按块划分,而不是按行划分,这在 C/C++ 中相对容易。

【问题讨论】:

    标签: c++ c stl gpgpu gpu


    【解决方案1】:

    如果在创建“主”矩阵时所需的子矩阵是已知的,并且如果它们形成主矩阵的分区,则可以创建一个类似于这样的复合矩阵类:

    // supposing an IMatrix<T> interface (pure virtual members only) class
    
    template< typename T >
    struct CompositeMatrix : public IMatrix<T> {
       typedef std::vector<PlainMatrix<T>*> tMatrices;
    
       tMatrices submatrices;
       T& element( size_t row, size_t column ) {
           return findsubmatrix( row, column )->element( row, column );
       }
    
       // find algorithm implementing 'chain of responsibility-like' pattern.
       PlainMatrix<T>* findsubmatrix( size_t row, size_t col ) {
         for( tMatrices::iterator it = submatrices.begin()
            ; it != submatrices.end()
            ; ++it)
         {
            if( it->contains( row,col ) ) return *it;            
         }
         return NULL;
       }
    };
    

    “PlainMatix”可以以节省内存的方式组织。

    【讨论】:

      【解决方案2】:

      如果您的矩阵维度是 2 的幂,您可以将它们存储在主机内存中的 z-order 中。这样,您只需要子矩阵的开始和结束索引即可通过一次调用 cudaMemcpy 来复制它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-16
        • 2023-03-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多