473,382 Members | 1,651 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Working with 3D matrices

Hi,
I am trying to speed up some work I have to do with a 3D Matrix.

If I am not mistaken, C is row major,
hence the matrix
A of size 2 x 2 x 2
would be stored like this in memory:
A[1][1][1],A[1][2][1],A[2][1][1],A[2][2][2],A[1][1][2],A[1][2][2],A[2][1][2],A[2][2][2]
?
Am I correct, I need to work on Big 3D matrices, and I know that the way
I traverse the matrices counts, which way is fastest? will gcc optimize
this as to minimize the page jumps in memory?
The matrix is declared using malloc so if A is n x n x d then i setup
the matrix this way:

A = (int***) malloc(n*sizeof(int));
for(i = 0;i < n;i++){
A[i] = (int**) malloc(n*sizeof(int));
for(j = 0;j < n;j++)
A[i][j] = (int*) malloc(d*sizeof(int));
}

Is this a good way to declare the 3d Matrix? and am I traversing it
correctly?

Thank You for the help.

Fred
Jan 30 '06 #1
5 2394
Frederic Soustra <Fr**************@sympatico.ca> writes:
I am trying to speed up some work I have to do with a 3D Matrix.

If I am not mistaken, C is row major,
hence the matrix
A of size 2 x 2 x 2
would be stored like this in memory:
A[1][1][1],A[1][2][1],A[2][1][1],A[2][2][2],A[1][1][2],A[1][2][2],A[2][1][2],A[2][2][2]
?
Um, not even close.

First, C arrays are 0-based, not 1-based.

Yes, C arrays are stored in row-major order. For

int A[2][2][2];

the order in memory would be:

A[0][0][0]
A[0][0][1]
A[0][1][0]
A[0][1][1]
A[1][0][0]
A[1][0][1]
A[1][1][0]
A[1][1][1]
Am I correct, I need to work on Big 3D matrices, and I know that the
way I traverse the matrices counts, which way is fastest? will gcc
optimize this as to minimize the page jumps in memory?
Any relationship between the order in which you traverse the array and
performance is implementation-specific; the language says nothing
about it. If you want to know about optimizations performed by gcc,
try one of the gnu.gcc.* newsgroups.
The matrix is declared using malloc so if A is n x n x d then i setup
the matrix this way:

A = (int***) malloc(n*sizeof(int));
for(i = 0;i < n;i++){
A[i] = (int**) malloc(n*sizeof(int));
for(j = 0;j < n;j++)
A[i][j] = (int*) malloc(d*sizeof(int));
}

Is this a good way to declare the 3d Matrix? and am I traversing it
correctly?


You haven't shown us the declaration of A.

There are several ways to create a 3-dimensional array: an array of
arrays of arrays, an array of arrays of pointers to arrays, and so
forth. What you're doing here doesn't look right. You should never
cast the result of malloc(). If A is an int***, it doesn't make sense
to point it to something of size (n*sizeof(int)).

Read question 6.16 in the C FAQ, at <http://www.c-faq.com/>. Then
read the rest of section 6, then read the rest of the FAQ. If you
still have questions, feel free to come back.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 30 '06 #2
Keith Thompson wrote:
Frederic Soustra <Fr**************@sympatico.ca> writes:
I am trying to speed up some work I have to do with a 3D Matrix.

If I am not mistaken, C is row major,
hence the matrix
A of size 2 x 2 x 2
would be stored like this in memory:
A[1][1][1],A[1][2][1],A[2][1][1],A[2][2][2],A[1][1][2],A[1][2][2],A[2][1][2],A[2][2][2]
?

Thx, I was still thinking in matlab mode.
Um, not even close.

First, C arrays are 0-based, not 1-based.

Yes, C arrays are stored in row-major order. For

int A[2][2][2];

the order in memory would be:

A[0][0][0]
A[0][0][1]
A[0][1][0]
A[0][1][1]
A[1][0][0]
A[1][0][1]
A[1][1][0]
A[1][1][1]

Am I correct, I need to work on Big 3D matrices, and I know that the
way I traverse the matrices counts, which way is fastest? will gcc
optimize this as to minimize the page jumps in memory?

Any relationship between the order in which you traverse the array and
performance is implementation-specific; the language says nothing
about it. If you want to know about optimizations performed by gcc,
try one of the gnu.gcc.* newsgroups.

The matrix is declared using malloc so if A is n x n x d then i setup
the matrix this way:
A = (int***) malloc(n*sizeof(int));
for(i = 0;i < n;i++){
A[i] = (int**) malloc(n*sizeof(int));
for(j = 0;j < n;j++)
A[i][j] = (int*) malloc(d*sizeof(int));
}

Is this a good way to declare the 3d Matrix? and am I traversing it
correctly?

You haven't shown us the declaration of A.

There are several ways to create a 3-dimensional array: an array of
arrays of arrays, an array of arrays of pointers to arrays, and so
forth. What you're doing here doesn't look right. You should never
cast the result of malloc(). If A is an int***, it doesn't make sense
to point it to something of size (n*sizeof(int)).

Read question 6.16 in the C FAQ, at <http://www.c-faq.com/>. Then
read the rest of section 6, then read the rest of the FAQ. If you
still have questions, feel free to come back.


Thank you for pointing me to the FAQ,
so according to the FAQ this is a correct setup for the 3D Matrix:

