Solved

dividing matrix into small blocks

Posted on 2003-12-09
4
329 Views
Last Modified: 2010-04-15

hi i want to divide a matrix of 512 x 512 dimension into small matrices of 8 x 8  blocks . how can i do it???
thanks
0
Comment
Question by:sanko50
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
skypalae earned 30 total points
Comment Utility
are you kidding that you can't do it yourself? ok, ok .. here's some code. since you haven't specified any details i don't care about indexes. if they don't fit you, change the code. but you get the idea.



int i, j, k, l ; // just indexes
int old_matrix [512][512] ;
int new_matrix [64][64][8][8] ; // there will be 64x64 array of 8x8 matrices

// one possibility .... do it the hard way
for (i=0; i<64; i++)
    for (j=0; j<64; j++)
        for (k=0; k<8; k++)
            for (l=0; l<8; l++)
                new_matrix [i][j][k][l] = old_matrix [(i*8)+k][(j*8)+l] ; // you can replace the (i*8) by (i<<3) and same with j. should be little faster

// second possibility .. bit faster i think. but the indexes could be damaged. you have to check yourself
memcpy (new_matrix, old_matrix, 512*512*sizeof(int)) ;

S.
0
 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
> you can replace the (i*8) by (i<<3) and same with j. should be little faster

Most compilers will generate the same code in both cases. I'd stick with the multiplication, because it is easier to read.
0
 
LVL 4

Expert Comment

by:skypalae
Comment Utility
Yes, rstaveley, youre right. I use VC++ and i thought that compiling it in 'debug' mode without any optimizations would make different code, but it surprisingly created exactly same thing.

Anyway, have you checked the difference between (i*9) and ((i<<3)+i) ?? The first thing compiles to 'imul ecx,ecx,9' which i would expect, but the second one compiles into:

mov         edx,dword ptr [ebp-4]
mov         eax,dword ptr [ebp-4]
lea         ecx,[eax+edx*8]

which is completely different from what i've expected (well, not completely, but almost completely). I've done some research on different mlutipliers, checked the assembler code. It really looked interesting.

And what's the point? That when i tried to compile it in 'release' mode using all optimizations the resulting assembler code was exactly the same. So don't use any shifting or other'shortcuts', because the compiler will recognize it and compile the code its own way no matter how you try to fool it.

S.
0
 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
If I optimise ((i<<3)+i) on VC7.1 with /Ox, I get...

      lea      eax, DWORD PTR [eax+eax*8]

...which makes more sense, because you get the whole shooting match in one 80386 instruction. Isn't it funny, though, to see it using a multiplication instead of the well-intended shift? The main trouble with attempting to help the compiler, is that it is tempting to think that we are still writing code for the 8086 instruction set :-)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now