cyclopstf.cxx 39.9 KB
Newer Older
1
/*Copyright (c) 2011, Edgar Solomonik, all rights reserved.*/
2 3 4 5 6 7 8 9 10 11 12 13 14

#include "dist_tensor_internal.h"
#include "cyclopstf.hpp"
#include "mach.h"
#include "../shared/util.h"
#include "../shared/memcontrol.h"
#include <stdint.h>
#include <limits.h>
#if VERIFY
#include "../unit_test/unit_test.h"
#include "../unit_test/unit_test_ctr.h"
#endif

15 16 17 18 19
#ifdef HPM
extern "C" void HPM_Start(char *);  
extern "C" void HPM_Stop(char *);
#endif

20
#define DEF_INNER_SIZE 256
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

/** 
 * \brief destructor
 */
template<typename dtype>
tCTF<dtype>::~tCTF(){
  exit();
}

/** 
 * \brief constructor
 */
template<typename dtype>
tCTF<dtype>::tCTF(){
  initialized = 0;
}

38 39 40 41
template<typename dtype>
MPI_Comm tCTF<dtype>::get_MPI_Comm(){
  return (dt->get_global_comm())->cm;
}
Edgar Solomonik's avatar
Edgar Solomonik committed
42 43 44 45 46 47 48 49 50 51 52
    
/* return MPI processor rank */
template<typename dtype>
int tCTF<dtype>::get_rank(){
  return (dt->get_global_comm())->rank;
}
/* return number of MPI processes in the defined global context */
template<typename dtype>
int tCTF<dtype>::get_num_pes(){
  return (dt->get_global_comm())->np;
}
53

54 55 56 57 58 59 60 61
/**
 * \brief  initializes library. 
 *      Sets topology to be a mesh of dimension ndim with
 *      edge lengths dim_len. 
 *
 * \param[in] global_context communicator decated to this library instance
 * \param[in] rank this pe rank within the global context
 * \param[in] np number of processors
62 63 64
 * \param[in] mach the type of machine we are running on
 * \param[in] argc number of arguments passed to main
 * \param[in] argv arguments passed to main
65 66 67 68 69
 */
template<typename dtype>
int tCTF<dtype>::init(MPI_Comm const  global_context,
                      int const       rank, 
                      int const       np,
70 71
                      CTF_MACHINE     mach,
                      int const       argc,
Devin Matthews's avatar
Devin Matthews committed
72
                      const char * const *  argv){
73 74 75
  int ndim, ret;
  int * dim_len;
  get_topo(np, mach, &ndim, &dim_len);
76
  ret = tCTF<dtype>::init(global_context, rank, np, ndim, dim_len, argc, argv);
77 78
  if (np > 1)
    CTF_free(dim_len);
79 80 81 82 83 84 85 86 87 88 89 90 91
  return ret;
}

/**
 * \brief  initializes library. 
 *      Sets topology to be a mesh of dimension ndim with
 *      edge lengths dim_len. 
 *
 * \param[in] global_context communicator decated to this library instance
 * \param[in] rank this pe rank within the global context
 * \param[in] np number of processors
 * \param[in] ndim is the number of dimensions in the topology
 * \param[in] dim_len is the number of processors along each dimension
92 93
 * \param[in] argc number of arguments passed to main
 * \param[in] argv arguments passed to main
94 95
 */
template<typename dtype>
96 97
int tCTF<dtype>::init(MPI_Comm const  global_context,
                      int const       rank, 
98 99 100
                      int const       np, 
                      int const       ndim, 
                      int const *     dim_len,
101
                      int const       argc,
Devin Matthews's avatar
Devin Matthews committed
102
                      const char * const *  argv){
103
  char * mst_size, * stack_size, * mem_size, * ppn;
104
  
105
  TAU_FSTART(CTF);
106 107 108
#ifdef HPM
  HPM_Start("CTF");
#endif
109 110
  CTF_set_context(global_context);
  CTF_set_main_args(argc, argv);
111

112
#ifdef USE_OMP
113
  if (rank == 0)
114
    VPRINTF(1,"Running with %d threads\n",omp_get_max_threads());
115
#endif
116 117
  
  mst_size = getenv("CTF_MST_SIZE");
118 119
  stack_size = getenv("CTF_STACK_SIZE");
  if (mst_size == NULL && stack_size == NULL){
120 121
#ifdef USE_MST
    if (rank == 0)
122
      VPRINTF(1,"Creating stack of size "PRId64"\n",1000*(long_int)1E6);
123 124 125
    CTF_mst_create(1000*(long_int)1E6);
#else
    if (rank == 0){
126
      VPRINTF(1,"Running without stack, define CTF_STACK_SIZE environment variable to activate stack\n");
127 128 129
    }
#endif
  } else {
130 131 132 133 134
    uint64_t imst_size = 0 ;
    if (mst_size != NULL) 
      imst_size = strtoull(mst_size,NULL,0);
    if (stack_size != NULL)
      imst_size = MAX(imst_size,strtoull(stack_size,NULL,0));
135
    if (rank == 0)
136
      VPRINTF(1,"Creating stack of size "PRIu64" due to CTF_STACK_SIZE enviroment variable\n",
137 138 139 140 141 142 143
                imst_size);
    CTF_mst_create(imst_size);
  }
  mem_size = getenv("CTF_MEMORY_SIZE");
  if (mem_size != NULL){
    uint64_t imem_size = strtoull(mem_size,NULL,0);
    if (rank == 0)
144
      VPRINTF(1,"Memory size set to "PRIu64" by CTF_MEMORY_SIZE environment variable\n",
145 146 147 148 149 150
                imem_size);
    CTF_set_mem_size(imem_size);
  }
  ppn = getenv("CTF_PPN");
  if (ppn != NULL){
    if (rank == 0)
151
      VPRINTF(1,"Assuming %d processes per node due to CTF_PPN environment variable\n",
152 153 154 155
                atoi(ppn));
    LIBT_ASSERT(atoi(ppn)>=1);
    CTF_set_memcap(.75/atof(ppn));
  }
156
  initialized = 1;
157
  CommData_t * glb_comm = (CommData_t*)CTF_alloc(sizeof(CommData_t));
158 159
  SET_COMM(global_context, rank, np, glb_comm);
  dt = new dist_tensor<dtype>();
160
  return dt->initialize(glb_comm, ndim, dim_len, DEF_INNER_SIZE);
161 162 163 164 165 166 167 168 169 170
}


