41 #ifndef __VMML__TENSOR_MMAPPER__HPP__
42 #define __VMML__TENSOR_MMAPPER__HPP__
44 #include "tensor4.hpp"
55 template<
typename T,
typename C >
63 tensor_mmapper(
const std::string& dir_,
const std::string& filename_,
const bool prot_read_,
const C& tx_converter );
66 void get_tensor( T& tensor_ ) { tensor_ = *_tensor; }
69 static std::string concat_path(
const std::string& dir_,
const std::string& filename_ );
71 void* t3_allocate_rd_mmap(
const std::string& dir_,
const std::string& filename_ );
72 void* t3_allocate_wr_mmap(
const std::string& dir_,
const std::string& filename_,
const C& tx_converter );
75 HANDLE _fd, _fd_mmapped;
91 #define VMML_TEMPLATE_STRING template< typename T, typename C >
92 #define VMML_TEMPLATE_CLASSNAME tensor_mmapper< T, C >
115 VMML_TEMPLATE_CLASSNAME::~tensor_mmapper()
119 _tensor->clear_array_pointer();
123 munmap( _data, _file_size );
129 VMML_TEMPLATE_CLASSNAME::tensor_mmapper(
const std::string& dir_,
const std::string& filename_,
const bool prot_read_,
const C& tx_converter_ )
130 : _file_size(0), _data(0), _tensor(0)
133 _file_size = T::get_array_size_in_bytes();
136 t3_allocate_rd_mmap( dir_, filename_ );
138 t3_allocate_wr_mmap( dir_, filename_, tx_converter_ );
143 _tensor =
new T( _data );
151 VMML_TEMPLATE_CLASSNAME::
152 t3_allocate_rd_mmap(
const std::string& dir_,
const std::string& filename_ )
155 std::string path = concat_path( dir_, filename_ );
157 _fd = CreateFile( path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
158 if( _fd == INVALID_HANDLE_VALUE )
166 _fd_mmapped = (
void* )CreateFileMapping( _fd, 0, PAGE_READONLY, 0, 0, NULL );
167 if (_fd_mmapped == NULL )
169 std::cout <<
"CreateFileMapping failed" << std::endl;
173 _data = MapViewOfFile( _fd_mmapped, FILE_MAP_READ, 0, 0, 0 );
176 std::cout <<
"MapViewOfFile failed" << std::endl;
182 _fd = open( path.c_str(), O_RDONLY, 0 );
187 std::cout <<
"no file open for memory mapping" << std::endl;
195 _data = (
void*)mmap( 0, _file_size, PROT_READ, MAP_FILE | MAP_SHARED, _fd, offset );
197 if( _data == MAP_FAILED)
199 std::cout <<
"mmap failed" << std::endl;
209 VMML_TEMPLATE_CLASSNAME::
210 t3_allocate_wr_mmap(
const std::string& dir_,
const std::string& filename_,
const C& tx_converter_ )
214 std::string path = concat_path( dir_, filename_ );
216 _fd = CreateFile( path, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
217 if( _fd == INVALID_HANDLE_VALUE )
225 tx_converter_.write_to_raw( *_tensor, dir_, filename_ );
227 _fd = CreateFile( path, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
228 if( _fd == INVALID_HANDLE_VALUE )
231 std::cout <<
"no file open for memory mapping" << std::endl;
238 _fd_mmapped = (
void* )CreateFileMapping( _fd, 0, PAGE_READWRITE, 0, 0, NULL );
239 if (_fd_mmapped == NULL )
241 std::cout <<
"CreateFileMapping failed" << std::endl;
245 _data = MapViewOfFile( _fd_mmapped, FILE_MAP_WRITE, 0, 0, 0 );
248 std::cout <<
"MapViewOfFile failed" << std::endl;
255 _fd = open( path.c_str(), O_RDWR, 0 );
263 tx_converter_.write_to_raw( *_tensor, dir_, filename_ );
265 _fd = open( path.c_str(), O_RDWR, 0 );
269 std::cout <<
"no file open for memory mapping" << std::endl;
276 _data = (
void*)mmap( 0, _file_size, PROT_WRITE, MAP_FILE | MAP_SHARED, _fd, offset );
278 if( _data == MAP_FAILED)
280 std::cout <<
"mmap failed" << std::endl;
290 VMML_TEMPLATE_CLASSNAME::concat_path(
const std::string& dir_,
const std::string& filename_ )
292 std::string path =
"";
293 int dir_length = dir_.size() -1;
295 int last_separator = dir_.find_last_of(
"\\");
297 int last_separator = dir_.find_last_of(
"/");
300 if (last_separator < dir_length ) {
307 path.append( filename_ );
310 if( filename_.find(
"raw", filename_.size()-3 ) == std::string::npos )
313 path.append(
"raw" );
320 #undef VMML_TEMPLATE_STRING
321 #undef VMML_TEMPLATE_CLASSNAME