23
23
import java .io .ByteArrayInputStream ;
24
24
import java .nio .ByteBuffer ;
25
25
import java .util .HashMap ;
26
- import java .util .UUID ;
27
26
28
27
import com .sun .jna .Pointer ;
29
28
58
57
*/
59
58
public final class SharedMemoryArrayLinux implements SharedMemoryArray
60
59
{
60
+ /**
61
+ * Instance of the CLibrary JNI containing the methods to interact with the Shared memory segments
62
+ */
61
63
private static final CLibrary INSTANCE = CLibrary .INSTANCE ;
62
64
/**
63
- * Shared memory location
65
+ * File descriptor value of the shared memory segment
64
66
*/
65
67
private final int shmFd ;
66
68
/**
67
- *
69
+ * Pointer referencing the shared memory byte array
68
70
*/
69
71
private Pointer pSharedMemory ;
70
72
/**
71
- * Name defining the location of the shared memory block
73
+ * Name of the file containing the shared memory segment. In Unix based systems consits of "/" + file_name.
74
+ * In Linux the shared memory segments can be inspected at /dev/shm
72
75
*/
73
76
private final String memoryName ;
74
77
/**
75
78
* Size of the shared memory block
76
79
*/
77
80
private int size ;
78
81
/**
79
- * Datatype of the shm array
82
+ * Shared memory segments store bytes. This field represents the original data type of the array that was written
83
+ * into the bytes of the shared memory segment. It is helful to retrieve the object later.
80
84
*/
81
85
private final String originalDataType ;
82
86
/**
83
- * Original dimensions of the shm array
87
+ * Shared memory segments are flat arrays, only one dimension. This field keeps the dimensions of the array before
88
+ * flattening it and copying it to the shared memory.
84
89
*/
85
90
private final long [] originalDims ;
86
91
/**
@@ -93,19 +98,39 @@ public final class SharedMemoryArrayLinux implements SharedMemoryArray
93
98
* of bytes corresponding to the values of the array, no header
94
99
*/
95
100
private boolean isNumpyFormat = false ;
96
-
97
- public static final int O_RDONLY = 0 ;
98
- static final int O_RDWR = 2 ; // Read-write mode
99
- static final int O_CREAT = 64 ; // Create if it does not exist
100
- private static final int PROT_READ = 0x1 ; // Page can be read
101
- private static final int PROT_WRITE = 0x2 ; // Page can be written
102
- private static final int MAP_SHARED = 0x01 ; // Share changes
103
101
102
+ /**
103
+ * Create a shared memory segment with the wanted size, where an object of a certain datatype and
104
+ * share is going to be stored.
105
+ * Unless the array of bytes that is going to be written into the shared memory segment has numpy format,
106
+ * the size parameter should only depend on the shape and the data type.
107
+ * The name of the file containing the shared memory segment is assigned automatically.
108
+ * @param size
109
+ * number of bytes that are going to be written into the shared memory
110
+ * @param dtype
111
+ * data type of the object that is going to be written into the shared memory
112
+ * @param shape
113
+ * shape (array dimensions) of the array that is going to be flattened and written into the shared memory segment
114
+ */
104
115
private SharedMemoryArrayLinux (int size , String dtype , long [] shape )
105
116
{
106
- this ("/shm-" + UUID . randomUUID (), size , dtype , shape );
117
+ this (SharedMemoryArray . createShmName (), size , dtype , shape );
107
118
}
108
-
119
+
120
+ /**
121
+ * Create a shared memory segment with the wanted size, where an object of a certain datatype and
122
+ * share is going to be stored. The shared memory name is created in the location of the name provided
123
+ * Unless the array of bytes that is going to be written into the shared memory segment has numpy format,
124
+ * the size parameter should only depend on the shape and the data type.
125
+ * @param name
126
+ * name of the file name that is going to be used to identify the shared memory segment
127
+ * @param size
128
+ * number of bytes that are going to be written into the shared memory
129
+ * @param dtype
130
+ * data type of the object that is going to be written into the shared memory
131
+ * @param shape
132
+ * shape (array dimensions) of the array that is going to be flattened and written into the shared memory segment
133
+ */
109
134
private SharedMemoryArrayLinux (String name , int size , String dtype , long [] shape )
110
135
{
111
136
this .originalDataType = dtype ;
@@ -130,22 +155,30 @@ private SharedMemoryArrayLinux(String name, int size, String dtype, long[] shape
130
155
}
131
156
}
132
157
133
- public String getMemoryLocationName () {
158
+ /**
159
+ * {@inheritDoc}
160
+ */
161
+ public String getName () {
134
162
return this .memoryName ;
135
163
}
136
164
137
- public String getMemoryLocationPythonName () {
165
+ /**
166
+ * {@inheritDoc}
167
+ */
168
+ public String getNameForPython () {
138
169
return this .memoryName .substring ("/" .length ());
139
170
}
140
171
172
+ /**
173
+ * {@inheritDoc}
174
+ */
141
175
public Pointer getPointer () {
142
176
return this .pSharedMemory ;
143
177
}
144
178
145
- public int getSharedMemoryBlock () {
146
- return this .shmFd ;
147
- }
148
-
179
+ /**
180
+ * {@inheritDoc}
181
+ */
149
182
public int getSize () {
150
183
return this .size ;
151
184
}
@@ -167,7 +200,7 @@ public int getSize() {
167
200
*/
168
201
protected static <T extends RealType <T > & NativeType <T >> SharedMemoryArrayLinux build (RandomAccessibleInterval <T > rai )
169
202
{
170
- return build ("/shm-" + UUID . randomUUID (), rai );
203
+ return build (SharedMemoryArray . createShmName (), rai );
171
204
}
172
205
173
206
/**
@@ -262,7 +295,7 @@ protected static <T extends RealType<T> & NativeType<T>> SharedMemoryArrayLinux
262
295
*/
263
296
protected static <T extends RealType <T > & NativeType <T >> SharedMemoryArrayLinux buildNumpyFormat (RandomAccessibleInterval <T > rai )
264
297
{
265
- return buildNumpyFormat ("/shm-" + UUID . randomUUID (), rai );
298
+ return buildNumpyFormat (SharedMemoryArray . createShmName (), rai );
266
299
}
267
300
268
301
/**
0 commit comments