/**
 * \brief  defines a tensor and retrieves handle
 *
 * \param[in] ndim number of tensor dimensions
 * \param[in] edge_len global edge lengths of tensor
 * \param[in] sym symmetry relations of tensor
 * \param[out] tensor_id the tensor index (handle)
171 172
 * \param[in] name string name for tensor (optionary)
 * \param[in] profile wether to make profile calls for the tensor
173 174
 */
template<typename dtype>
175
int tCTF<dtype>::define_tensor(int const        ndim,             
176 177
                               int const *      edge_len, 
                               int const *      sym,
178 179 180 181 182
                               int *            tensor_id,
                               char const *     name,
                               int              profile){
  return dt->define_tensor(ndim, edge_len, sym, 
                           tensor_id, 1, name, profile);
183 184 185 186 187 188 189 190 191 192 193 194 195 196
}
    
/* \brief clone a tensor object
 * \param[in] tensor_id id of old tensor
 * \param[in] copy_data if 0 then leave tensor blank, if 1 copy data from old
 * \param[out] new_tensor_id id of new tensor
 */
template<typename dtype>
int tCTF<dtype>::clone_tensor(int const tensor_id,
                              int const copy_data,
                              int *     new_tensor_id){
  dt->clone_tensor(tensor_id, copy_data, new_tensor_id);
  return DIST_TENSOR_SUCCESS;
}
197 198 199
   

template<typename dtype>
200
int tCTF<dtype>::get_name(int const tensor_id, char const ** name){
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
  return dt->get_name(tensor_id, name);
}
 
template<typename dtype>
int tCTF<dtype>::set_name(int const tensor_id, char const * name){
  return dt->set_name(tensor_id, name);
}

template<typename dtype>
int tCTF<dtype>::profile_on(int const tensor_id){
  return dt->profile_on(tensor_id);
}

template<typename dtype>
int tCTF<dtype>::profile_off(int const tensor_id){
  return dt->profile_off(tensor_id);
}
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
    
/* \brief get dimension of a tensor 
 * \param[in] tensor_id id of tensor
 * \param[out] ndim dimension of tensor
 */
template<typename dtype>
int tCTF<dtype>::get_dimension(int const tensor_id, int *ndim) const{
  *ndim = dt->get_dim(tensor_id);
  return DIST_TENSOR_SUCCESS;
}
    
/* \brief get lengths of a tensor 
 * \param[in] tensor_id id of tensor
 * \param[out] edge_len edge lengths of tensor
 */
template<typename dtype>
int tCTF<dtype>::get_lengths(int const tensor_id, int **edge_len) const{
235 236
  int ndim, * sym;
  dt->get_tsr_info(tensor_id, &ndim, edge_len, &sym);
237 238
  CTF_untag_mem(edge_len);
  CTF_free(sym);
239 240 241 242 243 244 245 246 247 248
  return DIST_TENSOR_SUCCESS;
}
    
/* \brief get symmetry of a tensor 
 * \param[in] tensor_id id of tensor
 * \param[out] sym symmetries of tensor
 */
template<typename dtype>
int tCTF<dtype>::get_symmetry(int const tensor_id, int **sym) const{
  *sym = dt->get_sym(tensor_id);
249
  CTF_untag_mem(*sym);
250 251 252 253 254 255 256 257
  return DIST_TENSOR_SUCCESS;
}
    
/* \brief get raw data pointer WARNING: includes padding 
 * \param[in] tensor_id id of tensor
 * \param[out] data raw local data
 */
template<typename dtype>
solomon's avatar
solomon committed
258
int tCTF<dtype>::get_raw_data(int const tensor_id, dtype ** data, long_int * size) {
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
  *data = dt->get_raw_data(tensor_id, size);
  return DIST_TENSOR_SUCCESS;
}

/**
 * \brief get information about tensor
 * \param[in] tensor_id id of tensor
 * \param[out] ndim dimension of tensor
 * \param[out] edge_len edge lengths of tensor
 * \param[out] sym symmetries of tensor
 */
template<typename dtype>
int tCTF<dtype>::info_tensor(int const  tensor_id,
                             int *      ndim,
                             int **     edge_len,
                             int **     sym) const{
  dt->get_tsr_info(tensor_id, ndim, edge_len, sym);
276 277
  CTF_untag_mem(*sym);
  CTF_untag_mem(*edge_len);
278 279 280 281 282 283 284 285 286 287 288 289
  return DIST_TENSOR_SUCCESS;
}

/**
 * \brief  Input tensor data with <key, value> pairs where key is the
 *              global index for the value. 
 * \param[in] tensor_id tensor handle
 * \param[in] num_pair number of pairs to write
 * \param[in] mapped_data pairs to write
 */
template<typename dtype>
int tCTF<dtype>::write_tensor(int const               tensor_id, 
solomon's avatar
solomon committed
290
                              long_int const           num_pair,  
Devin Matthews's avatar
Devin Matthews committed
291 292
                              tkv_pair<dtype> const * mapped_data){
  return dt->write_pairs(tensor_id, num_pair, 1.0, 0.0, const_cast<tkv_pair<dtype>*>(mapped_data), 'w');
293 294 295 296 297 298 299 300
}

/** 
 * \brief  Add tensor data new=alpha*new+beta*old
 *         with <key, value> pairs where key is the 
 *         global index for the value. 
 * \param[in] tensor_id tensor handle
 * \param[in] num_pair number of pairs to write
301 302
 * \param[in] alpha scaling factor of written value
 * \param[in] beta scaling factor of old value
303 304 305 306
 * \param[in] mapped_data pairs to write
 */
template<typename dtype>
int tCTF<dtype>::write_tensor(int const               tensor_id, 
307 308 309
                              long_int const          num_pair,  
                              dtype const              alpha,
                              dtype const              beta,
Devin Matthews's avatar
Devin Matthews committed
310 311
                              tkv_pair<dtype> const * mapped_data){
  return dt->write_pairs(tensor_id, num_pair, alpha, beta, const_cast<tkv_pair<dtype>*>(mapped_data), 'w');
312 313
}

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
/**
 * \brief read tensor data with <key, value> pairs where key is the
 *              global index for the value, which gets filled in. 
 * \param[in] tensor_id tensor handle
 * \param[in] num_pair number of pairs to read
 * \param[in] alpha scaling factor of read value
 * \param[in] beta scaling factor of old value
 * \param[in] mapped_data pairs to write
 */
