c - 使用矩阵算法和常量进行嵌套 for 循环调试。

标签 c matrix sse nested-loops matrix-multiplication

这组嵌套 for 循环对于 M=64 和 N=64 的值可以正常工作,但当我使 M=128 和 N=64 时不起作用。我有另一个程序来检查矩阵乘法的正确值。直觉上它似乎仍然有效,但给了我错误的答案。

for(int m=64;m<=M;m+=64){
for(int n=64;n<=N;n+=64){
    for(int i = m-64; i < m; i+=16){

        float *A_column_start, *C_column_start;
        __m128 c_1, c_2, c_3, c_4, a_1, a_2, a_3, a_4, mul_1, 
               mul_2, mul_3, mul_4, b_1;
        int j, k;

        for(j = m-64; j < m; j++){

            //Load 16 contiguous column aligned elements from matrix C in
            //c_1-c_4 registers

            C_column_start = C+i+j*M;

            c_1 = _mm_loadu_ps(C_column_start);
            c_2 = _mm_loadu_ps(C_column_start+4);
            c_3 = _mm_loadu_ps(C_column_start+8);
            c_4 = _mm_loadu_ps(C_column_start+12);

            for (k=n-64; k < n; k+=2){

                //Load 16 contiguous column aligned elements from matrix A to
                //the a_1-a_4 registers

                A_column_start = A+k*M;

                a_1 = _mm_loadu_ps(A_column_start+i);
                a_2 = _mm_loadu_ps(A_column_start+i+4);
                a_3 = _mm_loadu_ps(A_column_start+i+8);
                a_4 = _mm_loadu_ps(A_column_start+i+12);

                //Load a value to resgister b_1 to act as a "B" or ("A^T") 
                //element to multiply against the A matrix

                b_1 = _mm_load1_ps(A_column_start+j);

                mul_1 = _mm_mul_ps(a_1, b_1);
                mul_2 = _mm_mul_ps(a_2, b_1);
                mul_3 = _mm_mul_ps(a_3, b_1);
                mul_4 = _mm_mul_ps(a_4, b_1);

                //Add together all values of the multiplied A and "B"
                //(or "A^T") matrix elements

                c_4 = _mm_add_ps(c_4, mul_4);
                c_3 = _mm_add_ps(c_3, mul_3);
                c_2 = _mm_add_ps(c_2, mul_2);
                c_1 = _mm_add_ps(c_1, mul_1);

                //Move over one column in A, and load the next 16 contiguous 
                //column aligned elements from matrix A to the a_1-a_4 registers

                A_column_start+=M;

                a_1 = _mm_loadu_ps(A_column_start+i);
                a_2 = _mm_loadu_ps(A_column_start+i+4);
                a_3 = _mm_loadu_ps(A_column_start+i+8);
                a_4 = _mm_loadu_ps(A_column_start+i+12);

                //Load a value to resgister b_1 to act as a "B" or "A^T"
                //element to multiply against the A matrix

                b_1 = _mm_load1_ps(A_column_start+j);

                mul_1 = _mm_mul_ps(a_1, b_1);
                mul_2 = _mm_mul_ps(a_2, b_1);
                mul_3 = _mm_mul_ps(a_3, b_1);
                mul_4 = _mm_mul_ps(a_4, b_1);

                //Add together all values of the multiplied A and "B" or
                //("A^T") matrix elements

                c_4 = _mm_add_ps(c_4, mul_4);
                c_3 = _mm_add_ps(c_3, mul_3);
                c_2 = _mm_add_ps(c_2, mul_2);
                c_1 = _mm_add_ps(c_1, mul_1);

            }
            //Store the added up C values back to memory

            _mm_storeu_ps(C_column_start, c_1);
            _mm_storeu_ps(C_column_start+4, c_2);
            _mm_storeu_ps(C_column_start+8, c_3);
            _mm_storeu_ps(C_column_start+12, c_4);

        }

    }
    }
}}

最佳答案

我猜你在代码中使用了M

C_column_start = C+i+j*M;

需要使用m来代替。也可能在其他使用 M 的行中。 但是,我不太理解您的代码,因为您没有解释代码的用途,而且我不是数学程序员。

关于c - 使用矩阵算法和常量进行嵌套 for 循环调试。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8077037/

相关文章:

c - 在我不应该的情况下在没有警告的情况下获得分配?

C fread() 矩阵,无论矩阵大小如何,都读入九个错误值

numpy - 为什么矩阵乘法会根据它们的分组方式给出不同的结果?

上交所/AVX : Choose from two __m256 float vectors based on per-element min and max absolute value

c++ - 有没有比添加 0.5f 和截断转换更直接的方法来将 float 转换为 int 并进行舍入?

assembly - 将 SSE 与 AVX128 混合使用以获得更短的指令?

c - 如果输入换行符,如何继续扫描?

c - 错误: conflicting types for ‘fmin’

python - 如何在Python中编写numpy矩阵的函数

c - 前/后增量指针