Possible Solution
Content |
Make sure to verify that results (except for the memory layout) do not depend on the storage order.
Simple initialization and print functions for matrices
#include <stdlib.h> #include <stdio.h> #include <stddef.h> // function initGeMatrix void initGeMatrix(size_t m, size_t n, double *A, ptrdiff_t incRowA, ptrdiff_t incColA) { for (size_t i=0; i<m; ++i) { for (size_t j=0; j<n; ++j) { A[i*incRowA + j*incColA] = i*n + j +1; } } } // function printGeMatrix void printGeMatrix(size_t m, size_t n, const double *A, ptrdiff_t incRowA, ptrdiff_t incColA) { for (size_t i=0; i<m; ++i) { for (size_t j=0; j<n; ++j) { printf("%9.2lf ", A[i*incRowA + j*incColA]); } printf("\n"); } printf("\n"); } #ifndef COLMAJOR #define COLMAJOR 1 #endif int main() { printf("COLMAJOR = %d\n", COLMAJOR); size_t m = 5, n = 10; ptrdiff_t incRowA = COLMAJOR ? 1 : n; ptrdiff_t incColA = COLMAJOR ? m : 1; // allocate memory for A double *A = malloc(m*n*sizeof(double)); if (!A) { abort(); } // initialize matrix A initGeMatrix(m, n, A, incRowA, incColA); // print matrix A printf("A =\n"); printGeMatrix(m, n, A, incRowA, incColA); // print how elements are stored in memory printf("memory layout of A:\n"); printGeMatrix(1, m*n, A, 0, 1); // print a matrix view of A printf("A(0:2, 0:3) =\n"); printGeMatrix(3, 4, A, incRowA, incColA); // print a matrix view of A printf("A(2:4, 3:8) =\n"); printGeMatrix(3, 5, &A[2*incRowA+3*incColA], incRowA, incColA); // print a matrix view of A printf("A(2:4, 3:8)^T =\n"); printGeMatrix(5, 3, &A[2*incRowA+3*incColA], incColA, incRowA); // release memory free(A); }
Test for row major
thales$ gcc -Wall -std=c11 -DCOLMAJOR=0 -o matvec_01 matvec_01.c thales$ ./matvec_01 COLMAJOR = 0 A = 1.00 2.00 3.00 4.00 5.00 6.00 7.00 8.00 9.00 10.00 11.00 12.00 13.00 14.00 15.00 16.00 17.00 18.00 19.00 20.00 21.00 22.00 23.00 24.00 25.00 26.00 27.00 28.00 29.00 30.00 31.00 32.00 33.00 34.00 35.00 36.00 37.00 38.00 39.00 40.00 41.00 42.00 43.00 44.00 45.00 46.00 47.00 48.00 49.00 50.00 memory layout of A: 1.00 2.00 3.00 4.00 5.00 6.00 7.00 8.00 9.00 10.00 11.00 12.00 13.00 14.00 15.00 16.00 17.00 18.00 19.00 20.00 21.00 22.00 23.00 24.00 25.00 26.00 27.00 28.00 29.00 30.00 31.00 32.00 33.00 34.00 35.00 36.00 37.00 38.00 39.00 40.00 41.00 42.00 43.00 44.00 45.00 46.00 47.00 48.00 49.00 50.00 A(0:2, 0:3) = 1.00 2.00 3.00 4.00 11.00 12.00 13.00 14.00 21.00 22.00 23.00 24.00 A(2:4, 3:8) = 24.00 25.00 26.00 27.00 28.00 34.00 35.00 36.00 37.00 38.00 44.00 45.00 46.00 47.00 48.00 A(2:4, 3:8)^T = 24.00 34.00 44.00 25.00 35.00 45.00 26.00 36.00 46.00 27.00 37.00 47.00 28.00 38.00 48.00 thales$
Test for col major
thales$ gcc -Wall -std=c11 -DCOLMAJOR=1 -o matvec_01 matvec_01.c thales$ ./matvec_01 COLMAJOR = 1 A = 1.00 2.00 3.00 4.00 5.00 6.00 7.00 8.00 9.00 10.00 11.00 12.00 13.00 14.00 15.00 16.00 17.00 18.00 19.00 20.00 21.00 22.00 23.00 24.00 25.00 26.00 27.00 28.00 29.00 30.00 31.00 32.00 33.00 34.00 35.00 36.00 37.00 38.00 39.00 40.00 41.00 42.00 43.00 44.00 45.00 46.00 47.00 48.00 49.00 50.00 memory layout of A: 1.00 11.00 21.00 31.00 41.00 2.00 12.00 22.00 32.00 42.00 3.00 13.00 23.00 33.00 43.00 4.00 14.00 24.00 34.00 44.00 5.00 15.00 25.00 35.00 45.00 6.00 16.00 26.00 36.00 46.00 7.00 17.00 27.00 37.00 47.00 8.00 18.00 28.00 38.00 48.00 9.00 19.00 29.00 39.00 49.00 10.00 20.00 30.00 40.00 50.00 A(0:2, 0:3) = 1.00 2.00 3.00 4.00 11.00 12.00 13.00 14.00 21.00 22.00 23.00 24.00 A(2:4, 3:8) = 24.00 25.00 26.00 27.00 28.00 34.00 35.00 36.00 37.00 38.00 44.00 45.00 46.00 47.00 48.00 A(2:4, 3:8)^T = 24.00 34.00 44.00 25.00 35.00 45.00 26.00 36.00 46.00 27.00 37.00 47.00 28.00 38.00 48.00 thales$