PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // PrecomputedVMatrix.cc 00004 // 00005 // Copyright (C) 2003 Pascal Vincent 00006 // Copyright (C) 2005 Olivier Delalleau 00007 // 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are met: 00010 // 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 00014 // 2. Redistributions in binary form must reproduce the above copyright 00015 // notice, this list of conditions and the following disclaimer in the 00016 // documentation and/or other materials provided with the distribution. 00017 // 00018 // 3. The name of the authors may not be used to endorse or promote 00019 // products derived from this software without specific prior written 00020 // permission. 00021 // 00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // This file is part of the PLearn library. For more information on the PLearn 00034 // library, go to the PLearn Web site at www.plearn.org 00035 00036 /* ******************************************************* 00037 * $Id: PrecomputedVMatrix.cc 10005 2009-03-10 17:09:49Z tihocan $ 00038 ******************************************************* */ 00039 00040 // Authors: Pascal Vincent 00041 00045 #include "PrecomputedVMatrix.h" 00046 #include "DiskVMatrix.h" 00047 #include "FileVMatrix.h" 00048 #include "TemporaryDiskVMatrix.h" 00049 #include "TemporaryFileVMatrix.h" 00050 #include <plearn/io/PPath.h> 00051 #include <plearn/io/fileutils.h> 00052 00053 namespace PLearn { 00054 using namespace std; 00055 00056 00057 PrecomputedVMatrix::PrecomputedVMatrix(): 00058 precomp_type("dmat"), 00059 temporary(false) 00060 {} 00061 00062 PLEARN_IMPLEMENT_OBJECT( 00063 PrecomputedVMatrix, 00064 "VMatrix that pre-computes on disk the content of a source VMatrix", 00065 "This sub-class of SourceVMatrix pre-computes the content of a source\n" 00066 "VMatrix in a 'dmat' (DiskVMatrix) or 'pmat' (FileVMatrix) file. The\n" 00067 "name of the disk file is obtained from the metadatadir option, followed\n" 00068 "by 'precomp.dmat' or 'precomp.pmat'.\n" 00069 "With the 'temporary' option, one may automatically delete the\n" 00070 "precomputed data once it is not used anymore.\n" 00071 ); 00072 00074 // getNewRow // 00076 void PrecomputedVMatrix::getNewRow(int i, const Vec& v) const 00077 { 00078 if(precomp_source.isNull()) 00079 PLERROR("Source has not been precomputed. Did you properly set the " 00080 "MetaDataDir?"); 00081 precomp_source->getRow(i,v); 00082 } 00083 00085 // declareOptions // 00087 void PrecomputedVMatrix::declareOptions(OptionList& ol) 00088 { 00089 declareOption(ol, "precomp_type", &PrecomputedVMatrix::precomp_type, OptionBase::buildoption, 00090 "The type of VMatrix to be used for the cached precomputed version\n" 00091 "of the source. Currently supported are 'dmat' and 'pmat'."); 00092 00093 declareOption(ol, "temporary", &PrecomputedVMatrix::temporary, OptionBase::buildoption, 00094 "If set to 1, the created precomputed data will be temporary, i.e.\n" 00095 "will be deleted when not used anymore."); 00096 00097 // Now call the parent class' declareOptions 00098 inherited::declareOptions(ol); 00099 00100 // Hide useless options. 00101 redeclareOption(ol, "writable", &PrecomputedVMatrix::writable, OptionBase::nosave, 00102 "Unused option."); 00103 redeclareOption(ol, "length", &PrecomputedVMatrix::length_, OptionBase::nosave, 00104 "Unused option."); 00105 redeclareOption(ol, "width", &PrecomputedVMatrix::width_, OptionBase::nosave, 00106 "Unused option."); 00107 redeclareOption(ol, "inputsize", &PrecomputedVMatrix::inputsize_, OptionBase::nosave, 00108 "Unused option."); 00109 redeclareOption(ol, "targetsize", &PrecomputedVMatrix::targetsize_, OptionBase::nosave, 00110 "Unused option."); 00111 redeclareOption(ol, "weightsize", &PrecomputedVMatrix::weightsize_, OptionBase::nosave, 00112 "Unused option."); 00113 } 00114 00116 // setMetaDataDir // 00118 void PrecomputedVMatrix::setMetaDataDir(const PPath& the_metadatadir) 00119 { 00120 inherited::setMetaDataDir(the_metadatadir); 00121 if ( hasMetaDataDir() ) // don't do anything if the meta-data-dir is not yet set. 00122 { 00123 setMetaInfoFromSource(); 00124 usePrecomputed(); 00125 } 00126 } 00127 00128 void PrecomputedVMatrix::usePrecomputed() 00129 { 00130 PPath mdir = getMetaDataDir(); 00131 00132 if ( precomp_type == "dmat" ) 00133 { 00134 PPath dmatdir = mdir / "precomp.dmat"; 00135 bool recompute = true; 00136 00137 if ( isdir(dmatdir) ) 00138 { 00139 precomp_source = temporary ? new TemporaryDiskVMatrix(dmatdir, 00140 false) 00141 : new DiskVMatrix(dmatdir); 00142 if(isUpToDate(precomp_source)) 00143 recompute = false; 00144 } 00145 00146 if(recompute) 00147 { 00148 force_rmdir(dmatdir); 00149 source->saveDMAT(dmatdir); 00150 precomp_source = temporary ? new TemporaryDiskVMatrix(dmatdir, 00151 false) 00152 : new DiskVMatrix(dmatdir); 00153 } 00154 length_ = precomp_source->length(); 00155 } 00156 00157 else if ( precomp_type == "pmat" ) 00158 { 00159 PPath pmatfile = mdir / "precomp.pmat"; 00160 bool recompute = true; 00161 00162 if ( isfile(pmatfile) ) 00163 { 00164 precomp_source = temporary ? new TemporaryFileVMatrix(pmatfile) 00165 : new FileVMatrix(pmatfile); 00166 if(isUpToDate(pmatfile)) 00167 recompute = false; 00168 } 00169 00170 if(recompute) 00171 { 00172 rm(pmatfile); 00173 source->savePMAT(pmatfile); 00174 precomp_source = temporary ? new TemporaryFileVMatrix(pmatfile) 00175 : new FileVMatrix(pmatfile); 00176 } 00177 length_ = precomp_source->length(); 00178 } 00179 00180 else 00181 PLERROR("Invalid precomp_type=%s. Must be one of: dmat, pmat.", 00182 precomp_type.c_str()); 00183 } 00184 00185 void PrecomputedVMatrix::build_() 00186 { 00187 //We don't always call it as some matrix(FilteredVMatrix) are only 00188 //completly set if they have a metadatadir. 00189 if(hasMetaDataDir() ||(source->width()>0 && source->length()>0)) 00190 setMetaInfoFromSource(); 00191 } 00192 00193 // ### Nothing to add here, simply calls build_ 00194 void PrecomputedVMatrix::build() 00195 { 00196 inherited::build(); 00197 build_(); 00198 } 00199 00200 void PrecomputedVMatrix::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00201 { 00202 inherited::makeDeepCopyFromShallowCopy(copies); 00203 00204 // ### Call deepCopyField on all "pointer-like" fields 00205 // ### that you wish to be deepCopied rather than 00206 // ### shallow-copied. 00207 // ### ex: 00208 deepCopyField(precomp_source, copies); 00209 } 00210 00211 } // end of namespace PLearn 00212 00213 00214 /* 00215 Local Variables: 00216 mode:c++ 00217 c-basic-offset:4 00218 c-file-style:"stroustrup" 00219 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00220 indent-tabs-mode:nil 00221 fill-column:79 00222 End: 00223 */ 00224 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :