Marko Kobal <marko.ko...@arctur.si> wrote on 08/09/2012 04:32:25 AM:
>
> I'm new in X10 and I see that arrays in X10 are really "awesome" ... but
> will take me some time to fully understand how to work with them. I
> understand that arrays are indexed by points, etc., and are "different"
> from C or java-like arrays. For now lets talk about the simple, not
> distributed arrays.
>
> Now, before I go in detailed reading of the specifications, I would like
> to see fairly straightforward example that would help me understand what
> are the best ways to handle multi-dimensional arrays.
>
> Let's start with a simple case of dynamically allocated matrix
> representation in array. In C one might do something like that:
>
> int rows, columns;
> double **matrix;
>
> // typically would be set dynamically from some kind of input,
> // let's just make it simple for this example
> rows = 2;
> columns = 3;
>
> matrix = malloc(rows * sizeof(double*));
> for (i = 0; i < rows; i++) {
> matrix[i] = malloc(columns * sizeof(double*));
> }
>
> // typically would be set dynamically from some kind of input,
> // let's just make it simple for this example
> m1[0][0] = 1.01;
> m1[0][1] = 2.19;
> m1[0][2] = 3.28;
> m1[1][0] = 2.12;
> m1[1][1] = 4.61;
> m1[1][2] = 3.44;
>
> ---
>
> No, how would one do this in X10? As I understand there is probably more
> that one way to do it? There are probably "performance-wise" and
> "c/java-like" ways to do it?
>
> Many thanks for help!
>
>
Hi,
To really get good performance out of the current implementation of
X10 arrays there are a couple of tricks:
(1) in hot code you want to avoid using points and instead use
the "rank-specialized" methods that take ints (in effect exploded points)
as indices.
(2) for anything other than a Rail (a typedef for for Array
[T]{rank=1,zeroBased,rect,rail}) bounds checking is quite expensive. So,
for performance/productions runs you will want to disable bounds checking
(-NO_CHECKS=true) to avoid multiple virtual calls per array access.
(3) The X10 optimizer gets quite a bit of benefit from precise
(including constraints) type information about X10 arrays. This is really
important for the zeroBased, 1-D arrays, so we have the Rail typedef to
help people say all the critical facts concisely. Use it when you can...
For the code above, something like the following is what you want:
val r = (0..1) * (0..2); // create a Region of rank 2 containing
(0,0), (0,1), (0,2), (1,0), (1,1), (1,2)
val a = new Array[Double](r); // create a zero-initialized 3x2 array
of doubles
a(0,0) = 1.01; // set a(0,0) to 1.01
a(0,1) = 2.19;
... etc...
--dave
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
X10-users mailing list
X10-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/x10-users