Tensor Network Theory Library  Beta release 1.2.1 A library of routines for performing TNT-based operations
1 /*
2 Authors: Sarah Al-Assam, Stephen Clark and Dieter Jaksch
3 Date: $LastChangedDate: 2015-05-14 13:43:34 +0100 (Thu, 14 May 2015)$
4 (c) University of Oxford 2014
5 */
6
13 /*-------------------------------------------*/
14
18
24 #ifdef MAKINGPUBLICDOCS
26  int varA,
27  int varB,
28  int varC,
29  int varD,
30  int varE)
31 #else
33  int *varA,
34  const char *varnameA,
35  int *varB,
36  const char *varnameB,
37  int *varC,
38  const char *varnameC,
39  int *varD,
40  const char *varnameD,
41  int *varE,
42  const char *varnameE)
43 #endif
44 {
45  int *vars[TNT_MAX_NUMIO] = {varA,varB,varC,varD,varE};
46  const char *varnames[TNT_MAX_NUMIO] = {varnameA,varnameB,varnameC,varnameD,varnameE};
47  unsigned numint = _tnt_find_num_unique(varnames);
48  TNT_ERR_RET_DEFS
49
50  ret = _tnt_load_intparams(loadname, numint, varnames, vars);
51  TNT_PUB_N_ERR_CHK("tntIntParamsLoad") /* NO_COVERAGE */
52
53 }
54
60 #ifdef MAKINGPUBLICDOCS
62  double varA,
63  double varB,
64  double varC,
65  double varD,
66  double varE)
67 #else
69  double *varA,
70  const char *varnameA,
71  double *varB,
72  const char *varnameB,
73  double *varC,
74  const char *varnameC,
75  double *varD,
76  const char *varnameD,
77  double *varE,
78  const char *varnameE)
79 #endif
80 {
81  double *vars[TNT_MAX_NUMIO] = {varA,varB,varC,varD,varE};
82  const char *varnames[TNT_MAX_NUMIO] = {varnameA,varnameB,varnameC,varnameD,varnameE};
83  unsigned numdbl = _tnt_find_num_unique(varnames);
84  TNT_ERR_RET_DEFS
85
86  ret = _tnt_load_dblparams(loadname, numdbl, varnames, vars);
87  TNT_PUB_N_ERR_CHK("tntDoubleParamsLoad") /* NO_COVERAGE */
88
89 }
90
91
97 #ifdef MAKINGPUBLICDOCS
99  tntComplex varA,
100  tntComplex varB,
101  tntComplex varC,
102  tntComplex varD,
103  tntComplex varE)
104 #else
106  tntComplex *varA,
107  const char *varnameA,
108  tntComplex *varB,
109  const char *varnameB,
110  tntComplex *varC,
111  const char *varnameC,
112  tntComplex *varD,
113  const char *varnameD,
114  tntComplex *varE,
115  const char *varnameE)
116 #endif
117 {
118  tntComplex *vars[TNT_MAX_NUMIO] = {varA,varB,varC,varD,varE};
119  const char *varnames[TNT_MAX_NUMIO] = {varnameA,varnameB,varnameC,varnameD,varnameE};
120  unsigned numcmp = _tnt_find_num_unique(varnames);
121  TNT_ERR_RET_DEFS
122
123  ret = _tnt_load_cmpparams(loadname, numcmp, varnames, vars);
124  TNT_PUB_N_ERR_CHK("tntComplexParamsLoad") /* NO_COVERAGE */
125 }
126
132 #ifdef MAKINGPUBLICDOCS
134  char *varA,
135  char *varB,
136  char *varC,
137  char *varD,
138  char *varE)
139 #else
141  char *varA,
142  const char *varnameA,
143  char *varB,
144  const char *varnameB,
145  char *varC,
146  const char *varnameC,
147  char *varD,
148  const char *varnameD,
149  char *varE,
150  const char *varnameE)
151 #endif
152 {
153  char *vars[TNT_MAX_NUMIO] = {varA,varB,varC,varD,varE};
154  const char *varnames[TNT_MAX_NUMIO] = {varnameA,varnameB,varnameC,varnameD,varnameE};
155  unsigned numstrs = _tnt_find_num_unique(varnames);
156  TNT_ERR_RET_DEFS
157
158  ret = _tnt_load_strs(loadname, numstrs, varnames, vars);
159  TNT_PUB_N_ERR_CHK("tntStringsLoad") /* NO_COVERAGE */
160 }
161
162
168 #ifdef MAKINGPUBLICDOCS
170  tntIntArray arrA,
171  tntIntArray arrB,
172  tntIntArray arrC,
173  tntIntArray arrD,
174  tntIntArray arrE)
175 #else
177  tntIntArray *arrA,
178  const char *varnameA,
179  tntIntArray *arrB,
180  const char *varnameB,
181  tntIntArray *arrC,
182  const char *varnameC,
183  tntIntArray *arrD,
184  const char *varnameD,
185  tntIntArray *arrE,
186  const char *varnameE)
187 #endif
188 {
189  tntIntArray *arrs[TNT_MAX_NUMIO] = {arrA,arrB,arrC,arrD,arrE};
190  const char *varnames[TNT_MAX_NUMIO] = {varnameA,varnameB,varnameC,varnameD,varnameE};
191  unsigned numint = _tnt_find_num_unique(varnames);
192  TNT_ERR_RET_DEFS
193
194  ret = _tnt_load_intarrs(loadname, numint, varnames, arrs);
195  TNT_PUB_N_ERR_CHK("tntIntArraysLoad") /* NO_COVERAGE */
196
197 }
198
204 #ifdef MAKINGPUBLICDOCS
206  tntDoubleArray arrA,
207  tntDoubleArray arrB,
208  tntDoubleArray arrC,
209  tntDoubleArray arrD,
210  tntDoubleArray arrE)
211 #else
213  tntDoubleArray *arrA,
214  const char *varnameA,
215  tntDoubleArray *arrB,
216  const char *varnameB,
217  tntDoubleArray *arrC,
218  const char *varnameC,
219  tntDoubleArray *arrD,
220  const char *varnameD,
221  tntDoubleArray *arrE,
222  const char *varnameE)
223 #endif
224 {
225  tntDoubleArray *arrs[TNT_MAX_NUMIO] = {arrA,arrB,arrC,arrD,arrE};
226  const char *varnames[TNT_MAX_NUMIO] = {varnameA,varnameB,varnameC,varnameD,varnameE};
227  unsigned numdbl = _tnt_find_num_unique(varnames);
228  TNT_ERR_RET_DEFS
229
230  ret = _tnt_load_dblarrs(loadname, numdbl, varnames, arrs);
231  TNT_PUB_N_ERR_CHK("tntDoubleArraysLoad") /* NO_COVERAGE */
232
233 }
234
240 #ifdef MAKINGPUBLICDOCS
242  tntComplexArray arrA,
243  tntComplexArray arrB,
244  tntComplexArray arrC,
245  tntComplexArray arrD,
246  tntComplexArray arrE)
247 #else
249  tntComplexArray *arrA,
250  const char *varnameA,
251  tntComplexArray *arrB,
252  const char *varnameB,
253  tntComplexArray *arrC,
254  const char *varnameC,
255  tntComplexArray *arrD,
256  const char *varnameD,
257  tntComplexArray *arrE,
258  const char *varnameE)
259 #endif
260 {
261  tntComplexArray *arrs[TNT_MAX_NUMIO] = {arrA,arrB,arrC,arrD,arrE};
262  const char *varnames[TNT_MAX_NUMIO] = {varnameA,varnameB,varnameC,varnameD,varnameE};
263  unsigned numcmp = _tnt_find_num_unique(varnames);
264  TNT_ERR_RET_DEFS
265
266  ret = _tnt_load_cmparrs(loadname, numcmp, varnames, arrs);
267  TNT_PUB_N_ERR_CHK("tntComplexArraysLoad") /* NO_COVERAGE */
268 }
269
275 #ifdef MAKINGPUBLICDOCS
277  tntNode varA,
278  tntNode varB,
279  tntNode varC,
280  tntNode varD,
281  tntNode varE)
282 #else
284  tntNode *varA,
285  const char *varnameA,
286  tntNode *varB,
287  const char *varnameB,
288  tntNode *varC,
289  const char *varnameC,
290  tntNode *varD,
291  const char *varnameD,
292  tntNode *varE,
293  const char *varnameE)
294 #endif
295 {
296  tntNode *vars[TNT_MAX_NUMIO] = {varA,varB,varC,varD,varE};
297  const char *varnames[TNT_MAX_NUMIO] = {varnameA,varnameB,varnameC,varnameD,varnameE};
298  unsigned numnodes = _tnt_find_num_unique(varnames);
299  TNT_ERR_RET_DEFS
300
301  ret = _tnt_load_nodes(loadname, numnodes, varnames, vars);
302  TNT_PUB_N_ERR_CHK("tntNodesLoad") /* NO_COVERAGE */
303 }
304
310 #ifdef MAKINGPUBLICDOCS
312  tntNodeArray arrA,
313  tntNodeArray arrB,
314  tntNodeArray arrC,
315  tntNodeArray arrD,
316  tntNodeArray arrE)
317 #else
319  tntNodeArray *arrA,
320  const char *varnameA,
321  tntNodeArray *arrB,
322  const char *varnameB,
323  tntNodeArray *arrC,
324  const char *varnameC,
325  tntNodeArray *arrD,
326  const char *varnameD,
327  tntNodeArray *arrE,
328  const char *varnameE)
329 #endif
330 {
331  tntNodeArray *arrs[TNT_MAX_NUMIO] = {arrA,arrB,arrC,arrD,arrE};
332  const char *varnames[TNT_MAX_NUMIO] = {varnameA,varnameB,varnameC,varnameD,varnameE};
333  unsigned numarrs = _tnt_find_num_unique(varnames);
334  TNT_ERR_RET_DEFS
335
336  ret = _tnt_load_nodearrs(loadname, numarrs, varnames, arrs);
337  TNT_PUB_N_ERR_CHK("tntNodeArraysLoad"); /* NO_COVERAGE */
338 }
339
345 #ifdef MAKINGPUBLICDOCS
347  tntExOp varA,
348  tntExOp varB,
349  tntExOp varC,
350  tntExOp varD,
351  tntExOp varE)
352 #else
354  tntExOp *varA,
355  const char *varnameA,
356  tntExOp *varB,
357  const char *varnameB,
358  tntExOp *varC,
359  const char *varnameC,
360  tntExOp *varD,
361  const char *varnameD,
362  tntExOp *varE,
363  const char *varnameE)
364 #endif
365 {
366  tntExOp *vars[TNT_MAX_NUMIO] = {varA,varB,varC,varD,varE};
367  const char *varnames[TNT_MAX_NUMIO] = {varnameA,varnameB,varnameC,varnameD,varnameE};
368  unsigned numexs = _tnt_find_num_unique(varnames);
369  TNT_ERR_RET_DEFS
370
371  ret = _tnt_load_exops(loadname, numexs, varnames, vars);
372  TNT_PUB_N_ERR_CHK("tntExOpsLoad"); /* NO_COVERAGE */
373 }
374
380 #ifdef MAKINGPUBLICDOCS
382  tntNetwork varA,
383  tntNetwork varB,
384  tntNetwork varC,
385  tntNetwork varD,
386  tntNetwork varE)
387 #else
389  tntNetwork *varA,
390  const char *varnameA,
391  tntNetwork *varB,
392  const char *varnameB,
393  tntNetwork *varC,
394  const char *varnameC,
395  tntNetwork *varD,
396  const char *varnameD,
397  tntNetwork *varE,
398  const char *varnameE)
399 #endif
400 {
401  tntNetwork *vars[TNT_MAX_NUMIO] = {varA,varB,varC,varD,varE};
402  const char *varnames[TNT_MAX_NUMIO] = {varnameA,varnameB,varnameC,varnameD,varnameE};
403  unsigned numnws = _tnt_find_num_unique(varnames);
404  TNT_ERR_RET_DEFS
405
406  ret = _tnt_load_networks(loadname, numnws, varnames, vars);
407  TNT_PUB_N_ERR_CHK("tntNetworksLoad"); /* NO_COVERAGE */
408 }
409
412 /* Look for the begining of loop to determine the number of arguments */
413 unsigned _tnt_find_num_unique(const char **varnames)
414 {
415
416  unsigned loop;
417
418  for (loop = 1; loop < TNT_MAX_NUMIO; loop++) {
419  if (0 == strcmp(varnames[loop],varnames[0])) {
420  return loop;
421  }
422  }
423  return TNT_MAX_NUMIO;
424 }
425
426
486 void tntProcessCLOptions(int argc,
487  char **argv,
490  char *saveprefix,
491  int *chi,
492  int *outputState)
493 {
494  static struct option long_options[] = {
495  {"input", required_argument, 0, 'i'},
496  {"operators", required_argument, 0, 'o'},
497  {"directory", required_argument, 0, 'd'},
498  {"chi", required_argument, 0, 'c'},
499  {"TOL", required_argument, 0, 'T'},
500  {"abstrunc", required_argument, 0, 'a'},
501  {"reltrunc", required_argument, 0, 'r'},
502  {"truncerrbound", required_argument, 0, 'E'},
503  {"no-output", no_argument, 0, 'q'},
504  {"svdtype", required_argument, 0, 's'},
505  {"seed", required_argument, 0, 'e'},
506  {"quiet", no_argument, 0, 10},
507  {"gpusvdsz", required_argument, 0, 'g'},
508  {"help", no_argument, 0, 'h'},
509  {0, 0, 0, 0}
510  };
511  int req_in = 0, req_op = 0; /* Flag to specify whether an input file or operator file has been provided on the command line */
512  int opt_out = -1; /* Flag to specify whether output will be given, dependent on the argument no-output and directory */
513  int option_index;
514  int c; /* counter for arguments */
515  double TOL; /* truncation tolerance set on the command line */
516  int seed; /* random number seed passed on the command line */
517  char **argv_cp;
518  extern int _tnt_gpusvd_minsz;
519  TNT_ERR_RET_DEFS
520
521  /* Make a copy of argv - getopt_long will permute the arguments, and they may be required by another processing function */
522  chk = argv_cp = malloc(argc*sizeof(char *));
523  TNT_PUB_MEM_CHK /* NO_COVERAGE */
524
525  for (c = 0; c < argc; c++) {
526  argv_cp[c] = argv[c];
527  }
528
529  /* Set optind to 1 in case it has already been called previously */
530  optind = 1;
531
532  /* Set opterr to zero to prevent error message for unknown erguments, since they may be being treated by another process options function */
533  opterr = 0;
534
535  /* Set default value for chi */
536  if (chi != NULL) *chi = -1;
537
538  /* Loop over all input arguments */
539  while ((c = getopt_long (argc, argv_cp, "hi:o:d:c:T:qa:r:E:s:e:g:", long_options, &option_index)) != -1) {
540  switch (c) {
541  case 'i':
542  if (loadname != NULL) {
544  req_in = 1;
545  _tnt_printf("Input file : <<<%s>>>\n", loadname);
546  } else {
547  tntWarningPrint("Input file given on the command line will be ignored since no variable has been set to assign it to");
548  }
549  break;
550  case 'o':
551  if (loadname_op != NULL) {
553  req_op = 1;
554  _tnt_printf("Operator file : <<<%s>>>\n", loadname_op);
555  } else {
556  tntWarningPrint("Operator file given on the command line will be ignored since no variable has been set to assign it to");
557  }
558  break;
559  case 's':
560  if (0 == strcmp(optarg,"sdd")) {
561  tntSVDRoutineSet(TNT_LAPACK_SDD);
562  } else if (0 == strcmp(optarg,"svd")) {
563  tntSVDRoutineSet(TNT_LAPACK_SVD);
564  } else {
565  tntWarningPrint("Unknown SVD type. Default of sdd is being used.");
566  }
567  break;
568  case 'd':
569  if (saveprefix != NULL) {
570  strcpy(saveprefix, optarg);
571  opt_out = (-1 == opt_out) ? 1 : 0; /* Only set opt_out to 1 if the no_output flag has not already been given */
572  _tnt_printf("Save directory : <<<%s>>>\n", saveprefix);
573  } else {
574  tntWarningPrint("Save directory given on the command line will be ignored since no variable has been set to assign it to");
575  opt_out = 0;
576  }
577  /* set the output to be where error info is saved, regardless of whether other output will be saved */
578  sprintf(tnt_err.outputfile,"%s_err.mat",optarg);
579  break;
580  case 'c':
581  if (chi != NULL ) {
582  *chi = atoi(optarg);
583  _tnt_printf("Chi : <<<%d>>>\n", *chi);
584  } else {
585  tntWarningPrint("The value of chi given on the command line will be ignored since no variable has been set to assign it to");
586  }
587  break;
588  case 'e' :
589  seed = atoi(optarg);
590  if (seed) {
591  _tnt_printf("Setting random number seed to <<<%d>>>\n", seed);
592  tntRandSeedSet(seed);
593  } else {
594  _tnt_printf("Setting random number seed with the current time\n");
595  tntSysRandSeedSet(time(NULL));
596  }
597  break;
598  case 10 :
599  _tnt_quiet_mode_on();
600  break;
601  case 'g' :
602  #ifdef TNTMAGMAAVAIL
603  _tnt_gpusvd_minsz = atoi(optarg);
604  _tnt_printf("All matrices with elements larger than %d will be sent to the GPU\n",_tnt_gpusvd_minsz);
605  #endif
606  break;
607  case 'T' :
608  TOL = atof(optarg);
609  tntSVDTolSet(TOL);
610  break;
611  case 'a' :
612  TOL = atof(optarg);
613  tntSVDTruncTolSet(TOL);
614  break;
615  case 'r' :
616  TOL = atof(optarg);
618  break;
619  case 'E' :
620  TOL = atof(optarg);
622  break;
623  case 'q' :
624  opt_out = 0;
625  _tnt_printf("No output will be saved\n");
626  break;
627 /* START_NOCOV */
628  case 'h' :
629  _tnt_printf("\nOptions for all TNT routines\n");
630  _tnt_printf("-h [--help] : print this help\n");
631  _tnt_printf("-i [--input] FILE : path file to file containting input checkpoint (the networks and paramaters that change during the algorithm)\n");
632  _tnt_printf("-o [--operators] FILE : path to file containing operators (variables and operators not changed during the algorithm)\n");
633  _tnt_printf("-d [--directory] DIR : output directory and prefix for the output files (which may later be used as input checkpoints)\n");
634  _tnt_printf("-c [--chi] VAL : the maximum internal dimesion of nodes in the network (positive integer)\n");
635  _tnt_printf("-T [--TOL] VAL : tolerance for zeroing matrix values in SVD. Default is 1e-14.\n");
636  _tnt_printf("-a [--abstrunc] VAL : absolute value to truncate to in SVD. No value set by default.\n");
637  _tnt_printf("-r [--reltrunc] VAL : relative value to truncate to in SVD. Default is 1e-16.\n");
638  _tnt_printf("-r [--truncerrbound] VAL: allowed truncation error per SVD. Default is -1.\n");
639  _tnt_printf("-s [--svdtype](sdd|svd): Routine for SVD. Default is sdd.\n");
640  _tnt_printf("-e [--seed] VAL : Random number seed for the calculation. If zero or no argument is given the current time will be used as a seed.\n");
641  _tnt_printf("-q [--no-output] : no output files written \n");
642
643  free(argv_cp);
644  _tnt_printf("\n For options for other routines try --help-tebd or --help-dmrg");
645  tntFinalize();
646  exit(0);
647 /* END_NOCOV */
648  default: /* '?' Do nothing since argument may be treated by another function */
649  break;
650  }
651
652  }
653
654  /* Check require options are all there, else exit in error */
655  if (loadname != NULL && 0 == req_in) { /* NO_COVERAGE */
656  sprintf(tnt_err.errinfostr,"An input file is required but no option has been given on the command line (--input|-i)"); /* NO_COVERAGE */
657  sprintf(tnt_err.pubfuncname,__FUNCTION__); /* NO_COVERAGE */
658  _tnt_print_error(); /* NO_COVERAGE */
659  } /* NO_COVERAGE */
660  if (loadname_op != NULL && 0 == req_op) { /* NO_COVERAGE */
661  sprintf(tnt_err.errinfostr,"An operator file is required but no option has been given on the command line (--operators|-o)"); /* NO_COVERAGE */
662  sprintf(tnt_err.pubfuncname,__FUNCTION__); /* NO_COVERAGE */
663  _tnt_print_error(); /* NO_COVERAGE */
664  } /* NO_COVERAGE */
665
666  /* Set the output state */
667  if (outputState != NULL) {
668  if (-1 == opt_out && saveprefix != NULL) {
669  tntWarningPrint("No output directory specified (-d [--directory]) defaulting to no output");
670  *outputState = 0;
671  } else {
672  *outputState = opt_out;
673  }
674  }
675
676  if (chi != NULL && -1 == *chi) {
677  _tnt_printf("Chi : default <<<-1>>> i.e. no truncation. \n");
678  }
679
680  _tnt_print_sysinfo("(loaded from command line)");
681  free(argv_cp);
682
683  return;
684 }
void tntExOpsLoad(const char *loadname, tntExOp varA, tntExOp varB, tntExOp varC, tntExOp varD, tntExOp varE)
void tntNetworksLoad(const char *loadname, tntNetwork varA, tntNetwork varB, tntNetwork varC, tntNetwork varD, tntNetwork varE)
void tntDoubleArraysLoad(const char *loadname, tntDoubleArray arrA, tntDoubleArray arrB, tntDoubleArray arrC, tntDoubleArray arrD, tntDoubleArray arrE)
void tntSVDTolSet(double inpTOL)
Definition: tntSys.c:192
void tntSVDRoutineSet(int svdtype)
Definition: tntSys.c:221
void tntComplexArraysLoad(const char *loadname, tntComplexArray arrA, tntComplexArray arrB, tntComplexArray arrC, tntComplexArray arrD, tntComplexArray arrE)
void tntSVDTruncErrTolSet(double truncErrTol)
Definition: tntSys.c:293
void tntComplexParamsLoad(const char *loadname, tntComplex varA, tntComplex varB, tntComplex varC, tntComplex varD, tntComplex varE)
void tntSVDTruncTolSet(double truncTol)
Definition: tntSys.c:253
void tntSVDRelTruncTolSet(double relTruncTol)
Definition: tntSys.c:273
void tntStringsLoad(const char *loadname, char *varA, char *varB, char *varC, char *varD, char *varE)
Definition: tnt.h:65
void tntIntArraysLoad(const char *loadname, tntIntArray arrA, tntIntArray arrB, tntIntArray arrC, tntIntArray arrD, tntIntArray arrE)