-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatablock_host_memory_functions.h
More file actions
147 lines (116 loc) · 3.7 KB
/
datablock_host_memory_functions.h
File metadata and controls
147 lines (116 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#ifndef DATABLOCKHOSTMEMHELPERS
#define DATABLOCKHOSTMEMHELPERS
#include <filesystem>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
template<typename T>
class DataBlock_Host_Memory_Functions
{
public:
inline static void free_copy(DataBlock<T>&m, bool with_memmap);
inline static DataBlock<T> alloc_data_copy_strides_extents(size_t datalength,bool rowmajor, size_t rank, size_t*extents,size_t *strides, bool with_memmap);
inline static T* alloc_data_ptr(size_t length,bool create_memmap);
inline static void free_data_ptr(T*&pdata,size_t datalength,bool with_memmap);
inline static T* create_temp_mmap(const size_t array_size);
inline static void delete_temp_mmap(T* &mmap_ptr,const size_t array_size);
};
template<typename T>
T* DataBlock_Host_Memory_Functions<T>::create_temp_mmap(const size_t array_size)
{
size_t file_size = array_size * sizeof(T);
// Create a temporary file using std::tmpfile()
FILE* tmpf = tmpfile();
if (!tmpf)
{
perror("tmpfile");
return NULL;
}
// Get the file descriptor from the FILE*
int fd = fileno(tmpf);
if (fd == -1)
{
perror("fileno");
fclose(tmpf);
return NULL;
}
// Resize the file to the required size
if (ftruncate(fd, file_size) == -1)
{
perror("ftruncate");
fclose(tmpf);
return NULL;
}
// Memory map the file
T* mmap_ptr = (T*)mmap(NULL, file_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (mmap_ptr == MAP_FAILED)
{
perror("mmap");
fclose(tmpf);
return NULL;
}
// Close the FILE* but keep the memory mapping valid
fclose(tmpf);
// Return the pointer to the mapped memory
return mmap_ptr;
}
template<typename T>
void DataBlock_Host_Memory_Functions<T>::delete_temp_mmap(T* &mmap_ptr,const size_t array_size)
{
size_t file_size = array_size * sizeof(T);
if (mmap_ptr!=nullptr)
if (munmap(mmap_ptr, file_size) == -1)
{
perror("munmap");
}
}
template<typename T>
void DataBlock_Host_Memory_Functions<T>::free_data_ptr(T*&pdata,size_t datalength,bool with_memmap)
{
if(pdata!=nullptr)
{
if (with_memmap)
DataBlock_Host_Memory_Functions<T>::delete_temp_mmap(pdata,datalength);
else
if(pdata!=nullptr)
free(pdata);
}
}
template<typename T>
T* DataBlock_Host_Memory_Functions<T>::alloc_data_ptr(size_t length,bool create_memmap)
{
if (create_memmap)
return DataBlock_Host_Memory_Functions<T>::create_temp_mmap(length);
else
return (T*) malloc(sizeof(T)*length);
}
template<typename T>
DataBlock<T> DataBlock_Host_Memory_Functions<T>::alloc_data_copy_strides_extents(size_t datalength,bool rowmajor, size_t rank, size_t*extents,size_t *strides, bool with_memmap)
{
size_t*pextents;
size_t*pstrides;
T* pdata;
pextents=(size_t*) malloc(sizeof(size_t)*rank);
memcpy(pextents,extents,sizeof(size_t)*rank);
pstrides=(size_t*) malloc(sizeof(size_t)*rank);
memcpy(pstrides,strides,sizeof(size_t)*rank);
if (with_memmap)
pdata=DataBlock_Host_Memory_Functions<T>::create_temp_mmap(datalength);
else
pdata=(T*)malloc(sizeof(T)*datalength);
return DataBlock<T>(pdata,datalength,rowmajor,rank,pextents,pstrides,false, false,false);
}
template<typename T>
void DataBlock_Host_Memory_Functions<T>::free_copy(DataBlock<T>&m, bool with_memmap)
{
if(m.dpextents!=nullptr)
free(m.dpextents);
if(m.dpstrides!=nullptr)
free(m.dpstrides);
if (with_memmap)
DataBlock_Host_Memory_Functions<T>::delete_temp_mmap(m.dpdata,m.dpdatalength);
else
if(m.dpdata!=nullptr)
free(m.dpdata);
}
#endif