File: Customizing-Arrays.md

package info (click to toggle)
tiledarray 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 9,568 kB
  • sloc: cpp: 53,449; javascript: 1,599; sh: 393; ansic: 226; python: 223; xml: 195; makefile: 36
file content (560 lines) | stat: -rw-r--r-- 20,348 bytes parent folder | download
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
# Customizing DistArray {#Customizing-Arrays}

* [User Defined Tiles](#wiki-user-defined-tiles)
  * [Lazy Tiles](#wiki-lazy-tiles)
  * [Data Tiles](#wiki-data-tiles)
* [User Defined Shapes](#wiki-ser-defined-shapes)
* [User Defined Process Map](#wiki-user-defined-process-map)

# User Defined Tiles

The default tile type of `TiledArray::DistArray` is `TiledArray::Tensor`. However, TiledArray supports using user-defined types as tiles.
There are few scenarios where one would like to provide a non-standard type as a tile; for example,
user wants to provide a more efficient implementation of certain operations on tiles.
There are two modes of user-defined types that can be used as tiles: types that store the data elements explicitly (“data tiles”)
and types that generate a data tile as needed (“lazy evaluation tiles”).

## User-Defined Data Tiles

Any user-defined tensor type can play a role of a data tile provided it matches the same concept as `TiledArray::Tensor`. For brevity, instead of an actual concept spec here is an example of a custom tile type that meets the concept spec.

```
class MyTensor {
public:
  // Typedefs
  typedef MyTensor eval_type;           // The type used when evaluating expressions
  typedef TiledArray::Range range_type; // Tensor range type
  typedef ... value_type;               // Element type
  typedef ... numeric_type;             // The scalar type that is compatible with value_type
  typedef ... size_type;                // Size type

public:

  // Default constructors (may be uninitialized)
  MyTensor();

  // Shallow copy constructor; see MyTensor::clone() for deep copy
  MyTensor(const MyTensor& other);

  // Shallow assignment operator; see MyTensor::clone() for deep copy
  MyTensor& operator=(const MyTensor& other);

  // Deep copy
  MyTensor clone() const;

  // Tile range accessor
  const range_type& range() const;

  // Number of elements in the tile
  size_type size() const;

  // Initialization check. False if the tile is fully initialized.
  bool empty() const;

  // MADNESS-compliant serialization
  template <typename Archive>
  void serialize(Archive& ar);

  // Permutation operation

  // result[perm ^ i] = (*this)[i]
  MyTensor permute(const TiledArray::Permutation& perm) const;

  // Scaling operations

  // result[i] = (*this)[i] * factor
  MyTensor scale(const numeric_type factor) const;
  // result[perm ^ i] = (*this)[i] * factor
  MyTensor scale(const numeric_type factor, const TiledArray::Permutation& perm) const;
  // (*this)[i] *= factor
  MyTensor& scale_to(const numeric_type factor) const;

  // Addition operations

  // result[i] = (*this)[i] + right[i]
  MyTensor add(const MyTensor& right) const;
  // result[i] = ((*this)[i] + right[i]) * factor
  MyTensor add(const MyTensor& right, const numeric_type factor) const;
  // result[i] = (*this)[i] + value
  MyTensor add(const value_type& value) const;

  // result[perm ^ i] = (*this)[i] + right[i]
  MyTensor add(const MyTensor& right, const TiledArray::Permutation& perm) const;
  // result[perm ^ i] = ((*this)[i] + right[i]) * factor
  MyTensor add(const MyTensor& right, const numeric_type factor, const TiledArray::Permutation& perm) const;
  // result[perm ^ i] = (*this)[i] + value
  MyTensor add(const value_type& value, const TiledArray::Permutation& perm) const;

  // (*this)[i] += right[i]
  MyTensor& add_to(const MyTensor& right) const;
  // ((*this)[i] += right[i]) *= factor
  MyTensor& add_to(const MyTensor& right, const numeric_type factor) const;
  // (*this)[i] += value
  MyTensor& add_to(const value_type& value) const;

  // Subtraction operations

  // result[i] = (*this)[i] - right[i]
  MyTensor subt(const MyTensor& right) const;
  // result[i] = ((*this)[i] - right[i]) * factor
  MyTensor subt(const MyTensor& right, const numeric_type factor) const;
  // result[i] = (*this)[i] - value
  MyTensor subt(const value_type& value) const;

  // result[perm ^ i] = (*this)[i] - right[i]
  MyTensor subt(const MyTensor& right, const TiledArray::Permutation& perm) const;
  // result[perm ^ i] = ((*this)[i] - right[i]) * factor
  MyTensor subt(const MyTensor& right, const numeric_type factor, const TiledArray::Permutation& perm) const;
  // result[perm ^ i] = (*this)[i] - value
  MyTensor subt(const value_type value, const TiledArray::Permutation& perm) const;

  // (*this)[i] -= right[i]
  MyTensor& subt_to(const MyTensor& right);
  // ((*this)[i] -= right[i]) *= factor
  MyTensor& subt_to(const MyTensor& right, const numeric_type factor);
  // (*this)[i] -= value
  MyTensor& subt_to(const value_type& value);

  // (Entrywise) multiplication operations (Hadamard product)

  // result[i] = (*this)[i] * right[i]
  MyTensor mult(const MyTensor& right) const;
  // result[i] = ((*this)[i] * right[i]) * factor
  MyTensor mult(const MyTensor& right, const numeric_type factor) const;

  // result[perm ^ i] = (*this)[i] * right[i]
  MyTensor mult(const MyTensor& right, const TiledArray::Permutation& perm) const;
  // result[perm^ i] = ((*this)[i] * right[i]) * factor
  MyTensor mult(const MyTensor& right, const numeric_type factor, const TiledArray::Permutation& perm) const;

  // *this[i] *= right[i]
  MyTensor& mult_to(const MyTensor& right);
  // (*this[i] *= right[i]) *= factor
  MyTensor& mult_to(const MyTensor& right, const numeric_type factor);

  // Negation operations

  // result[i] = -((*this)[i])
  MyTensor neg() const;
  // result[perm ^ i] = -((*this)[i])
  MyTensor neg(const TiledArray::Permutation& perm) const;
  // arg[i] = -((*this)[i])
  MyTensor& neg_to();

  // Contraction operations

  // GEMM operation with fused indices as defined by gemm_config; multiply this by other, return the result
  MyTensor gemm(const MyTensor& other, const numeric_type factor,
                const TiledArray::math::GemmHelper& gemm_config) const;

  // GEMM operation with fused indices as defined by gemm_config; multiply left by right, store to this
  MyTensor& gemm(const MyTensor& left, const MyTensor& right, const numeric_type factor,
                 const TiledArray::math::GemmHelper& gemm_config);

  // Reduction operations

  // Sum of hyper diagonal elements
  numeric_type trace() const;
  // foreach(i) result += arg[i]
  numeric_type sum() const;
  // foreach(i) result *= arg[i]
  numeric_type product() const;
  // foreach(i) result += arg[i] * arg[i]
  numeric_type squared_norm() const;
  // sqrt(squared_norm(arg))
  numeric_type norm() const;
  // foreach(i) result = max(result, arg[i])
  numeric_type max() const;
  // foreach(i) result = min(result, arg[i])
  numeric_type min() const;
  // foreach(i) result = max(result, abs(arg[i]))
  numeric_type abs_max() const;
  // foreach(i) result = max(result, abs(arg[i]))
  numeric_type abs_min() const;
  
} // class MyTensor
```

It is also possible to implement most of the concept requirements non-intrusively, by providing free functions. This can be helpful if you want to use an existing tensor class as a tile. Here’s an example of how to implement MyTensor without member functions:

```
class MyTensor {
public:
  // Typedefs
  typedef TiledArray::Range range_type; // Tensor range type
  typedef ... value_type;               // Element type
  typedef ... numeric_type;             // The scalar type that is compatible with value_type
  typedef ... size_type;                // Size type

public:

  // Default constructors (may be uninitialized)
  MyTensor();

  // Shallow copy constructor; see MyTensor::clone() for deep copy
  MyTensor(const MyTensor& other);

  // Shallow assignment operator; see MyTensor::clone() for deep copy
  MyTensor& operator=(const MyTensor& other);

  // Deep copy
  MyTensor clone() const;

  // Tile range accessor
  const range_type& range() const;

  // Number of elements in the tile
  size_type size() const;

  // Initialization check. False if the tile is fully initialized.
  bool empty() const;

  // MADNESS-compliant serialization
  template <typename Archive>
  void serialize(Archive& ar);

  // Scaling operations

  // result[i] = (*this)[i] * factor
  MyTensor scale(const numeric_type factor) const;
  // result[perm ^ i] = (*this)[i] * factor
  MyTensor scale(const numeric_type factor, const TiledArray::Permutation& perm) const;
  // (*this)[i] *= factor
  MyTensor& scale_to(const numeric_type factor) const;

} // class MyTensor

namespace TiledArray {

  // MyTensor is used directly evaluate expressions (see also Lazy Tiles section below)
  struct eval_trait<MyTensor> {
      typedef MyTensor type;
  };

  namespace math {

  // Permutation operation

  // returns a tile for which result[perm ^ i] = tile[i]
  MyTensor permute(const MyTensor& tile,
                   const TiledArray::Permutation& perm);

  // Addition operations

  // result[i] = arg1[i] + arg2[i]
  MyTensor add(const MyTensor& arg1,
               const MyTensor& arg2);
  // result[i] = (arg1[i] + arg2[i]) * factor
  MyTensor add(const MyTensor& arg1,
               const MyTensor& arg2,
               const MyTensor::value_type factor);
  // result[i] = arg[i] + value
  MyTensor add(const MyTensor& arg,
               const MyTensor::value_type& value);

  // result[perm ^ i] = arg1[i] + arg2[i]
  MyTensor add(const MyTensor& arg1,
               const MyTensor& arg2,
               const TiledArray::Permutation& perm);
  // result[perm ^ i] = (arg1[i] + arg2[i]) * factor
  MyTensor add(const MyTensor& arg1,
               const MyTensor& arg2,
               const MyTensor::numeric_type factor,
               const TiledArray::Permutation& perm);
  // result[perm ^ i] = arg[i] + value
  MyTensor add(const MyTensor& arg,
               const MyTensor::value_type& value,
               const TiledArray::Permutation& perm);

  // result[i] += arg[i]
  void add_to(MyTensor& result,
              const MyTensor& arg);
  // (result[i] += arg[i]) *= factor
  void add_to(MyTensor& result,
              const MyTensor& arg,
              const MyTensor::numeric_type factor);
  // result[i] += value
  void add_to(MyTensor& result,
              const MyTensor::value_type& value);

  // Subtraction operations

  // result[i] = arg1[i] - arg2[i]
  MyTensor subt(const MyTensor& arg1,
                const MyTensor& arg2);
  // result[i] = (arg1[i] - arg2[i]) * factor
  MyTensor subt(const MyTensor& arg1,
                const MyTensor& arg2,
                const MyTensor::numeric_type factor);
  // result[i] = arg[i] - value
  MyTensor subt(const MyTensor& arg,
                const MyTensor::value_type& value);

  // result[perm ^ i] = arg1[i] - arg2[i]
  MyTensor subt(const MyTensor& arg1,
                const MyTensor& arg2,
                const TiledArray::Permutation& perm);
  // result[perm ^ i] = (arg1[i] - arg2[i]) * factor
  MyTensor subt(const MyTensor& arg1,
                const MyTensor& arg2,
                const MyTensor::numeric_type factor,
                const TiledArray::Permutation& perm);
  // result[perm ^ i] = arg[i] - value
  MyTensor subt(const MyTensor& arg,
                const MyTensor::value_type value,
                const TiledArray::Permutation& perm);

  // result[i] -= arg[i]
  void subt_to(MyTensor& result,
               const MyTensor& arg);
  // (result[i] -= arg[i]) *= factor
  void subt_to(MyTensor& result,
               const MyTensor& arg,
               const MyTensor::numeric_type factor);
  // result[i] -= value
  void subt_to(MyTensor& result,
               const MyTensor::value_type& value);

  // (Entrywise) multiplication operations (Hadamard product)

  // result[i] = arg1[i] * arg2[i]
  MyTensor mult(const MyTensor& arg1,
                const MyTensor& arg2);
  // result[i] = (arg1[i] * arg2[i]) * factor
  MyTensor mult(const MyTensor& arg1,
                const MyTensor& arg2,
                const MyTensor::numeric_type factor);

  // result[perm ^ i] = arg1[i] * arg2[i]
  MyTensor mult(const MyTensor& arg1,
                const MyTensor& arg2,
                const TiledArray::Permutation& perm);
  // result[perm^ i] = (arg1[i] * arg2[i]) * factor
  MyTensor mult(const MyTensor& arg1,
                const MyTensor& arg2,
                const MyTensor::numeric_type factor,
                const TiledArray::Permutation& perm);

  // result[i] *= arg[i]
  void mult_to(MyTensor& result,
               const MyTensor& arg);
  // (result[i] *= arg[i]) *= factor
  void mult_to(MyTensor& result,
               const MyTensor& arg,
               const MyTensor::numeric_type factor);

  // Negation operations

  // result[i] = -(arg[i])
  MyTensor neg(const MyTensor& arg);
  // result[perm ^ i] = -(arg[i])
  MyTensor neg(const MyTensor& arg,
               const TiledArray::Permutation& perm);
  // result[i] = -(result[i])
  void neg_to(MyTensor& result);

  // Contraction operations

  // GEMM operation with fused indices as defined by gemm_config; multiply arg1 by arg2, return the result
  MyTensor gemm(const MyTensor& arg1,
                const MyTensor& arg2,
                const MyTensor::numeric_type factor,
                const TiledArray::math::GemmHelper& gemm_config);

  // GEMM operation with fused indices as defined by gemm_config; multiply left by right, store to result
  void gemm(MyTensor& result,
            const MyTensor& arg1,
            const MyTensor& arg2,
            const MyTensor::numeric_type factor,
            const TiledArray::math::GemmHelper& gemm_config);


  // Reduction operations

  // Sum of hyper diagonal elements
  MyTensor::numeric_type trace(const MyTensor& arg);
  // foreach(i) result += arg[i]
  MyTensor::numeric_type sum(const MyTensor& arg);
  // foreach(i) result *= arg[i]
  MyTensor::numeric_type product(const MyTensor& arg);
  // foreach(i) result += arg[i] * arg[i]
  MyTensor::numeric_type squared_norm(const MyTensor& arg);
  // sqrt(squared_norm(arg))
  MyTensor::numeric_type norm(const MyTensor& arg);
  // foreach(i) result = max(result, arg[i])
  MyTensor::numeric_type max(const MyTensor& arg);
  // foreach(i) result = min(result, arg[i])
  MyTensor::numeric_type min(const MyTensor& arg);
  // foreach(i) result = max(result, abs(arg[i]))
  MyTensor::numeric_type abs_max(const MyTensor& arg);
  // foreach(i) result = min(result, abs(arg[i]))
  MyTensor::numeric_type abs_min(const MyTensor& arg);
  
```

## User-Defined Lazy Tiles

Lazy tiles are generated only when they are needed and discarded immediately after use. Common uses for lazy tiles include computing arrays on-the-fly, reading them from disk, etc. Lazy tiles are used internally by `TiledArray::DistArray` to generate data tiles that are then fed into arithmetic operations.

The main requirements of lazy tiles are:

1. `typedef ... eval_type`, which is the data tile type (e.g. TiledArray::Tensor).
2. `eval_type` cannot be the same object type as the lazy tile itself.
3. `explicit operator eval_type() const`, which is the function used to generate the data tile.

Lazy tiles should have the following interface.

```
class MyLazyTile {
public:
  typedef ... eval_type; // The data tile to which this tile will be converted to; typically TiledArray::Tensor
                         // Can instead define TiledArray::eval_trait<MyLazyTile>::type

  // Default constructor
  MyLazyTile();

  // Copy constructor
  MyLazyTile(const MyLazyTile& other);

  // Assignment operator
  MyLazyTile& operator=(const MyLazyTile& other);

  // Convert lazy tile to data tile
  explicit operator eval_type() const;

  // MADNESS compliant serialization
  template <typename Archive>
  void serialize(const Archive&);

}; // class MyLazyTile
```

# User Defined Shapes

You can define a shape object for your `Array` object, which defines the sparsity of an array. A shape object is a replicated object, so you should design your shape object accordingly. You may implement an initialization algorithm for your shape that communicates with other processes. However, communication is not allowed after the object has been initialized, shape arithmetic operations must be completely local (non-communicating) operations. 

```
class MyShape {
public:

  // Return true if range matches the range of this shape
  bool validate(const Range& shape);

  // Returns true if the 
  template <typename Index>
  bool is_zero(const Index&);

  /// Returns true if this shape is dense.
  bool is_dense();

  // Permute shape
  MyShape perm(const TiledArray::Permutation& perm);

  // Scale shape
  template <typename Scalar>
  MyShape scale(const Scalar factor);

  // Scale and permute shape
  template <typename Scalar>
  MyShape scale(const Scalar factor, const TiledArray::Permutation& perm);

  // Add shapes
  MyShape add(const MyShape& right);

  // Add shapes and permute the result
  MyShape add(const MyShape& right, const TiledArray::Permutation& perm);

  // Add and scale shapes
  template <typename Scalar>
  MyShape add(const MyShape& right, const Scalar factor);

  // Add and scale shapes, and permute the result
  template <typename Scalar>
  MyShape add(const MyShape& right, const Scalar factor, const TiledArray::Permutation& perm);

  // Add a constant to a shape
  template <typename Scalar>
  MyShape add(const Scalar value)

  // Add a constant to and scale a shape, and permute the result
  template <typename Scalar>
  MyShape add(const Scalar value, const TiledArray::Permutation& perm);

  // Subtract shapes
  MyShape subt(const MyShape& right);

  // Subtract shapes, and permute the result
  MyShape subt(const MyShape& right, const TiledArray::Permutation& perm);

  // Subtract and scale shapes 
  template <typename Scalar>
  MyShape subt(const MyShape& right, const Scalar factor);

  // Subtract and scale shapes, and permute the result
  template <typename Scalar>
  MyShape subt(const MyShape& right, const Scalar factor, const TiledArray::Permutation& perm);

  // Subtract a constant value
  template <typename Scalar>
  MyShape subt(const Scalar value);

  // Subtract a constant value, and permute the result
  template <typename Scalar>
  MyShape subt(const Scalar value, const TiledArray::Permutation& perm);

  // (Entrywise) multiplication of shapes
  MyShape mult(const MyShape& right);

  // (Entrywise) multiplication of shapes, followed by permutation
  MyShape mult(const MyShape& right, const TiledArray::Permutation& perm);

  // (Entrywise) multiplication of shapes, followed by scaling
  template <typename Scalar>
  MyShape mult(const MyShape& right, const Scalar factor);

  // (Entrywise) multiplication of shapes, followed by scaling, followed by permutation
  template <typename Scalar>
  MyShape mult(const MyShape& right, const Scalar factor, const TiledArray::Permutation& perm);

  // Contract and scale shapes
  template <typename Scalar>
  MyShape gemm(const MyShape& right, const Scalar factor,
      const TiledArray::math::GemmHelper& gemm_helper);

  // Contract and scale shapes, and permute the result
  template <typename Scalar>
  MyShape gemm(const MyShape& right, const Scalar factor, 
      const TiledArray::math::GemmHelper& gemm_helper, const TiledArray::Permutation& perm);
}; // class MyShape
```

# User Defined Process Map

You can also create process maps for your `Array` object, which is used by TiledArray to determine the process that owns a tile for a given `Array` object. For a process map to be valid, all tiles are owned by exactly one process and all processes must agree on this tile ownership. The exception to these rules is a replicated process map. In addition, a process map must maintain a list of local tiles.

```
class MyPmap : public TiledArray::Pmap {
protected:

  // Import Pmap protected variables
  using Pmap::rank_;  // The rank of this process
  using Pmap::procs_; // The number of processes
  using Pmap::size_;  // The number of tiles mapped among all processes
  using Pmap::local_; // A list of local tiles (you must initialize this in the constructor)

public:
  typedef Pmap::size_type size_type; // Key type

  // Constructor
  MyPmap(madness::World& world, size_type size);

  // Virtual destructor
  virtual ~MyPmap();

  // Returns the process that owns tile
  virtual size_type owner(const size_type tile) const;

  // Returns true if tile is owned by this process
  virtual bool is_local(const size_type tile) const;
}; // class MyPmap
```