about summary refs log tree commit diff stats
path: root/071deep_copy.cc
blob: f5f0f1eb3b71ce122e37e4cce269e7ec2b1051cd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// To recursively copy containers and any addresses they contain, use
// 'deep-copy'.
//
// Invariant: After a deep-copy its ingredient and result will point to no
// common addresses.
//
// We do handle cycles in the ingredient, however. All cycles are translated
// to new cycles in the product.

:(scenario deep_copy_number)
def main [
  local-scope
  x:num <- copy 34
  y:num <- deep-copy x
  10:bool/raw <- equal x, y
]
# non-address primitives are identical
+mem: storing 1 in location 10

:(scenario deep_copy_null_pointer)
def main [
  local-scope
  x:&:num <- copy 0
  y:&:num <- deep-copy x
  10:bool/raw <- equal x, y
]
# null addresses are identical
+mem: storing 1 in location 10

:(scenario deep_copy_container_without_address)
container foo [
  x:num
  y:num
]
def main [
  local-scope
  a:foo <- merge 34, 35
  b:foo <- deep-copy a
  10:bool/raw <- equal a, b
]
# containers are identical as long as they don't contain addresses
+mem: storing 1 in location 10

:(scenario deep_copy_address)
% Memory_allocated_until = 200;
def main [
  # avoid all memory allocations except the implicit ones inside deep-copy, so
  # that the result is deterministic
  1:&:num <- copy 100/unsafe  # pretend allocation
  *1:&:num <- copy 34
  2:&:num <- deep-copy 1:&:num
  10:bool <- equal 1:&:num, 2:&:num
  11:bool <- equal *1:&:num, *2:&:num
  2:&:num <- copy 0
]
# the result of deep-copy is a new address
+mem: storing 0 in location 10
# however, the contents are identical
+mem: storing 1 in location 11

:(scenario deep_copy_address_to_container)
% Memory_allocated_until = 200;
def main [
  # avoid all memory allocations except the implicit ones inside deep-copy, so
  # that the result is deterministic
  1:&:point <- copy 100/unsafe  # pretend allocation
  *1:&:point <- merge 34, 35
  2:&:point <- deep-copy 1:&:point
  10:bool <- equal 1:&:point, 2:&:point
  11:bool <- equal *1:&:point, *2:&:point
]
# the result of deep-copy is a new address
+mem: storing 0 in location 10
# however, the contents are identical
+mem: storing 1 in location 11

:(scenario deep_copy_address_to_address)
% Memory_allocated_until = 200;
def main [
  # avoid all memory allocations except the implicit ones inside deep-copy, so
  # that the result is deterministic
  1:&:&:num <- copy 100/unsafe  # pretend allocation
  *1:&:&:num <- copy 150/unsafe
  **1:&:&:num <- copy 34
  2:&:&:num <- deep-copy 1:&:&:num
  10:bool <- equal 1:&:&:num, 2:&:&:num
  11:bool <- equal *1:&:&:num, *2:&:&:num
  12:bool <- equal **1:&:&:num, **2:&:&:num
]
# the result of deep-copy is a new address
+mem: storing 0 in location 10
# any addresses in it or pointed to it are also new
+mem: storing 0 in location 11
# however, the non-address contents are identical
+mem: storing 1 in location 12

:(scenario deep_copy_array)
% Memory_allocated_until = 200;
def main [
  # avoid all memory allocations except the implicit ones inside deep-copy, so
  # that the result is deterministic
  100:num <- copy 3  # pretend array length
  1:&:@:num <- copy 100/unsafe  # pretend allocation
  put-index *1:&:@:num, 0, 34
  put-index *1:&:@:num, 1, 35
  put-index *1:&:@:num, 2, 36
  stash [old:], *1:&:@:num
  2:&:@:num <- deep-copy 1:&:@:num
  stash 2:&:@:num
  stash [new:], *2:&:@:num
  10:bool <- equal 1:&:@:num, 2:&:@:num
  11:bool <- equal *1:&:@:num, *2:&:@:num
]
+app: old: 3 34 35 36
+app: new: 3 34 35 36
# the result of deep-copy is a new address
+mem: storing 0 in location 10
# however, the contents are identical
+mem: storing 1 in location 11