template<typename dtype>
int tCTF<dtype>::read_tensor(int const                tensor_id, 
                             long_int const           num_pair, 
                             dtype const               alpha, 
                             dtype const               beta, 
                             tkv_pair<dtype> * const  mapped_data){
  return dt->write_pairs(tensor_id, num_pair, alpha, beta, mapped_data, 'r');
}


333 334 335 336 337 338 339 340 341
/**
 * \brief read tensor data with <key, value> pairs where key is the
 *              global index for the value, which gets filled in. 
 * \param[in] tensor_id tensor handle
 * \param[in] num_pair number of pairs to read
 * \param[in,out] mapped_data pairs to read
 */
template<typename dtype>
int tCTF<dtype>::read_tensor(int const                tensor_id, 
342
                             long_int const           num_pair, 
343
                             tkv_pair<dtype> * const  mapped_data){
344
  return read_tensor(tensor_id, num_pair, 1.0, 0.0, mapped_data);
345 346
}

347 348 349 350 351 352 353 354 355 356 357 358 359
template<typename dtype>
int tCTF<dtype>::permute_tensor( int const              tid_A,
                                 int * const *          permutation_A,
                                 dtype const            alpha,
                                 tCTF<dtype> *          tC_A,
                                 int const              tid_B,
                                 int * const *          permutation_B,
                                 dtype const            beta,
                                 tCTF<dtype> *          tC_B){
  return dt->permute_tensor(tid_A, permutation_A, alpha, tC_A->dt, tid_B, permutation_B, beta, tC_B->dt);
}


360 361 362 363
template<typename dtype>
int tCTF<dtype>::slice_tensor( int const    tid_A,
                               int const *  offsets_A,
                               int const *  ends_A,
364
                               dtype const  alpha,
365 366 367
                               int const    tid_B,
                               int const *  offsets_B,
                               int const *  ends_B,
368
                               dtype const  beta){
369 370 371 372 373 374 375 376
  return dt->slice_tensor(tid_A, offsets_A, ends_A, alpha, dt,
                          tid_B, offsets_B, ends_B, beta, dt);
}

template<typename dtype>
int tCTF<dtype>::slice_tensor( int const      tid_A,
                               int const *    offsets_A,
                               int const *    ends_A,
377
                               dtype const    alpha,
378 379 380 381
                               tCTF<dtype> *  dt_other_A,
                               int const      tid_B,
                               int const *    offsets_B,
                               int const *    ends_B,
382
                               dtype const    beta){
383 384 385 386 387 388 389 390
  return dt->slice_tensor(tid_A, offsets_A, ends_A, alpha, dt_other_A->dt,
                          tid_B, offsets_B, ends_B, beta, dt);
}

template<typename dtype>
int tCTF<dtype>::slice_tensor( int const      tid_A,
                               int const *    offsets_A,
                               int const *    ends_A,
391
                               dtype const    alpha,
392 393 394
                               int const      tid_B,
                               int const *    offsets_B,
                               int const *    ends_B,
395
                               dtype const    beta,
396 397 398
                               tCTF<dtype> *  dt_other_B){
  return dt->slice_tensor(tid_A, offsets_A, ends_A, alpha, dt,
                          tid_B, offsets_B, ends_B, beta, dt_other_B->dt);
399 400
}

401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419

/**
 * \brief read entire tensor with each processor (in packed layout).
 *         WARNING: will use a lot of memory. 
 * \param[in] tensor_id tensor handle
 * \param[out] num_pair number of values read
 * \param[in,out] preallocated mapped_data values read
 */
template<typename dtype>
int tCTF<dtype>::allread_tensor(int const   tensor_id, 
                                long_int *   num_pair, 
                                dtype *     all_data){
  int ret;
  long_int np;
  ret = dt->allread_tsr(tensor_id, &np, &all_data, 1);
  *num_pair = np;
  return ret;
}

420 421 422 423 424 425 426 427
/**
 * \brief read entire tensor with each processor (in packed layout).
 *         WARNING: will use a lot of memory. 
 * \param[in] tensor_id tensor handle
 * \param[out] num_pair number of values read
 * \param[in,out] mapped_data values read
 */
template<typename dtype>
428
int tCTF<dtype>::allread_tensor(int const   tensor_id, 
solomon's avatar
solomon committed
429
                                long_int *   num_pair, 
430 431 432
                                dtype **    all_data){
  int ret;
  long_int np;
433
  ret = dt->allread_tsr(tensor_id, &np, all_data, 0);
434
  CTF_untag_mem(*all_data);
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
  *num_pair = np;
  return ret;
}

/* input tensor local data or set buffer for contract answer. */
/*int tCTF<dtype>::set_local_tensor(int const   tensor_id, 
                         int const      num_val, 
                         dtype *        tsr_data){
  return set_tsr_data(tensor_id, num_val, tsr_data);  
}*/

/**
 * \brief  map input tensor local data to zero
 * \param[in] tensor_id tensor handle
 */
template<typename dtype>
int tCTF<dtype>::set_zero_tensor(int const tensor_id){
  return dt->set_zero_tsr(tensor_id);
}

/**
 * \brief read tensor data pairs local to processor. 
 * \param[in] tensor_id tensor handle
 * \param[out] num_pair number of values read
 * \param[out] mapped_data values read
 */
template<typename dtype>
int tCTF<dtype>::read_local_tensor(int const          tensor_id, 
solomon's avatar
solomon committed
463
                                   long_int *          num_pair,  
464 465 466 467
                                   tkv_pair<dtype> ** mapped_data){
  int ret;
  long_int np;
  ret = dt->read_local_pairs(tensor_id, &np, mapped_data);
468 469
  if (np > 0)
    CTF_untag_mem(*mapped_data);
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
  *num_pair = np;
  return ret;
}