int*** A = (int***) malloc(n*sizeof(int**));
for(i = 0;i < n;i++){
A[i] = (int**) malloc(n*sizeof(int*));
for(j = 0;j < n;j++)
A[i][j] = (int*) malloc(d*sizeof(int));
}
and if I read your example of how it is stored in memory then,
it is completely inneficient to build the matrix this way if i am
traversing it in the following manner:
for(l = 0; l < d; l++){
for(i = 0;i <n;i++){
for(j = 0;j < n;j++){
/* do stuff with A[i][j][l]
}
}
}

Again correct me if I am wrong:
If I traverse this matrix using l, then i then j as the indices then my
declaration should look like this:
int*** A = (int***) malloc(d*sizeof(int**));
for(i = 0;i < d;i++){
A[i] = (int**) malloc(n*sizeof(int*));
for(j = 0;j < n;j++)
A[i][j] = (int*) malloc(n*sizeof(int));
}
Again thank you for pointing me to Q6.16, I had gone through the others
but had not seen 6.16.

Thanks a lot.

Fred
Jan 30 '06 #3
[...]
The matrix is declared using malloc so if A is n x n x d then i setup
the matrix this way:
A = (int***) malloc(n*sizeof(int));
as Keith pointed out, never cast return of malloc
are you using C++ compiler?
if so then replace malloc with new
for(i = 0;i < n;i++){
A[i] = (int**) malloc(n*sizeof(int));
for(j = 0;j < n;j++)
A[i][j] = (int*) malloc(d*sizeof(int));
}

here the same
and if I read your example of how it is stored in memory then,
it is completely inneficient to build the matrix this way if i am
traversing it in the following manner:
for(l = 0; l < d; l++){
for(i = 0;i <n;i++){
for(j = 0;j < n;j++){
/* do stuff with A[i][j][l]
}
}
}


just as idea, you could do

int dim1, dim2, dim3;
int * large = malloc(sizeof(int) * dim1*dim2*dim3);

inline int access_large(int i, int j, int k)
{ return large + i*dim2*dim3 + j*dim3 + k; }

you can also provide DEBUG_ARRAY mode and check the ranges
whether i,j,k are valid

Regards, Daniel

Jan 30 '06 #4
const int dim1 = 10, dim2 = 20, dim3 = 5;
int * large = malloc(sizeof(int) * dim1*dim2*dim3);

inline int * access_large(int i, int j, int k)
{ return large + i*dim2*dim3 + j*dim3 + k; }

*acces_large(0,0,0) = 10;
int xyz = *acces_large(0,0,0);

Jan 30 '06 #5
In article <m2*******************@news20.bellglobal.com> Frederic Soustra <Fr**************@sympatico.ca> writes:
Thank you for pointing me to the FAQ,
so according to the FAQ this is a correct setup for the 3D Matrix:

int*** A = (int***) malloc(n*sizeof(int**));
for(i = 0;i < n;i++){
A[i] = (int**) malloc(n*sizeof(int*));
for(j = 0;j < n;j++)
A[i][j] = (int*) malloc(d*sizeof(int));
}
This is not a three-dimensional array. A three-dimensional array would
have the type "array of array of array of int". Your array has type
"array of pointer to array of pointer to array of int".
and if I read your example of how it is stored in memory then,
It is completely irrelevant. In the above example (with n = 2) you
can not be sure that A[0][0][1] is followed by A[0][1][0].
it is completely inneficient to build the matrix this way if i am
traversing it in the following manner:
for(l = 0; l < d; l++){
for(i = 0;i <n;i++){
for(j = 0;j < n;j++){
/* do stuff with A[i][j][l]
}
}
}


With your definition it is indeed extremely inefficient. When you
have a true 3-dimensional array it may be inefficient, but that
depends on how the cache of your processor works.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Jan 31 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Nils Wagner | last post by:
Hi all, Has someone written a C binding to transfer matrices from C to Python and vice versa ? Any pointer would be appreciated. Nils
3
by: tommy | last post by:
Hello everyone, I'm trying to write a program which will be adding and subtracting two matrices. I've already written some code but there are a few problems. 1) During compilation I get this...
8
by: Noesis Strategy | last post by:
I am attempting to build a database that profiles about 50 companies in a single industries. In a standard MS Word format, each profile would be about 20 pages of dense text and tables. In order...
3
by: Prototipo | last post by:
Hi! I need to dynamically create X vectors of matrices (with a known size of 5x5). It's like a three-dimensional matrix with 2 known dimensions and the third unknown. I have something like: ...
1
emaghero
by: emaghero | last post by:
Does anybody know an algorithm for the fast multiplication of three n-x-n symmetric matrices? I have an algorithm, which I'm not too pleased with, that does the job. Does anybody know a faster...
9
by: tomamil | last post by:
imagine that you have different matrices with different names and you want to perform the same action with each of them. is it possible to put their names into some array and to create a loop that...
5
by: td0g03 | last post by:
This program adds two square matrices - Algorithm 1-3, Page 35. Change it to implement the generalized algorithm to add two m x n matrices. Matrices, in general, are not square or n x n...
2
by: debiasri | last post by:
I have to devide a matrix in variable no of matrices with each having variable no of rows. Like I have to devide a matrix with dim.9X19 into say 4 matrices with row nos 1,6,6,6 respectively, and I...
0
by: Sin Jeong-hun | last post by:
There's a sample project in the DirectX SDK that draws a revolving triangle. From it, I've written a simple program that draws a revolving box. The problem is, even though I have set the material...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.