:(scenario deep_copy_container_with_address)
container foo [
  x:num
  y:&:num
]
def main [
  local-scope
  y0:&:num <- new number:type
  *y0 <- copy 35
  a:foo <- merge 34, y0
  b:foo <- deep-copy a
  10:bool/raw <- equal a, b
  y1:&:num <- get b, y:offset
  11:bool/raw <- equal y0, y1
  12:num/raw <- copy *y1
]
# containers containing addresses are not identical to their deep copies
+mem: storing 0 in location 10
# the addresses they contain are not identical either
+mem: storing 0 in location 11
+mem: storing 35 in location 12

:(scenario deep_copy_exclusive_container_with_address)
exclusive-container foo [
  x:num
  y:&:num
]
def main [
  local-scope
  y0:&:num <- new number:type
  *y0 <- copy 34
  a:foo <- merge 1/y, y0
  b:foo <- deep-copy a
  10:bool/raw <- equal a, b
  y1:&:num, z:bool <- maybe-convert b, y:variant
  11:bool/raw <- equal y0, y1
  12:num/raw <- copy *y1
]
# exclusive containers containing addresses are not identical to their deep copies
+mem: storing 0 in location 10
# the addresses they contain are not identical either
+mem: storing 0 in location 11
+mem: storing 34 in location 12

:(scenario deep_copy_exclusive_container_with_container_with_address)
exclusive-container foo [
  x:num
  y:bar  # inline
]
container bar [
  x:&:num
]
def main [
  local-scope
  y0:&:num <- new number:type
  *y0 <- copy 34
  a:bar <- merge y0
  b:foo <- merge 1/y, a
  c:foo <- deep-copy b
  10:bool/raw <- equal b, c
  d:bar, z:bool <- maybe-convert c, y:variant
  y1:&:num <- get d, x:offset
  11:bool/raw <- equal y0, y1
  12:num/raw <- copy *y1
]
# exclusive containers containing addresses are not identical to their deep copies
+mem: storing 0 in location 10
# sub-containers containing addresses are not identical either
+mem: storing 0 in location 11
+mem: storing 34 in location 12

:(before "End Primitive Recipe Declarations")
DEEP_COPY,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "deep-copy", DEEP_COPY);
:(before "End Primitive Recipe Checks")
case DEEP_COPY: {
  if (SIZE(inst.ingredients) != 1) {
    raise << maybe(get(Recipe, r).name) << "'deep-copy' takes exactly one ingredient rather than '" << to_original_string(inst) << "'\n" << end();
    break;
  }
  if (SIZE(inst.products) != 1) {
    raise << maybe(get(Recipe, r).name) << "'deep-copy' takes exactly one ingredient rather than '" << to_original_string(inst) << "'\n" << end();
    break;
  }
  if (!types_strictly_match(inst.ingredients.at(0), inst.products.at(0))) {
    raise << maybe(get(Recipe, r).name) << "'deep-copy' requires its ingredient and product to be the same type, but got '" << to_original_string(inst) << "'\n" << end();
    break;
  }
  break;
}
:(before "End Primitive Recipe Implementations")
case DEEP_COPY: {
  products.push_back(deep_copy(current_instruction().ingredients.at(0)));
  break;
}

:(code)
vector<double> deep_copy(const reagent& in) {
  // allocate a tiny bit of temporary space for deep_copy()
  trace(9991, "run") << "deep-copy: allocating space for temporary" << end();
  reagent tmp("tmp:address:number");
  tmp.set_value(allocate(1));
  map<int, int> addresses_copied;
  vector<double> result = deep_copy(in, addresses_copied, tmp);
  // reclaim Mu memory allocated for tmp
  trace(9991, "run") << "deep-copy: reclaiming temporary" << end();
  abandon(tmp.value, payload_size(tmp));
  // reclaim host memory allocated for tmp.type when tmp goes out of scope
  return result;
}

vector<double> deep_copy(reagent/*copy*/ in, map<int, int>& addresses_copied, const reagent& tmp) {
  canonize(in);
  if (is_mu_address(in)) {
    // hack: skip primitive containers that do their own locking; they're designed to be shared between routines
    type_tree* payload = in.type->right;
    if (payload->left->name == "channel" || payload->left->name == "screen" || payload->left->name == "console" || payload->left->name == "resources")
      return read_memory(in);
  }
  vector<double> result;
  if (is_mu_address(in))
    result.push_back(deep_copy_address(in, addresses_copied, tmp));
  else
    deep_copy(in, addresses_copied, tmp, result);
  return result;
}

// deep-copy an address and return a new address
int deep_copy_address(const reagent& canonized_in, map<int, int>& addresses_copied, const reagent& tmp) {
  if (address_value(canonized_in) == 0) return 0;
  int in_address = payload_address(canonized_in);
  trace(9991, "run") << "deep-copy: copying address " << in_address << end();
  if (contains_key(addresses_copied, in_address)) {
    int out = get(addresses_copied, in_address);
    trace(9991, "run") << "deep-copy: copy already exists: " << out << end();
    return out;
  }
  int out = allocate(payload_size(canonized_in));
  trace(9991, "run") << "deep-copy: new address is " << out << end();
  put(addresses_copied, in_address, out);
  reagent/*copy*/ payload = canonized_in;
  payload.properties.push_back(pair<string, string_tree*>("lookup", NULL));
  trace(9991, "run") << "recursing on payload " << payload.value << ' ' << to_string(payload) << end();
  vector<double> data = deep_copy(payload, addresses_copied, tmp);
  trace(9991, "run") << "deep-copy: writing result " << out << ": " << to_string(data) << end();
  // HACK: write_memory interface isn't ideal for this situation; we need
  // a temporary location to help copy the payload.
  trace(9991, "run") << "deep-copy: writing temporary " << tmp.value << ": " << out << end();
  put(Memory, tmp.value, out);
  payload.set_value(tmp.value);  // now modified for output
  vector<double> old_data = read_memory(payload);
  trace(9991, "run") << "deep-copy: really writing to " << payload.value << ' ' << to_string(payload) << " (old value " << to_string(old_data) << " new value " << to_string(data) << ")" << end();
  write_memory(payload, data);
  trace(9991, "run") << "deep-copy: output is " << to_string(data) << end();
  return out;
}

// deep-copy a non-address and return a vector of locations
void deep_copy(const reagent& canonized_in, map<int, int>& addresses_copied, const reagent& tmp, vector<double>& out) {
  assert(!is_mu_address(canonized_in));
  vector<double> data = read_memory(canonized_in);
  out.insert(out.end(), data.begin(), data.end());
  if (!contains_key(Container_metadata, canonized_in.type)) return;
  trace(9991, "run") << "deep-copy: scanning for addresses in " << to_string(data) << end();
  const container_metadata& metadata = get(Container_metadata, canonized_in.type);
  for (map<set<tag_condition_info>, set<address_element_info> >::const_iterator p = metadata.address.begin();  p != metadata.address.end();  ++p) {
    if (!all_match(data, p->first)) continue;
    for (set<address_element_info>::const_iterator info = p->second.begin();  info != p->second.end();  ++info) {
      // hack: skip primitive containers that do their own locking; they're designed to be shared between routines
      if (!info->payload_type->atom && info->payload_type->left->name == "channel")
        continue;
      if (info->payload_type->name == "screen" || info->payload_type->name == "console" || info->payload_type->name == "resources")
        continue;
      // construct a fake reagent that reads directly from the appropriate
      // field of the container
      reagent curr;
      if (info->payload_type->atom)
        curr.type = new type_tree(new type_tree("address"), new type_tree(new type_tree(info->payload_type->name), NULL));
      else
        curr.type = new type_tree(new type_tree("address"), new type_tree(*info->payload_type));
      curr.set_value(canonized_in.value + info->offset);
      curr.properties.push_back(pair<string, string_tree*>("raw", NULL));
      trace(9991, "run") << "deep-copy: copying address " << curr.value << end();
      out.at(info->offset) = deep_copy_address(curr, addresses_copied, tmp);
    }
  }
}

int payload_address(reagent/*copy*/ x) {
  x.properties.push_back(pair<string, string_tree*>("lookup", NULL));
  canonize(x);
  return x.value;
}

int address_value(const reagent& x) {
  assert(is_mu_address(x));
  vector<double> result = read_memory(x);
  assert(SIZE(result) == 1);
  return static_cast<int>(result.at(0));
}

:(scenario deep_copy_stress_test_1)
container foo1 [
  p:&:num
]
container foo2 [
  p:&:foo1
]
exclusive-container foo3 [
  p:&:foo1
  q:&:foo2
]
def main [
  local-scope
  x:&:num <- new number:type
  *x <- copy 34
  a:&:foo1 <- new foo1:type
  *a <- merge x
  b:&:foo2 <- new foo2:type
  *b <- merge a
  c:foo3 <- merge 1/q, b
  d:foo3 <- deep-copy c
  e:&:foo2, z:bool <- maybe-convert d, q:variant
  f:&:foo1 <- get *e, p:offset
  g:&:num <- get *f, p:offset
  1:num/raw <- copy *g
]
+mem: storing 34 in location 1

