00001 #ifndef Impala_Core_VideoSet_ComputeKernelData_h
00002 #define Impala_Core_VideoSet_ComputeKernelData_h
00003
00004 #include "Core/VideoSet/Reporter.h"
00005 #include "Sandbox/koen/cuda/projects/precomputeMatrix/ComputeKernelMatrix.h"
00006
00007 namespace Impala
00008 {
00009 namespace Core
00010 {
00011 namespace VideoSet
00012 {
00013
00014
00015 class ComputeKernelData : public Listener
00016 {
00017 public:
00018 ComputeKernelData(Reporter* reporter, CmdOptions& options)
00019 {
00020 mReporter = reporter;
00021 if (options.GetNrArg() < 4)
00022 {
00023 ILOG_ERROR("Missing argument");
00024 return;
00025 }
00026 String name2 = options.GetArg(2);
00027 mBaseSet = Database::MakeRawDataSet(name2, true);
00028 mKernelMatrixName = options.GetArg(3);
00029 }
00030
00031 virtual
00032 ~ComputeKernelData()
00033 {
00034 delete mBaseSet;
00035 }
00036
00037 virtual void
00038 HandleNewWalk(VideoSet* vs, String walkType)
00039 {
00040 mWalkType = walkType;
00041 }
00042
00043
00045
00047 class PrecomputeTask
00048 {
00049 public:
00050 PrecomputeTask(String inputDefinitionFilename, String averagesFilename,
00051 String outputFilename, Database::RawDataSet* dataSet,
00052 Database::RawDataSet* mBaseSet, int fileId, String walkType)
00053 : mOutputFilename(outputFilename), mTotalFeatureWeight(0),
00054 mWriteAsFeatureTable(true)
00055 {
00056 ILOG_INFO("Loading precompute definition (" << inputDefinitionFilename
00057 << ")...");
00058 std::ifstream ifs(inputDefinitionFilename.c_str());
00059 std::ifstream fs(averagesFilename.c_str(), std::ios::binary);
00060 if(fs.fail() || fs.eof())
00061 {
00062 ILOG_ERROR("Could not load averages from " << averagesFilename);
00063 }
00064
00065 if (mBaseSet->GetDatabase()->GetDataChannel() || dataSet->GetDatabase()->GetDataChannel())
00066 {
00067 ILOG_ERROR("Does not work with data channel");
00068 throw("bye");
00069 }
00070
00071 double weight = 0.0;
00072 String name;
00073 while(ifs >> weight)
00074 {
00075 ifs >> name;
00076 int i = mFeatureNames.size();
00077 mFeatureNames.push_back(name);
00078 mFeatureWeights.push_back(weight);
00079
00080 double q = 0.0;
00081 fs.read(reinterpret_cast<char *>(&q), sizeof(q));
00082 mFeatureAverages.push_back(q);
00083 ILOG_INFO(i << " " << mFeatureAverages[i] << " (" << mFeatureWeights[i] << "*" << mFeatureNames[i] << ")");
00084 mTotalFeatureWeight += weight;
00085
00086 String tableA = mBaseSet->GetFilePathFeatureIndex(mFeatureNames[i], "", false, false);
00087 String tableB = dataSet->GetFilePathFeatureData(walkType,
00088 Feature::FeatureDefinition(mFeatureNames[i]), fileId, false, -1, false, false);
00089 mTableNamesA.push_back(tableA);
00090 mTableNamesB.push_back(tableB);
00091 }
00092 fs.close();
00093 ifs.close();
00094 if(Size() == 0)
00095 ILOG_ERROR("Empty input definition?");
00096 }
00097
00098 inline size_t
00099 Size()
00100 {
00101 return mFeatureNames.size();
00102 }
00103
00104 String
00105 GetTableFilenameA(int index)
00106 {
00107 return mTableNamesA[index];
00108 }
00109
00110 String
00111 GetTableFilenameB(int index)
00112 {
00113 return mTableNamesB[index];
00114 }
00115
00116 KernelWriter*
00117 GetKernelWriter(int vectorCountA, int vectorCountB)
00118 {
00119 KernelWriter* kernelWriter = new KernelWriter(mOutputFilename, vectorCountA, vectorCountB, mWriteAsFeatureTable);
00120
00121
00122 std::ifstream fs(mTableNamesB[0].c_str(), std::ios::binary);
00123 int n = 200 + 8 * vectorCountB;
00124 char* buffer = new char[n];
00125 fs.seekg(1024 + 128);
00126 fs.read(buffer, n);
00127 fs.close();
00128
00129 kernelWriter->outputStream.seekp(1024 + 128);
00130 kernelWriter->outputStream.write(buffer, n);
00131 delete buffer;
00132
00133 return kernelWriter;
00134 }
00135
00136 std::vector<String> mFeatureNames;
00137 std::vector<String> mTableNamesA;
00138 std::vector<String> mTableNamesB;
00139 std::vector<double> mFeatureWeights;
00140 std::vector<double> mFeatureAverages;
00141 bool mWriteAsFeatureTable;
00142 String mOutputFilename;
00143 double mTotalFeatureWeight;
00144 };
00145
00146
00147 virtual void
00148 HandleDoneFile(VideoSet* vs, int fileId, Stream::RgbDataSrc* src)
00149 {
00150 String kernelDataCacheFilename = vs->GetFilePathKernelData(
00151 mWalkType,
00152 mBaseSet->GetSetName(),
00153 mKernelMatrixName,
00154 fileId, false, true);
00155 if(!kernelDataCacheFilename.empty())
00156 {
00157 ILOG_INFO("Skipping, KernelData already exists: " <<
00158 kernelDataCacheFilename);
00159 return;
00160 }
00161
00162 String storageFilename = vs->GetFilePathKernelData(
00163 mWalkType,
00164 mBaseSet->GetSetName(),
00165 mKernelMatrixName,
00166 fileId, true, false);
00167
00168 String inputDefinition = mBaseSet->GetFilePathPrecomputedKernels
00169 (mKernelMatrixName + ".input.txt", "", false, false);
00170
00171 String averagesFilename = mBaseSet->GetFilePathPrecomputedKernels
00172 (mKernelMatrixName + ".averages.raw", "", false, false);
00173
00174
00175 #ifdef CUDA
00176 bool GPU = Link::Cuda::CudaUsed();
00177 #else
00178 bool GPU = false;
00179 #endif
00180 int setting_slabWidth = 1024;
00181 PrecomputeTask* pt = new PrecomputeTask(inputDefinition,
00182 averagesFilename, storageFilename, vs, mBaseSet, fileId, mWalkType);
00183 ILOG_INFO("GPU mode: " << GPU);
00184 if(GPU)
00185 {
00186 ComputeKernelMatrix<float, true>(pt, setting_slabWidth);
00187 }
00188 else
00189 {
00190 #ifdef CUDA
00191
00192 ComputeKernelMatrix<float, false>(pt, setting_slabWidth);
00193 #else
00194 ComputeKernelMatrix<double, false>(pt, setting_slabWidth);
00195 #endif
00196 }
00197 delete pt;
00198 }
00199
00200 private:
00201 Reporter* mReporter;
00202 Database::RawDataSet* mBaseSet;
00203 String mWalkType;
00204 String mKernelMatrixName;
00205
00206 ILOG_VAR_DEC;
00207 };
00208
00209 ILOG_VAR_INIT(ComputeKernelData, Impala.Core.VideoSet);
00210
00211 }
00212 }
00213 }
00214
00215 #endif