PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // LocallyPrecomputedVMatrix.cc 00004 // 00005 // Copyright (C) 2004-2005 Olivier Delalleau 00006 // 00007 // Redistribution and use in source and binary forms, with or without 00008 // modification, are permitted provided that the following conditions are met: 00009 // 00010 // 1. Redistributions of source code must retain the above copyright 00011 // notice, this list of conditions and the following disclaimer. 00012 // 00013 // 2. Redistributions in binary form must reproduce the above copyright 00014 // notice, this list of conditions and the following disclaimer in the 00015 // documentation and/or other materials provided with the distribution. 00016 // 00017 // 3. The name of the authors may not be used to endorse or promote 00018 // products derived from this software without specific prior written 00019 // permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 // 00032 // This file is part of the PLearn library. For more information on the PLearn 00033 // library, go to the PLearn Web site at www.plearn.org 00034 00035 /* ******************************************************* 00036 * $Id: LocallyPrecomputedVMatrix.cc 8617 2008-03-03 17:45:54Z nouiz $ 00037 ******************************************************* */ 00038 00039 // Authors: Olivier Delalleau 00040 00043 #include "LocallyPrecomputedVMatrix.h" 00044 #include <plearn/base/tostring.h> 00045 #include <plearn/io/fileutils.h> 00046 #include <plearn/vmat/FileVMatrix.h> 00047 00048 namespace PLearn { 00049 using namespace std; 00050 00052 // LocallyPrecomputedVMatrix // 00054 LocallyPrecomputedVMatrix::LocallyPrecomputedVMatrix(): 00055 local_dir("/tmp"), 00056 max_wait(30), 00057 remove_when_done(-1), 00058 sequential_access(true), 00059 verbosity(2) 00060 { 00061 precomp_type = "pmat"; 00062 temporary = true; 00063 } 00064 00065 PLEARN_IMPLEMENT_OBJECT( 00066 LocallyPrecomputedVMatrix, 00067 "A VMat that precomputes its source in a local directory.", 00068 "The 'sequential_access' option can be used to ensure that parallel experiments\n" 00069 "do not access simultaneously the source VMat, in order to stay nice with the\n" 00070 "disk usage. This is achieved thanks to a system lock file in the metadatadir of\n" 00071 "the source VMat. Because it may happen that a lock file remains after an experiment\n" 00072 "crashed, it will be ignored when it gets older than 'max_wait' minutes.\n" 00073 "The default behavior (which can be modified through this VMat's options) is to\n" 00074 "automatically delete the precomputed file when the underlying VMatrix is deleted.\n" 00075 "Note that the whole metadata directory will be deleted, so you should not store\n" 00076 "important data in it).\n" 00077 "Two formats are accepted: 'pmat' (FileVMatrix) and 'dmat' (DiskVMatrix).\n" 00078 ); 00079 00081 // declareOptions // 00083 void LocallyPrecomputedVMatrix::declareOptions(OptionList& ol) 00084 { 00085 declareOption(ol, "local_dir", &LocallyPrecomputedVMatrix::local_dir, OptionBase::buildoption, 00086 "The local directory in which we want to save the precomputed data."); 00087 00088 declareOption(ol, "remove_when_done", 00089 &LocallyPrecomputedVMatrix::remove_when_done, 00090 OptionBase::learntoption, 00091 "Deprecated! (use 'temporary' instead)."); 00092 00093 declareOption(ol, "sequential_access", &LocallyPrecomputedVMatrix::sequential_access, OptionBase::buildoption, 00094 "If set to 1, ensures there are no multiple parallel precomputations (see class help)."); 00095 00096 declareOption(ol, "max_wait", &LocallyPrecomputedVMatrix::max_wait, OptionBase::buildoption, 00097 "Maximum wait time in minutes when 'sequential access' is set to 1 (see class help)."); 00098 00099 declareOption(ol, "verbosity", &LocallyPrecomputedVMatrix::verbosity, OptionBase::buildoption, 00100 "Controls the amount of displayed information."); 00101 00102 // Now call the parent class' declareOptions 00103 inherited::declareOptions(ol); 00104 00105 redeclareOption(ol, "metadatadir", &LocallyPrecomputedVMatrix::metadatadir, OptionBase::nosave, 00106 "The metadatadir will be defined by the 'local_dir' option."); 00107 } 00108 00110 // build // 00112 void LocallyPrecomputedVMatrix::build() 00113 { 00114 inherited::build(); 00115 build_(); 00116 } 00117 00119 // build_ // 00121 void LocallyPrecomputedVMatrix::build_() 00122 { 00123 // Check for deprecated option. 00124 if (remove_when_done != -1) { 00125 PLDEPRECATED("In LocallyPrecomputedVMatrix::build_ - You should now be" 00126 " using the 'temporary' option instead of " 00127 "the 'remove_when_done' option, which is deprecated."); 00128 PLASSERT( remove_when_done == 0 || remove_when_done == 1 ); 00129 temporary = remove_when_done != 0; 00130 } 00131 00132 if (!hasMetaDataDir()) { 00133 bool made_dir = force_mkdir(local_dir); 00134 if (!made_dir) { 00135 PLWARNING( 00136 "In LocallyPrecomputedVMatrix::build_ - Could not create dir: %s\n" 00137 "The program may crash if it needs to access an element of this VMatrix.", 00138 local_dir.c_str()); 00139 return; 00140 } 00141 metadatadir = newFilename(local_dir, "locally_precomputed_", true); 00142 if (sequential_access) { 00143 // If necessary, wait until we are allowed to start the precomputation. 00144 if (source->hasMetaDataDir()) 00145 source->lockMetaDataDir(max_wait * 60, verbosity >= 2); 00146 else 00147 PLERROR("In LocallyPrecomputedVMatrix::build_ - The source VMatrix must have a metadatadir"); 00148 } 00149 inherited::build(); 00150 if (sequential_access) 00151 source->unlockMetaDataDir(); 00152 } 00153 updateMtime(precomp_source); 00154 updateMtime(source); 00155 } 00156 00158 // makeDeepCopyFromShallowCopy // 00160 void LocallyPrecomputedVMatrix::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00161 { 00162 inherited::makeDeepCopyFromShallowCopy(copies); 00163 } 00164 00166 // ~LocallyPrecomputedVMatrix // 00168 LocallyPrecomputedVMatrix::~LocallyPrecomputedVMatrix() 00169 { 00170 if (temporary && hasMetaDataDir()) 00171 { 00172 string filename_option; 00173 // Get the name of the precomputed file. 00174 if (precomp_type == "pmat") 00175 { 00176 string source_filename = precomp_source->getOption("filename"); 00177 openString(source_filename, PStream::plearn_ascii) 00178 >> filename_option; 00179 } 00180 else 00181 { 00182 PLASSERT( precomp_type == "dmat" ); 00183 string source_dirname = precomp_source->getOption("dirname"); 00184 openString(source_dirname, PStream::plearn_ascii) 00185 >> filename_option; 00186 } 00187 PPath precomputed_file(filename_option); 00188 // First we delete the precomputed source, so that it does not try 00189 // to save more stuff in the metadatadir after it has been deleted. 00190 // This should also trigger the deletion of the underlying data. 00191 precomp_source = 0; 00192 // If there is nobody accessing the data file (i.e. it should have 00193 // been deleted by now), we can also remove the metadata directory 00194 // of this VMatrix (the one that contained the data file). 00195 if (noReferenceToFile(precomputed_file)) { 00196 PPath mdir = getMetaDataDir(); 00197 bool removed = force_rmdir(mdir); 00198 if (!removed && verbosity >= 1) 00199 PLWARNING( 00200 "In LocallyPrecomputedVMatrix::~LocallyPrecomputedVMatrix " 00201 "- The precomputed data (in '%s') could not be removed", 00202 mdir.absolute().c_str()); 00203 } 00204 } 00205 } 00206 00207 } // end of namespace PLearn 00208 00209 00210 /* 00211 Local Variables: 00212 mode:c++ 00213 c-basic-offset:4 00214 c-file-style:"stroustrup" 00215 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00216 indent-tabs-mode:nil 00217 fill-column:79 00218 End: 00219 */ 00220 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :