Cyclops Tensor Framework
parallel arithmetic on multidimensional arrays
distribution.cxx
Go to the documentation of this file.
1 /*Copyright (c) 2011, Edgar Solomonik, all rights reserved.*/
2 
3 #include "../tensor/untyped_tensor.h"
4 #include "../shared/util.h"
5 
6 namespace CTF_int {
7 
9  order = -1;
10  /*phase = NULL;
11  virt_phase = NULL;
12  pe_lda = NULL;
13  pad_edge_len = NULL;
14  padding = NULL;
15  perank = NULL;
16  order = -1;
17  size = -1;*/
18  }
19 
21  order = tsr->order;
22  CTF_int::alloc_ptr(sizeof(int)*order, (void**)&phase);
23  CTF_int::alloc_ptr(sizeof(int)*order, (void**)&virt_phase);
24  CTF_int::alloc_ptr(sizeof(int)*order, (void**)&phys_phase);
25  CTF_int::alloc_ptr(sizeof(int)*order, (void**)&pe_lda);
26  CTF_int::alloc_ptr(sizeof(int)*order, (void**)&pad_edge_len);
27  CTF_int::alloc_ptr(sizeof(int)*order, (void**)&padding);
28  CTF_int::alloc_ptr(sizeof(int)*order, (void**)&perank);
29 
30  size = tsr->size;
31 
32  for (int j=0; j<tsr->order; j++){
33  mapping * map = tsr->edge_map + j;
34  phase[j] = map->calc_phase();
35  perank[j] = map->calc_phys_rank(tsr->topo);
36  ASSERT(perank[j] <= phase[j]);
37  phys_phase[j] = map->calc_phys_phase();
38  virt_phase[j] = phase[j]/phys_phase[j];
39  if (map->type == PHYSICAL_MAP)
40  pe_lda[j] = tsr->topo->lda[map->cdt];
41  else
42  pe_lda[j] = 0;
43  }
44  memcpy(pad_edge_len, tsr->pad_edge_len, sizeof(int)*tsr->order);
45  memcpy(padding, tsr->padding, sizeof(int)*tsr->order);
46  is_cyclic = tsr->is_cyclic;
47  }
48 
49  distribution::distribution(char const * buffer){
50  int buffer_ptr = 0;
51 
52  order = ((int*)(buffer+buffer_ptr))[0];
53  buffer_ptr += sizeof(int);
54 
55  CTF_int::alloc_ptr(sizeof(int)*order, (void**)&phase);
56  CTF_int::alloc_ptr(sizeof(int)*order, (void**)&virt_phase);
57  CTF_int::alloc_ptr(sizeof(int)*order, (void**)&phys_phase);
58  CTF_int::alloc_ptr(sizeof(int)*order, (void**)&pe_lda);
59  CTF_int::alloc_ptr(sizeof(int)*order, (void**)&pad_edge_len);
60  CTF_int::alloc_ptr(sizeof(int)*order, (void**)&padding);
61  CTF_int::alloc_ptr(sizeof(int)*order, (void**)&perank);
62 
63  is_cyclic = ((int*)(buffer+buffer_ptr))[0];
64  buffer_ptr += sizeof(int);
65  size = ((int64_t*)(buffer+buffer_ptr))[0];
66  buffer_ptr += sizeof(int64_t);
67  memcpy(phase, (int*)(buffer+buffer_ptr), sizeof(int)*order);
68  buffer_ptr += sizeof(int)*order;
69  memcpy(virt_phase, (int*)(buffer+buffer_ptr), sizeof(int)*order);
70  buffer_ptr += sizeof(int)*order;
71  memcpy(phys_phase, (int*)(buffer+buffer_ptr), sizeof(int)*order);
72  buffer_ptr += sizeof(int)*order;
73  memcpy(pe_lda, (int*)(buffer+buffer_ptr), sizeof(int)*order);
74  buffer_ptr += sizeof(int)*order;
75  memcpy(pad_edge_len, (int*)(buffer+buffer_ptr), sizeof(int)*order);
76  buffer_ptr += sizeof(int)*order;
77  memcpy(padding, (int*)(buffer+buffer_ptr), sizeof(int)*order);
78  buffer_ptr += sizeof(int)*order;
79  memcpy(perank, (int*)(buffer+buffer_ptr), sizeof(int)*order);
80  buffer_ptr += sizeof(int)*order;
81 
82  ASSERT(buffer_ptr == get_distribution_size(order));
83  }
85  free_data();
86  }
87 
88  void distribution::serialize(char ** buffer_, int * bufsz_){
89 
90  ASSERT(order != -1);
91 
92  int bufsz;
93  char * buffer;
94 
96 
97  CTF_int::alloc_ptr(bufsz, (void**)&buffer);
98 
99  int buffer_ptr = 0;
100 
101  ((int*)(buffer+buffer_ptr))[0] = order;
102  buffer_ptr += sizeof(int);
103  ((int*)(buffer+buffer_ptr))[0] = is_cyclic;
104  buffer_ptr += sizeof(int);
105  ((int64_t*)(buffer+buffer_ptr))[0] = size;
106  buffer_ptr += sizeof(int64_t);
107  memcpy((int*)(buffer+buffer_ptr), phase, sizeof(int)*order);
108  buffer_ptr += sizeof(int)*order;
109  memcpy((int*)(buffer+buffer_ptr), virt_phase, sizeof(int)*order);
110  buffer_ptr += sizeof(int)*order;
111  memcpy((int*)(buffer+buffer_ptr), phys_phase, sizeof(int)*order);
112  buffer_ptr += sizeof(int)*order;
113  memcpy((int*)(buffer+buffer_ptr), pe_lda, sizeof(int)*order);
114  buffer_ptr += sizeof(int)*order;
115  memcpy((int*)(buffer+buffer_ptr), pad_edge_len, sizeof(int)*order);
116  buffer_ptr += sizeof(int)*order;
117  memcpy((int*)(buffer+buffer_ptr), padding, sizeof(int)*order);
118  buffer_ptr += sizeof(int)*order;
119  memcpy((int*)(buffer+buffer_ptr), perank, sizeof(int)*order);
120  buffer_ptr += sizeof(int)*order;
121 
122  ASSERT(buffer_ptr == bufsz);
123 
124  *buffer_ = buffer;
125  *bufsz_ = bufsz;
126 
127  }
128 
129  void distribution::free_data(){
130  if (order != -1){
138  }
139  order = -1;
140  }
141 
142 
143  void calc_dim(int order,
144  int64_t size,
145  int const * edge_len,
146  mapping const * edge_map,
147  int64_t * vrt_sz,
148  int * vrt_edge_len,
149  int * blk_edge_len){
150  int64_t vsz, i, cont;
151  mapping const * map;
152  vsz = size;
153 
154  for (i=0; i<order; i++){
155  if (blk_edge_len != NULL)
156  blk_edge_len[i] = edge_len[i];
157  vrt_edge_len[i] = edge_len[i];
158  map = &edge_map[i];
159  do {
160  if (blk_edge_len != NULL){
161  if (map->type == PHYSICAL_MAP)
162  blk_edge_len[i] = blk_edge_len[i] / map->np;
163  }
164  vrt_edge_len[i] = vrt_edge_len[i] / map->np;
165  if (vrt_sz != NULL){
166  if (map->type == VIRTUAL_MAP)
167  vsz = vsz / map->np;
168  }
169  if (map->has_child){
170  cont = 1;
171  map = map->child;
172  } else
173  cont = 0;
174  } while (cont);
175  }
176  if (vrt_sz != NULL)
177  *vrt_sz = vsz;
178  }
179 
180 }
int calc_phys_rank(topology const *topo) const
compute the physical rank of a mapping
Definition: mapping.cxx:74
map_type type
Definition: mapping.h:22
int calc_phase() const
compute the phase of a mapping
Definition: mapping.cxx:39
void calc_dim(int order, int64_t size, int const *edge_len, mapping const *edge_map, int64_t *vrt_sz, int *vrt_edge_len, int *blk_edge_len)
calculate the block-sizes of a tensor
int * pad_edge_len
padded tensor edge lengths
void serialize(char **buffer, int *size)
serialize object into contiguous data buffer
int calc_phys_phase() const
compute the physical phase of a mapping
Definition: mapping.cxx:57
int64_t size
current size of local tensor data chunk (mapping-dependent)
#define ASSERT(...)
Definition: util.h:88
int order
number of tensor dimensions
bool is_cyclic
whether the tensor data is cyclically distributed (blocked if false)
int * padding
padding along each edge length (less than distribution phase)
int alloc_ptr(int64_t len, void **const ptr)
alloc abstraction
Definition: memcontrol.cxx:320
mapping * edge_map
mappings of each tensor dimension onto topology dimensions
mapping * child
Definition: mapping.h:26
int get_distribution_size(int order)
Definition: distribution.h:13
int cdealloc(void *ptr)
free abstraction
Definition: memcontrol.cxx:480
internal distributed tensor class
topology * topo
topology to which the tensor is mapped