blas_interface.cc

00001 /***************************************************************************
00002  *  PSIMRCC : Copyright (C) 2007 by Francesco Evangelista and Andrew Simmonett
00003  *  frank@ccc.uga.edu   andysim@ccc.uga.edu
00004  *  A multireference coupled cluster code
00005  ***************************************************************************/
00006 #include "blas.h"
00007 #include "debugging.h"
00008 #include "memory_manager.h"
00009 #include "moinfo.h"
00010 #include "utilities.h"
00011 #include <libciomr/libciomr.h>
00012 #include <libqt/qt.h>
00013 
00014 extern FILE *infile, *outfile;
00015 
00016 namespace psi{ namespace psimrcc{
00017 
00018 using namespace std;
00019 
00020 void CCBLAS::add_index(char* cstr)
00021 {
00022   // Make sure that the element that we are adding is not present
00023   string str(cstr);
00024   to_lower(str);
00025   if(indices.find(str)==indices.end()){
00026     indices.insert(make_pair(str,new CCIndex(str)));
00027   }
00028 }
00029 
00030 void CCBLAS::add_Matrix(char* cstr)
00031 {
00032   string str(cstr);
00033   vector<string> names = moinfo->get_matrix_names(str);
00034   for(int n=0;n<names.size();n++)
00035     add_Matrix_ref(names[n]);
00036 }
00037 
00038 void CCBLAS::add_Matrix(string str)
00039 {
00040   vector<string> names = moinfo->get_matrix_names(str);
00041   for(int n=0;n<names.size();n++)
00042     add_Matrix_ref(names[n]);
00043 }
00044 
00045 void CCBLAS::add_Matrix_ref(std::string& str)
00046 {
00047   // Make sure that the element that we are adding is not present
00048   MatrixMap::iterator iter = matrices.find(str);
00049   if(iter==matrices.end()){
00050     CCIndex* index_pointer[2];
00051     // Default: assume the [] indexing
00052     index_pointer[0]=get_index("[]");
00053     index_pointer[1]=get_index("[]");
00054     vector<string> index_string_vec = split_indices(str);
00055     for(int i=0;i<index_string_vec.size();++i)
00056       index_pointer[i]=get_index(index_string_vec[i]);
00057     matrices.insert(make_pair(str,new CCMatrix(str,index_pointer[0],index_pointer[1])));
00058   }
00059 }
00060 
00061 CCIndex* CCBLAS::get_index(char* cstr)
00062 {
00063   string str(cstr);
00064   to_lower(str);
00065   // Make sure that the element that we are retrieving is present
00066   IndexMap::iterator iter = indices.find(str);
00067   if(iter!=indices.end()){
00068     return(indices[str]);
00069   }
00070   string err("\nCCBLAS::get_index() couldn't find index " + str);
00071   print_error(err,__FILE__,__LINE__);
00072   return(NULL);
00073 }
00074 
00075 CCIndex* CCBLAS::get_index(string& str)
00076 {
00077   to_lower(str);
00078   // Make sure that the element that we are retrieving is present
00079   IndexMap::iterator iter = indices.find(str);
00080   if(iter!=indices.end()){
00081     return(indices[str]);
00082   }
00083   string err("\nCCBLAS::get_index() couldn't find index " + str);
00084   print_error(err,__FILE__,__LINE__);
00085   return(NULL);
00086 }
00087 
00088 CCMatTmp CCBLAS::get_MatTmp(std::string str, int reference, DiskOpt disk_option)
00089 {
00090   append_reference(str,reference);
00091   load(get_Matrix(str));
00092   return(CCMatTmp(get_Matrix(str),disk_option)); 
00093 }
00094 
00095 CCMatTmp CCBLAS::get_MatTmp(std::string str, DiskOpt disk_option)
00096 {
00097   load(get_Matrix(str));
00098   return(CCMatTmp(get_Matrix(str),disk_option)); 
00099 }
00100 
00101 CCMatTmp CCBLAS::get_MatTmp(CCMatrix* Matrix, DiskOpt disk_option)
00102 {
00103   load(Matrix);
00104   return(CCMatTmp(Matrix,disk_option)); 
00105 }
00106 
00107 CCMatIrTmp CCBLAS::get_MatIrTmp(std::string str, int reference, int irrep,  DiskOpt disk_option)
00108 {
00109   append_reference(str,reference);
00110   load_irrep(get_Matrix(str),irrep);
00111   return(CCMatIrTmp(get_Matrix(str),irrep,disk_option)); 
00112 }
00113 
00114 CCMatIrTmp CCBLAS::get_MatIrTmp(std::string str, int irrep, DiskOpt disk_option)
00115 {
00116   load_irrep(get_Matrix(str),irrep);
00117   return(CCMatIrTmp(get_Matrix(str),irrep,disk_option));
00118 }
00119 
00120 CCMatIrTmp CCBLAS::get_MatIrTmp(CCMatrix* Matrix, int irrep, DiskOpt disk_option)
00121 {
00122   load_irrep(Matrix,irrep);
00123   return(CCMatIrTmp(Matrix,irrep,disk_option));
00124 }
00125 
00126 CCMatrix* CCBLAS::get_Matrix(char* cstr, int reference)
00127 {
00128   string str(cstr);
00129   append_reference(str,reference);
00130   return(get_Matrix(str));
00131 }
00132 
00133 CCMatrix* CCBLAS::get_Matrix(char* cstr)
00134 {
00135   string str(cstr);
00136   return(get_Matrix(str));
00137 }
00138 
00139 CCMatrix* CCBLAS::get_Matrix(string& str)
00140 {
00141   // Make sure that the element that we are retrieving is present
00142   MatrixMap::iterator iter = matrices.find(str);
00143   if(iter!=matrices.end())
00144     return(matrices[str]);
00145   string err("\nCCBLAS::get_matrix() couldn't find matrix " + str);
00146   print_error(err,__FILE__,__LINE__);
00147   return(NULL);
00148 }
00149 
00150 CCMatrix* CCBLAS::get_Matrix(string& str, string& expression)
00151 {
00152   // Make sure that the element that we are retrieving is present
00153   MatrixMap::iterator iter = matrices.find(str);
00154   if(iter!=matrices.end()){
00155     return(matrices[str]);
00156   }
00157   fprintf(outfile,"\n\nCCBLAS::parse() couldn't find matrix %s in the CCMatrix list\n\nwhile parsing the string:\n\t%s\n\n",str.c_str(),expression.c_str());
00158   fflush(outfile);
00159   exit(1);
00160 }
00161 
00162 void CCBLAS::set_scalar(char* cstr,int reference,double value)
00163 {
00164   string str(cstr);
00165   set_scalar(str,reference,value);
00166 }
00167 
00168 void CCBLAS::set_scalar(string& str,int reference,double value)
00169 {
00170   string matrix_str = add_reference(str,reference);
00171   // Make sure that the element that we are retrieving is present
00172   MatrixMap::iterator iter = matrices.find(matrix_str);
00173   if(iter!=matrices.end()){
00174     load(iter->second);
00175     iter->second->set_scalar(value);
00176     return;
00177   }
00178   string err("\nCCBLAS::set_scalar() couldn't find matrix " + matrix_str);
00179   print_error(err.c_str(),__FILE__,__LINE__);
00180 }
00181 
00182 double CCBLAS::get_scalar(char* cstr,int reference)
00183 {
00184   string str(cstr);
00185   return(get_scalar(str,reference));
00186 }
00187 
00188 double CCBLAS::get_scalar(string& str,int reference)
00189 {
00190   string matrix_str(str);
00191   append_reference(matrix_str,reference);
00192   // Make sure that the element that we are retrieving is present
00193   MatrixMap::iterator iter = matrices.find(matrix_str);
00194   if(iter!=matrices.end()){
00195     load(iter->second);
00196     return(iter->second->get_scalar());
00197   }
00198   string err("\nCCBLAS::get_scalar() couldn't find matrix " + matrix_str);
00199   print_error(err.c_str(),__FILE__,__LINE__);
00200   return (0.0);
00201 }
00202 
00203 double CCBLAS::get_scalar(string str)
00204 {
00205   // Make sure that the element that we are retrieving is present
00206   MatrixMap::iterator iter = matrices.find(str);
00207   if(iter!=matrices.end()){
00208     load(iter->second);
00209     return(iter->second->get_scalar());
00210   }
00211   string err("\nCCBLAS::get_scalar() couldn't find matrix " + str);
00212   print_error(err.c_str(),__FILE__,__LINE__);
00213   return (0.0);
00214 }
00215 
00216 void CCBLAS::load(CCMatrix* Matrix)
00217 {
00218   if(Matrix->is_allocated()){
00219     DEBUGGING(2,
00220       fprintf(outfile,"\nCCBLAS::load(%s): matrix is in core.",Matrix->get_label().c_str());
00221     );
00222   }else{
00223     DEBUGGING(2,
00224       fprintf(outfile,"\nCCBLAS::load(%s): matrix is not in core. Loading it :[",Matrix->get_label().c_str());
00225     );
00226     // Do we have enough memory to fit the entire matrix in core?
00227     double memory_required = Matrix->get_memory();
00228     make_space(memory_required);
00229     Matrix->load();
00230     DEBUGGING(2,
00231       fprintf(outfile,"\n] <- done.");
00232     );
00233   }
00234 }
00235 
00236 void CCBLAS::load_irrep(CCMatrix* Matrix,int h)
00237 {
00238   if(Matrix->is_block_allocated(h)){
00239     DEBUGGING(2,
00240       fprintf(outfile,"\nCCBLAS::load_irrep(%s,%d): matrix block is in core.",Matrix->get_label().c_str(),h);
00241     )
00242   }else{
00243     DEBUGGING(2,
00244       fprintf(outfile,"\nCCBLAS::load_irrep(%s,%d): matrix block is not in core. Loading it : [",Matrix->get_label().c_str(),h);
00245     )
00246     // Do we have enough memory to fit the entire matrix in core?
00247     double memory_required = Matrix->get_memorypi(h);
00248     make_space(memory_required);
00249     Matrix->load_irrep(h);
00250     DEBUGGING(2,
00251       fprintf(outfile,"\n] <- done.");
00252     )
00253   }
00254 }
00255 
00256 void CCBLAS::make_space(double memory_required)
00257 {
00258   if(memory_required < mem->get_free_memory())
00259     return;
00260   else{
00261     fprintf(outfile,"\nCCBLAS::make_space() not implemented yet!!!");
00262     // Attempt #1
00263     
00264 
00265   }
00266 }
00267 
00268 // double*** CCBLAS::get_sortmap(CCIndex* T_left,CCIndex* T_right,int thread)
00269 // {
00270 //   string sortstr = T_left->get_label() + T_right->get_label() + to_string(thread);
00271 //   double*** T_matrix=NULL;
00272 //   SortMap::iterator iter = sortmap.find(sortstr);
00273 //   if(iter==sortmap.end()){
00274 //     int T_matrix_offset = 0;
00275 //     T_matrix = new double**[moinfo->get_nirreps()];
00276 //     for(int irrep=0;irrep<moinfo->get_nirreps();irrep++){
00277 //       T_matrix[irrep] = new double*[T_left->get_pairpi(irrep)];
00278 //       for(int i=0;i<T_left->get_pairpi(irrep);i++){
00279 //         T_matrix[irrep][i]=&(work[thread][T_matrix_offset+i*T_right->get_pairpi(irrep)]);
00280 //       }
00281 //       zero_arr(&(T_matrix[irrep][0][0]),T_left->get_pairpi(irrep)*T_right->get_pairpi(irrep));
00282 //       T_matrix_offset+=T_left->get_pairpi(irrep)*T_right->get_pairpi(irrep);
00283 //     }
00284 //     sortmap.insert(make_pair(sortstr,T_matrix));
00285 //   }else{
00286 //     T_matrix = sortmap[sortstr];
00287 //   }
00288 //   return(T_matrix);
00289 // }
00290 
00291 }} /* End Namespaces */

Generated on Wed Feb 13 16:35:39 2008 for PSI by  doxygen 1.5.4