2

A great man once said, I have a matrix A. But this time she has a friend B. Like the Montagues and Capulets, they have different domains.

// A.domain is { 1..10, 1..10 }
// B.domain is { 0.. 9, 0.. 9 }

for ij in B.domain {
  if B[ij] <has a condition> {
    // poops
    A[ij] = B[ij];
  }
}

My guess is I need to reindex so that the B.domain is {1..10, 1..10}. Since B is an input, I get push back from the compiler. Any suggestions?

user3666197
  • 1
  • 6
  • 50
  • 92
Brian Dolan
  • 3,086
  • 2
  • 24
  • 35
  • 1
    @user3666197 is correct about your example being invalid Chapel code, due to assigning domains of arrays like `A.domain`.. It might be worth updating it to avoid confusion in the future -- My answer took an educated guess at what you intended to do. – ben-albrecht Sep 01 '17 at 00:14
  • Ben, feel free to modify the syntax. Just tried to sketch an idea to approach the solution from a pure and cheap index-translation so as to avoid any large-scale ( as Brian is keen to often add ) **`.reindex()`** or any even more expensive memory-mapping / memory-dupes. – user3666197 Sep 01 '17 at 00:23

2 Answers2

1

There's a reindex array method to accomplish exactly this, and you can create a ref to the result to prevent creating a new array:

var Adom = {1..10,1..10},
    Bdom = {0..9, 0..9};

var A: [Adom] real,
    B: [Bdom] real;

// Set B to 1.0
B = 1;

// 0-based reference to A -- note that Bdom must be same shape as Adom
ref A0 = A.reindex(Bdom);

// Set all of A's values to B's values
for ij in B.domain {
  A0[ij] = B[ij];
}

// Confirm A is now 1.0 now
writeln(A);
ben-albrecht
  • 1,785
  • 10
  • 23
0

compiler must object,
documentation is explicit and clear on this:

Note that querying an array's domain via the .domain method or the function argument query syntax does not result in a domain expression that can be reassigned. In particular, we cannot do:

VarArr.domain = {1..2*n};

In case the <has_a_condition> is non-intervening and without side-effects, a solution to the expressed will may use domain-operators similar to this pure, contiguous-domain, index translation:

forall ij in B.domain do {
   if <has_a_condition> {
      A[ ij(1) + A.domain.dims()(1).low,
         ij(2) + A.domain.dims()(2).low
         ] = B[ij];
   }
}
user3666197
  • 1
  • 6
  • 50
  • 92
  • I understand the syntax, I'm just conveying the ideas. – Brian Dolan Sep 01 '17 at 01:59
  • 1
    Interesting are the hopefully just [PTIME] and [PSPACE] costs of the Ben's approach, vs. a [CTIME] ( lim -> 0, if compiler gets the CPU-registers to service properly ) and [CSPACE] == 0 approach here. For MAD/Massive Matrix A, B manipulations, this is out of question an aspect to carefully check. – user3666197 Sep 01 '17 at 02:33