FasTensor  1.0.0
Transform Supercomputing for AI
ft_utility.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 ARRAY_UDF_UTILITY_H
83 #define ARRAY_UDF_UTILITY_H
84 
85 #include <vector>
86 #include <type_traits>
87 #include <cstring>
88 #include <cmath>
89 #include <sys/types.h>
90 #include <sys/stat.h>
91 #include <unistd.h>
92 #include <tuple>
93 #include <string>
94 #include <sstream>
95 #include <algorithm>
96 #include <iterator>
97 #include <iostream>
98 #include <utility>
99 #include <variant>
100 #include <dirent.h>
101 
102 #include "ft_type.h"
103 #include "ft_utility_macro.h"
104 
105 #if __cplusplus > 201402L
106 #include "cista.h"
107 #endif
108 
109 #define V2VOIDP(vv) static_cast<void *>(vv.data())
110 
111 extern int ft_rank;
112 
120 std::string realpathEx(std::string path);
121 
128 std::vector<std::string> GetDirFileList(std::string dir_str_p);
129 
138 int ExtractEndpointTypeInfo(std::string endpoint_type_info, AuEndpointType &endpoint_type, std::string &endpoint_info);
139 
146 int file_exist(const char *filename);
147 
154 std::string ExtractFileName(const std::string &fullPath);
155 
162 std::string ExtractPath(const std::string &fullPath);
163 
164 template <typename T>
165 inline void PrintVector(std::string name, std::vector<T> v)
166 {
167  int n = v.size();
168  if (name != "")
169  std::cout << "Rank " << ft_rank << ", " << name << ": ";
170 
171  if (!n)
172  {
173  std::cout << std::endl;
174  return;
175  }
176 
177  if (n > 8)
178  {
179  for (int i = 0; i < 4; i++)
180  {
181  std::cout << v[i] << ",";
182  }
183  std::cout << " ... ";
184  for (int i = n - 4; i < n - 1; i++)
185  {
186  std::cout << v[i] << ",";
187  }
188  std::cout << v[n - 1];
189  }
190  else
191  {
192  for (int i = 0; i < n - 1; i++)
193  {
194  std::cout << v[i] << ",";
195  }
196  std::cout << v[n - 1];
197  }
198  std::cout << std::endl;
199 }
200 
201 template <typename T>
202 inline void PrintVV(std::string name, std::vector<std::vector<T>> v)
203 {
204  int n = v.size();
205  if (name != "")
206  std::cout << "Rank " << ft_rank << ", " << name << ": ";
207 
208  if (!n)
209  {
210  std::cout << std::endl;
211  return;
212  }
213 
214  if (n > 8)
215  {
216  for (int i = 0; i < 4; i++)
217  {
218  PrintVector("", v[i]);
219  }
220  std::cout << " ... \n";
221  for (int i = n - 4; i < n; i++)
222  {
223  PrintVector("", v[i]);
224  }
225  }
226  else
227  {
228  for (int i = 0; i < n - 1; i++)
229  {
230  PrintVector("", v[i]);
231  }
232  }
233  std::cout << std::endl;
234 }
235 
236 template <typename T>
237 inline void PrintScalar(std::string name, T v)
238 {
239  if (name != "")
240  std::cout << "Rank " << ft_rank << ", " << name << ": "
241  << ": " << v << std::endl;
242 }
243 
244 template <typename T>
245 inline void PrintString(std::string name)
246 {
247  if (name != "")
248  std::cout << "Rank " << ft_rank << ", " << name << std::endl;
249 }
250 
258 inline unsigned long long RowMajorOrder(std::vector<unsigned long long> dsize, std::vector<unsigned long long> coordinate)
259 {
260  unsigned long long offset = coordinate[0];
261  int n = dsize.size();
262  for (int i = 1; i < n; i++)
263  {
264  offset = offset * dsize[i] + coordinate[i];
265  }
266  return offset;
267 }
268 
276 inline std::vector<unsigned long long> RowMajorOrderReverse(unsigned long long offset, std::vector<unsigned long long> dsize)
277 {
278  int n = dsize.size();
279  std::vector<unsigned long long> original_coordinate;
280  original_coordinate.resize(n);
281  //unsigned long long reminder;
282  for (unsigned long long i = n - 1; i >= 1; i--)
283  {
284  original_coordinate[i] = offset % dsize[i];
285  offset = offset / dsize[i];
286  }
287  //Last dimenstion
288  original_coordinate[0] = offset;
289 
290  return original_coordinate;
291 }
292 
302 template <class T1, class T2>
303 inline void InsertAttribute2VirtualArrayVector(const std::vector<T1> &attribute_vector, AuEndpointDataType union_index, std::vector<T2> &virtual_array_vector, int attribute_index)
304 {
305  assert(attribute_vector.size() == virtual_array_vector.size());
306  size_t n = attribute_vector.size();
307  for (size_t i = 0; i < n; i++)
308  {
309  T1 attribute_vector_value = attribute_vector[i];
310  int m_index = 0;
311  cista::for_each_field(virtual_array_vector[i], [&m_index, attribute_index, attribute_vector_value, union_index](auto &&m) {
312  if (m_index == attribute_index)
313  {
314  switch (union_index)
315  {
316  case AU_SHORT:
317  {
318  m = std::get<AU_SHORT>(attribute_vector_value);
319  break;
320  }
321  case AU_INT:
322  {
323  m = std::get<AU_INT>(attribute_vector_value);
324  break;
325  }
326  case AU_LONG:
327  {
328  m = std::get<AU_LONG>(attribute_vector_value);
329  break;
330  }
331  case AU_LONG_LONG:
332  {
333  m = std::get<AU_LONG_LONG>(attribute_vector_value);
334  break;
335  }
336  case AU_USHORT:
337  {
338  m = std::get<AU_USHORT>(attribute_vector_value);
339  break;
340  }
341  case AU_UINT:
342  {
343  m = std::get<AU_UINT>(attribute_vector_value);
344  break;
345  }
346  case AU_ULONG:
347  {
348  m = std::get<AU_ULONG>(attribute_vector_value);
349  break;
350  }
351  case AU_ULLONG:
352  {
353  m = std::get<AU_ULLONG>(attribute_vector_value);
354  break;
355  }
356  case AU_FLOAT:
357  {
358  m = std::get<AU_FLOAT>(attribute_vector_value);
359  break;
360  }
361  case AU_DOUBLE:
362  {
363  m = std::get<AU_DOUBLE>(attribute_vector_value);
364  break;
365  }
366  default:
367  std::cout << "Unsupported datatype in " << __FILE__ << " : " << __LINE__ << std::endl;
368  std::flush(std::cout);
369  std::exit(EXIT_FAILURE);
370  }
371  return;
372  }
373  m_index++;
374  });
375  }
376 }
377 
378 template <>
379 inline void InsertAttribute2VirtualArrayVector<AuEndpointDataTypeUnion, std::complex<double>>(const std::vector<AuEndpointDataTypeUnion> &attribute_vector, AuEndpointDataType union_index, std::vector<std::complex<double>> &virtual_array_vector, int attribute_index)
380 {
381  AU_EXIT("std::complex does not work with cista::for_each_field now");
382 }
383 
384 #define ExtractAttributeFromVirtualArrayVector_HELPER(ELEMENT_TYPE) \
385  { \
386  ELEMENT_TYPE *attribute_data_typed = (ELEMENT_TYPE *)attribute_data_void_pointer; \
387  for (size_t i = 0; i < n; i++) \
388  { \
389  int m_index = 0; \
390  ELEMENT_TYPE temp_attribute_value; \
391  cista::for_each_field(virtual_array_vector[i], [&m_index, attribute_index, &temp_attribute_value](auto &&m) { \
392  if (m_index == attribute_index) \
393  { \
394  temp_attribute_value = m; \
395  } \
396  m_index++; \
397  }); \
398  attribute_data_typed[i] = temp_attribute_value; \
399  } \
400  }
401 
402 template <class T2>
403 inline void *ExtractAttributeFromVirtualArrayVector(std::vector<T2> &virtual_array_vector, int attribute_index, AuEndpointDataType element_type, int element_type_size)
404 {
405 
406  size_t n = virtual_array_vector.size();
407  void *attribute_data_void_pointer = malloc(n * element_type_size);
408 
409  switch (element_type)
410  {
411  case AU_SHORT:
412  {
414  break;
415  }
416  case AU_INT:
417  {
419  break;
420  }
421  case AU_LONG:
422  {
424  break;
425  }
426  case AU_LONG_LONG:
427  {
429  break;
430  }
431  case AU_USHORT:
432  {
434  break;
435  }
436  case AU_UINT:
437  {
439  break;
440  }
441  case AU_ULONG:
442  {
444  break;
445  }
446  case AU_ULLONG:
447  {
449  break;
450  }
451  case AU_FLOAT:
452  {
454  break;
455  }
456  case AU_DOUBLE:
457  {
459  break;
460  }
461  default:
462  std::cout << "Unsupported datatype in " << __FILE__ << " : " << __LINE__ << std::endl;
463  std::flush(std::cout);
464  std::exit(EXIT_FAILURE);
465  }
466  return attribute_data_void_pointer;
467 }
468 
469 template <>
470 inline void *ExtractAttributeFromVirtualArrayVector<std::complex<double>>(std::vector<std::complex<double>> &virtual_array_vector, int attribute_index, AuEndpointDataType element_type, int element_type_size)
471 {
472  AU_EXIT("std::complex<double> does work with cista::to_tuple<");
473 }
474 
475 #include <vector>
476 #include <iterator>
477 #include <iostream>
478 #include <string>
479 #include <sstream>
480 #include <iomanip>
481 template <typename T>
482 std::string Vector2String(const std::vector<T> &vec)
483 {
484  std::ostringstream vts;
485  vts << std::setprecision(17); //force high prevision for float
486  if (!vec.empty())
487  {
488  // Convert all but the last element to avoid a trailing ","
489  std::copy(vec.begin(), std::prev(vec.end(), 1),
490  std::ostream_iterator<T>(vts, ","));
491 
492  // Now add the last element with no delimiter
493  vts << vec.back();
494  }
495  return vts.str();
496 }
497 
498 template <typename T>
499 void String2Vector(const std::string &str, std::vector<T> &vec_new)
500 {
501  std::stringstream ss(str);
502  vec_new.clear();
503  for (T i; ss >> i;)
504  {
505  vec_new.push_back(i);
506  if (ss.peek() == ',')
507  ss.ignore();
508  }
509 }
510 
511 #endif
AuEndpointType
Definition: ft_type.h:95
AuEndpointDataType
Definition: ft_type.h:118
@ AU_LONG
Definition: ft_type.h:122
@ AU_ULLONG
Definition: ft_type.h:127
@ AU_DOUBLE
Definition: ft_type.h:129
@ AU_FLOAT
Definition: ft_type.h:128
@ AU_INT
Definition: ft_type.h:121
@ AU_ULONG
Definition: ft_type.h:126
@ AU_USHORT
Definition: ft_type.h:124
@ AU_LONG_LONG
Definition: ft_type.h:123
@ AU_SHORT
Definition: ft_type.h:120
@ AU_UINT
Definition: ft_type.h:125
std::string ExtractPath(const std::string &fullPath)
Definition: ft_utility.cpp:127
void PrintVV(std::string name, std::vector< std::vector< T >> v)
Definition: ft_utility.h:202
int ft_rank
Definition: ft.cpp:86
void * ExtractAttributeFromVirtualArrayVector(std::vector< T2 > &virtual_array_vector, int attribute_index, AuEndpointDataType element_type, int element_type_size)
Definition: ft_utility.h:403
std::string Vector2String(const std::vector< T > &vec)
Definition: ft_utility.h:482
void PrintVector(std::string name, std::vector< T > v)
Definition: ft_utility.h:165
std::vector< unsigned long long > RowMajorOrderReverse(unsigned long long offset, std::vector< unsigned long long > dsize)
convert linearized coordinate to multidimensional one
Definition: ft_utility.h:276
unsigned long long RowMajorOrder(std::vector< unsigned long long > dsize, std::vector< unsigned long long > coordinate)
convert coordinate to linearized one
Definition: ft_utility.h:258
std::vector< std::string > GetDirFileList(std::string dir_str_p)
Get file list of a direction.
Definition: ft_utility.cpp:133
std::string ExtractFileName(const std::string &fullPath)
Definition: ft_utility.cpp:121
void InsertAttribute2VirtualArrayVector(const std::vector< T1 > &attribute_vector, AuEndpointDataType union_index, std::vector< T2 > &virtual_array_vector, int attribute_index)
Definition: ft_utility.h:303
int ExtractEndpointTypeInfo(std::string endpoint_type_info, AuEndpointType &endpoint_type, std::string &endpoint_info)
Split endpoint_type_info string to type and information.
Definition: ft_utility.cpp:100
std::string realpathEx(std::string path)
expand the path to full directory https://www.dreamincode.net/forums/topic/218601-realpath-and-tilde/
Definition: ft_utility.cpp:83
void PrintString(std::string name)
Definition: ft_utility.h:245
#define ExtractAttributeFromVirtualArrayVector_HELPER(ELEMENT_TYPE)
Definition: ft_utility.h:384
void String2Vector(const std::string &str, std::vector< T > &vec_new)
Definition: ft_utility.h:499
void PrintScalar(std::string name, T v)
Definition: ft_utility.h:237
int file_exist(const char *filename)
Check wether the file exists.
Definition: ft_utility.cpp:115
#define AU_EXIT(info)
Definition: ft_utility_macro.h:147