/**
 * \brief contracts tensors alpha*A*B+beta*C -> C,
 *      uses standard symmetric contraction sequential kernel 
 * \param[in] type the contraction type (defines contraction actors)
 * \param[in] alpha scaling factor for A*B
 * \param[in] beta scaling factor for C
 */
template<typename dtype>
int tCTF<dtype>::contract(CTF_ctr_type_t const *  type,
                          dtype const             alpha,
                          dtype const             beta){
  fseq_tsr_ctr<dtype> fs;
  fs.func_ptr=sym_seq_ctr_ref<dtype>;
487
  return contract(type, fs, alpha, beta, 1);
488 489 490 491 492 493
}


/**
 * \brief contracts tensors alpha*A*B+beta*C -> C. 
        Accepts custom-sized buffer-space (set to NULL for dynamic allocs).
494
 *      seq_func used to perform sequential block op 
495
 * \param[in] type the contraction type (defines contraction actors)
496
 * \param[in] func_ptr sequential block ctr func pointer 
497 498 499 500 501 502 503 504 505
 * \param[in] alpha scaling factor for A*B
 * \param[in] beta scaling factor for C
 */
template<typename dtype>
int tCTF<dtype>::contract(CTF_ctr_type_t const *    type,
                          fseq_tsr_ctr<dtype> const func_ptr, 
                          dtype const               alpha,
                          dtype const               beta,
                          int const                 map_inner){
506
  int i, ret;
507 508
#if DEBUG >= 1
  if (dt->get_global_comm()->rank == 0)
509
    printf("Start head contraction :\n");
510
  dt->print_ctr(type,alpha,beta);
511
#endif
512 513 514
  fseq_elm_ctr<dtype> felm;
  felm.func_ptr = NULL;

515 516
  if ((*dt->get_tensors())[type->tid_A]->profile &&
      (*dt->get_tensors())[type->tid_B]->profile &&
517 518 519 520
      (*dt->get_tensors())[type->tid_C]->profile){
    char cname[200];
    cname[0] = '\0';
    if ((*dt->get_tensors())[type->tid_C]->name != NULL)
521
      sprintf(cname, "%s", (*dt->get_tensors())[type->tid_C]->name);
522 523 524 525 526 527 528 529 530 531 532
    else
      sprintf(cname, "%d", type->tid_C);
    sprintf(cname+strlen(cname),"[");
    for (i=0; i<(*dt->get_tensors())[type->tid_C]->ndim; i++){
      if (i>0)
        sprintf(cname+strlen(cname)," %d",type->idx_map_C[i]);
      else 
        sprintf(cname+strlen(cname),"%d",type->idx_map_C[i]);
    }
    sprintf(cname+strlen(cname),"]=");
    if ((*dt->get_tensors())[type->tid_A]->name != NULL)
533
      sprintf(cname+strlen(cname), "%s", (*dt->get_tensors())[type->tid_A]->name);
534 535 536 537 538 539 540 541 542 543 544
    else
      sprintf(cname+strlen(cname), "%d", type->tid_A);
    sprintf(cname+strlen(cname),"[");
    for (i=0; i<(*dt->get_tensors())[type->tid_A]->ndim; i++){
      if (i>0)
        sprintf(cname+strlen(cname)," %d",type->idx_map_A[i]);
      else
        sprintf(cname+strlen(cname),"%d",type->idx_map_A[i]);
    }
    sprintf(cname+strlen(cname),"]*");
    if ((*dt->get_tensors())[type->tid_B]->name != NULL)
545
      sprintf(cname+strlen(cname), "%s", (*dt->get_tensors())[type->tid_B]->name);
546 547 548 549 550 551 552 553 554 555
    else
      sprintf(cname+strlen(cname), "%d", type->tid_B);
    sprintf(cname+strlen(cname),"[");
    for (i=0; i<(*dt->get_tensors())[type->tid_B]->ndim; i++){
      if (i>0)
        sprintf(cname+strlen(cname)," %d",type->idx_map_B[i]);
      else 
        sprintf(cname+strlen(cname),"%d",type->idx_map_B[i]);
    }
    sprintf(cname+strlen(cname),"]");
556 557 558 559 560 561

    double dtt;
    if (dt->get_global_comm()->rank == 0){
      dtt = MPI_Wtime();
      VPRINTF(1,"Starting %s\n",cname);
    }
562 563 564 565 566
   
    CTF_Timer tctr(cname);
    tctr.start(); 
    ret = dt->home_contract(type, func_ptr, felm, alpha, beta, map_inner);
    tctr.stop();
567 568
    if (dt->get_global_comm()->rank == 0){
      VPRINTF(1,"Ended %s in %lf seconds\n",cname,MPI_Wtime()-dtt);   }
569 570
  } else 
    ret = dt->home_contract(type, func_ptr, felm, alpha, beta, map_inner);
571 572 573 574
  if ((*dt->get_tensors())[type->tid_A]->profile &&
      (*dt->get_tensors())[type->tid_B]->profile &&
      (*dt->get_tensors())[type->tid_C]->profile){
  }
575 576 577 578
#if DEBUG >= 1
  if (dt->get_global_comm()->rank == 0)
    printf("End head contraction :\n");
#endif
579 580 581

  return ret;

582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
}
    
/**
 * \brief contracts tensors alpha*A*B+beta*C -> C. 
        Accepts custom-sized buffer-space (set to NULL for dynamic allocs).
 *      seq_func used to perform elementwise sequential op 
 * \param[in] type the contraction type (defines contraction actors)
 * \param[in] felm sequential elementwise ctr func ptr
 * \param[in] alpha scaling factor for A*B
 * \param[in] beta scaling factor for C
 */
template<typename dtype>
int tCTF<dtype>::contract(CTF_ctr_type_t const *     type,
                          fseq_elm_ctr<dtype> const  felm,
                          dtype const                alpha,
                          dtype const                beta){
#if DEBUG >= 1
  if (dt->get_global_comm()->rank == 0)
600
    printf("Start head custom contraction:\n");
601 602 603 604
  dt->print_ctr(type,alpha,beta);
#endif
  fseq_tsr_ctr<dtype> fs;
  fs.func_ptr=sym_seq_ctr_ref<dtype>;
605 606 607 608 609 610
  int ret = dt->home_contract(type, fs, felm, alpha, beta, 0);
#if DEBUG >= 1
  if (dt->get_global_comm()->rank == 0)
    printf("End head custom contraction.\n");
#endif
  return ret;
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644

}

/**
 * \brief copy tensor from one handle to another
 * \param[in] tid_A tensor handle to copy from
 * \param[in] tid_B tensor handle to copy to
 */
template<typename dtype>
int tCTF<dtype>::copy_tensor(int const tid_A, int const tid_B){
  return dt->cpy_tsr(tid_A, tid_B);
}

/**
 * \brief scales a tensor by alpha
 * \param[in] alpha scaling factor
 * \param[in] tid tensor handle
 */
template<typename dtype>
int tCTF<dtype>::scale_tensor(dtype const alpha, int const tid){
  return dt->scale_tsr(alpha, tid);
}
/**
 * \brief scales a tensor by alpha iterating on idx_map
 * \param[in] alpha scaling factor
 * \param[in] tid tensor handle
 * \param[in] idx_map indexer to the tensor
 */
template<typename dtype>
int tCTF<dtype>::scale_tensor(dtype const               alpha, 
                              int const                 tid, 
                              int const *               idx_map){
  fseq_tsr_scl<dtype> fs;
  fs.func_ptr=sym_seq_scl_ref<dtype>;
645 646 647
  fseq_elm_scl<dtype> felm;
  felm.func_ptr = NULL;
  return dt->scale_tsr(alpha, tid, idx_map, fs, felm);
648 649 650 651 652 653 654 655 656 657 658 659 660 661
}

/**
 * \brief scales a tensor by alpha iterating on idx_map
 * \param[in] alpha scaling factor
 * \param[in] tid tensor handle
 * \param[in] idx_map indexer to the tensor
 * \param[in] func_ptr pointer to sequential scale function
 */
template<typename dtype>
int tCTF<dtype>::scale_tensor(dtype const               alpha, 
                              int const                 tid, 
                              int const *               idx_map,
                              fseq_tsr_scl<dtype> const func_ptr){
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
  fseq_elm_scl<dtype> felm;
  felm.func_ptr = NULL;
  return dt->scale_tsr(alpha, tid, idx_map, func_ptr, felm);
}

/**
 * \brief scales a tensor by alpha iterating on idx_map
 * \param[in] alpha scaling factor
 * \param[in] tid tensor handle
 * \param[in] idx_map indexer to the tensor
 * \param[in] felm pointer to sequential elemtwise scale function
 */
template<typename dtype>
int tCTF<dtype>::scale_tensor(dtype const               alpha, 
                              int const                 tid, 
                              int const *               idx_map,
                              fseq_elm_scl<dtype> const felm){
  fseq_tsr_scl<dtype> fs;
  fs.func_ptr=sym_seq_scl_ref<dtype>;
  return dt->scale_tsr(alpha, tid, idx_map, fs, felm);
}
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727

  /**
   * \brief computes a dot product of two tensors A dot B
   * \param[in] tid_A tensor handle to A
 * \param[in] tid_B tensor handle to B
 * \param[out] product the result of the dot-product
 */
template<typename dtype>
int tCTF<dtype>::dot_tensor(int const tid_A, int const tid_B, dtype *product){
  int stat;
  /* check if the mappings of A and B are the same */
  stat = dt->check_pair_mapping(tid_A, tid_B);
  if (stat == 0){
    /* Align the mappings of A and B */
    stat = dt->map_tensor_pair(tid_A, tid_B);
    if (stat != DIST_TENSOR_SUCCESS)
      return stat;
  }
  /* Compute the dot product of A and B */
  return dt->dot_loc_tsr(tid_A, tid_B, product);
}

/**
 * \brief Performs an elementwise reduction on a tensor 
 * \param[in] tid tensor handle
 * \param[in] CTF::OP reduction operation to apply
 * \param[out] result result of reduction operation
 */
template<typename dtype>
int tCTF<dtype>::reduce_tensor(int const tid, CTF_OP op, dtype * result){
  return dt->red_tsr(tid, op, result);
}

/**
 * \brief Calls a mapping function on each element of the tensor 
 * \param[in] tid tensor handle
 * \param[in] map_func function pointer to apply to each element
 */
template<typename dtype>
int tCTF<dtype>::map_tensor(int const tid, 
                            dtype (*map_func)(int const   ndim, 
                                              int const * indices, 
                                              dtype const elem)){
  return dt->map_tsr(tid, map_func);
}
728 729 730 731 732 733 734 735 736 737 738 739 740 741

/**
 * \brief obtains a small number of the biggest elements of the 
 *        tensor in sorted order (e.g. eigenvalues)
 * \param[in] tid index of tensor
 * \param[in] n number of elements to collect
 * \param[in] data output data (should be preallocated to size at least n)
 */
template<typename dtype>
int tCTF<dtype>::get_max_abs(int const  tid,
                             int const  n,
                             dtype *    data){
  return dt->get_max_abs(tid, n, data);
}
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
    
/**
 * \brief DAXPY: a*idx_map_A(A) + b*idx_map_B(B) -> idx_map_B(B). 
 *               uses standard summation pointer
 * \param[in] type idx_maps and tids of contraction
 * \param[in] alpha scaling factor for A*B
 * \param[in] beta scaling factor for C
 */
template<typename dtype>
int tCTF<dtype>::sum_tensors(CTF_sum_type_t const * type,
                             dtype const            alpha,
                             dtype const            beta){
  
  fseq_tsr_sum<dtype> fs;
  fs.func_ptr=sym_seq_sum_ref<dtype>;
  return sum_tensors(alpha, beta, type->tid_A, type->tid_B, 
                     type->idx_map_A, type->idx_map_B, fs);

}
    
/**
 * \brief DAXPY: a*idx_map_A(A) + b*idx_map_B(B) -> idx_map_B(B). 
 * \param[in] type idx_maps and tids of contraction
 * \param[in] alpha scaling factor for A*B
 * \param[in] beta scaling factor for C
 * \param[in] func_ptr sequential ctr func pointer 
 */
template<typename dtype>
int tCTF<dtype>::sum_tensors(CTF_sum_type_t const *     type,
                             dtype const                alpha,
                             dtype const                beta,
                             fseq_tsr_sum<dtype> const  func_ptr){
  return sum_tensors(alpha, beta, type->tid_A, type->tid_B, 
775
                     type->idx_map_A, type->idx_map_B, func_ptr);
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796

}

/**
 * \brief DAXPY: a*idx_map_A(A) + b*idx_map_B(B) -> idx_map_B(B). 
 * \param[in] alpha scaling factor for A*B
 * \param[in] beta scaling factor for C
 * \param[in] tid_A tensor handle to A
 * \param[in] tid_B tensor handle to B
 * \param[in] idx_map_A index map of A
 * \param[in] idx_map_B index map of B
 * \param[in] func_ptr sequential ctr func pointer 
 */
template<typename dtype>
int tCTF<dtype>::sum_tensors(dtype const                alpha,
                             dtype const                beta,
                             int const                  tid_A,
                             int const                  tid_B,
                             int const *                idx_map_A,
                             int const *                idx_map_B,
                             fseq_tsr_sum<dtype> const  func_ptr){
797 798
  fseq_elm_sum<dtype> felm;
  felm.func_ptr = NULL;
799
  return dt->home_sum_tsr(alpha, beta, tid_A, tid_B, idx_map_A, idx_map_B, func_ptr, felm);
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
}

/**
 * \brief DAXPY: a*idx_map_A(A) + b*idx_map_B(B) -> idx_map_B(B). 
 * \param[in] alpha scaling factor for A*B
 * \param[in] beta scaling factor for C
 * \param[in] tid_A tensor handle to A
 * \param[in] tid_B tensor handle to B
 * \param[in] idx_map_A index map of A
 * \param[in] idx_map_B index map of B
 * \param[in] func_ptr sequential ctr func pointer 
 */
template<typename dtype>
int tCTF<dtype>::sum_tensors(dtype const                alpha,
                             dtype const                beta,
                             int const                  tid_A,
                             int const                  tid_B,
                             int const *                idx_map_A,
                             int const *                idx_map_B,
                             fseq_elm_sum<dtype> const  felm){
  fseq_tsr_sum<dtype> fs;
  fs.func_ptr=sym_seq_sum_ref<dtype>;
822
  return dt->home_sum_tsr(alpha, beta, tid_A, tid_B, idx_map_A, idx_map_B, fs, felm);
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
}

/**
 * \brief daxpy tensors A and B, B = B+alpha*A
 * \param[in] alpha scaling factor
 * \param[in] tid_A tensor handle of A
 * \param[in] tid_B tensor handle of B
 */
template<typename dtype>
int tCTF<dtype>::sum_tensors(dtype const  alpha,
                             int const    tid_A,
                             int const    tid_B){
  int stat;
  
  /* check if the mappings of A and B are the same */
  stat = dt->check_pair_mapping(tid_A, tid_B);
  if (stat == 0){
    /* Align the mappings of A and B */
    stat = dt->map_tensor_pair(tid_A, tid_B);
    if (stat != DIST_TENSOR_SUCCESS)
      return stat;
  }
  /* Sum tensors */
  return dt->daxpy_local_tensor_pair(alpha, tid_A, tid_B);
}

Edgar Solomonik's avatar
Edgar Solomonik committed
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
/**
 * \brief align mapping of tensor A to that of B
 * \param[in] tid_A tensor handle of A
 * \param[in] tid_B tensor handle of B
 */
template<typename dtype>
int tCTF<dtype>::align(int const    tid_A,
                       int const    tid_B){
  int stat;
  
  /* check if the mappings of A and B are the same */
  stat = dt->check_pair_mapping(tid_A, tid_B);
  if (stat == 0){
    /* Align the mappings of A and B */
    stat = dt->map_tensor_pair(tid_B, tid_A);
    if (stat != DIST_TENSOR_SUCCESS)
      return stat;
  }
  return DIST_TENSOR_SUCCESS;
}

870
template<typename dtype>
871 872
int tCTF<dtype>::print_tensor(FILE * stream, int const tid, double cutoff) {
  return dt->print_tsr(stream, tid, cutoff);
873
}
874 875

template<typename dtype>
876
int tCTF<dtype>::compare_tensor(FILE * stream, int const tid_A, int const tid_B, double cutoff) {
877 878
  int stat = align(tid_A, tid_B);
  if (stat != DIST_TENSOR_SUCCESS) return stat;
879
  return dt->compare_tsr(stream, tid_A, tid_B, cutoff);
880 881
}

882 883
/* Prints contraction type. */
template<typename dtype>
884 885 886 887
int tCTF<dtype>::print_ctr(CTF_ctr_type_t const * ctype,
                           dtype const            alpha,
                           dtype const            beta) const {
  return dt->print_ctr(ctype,alpha,beta);
888 889 890 891
}

/* Prints sum type. */
template<typename dtype>
892 893 894 895
int tCTF<dtype>::print_sum(CTF_sum_type_t const * stype,
                           dtype const            alpha,
                           dtype const            beta) const {
  return dt->print_sum(stype,alpha,beta);
896 897 898 899 900 901 902 903 904 905 906 907
}


/**
 * \brief removes all tensors, invalidates all handles
 */
template<typename dtype>
int tCTF<dtype>::clean_tensors(){
  unsigned int i;
  std::vector< tensor<dtype>* > * tensors = dt->get_tensors();
  for (i=0; i<tensors->size(); i++){
    dt->del_tsr(i);
908
//    CTF_free((*tensors)[i]);
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
  }
  tensors->clear();
  return DIST_TENSOR_SUCCESS;
}

/**
 * \brief removes a tensor, invalidates its handle
 * \param tid tensor handle
 */
template<typename dtype>
int tCTF<dtype>::clean_tensor(int const tid){
  return dt->del_tsr(tid);
}

/**
 * \brief removes all tensors, invalidates all handles, and exits library.
 *              Do not use library instance after executing this.
 */
template<typename dtype>
int tCTF<dtype>::exit(){
  int ret;
  if (initialized){
931
    TAU_FSTOP(CTF);
932
#ifdef HPM
Edgar Solomonik's avatar
Edgar Solomonik committed
933
    HPM_Stop("CTF");
934
#endif
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
    ret = tCTF<dtype>::clean_tensors();
    LIBT_ASSERT(ret == DIST_TENSOR_SUCCESS);
    delete dt;
    initialized = 0;
    return ret;
  } else
    return DIST_TENSOR_SUCCESS;
}

