FasTensor  1.0.0
Transform Supercomputing for AI
ft_local_mirror.h
Go to the documentation of this file.
1 
2 
3 /*
4 ****************************
5 
6 FasTensor (FT) Copyright (c) 2021, The Regents of the University of
7 California, through Lawrence Berkeley National Laboratory (subject to
8 receipt of any required approvals from the U.S. Dept. of Energy).
9 All rights reserved.
10 
11 If you have questions about your rights to use or distribute this software,
12 please contact Berkeley Lab's Intellectual Property Office at
14 
15 NOTICE. This Software was developed under funding from the U.S. Department
16 of Energy and the U.S. Government consequently retains certain rights. As
17 such, the U.S. Government has been granted for itself and others acting on
18 its behalf a paid-up, nonexclusive, irrevocable, worldwide license in the
19 Software to reproduce, distribute copies to the public, prepare derivative
20 works, and perform publicly and display publicly, and to permit others to do so.
21 
22 
23 ****************************
24 
25 
26 *** License Agreement ***
27 
28 FasTensor (FT) Copyright (c) 2021, The Regents of the University of
29 California, through Lawrence Berkeley National Laboratory (subject to
30 receipt of any required approvals from the U.S. Dept. of Energy).
31 All rights reserved.
32 
33 Redistribution and use in source and binary forms, with or without
34 modification, are permitted provided that the following conditions are met:
35 
36 (1) Redistributions of source code must retain the above copyright notice,
37 this list of conditions and the following disclaimer.
38 
39 (2) Redistributions in binary form must reproduce the above copyright
40 notice, this list of conditions and the following disclaimer in the
41 documentation and/or other materials provided with the distribution.
42 
43 (3) Neither the name of the University of California, Lawrence Berkeley
44 National Laboratory, U.S. Dept. of Energy nor the names of its contributors
45 may be used to endorse or promote products derived from this software
46 without specific prior written permission.
47 
48 
49 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
50 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
53 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
54 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
55 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
56 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
57 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59 POSSIBILITY OF SUCH DAMAGE.
60 
61 You are under no obligation whatsoever to provide any bug fixes, patches,
62 or upgrades to the features, functionality or performance of the source
63 code ("Enhancements") to anyone; however, if you choose to make your
64 Enhancements available either publicly, or directly to Lawrence Berkeley
65 National Laboratory, without imposing a separate written license agreement
66 for such Enhancements, then you hereby grant the following license: a
67 non-exclusive, royalty-free perpetual license to install, use, modify,
68 prepare derivative works, incorporate into other computer software,
69 distribute, and sublicense such enhancements or derivative works thereof,
70 in binary and source code form.
71 */
72 
82 #ifndef AU_LOCAL_MIRROR_H
83 #define AU_LOCAL_MIRROR_H
84 
85 #define LOCAL_MIRROR_READ_FLAG 0
86 #define LOCAL_MIRROR_WRITE_FLAG 1
87 
88 #include <iostream> // std::cout
89 #include <sstream> // std::istringstream
90 #include <string> // std::string
91 
92 #include "ft_utility.h"
93 #include "ft_mpi.h"
94 
95 //see au.h for its definations
96 extern int ft_size;
97 extern int ft_rank;
98 
99 template <typename T>
100 void *CreateLocalMirrorHelp(std::string init_value_str, size_t local_mirror_size)
101 {
102  T *local_mirror_buffer_typed = (T *)malloc(local_mirror_size * sizeof(T));
103  if (local_mirror_buffer_typed == NULL)
104  {
105  AU_EXIT("Not enough memory to crate local mirror.");
106  return NULL;
107  }
108 
109  //Set it to zeror by default
110  std::memset(local_mirror_buffer_typed, 0, local_mirror_size * sizeof(T));
111 
112  if (init_value_str != "")
113  {
114  std::istringstream reader(init_value_str);
115  T init_val;
116  reader >> init_val;
117  for (int i = 0; i < local_mirror_size; i++)
118  {
119  local_mirror_buffer_typed[i] = init_val;
120  }
121  }
122  return (void *)local_mirror_buffer_typed;
123 }
124 
125 template <typename T>
126 int AccessLocalMirrorHelp(void *local_mirror_buffer, std::vector<unsigned long long> &mirror_size,
127  std::vector<unsigned long long> &start, std::vector<unsigned long long> &end,
128  void *data, int read_write_code)
129 {
130  T *local_mirror_buffer_typed = (T *)local_mirror_buffer;
131  T *data_typed = (T *)data;
132 
133  std::vector<unsigned long long> read_count;
134  unsigned long long element_count = 1;
135  read_count.resize(mirror_size.size());
136  for (int i = 0; i < mirror_size.size(); i++)
137  {
138  read_count[i] = end[i] - start[i] + 1;
139  element_count = element_count * read_count[i];
140  }
141 
142  if (read_count == mirror_size)
143  {
144  //std::cout << "Access local whole ! \n";
145  if (read_write_code == LOCAL_MIRROR_READ_FLAG)
146  {
147  std::memcpy(data, local_mirror_buffer, element_count * sizeof(T));
148  }
149  else
150  {
151  std::memcpy(local_mirror_buffer, data, element_count * sizeof(T));
152  }
153  return 0;
154  }
155  // else
156  // {
157  // PrintVector("read_count: ", read_count);
158  // PrintVector("mirror_size: ", mirror_size);
159  // //std::cout << "Not Access local whole ! \n";
160  // }
161 
162  switch (mirror_size.size())
163  {
164  case 1:
165  {
166  unsigned long long data_typed_offset = 0;
167  for (unsigned long long i = start[0]; i <= end[0]; i++)
168  {
169  if (read_write_code == LOCAL_MIRROR_READ_FLAG)
170  {
171  data_typed[data_typed_offset] = local_mirror_buffer_typed[i];
172  }
173  else
174  {
175  local_mirror_buffer_typed[i] = data_typed[data_typed_offset];
176  }
177  data_typed_offset++;
178  }
179  break;
180  }
181  case 2:
182  {
183  unsigned long long offset;
184  unsigned long long data_typed_offset = 0;
185  std::vector<unsigned long long> coodinate_temp(2);
186  for (unsigned long long i = start[0]; i <= end[0]; i++)
187  {
188  for (unsigned long long j = start[1]; j <= end[1]; j++)
189  {
190  coodinate_temp[0] = i;
191  coodinate_temp[1] = j;
192  ROW_MAJOR_ORDER_MACRO(mirror_size, mirror_size.size(), coodinate_temp, offset);
193  //std::cout << "coodinate_temp =" << i << ", " << j << ", offset = " << offset << ", data_typed[offset] =" << data_typed[offset] << ", buffer_typed[offset]=" << local_mirror_buffer_typed[offset] << std::endl;
194  if (read_write_code == LOCAL_MIRROR_READ_FLAG)
195  {
196  data_typed[data_typed_offset] = local_mirror_buffer_typed[offset];
197  }
198  else
199  {
200  local_mirror_buffer_typed[offset] = data_typed[data_typed_offset];
201  }
202  data_typed_offset++;
203  }
204  }
205  break;
206  }
207  break;
208  case 3:
209  {
210  unsigned long long offset;
211  unsigned long long data_typed_offset = 0;
212  std::vector<unsigned long long> coodinate_temp(3);
213  for (unsigned long long i = start[0]; i <= end[0]; i++)
214  {
215  for (unsigned long long j = start[1]; j <= end[1]; j++)
216  {
217  for (unsigned long long k = start[2]; k <= end[2]; k++)
218  {
219  coodinate_temp[0] = i;
220  coodinate_temp[1] = j;
221  coodinate_temp[2] = k;
222  ROW_MAJOR_ORDER_MACRO(mirror_size, mirror_size.size(), coodinate_temp, offset);
223  if (read_write_code == LOCAL_MIRROR_READ_FLAG)
224  {
225  data_typed[data_typed_offset] = local_mirror_buffer_typed[offset];
226  }
227  else
228  {
229  local_mirror_buffer_typed[offset] = data_typed[data_typed_offset];
230  }
231  data_typed_offset++;
232  }
233  }
234  }
235  break;
236  }
237  default:
238  break;
239  }
240 
241  return 0;
242 }
243 
244 template <typename T>
245 T *MergeMirrorsHelp(void *local_mirror_buffer, unsigned long long &local_mirror_size, std::string &opt_str)
246 {
247  T *reduced_mirror_buffer = NULL;
248  if (!ft_rank)
249  {
250  reduced_mirror_buffer = (T *)malloc(local_mirror_size * sizeof(T));
251  }
252  AU_MPI_Datatype merge_data_type = InferMPIType<T>();
253  AU_MPI_Op merge_op = InferMPIMergeOp(opt_str);
254 
255  AU_Reduce(local_mirror_buffer, reduced_mirror_buffer, local_mirror_size, merge_data_type, merge_op, 0, MPI_COMM_WORLD_DEFAULT);
256 
257  if (!ft_rank)
258  {
259  std::memcpy(local_mirror_buffer, reduced_mirror_buffer, local_mirror_size * sizeof(T));
260  }
261 
262  return reduced_mirror_buffer;
263 }
264 
265 template <typename T>
266 void BcastHelp(void *local_mirror_buffer, unsigned long long &local_mirror_size)
267 {
268  AU_MPI_Datatype data_type = InferMPIType<T>();
269 
270  AU_Bcast(local_mirror_buffer, local_mirror_size, data_type, 0, MPI_COMM_WORLD_DEFAULT);
271 }
272 
273 #endif
void * CreateLocalMirrorHelp(std::string init_value_str, size_t local_mirror_size)
Definition: ft_local_mirror.h:100
int ft_rank
Definition: ft.cpp:86
void BcastHelp(void *local_mirror_buffer, unsigned long long &local_mirror_size)
Definition: ft_local_mirror.h:266
T * MergeMirrorsHelp(void *local_mirror_buffer, unsigned long long &local_mirror_size, std::string &opt_str)
Definition: ft_local_mirror.h:245
int AccessLocalMirrorHelp(void *local_mirror_buffer, std::vector< unsigned long long > &mirror_size, std::vector< unsigned long long > &start, std::vector< unsigned long long > &end, void *data, int read_write_code)
Definition: ft_local_mirror.h:126
int ft_size
Definition: ft.cpp:85
#define LOCAL_MIRROR_READ_FLAG
Definition: ft_local_mirror.h:85
#define AU_MPI_Datatype
Definition: ft_mpi.h:96
MPI_Op InferMPIMergeOp(std::string &opt_str)
Definition: ft_mpi.h:175
#define AU_MPI_Op
Definition: ft_mpi.h:97
#define MPI_COMM_WORLD_DEFAULT
Definition: ft_mpi.h:94
#define AU_Bcast(data_bffer_p, count_p, datatype_p, root_p, comm_p)
Definition: ft_mpi.h:234
#define AU_Reduce(local_buffer_p, reduced_buffer_p, size, type, op, root, comm)
Definition: ft_mpi.h:230
#define ROW_MAJOR_ORDER_MACRO(dsize, dsize_len, coordinate, offset)
macro version of above two functions for speed
Definition: ft_utility_macro.h:124
#define AU_EXIT(info)
Definition: ft_utility_macro.h:147