Gamestudio Links
Zorro Links
Newest Posts
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
M1 Oversampling
by 11honza11. 04/30/24 08:16
Trading Journey
by howardR. 04/28/24 09:55
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
Eigenwerbung
by jcl. 04/26/24 11:08
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (degenerate_762, AndrewAMD), 877 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
wandaluciaia, Mega_Rod, EternallyCurious, howardR, 11honza11
19049 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 4 of 5 1 2 3 4 5
Re: A8 Version 8.012 Public Beta [Re: Liamissimo] #333255
07/15/10 08:29
07/15/10 08:29
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline OP

Chief Engineer
jcl  Offline OP

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
Here's the solution to the user quiz. Aside from the memory bug, there's one main problem. A one dimensional array is indeed identical to a pointer, but a multi dimensional array is not. int array[x][y] is very different to int **array. In fact a pointer to a pointer is no array at all.

Whenever you access this construct, the program hits two different memory areas. This is much slower than using an array, which is a single contiguous memory area. There are further subtle problems with this coding style, so better don't use it in a real application.

If you do, you must use brackets, and you should use them even when not required by the language syntax. Otherwise either you or some other poor fellow working with your code will confuse this construct with a real multidimensional array, and then you'll get the nicest bugs and crashes.

To be on the safe side, use a normal multidimensional array for a static definition, and a single pointer for a dynamic definition, no matter how many dimensions.

Re: A8 Version 8.012 Public Beta [Re: jcl] #333265
07/15/10 10:29
07/15/10 10:29
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
I use this construct in many real applications, in fact my iPhone engine bases heavily on this kind of pointer to pointer to memory access relation.

There is a performance problem, but this has nothing todo with those pointers but with copying the data into the GPU memory and blocking the CPU in the meantime.

I just checked the access time on my very old iPhone 3G running iOS 4.1 and the difference between fixed array and pointer to pointer is not visible in the log (about 0.010 seconds to read and write into the memory).

I know that I can't compare a RISC CPU with an x86 CISC CPU, so I checked it also on my 2 Ghz Intel Dual Core Machine running Mac OS X 10.6.4, the result:
Read and write into the memory was about 0.001 seconds (both, fixed arrays and pointer to pointer).

Both versions had debug symbols and was build with LLVM 1.5 and Clang. No optimizations and no link time optimization.
I have checked the assembler code that was generated to make sure that the loops will be executed.
The test function has written PI into every member of the array and then read the value and saved it into another float.

Output:
OS X:
Code:
2010-07-15 12:19:57.766 Test[1375:903] Writing fixed
2010-07-15 12:19:57.766 Test[1375:903] Reading fixed
2010-07-15 12:19:57.767 Test[1375:903] Writing pointer
2010-07-15 12:19:57.767 Test[1375:903] Reading pointer
2010-07-15 12:19:57.768 Test[1375:903] Writing fixed
2010-07-15 12:19:57.769 Test[1375:903] Reading fixed
2010-07-15 12:19:57.769 Test[1375:903] Writing pointer
2010-07-15 12:19:57.770 Test[1375:903] Reading pointer



iPhone:
Code:
2010-07-15 12:24:23.859 iTest[507:307] Writing fixed
2010-07-15 12:24:23.872 iTest[507:307] Reading fixed
2010-07-15 12:24:23.885 iTest[507:307] Writing pointer
2010-07-15 12:24:23.900 iTest[507:307] Reading pointer
2010-07-15 12:24:23.913 iTest[507:307] Writing fixed
2010-07-15 12:24:23.926 iTest[507:307] Reading fixed
2010-07-15 12:24:23.940 iTest[507:307] Writing pointer
2010-07-15 12:24:23.953 iTest[507:307] Reading pointer



Could you please explain what is so much slower? Because I really can't reproduce this (was too lazy to boot windows, so I haven't tested it with Gamestudio).
I use this construct very often, and if there is really a performance problem with that, I wish to know about that so I can avoid this.


Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com
Re: A8 Version 8.012 Public Beta [Re: WretchedSid] #333268
07/15/10 10:58
07/15/10 10:58
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline OP

Chief Engineer
jcl  Offline OP

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
0.001 seconds seems very slow even for copying a huge memory area, regardless of the method.

Anyway, the reason of the performance problem is the CPU cache. When you access a memory location, a cache line is filled. The next time you hit that area, the CPU reads from the cache instead of the memory.

The more different memory areas are accessed, the more cache lines are created. When the cache is full, old lines are removed and new lines filled. This causes a severe performance hit. That's why in time critical programs, especially 3D engines, the programmers avoid reading from different allocated memory areas at the same time. Of course this effect is the worse, the bigger the memory areas are and the more fragmented the memory is.

Of course there are more problems with that method - for instance, you'll risk a crash when you initialize that construct with a memset call, as you would do with an array. I would not use that method for non time critical applications either.

Re: A8 Version 8.012 Public Beta [Re: jcl] #333271
07/15/10 11:20
07/15/10 11:20
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
Originally Posted By: jcl
0.001 seconds seems very slow even for copying a huge memory area, regardless of the method.


Well, I'm sure that it is much faster, but the log function needs some time too. On the iPhone it also needs to tether the log message to the host and burn some CPU time on this task.


Thanks for explaining, I didn't know about this issue and I will try to avoid this kind of pointer to pointer relation in the future too.

Edit: Another wish for the A8 compiler: Closures and thread-safe engine functions.
Both together would be perfect for multi-threaded projects. Or even better than Closures, Blocks: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1370.pdf

