Cyclops Tensor Framework
parallel arithmetic on multidimensional arrays
set.h
Go to the documentation of this file.
1 #ifndef __SET_H__
2 #define __SET_H__
3 
4 #include "../tensor/algstrct.h"
5 //#include <stdint.h>
6 #include <limits>
7 #include <inttypes.h>
8 
9 namespace CTF_int {
10 
11  //does conversion using MKL function if it is available
12  bool try_mkl_coo_to_csr(int64_t nz, int nrow, char * csr_vs, int * csr_ja, int * csr_ia, char const * coo_vs, int const * coo_rs, int const * coo_cs, int el_size);
13 
14  bool try_mkl_csr_to_coo(int64_t nz, int nrow, char const * csr_vs, int const * csr_ja, int const * csr_ia, char * coo_vs, int * coo_rs, int * coo_cs, int el_size);
15 
16  template <typename dtype>
17  void seq_coo_to_csr(int64_t nz, int nrow, dtype * csr_vs, int * csr_ja, int * csr_ia, dtype const * coo_vs, int const * coo_rs, int const * coo_cs){
18  int sz = sizeof(dtype);
19  if (sz == 4 || sz == 8 || sz == 16){
20  bool b = try_mkl_coo_to_csr(nz, nrow, (char*)csr_vs, csr_ja, csr_ia, (char const*)coo_vs, coo_rs, coo_cs, sz);
21  if (b) return;
22  }
23  csr_ia[0] = 1;
24 #ifdef _OPENMP
25  #pragma omp parallel for
26 #endif
27  for (int i=1; i<nrow+1; i++){
28  csr_ia[i] = 0;
29  }
30 #ifdef _OPENMP
31  #pragma omp parallel for
32 #endif
33  for (int64_t i=0; i<nz; i++){
34  csr_ia[coo_rs[i]]++;
35  }
36 #ifdef _OPENMP
37  #pragma omp parallel for
38 #endif
39  for (int i=0; i<nrow; i++){
40  csr_ia[i+1] += csr_ia[i];
41  }
42 #ifdef _OPENMP
43  #pragma omp parallel for
44 #endif
45  for (int64_t i=0; i<nz; i++){
46  csr_ja[i] = i;
47  }
48 
49  class comp_ref {
50  public:
51  int const * a;
52  comp_ref(int const * a_){ a = a_; }
53  bool operator()(int u, int v){
54  return a[u] < a[v];
55  }
56  };
57 
58  comp_ref crc(coo_cs);
59  std::sort(csr_ja, csr_ja+nz, crc);
60  comp_ref crr(coo_rs);
61  std::stable_sort(csr_ja, csr_ja+nz, crr);
62  // do not copy by value in case values are objects, then csr_vs is uninitialized
63  //printf("csr nz = %ld\n",nz);
64 #ifdef _OPENMP
65  #pragma omp parallel for
66 #endif
67  for (int64_t i=0; i<nz; i++){
68  //printf("%d, %d, %ld\n",(int)((char*)(coo_vs+csr_ja[i])-(char*)(coo_vs))-csr_ja[i]*sizeof(dtype),sizeof(dtype),csr_ja[i]);
69 // memcpy(csr_vs+i, coo_vs+csr_ja[i]-1,sizeof(dtype));
70  //memcpy(csr_vs+i, coo_vs+csr_ja[i],sizeof(dtype));
71  csr_vs[i] = coo_vs[csr_ja[i]];
72 // printf("i %ld csr_ja[i] %d\n", i, csr_ja[i]);
73 // printf("i %ld v %lf\n", i, csr_vs[i]);
74  //printf("%p %d\n",coo_vs+i,*(int32_t*)(coo_vs+i));
75  }
76 #ifdef _OPENMP
77  #pragma omp parallel for
78 #endif
79  for (int64_t i=0; i<nz; i++){
80  csr_ja[i] = coo_cs[csr_ja[i]];
81  }
82 
83  }
84  template <typename dtype>
85  void seq_csr_to_coo(int64_t nz, int nrow, dtype const * csr_vs, int const * csr_ja, int const * csr_ia, dtype * coo_vs, int * coo_rs, int * coo_cs){
86  int sz = sizeof(dtype);
87  if (sz == 4 || sz == 8 || sz == 16){
88  bool b = try_mkl_csr_to_coo(nz, nrow, (char const*)csr_vs, csr_ja, csr_ia, (char*)coo_vs, coo_rs, coo_cs, sz);
89  if (b) return;
90  }
91  //memcpy(coo_vs, csr_vs, sizeof(dtype)*nz);
92  std::copy(csr_vs, csr_vs+nz, coo_vs);
93  memcpy(coo_cs, csr_ja, sizeof(int)*nz);
94  for (int i=0; i<nrow; i++){
95  std::fill(coo_rs+csr_ia[i]-1, coo_rs+csr_ia[i+1]-1, i+1);
96  }
97  }
98 
99  template <typename dtype>
100  void def_coo_to_csr(int64_t nz, int nrow, dtype * csr_vs, int * csr_ja, int * csr_ia, dtype const * coo_vs, int const * coo_rs, int const * coo_cs){
101  seq_coo_to_csr<dtype>(nz, nrow, csr_vs, csr_ja, csr_ia, coo_vs, coo_rs, coo_cs);
102  }
103 
104  template <typename dtype>
105  void def_csr_to_coo(int64_t nz, int nrow, dtype const * csr_vs, int const * csr_ja, int const * csr_ia, dtype * coo_vs, int * coo_rs, int * coo_cs){
106  seq_csr_to_coo<dtype>(nz, nrow, csr_vs, csr_ja, csr_ia, coo_vs, coo_rs, coo_cs);
107  }
108 
109  template <typename dtype>
111  return -a;
112  }
113 
114  template <typename dtype, bool is_ord>
115  inline typename std::enable_if<is_ord, dtype>::type
117  dtype b = default_addinv<dtype>(a);
118  return a>=b ? a : b;
119  }
120 
121  template <typename dtype, bool is_ord>
122  inline typename std::enable_if<!is_ord, dtype>::type
124  printf("CTF ERROR: cannot compute abs unless the set is ordered");
125  assert(0);
126  return a;
127  }
128 
129  template <typename dtype, dtype (*abs)(dtype)>
130  void char_abs(char const * a,
131  char * b){
132  ((dtype*)b)[0]=abs(((dtype const*)a)[0]);
133  }
134 
135  //C++14 support needed for these std::enable_if
136  template <typename dtype, bool is_ord>
137  inline typename std::enable_if<is_ord, dtype>::type
139  return a>b ? b : a;
140  }
141 
142  template <typename dtype, bool is_ord>
143  inline typename std::enable_if<!is_ord, dtype>::type
145  printf("CTF ERROR: cannot compute a max unless the set is ordered");
146  assert(0);
147  return a;
148  }
149 
150  template <typename dtype, bool is_ord>
151  inline typename std::enable_if<is_ord, dtype>::type
153  return std::numeric_limits<dtype>::max();
154  }
155 
156  template <typename dtype, bool is_ord>
157  inline typename std::enable_if<!is_ord, dtype>::type
159  printf("CTF ERROR: cannot compute a max unless the set is ordered");
160  assert(0);
161  dtype * a = NULL;
162  return *a;
163  }
164 
165  template <typename dtype, bool is_ord>
166  inline typename std::enable_if<is_ord, dtype>::type
168  return std::numeric_limits<dtype>::min();
169  }
170 
171  template <typename dtype, bool is_ord>
172  inline typename std::enable_if<!is_ord, dtype>::type
174  printf("CTF ERROR: cannot compute a max unless the set is ordered");
175  assert(0);
176  dtype * a = NULL;
177  return *a;
178  }
179 
180  template <typename dtype, bool is_ord>
181  inline typename std::enable_if<is_ord, dtype>::type
183  return b>a ? b : a;
184  }
185 
186  template <typename dtype, bool is_ord>
187  inline typename std::enable_if<!is_ord, dtype>::type
189  printf("CTF ERROR: cannot compute a min unless the set is ordered");
190  assert(0);
191  return a;
192  }
193  template <typename dtype>
194  MPI_Datatype get_default_mdtype(bool & is_custom){
195  MPI_Datatype newtype;
196  MPI_Type_contiguous(sizeof(dtype), MPI_BYTE, &newtype);
197  MPI_Type_commit(&newtype);
198  is_custom = true;
199  return newtype;
200  }
201 
202  extern MPI_Datatype MPI_CTF_BOOL;
203  extern MPI_Datatype MPI_CTF_DOUBLE_COMPLEX;
204  extern MPI_Datatype MPI_CTF_LONG_DOUBLE_COMPLEX;
205 
206  template <>
207  inline MPI_Datatype get_default_mdtype<bool>(bool & is_custom){ is_custom=false; return MPI_CTF_BOOL; }
208  template <>
209  inline MPI_Datatype get_default_mdtype< std::complex<double> >(bool & is_custom){ is_custom=false; return MPI_CTF_DOUBLE_COMPLEX; }
210  template <>
211  inline MPI_Datatype get_default_mdtype< std::complex<long double> >(bool & is_custom){ is_custom=false; return MPI_CTF_LONG_DOUBLE_COMPLEX; }
212  template <>
213  inline MPI_Datatype get_default_mdtype<char>(bool & is_custom){ is_custom=false; return MPI_CHAR; }
214  template <>
215  inline MPI_Datatype get_default_mdtype<int>(bool & is_custom){ is_custom=false; return MPI_INT; }
216  template <>
217  inline MPI_Datatype get_default_mdtype<int64_t>(bool & is_custom){ is_custom=false; return MPI_INT64_T; }
218  template <>
219  inline MPI_Datatype get_default_mdtype<unsigned int>(bool & is_custom){ is_custom=false; return MPI_UNSIGNED; }
220  template <>
221  inline MPI_Datatype get_default_mdtype<uint64_t>(bool & is_custom){ is_custom=false; return MPI_UINT64_T; }
222  template <>
223  inline MPI_Datatype get_default_mdtype<float>(bool & is_custom){ is_custom=false; return MPI_FLOAT; }
224  template <>
225  inline MPI_Datatype get_default_mdtype<double>(bool & is_custom){ is_custom=false; return MPI_DOUBLE; }
226  template <>
227  inline MPI_Datatype get_default_mdtype<long double>(bool & is_custom){ is_custom=false; return MPI_LONG_DOUBLE; }
228  template <>
229  inline MPI_Datatype get_default_mdtype< std::complex<float> >(bool & is_custom){ is_custom=false; return MPI_COMPLEX; }
230 
231 
232 
233  template <typename dtype>
234  constexpr bool get_default_is_ord(){
235  return false;
236  }
237 
238  #define INST_ORD_TYPE(dtype) \
239  template <> \
240  constexpr bool get_default_is_ord<dtype>(){ \
241  return true; \
242  }
243 
244  INST_ORD_TYPE(float)
245  INST_ORD_TYPE(double)
246  INST_ORD_TYPE(long double)
247  INST_ORD_TYPE(bool)
248  INST_ORD_TYPE(char)
249  INST_ORD_TYPE(int)
250  INST_ORD_TYPE(unsigned int)
251  INST_ORD_TYPE(int64_t)
252  INST_ORD_TYPE(uint64_t)
253 
254 }
255 
256 
257 namespace CTF {
258 
260  template <typename dtype>
261  struct dtypePair{
262  int64_t key;
264  bool operator < (const dtypePair<dtype>& other) const {
265  return (key < other.key);
266  }
267  };
268 
279  template <typename dtype=double, bool is_ord=CTF_int::get_default_is_ord<dtype>()>
280  class Set : public CTF_int::algstrct {
281  public:
282  int pair_sz;
284  MPI_Datatype tmdtype;
285  ~Set(){
286  if (is_custom_mdtype) MPI_Type_free(&tmdtype);
287  }
288 
289  Set(Set const & other) : CTF_int::algstrct(other) {
290  if (other.is_custom_mdtype){
291  tmdtype = CTF_int::get_default_mdtype<dtype>(is_custom_mdtype);
292  } else {
293  this->tmdtype = other.tmdtype;
294  is_custom_mdtype = false;
295  }
296  pair_sz = sizeof(std::pair<int64_t,dtype>);
297  //printf("%ld %ld \n", sizeof(dtype), pair_sz);
298  abs = other.abs;
299  }
300 
301  int pair_size() const {
302  //printf("%d %d \n", sizeof(dtype), pair_sz);
303  return pair_sz;
304  }
305 
306  int64_t get_key(char const * a) const {
307  return ((std::pair<int64_t,dtype> const *)a)->first;
308  }
309 
310  char * get_value(char * a) const {
311  return (char*)&(((std::pair<int64_t,dtype> const *)a)->second);
312  }
313 
314  char const * get_const_value(char const * a) const {
315  return (char const *)&(((std::pair<int64_t,dtype> const *)a)->second);
316  }
317 
318 
319 
320  virtual CTF_int::algstrct * clone() const {
321  return new Set<dtype, is_ord>(*this);
322  }
323 
324  bool is_ordered() const { return is_ord; }
325 
326  Set() : CTF_int::algstrct(sizeof(dtype)){
327  tmdtype = CTF_int::get_default_mdtype<dtype>(is_custom_mdtype);
328  set_abs_to_default();
329  pair_sz = sizeof(std::pair<int64_t,dtype>);
330  }
331 
333  abs = &CTF_int::char_abs< dtype, CTF_int::default_abs<dtype, is_ord> >;
334  }
335 
336  MPI_Datatype mdtype() const {
337  return tmdtype;
338  }
339 
340  void min(char const * a,
341  char const * b,
342  char * c) const {
343  ((dtype*)c)[0] = CTF_int::default_min<dtype,is_ord>(((dtype*)a)[0],((dtype*)b)[0]);
344  }
345 
346  void max(char const * a,
347  char const * b,
348  char * c) const {
349  ((dtype*)c)[0] = CTF_int::default_max<dtype,is_ord>(((dtype*)a)[0],((dtype*)b)[0]);
350  }
351 
352  void min(char * c) const {
353  ((dtype*)c)[0] = CTF_int::default_min_lim<dtype,is_ord>();
354  }
355 
356  void max(char * c) const {
357  ((dtype*)c)[0] = CTF_int::default_max_lim<dtype,is_ord>();
358  }
359 
360  void cast_double(double d, char * c) const {
361  //((dtype*)c)[0] = (dtype)d;
362  printf("CTF ERROR: double cast not possible for this algebraic structure\n");
363  assert(0);
364  }
365 
366  void cast_int(int64_t i, char * c) const {
367  //((dtype*)c)[0] = (dtype)i;
368  printf("CTF ERROR: integer cast not possible for this algebraic structure\n");
369  assert(0);
370  }
371 
372  double cast_to_double(char const * c) const {
373  printf("CTF ERROR: double cast not possible for this algebraic structure\n");
374  IASSERT(0);
375  assert(0);
376  return 0.0;
377  }
378 
379  int64_t cast_to_int(char const * c) const {
380  printf("CTF ERROR: int cast not possible for this algebraic structure\n");
381  assert(0);
382  return 0;
383  }
384 
385 
386  void print(char const * a, FILE * fp=stdout) const {
387  for (int i=0; i<el_size; i++){
388  fprintf(fp,"%x",a[i]);
389  }
390  }
391 
392 
393  bool isequal(char const * a, char const * b) const {
394  if (a == NULL && b == NULL) return true;
395  if (a == NULL || b == NULL) return false;
396  for (int i=0; i<el_size; i++){
397  if (a[i] != b[i]) return false;
398  }
399  return true;
400  }
401 
402  void coo_to_csr(int64_t nz, int nrow, char * csr_vs, int * csr_ja, int * csr_ia, char const * coo_vs, int const * coo_rs, int const * coo_cs) const {
403  CTF_int::def_coo_to_csr(nz, nrow, (dtype *)csr_vs, csr_ja, csr_ia, (dtype const *) coo_vs, coo_rs, coo_cs);
404  }
405 
406  void csr_to_coo(int64_t nz, int nrow, char const * csr_vs, int const * csr_ja, int const * csr_ia, char * coo_vs, int * coo_rs, int * coo_cs) const {
407  CTF_int::def_csr_to_coo(nz, nrow, (dtype const *)csr_vs, csr_ja, csr_ia, (dtype*) coo_vs, coo_rs, coo_cs);
408  }
409 
410  char * pair_alloc(int64_t n) const {
411  //assert(sizeof(std::pair<int64_t,dtype>[n])==(uint64_t)(pair_size()*n));
412  return (char*)(new std::pair<int64_t,dtype>[n]);
413  }
414 
415  char * alloc(int64_t n) const {
416  //assert(sizeof(dtype[n])==(uint64_t)(el_size*n));
417  return (char*)(new dtype[n]);
418  }
419 
420  void dealloc(char * ptr) const {
421  return delete [] (dtype*)ptr;
422  }
423 
424  void pair_dealloc(char * ptr) const {
425  return delete [] (std::pair<int64_t,dtype>*)ptr;
426  }
427 
428 
429  void sort(int64_t n, char * pairs) const {
430  std::sort((dtypePair<dtype>*)pairs,((dtypePair<dtype>*)pairs)+n);
431  }
432 
433  void copy(char * a, char const * b) const {
434  ((dtype *)a)[0] = ((dtype const *)b)[0];
435  }
436 
437  void copy(char * a, char const * b, int64_t n) const {
438  std::copy((dtype const *)b, ((dtype const *)b) + n, (dtype *)a);
439  }
440 
441  void copy_pair(char * a, char const * b) const {
442  ((std::pair<int64_t,dtype> *)a)[0] = ((std::pair<int64_t,dtype> const *)b)[0];
443  }
444 
445  void copy_pairs(char * a, char const * b, int64_t n) const {
446  std::copy((std::pair<int64_t,dtype> const *)b, ((std::pair<int64_t,dtype> const *)b) + n, (std::pair<int64_t,dtype> *)a);
447  //std::copy((std::pair<int64_t,dtype> *)a, (std::pair<int64_t,dtype> const *)b, n);
448  //for (int64_t i=0; i<n; i++){
449  /*printf("i=%ld\n",i);
450  this->print((char*)&(((std::pair<int64_t,dtype> const *)a)[i].second));
451  this->print((char*)&(((std::pair<int64_t,dtype> const *)b)[i].second));*/
452  //((std::pair<int64_t,dtype>*)a)[i] = ((std::pair<int64_t,dtype> const *)b)[i];
453  //this->print((char*)&(((std::pair<int64_t,dtype> const *)a)[i].second));
454  //}
455  }
456 
457  void set(char * a, char const * b, int64_t n) const {
458  std::fill((dtype*)a, ((dtype*)a)+n, *((dtype*)b));
459  }
460 
461  void set_pair(char * a, int64_t key, char const * b) const {
462  ((std::pair<int64_t,dtype> *)a)[0] = std::pair<int64_t,dtype>(key,*((dtype*)b));
463  }
464 
465  void set_pairs(char * a, char const * b, int64_t n) const {
466  std::fill((std::pair<int64_t,dtype> *)a, (std::pair<int64_t,dtype> *)a + n, *(std::pair<int64_t,dtype> const*)b);
467  }
468 
469  void copy(int64_t n, char const * a, int inc_a, char * b, int inc_b) const {
470  dtype const * da = (dtype const*)a;
471  dtype * db = (dtype *)b;
472  for (int64_t i=0; i<n; i++){
473  db[inc_b*i] = da[inc_a*i];
474  }
475  }
476 
477  void copy(int64_t m,
478  int64_t n,
479  char const * a,
480  int64_t lda_a,
481  char * b,
482  int64_t lda_b) const {
483 
484  dtype const * da = (dtype const*)a;
485  dtype * db = (dtype *)b;
486  for (int64_t j=0; j<n; j++){
487  for (int64_t i=0; i<m; i++){
488  db[j*lda_b+i] = da[j*lda_a+i];
489  }
490  }
491  }
492 
493  void init(int64_t n, char * arr) const {
494  std::fill((dtype*)arr,((dtype*)arr)+n,dtype());
495  }
496 
501  virtual void init_shell(int64_t n, char * arr) const {
502  dtype dummy = dtype();
503  for (int i=0; i<n; i++){
504  memcpy(arr+i*el_size,(char*)&dummy,el_size);
505  }
506  }
507 
508 
509 /*
510  void copy(int64_t m,
511  int64_t n,
512  char const * a,
513  int64_t lda_a,
514  char const * alpha,
515  char * b,
516  int64_t lda_b,
517  char const * beta) const {
518 
519  dtype const * da = (dtype const*)a;
520  dtype dalpha = *((dtype const*)alpha);
521  dtype dbeta = *((dtype const*)beta);
522  dtype * db = (dtype *)b;
523  for (int64_t j=0; j<n; j++){
524  for (int64_t i=0; i<m; i++){
525  dbeta*db[j*lda_b+i] += dalpha*da[j*lda_a+i]
526  }
527  }
528  }*/
529 
530  };
531 
532  //FIXME do below with macros to shorten
533 
534  template <>
535  inline void Set<float>::cast_double(double d, char * c) const {
536  ((float*)c)[0] = (float)d;
537  }
538 
539  template <>
540  inline void Set<double>::cast_double(double d, char * c) const {
541  ((double*)c)[0] = d;
542  }
543 
544  template <>
545  inline void Set<long double>::cast_double(double d, char * c) const {
546  ((long double*)c)[0] = (long double)d;
547  }
548 
549  template <>
550  inline void Set<int>::cast_double(double d, char * c) const {
551  ((int*)c)[0] = (int)d;
552  }
553 
554  template <>
555  inline void Set<uint64_t>::cast_double(double d, char * c) const {
556  ((uint64_t*)c)[0] = (uint64_t)d;
557  }
558 
559  template <>
560  inline void Set<int64_t>::cast_double(double d, char * c) const {
561  ((int64_t*)c)[0] = (int64_t)d;
562  }
563 
564  template <>
565  inline void Set< std::complex<float>,false >::cast_double(double d, char * c) const {
566  ((std::complex<float>*)c)[0] = (std::complex<float>)d;
567  }
568 
569  template <>
570  inline void Set< std::complex<double>,false >::cast_double(double d, char * c) const {
571  ((std::complex<double>*)c)[0] = (std::complex<double>)d;
572  }
573 
574  template <>
575  inline void Set< std::complex<long double>,false >::cast_double(double d, char * c) const {
576  ((std::complex<long double>*)c)[0] = (std::complex<long double>)d;
577  }
578 
579  template <>
580  inline void Set<float>::cast_int(int64_t d, char * c) const {
581  ((float*)c)[0] = (float)d;
582  }
583 
584  template <>
585  inline void Set<double>::cast_int(int64_t d, char * c) const {
586  ((double*)c)[0] = (double)d;
587  }
588 
589  template <>
590  inline void Set<long double>::cast_int(int64_t d, char * c) const {
591  ((long double*)c)[0] = (long double)d;
592  }
593 
594  template <>
595  inline void Set<int>::cast_int(int64_t d, char * c) const {
596  ((int*)c)[0] = (int)d;
597  }
598 
599  template <>
600  inline void Set<uint64_t>::cast_int(int64_t d, char * c) const {
601  ((uint64_t*)c)[0] = (uint64_t)d;
602  }
603 
604  template <>
605  inline void Set<int64_t>::cast_int(int64_t d, char * c) const {
606  ((int64_t*)c)[0] = (int64_t)d;
607  }
608 
609  template <>
610  inline void Set< std::complex<float>,false >::cast_int(int64_t d, char * c) const {
611  ((std::complex<float>*)c)[0] = (std::complex<float>)d;
612  }
613 
614  template <>
615  inline void Set< std::complex<double>,false >::cast_int(int64_t d, char * c) const {
616  ((std::complex<double>*)c)[0] = (std::complex<double>)d;
617  }
618 
619  template <>
620  inline void Set< std::complex<long double>,false >::cast_int(int64_t d, char * c) const {
621  ((std::complex<long double>*)c)[0] = (std::complex<long double>)d;
622  }
623 
624  template <>
625  inline double Set<float>::cast_to_double(char const * c) const {
626  return (double)(((float*)c)[0]);
627  }
628 
629  template <>
630  inline double Set<double>::cast_to_double(char const * c) const {
631  return ((double*)c)[0];
632  }
633 
634  template <>
635  inline double Set<int>::cast_to_double(char const * c) const {
636  return (double)(((int*)c)[0]);
637  }
638 
639  template <>
640  inline double Set<uint64_t>::cast_to_double(char const * c) const {
641  return (double)(((uint64_t*)c)[0]);
642  }
643 
644  template <>
645  inline double Set<int64_t>::cast_to_double(char const * c) const {
646  return (double)(((int64_t*)c)[0]);
647  }
648 
649 
650  template <>
651  inline int64_t Set<int64_t>::cast_to_int(char const * c) const {
652  return ((int64_t*)c)[0];
653  }
654 
655  template <>
656  inline int64_t Set<int>::cast_to_int(char const * c) const {
657  return (int64_t)(((int*)c)[0]);
658  }
659 
660  template <>
661  inline int64_t Set<unsigned int>::cast_to_int(char const * c) const {
662  return (int64_t)(((unsigned int*)c)[0]);
663  }
664 
665  template <>
666  inline int64_t Set<uint64_t>::cast_to_int(char const * c) const {
667  return (int64_t)(((uint64_t*)c)[0]);
668  }
669 
670  template <>
671  inline int64_t Set<bool>::cast_to_int(char const * c) const {
672  return (int64_t)(((bool*)c)[0]);
673  }
674 
675  template <>
676  inline void Set<float>::print(char const * a, FILE * fp) const {
677  fprintf(fp,"%11.5E",((float*)a)[0]);
678  }
679 
680  template <>
681  inline void Set<double>::print(char const * a, FILE * fp) const {
682  fprintf(fp,"%11.5E",((double*)a)[0]);
683  }
684 
685  template <>
686  inline void Set<int64_t>::print(char const * a, FILE * fp) const {
687  fprintf(fp,"%ld",((int64_t*)a)[0]);
688  }
689 
690  template <>
691  inline void Set<int>::print(char const * a, FILE * fp) const {
692  fprintf(fp,"%d",((int*)a)[0]);
693  }
694 
695  template <>
696  inline void Set< std::complex<float>,false >::print(char const * a, FILE * fp) const {
697  fprintf(fp,"(%11.5E,%11.5E)",((std::complex<float>*)a)[0].real(),((std::complex<float>*)a)[0].imag());
698  }
699 
700  template <>
701  inline void Set< std::complex<double>,false >::print(char const * a, FILE * fp) const {
702  fprintf(fp,"(%11.5E,%11.5E)",((std::complex<double>*)a)[0].real(),((std::complex<double>*)a)[0].imag());
703  }
704 
705  template <>
706  inline void Set< std::complex<long double>,false >::print(char const * a, FILE * fp) const {
707  fprintf(fp,"(%11.5LE,%11.5LE)",((std::complex<long double>*)a)[0].real(),((std::complex<long double>*)a)[0].imag());
708  }
709 
710  template <>
711  inline bool Set<float>::isequal(char const * a, char const * b) const {
712  if (a == NULL && b == NULL) return true;
713  if (a == NULL || b == NULL) return false;
714  return ((float*)a)[0] == ((float*)b)[0];
715  }
716 
717  template <>
718  inline bool Set<double>::isequal(char const * a, char const * b) const {
719  if (a == NULL && b == NULL) return true;
720  if (a == NULL || b == NULL) return false;
721  return ((double*)a)[0] == ((double*)b)[0];
722  }
723 
724  template <>
725  inline bool Set<int>::isequal(char const * a, char const * b) const {
726  if (a == NULL && b == NULL) return true;
727  if (a == NULL || b == NULL) return false;
728  return ((int*)a)[0] == ((int*)b)[0];
729  }
730 
731  template <>
732  inline bool Set<uint64_t>::isequal(char const * a, char const * b) const {
733  if (a == NULL && b == NULL) return true;
734  if (a == NULL || b == NULL) return false;
735  return ((uint64_t*)a)[0] == ((uint64_t*)b)[0];
736  }
737 
738  template <>
739  inline bool Set<int64_t>::isequal(char const * a, char const * b) const {
740  if (a == NULL && b == NULL) return true;
741  if (a == NULL || b == NULL) return false;
742  return ((int64_t*)a)[0] == ((int64_t*)b)[0];
743  }
744 
745  template <>
746  inline bool Set<long double>::isequal(char const * a, char const * b) const {
747  if (a == NULL && b == NULL) return true;
748  if (a == NULL || b == NULL) return false;
749  return ((long double*)a)[0] == ((long double*)b)[0];
750  }
751 
752  template <>
753  inline bool Set< std::complex<float>,false >::isequal(char const * a, char const * b) const {
754  if (a == NULL && b == NULL) return true;
755  if (a == NULL || b == NULL) return false;
756  return (( std::complex<float> *)a)[0] == (( std::complex<float> *)b)[0];
757  }
758 
759  template <>
760  inline bool Set< std::complex<double>,false >::isequal(char const * a, char const * b) const {
761  if (a == NULL && b == NULL) return true;
762  if (a == NULL || b == NULL) return false;
763  return (( std::complex<double> *)a)[0] == (( std::complex<double> *)b)[0];
764  }
765 
766  template <>
767  inline bool Set< std::complex<long double>,false >::isequal(char const * a, char const * b) const {
768  if (a == NULL && b == NULL) return true;
769  if (a == NULL || b == NULL) return false;
770  return (( std::complex<long double> *)a)[0] == (( std::complex<long double> *)b)[0];
771  }
772 
773 
774 
778 }
779 #include "monoid.h"
780 #endif
Set class defined by a datatype and a min/max function (if it is partially ordered i...
Definition: set.h:280
void set_pair(char *a, int64_t key, char const *b) const
sets 1 elements of pair a to value and key
Definition: set.h:461
double cast_to_double(char const *c) const
return (double)*c
Definition: set.h:372
pair for sorting
Definition: set.h:261
#define INST_ORD_TYPE(dtype)
Definition: set.h:238
char * pair_alloc(int64_t n) const
allocate space for n (int64_t,dtype) pairs, necessary for object types
Definition: set.h:410
bool try_mkl_coo_to_csr(int64_t nz, int nrow, char *csr_vs, int *csr_ja, int *csr_ia, char const *coo_vs, int const *coo_rs, int const *coo_cs, int el_size)
Definition: set.cxx:70
void min(char *c) const
c = minimum possible value
Definition: set.h:352
MPI_Datatype mdtype() const
MPI datatype.
Definition: set.h:336
void copy_pairs(char *a, char const *b, int64_t n) const
copies n pair from array b to array a
Definition: set.h:445
std::enable_if< is_ord, dtype >::type default_max_lim()
Definition: set.h:152
~Set()
Definition: set.h:285
MPI_Datatype get_default_mdtype< int >(bool &is_custom)
Definition: set.h:215
void max(char const *a, char const *b, char *c) const
c = max(a,b)
Definition: set.h:346
void pair_dealloc(char *ptr) const
deallocate given pointer containing contiguous array of pairs
Definition: set.h:424
int64_t key
Definition: set.h:262
virtual CTF_int::algstrct * clone() const
&#39;&#39;copy constructor&#39;&#39;
Definition: set.h:320
def real(tensor, A)
Definition: core.pyx:2997
void copy(char *a, char const *b) const
copies element b to element a
Definition: set.h:433
MPI_Datatype get_default_mdtype< bool >(bool &is_custom)
Definition: set.h:207
int pair_size() const
gets pair size el_size plus the key size
Definition: set.h:301
void def_csr_to_coo(int64_t nz, int nrow, dtype const *csr_vs, int const *csr_ja, int const *csr_ia, dtype *coo_vs, int *coo_rs, int *coo_cs)
Definition: set.h:105
void char_abs(char const *a, char *b)
Definition: set.h:130
void seq_csr_to_coo(int64_t nz, int nrow, dtype const *csr_vs, int const *csr_ja, int const *csr_ia, dtype *coo_vs, int *coo_rs, int *coo_cs)
Definition: set.h:85
int pair_sz
Definition: set.h:282
char const * get_const_value(char const *a) const
Definition: set.h:314
constexpr bool get_default_is_ord()
Definition: set.h:234
MPI_Datatype get_default_mdtype< long double >(bool &is_custom)
Definition: set.h:227
#define IASSERT(...)
Definition: common.h:74
def imag(tensor, A)
Definition: core.pyx:3038
MPI_Datatype get_default_mdtype(bool &is_custom)
Definition: set.h:194
void set_abs_to_default()
Definition: set.h:332
std::enable_if< is_ord, dtype >::type default_max(dtype a, dtype b)
Definition: set.h:182
bool is_custom_mdtype
Definition: set.h:283
bool try_mkl_csr_to_coo(int64_t nz, int nrow, char const *csr_vs, int const *csr_ja, int const *csr_ia, char *coo_vs, int *coo_rs, int *coo_cs, int el_size)
Definition: set.cxx:91
void copy(int64_t n, char const *a, int inc_a, char *b, int inc_b) const
copies n elements TO array b with increment inc_a FROM array a with increment inc_b ...
Definition: set.h:469
bool isequal(char const *a, char const *b) const
returns true if algstrct elements a and b are equal
Definition: set.h:393
void print(char const *a, FILE *fp=stdout) const
prints the value
Definition: set.h:386
MPI_Datatype get_default_mdtype< double >(bool &is_custom)
Definition: set.h:225
void csr_to_coo(int64_t nz, int nrow, char const *csr_vs, int const *csr_ja, int const *csr_ia, char *coo_vs, int *coo_rs, int *coo_cs) const
converts CSR sparse matrix layout to coordinate (COO) layout
Definition: set.h:406
MPI_Datatype get_default_mdtype< unsigned int >(bool &is_custom)
Definition: set.h:219
void copy_pair(char *a, char const *b) const
copies pair b to element a
Definition: set.h:441
std::enable_if< is_ord, dtype >::type default_abs(dtype a)
Definition: set.h:116
void coo_to_csr(int64_t nz, int nrow, char *csr_vs, int *csr_ja, int *csr_ia, char const *coo_vs, int const *coo_rs, int const *coo_cs) const
converts coordinate sparse matrix layout to CSR layout
Definition: set.h:402
MPI_Datatype MPI_CTF_LONG_DOUBLE_COMPLEX
Definition: set.cxx:16
void(* abs)(char const *a, char *b)
b = max(a,addinv(a))
Definition: algstrct.h:42
MPI_Datatype get_default_mdtype< float >(bool &is_custom)
Definition: set.h:223
void dealloc(char *ptr) const
deallocate given pointer containing contiguous array of values
Definition: set.h:420
void copy(int64_t m, int64_t n, char const *a, int64_t lda_a, char *b, int64_t lda_b) const
copies m-by-n submatrix from a with lda_a to b with lda_b
Definition: set.h:477
void max(char *c) const
c = maximum possible value
Definition: set.h:356
Set(Set const &other)
Definition: set.h:289
def abs(initA)
Definition: core.pyx:5440
char * get_value(char *a) const
gets pair to value from pair
Definition: set.h:310
void sort(int64_t n, char *pairs) const
sorts n sets of pairs using std::sort
Definition: set.h:429
Set()
Definition: set.h:326
char * alloc(int64_t n) const
allocate space for n items, necessary for object types
Definition: set.h:415
def copy(tensor, A)
Definition: core.pyx:3583
std::enable_if< is_ord, dtype >::type default_min(dtype a, dtype b)
Definition: set.h:138
std::enable_if< is_ord, dtype >::type default_min_lim()
Definition: set.h:167
void copy(char *a, char const *b, int64_t n) const
copies n elements from array b to array a
Definition: set.h:437
MPI_Datatype get_default_mdtype< char >(bool &is_custom)
Definition: set.h:213
void init(int64_t n, char *arr) const
initialize n objects to zero
Definition: set.h:493
MPI_Datatype get_default_mdtype< uint64_t >(bool &is_custom)
Definition: set.h:221
bool is_ordered() const
Definition: set.h:324
dtype default_addinv(dtype a)
Definition: set.h:110
void set_pairs(char *a, char const *b, int64_t n) const
sets n elements of array of pairs a to value b
Definition: set.h:465
algstrct (algebraic structure) defines the elementwise operations computed in each tensor contraction...
Definition: algstrct.h:34
Definition: apsp.cxx:17
void def_coo_to_csr(int64_t nz, int nrow, dtype *csr_vs, int *csr_ja, int *csr_ia, dtype const *coo_vs, int const *coo_rs, int const *coo_cs)
Definition: set.h:100
dtype data
Definition: set.h:263
MPI_Datatype tmdtype
Definition: set.h:284
void seq_coo_to_csr(int64_t nz, int nrow, dtype *csr_vs, int *csr_ja, int *csr_ia, dtype const *coo_vs, int const *coo_rs, int const *coo_cs)
Definition: set.h:17
int64_t key
Definition: back_comp.h:66
void cast_double(double d, char *c) const
c = &d
Definition: set.h:360
virtual void init_shell(int64_t n, char *arr) const
initialize n objects to zero
Definition: set.h:501
MPI_Datatype get_default_mdtype< int64_t >(bool &is_custom)
Definition: set.h:217
int64_t cast_to_int(char const *c) const
return (int64_t)*c
Definition: set.h:379
int64_t get_key(char const *a) const
gets key from pair
Definition: set.h:306
void cast_int(int64_t i, char *c) const
c = &i
Definition: set.h:366
void min(char const *a, char const *b, char *c) const
c = min(a,b)
Definition: set.h:340
MPI_Datatype MPI_CTF_BOOL
Definition: set.cxx:14
MPI_Datatype MPI_CTF_DOUBLE_COMPLEX
Definition: set.cxx:15