Note that there are some explanatory texts on larger screens.

plurals
  1. POBlock Matrix Transpose
    primarykey
    data
    text
    <p>I wanted to implement transposition of a matrix by dividing the input matrix into blocks and then transposing them. I referred to the corresponding post <a href="https://stackoverflow.com/questions/5200338/a-cache-efficient-matrix-transpose-program">A Cache Efficient Matrix Transpose Program?</a> and wrote my code like this: </p> <pre><code>#include&lt;iostream&gt; #include&lt;stdlib.h&gt; #define m 4 #include&lt;sys/time.h&gt; #include&lt;time.h&gt; #include&lt;malloc.h&gt; using namespace std; int **a, **b, **c; int count = 0; clock_t t1, t2; int blocksize = 2; int main(){ a = (int **)malloc(m*sizeof(int *)); for(int i = 0;i&lt;m;i++){ a[i] = (int *)malloc(m*sizeof(int)); } b = (int **)malloc(m*sizeof(int *)); for(int i = 0;i&lt;m;i++){ b[i] = (int *)malloc(m*sizeof(int)); } for(int i=0; i&lt;m; i++){ for(int j =0; j&lt;m; j++){ a[i][j]=(2*i)+(3*j); } } for(int i=0; i&lt;m; i++){ for(int j =0; j&lt;m; j++){ cout &lt;&lt; a[i][j] &lt;&lt; "\t"; } cout &lt;&lt; "\n"; } cout &lt;&lt; "\n"; t1 = clock(); // MAIN BLOCK TRANSPOSE CODE for (int i = 0; i &lt; m; i += blocksize) { for (int j = 0; j &lt; m; j += blocksize) { for (int k = i; k &lt; i + blocksize; ++k) { for (int l = j; l &lt; j + blocksize; ++l) { b[k + l*m] = a[l + k*m]; } } } } t2 = clock(); for(int i=0; i&lt;m; i++){ for(int j =0; j&lt;m; j++){ cout &lt;&lt; b[i][j] &lt;&lt; "\t"; } cout &lt;&lt; "\n"; } free(a); free(b); cout &lt;&lt; "\n"; cout &lt;&lt; (double)(t2-t1)/CLOCKS_PER_SEC &lt;&lt; "\n"; return 0; } </code></pre> <p>However, the code is not working as expected. I implemented the code that is said to be working in the corresponding post. Please help if possible. </p> <p>Input Array: </p> <pre><code>0 3 6 9 2 5 8 11 4 7 10 13 6 9 12 15 </code></pre> <p>Expected Output Array:</p> <pre><code>0 2 4 6 3 5 7 9 6 8 10 12 9 11 13 15 </code></pre> <p>Obtained Result: </p> <pre><code>0 3 6 9 Segmentation fault </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload