mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-01-25 05:29:41 -06:00
Redoing file management for TSP. Not thread safe?
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
/*****************************************************************************************
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
@@ -63,15 +63,50 @@ TSP::TSP(const std::string& filename)
|
||||
, _spatialErrorReady(false)
|
||||
, _temporalErrorReady(false)
|
||||
{
|
||||
_file.open(_filename, std::ios::in | std::ios::binary);
|
||||
openFile();
|
||||
openMemoryMap();
|
||||
}
|
||||
|
||||
TSP::~TSP() {
|
||||
closeFile();
|
||||
closeMemoryMap();
|
||||
}
|
||||
|
||||
bool TSP::openFile() {
|
||||
if (!_file.is_open()) {
|
||||
// Set filesize
|
||||
_file.open(_filename, std::ios::ate | std::ios::binary);
|
||||
_filesize = _file.tellg();
|
||||
_file.close();
|
||||
|
||||
// Get file for reading
|
||||
_file.open(_filename, std::ios::in | std::ios::binary);
|
||||
}
|
||||
return _file.is_open() && _file.good();
|
||||
}
|
||||
|
||||
bool TSP::closeFile() {
|
||||
if (_file.is_open())
|
||||
_file.close();
|
||||
return !_file.is_open();
|
||||
}
|
||||
|
||||
bool TSP::openMemoryMap() {
|
||||
if (!_memoryMap.is_open()) {
|
||||
_memoryMap.open(_filename, boost::iostreams::mapped_file::mapmode::readonly, _filesize);
|
||||
}
|
||||
return _memoryMap.is_open();
|
||||
}
|
||||
|
||||
bool TSP::closeMemoryMap() {
|
||||
if (_memoryMap.is_open()) {
|
||||
_memoryMap.close();
|
||||
}
|
||||
return !_memoryMap.is_open();
|
||||
}
|
||||
|
||||
bool TSP::load() {
|
||||
openFile();
|
||||
if (!readHeader()) {
|
||||
LERROR("Could not read header");
|
||||
return false;
|
||||
@@ -276,14 +311,11 @@ GLuint TSP::ssbo() const {
|
||||
std::vector<float> TSP::calculateBrickAverages() {
|
||||
const unsigned int numBrickVals = paddedBrickDim_*paddedBrickDim_*paddedBrickDim_;
|
||||
|
||||
boost::iostreams::mapped_file_source mfile;
|
||||
mfile.open(_filename);
|
||||
|
||||
if (!mfile.is_open()) {
|
||||
if (!openMemoryMap()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const float * voxelData = (float *)mfile.data();
|
||||
const float * voxelData = (float *)_memoryMap.data();
|
||||
const long long headerOffset = dataPosition() / sizeof(float);
|
||||
|
||||
std::vector<float> averages(numTotalNodes_);
|
||||
@@ -301,22 +333,17 @@ std::vector<float> TSP::calculateBrickAverages() {
|
||||
averages[brick] = average / static_cast<double>(numBrickVals);
|
||||
}
|
||||
|
||||
mfile.close();
|
||||
|
||||
return averages;
|
||||
}
|
||||
|
||||
std::vector<float> TSP::calculateBrickStdDevs(std::vector<float> brickAverages) {
|
||||
const unsigned int numBrickVals = paddedBrickDim_*paddedBrickDim_*paddedBrickDim_;
|
||||
|
||||
boost::iostreams::mapped_file_source mfile;
|
||||
mfile.open(_filename);
|
||||
|
||||
if (!mfile.is_open()) {
|
||||
if (!openMemoryMap()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const float * voxelData = (float *)mfile.data();
|
||||
const float * voxelData = (float *)_memoryMap.data();
|
||||
const long long headerOffset = dataPosition() / sizeof(float);
|
||||
|
||||
std::vector<float> stdDevs(numTotalNodes_);
|
||||
@@ -365,8 +392,6 @@ std::vector<float> TSP::calculateBrickStdDevs(std::vector<float> brickAverages)
|
||||
stdDevs[brick] = stdDev;
|
||||
}
|
||||
|
||||
mfile.close();
|
||||
|
||||
return stdDevs;
|
||||
}
|
||||
|
||||
@@ -418,14 +443,11 @@ bool TSP::calculateSpatialError() {
|
||||
|
||||
bool TSP::calculateTemporalError() {
|
||||
|
||||
boost::iostreams::mapped_file_source mfile;
|
||||
mfile.open(_filename);
|
||||
|
||||
if (!mfile.is_open()) {
|
||||
if (!openMemoryMap()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const float * voxelData = (float *)mfile.data();
|
||||
const float * voxelData = (float *)_memoryMap.data();
|
||||
const long long headerOffset = dataPosition() / sizeof(float);
|
||||
|
||||
LDEBUG("Calculating temporal error");
|
||||
@@ -488,7 +510,6 @@ bool TSP::calculateTemporalError() {
|
||||
errors[brick] = avgStdDev;
|
||||
|
||||
} // for all bricks
|
||||
mfile.close();
|
||||
|
||||
std::sort(meanArray.begin(), meanArray.end());
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/*****************************************************************************************
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
@@ -33,6 +33,10 @@
|
||||
|
||||
#include <ghoul/opengl/ghoul_gl.h>
|
||||
|
||||
#include <boost/iostreams/device/mapped_file.hpp>
|
||||
|
||||
// Forward declare boost memory map
|
||||
|
||||
namespace openspace {
|
||||
class TSP {
|
||||
public:
|
||||
@@ -66,6 +70,14 @@ public:
|
||||
TSP(const std::string& filename);
|
||||
~TSP();
|
||||
|
||||
bool openFile();
|
||||
|
||||
bool closeFile();
|
||||
|
||||
bool openMemoryMap();
|
||||
|
||||
bool closeMemoryMap();
|
||||
|
||||
// load performs readHeader, readCache, writeCache and construct
|
||||
// in the correct sequence
|
||||
bool load();
|
||||
@@ -131,12 +143,15 @@ protected:
|
||||
std::ifstream _file;
|
||||
std::streampos _dataOffset;
|
||||
|
||||
boost::iostreams::mapped_file _memoryMap;
|
||||
|
||||
// Holds the actual structure
|
||||
std::vector<int> data_;
|
||||
GLuint _dataSSBO;
|
||||
|
||||
// Data from file
|
||||
Header _header;
|
||||
size_t _filesize;
|
||||
|
||||
// Additional metadata
|
||||
unsigned int paddedBrickDim_;
|
||||
|
||||
Reference in New Issue
Block a user