Last edited by JustSid; 07/15/10 11:26.

Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com
Re: A8 Version 8.012 Public Beta [Re: WretchedSid] #333277
07/15/10 13:29
07/15/10 13:29
Joined: Dec 2008
Posts: 1,218
Germany
Rackscha Offline
Serious User
Rackscha  Offline
Serious User

Joined: Dec 2008
Posts: 1,218
Germany
@JCL:
Ok how to use a single pointer for a dynamic definition?

when i use:

int *array;

and try to write/read like this:

(array[1])[1] = 5;

i always get:
Subscript requires array or pointer type.


MY Website with news of my projects:
(for example my current
Muliplayer Bomberman,
GenesisPrecompiler for LiteC
and TileMaster, an easy to use Tile editor)
Sparetime-Development

Re: A8 Version 8.012 Public Beta [Re: Rackscha] #333287
07/15/10 14:16
07/15/10 14:16
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
You can use something like this:

Code:
int *array;

array[(row * n) + col] = x



While n is the number of elements, row the row and col the column. To make it more convenient, you can use this makro:

Code:
int *array;

#define M(row,col)  array[(row * n) + col]
M(1, 1) = x;



Of course you need to allocate the memory as one dimensional array. Like this:
Code:
int *array = (int *)malloc((rows * cols) * sizeof(int));




Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com
Re: A8 Version 8.012 Public Beta [Re: WretchedSid] #333295
07/15/10 16:16
07/15/10 16:16
Joined: Jul 2009
Posts: 1,198
Berlin, Germany
L
Liamissimo Offline
Serious User
Liamissimo  Offline
Serious User
L

Joined: Jul 2009
Posts: 1,198
Berlin, Germany
@JCL

I found a bug. Maybe. I can move through all my models, even if I am setting BBOX or Polygon in WED/SED. Anyone else has this problem?


"Ich weiss nicht genau, was Sie vorhaben, aber Sie können keine Triggerzonen durch Ihr Level kullern lassen."
-JCL, 2011
Re: A8 Version 8.012 Public Beta [Re: jcl] #333301
07/15/10 16:49
07/15/10 16:49
Joined: May 2007
Posts: 2,043
Germany
Lukas Offline

Programmer
Lukas  Offline

Programmer

Joined: May 2007
Posts: 2,043
Germany
Originally Posted By: jcl
Here's the solution to the user quiz. Aside from the memory bug, there's one main problem. A one dimensional array is indeed identical to a pointer, but a multi dimensional array is not. int array[x][y] is very different to int **array. In fact a pointer to a pointer is no array at all.

Whenever you access this construct, the program hits two different memory areas. This is much slower than using an array, which is a single contiguous memory area. There are further subtle problems with this coding style, so better don't use it in a real application.

If you do, you must use brackets, and you should use them even when not required by the language syntax. Otherwise either you or some other poor fellow working with your code will confuse this construct with a real multidimensional array, and then you'll get the nicest bugs and crashes.

To be on the safe side, use a normal multidimensional array for a static definition, and a single pointer for a dynamic definition, no matter how many dimensions.


Really? REALLY? This sounds weird. In fact, afair I learned that multidimensional arrays are nothing else than an array of pointers to other arrays, which don't necessarily have to be in one contiguous memory area.
However, if this is true, what you say, why can I use pointers to pointers in function parameters if I want to pass an array to a function? And if that makes a difference, then Lite-C should support arrays with indefinite size as function parameters.
However, I think whenever I saw an example how to create a multidimensional array at runtime, it was always like Rackscha did it. If it is true what you say creating a "real" multidimensional array would look like this:
Code:
int** create_2darray (int sx, int sy)
{
	int** array;
	int* p = malloc(sx*sy*sizeof(int)+sx*sizeof(int)); // allocate one single contiguous memory area (for the data and the pointers)
	array = p; // set the array pointer to its beginning
	int i;
	for(i=0;i<sx;i++) array[i] = p+sx+sy*i; //fill the beginning of the memory with pointers to the 1-dimensional pointers
	return(array);
}


Is that right? But in this case, the pointers to the arrays would be a waste of memory in comparision to a 1-dimensional array of the size sx*sy.

Re: A8 Version 8.012 Public Beta [Re: Lukas] #333338
07/15/10 20:32
07/15/10 20:32
Joined: Sep 2003
Posts: 929
Spirit Offline

Moderator
Spirit  Offline

Moderator

Joined: Sep 2003
Posts: 929
Quote:
In fact, afair I learned that multidimensional arrays are nothing else than an array of pointers to other arrays, which don't necessarily have to be in one contiguous memory area.

I am sure this is wrong, no compiler that I know uses an array of pointers to other arrays or something like your code for substituting a multidimensional array. All normal C/C++ compilers support multidimensional arrays natively.

Re: A8 Version 8.012 Public Beta [Re: Spirit] #333339
07/15/10 20:36
07/15/10 20:36
Joined: May 2007
Posts: 2,043
Germany
Lukas Offline

Programmer
Lukas  Offline

Programmer

Joined: May 2007
Posts: 2,043
Germany
So this means that multidimensional arrays are indeed just the same as a one-dimensional array, just that it's preceeded by an array of pointers? In this case multi-dimensional arrays would indeed be just a waste of memory and wouldn't make sense to use as it would always be better to use one-dimensional arrays. But then, why do they exist? o.O

Page 4 of 5 1 2 3 4 5

Moderated by  Matt_Coles 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1