:(scenario deep_copy_stress_test_2)
container foo1 [
  p:&:num
]
container foo2 [
  p:&:foo1
]
exclusive-container foo3 [
  p:&:foo1
  q:&:foo2
]
container foo4 [
  p:num
  q:&:foo3
]
def main [
  local-scope
  x:&:num <- new number:type
  *x <- copy 34
  a:&:foo1 <- new foo1:type
  *a <- merge x
  b:&:foo2 <- new foo2:type
  *b <- merge a
  c:&:foo3 <- new foo3:type
  *c <- merge 1/q, b
  d:foo4 <- merge 35, c
  e:foo4 <- deep-copy d
  f:&:foo3 <- get e, q:offset
  g:&:foo2, z:bool <- maybe-convert *f, q:variant
  h:&:foo1 <- get *g, p:offset
  y:&:num <- get *h, p:offset
  1:num/raw <- copy *y
]
+mem: storing 34 in location 1

:(scenario deep_copy_cycles)
container foo [
  p:num
  q:&:foo
]
def main [
  local-scope
  x:&:foo <- new foo:type
  *x <- put *x, p:offset, 34
  *x <- put *x, q:offset, x  # create a cycle
  y:&:foo <- deep-copy x
  1:num/raw <- get *y, p:offset
  y2:&:foo <- get *y, q:offset
  2:bool/raw <- equal y, y2  # is it still a cycle?
  3:bool/raw <- equal x, y  # is it the same cycle?
  # not bothering cleaning up; both cycles leak memory
]
+mem: storing 34 in location 1
# deep copy also contains a cycle
+mem: storing 1 in location 2
# but it's a completely different (disjoint) cycle
+mem: storing 0 in location 3

//: todo: version of deep_copy_cycles that breaks cycles at end and verifies no memory leaks
//: needs approximate matching over scenario traces

:(scenario deep_copy_skips_channel)
# ugly! dummy 'channel' type if we happen to be building without that layer
% if (!contains_key(Type_ordinal, "channel")) get_or_insert(Type, put(Type_ordinal, "channel", Next_type_ordinal++)).name = "channel";
def main [
  local-scope
  x:&:channel:num <- new {(channel num): type}
  y:&:channel:num <- deep-copy x
  10:bool/raw <- equal x, y
]
# channels are never deep-copied
+mem: storing 1 in location 10

:(scenario deep_copy_skips_nested_channel)
# ugly! dummy 'channel' type if we happen to be building without that layer
% if (!contains_key(Type_ordinal, "channel")) get_or_insert(Type, put(Type_ordinal, "channel", Next_type_ordinal++)).name = "channel";
container foo [
  c:&:channel:num
]
def main [
  local-scope
  x:&:foo <- new foo:type
  y:&:foo <- deep-copy x
  xc:&:channel:num <- get *x, c:offset
  yc:&:channel:num <- get *y, c:offset
  10:bool/raw <- equal xc, yc
]
# channels are never deep-copied
+mem: storing 1 in location 10

:(scenario deep_copy_skips_resources)
# ugly! dummy 'resources' type if we happen to be building without that layer
% if (!contains_key(Type_ordinal, "resources")) get_or_insert(Type, put(Type_ordinal, "resources", Next_type_ordinal++)).name = "resources";
def main [
  local-scope
  x:&:resources <- new resources:type
  y:&:resources <- deep-copy x
  10:bool/raw <- equal x, y
]
# resources are never deep-copied
+mem: storing 1 in location 10

:(scenario deep_copy_skips_nested_resources)
# ugly! dummy 'resources' type if we happen to be building without that layer
% if (!contains_key(Type_ordinal, "resources")) get_or_insert(Type, put(Type_ordinal, "resources", Next_type_ordinal++)).name = "resources";
container foo [
  c:&:resources
]
def main [
  local-scope
  x:&:foo <- new foo:type
  y:&:foo <- deep-copy x
  xc:&:resources <- get *x, c:offset
  yc:&:resources <- get *y, c:offset
  10:bool/raw <- equal xc, yc
]
# resources are never deep-copied
+mem: storing 1 in location 10