00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00044
#include "PrecomputedVMatrix.h"
00045
#include "DiskVMatrix.h"
00046
#include "FileVMatrix.h"
00047
00048
namespace PLearn {
00049
using namespace std;
00050
00051
00052 PrecomputedVMatrix::PrecomputedVMatrix()
00053 :precomp_type("dmat")
00054 {
00055 }
00056
00057
PLEARN_IMPLEMENT_OBJECT(
PrecomputedVMatrix,
00058
"VMatrix that caches (pre-computes on disk) the content of a source vmatrix",
00059
"This sub-class of SourceVMatrix pre-computes the content of a source vmatrix\n"
00060
"in a dmat or pmat file. The name of the disk file is obtained from the metadatadir option\n"
00061
"followed by precomp.dmat or precomp.pmat");
00062
00063 void PrecomputedVMatrix::getNewRow(
int i,
const Vec& v)
const
00064
{
00065
if(
precomp_source.
isNull())
00066
PLERROR(
"Source has not been precomputed. Did you properly set the MetaDataDir?");
00067
precomp_source->getRow(i,v);
00068 }
00069
00070 void PrecomputedVMatrix::declareOptions(
OptionList& ol)
00071 {
00072
00073
00074
00075
00076
00077
00078
00079
declareOption(ol,
"precomp_type", &PrecomputedVMatrix::precomp_type, OptionBase::buildoption,
00080
"The type of vmatrix to be used for the cached precomputed version of the source.\n"
00081
"Currently supported are: dmat, pmat");
00082
00083
00084
00085 inherited::declareOptions(ol);
00086 }
00087
00088 void PrecomputedVMatrix::setMetaDataDir(
const string& the_metadatadir)
00089 {
00090 inherited::setMetaDataDir(the_metadatadir);
00091
if(
hasMetaDataDir())
00092
usePrecomputed();
00093 }
00094
00095 void PrecomputedVMatrix::usePrecomputed()
00096 {
00097
string mdir =
append_slash(
getMetaDataDir());
00098
if(
precomp_type==
"dmat")
00099 {
00100
string dmatdir = mdir +
"precomp.dmat";
00101
bool recompute =
true;
00102
if(
isdir(dmatdir))
00103 {
00104
precomp_source =
new DiskVMatrix(dmatdir);
00105
if(
precomp_source->getMtime() >= source->getMtime())
00106 recompute =
false;
00107 }
00108
if(recompute)
00109 {
00110
force_rmdir(dmatdir);
00111 source->saveDMAT(dmatdir);
00112
precomp_source =
new DiskVMatrix(dmatdir);
00113 }
00114 length_ =
precomp_source->
length();
00115 }
00116
else if(
precomp_type==
"pmat")
00117 {
00118
string pmatfile = mdir +
"precomp.pmat";
00119
bool recompute =
true;
00120
if(
isfile(pmatfile))
00121 {
00122
precomp_source =
new FileVMatrix(pmatfile);
00123
if(
precomp_source->getMtime() >= source->getMtime())
00124 recompute =
false;
00125 }
00126
if(recompute)
00127 {
00128
rm(pmatfile);
00129 source->savePMAT(pmatfile);
00130
precomp_source =
new FileVMatrix(pmatfile);
00131 }
00132 length_ =
precomp_source->
length();
00133 }
00134
else
00135
PLERROR(
"Invalid precomp_type=%s. Must be one of: dmat, pmat.",
precomp_type.c_str());
00136
00137 }
00138
00139 void PrecomputedVMatrix::build_()
00140 {
00141
setMetaInfoFromSource();
00142 }
00143
00144
00145 void PrecomputedVMatrix::build()
00146 {
00147 inherited::build();
00148
build_();
00149 }
00150
00151 void PrecomputedVMatrix::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies)
00152 {
00153 inherited::makeDeepCopyFromShallowCopy(copies);
00154
00155
00156
00157
00158
00159
deepCopyField(
precomp_source, copies);
00160 }
00161
00162 }
00163