/* \brief ScaLAPACK back-end, see their DOC */
template<typename dtype>
int tCTF<dtype>::pgemm(char const   TRANSA, 
                       char const   TRANSB, 
                       int const    M, 
                       int const    N, 
                       int const    K, 
                       dtype const  ALPHA,
                       dtype *      A, 
                       int const    IA, 
                       int const    JA, 
                       int const *  DESCA, 
                       dtype *      B, 
                       int const    IB, 
                       int const    JB, 
                       int const *  DESCB, 
                       dtype const  BETA,
                       dtype *      C, 
                       int const    IC, 
                       int const    JC, 
                       int const *  DESCC){
  int ret, need_remap, i, j;
#if (!REDIST)
  int redist;
#endif
  int stid_A, stid_B, stid_C;
  int otid_A, otid_B, otid_C;
  long_int old_size_C;
  int * old_phase_C, * old_rank_C, * old_virt_dim_C, * old_pe_lda_C;
  int * old_padding_C, * old_edge_len_C;
  int * need_free;
  int was_padded_C, was_cyclic_C;
  tensor<dtype> * tsr_nC, * tsr_oC;
  CTF_ctr_type ct;
  fseq_tsr_ctr<dtype> fs;
  std::vector< tensor<dtype>* > * tensors = dt->get_tensors();
980
  CTF_alloc_ptr(3*sizeof(int), (void**)&need_free);
981 982 983 984 985 986 987 988 989 990 991 992
  ret = dt->pgemm(TRANSA, TRANSB, M, N, K, ALPHA, A, IA, JA, DESCA,
                  B, IB, JB, DESCB,
                  BETA, C, IC, JC, DESCC, &ct, &fs, need_free);
  if (ret != DIST_TENSOR_SUCCESS)
    return ret;

  otid_A = ct.tid_A;
  otid_B = ct.tid_B;
  otid_C = ct.tid_C;
#if (!REDIST)
  ret = dt->try_topo_morph(otid_A, otid_B, otid_C);
  LIBT_ASSERT(ret == DIST_TENSOR_SUCCESS);
993 994 995
/*  dt->print_map(stdout, otid_A);
  dt->print_map(stdout, otid_B);
  dt->print_map(stdout, otid_C);*/
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
  redist = dt->check_contraction_mapping(&ct);
  if (redist == 0) {
    printf("REDISTRIBUTING\n");
#endif
    clone_tensor(ct.tid_A, 1, &stid_A);
    clone_tensor(ct.tid_B, 1, &stid_B);
    clone_tensor(ct.tid_C, 1, &stid_C);
    ct.tid_A = stid_A;
    ct.tid_B = stid_B;
    ct.tid_C = stid_C;
#if (!REDIST)
  }
#endif

1010
  ret = this->contract(&ct, fs, ALPHA, BETA);
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
  if (ret != DIST_TENSOR_SUCCESS)
    return ret;
#if (!REDIST)
  if (redist == 0){
#endif
    tsr_oC = (*tensors)[otid_C];
    tsr_nC = (*tensors)[stid_C];
    need_remap = 0;
    if (tsr_oC->itopo == tsr_nC->itopo){
      if (!comp_dim_map(&tsr_oC->edge_map[0],&tsr_nC->edge_map[0]))
        need_remap = 1;
      if (!comp_dim_map(&tsr_oC->edge_map[1],&tsr_nC->edge_map[1]))
        need_remap = 1;
    } else
      need_remap = 1;
    if (need_remap){
      save_mapping(tsr_nC, &old_phase_C, &old_rank_C, &old_virt_dim_C, 
                   &old_pe_lda_C, &old_size_C, &was_padded_C, &was_cyclic_C, 
                   &old_padding_C, &old_edge_len_C, 
                   dt->get_topo(tsr_nC->itopo));
      if (need_free[2])
1032
        CTF_free(tsr_oC->data);
1033 1034 1035 1036 1037 1038 1039
      tsr_oC->data = tsr_nC->data;
      remap_tensor(otid_C, tsr_oC, dt->get_topo(tsr_oC->itopo), old_size_C, 
                   old_phase_C, old_rank_C, old_virt_dim_C, 
                   old_pe_lda_C, was_padded_C, was_cyclic_C, 
                   old_padding_C, old_edge_len_C, dt->get_global_comm());
    } else{
      if (need_free[2])
1040
              CTF_free(tsr_oC->data);
1041 1042 1043 1044 1045 1046
      tsr_oC->data = tsr_nC->data;
    }
    /* If this process owns any data */
    if (!need_free[2]){
      memcpy(C,tsr_oC->data,tsr_oC->size*sizeof(dtype));
    } else
1047
      CTF_free(tsr_oC->data);
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
    if (need_free[0])
      dt->del_tsr(otid_A);
    if (need_free[1])
      dt->del_tsr(otid_B);
    dt->del_tsr(stid_A);
    dt->del_tsr(stid_B);
    (*tensors)[stid_A]->is_alloced = 0;
    (*tensors)[stid_B]->is_alloced = 0;
    (*tensors)[stid_C]->is_alloced = 0;
#if (!REDIST)
  }
#endif
1060 1061
  if ((*tensors)[otid_A]->scp_padding[0] != 0 ||
      (*tensors)[otid_A]->scp_padding[1] != 0){
1062
    CTF_free((*tensors)[otid_A]->data);
1063
  }
1064 1065
  if ((*tensors)[otid_B]->scp_padding[0] != 0 ||
      (*tensors)[otid_B]->scp_padding[1] != 0){
1066
    CTF_free((*tensors)[otid_B]->data);
1067
  }
1068 1069
  if ((*tensors)[otid_C]->scp_padding[0] != 0 ||
      (*tensors)[otid_C]->scp_padding[1] != 0){
1070 1071 1072
    int brow, bcol;
    brow = DESCC[4];
    bcol = DESCC[5];
1073 1074 1075
    for (i=0; i<bcol-(*tensors)[otid_C]->scp_padding[1]; i++){
      for (j=0; j<brow-(*tensors)[otid_C]->scp_padding[0]; j++){
        C[i*(brow-(*tensors)[otid_C]->scp_padding[0])+j] 
1076 1077 1078
          = (*tensors)[otid_C]->data[i*brow+j];
      }
    }
1079
    CTF_free((*tensors)[otid_C]->data);
1080
  }
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
  (*tensors)[otid_A]->is_data_aliased = 1;
  (*tensors)[otid_B]->is_data_aliased = 1;
  (*tensors)[otid_C]->is_data_aliased = 1;
  dt->del_tsr(otid_A);
  dt->del_tsr(otid_B);
  dt->del_tsr(otid_C);
  CTF_free(ct.idx_map_A);
  CTF_free(ct.idx_map_B);
  CTF_free(ct.idx_map_C);
  CTF_free(need_free);
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
  return DIST_TENSOR_SUCCESS;
}


/**
 * \brief define matrix from ScaLAPACK descriptor
 *
 * \param[in] DESCA ScaLAPACK descriptor for a matrix
 * \param[in] data pointer to actual data
 * \param[out] tid tensor handle
 */
template<typename dtype>
int tCTF<dtype>::def_scala_mat(int const * DESCA,
                               dtype const * data,
                               int * tid){
  int ret, stid;
  ret = dt->load_matrix((dtype*)data, DESCA, &stid, NULL);
  if (ret != DIST_TENSOR_SUCCESS) return ret;
  clone_tensor(stid, 1, tid);
  std::vector< tensor<dtype>* > * tensors = dt->get_tensors();
  tensor<dtype> * stsr = (*tensors)[stid];
  tensor<dtype> * tsr = (*tensors)[*tid];
1113
  CTF_free(stsr->data);
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
  stsr->is_alloced = 0;
  tsr->is_matrix = 1;
  tsr->slay = stid;
  return DIST_TENSOR_SUCCESS;
}

/**
 * \brief reads a ScaLAPACK matrix to the original data pointer
 *
 * \param[in] tid tensor handle
 * \param[in,out] data pointer to buffer data
 */
template<typename dtype>
int tCTF<dtype>::read_scala_mat(int const tid,
                                dtype * data){
  int * old_phase, * old_rank, * old_virt_dim, * old_pe_lda;
  int * old_padding, * old_edge_len;
  int was_padded, was_cyclic;
  long_int old_size;
  std::vector< tensor<dtype>* > * tensors = dt->get_tensors();
  tensor<dtype> * tsr = (*tensors)[tid];
  tensor<dtype> * stsr = (*tensors)[tsr->slay];
  dt->unmap_inner(tsr);
  save_mapping(tsr, &old_phase, &old_rank, &old_virt_dim, 
               &old_pe_lda, &old_size, &was_padded, &was_cyclic, 
               &old_padding, &old_edge_len, 
               dt->get_topo(tsr->itopo));
  LIBT_ASSERT(tsr->is_matrix);
1142
  CTF_alloc_ptr(sizeof(dtype)*tsr->size, (void**)&stsr->data);
1143 1144 1145 1146 1147 1148 1149
  memcpy(stsr->data, tsr->data, sizeof(dtype)*tsr->size);
  remap_tensor(tsr->slay, stsr, dt->get_topo(stsr->itopo), old_size, 
               old_phase, old_rank, old_virt_dim, 
               old_pe_lda, was_padded, was_cyclic, 
               old_padding, old_edge_len, dt->get_global_comm());
  if (data!=NULL)
    memcpy(data, stsr->data, stsr->size*sizeof(dtype));  
1150
  CTF_free(stsr->data);
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
  return DIST_TENSOR_SUCCESS;
}
/**
 * \brief CTF interface for pgemm
 */
template<typename dtype>
int tCTF<dtype>::pgemm(char const   TRANSA, 
                       char const   TRANSB, 
                       dtype const  ALPHA,
                       int const    tid_A,
                       int const    tid_B,
                       dtype const  BETA,
                       int const    tid_C){
  int herm_A, herm_B, ret;
  CTF_ctr_type ct;
  fseq_tsr_ctr<dtype> fs;
  ct.tid_A = tid_A;
  ct.tid_B = tid_B;
  ct.tid_C = tid_C;

1171 1172 1173
  ct.idx_map_A = (int*)CTF_alloc(sizeof(int)*2);
  ct.idx_map_B = (int*)CTF_alloc(sizeof(int)*2);
  ct.idx_map_C = (int*)CTF_alloc(sizeof(int)*2);
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
  ct.idx_map_C[0] = 1;
  ct.idx_map_C[1] = 2;
  herm_A = 0;
  herm_B = 0;
  if (TRANSA == 'N' || TRANSA == 'n'){
    ct.idx_map_A[0] = 1;
    ct.idx_map_A[1] = 0;
  } else {
    LIBT_ASSERT(TRANSA == 'T' || TRANSA == 't' || TRANSA == 'c' || TRANSA == 'C');
    if (TRANSA == 'c' || TRANSA == 'C')
      herm_A = 1;
    ct.idx_map_A[0] = 0;
    ct.idx_map_A[1] = 1;
  }
  if (TRANSB == 'N' || TRANSB == 'n'){
    ct.idx_map_B[0] = 0;
    ct.idx_map_B[1] = 2;
  } else {
    LIBT_ASSERT(TRANSB == 'T' || TRANSB == 't' || TRANSB == 'c' || TRANSB == 'C');
    if (TRANSB == 'c' || TRANSB == 'C')
      herm_B = 1;
    ct.idx_map_B[0] = 2;
    ct.idx_map_B[1] = 0;
  }
  if (herm_A && herm_B)
    fs.func_ptr = &gemm_ctr<dtype,1,1>;
  else if (herm_A)
    fs.func_ptr = &gemm_ctr<dtype,1,0>;
  else if (herm_B)
    fs.func_ptr = &gemm_ctr<dtype,0,1>;
  else
    fs.func_ptr = &gemm_ctr<dtype,0,0>;
1206
  ret = this->contract(&ct, fs, ALPHA, BETA);
1207 1208 1209
  CTF_free(ct.idx_map_A);
  CTF_free(ct.idx_map_B);
  CTF_free(ct.idx_map_C);
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
  return ret;
};
  
/* Instantiate the ugly templates */
template class tCTF<double>;
#if (VERIFY==0)
template class tCTF< std::complex<double> >;
#endif